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
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Absolute path to file or directory. | |
| head | No | Read first N lines (preview) | |
| tail | No | Read last N lines | |
| startLine | No | Start line (1-based, inclusive). Defaults to 1 when endLine is set. | |
| endLine | No | End line (1-based, inclusive). Defaults to last line when startLine is set. | |
| includeHash | No | Include SHA-256 hash of full file content |
Implementation Reference
- src/tools/read.ts:191-217 (handler)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); } - src/tools/read.ts:219-265 (registration)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 ); }