Skip to content
Back to log

How do MCP roots and sampling work?

Chainabit's Chao · · 7 min read

In short

In MCP, roots define file system boundaries, while sampling lets a server request model-generated output through a client. Learn how each workflow handles messages and what limits apply.

What are roots and sampling used for?

Model Context Protocol (MCP) defines separate workflows for roots and sampling between a client and a server. Roots let the client tell the server where it can work in the file system. Sampling lets the server request output from a language model through a client.

Under the MCP roots specification, a server can request the list of roots from a client that supports the feature. It can also receive a notification when the list changes. A root helps the server understand which directories and files it can access in the file system.

Under the MCP sampling specification, the client retains control over model access, model selection, and permissions. The server can request interactions involving text, audio, or images. It can also choose to include context from MCP servers in the prompt.

How is the list of roots exposed?

MCP notes that roots are typically exposed through workspace or project configuration interfaces. An application can provide a workspace or project picker so users can choose which directories and files the server can access. The application can combine this selection with automatic workspace detection based on version control systems or project files.

The protocol leaves the choice of user interface for roots to each application. Applications can use a layout that suits their needs. How users manage roots depends on the layout the application chooses.

A client that supports roots must declare the roots capability at the start of the connection. The listChanged field in that capability indicates whether the client will send a notification when the list of roots changes. Its value indicates support for change notifications. The list itself is provided separately.

The server sends a roots/list request to retrieve the list. In MCP's example request, the method field is set to roots/list. In the client's response, root definitions appear in the roots field within result.

The uri in a root definition is the root's unique identifier. The MCP roots specification requires it to be a file:// URI. The optional name field provides a human-readable name for display. The name and URI are separate fields in the definition.

A root can point to a single project directory. The list can also contain separate root definitions for multiple repositories. In MCP's example, the frontend and backend repositories each have their own URI and name.

When the roots change, a client with listChanged support must send a notifications/roots/list_changed notification. This tells the server that the list has changed. The roots/list request retrieves the list, while notifications/roots/list_changed is a separate message that reports a change.

Permissions and error cases for roots

MCP's roots security rules require the client to expose only roots with appropriate permissions. The client must also validate all root URIs to prevent path traversal, apply appropriate access controls, and monitor root availability.

MCP recommends that the client ask for user approval before exposing roots to the server. Other implementation recommendations include providing clear interfaces for managing roots, checking their availability before exposing them, and monitoring changes.

On the server side, MCP recommends respecting root boundaries during operations and validating all paths against the roots the client exposes. It also calls for servers to handle roots that become unavailable. The implementation guidance includes checking capability support before using roots and caching root information as appropriate.

If the client does not support roots, it can return a standard JSON-RPC error response. MCP lists unsupported methods and internal errors as common examples. In the example error response, error contains code, message, and data fields. The data field can include a reason indicating that the client does not support the roots capability.

How does a sampling request work?

In MCP's sampling workflow, the server initiates the request. It sends a sampling/createMessage request to the client to ask a language model to generate output. A client that supports sampling must declare the sampling capability at the start of the connection.

Sampling allows model calls to be embedded in other MCP server features. MCP notes that this mechanism lets servers implement agentic behavior. As with roots, the protocol leaves the choice of user interface for sampling to each application.

For trust and safety, MCP recommends keeping a human in the loop who can reject a sampling request. It also recommends that applications provide an interface that makes sampling requests easy to review. The same interaction model lets users view and edit prompts before they are sent and review generated responses before delivery.

Turn this insight into action.

Use the mobile app to continue with guided workflows.

In the example request, params contains the messages, modelPreferences, systemPrompt, and maxTokens fields. Each message in messages has role and content fields. The example message sets role to user. For text content, content contains type and text.

Sampling messages can contain text, images, or audio. MCP's image and audio examples include type, data, and mimeType fields. In these examples, data holds base64-encoded image or audio data. In the text example, the content appears in the text field.

In MCP's example response, result contains role, content, model, and stopReason. The example sets role to assistant and the content type to text. Along with the generated content, the response has separate fields for the model and the reason generation stopped.

If the user rejects a sampling request, the client can return an error response. In MCP's example for this case, the error object contains code and message fields. The message indicates that the user rejected the sampling request.

How are model preferences expressed?

MCP notes that the server and client may use different model providers and have different models available. The client may lack access to the model the server names or may prefer an equivalent model from another provider. To accommodate this, sampling defines a preference system that combines capability priorities with optional model hints.

The server can express its needs through three priority fields. costPriority indicates how important it is to minimize cost, with higher values favoring lower-cost models. speedPriority indicates how important low latency is, with higher values favoring faster models. intelligencePriority indicates how important advanced capabilities are, with higher values favoring more capable models.

The hints field lets the server suggest specific models or model families. Hints are treated as substrings that can be matched flexibly against model names. When there are multiple hints, they are evaluated in order of preference. The client can also map a hint to an equivalent model from a different provider.

These hints are advisory. The client makes the final model selection and takes the stated preferences into account when choosing from the options it can access. MCP also recommends that clients honor model preference hints.

Boundaries and security

The client declares support for roots and sampling through separate capabilities at the start of the connection. A client that lacks roots support can return an error for the method. The user can also reject a sampling request. Retrieving the list of roots and generating output with a language model are separate workflows with distinct requests.

MCP's sampling security guidance recommends that clients implement user approval controls and rate limiting. It also recommends that both clients and servers validate message content. Both sides must handle sensitive data appropriately.

The client retains control over model access and selection even when the server suggests a specific model by name. According to MCP, if the client lacks access to the suggested model, it chooses from the options it can access while taking the preferences into account. Model hints are advisory, and the final choice rests with the client.

Root availability can change. MCP recommends that the server handle roots that become unavailable and requires the client to monitor root availability. It also separately recommends that the client check availability before exposing roots.

Users interact with the two workflows in different ways. In the roots example, users can use a picker to choose which directories and files the server can access. In MCP's recommended sampling interaction, users can view and edit the prompt before it is sent, reject the request, and review the generated response before delivery.

The content structure of a sampling message depends on its type. In MCP's text example, type is set to text and the content field is text. In the image example, type is set to image, and in the audio example it is set to audio. Both examples have separate fields for the data and the media type.

In the response that lists roots, each root has its own uri value, and name is optional for display. In the example with multiple repositories, each repository appears as a separate root object. Support for list change notifications is declared at the capability level, in the roots capability the client announces at the start of the connection.

Ready to start?

Download the app and continue from your phone.

End of Entry