generate_report
Create structured Word documents from data payloads with Markdown formatting, tables, and sections for reports and documentation.
Instructions
Generates a complete Word document based on a structured content payload.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes | Filename for the generated report (e.g. 'audit_report') | |
| title | Yes | Title of the document | |
| sections | Yes | List of sections to include in the report |
Implementation Reference
- src/word-generator.ts:31-57 (handler)Core handler function that generates the Word report document by creating sections, paragraphs, tables using docx library, packing to buffer, and saving to output directory.public async generateReport(data: ReportData): Promise<string> { const doc = new Document({ sections: [ { children: [ new Paragraph({ text: data.title, heading: HeadingLevel.TITLE, spacing: { after: 200, }, }), ...this.renderSections(data.sections), ], }, ], }); const buffer = await Packer.toBuffer(doc); const fileNameWithExt = data.filename.endsWith(".docx") ? data.filename : `${data.filename}.docx`; const filePath = path.join(this.outputDir, fileNameWithExt); fs.writeFileSync(filePath, buffer); return filePath; }
- src/index.ts:23-36 (schema)Zod schema defining the input structure for the generate_report tool, including filename, title, and sections with optional content and tables.const GenerateReportSchema = z.object({ filename: z.string().describe("Filename for the generated report (e.g. 'audit_report')"), title: z.string().describe("Title of the document"), sections: z.array( z.object({ heading: z.string(), content: z.string().optional().describe("Markdown-supported text content for this section"), table: z.object({ headers: z.array(z.string()), rows: z.array(z.array(z.string())), }).optional(), }) ).describe("List of sections to include in the report"), }).describe("Generates a complete Word document based on a structured content payload.");
- src/index.ts:39-68 (registration)Registers the generate_report tool with the MCP server, providing name, description, input schema, and a handler that delegates to WordGenerator.generateReport and returns success/error message.server.tool( "generate_report", "Generates a complete Word document based on a structured content payload.", GenerateReportSchema.shape, async (args) => { try { console.error(`Generating report: ${args.filename}`); // Log to stderr for debugging const filePath = await wordGenerator.generateReport(args); return { content: [ { type: "text", text: `Report generated successfully at ${filePath}` } ], }; } catch (error) { console.error("Error generating report:", error); return { content: [ { type: "text", text: `Error generating report: ${error instanceof Error ? error.message : String(error)}` } ], isError: true, }; } } );
- src/word-generator.ts:14-18 (schema)TypeScript interface defining the ReportData type used by the generateReport handler, matching the tool schema.export interface ReportData { filename: string; title: string; sections: ReportSection[]; }
- src/word-generator.ts:59-99 (helper)Helper method to render report sections into docx Paragraph and Table elements, handling headings, markdown content, and tables.private renderSections(sections: ReportSection[]): (Paragraph | Table)[] { const elements: (Paragraph | Table)[] = []; for (const section of sections) { // detailed heading elements.push( new Paragraph({ text: section.heading, heading: HeadingLevel.HEADING_1, spacing: { before: 200, after: 100, }, }) ); // plain content (basic markdown support: splitting by lines) if (section.content) { const lines = section.content.split("\n"); for (const line of lines) { // Basic bold handling for **text** // This is a very primitive parser. const textRuns = this.parseMarkdownLine(line); elements.push( new Paragraph({ children: textRuns, spacing: { after: 100 } }) ); } } // table if (section.table) { elements.push(this.createTable(section.table.headers, section.table.rows)); } } return elements;