← Back to ToolBench
Client Features
Sampling
Server-initiated LLM completions through the client
What it does
Sampling allows an MCP server to request LLM completions through the client, enabling agentic behaviors where the server can leverage AI reasoning. The server sends sampling/createMessage to the client with a prompt, and the client returns the LLM's response. This creates a human-in-the-loop pattern where the client controls which model is used and can approve requests before sending them.
How it works
Example implementation
const result = await server.requestSampling({
messages: [{ role: "user", content: { type: "text", text: "Summarize this data" } }],
modelPreferences: { hints: [{ name: "claude-3-5-sonnet" }] },
maxTokens: 500
});