add_paragraph
Insert formatted text paragraphs into Microsoft Word documents programmatically, supporting bold, italic, underline, font customization, color, and alignment options.
Instructions
Add a paragraph of text to the document
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| docId | Yes | Document identifier | |
| text | Yes | Paragraph text | |
| formatting | No | Optional formatting options |
Implementation Reference
- src/tools/tool-handlers.ts:29-38 (handler)Switch case handler for the 'add_paragraph' tool. Extracts arguments and calls documentManager.addParagraph(), then returns a success message.case "add_paragraph": documentManager.addParagraph(args.docId, args.text, args.formatting); return { content: [ { type: "text", text: `Paragraph added successfully.`, }, ], };
- src/tools/document-tools.ts:49-77 (schema)Input schema defining the parameters for the add_paragraph tool: required docId and text, optional formatting object.inputSchema: { type: "object", properties: { docId: { type: "string", description: "Document identifier", }, text: { type: "string", description: "Paragraph text", }, formatting: { type: "object", description: "Optional formatting options", properties: { bold: { type: "boolean" }, italic: { type: "boolean" }, underline: { type: "boolean" }, fontSize: { type: "number" }, font: { type: "string" }, color: { type: "string", description: "Hex color (e.g., FF0000 for red)" }, alignment: { type: "string", enum: ["left", "center", "right", "justified"], }, }, }, }, required: ["docId", "text"],
- src/index.ts:24-28 (registration)Registration of all tools including 'add_paragraph' via the documentTools array provided to the MCP server's ListToolsRequestHandler.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: documentTools, }; });
- Core implementation of adding a formatted paragraph to the document using docx library, called by the tool handler.addParagraph(docId: string, text: string, formatting?: FormattingOptions): void { const docInfo = this.getDocument(docId); const textRunOptions: any = { text }; if (formatting?.bold !== undefined) textRunOptions.bold = formatting.bold; if (formatting?.italic !== undefined) textRunOptions.italics = formatting.italic; if (formatting?.underline) textRunOptions.underline = {}; if (formatting?.fontSize !== undefined) textRunOptions.size = formatting.fontSize * 2; // Half-points if (formatting?.font !== undefined) textRunOptions.font = formatting.font; if (formatting?.color !== undefined) textRunOptions.color = formatting.color; const textRun = new TextRun(textRunOptions); const paragraphOptions: any = { children: [textRun], }; const alignment = this.getAlignment(formatting?.alignment); if (alignment !== undefined) { paragraphOptions.alignment = alignment; } const paragraph = new Paragraph(paragraphOptions); docInfo.paragraphs.push(paragraph); this.updateDocument(docId); }
- src/types/document.types.ts:11-20 (helper)Type definition for FormattingOptions used in add_paragraph tool schema and implementation.export interface FormattingOptions { bold?: boolean; italic?: boolean; underline?: boolean; fontSize?: number; font?: string; color?: string; alignment?: "left" | "center" | "right" | "justified"; highlight?: string; }