A file-reading tool inside an MCP server is a small piece of code with a large blast radius: it is the function an AI agent calls with a string the model just composed. I wanted to know how bad the default version is, so I built three MCP servers — naive, checked, and one that crashes on purpose — and drove them over real stdio JSON-RPC (tools/list and tools/call) instead of calling the Python functions directly.

Environment: Python 3.12.16, MCP SDK 2.3.0, a 2-vCPU container, a work/ folder holding a fake .env with three keys and two data files, and a project root above it.

The six inputs

All six were sent as the path argument of a read_file tool. The naive server built paths with Path(root) / path; the checked server resolved and verified them before opening anything.

input naive join resolve() + is_relative_to
data/a.txt read read
../.env read DENY
exposed/../../.env read DENY
link/.env (symlink inside root -> ../) read DENY
/proc/version read DENY
~/.ssh/id_rsa FileNotFoundError DENY

Four of six escaped the folder on the naive server: three versions of .env plus one absolute system path. The ~ case was the only miss, and it failed for the boring reason that join does not expand ~ — it looked for a directory literally named ~ inside the sandbox. That is not safety, just an accident.

The check that holds

from pathlib import Path from mcp.server.fastmcp.exceptions import ToolError ROOT = Path("/srv/work").resolve() def safe_read(path: str) -> str: target = (ROOT / path).resolve() # collapses .. and follows symlinks if not target.is_relative_to(ROOT): raise ToolError("DENY:outside") # ToolError, not PermissionError return target.read_text(encoding="utf-8") 

resolve() is the part that matters. It turns exposed/../../.env into an absolute path and follows a symlink to the place it really points, so the comparison afterwards sees where the read will actually happen. is_relative_to() is a plain prefix test on the resolved parts, and it already returns True for ROOT itself, so no special case is needed.

What the check costs

I expected the extra syscalls to show up in latency. They do not:

step median
Path(root) / path 1,250 µs
resolve() + is_relative_to() 1,381 µs
— resolve() alone 22 µs
— is_relative_to() on top +38 µs
one MCP tools/call round trip 2.7 ms

These are medians of 20,000 calls over three runs; a second pass at 5,000 calls × 5 runs gave 1,312 → 1,387 µs (+5.7%). Almost all of the millisecond is the file read, not the path check. The check adds roughly 0.1 ms, which is inside the noise of a single JSON-RPC round trip. There is no performance argument for skipping it.

The error you return matters as much as the check

A bare PermissionError looks harmless until you see it from the client side. With the crash server, the SDK turns an unhandled exception into is_error: true and a generic message:

{"content":[{"type":"text","text":"Error executing tool read_file"}],"is_error":true} 

The reason lives only in the server's stderr. From the agent's point of view there is nothing to correct — it sees "this tool is broken", not "that path is outside the sandbox", so it retries the same call or drops the task. Raising ToolError("DENY:outside") puts the reason into the content the model actually reads:

{"content":[{"type":"text","text":"Error executing tool read_file: DENY:outside"}],"is_error":true} 

Three things this does not fix

  • Writes and shell tools. The same six inputs against a write_file, or against a tool that shells out, are worse. Validate the resolved path before any filesystem operation, and keep shell execution out of the agent's reach in the first place.
  • Symlinks created after the check. Resolve and open in the same step; do not resolve, pass the string somewhere else, and open later.
  • A model that was talked into trying. A path check bounds the damage, it does not stop prompt injection. That is a separate layer: read-only roots, allowlists, and no secrets inside the working tree at all.

The short version

Of the variants I tested, only resolve() plus is_relative_to() returned zero leaks, and it costs about 0.1 ms per call — invisible next to the 2.7 ms round trip.

If you maintain an MCP server, the cheapest regression test is one case that sends ../.env and asserts a refusal. Not because that path is special, but because it fails loudly the moment somebody replaces the safe helper with a quick join.