Skip to main content
Glama

read

Read text file contents from the filesystem, preview sections with head/tail options, or extract specific line ranges for file analysis.

Instructions

Read text file contents. Use head to preview first N lines of large files. For multiple files, use read_many.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesAbsolute path to file or directory.
headNoRead first N lines (preview)
tailNoRead last N lines
startLineNoStart line (1-based, inclusive). Defaults to 1 when endLine is set.
endLineNoEnd line (1-based, inclusive). Defaults to last line when startLine is set.
includeHashNoInclude SHA-256 hash of full file content

Implementation Reference

  • The core handler function `handleReadFile` for the "read" tool, responsible for reading file content and constructing the tool response.
    async function handleReadFile(
      args: ReadFileInput,
      signal?: AbortSignal,
      resourceStore?: ToolRegistrationOptions['resourceStore']
    ): Promise<ToolResponse<ReadFileOutput>> {
      const options = buildReadOptions(args, signal);
      const result = await readFile(args.path, options);
      const structured = toStructuredReadFileResult(args, result);
    
      if (args.includeHash) {
        structured.contentHash = createHash('sha256')
          .update(result.content, 'utf-8')
          .digest('hex');
      }
    
      const externalizedResponse = maybeBuildExternalizedReadResponse(
        args.path,
        result.content,
        structured,
        resourceStore
      );
      if (externalizedResponse) {
        return externalizedResponse;
      }
    
      return buildToolResponse(result.content, structured);
    }
  • Tool registration function `registerReadFileTool` which wires up the handler to the MCP server.
    export function registerReadFileTool(
      server: McpServer,
      options: ToolRegistrationOptions = {}
    ): void {
      const handler = (
        args: ReadFileInput,
        extra: ToolExtra
      ): Promise<ToolResult<ReadFileOutput>> =>
        executeToolWithDiagnostics({
          toolName: READ_TOOL_NAME,
          extra,
          outputSchema: ReadFileOutputSchema,
          timedSignal: { timeoutMs: DEFAULT_SEARCH_TIMEOUT_MS },
          context: { path: args.path },
          run: (signal) => handleReadFile(args, signal, options.resourceStore),
          onError: (error) =>
            buildToolErrorResponse(error, ErrorCode.E_NOT_FILE, args.path),
        });
    
      const wrappedHandler = wrapToolHandler(handler, {
        guard: options.isInitialized,
        progressMessage: buildReadProgressMessage,
        completionMessage: buildReadCompletionMessage,
      });
    
      const validatedHandler = withValidatedArgs(
        ReadFileInputSchema,
        wrappedHandler
      );
    
      if (
        registerToolTaskIfAvailable(
          server,
          READ_TOOL_NAME,
          READ_FILE_TOOL,
          validatedHandler,
          options.iconInfo,
          options.isInitialized
        )
      )
        return;
      server.registerTool(
        READ_TOOL_NAME,
        withDefaultIcons({ ...READ_FILE_TOOL }, options.iconInfo),
        validatedHandler
      );
    }

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

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