extract-lines
Extract a specific range of lines from text content while preserving exact formatting and newlines for focused analysis or sharing.
Instructions
Extract a specific range of lines from text content
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | The full text content to extract lines from | |
| start_line | Yes | Starting line number (inclusive) | |
| end_line | Yes | Ending line number (inclusive) |
Implementation Reference
- src/index.ts:20-78 (handler)The handler function that implements the extract-lines tool logic: validates input line numbers, splits text into lines, extracts the specified range, and returns the result or an error.async ({ text, start_line, end_line }) => { try { // Validate line numbers if (start_line > end_line) { return { isError: true, content: [ { type: "text", text: `Error: Start line (${start_line}) cannot be greater than end line (${end_line}).`, }, ], }; } // Split the text into lines const allLines = text.split('\n'); // Validate that requested lines are within range if (start_line > allLines.length) { return { isError: true, content: [ { type: "text", text: `Error: Start line (${start_line}) exceeds the total number of lines (${allLines.length}).`, }, ], }; } // Extract the requested lines (adjusting for 0-based array indexing) const extractedLines = allLines.slice( start_line - 1, Math.min(end_line, allLines.length) ); // Return the extracted lines return { content: [ { type: "text", text: extractedLines.join('\n'), }, ], }; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : String(error); return { isError: true, content: [ { type: "text", text: `Error extracting lines: ${errorMessage}`, }, ], }; } }
- src/index.ts:16-19 (schema)Zod schema for the tool's input parameters: text (string), start_line (integer >=1), end_line (integer).text: z.string().describe("The full text content to extract lines from"), start_line: z.number().int().min(1).describe("Starting line number (inclusive)"), end_line: z.number().int().describe("Ending line number (inclusive)"), },
- src/index.ts:13-79 (registration)Registration of the 'extract-lines' tool on the MCP server, specifying name, description, input schema, and handler function."extract-lines", "Extract a specific range of lines from text content", { text: z.string().describe("The full text content to extract lines from"), start_line: z.number().int().min(1).describe("Starting line number (inclusive)"), end_line: z.number().int().describe("Ending line number (inclusive)"), }, async ({ text, start_line, end_line }) => { try { // Validate line numbers if (start_line > end_line) { return { isError: true, content: [ { type: "text", text: `Error: Start line (${start_line}) cannot be greater than end line (${end_line}).`, }, ], }; } // Split the text into lines const allLines = text.split('\n'); // Validate that requested lines are within range if (start_line > allLines.length) { return { isError: true, content: [ { type: "text", text: `Error: Start line (${start_line}) exceeds the total number of lines (${allLines.length}).`, }, ], }; } // Extract the requested lines (adjusting for 0-based array indexing) const extractedLines = allLines.slice( start_line - 1, Math.min(end_line, allLines.length) ); // Return the extracted lines return { content: [ { type: "text", text: extractedLines.join('\n'), }, ], }; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : String(error); return { isError: true, content: [ { type: "text", text: `Error extracting lines: ${errorMessage}`, }, ], }; } } );