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; }

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