Skip to main content
Glama
document-replace.ts1.51 kB
import {z} from 'zod'; import type {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js'; import type {Config} from './types.js'; import {makeDocsApiCall} from '../utils/docs-api.js'; import {jsonResult} from '../utils/response.js'; const inputSchema = { documentId: z.string().describe('The ID of the document'), findText: z.string().describe('The text to search for'), replaceText: z.string().describe('The text to replace matches with'), matchCase: z.boolean().optional().default(false).describe('Whether the search should be case-sensitive'), }; const outputSchema = z.object({ documentId: z.string(), replies: z.array(z.object({ replaceAllText: z.object({ occurrencesChanged: z.number().optional(), }).optional(), })).optional(), }).passthrough(); export function registerDocumentReplace(server: McpServer, config: Config): void { server.registerTool( 'document_replace', { title: 'Find and replace in document', description: 'Find and replace all occurrences of text in a Google Doc. Returns the number of replacements made.', inputSchema, outputSchema, }, async ({documentId, findText, replaceText, matchCase}) => { const requests = [ { replaceAllText: { containsText: { text: findText, matchCase, }, replaceText, }, }, ]; const result = await makeDocsApiCall('POST', `/documents/${documentId}:batchUpdate`, config.token, {requests}); return jsonResult(outputSchema.parse(result)); }, ); }

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/domdomegg/google-docs-mcp'

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