Skip to main content
Glama

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
NameRequiredDescriptionDefault
textYesThe full text content to extract lines from
start_lineYesStarting line number (inclusive)
end_lineYesEnding line number (inclusive)

Implementation Reference

  • 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}`,
                    },
                ],
            };
        }
    }
  • 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}`,
                        },
                    ],
                };
            }
        }
    );
Install Server

Other Tools

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/ananddtyagi/copy-paste-mcp'

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