Skip to main content
Glama

create_document

Create Word documents with specified file paths, titles, and authors to enable AI-assisted document generation and management.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filePathYes
titleNo
authorNo

Implementation Reference

  • The handler function for the MCP 'create_document' tool. It processes the parameters, calls the DocumentService to create the document, handles errors, and returns an MCP-compatible response with content and isError flag.
    }, 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,
        };
      }
    });
  • Zod-based input schema definition for the 'create_document' tool parameters in the MCP server registration.
    server.tool("create_document", {
      filePath: z.string(),
      title: z.string().optional(),
      author: z.string().optional(),
  • Full registration of the 'create_document' tool on the MCP server using McpServer.tool(), including schema and 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,
        };
      }
    });
  • The core helper function in DocumentService that performs the actual document creation using the docx library, sets metadata, packs to buffer, and writes to the specified file path.
    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}` };
      }
    }
  • JSON schema for the 'create_document' tool parameters defined in the HTTP server's 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',
      },
    },

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