Skip to main content
Glama
A-Niranjan

MCP Filesystem Server

by A-Niranjan

read_file

Retrieve and examine the contents of a file from the file system, supporting multiple text encodings like UTF-8 and base64. Provides clear error messages for unreadable files and operates within predefined directory limits.

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
encodingNoFile encodingutf-8
pathYesPath to the file to read

Implementation Reference

  • Core implementation of the read_file tool: performs path validation, file size checks, reads file content with fs.readFile, handles errors including PathNotFoundError, and integrates metrics and logging.
    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.
    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 server's listTools response: defines name, description, and converts Zod schema to JSON schema for MCP protocol.
    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 server request handler for read_file: validates arguments with schema, calls the readFile implementation, and returns content in MCP format.
    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 }], } }

Other Tools

Related Tools

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