Sampling (Server-Initiated)
Sampling is an advanced feature of the Model Context Protocol that allows a Server to ask the Client to run a model inference. This inverted pattern is useful for servers that need the model's "opinion" or "reasoning" during a complex task.
Use Case
A "Code Review Server" might use sampling to ask the model: "Given this diff, write a concise comment about the performance implications." The model's response is then returned to the server to complete the review action.
Sampling enables multi-step "agentic loops" where the server orchestrates the reasoning process.
Questions & Answers
What is "Sampling" in the context of the Model Context Protocol?
Sampling is a specialized capability that allows an MCP server to request text generation or completion from the client's model. It is used when a server needs the model's "opinion" or "reasoning" to complete a task.
Why is sampling considered an "inverted pattern" in MCP?
Typically, the host (client) makes requests to the server for tools or resources. with sampling, the pattern is reversed: the server initiates a request to the client, asking the client's AI model to perform an inference.
How does sampling enable multi-step "agentic loops"?
By allowing the server to ask the model for reasoning during a task, sampling facilitates a loop where the server orchestrates complex actions, retrieves data, asks the model for analysis, and then proceeds based on that analysis.