Model Context Protocol (MCP) servers are becoming the glue between AI agents and local infrastructure, but a new benchmark reveals that the default file-reading tools are dangerously permissive. Developer koreanstudentlee tested three variations of an MCP server using Python 3.12.16 and MCP SDK 2.3.0, discovering that a naive implementation leaked sensitive files in four out of six test cases. The fix, however, is trivial: adding resolve() and is_relative_to() checks to path validation costs only about 0.1 milliseconds per call, a negligible overhead compared to the 2.7ms average round-trip time of a tools/call operation.

The Blast Radius of Naive Joins

The test harness sent six specific path inputs to a read_file tool, simulating strings an LLM might compose. The naive server, which constructed paths using Path(root) / path, successfully read ../.env, exposed/../../.env, and /proc/version without complaint. It also followed a symlink inside the root directory that pointed outside the sandbox, exposing another .env file. The only input that failed was ~/.ssh/id_rsa, but this was due to a lack of tilde expansion in the naive join rather than any security feature. This means the default behavior allows directory traversal and absolute path access, effectively exposing the entire filesystem to any agent that can craft a path string.

Performance Overhead Is a Myth

Developers often skip path validation due to perceived performance costs, but the data shows otherwise. The benchmark measured the latency of path construction and validation over 20,000 calls. While the naive Path(root) / path operation took a median of 1,250 Β΅s, the safe implementation using resolve() and is_relative_to() took 1,381 Β΅s. The actual security checkβ€”resolve() followed by is_relative_to()β€”added only 38 Β΅s to the process. Since the entire MCP tools/call round-trip averages 2.7 ms, the security overhead is effectively invisible, representing less than 2% of the total operation time. There is no performance argument for leaving the sandbox open.

Error Handling Matters for Agent Recovery

Beyond preventing leaks, how the server reports errors significantly impacts agent behavior. When the safe implementation raised a standard PermissionError, the MCP SDK converted it into a generic is_error: true response with no actionable details, leaving the AI agent confused and likely to retry the same failing call. By contrast, raising a ToolError with a specific message like "DENY:outside" provides context directly in the response content. This allows the model to understand that the path was rejected due to sandbox boundaries, enabling it to adjust its strategy rather than hallucinating a fix or dropping the task entirely.

Key Takeaways

  • Use Path.resolve() to canonicalize paths before checking containment, as it handles symlinks and directory traversal (..) correctly.
  • The is_relative_to() method is sufficient for containment checks and adds negligible latency (<40 Β΅s).
  • Never use generic exceptions like PermissionError for tool failures; use ToolError to pass specific rejection reasons back to the agent.
  • Add a regression test that asserts a refusal on ../.env to catch accidental reversion to naive path joining.

The Bottom Line

If you are building MCP servers, stop trusting the model's path strings. The cost of validation is microscopic, and the blast radius of a leaked .env file is catastrophic. Implement resolve() checks today and make ToolError your default exception for sandbox violations.