extract-lines
Extract a specific range of lines from text content while preserving formatting and newlines. Input the text, start line, and end line to retrieve the desired section.
Instructions
Extract a specific range of lines from text content
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| end_line | Yes | Ending line number (inclusive) | |
| start_line | Yes | Starting line number (inclusive) | |
| text | Yes | The full text content to extract lines from |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"end_line": {
"description": "Ending line number (inclusive)",
"type": "integer"
},
"start_line": {
"description": "Starting line number (inclusive)",
"minimum": 1,
"type": "integer"
},
"text": {
"description": "The full text content to extract lines from",
"type": "string"
}
},
"required": [
"text",
"start_line",
"end_line"
],
"type": "object"
}
Implementation Reference
- src/index.ts:20-77 (handler)The handler function that parses input text into lines, validates the start and end line numbers, extracts the specified range using array slice, joins them back, and returns as text content block or 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:15-19 (schema)Input schema using Zod: 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:12-79 (registration)Registers the 'extract-lines' tool on the MCP server using server.tool(), including name, description, input schema, and handler function.server.tool( "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}`, }, ], }; } } );