4.9 KiB
4.9 KiB
System Patterns: PDF Reader MCP Server
1. Architecture Overview
The PDF Reader MCP server is a standalone Node.js application based on the original Filesystem MCP. It's designed to run as a child process, communicating with its parent (the AI agent host) via standard input/output (stdio) using the Model Context Protocol (MCP) to provide PDF reading capabilities.
graph LR
A[Agent Host Environment] -- MCP over Stdio --> B(PDF Reader MCP Server);
B -- Node.js fs/path/pdfjs-dist --> C[User Filesystem (Project Root)];
C -- Results/Data --> B;
B -- MCP over Stdio --> A;
2. Key Technical Decisions & Patterns
- MCP SDK Usage: Leverages the
@modelcontextprotocol/sdkfor handling MCP communication (request parsing, response formatting, error handling). This standardizes interaction and reduces boilerplate code. - Stdio Transport: Uses
StdioServerTransportfrom the SDK for communication, suitable for running as a managed child process. - Asynchronous Operations: All filesystem interactions and request handling
are implemented using
async/awaitand Node.js's promise-basedfsmodule (fs.promises) for non-blocking I/O. - Strict Path Resolution: A dedicated
resolvePathfunction is used for every path received from the agent.- It normalizes the path.
- It resolves the path relative to the server process's current working
directory (
process.cwd()), which is treated as thePROJECT_ROOT. Crucially, this requires the process launching the server (e.g., the agent host) to set the correctcwdfor the target project. - It explicitly checks if the resolved absolute path still starts with the
PROJECT_ROOTabsolute path to prevent path traversal vulnerabilities (e.g.,../../sensitive-file). - It rejects absolute paths provided by the agent.
- Zod for Schemas & Validation: Uses
zodlibrary to define input schemas for tools and perform robust validation within each handler. JSON schemas for MCP listing are generated from Zod schemas. - Tool Definition Aggregation: Tool definitions (name, description, Zod
schema, handler function) are defined in their respective handler files and
aggregated in
src/handlers/index.tsfor registration insrc/index.ts. edit_fileLogic:- Processes multiple changes per file, applying them sequentially from bottom-to-top to minimize line number conflicts.
- Handles insertion, text replacement, and deletion.
- Implements basic indentation detection (
detect-indent) and preservation for insertions/replacements. - Uses
difflibrary to generate unified diff output.
- Error Handling:
- Uses
try...catchblocks within each tool handler. - Catches specific Node.js filesystem errors (like
ENOENT,EPERM,EACCES) and maps them to appropriate MCP error codes (InvalidRequest). - Uses custom
McpErrorobjects for standardized error reporting back to the agent. - Logs unexpected errors to the server's console (
stderr) for debugging.
- Uses
- Glob for Listing/Searching: Uses the
globlibrary for flexible and powerful file listing and searching based on glob patterns, including recursive operations and stat retrieval. Careful handling ofglob's different output types based on options (string[],Path[],Path[]withstats) is implemented. - TypeScript: Provides static typing for better code maintainability, early
error detection, and improved developer experience. Uses ES module syntax
(
import/export). - PDF Parsing: Uses Mozilla's
pdfjs-distlibrary to load PDF documents and extract text content, metadata, and page information. Theread_pdfhandler uses its API.
3. Component Relationships
index.ts: Main entry point. Sets up the MCP server instance, defines tool schemas, registers request handlers, and starts the server connection.Server(from SDK): Core MCP server class handling protocol logic.StdioServerTransport(from SDK): Handles reading/writing MCP messages via stdio.- Tool Handler Function (
handleReadPdfFunc): Contains the logic for the consolidatedread_pdftool, including Zod argument validation, path resolution, PDF loading/parsing viapdfjs-dist, and result formatting based on input parameters. resolvePathHelper: Centralized security function for path validation.formatStatsHelper: Utility to create a consistent stats object structure.- Node.js Modules (
fs,path): Used for actual filesystem operations and path manipulation. globLibrary: Used for pattern-based file searching and listing.zodLibrary: Used for defining and validating tool input schemas.diffLibrary: (Inherited, but not used by PDF tools) Used byedit_file.detect-indentLibrary: (Inherited, but not used by PDF tools) Used byedit_file.pdfjs-distLibrary: Used by theread_pdfhandler to load and process PDF documents.