Provide a sampling_handler function when creating the client:
Copy
from fastmcp import Clientfrom fastmcp.client.sampling import ( SamplingMessage, SamplingParams, RequestContext,)async def sampling_handler( messages: list[SamplingMessage], params: SamplingParams, context: RequestContext) -> str: # Your LLM integration logic here # Extract text from messages and generate a response return "Generated response based on the messages"client = Client( "my_mcp_server.py", sampling_handler=sampling_handler,)
from fastmcp import Clientfrom fastmcp.client.sampling import SamplingMessage, SamplingParams, RequestContextasync def basic_sampling_handler( messages: list[SamplingMessage], params: SamplingParams, context: RequestContext) -> str: # Extract message content conversation = [] for message in messages: content = message.content.text if hasattr(message.content, 'text') else str(message.content) conversation.append(f"{message.role}: {content}") # Use the system prompt if provided system_prompt = params.systemPrompt or "You are a helpful assistant." # Here you would integrate with your preferred LLM service # This is just a placeholder response return f"Response based on conversation: {' | '.join(conversation)}"client = Client( "my_mcp_server.py", sampling_handler=basic_sampling_handler)
If the client doesn’t provide a sampling handler, servers can optionally configure a fallback handler. See Server Sampling for details.
When you provide a sampling_handler, FastMCP automatically advertises full sampling capabilities to the server, including tool support. To disable tool support (for simpler handlers that don’t support tools), pass sampling_capabilities explicitly:
Copy
from mcp.types import SamplingCapabilityclient = Client( "my_mcp_server.py", sampling_handler=basic_handler, sampling_capabilities=SamplingCapability(), # No tool support)
FastMCP provides built-in sampling handlers for OpenAI and Anthropic APIs. These handlers support the full sampling API including tool use, handling message conversion and response formatting automatically.
Tool execution happens on the server side. The client’s role is to pass tools to the LLM and return the LLM’s response (which may include tool use requests). The server then executes the tools and may send follow-up sampling requests with tool results.
To implement a custom sampling handler, see the handler source code as a reference.