Skip to main content
Glama
A-Niranjan

MCP Filesystem Server

by A-Niranjan

read_file

Read file contents from the filesystem with support for text encodings and detailed error handling. Use this tool to examine the contents of a single file within allowed directories.

Instructions

Read the complete contents of a file from the file system. Handles various text encodings and provides detailed error messages if the file cannot be read. Use this tool when you need to examine the contents of a single file. Only works within allowed directories.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesPath to the file to read
encodingNoFile encodingutf-8

Implementation Reference

  • The core handler function for the 'read_file' tool. Validates the path, checks file size limits, reads the file content using Node.js fs.readFile, logs success, records metrics, and handles specific errors like file not found.
    export async function readFile( args: z.infer<typeof ReadFileArgsSchema>, config: Config ): Promise<string> { const endMetric = metrics.startOperation('read_file') try { const validPath = await validatePath(args.path, config) // Validate file size before reading if (config.security.maxFileSize > 0) { await validateFileSize(validPath, config.security.maxFileSize) } const content = await fs.readFile(validPath, args.encoding) await logger.debug(`Successfully read file: ${validPath}`) endMetric() return content } catch (error) { metrics.recordError('read_file') if ((error as NodeJS.ErrnoException).code === 'ENOENT') { throw new PathNotFoundError(args.path) } throw error } }
  • Zod schema defining the input parameters for the read_file tool: required 'path' string and optional 'encoding' enum with default 'utf-8'.
    export const ReadFileArgsSchema = z.object({ path: z.string().describe('Path to the file to read'), encoding: z .enum(['utf-8', 'utf8', 'base64']) .optional() .default('utf-8') .describe('File encoding'), })
  • src/index.ts:236-243 (registration)
    Tool registration in the list_tools handler, specifying the name, detailed description, and input schema derived from ReadFileArgsSchema.
    name: 'read_file', description: 'Read the complete contents of a file from the file system. ' + 'Handles various text encodings and provides detailed error messages ' + 'if the file cannot be read. Use this tool when you need to examine ' + 'the contents of a single file. Only works within allowed directories.', inputSchema: zodToJsonSchema(ReadFileArgsSchema) as ToolInput, },
  • MCP call_tool dispatch case for 'read_file' that validates input arguments using ReadFileArgsSchema and delegates to the readFile handler function.
    case 'read_file': { const parsed = ReadFileArgsSchema.safeParse(a) if (!parsed.success) { throw new FileSystemError(`Invalid arguments for ${name}`, 'INVALID_ARGS', undefined, { errors: parsed.error.format(), }) } const content = await readFile(parsed.data, config) endMetric() return { content: [{ type: 'text', text: content }], } }

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/A-Niranjan/mcp-filesystem'

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