create_document
Generate Word documents programmatically by specifying file path, title, and author. Integrates with AI to simplify document creation through natural language commands.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| author | No | ||
| filePath | Yes | ||
| title | No |
Implementation Reference
- src/services/DocumentService.ts:27-52 (handler)Core handler function that creates a new empty Word document using the docx library, applies optional metadata from options, packs it into a buffer, writes to filePath, and returns APIResponse.async createDocument(filePath: string, options?: DocumentCreateOptions): Promise<APIResponse> { try { const doc = new Document({ sections: [{ properties: {}, children: [new Paragraph({ text: '', style: 'Normal' })] }], title: options?.title, subject: options?.subject, creator: options?.author, keywords: options?.keywords?.join(','), }); await Packer.toBuffer(doc).then((buffer) => { return fs.writeFile(filePath, buffer); }); return { success: true, data: { filePath } }; } catch (error) { const err = error as Error; return { success: false, error: `创建文档失败: ${err.message}` }; } }
- src/types/index.ts:13-18 (schema)TypeScript interface defining the input options for the createDocument handler.export interface DocumentCreateOptions { title?: string; author?: string; subject?: string; keywords?: string[]; }
- src/mcp-server.ts:17-47 (registration)Registers the 'create_document' tool in the MCP server with Zod input schema and a thin wrapper that invokes the DocumentService handler.server.tool("create_document", { filePath: z.string(), title: z.string().optional(), author: z.string().optional(), }, async (params) => { try { const result = await docService.createDocument(params.filePath, { title: params.title, author: params.author, }); return { content: [ { type: "text", text: result.success ? `文档已创建: ${params.filePath}` : result.error!, }, ], isError: !result.success, }; } catch (error: any) { return { content: [ { type: "text", text: `创建文档失败: ${error.message}`, }, ], isError: true, }; } });
- src/server.ts:15-26 (registration)Tool definition and schema registration in the HTTP server tools list.name: 'create_document', description: '创建新的 Word 文档', parameters: { properties: { filePath: { type: 'string', description: '文档保存路径' }, title: { type: 'string', description: '文档标题' }, author: { type: 'string', description: '文档作者' }, }, required: ['filePath'], type: 'object', }, },
- src/server.ts:122-127 (handler)Handler dispatch in the HTTP server's tool call endpoint switch statement, calling the DocumentService.case 'create_document': result = await docService.createDocument(parameters.filePath, { title: parameters.title, author: parameters.author, }); break;