get_doctype_fields
Retrieve field definitions for ERPNext document types to understand data structure and enable accurate API interactions.
Instructions
Get fields list for a specific DocType
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| doctype | Yes | ERPNext DocType (e.g., Customer, Item) |
Implementation Reference
- src/index.ts:612-669 (handler)The handler for the 'get_doctype_fields' tool within the CallToolRequestSchema switch statement. It retrieves a sample document for the specified doctype using getDocList, extracts field names, types, and sample values from it, and returns them as JSON.case "get_doctype_fields": { 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); if (!doctype) { throw new McpError( ErrorCode.InvalidParams, "Doctype is required" ); } try { // Get a sample document to understand the fields const documents = await erpnext.getDocList(doctype, {}, ["*"], 1); if (!documents || documents.length === 0) { return { content: [{ type: "text", text: `No documents found for ${doctype}. Cannot determine fields.` }], isError: true }; } // Extract field names from the first document const sampleDoc = documents[0]; const fields = Object.keys(sampleDoc).map(field => ({ fieldname: field, value: typeof sampleDoc[field], sample: sampleDoc[field]?.toString()?.substring(0, 50) || null })); return { content: [{ type: "text", text: JSON.stringify(fields, null, 2) }] }; } catch (error: any) { return { content: [{ type: "text", text: `Failed to get fields for ${doctype}: ${error?.message || 'Unknown error'}` }], isError: true }; } }
- src/index.ts:335-348 (registration)Registration of the 'get_doctype_fields' tool in the ListToolsRequestSchema handler, including its name, description, and input schema.{ name: "get_doctype_fields", description: "Get fields list for a specific DocType", inputSchema: { type: "object", properties: { doctype: { type: "string", description: "ERPNext DocType (e.g., Customer, Item)" } }, required: ["doctype"] } },
- src/index.ts:338-347 (schema)Input schema definition for the 'get_doctype_fields' tool, specifying the required 'doctype' parameter.inputSchema: { type: "object", properties: { doctype: { type: "string", description: "ERPNext DocType (e.g., Customer, Item)" } }, required: ["doctype"] }
- src/index.ts:81-102 (helper)ERPNextClient.getDocList method used by the handler to fetch a sample document and infer fields.// Get list of documents for a doctype async getDocList(doctype: string, filters?: Record<string, any>, fields?: string[], limit?: number): Promise<any[]> { try { let params: Record<string, any> = {}; if (fields && fields.length) { params['fields'] = JSON.stringify(fields); } if (filters) { params['filters'] = JSON.stringify(filters); } if (limit) { params['limit_page_length'] = limit; } const response = await this.axiosInstance.get(`/api/resource/${doctype}`, { params }); return response.data.data; } catch (error: any) { throw new Error(`Failed to get ${doctype} list: ${error?.message || 'Unknown error'}`); }