create_document
Create new documents in ERPNext by specifying the DocType and providing the required data fields for records like customers or items.
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:489-527 (handler)Primary MCP tool handler for 'create_document'. Validates authentication and parameters, invokes ERPNextClient.createDocument, handles success/error responses.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:104-113 (helper)Core implementation in ERPNextClient that performs the HTTP POST request to ERPNext API to create a new 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:380-394 (schema)Input schema definition for the 'create_document' tool, specifying required 'doctype' and 'data' parameters.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:377-395 (registration)Tool registration in the ListToolsRequestSchema handler, defining name, description, and input schema for 'create_document'.{ 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"] } },