create_document
Create new documents in ERPNext by specifying DocType and data, enabling automated record management through structured API calls.
Instructions
Create a new document in ERPNext
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| doctype | Yes | ERPNext DocType (e.g., Customer, Item) | |
| data | Yes | Document data |
Implementation Reference
- src/index.ts:491-529 (handler)MCP tool handler for 'create_document': authenticates, validates parameters (doctype and data), calls erpnext.createDocument, and returns the result or error.case "create_document": { if (!erpnext.isAuthenticated()) { return { content: [{ type: "text", text: "Not authenticated with ERPNext. Please configure API key authentication." }], isError: true }; } const doctype = String(request.params.arguments?.doctype); const data = request.params.arguments?.data as Record<string, any> | undefined; if (!doctype || !data) { throw new McpError( ErrorCode.InvalidParams, "Doctype and data are required" ); } try { const result = await erpnext.createDocument(doctype, data); return { content: [{ type: "text", text: `Created ${doctype}: ${result.name}\n\n${JSON.stringify(result, null, 2)}` }] }; } catch (error: any) { return { content: [{ type: "text", text: `Failed to create ${doctype}: ${error?.message || 'Unknown error'}` }], isError: true }; } }
- src/index.ts:106-115 (helper)ERPNextClient helper method that implements the core logic: HTTP POST to /api/resource/{doctype} with document data, returns the created document.async createDocument(doctype: string, doc: Record<string, any>): Promise<any> { try { const response = await this.axiosInstance.post(`/api/resource/${doctype}`, { data: doc }); return response.data.data; } catch (error: any) { throw new Error(`Failed to create ${doctype}: ${error?.message || 'Unknown error'}`); } }
- src/index.ts:379-397 (registration)Tool registration in ListToolsRequestSchema handler: defines name, description, and input schema for create_document tool.{ name: "create_document", description: "Create a new document in ERPNext", inputSchema: { type: "object", properties: { doctype: { type: "string", description: "ERPNext DocType (e.g., Customer, Item)" }, data: { type: "object", additionalProperties: true, description: "Document data" } }, required: ["doctype", "data"] } },
- src/index.ts:382-396 (schema)Input schema definition for the create_document tool, specifying required doctype (string) and data (object).inputSchema: { type: "object", properties: { doctype: { type: "string", description: "ERPNext DocType (e.g., Customer, Item)" }, data: { type: "object", additionalProperties: true, description: "Document data" } }, required: ["doctype", "data"] }