How do you implement sampling on the MCP server side?
MCP Advanced: Sampling, Server-to-Client LLM Requests, Cost Shifting Explained
Audio flashcard · 0:29Nortren·
How do you implement sampling on the MCP server side?
0:29
On the server side, you use the create_message method available through the Context object in your tool function. You construct a SamplingMessage with a role of user and a TextContent object containing your prompt. You pass this message along with parameters like max_tokens and an optional system_prompt to the create_message call on the context session. The method returns a result object whose content property contains the generated text. If the content type is text, you extract and return it. This keeps the server code minimal since all model interaction happens on the client side.
github.com