My AI Couldn’t See My Files — I Built a Zero-Dependency MCP Server
Summary
A zero-dependency Model Context Protocol (MCP) server has been developed using only Python's standard library, bypassing common frameworks like FastAPI or LangChain. This server allows AI clients to directly access local project files, eliminating the need for manual copy-pasting. It implements both stdio and HTTP/SSE transports, supporting single-client local connections and concurrent clients, with a demonstrated performance of handling 5 concurrent clients in under 50ms total wall time on Windows 11, Python 3.12.6. The architecture features a security layer that strictly sandboxes file access to an "MCP_ROOT" using "pathlib.resolve()", preventing path traversal attacks. Four core tools are provided: "list_directory", "read_file" (with a 1MB cap and binary fallback), "search_files" (shallow by default), and "get_file_info". The project highlights that complex frameworks are often unnecessary for well-defined, smaller problems, and that careful consideration of default behaviors is crucial for real-world stability and performance.
Key takeaway
For AI Engineers integrating local file access for models, this project demonstrates that you can build robust, performant tools without relying on complex external frameworks. Consider using Python's standard library for well-scoped tasks like an MCP server to minimize dependencies and gain finer control. When implementing, prioritize strict security measures like path sandboxing and carefully evaluate default behaviors for file operations, as seemingly minor choices can significantly impact real-world stability and performance, particularly across different operating systems.
Key insights
Building simple tools with standard libraries avoids unnecessary complexity and improves real-world reliability.
Principles
- Prioritize standard library solutions for small, well-defined problems.
- Default behaviors must not silently degrade performance or stability.
- Implement strict path validation for any filesystem interaction.
Method
Implement MCP using a four-layer architecture: Transport, Dispatcher, Security, and Tools. Decouple network I/O from core logic, and use "pathlib.resolve().relative_to()" for robust path sandboxing.
In practice
- Use "pathlib.resolve().relative_to()" for secure file path validation.
- Set "recursive=False" as default for file search operations.
- Configure Windows stdio to "O_BINARY" mode for JSON streams.
Topics
- Model Context Protocol
- JSON-RPC 2.0
- Python Standard Library
- Local AI Tools
- Filesystem Security
- Server-Sent Events
- Zero-Dependency Development
Code references
Best for: AI Engineer, Machine Learning Engineer, Software Engineer
Related on AIssential
Editorial summary, takeaway, and curation by AIssential. Original article published by Towards Data Science.