Skip to main content
Glama
ai-yliu

Filesystem MCP Server

by ai-yliu

read_file

Retrieve complete file contents from the filesystem. Specify the file path to access text, code, or data stored in documents.

Instructions

Read complete contents of a file

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesPath to the file to read

Implementation Reference

  • The handler logic for the 'read_file' tool. It extracts the file path from the request arguments, validates the path is allowed, reads the entire file content using fs.readFile, and returns it formatted as MCP tool content with type 'text'.
    case 'read_file': { const { path: filePath } = request.params.arguments as { path: string }; validatePath(filePath); const content = await fs.readFile(filePath, 'utf8'); return { content: [ { type: 'text', text: content, }, ], }; }
  • The JSON schema defining the input parameters for the 'read_file' tool, requiring a single 'path' string property.
    inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the file to read', }, }, required: ['path'], },
  • src/index.ts:95-108 (registration)
    The tool registration entry in the list_tools response, including name, description, and input schema for 'read_file'.
    { name: 'read_file', description: 'Read complete contents of a file', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the file to read', }, }, required: ['path'], }, },
  • Helper function called by the read_file handler to ensure the requested file path is within the server-allowed directories, throwing an error if not.
    function validatePath(filePath: string): void { if (!isPathAllowed(filePath)) { throw new McpError( ErrorCode.InvalidRequest, `Access denied: ${filePath} is not within allowed directories` ); } }
  • Supporting helper function used by validatePath to check if a resolved path is within any of the allowed directories.
    function isPathAllowed(filePath: string): boolean { const resolvedPath = path.resolve(filePath); return resolvedAllowedDirectories.some(allowedDir => resolvedPath === allowedDir || resolvedPath.startsWith(allowedDir + path.sep) ); }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ai-yliu/filesystem-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server