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 }],
      }
    }
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It effectively describes key behaviors: it reads complete file contents (not partial), handles various text encodings, provides detailed error messages, and has directory restrictions. It doesn't mention performance characteristics like file size limits or whether it's idempotent, but covers the essential operational aspects well for a read operation.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is perfectly front-loaded with the core purpose in the first sentence, followed by behavioral details and usage guidelines. Every sentence earns its place: the first defines the action, the second adds behavioral context, the third provides usage guidance, and the fourth states constraints. No wasted words or redundancy.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a read operation with no annotations and no output schema, the description provides good coverage of what the tool does, when to use it, and important constraints. It could be more complete by mentioning the return format (e.g., whether it returns raw text or structured data) or any performance considerations, but it addresses the essential context well given the tool's relative simplicity.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 100% schema description coverage, the schema already fully documents both parameters (path and encoding with enum values). The description adds no additional parameter semantics beyond what's in the schema. According to scoring rules, when schema_description_coverage is high (>80%), the baseline is 3 even with no param info in description.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Read the complete contents') and resource ('a file from the file system'), distinguishing it from siblings like 'read_multiple_files' (single vs multiple) and 'get_file_info' (contents vs metadata). It provides a precise verb+resource combination that leaves no ambiguity about the tool's function.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides clear context for when to use this tool ('when you need to examine the contents of a single file') and mentions a constraint ('Only works within allowed directories'). However, it doesn't explicitly contrast with alternatives like 'read_multiple_files' for bulk operations or 'get_file_info' for metadata-only needs, which would elevate it to a 5.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other 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