Skip to main content
Glama

insert_text

Replace or insert text at specific line ranges in files using line-number operations. Ideal for large files or precise edits without context-heavy processing.

Instructions

Insert or replace text at precise line ranges in files

  • Ideal for direct line-number operations (from code citations like 12:15:file.ts) and large files where context-heavy editing is inefficient.

  • TIP: Combine with read_symbol to edit any symbol anywhere without knowing its file or line range!

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYesPath to the file
from_lineYesStarting line number (1-based)
textYesText to insert
to_lineNoReplace up to this line number (1-based, inclusive). If omitted only inserts

Implementation Reference

  • Handler function that resolves the file path, reads the file content, updates the text at the specified line range using helper functions, writes back to the file, and returns context around the change.
    handler: (args) => {
      const fullPath = util.resolve(args.file_path)
      const content = util.readFile(fullPath)
      const newContent = updateText(content, args.from_line, args.text, args.to_line)
      util.writeFile(fullPath, newContent)
      return getContext(newContent, args.from_line, args.text, fullPath)
    },
  • Zod schema defining the input parameters for the insert_text tool.
    const schema = z.object({
      file_path: z.string().min(1).describe('Path to the file'),
      from_line: z.number().int().min(1).describe('Starting line number (1-based)'),
      text: z.string().describe('Text to insert'),
      to_line: z.number().int().min(1).optional().describe('Replace up to this line number (1-based, inclusive). If omitted only inserts'),
    })
  • src/tools.ts:22-29 (registration)
    Registration of all tools in the tools object, including insert_text mapped to its implementation.
    const tools = {
      read_symbol: readSymbol,
      import_symbol: importSymbol,
      search_replace: searchReplace,
      insert_text: insertText,
      os_notification: osNotification,
      utils_debug: utilsDebug,
    } as const satisfies Record<string, Tool<any>>
  • Helper function that performs the actual line-based text insertion or replacement in the file content.
    export function updateText(content: string, fromLine: number, text: string, toLine?: number): string {
      const endLine = toLine ?? fromLine
      if (endLine < fromLine) {
        throw new Error(`Invalid line range: to_line (${endLine}) cannot be less than from_line (${fromLine})`)
      }
      const lines = content === '' ? [] : content.split('\n')
      if (fromLine > lines.length + 1) {
        throw new Error(`from_line ${fromLine} is beyond file length (${lines.length} lines). Maximum allowed: ${lines.length + 1}`)
      }
      if (toLine && endLine > lines.length) {
        throw new Error(`to_line ${endLine} is beyond file length (${lines.length} lines). Maximum allowed: ${lines.length}`)
      }
      const linesToRemove = toLine ? (endLine - fromLine + 1) : 0
      const newLines = text.split('\n')
      lines.splice(fromLine - 1, linesToRemove, ...newLines)
      return lines.join('\n')
    }
  • Helper function that generates a context snippet around the inserted text for response.
    function getContext(content: string, fromLine: number, text: string, path: string): string {
      const lines = content.split('\n')
      const newLines = text.split('\n')
      // Show from_line - 2 to from_line + newLines.length + 2
      const startLine = Math.max(0, fromLine - 1 - CONTEXT_LINES)
      const endLine = Math.min(lines.length, fromLine - 1 + newLines.length + CONTEXT_LINES)
      const header = `=== ${startLine + 1}:${endLine + 1}:${path} ===`
      return `${header}\n${lines.slice(startLine, endLine).join('\n')}`
    }
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations indicate readOnlyHint=false and openWorldHint=false, which the description aligns with by describing a write operation ('insert or replace text'). The description adds valuable behavioral context beyond annotations, explaining the tool is designed for line-number operations and large files, and suggesting a complementary tool (read_symbol) for symbol-based editing. No contradictions with annotations exist.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is highly concise and well-structured with three sentences that each serve a distinct purpose: stating the core function, providing usage context, and offering a practical tip. There is no wasted text, and information is front-loaded effectively.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (file editing with line ranges), the description provides good contextual completeness despite no output schema. It explains the tool's purpose, ideal use cases, and how to combine it with another tool. However, it doesn't detail error conditions or the exact behavior when to_line is omitted, leaving minor gaps.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 100% schema description coverage, the input schema already fully documents all parameters. The description adds minimal parameter semantics beyond the schema, mentioning 'line ranges' and 'line-number operations' which are implied by the parameter names. It doesn't provide additional syntax or format details, so the baseline score of 3 is appropriate.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose with specific verbs ('insert or replace text') and resources ('at precise line ranges in files'), distinguishing it from sibling tools like os_notification and read_symbol. It explicitly mentions the target resource (files) and the precise nature of the operation (line-number based editing).

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides explicit guidance on when to use this tool ('ideal for direct line-number operations... and large files where context-heavy editing is inefficient') and when to use alternatives ('Combine with read_symbol to edit any symbol anywhere without knowing its file or line range'). It clearly differentiates from sibling tools and offers practical usage scenarios.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related 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/flesler/mcp-tools'

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