add_table
Insert structured data tables into Microsoft Word documents programmatically by specifying headers and rows for organized content presentation.
Instructions
Add a table to the document
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| docId | Yes | Document identifier | |
| headers | Yes | Table header row | |
| rows | Yes | Table data rows |
Implementation Reference
- src/tools/tool-handlers.ts:62-71 (handler)The switch case that handles the 'add_table' tool invocation, calling the underlying documentManager.addTable method and returning a formatted success message.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.`, }, ], };
- src/tools/document-tools.ts:121-143 (schema)Defines the input schema for the add_table tool, specifying docId, headers as array of strings, and rows as 2D array of strings.inputSchema: { type: "object", properties: { docId: { type: "string", description: "Document identifier", }, headers: { type: "array", items: { type: "string" }, description: "Table header row", }, rows: { type: "array", items: { type: "array", items: { type: "string" }, }, description: "Table data rows", }, }, required: ["docId", "headers", "rows"], },
- src/index.ts:24-28 (registration)Registers all document tools, including 'add_table', for the MCP server by returning the documentTools array in response to list tools requests.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: documentTools, }; });
- Implements the core logic for adding a table to a document, constructing TableRows for headers (with bold and shading) and data rows using the docx library, then appending to the document paragraphs.addTable(docId: string, headers: string[], rows: string[][]): void { const docInfo = this.getDocument(docId); const tableRows: TableRow[] = []; // Add header row const headerRow = new TableRow({ children: headers.map( (header) => new TableCell({ children: [new Paragraph({ children: [new TextRun({ text: header, bold: true })], })], shading: { fill: "D3D3D3", }, }) ), }); tableRows.push(headerRow); // Add data rows rows.forEach((row) => { const tableRow = new TableRow({ children: row.map( (cell) => new TableCell({ children: [new Paragraph(cell)], }) ), }); tableRows.push(tableRow); }); const table = new Table({ rows: tableRows, width: { size: 100, type: WidthType.PERCENTAGE, }, }); // Tables need to be added differently - we'll add as a section child docInfo.paragraphs.push(table as any); this.updateDocument(docId); }