search_and_replace
Modify Word documents by finding specific text and replacing it with desired content. Specify file path, search text, replace text, and optional case sensitivity for precise updates.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | ||
| matchCase | No | ||
| replaceText | Yes | ||
| searchText | Yes |
Implementation Reference
- src/services/DocumentService.ts:153-189 (handler)Core handler function that reads the Word document using mammoth, extracts text, performs regex-based search and replace, recreates the document with docx, writes it back, and returns match count and preview.async searchAndReplace(filePath: string, options: SearchReplaceOptions): Promise<APIResponse> { try { const buffer = await fs.readFile(filePath); const result = await mammoth.extractRawText({ buffer }); let content = result.value; const searchRegex = new RegExp( options.searchText, `g${options.matchCase ? '' : 'i'}` ); const matches = content.match(searchRegex) || []; content = content.replace(searchRegex, options.replaceText); // 注意:这里需要重新创建文档,因为 mammoth 主要用于读取 const doc = new Document({ sections: [{ children: [new Paragraph({ text: content })], }], }); await Packer.toBuffer(doc).then((buffer) => { return fs.writeFile(filePath, buffer); }); return { success: true, data: { matchCount: matches.length, preview: content.substring(0, 200) + '...', }, }; } catch (error) { const err = error as Error; return { success: false, error: `查找替换失败: ${err.message}` }; } }
- src/types/index.ts:45-49 (schema)TypeScript interface defining the input parameters for the search_and_replace tool.export interface SearchReplaceOptions { searchText: string; replaceText: string; matchCase?: boolean; }
- src/mcp-server.ts:122-148 (registration)Registration of the search_and_replace tool in the MCP server using server.tool, with Zod schema and wrapper that calls DocumentService.searchAndReplace.server.tool( "search_and_replace", { filePath: z.string(), searchText: z.string(), replaceText: z.string(), matchCase: z.boolean().optional(), }, async (params) => { const result = await docService.searchAndReplace(params.filePath, { searchText: params.searchText, replaceText: params.replaceText, matchCase: params.matchCase, }); return { content: [ { type: "text", text: result.success ? `替换完成: ${result.data.matchCount} 处匹配\n预览: ${result.data.preview}` : result.error!, }, ], isError: !result.success, }; } );
- src/server.ts:68-80 (registration)Tool definition and schema registration in the Express server's tools list for search_and_replace.name: 'search_and_replace', description: '查找并替换文本', parameters: { properties: { filePath: { type: 'string', description: '文档路径' }, searchText: { type: 'string', description: '要查找的文本' }, replaceText: { type: 'string', description: '替换为的文本' }, matchCase: { type: 'boolean', description: '是否区分大小写' }, }, required: ['filePath', 'searchText', 'replaceText'], type: 'object', }, },
- src/server.ts:150-156 (registration)Dispatch handler in the Express server's /tools/call endpoint that invokes DocumentService.searchAndReplace for search_and_replace.case 'search_and_replace': result = await docService.searchAndReplace(parameters.filePath, { searchText: parameters.searchText, replaceText: parameters.replaceText, matchCase: parameters.matchCase, }); break;