apply_text_formatting
Apply bold, italic, underline, or highlight formatting to specific text in Microsoft Word documents programmatically.
Instructions
Apply formatting to specific text in the document
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| docId | Yes | Document identifier | |
| searchText | Yes | Text to format | |
| formatting | Yes |
Implementation Reference
- src/tools/document-tools.ts:200-226 (schema)Defines the Tool object for 'apply_text_formatting' including name, description, and input schema.{ name: "apply_text_formatting", description: "Apply formatting to specific text in the document", inputSchema: { type: "object", properties: { docId: { type: "string", description: "Document identifier", }, searchText: { type: "string", description: "Text to format", }, formatting: { type: "object", properties: { bold: { type: "boolean" }, italic: { type: "boolean" }, underline: { type: "boolean" }, highlight: { type: "string", description: "Highlight color" }, }, }, }, required: ["docId", "searchText", "formatting"], }, },
- src/index.ts:24-28 (registration)Registers the list of tools including 'apply_text_formatting' via the documentTools array for ListToolsRequest.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: documentTools, }; });
- src/tools/tool-handlers.ts:5-114 (handler)Generic tool handler dispatcher. 'apply_text_formatting' falls to default case throwing 'Unknown tool' error since no specific case.export async function handleToolCall(toolName: string, args: any) { switch (toolName) { case "create_document": const docId = documentManager.createDocument(args.filepath, args.title); return { content: [ { type: "text", text: `Document created successfully with ID: ${docId}. Use this ID for all future operations on this document.`, }, ], }; case "add_heading": documentManager.addHeading(args.docId, args.text, args.level); return { content: [ { type: "text", text: `Heading level ${args.level} added: "${args.text}"`, }, ], }; case "add_paragraph": documentManager.addParagraph(args.docId, args.text, args.formatting); return { content: [ { type: "text", text: `Paragraph added successfully.`, }, ], }; case "add_bullet_list": documentManager.addBulletList(args.docId, args.items); return { content: [ { type: "text", text: `Bullet list added with ${args.items.length} items.`, }, ], }; case "add_numbered_list": documentManager.addNumberedList(args.docId, args.items); return { content: [ { type: "text", text: `Numbered list added with ${args.items.length} items.`, }, ], }; case "add_table": documentManager.addTable(args.docId, args.headers, args.rows); return { content: [ { type: "text", text: `Table added with ${args.headers.length} columns and ${args.rows.length} rows.`, }, ], }; 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}".`, }, ], }; case "save_document": const filepath = await documentManager.saveDocument(args.docId); return { content: [ { type: "text", text: `Document saved successfully to: ${filepath}`, }, ], }; case "get_document_structure": const structure = documentManager.getDocumentStructure(args.docId); return { content: [ { type: "text", text: `Document structure:\n${structure}`, }, ], }; default: throw new Error(`Unknown tool: ${toolName}`); } }
- src/index.ts:31-47 (registration)Registers the generic CallToolRequest handler which dispatches to handleToolCall for all tools including 'apply_text_formatting'.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, }; } });