AI Hints
Home›MCP Tutorial›Caching and Changes
MCP · Lesson 83 of 120

Roots

Where the client allows.

⏱ 2 min read▶ Video at 1:14:11🎓 Free
Watch this lesson in the MCP courseWatch on YouTube ↗Full MCP course

Older MCP versions had roots. The client told the server which folders it was allowed to use. In the newest revision, roots are deprecated, so new servers shouldn't rely on them.

Roots
From the video: Roots
  • Roots: allowed folders
  • Sent by the client
  • Now deprecated

Folders from config instead

Today, give a server its allowed folder through configuration. The server reads one folder from the environment, and refuses anything outside it. Run it. The help page is read, and the path outside is refused.

allowed_folder.pypython
import os
from pathlib import Path
from mcp.server.mcpserver import MCPServer
from mcp.server.mcpserver.exceptions import ToolError
from tester import call

ALLOWED = Path(os.environ.get("DOCS_DIR", "data/rag/northwind")).resolve()
mcp = MCPServer("docs")

@mcp.tool()
def read_doc(name: str) -> str:
    """Read one Northwind help page."""
    path = (ALLOWED / name).resolve()
    if not path.is_relative_to(ALLOWED):
        raise ToolError("outside the allowed folder")
    return path.read_text().splitlines()[2]

for name in ["stores.md", "../../secrets.txt"]:
    print(name, "->", call(mcp, "read_doc", {"name": name}).content[0].text)
Output
stores.md -> Northwind Bikes has three stores.
../../secrets.txt -> Error executing tool read_doc: outside the allowed folder
Also uses tester.py
import asyncio
from mcp import Client

def call(server, tool, args=None, **options):
    async def go():
        async with Client(server, **options) as client:
            return await client.call_tool(tool, args or {})
    return asyncio.run(go())

def tools(server):
    async def go():
        async with Client(server) as client:
            return (await client.list_tools()).tools
    return asyncio.run(go())

Summary

Roots are deprecated. Pass allowed folders through configuration, and check every path. Next, the other deprecated features.

Key takeaways
  • Roots deprecated
  • Config instead
  • Check paths
Next up: Deprecated Features — logging and sampling