MCP Sampling
Sampling allows MCP servers to request LLM completions from clients. This enables servers to leverage the client’s LLM capabilities for text generation, with support for tool calling.Protocol Version
This feature implements MCP Protocol Version 2025-11-25.Tool Choice Modes
| Mode | Description |
|---|---|
auto | Model decides whether to use tools |
none | Model should not use tools |
any | Model must use at least one tool |
tool | Model must use a specific tool |
Python API
Basic Sampling
Sampling with Tools
Tool Choice Factory Methods
Model Preferences
Custom Callback
MCP Protocol Messages
Sampling Request
Sampling Response
Response with Tool Use
Stop Reasons
| Reason | Description |
|---|---|
end_turn | Model finished naturally |
max_tokens | Hit token limit |
toolUse | Model wants to use a tool |
error | An error occurred |
Best Practices
- Set appropriate max_tokens - Avoid unnecessary token usage
- Use model preferences - Guide model selection
- Handle tool calls - Process and respond to tool use
- Provide system prompts - Set context for better responses
Related
- MCP Tasks API - Long-running operations
- MCP Elicitation - User input
- PraisonAI MCP Server - Full documentation

