find_and_replace
Locate and modify text in Microsoft Word documents by specifying text to find and replacement content, with options for single or multiple occurrences.
Instructions
Find and replace text in the document
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| docId | Yes | Document identifier | |
| findText | Yes | Text to find | |
| replaceText | Yes | Text to replace with | |
| replaceAll | No | Replace all occurrences (true) or just first (false) |
Implementation Reference
- src/services/document-manager.ts:203-237 (handler)The core handler function that implements find and replace logic by traversing document paragraphs and replacing text in TextRun elements, supporting all or first occurrence.*/ 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; }
- src/tools/document-tools.ts:145-171 (schema)Tool schema definition specifying input parameters for find_and_replace: docId, findText, replaceText, and optional replaceAll.{ 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"], }, },
- src/tools/tool-handlers.ts:73-87 (registration)Tool handler registration in switch statement that dispatches find_and_replace calls to the DocumentManager's findAndReplace method and formats 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}".`, }, ], };
- src/index.ts:24-28 (registration)Registers the list of available tools, including find_and_replace schema from documentTools array.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: documentTools, }; });
- src/index.ts:31-47 (registration)Registers the general tool call handler that routes to handleToolCall based on tool name, enabling find_and_replace execution.server.setRequestHandler(CallToolRequestSchema, async (request) => { try { const result = await handleToolCall(request.params.name, request.params.arguments); return result; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: "text", text: `Error: ${errorMessage}`, }, ], isError: true, }; } });