create_document
Create ERPNext documents by specifying DocType and data parameters. Add customers, items, invoices, or custom records to your system through the MCP server.
Instructions
Create a new document in ERPNext
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| doctype | Yes | ERPNext DocType (e.g., Customer, Item) | |
| data | Yes | Document data |
Implementation Reference
- src/index.ts:489-527 (handler)The main handler for the 'create_document' tool call. Validates authentication, extracts doctype and data parameters, calls erpnext.createDocument(), and returns the created document 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:377-395 (schema)The input schema (and registration) for the 'create_document' tool. Defines the JSON Schema for tool input validation with required parameters: doctype (string) and data (object).
{ 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:104-113 (helper)The helper method in ERPNextClient class that makes the actual HTTP POST request to the ERPNext API to create a document. Wraps the document data and handles errors.
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'}`); } }