add_table
Create and insert structured tables into Word documents by specifying file path, rows, columns, headers, and data for organized content presentation.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cols | Yes | ||
| data | No | ||
| filePath | Yes | ||
| headers | No | ||
| rows | Yes |
Implementation Reference
- src/services/DocumentService.ts:95-125 (handler)The core handler function for the 'add_table' tool. It creates a new Word document with a table of specified rows and columns, populates cells with data from options.data if provided, and writes the document to filePath using the docx Packer.async addTable(filePath: string, options: TableOptions): Promise<APIResponse> { try { const doc = new Document({ sections: [{ children: [ new Table({ rows: Array(options.rows).fill(0).map((_, rowIndex) => { return new TableRow({ children: Array(options.cols).fill(0).map((_, colIndex) => { const text = options.data?.[rowIndex]?.[colIndex] || ''; return new TableCell({ children: [new Paragraph({ text })], }); }), }); }), }), ], }], }); await Packer.toBuffer(doc).then((buffer) => { return fs.writeFile(filePath, buffer); }); return { success: true }; } catch (error) { const err = error as Error; return { success: false, error: `添加表格失败: ${err.message}` }; } }
- src/types/index.ts:29-35 (schema)TypeScript interface defining the input schema for add_table parameters: required rows and cols (numbers), optional headers (string[]), data (string[][]), and style (string). Used by DocumentService.addTable.export interface TableOptions { rows: number; cols: number; headers?: string[]; data?: string[][]; style?: string; }
- src/mcp-server.ts:94-120 (registration)MCP server registration of the 'add_table' tool using McpServer.tool(). Includes Zod input validation schema matching TableOptions and delegates to DocumentService.addTable.server.tool( "add_table", { filePath: z.string(), rows: z.number(), cols: z.number(), headers: z.array(z.string()).optional(), data: z.array(z.array(z.string())).optional(), }, async (params) => { const result = await docService.addTable(params.filePath, { rows: params.rows, cols: params.cols, headers: params.headers, data: params.data, }); return { content: [ { type: "text", text: result.success ? "表格已添加" : result.error!, }, ], isError: !result.success, }; } );
- src/server.ts:52-67 (registration)HTTP server registration of 'add_table' tool schema in the tools list endpoint (/tools/list), providing JSON Schema for the tool parameters.{ name: 'add_table', description: '向文档添加表格', parameters: { properties: { filePath: { type: 'string', description: '文档路径' }, rows: { type: 'number', description: '行数' }, cols: { type: 'number', description: '列数' }, headers: { type: 'array', description: '表头' }, data: { type: 'array', description: '表格数据' }, }, required: ['filePath', 'rows', 'cols'], type: 'object', }, }, {
- src/server.ts:141-148 (registration)Dispatch handler in HTTP server's /tools/call endpoint switch statement that invokes DocumentService.addTable for 'add_table' tool calls.case 'add_table': result = await docService.addTable(parameters.filePath, { rows: parameters.rows, cols: parameters.cols, headers: parameters.headers, data: parameters.data, }); break;