# What are MCP roots?
Source: https://speakeasy.com/mcp/core-concepts/roots

MCP roots are context-defining [URIs](https://en.wikipedia.org/wiki/Uniform_Resource_Identifier) that establish operational boundaries for MCP servers. They function as "safe zones" or "allowed directories" that an AI agent can access when interacting with your system. When an MCP client provides roots for servers, it's essentially saying, "You're allowed to work within these specific areas."

While roots are sometimes described as a **security** feature, the MCP Specification does not dictate how servers must implement root handling. So when the client tells the server which areas it can work within, the server may ignore this rule and still be within the specification.

Any suggestion that roots provide security needs to assume that the server will respect the boundaries set by the client.

## Why are MCP roots useful?

MCP roots allow the user (or an LLM) to control **where** a server should focus or within which boundaries a server should operate.

For example, if a user has a stock-trading MCP server, they may want their client to limit the trading server to only certain stocks or exchanges.

In this example, the client will prompt the user to select a list of stocks and exchanges, and then notify the server that the list of roots has changed. The server will then request the new roots, and should limit its actions to the selected list of stocks and exchanges.

![Roots](/assets/mcp/building-servers/roots-diagram.png)

Roots help in several important ways.

- **Security**: If a server is known to respect root boundaries, roots act like fences that keep the server from accessing things it shouldn't.
- **Focus**: Roots guide servers to look only at relevant information, like giving them a map of where to search.
- **Performance**: By limiting where a server can look, the server may run faster and with less searching or filtering logic.
- **Trust**: Users feel more comfortable knowing servers can only access specific areas they've approved. This still depends on the server's implementation of roots.

MCP roots can point to different kinds of places:

- **Folders on your computer**: `file:///home/user/projects/myapp`
- **Websites and APIs**: `https://api.example.com/v1`
- **Database connections**: `db://mycompany/customers`
- **Special locations**: `note://meeting-notes` or `config://app-settings`

## How MCP roots work

Here's how MCP roots could work in practice:

1. **Client to the server: _"Hello, I can use roots!"_** - The client tells the server it supports roots when they first connect.
2. **Server to the client: _"What roots do you have?"_** - The server asks the client for available roots using `roots/list`.
3. **Client to the server: _"Stay within these boundaries."_** - The client returns a list of roots.
4. **User to the client: _"I want to change the boundaries."_** - The user updates the boundaries the client and server should focus on.
5. **Client to the server: _"Roots have changed."_** - If needed, the client can tell the server when roots are added or removed using `notifications/roots/list_changed`.
6. **Server to the client: _"What roots do you have?"_** - The server requests a list of roots using `roots/list`.
7. **Client to the server: _"Stay within these boundaries."_** - The client responds with a list of roots.

It bears repeating that while the client owns and defines the list of allowed roots, the server **should** stay within those boundaries but is free to implement root handling how it wants. The MCP Specification does not enforce that servers respect those boundaries. It only standardizes how roots are communicated.

Here's an example of a roots list response from a client:

```json
{
  "roots": [
    {
      "uri": "file:///home/user/projects/myapp",
      "name": "My Application Code"
    },
    {
      "uri": "https://api.example.com/v1",
      "name": "Example API Endpoint"
    }
  ]
}
```

The `uri` indicates the precise resource location, while the optional `name` field gives the root an identifier that can be used by an LLM.

## Implementing MCP roots in Python

Let's look at examples of how to implement MCP roots using [FastMCP](https://github.com/jlowin/fastmcp/):

### Server implementation

```python filename="server-example.py"
from fastmcp import FastMCP
import asyncio
from mcp.server.stdio import stdio_server
from mcp.server import InitializationOptions, NotificationOptions