read_file
Read the contents of a file by specifying its path, with optional start and end lines to limit the output to a range.
Instructions
Read the contents of a file. Optionally specify a line range.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | File path (absolute or relative to workspace root) | |
| start_line | No | First line to read (1-indexed, inclusive) | |
| end_line | No | Last line to read (1-indexed, inclusive) | |
| workspace_root | No | Workspace root directory (optional) |
Implementation Reference
- src/index.ts:167-180 (registration)Tool registration with name 'read_file', description, and JSON Schema input definition requiring 'path' and accepting optional 'start_line', 'end_line', and 'workspace_root'.
{ name: "read_file", description: "Read the contents of a file. Optionally specify a line range.", inputSchema: { type: "object", properties: { path: { type: "string", description: "File path (absolute or relative to workspace root)" }, start_line: { type: "number", description: "First line to read (1-indexed, inclusive)" }, end_line: { type: "number", description: "Last line to read (1-indexed, inclusive)" }, workspace_root: { type: "string", description: "Workspace root directory (optional)" }, }, required: ["path"], }, }, - src/index.ts:434-442 (handler)Handler that resolves the file path, calls readFileLines with optional line range, and returns text content result.
case "read_file": { const filePath = resolveWorkspacePath(a.path as string, a.workspace_root as string | undefined); const content = await readFileLines( filePath, a.start_line as number | undefined, a.end_line as number | undefined ); return { content: [{ type: "text", text: content }] }; } - src/index.ts:65-84 (helper)Helper function that reads a file. If no line range given, reads entire file via fs/promises readFile. Otherwise streams the file line-by-line and returns only the requested lines.
async function readFileLines( filePath: string, startLine?: number, endLine?: number ): Promise<string> { if (startLine === undefined && endLine === undefined) { return readFile(filePath, "utf-8"); } const lines: string[] = []; const rl = createInterface({ input: createReadStream(filePath), crlfDelay: Infinity }); let lineNum = 1; for await (const line of rl) { if (startLine === undefined || lineNum >= startLine) { if (endLine === undefined || lineNum <= endLine) lines.push(line); else break; } lineNum++; } return lines.join("\n"); } - src/index.ts:59-63 (helper)Helper that resolves a potentially relative file path against a workspace root (or cwd). Absolute paths are returned as-is.
function resolveWorkspacePath(filePath: string, workspaceRoot?: string): string { if (filePath.startsWith("/") || /^[A-Za-z]:/.test(filePath)) return filePath; const base = workspaceRoot ?? process.cwd(); return resolve(join(base, filePath)); }