Skip to main content
Glama

find_and_replace

Locate and substitute text in Word documents to update content, correct errors, or modify formatting across specified sections or the entire file.

Instructions

Find and replace text in the document

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
docIdYesDocument identifier
findTextYesText to find
replaceTextYesText to replace with
replaceAllNoReplace all occurrences (true) or just first (false)

Implementation Reference

  • Tool registration including name, description, and input schema for find_and_replace
    {
      name: "find_and_replace",
      description: "Find and replace text in the document",
      inputSchema: {
        type: "object",
        properties: {
          docId: {
            type: "string",
            description: "Document identifier",
          },
          findText: {
            type: "string",
            description: "Text to find",
          },
          replaceText: {
            type: "string",
            description: "Text to replace with",
          },
          replaceAll: {
            type: "boolean",
            description: "Replace all occurrences (true) or just first (false)",
            default: true,
          },
        },
        required: ["docId", "findText", "replaceText"],
      },
    },
  • Main tool handler switch case that processes find_and_replace tool calls by invoking documentManager.findAndReplace and formatting the response
    case "find_and_replace":
      const count = documentManager.findAndReplace(
        args.docId,
        args.findText,
        args.replaceText,
        args.replaceAll ?? true
      );
      return {
        content: [
          {
            type: "text",
            text: `Replaced ${count} occurrence(s) of "${args.findText}" with "${args.replaceText}".`,
          },
        ],
      };
  • Core implementation of find and replace logic that iterates through document paragraphs, performs replacements on TextRun elements, counts occurrences, and updates the document
    findAndReplace(
      docId: string,
      findText: string,
      replaceText: string,
      replaceAll: boolean = true
    ): number {
      const docInfo = this.getDocument(docId);
      let replacementCount = 0;
    
      docInfo.paragraphs.forEach((paragraph: any) => {
        if (paragraph.root && paragraph.root.length > 0) {
          paragraph.root.forEach((element: any) => {
            if (element.text) {
              if (replaceAll) {
                const regex = new RegExp(findText, "g");
                if (regex.test(element.text)) {
                  const matches = element.text.match(regex);
                  replacementCount += matches ? matches.length : 0;
                  element.text = element.text.replace(regex, replaceText);
                }
              } else {
                if (element.text.includes(findText) && replacementCount === 0) {
                  element.text = element.text.replace(findText, replaceText);
                  replacementCount = 1;
                }
              }
            }
          });
        }
      });
    
      this.updateDocument(docId);
      return replacementCount;
    }
Behavior2/5

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

No annotations are provided, so the description carries full burden. 'Find and replace text' implies a mutation operation, but it doesn't disclose behavioral traits like whether changes are immediate or require saving, if it affects formatting, error conditions, or permissions needed. This leaves significant gaps for a tool that modifies documents.

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 a single, efficient sentence with zero waste. It's front-loaded and appropriately sized for the tool's complexity, making it easy to parse quickly.

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

Completeness2/5

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

Given the tool's complexity (a mutation operation with 4 parameters), lack of annotations, and no output schema, the description is incomplete. It doesn't explain what happens after replacement (e.g., success indicators, error handling), leaving the agent with insufficient context for reliable use.

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?

Schema description coverage is 100%, so the schema already documents all parameters (docId, findText, replaceText, replaceAll). The description adds no additional meaning beyond what's in the schema, such as examples or edge cases. Baseline 3 is appropriate when schema does the heavy lifting.

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

Purpose4/5

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

The description clearly states the action ('find and replace text') and resource ('in the document'), making the purpose immediately understandable. It doesn't specifically distinguish from sibling tools like 'apply_text_formatting' or 'save_document', but the core functionality is well-defined.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention prerequisites (e.g., document must exist), exclusions, or comparisons to sibling tools like 'apply_text_formatting' for other text modifications.

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/bibash44/word-documet-mcp-server'

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