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 handler function for the 'add_table' tool. It calls documentManager.addTable with the provided arguments and returns a success message indicating the table dimensions.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.`, }, ], };
- The core helper method in DocumentManager that constructs and adds a table to the document using the docx library, including header row with bold text and shading, and data rows.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); }
- src/tools/document-tools.ts:118-144 (registration)Registration of the 'add_table' tool in the documentTools array, including name, description, and input schema.{ name: "add_table", description: "Add a table to the document", 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/tools/document-tools.ts:121-143 (schema)Input schema definition for the 'add_table' tool, specifying parameters docId, headers (string[]), and rows (string[][]).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"], },