Skip to main content
Glama

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
NameRequiredDescriptionDefault
authorNo
filePathYes
titleNo

Implementation Reference

  • 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}` }; } }
  • TypeScript interface defining the input options for the createDocument handler.
    export interface DocumentCreateOptions { title?: string; author?: string; subject?: string; keywords?: string[]; }
  • 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', }, },
  • 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;

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/puchunjie/doc-tools-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server