docx-insertContent
Insert content blocks into Word documents at specified indexes using JSON schema. Query document structure and schema first to ensure accurate placement and formatting.
Instructions
Insert a block at index. Use docx-queryObjects first to see current structure, and docx-getSchema to understand block structure.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| block | Yes | ||
| id | Yes | ||
| index | Yes |
Implementation Reference
- src/docx-utils.ts:221-227 (handler)Core implementation of inserting a new block at the specified index in the document's content array. Updates the JSON model, validates it, and rebuilds the underlying docx Document instance via updateJson.insertContent(id: DocId, index: number, newBlock: any) { return this.updateJson(id, (json) => { const arr = [...json.content]; if (index < 0 || index > arr.length) throw new Error("index out of range"); arr.splice(index, 0, newBlock); return { ...json, content: arr } as DocxJSON; });
- src/index.ts:192-195 (handler)MCP server tool handler that validates input arguments using the tool's schema and calls the DocRegistry.insertContent method.case "docx-insertContent": { const { id, index, block } = parseArgs<{ id: string; index: number; block: any }>(args, tools["docx-insertContent"].inputSchema); const res = registry.insertContent(id, index, block); return ok({ id: res.id, updatedAt: res.updatedAt });
- src/index.ts:65-67 (schema)Tool definition including description and inputSchema used for registration and argument validation in parseArgs."docx-insertContent": { description: "Insert a block at index. Use docx-queryObjects first to see current structure, and docx-getSchema to understand block structure.", inputSchema: { type: "object", required: ["id", "index", "block"], properties: { id: { type: "string" }, index: { type: "integer", minimum: 0 }, block: { $ref: "docx:/$defs/Block" } } }
- src/schema.ts:97-112 (schema)JSON Schema definition for Block type ($defs.Block), referenced by the tool inputSchema's 'block' property for structural validation.Block: { type: "object", oneOf: [ { $ref: "#/$defs/Paragraph" }, { $ref: "#/$defs/Table" }, { $ref: "#/$defs/Image" }, { $ref: "#/$defs/Heading" }, { $ref: "#/$defs/CodeBlock" }, { $ref: "#/$defs/List" }, { $ref: "#/$defs/PageBreak" }, { $ref: "#/$defs/HorizontalRule" }, { $ref: "#/$defs/Blockquote" }, { $ref: "#/$defs/InfoBox" }, { $ref: "#/$defs/TextBox" } ] },
- src/index.ts:101-103 (registration)Registration of tool list handler that exposes the docx-insertContent tool (via the tools object) to MCP clients.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: Object.entries(tools).map(([name, t]) => ({ name, description: t.description, inputSchema: t.inputSchema as any })) }));