Sampling & Roots — Advanced Features
Sampling & Roots
Reversing the Flow (Sampling)
Normally, the Client asks the Server for data. Sampling (sampling/createMessage) reverses this: the Server asks the Client's LLM to generate text or invoke nested tools (tools and toolChoice added in 2025-11-25) on its behalf!
This enables agentic sub-workflows without servers holding their own LLM API keys. Because requesting LLM completions implies cost and untrusted prompt exposure, MCP mandates Human-in-the-Loop (HITL) approval via the Client UI.
Establishing Roots & Spec 2026-07-28 Lifecycle (SEP-2577)
Roots (roots/list) define the operational filesystem or URI boundaries of an MCP Server. Lifecycle Note: In the 2026-07-28 specification (SEP-2577), Roots, Sampling, and Logging entered a formal 12-month deprecation window so the core protocol remains stateless—new servers should accept allowed paths via explicit tool parameters/resource URIs, call LLM APIs directly when feasible, and emit telemetry via stderr or OpenTelemetry (while stateless Multi Round-Trip Requests / MRTR handle remaining interactive client round-trips).
// Legacy/Transitional Server: Requesting declared client roots (never log to stdout!)
const rootList = await server.server.listRoots();
console.error("Authorized roots:", rootList.roots); // e.g. [{ uri: "file:///usr/src/app" }]
Query the client's declared root directories to enforce filesystem sandboxing boundaries.