Skip to main content
Glama

Replace Content

replace_content
Destructive

Replace text patterns in files using literal or regex matching to modify content efficiently in Serena's coding environment.

Instructions

Replaces one or more occurrences of a given pattern in a file with new content.

This is the preferred way to replace content in a file whenever the symbol-level tools are not appropriate.

VERY IMPORTANT: The "regex" mode allows very large sections of code to be replaced without fully quoting them! Use a regex of the form "beginning.*?end-of-text-to-be-replaced" to be faster and more economical! ALWAYS try to use wildcards to avoid specifying the exact content to be replaced, especially if it spans several lines. Note that you cannot make mistakes, because if the regex should match multiple occurrences while you disabled allow_multiple_occurrences, an error will be returned, and you can retry with a revised regex. Therefore, using regex mode with suitable wildcards is usually the best choice!.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
relative_pathYesThe relative path to the file.
needleYesThe string or regex pattern to search for. If `mode` is "literal", this string will be matched exactly. If `mode` is "regex", this string will be treated as a regular expression (syntax of Python's `re` module, with flags DOTALL and MULTILINE enabled).
replYesThe replacement string (verbatim). If mode is "regex", the string can contain backreferences to matched groups in the needle regex, specified using the syntax $!1, $!2, etc. for groups 1, 2, etc.
modeYesEither "literal" or "regex", specifying how the `needle` parameter is to be interpreted.
allow_multiple_occurrencesNoIf True, the regex may match multiple occurrences in the file and all of them will be replaced. If this is set to False and the regex matches multiple occurrences, an error will be returned (and you may retry with a revised, more specific regex).

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes
Behavior4/5

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

Annotations indicate destructiveHint=true and readOnlyHint=false, which the description aligns with by describing a replacement operation. The description adds valuable behavioral context beyond annotations: it explains error handling when allow_multiple_occurrences is false, provides regex performance optimization tips, and mentions retry capabilities. However, it doesn't cover rate limits or specific permission requirements.

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

Conciseness3/5

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

The description is appropriately front-loaded with the core purpose, but contains some redundancy in advocating for regex mode (mentioned multiple times with similar points). The 'VERY IMPORTANT' section could be more streamlined, though all sentences contribute meaningful guidance about tool usage strategies.

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

Completeness5/5

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

Given the tool's complexity (destructive file operation with regex capabilities), the description provides comprehensive context: it explains when to use this versus alternatives, offers detailed regex usage strategies, describes error behavior, and references sibling tools. With annotations covering safety aspects and an output schema presumably handling return values, the description fills all necessary contextual gaps.

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

Parameters4/5

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

With 100% schema description coverage, the baseline is 3. The description adds meaningful context about parameter usage: it emphasizes regex mode advantages for large sections, explains wildcard strategies to avoid exact content specification, and clarifies the interaction between regex patterns and the allow_multiple_occurrences parameter. This provides practical guidance beyond the schema's technical specifications.

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 ('replaces one or more occurrences of a given pattern in a file with new content') and distinguishes it from sibling tools by mentioning 'symbol-level tools' as alternatives. It explicitly names the resource (file content) and operation (replacement).

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 ('preferred way to replace content... whenever the symbol-level tools are not appropriate') and offers detailed advice on regex mode usage versus literal mode. It also references sibling tools like 'replace_symbol_body' as alternatives for symbol-level operations.

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

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/oraios/serena'

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