Whatever message this page gives is out now! Go check it out!

Sampling

Last update:
May 18, 2026
Enable your MCP client to handle server-initiated LLM sampling requests.
Sampling is used when the MCP server wants your client to call its own LLM and return the completion back to the server.
Servers send sampling requests with a messages array; your handler decides how to respond and which model to use.
Sampling Request Format (from server):

{
    "messages": [
        {
            "role": "user",
            "content": {
                "type": "text",
                "text": "What is the weather in New York?"
            }
        }
    ]
}
            
Struct Response (with model name):

return {
    "modelname": "gpt-4o",
    "result": "The weather in New York is sunny, 75°F"
};
            
  1. Define your sampling handler function.
    ADDITIONAL INFORMATION:

<cfscript>

function mySamplingHandler(request) {

    var messages = request.messages;
    var userMessage = messages[1].content.text;

    var llmResponse = callMyLLM(userMessage);

    return {
        modelname: "gpt-4o",
        result: llmResponse
    };
}

</cfscript>
                    
  1. Configure MCP client with sampling enabled.
    ADDITIONAL INFORMATION:

<cfscript>

configData = {

    transport: { /* transport config */ },

    capabilities: {
        sampling: true
    },

    samplingConsumer: mySamplingHandler
};

mcpClient = MCPClient(configData);

</cfscript>
                    
Result
Your MCP client can now receive sampling requests, call your LLM, and return structured results to the server.

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page