get_documents
Retrieve ERPNext documents by doctype with optional filters, fields selection, and result limits for data management.
Instructions
Get a list of documents for a specific doctype
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| doctype | Yes | ERPNext DocType (e.g., Customer, Item) | |
| fields | No | Fields to include (optional) | |
| filters | No | Filters in the format {field: value} (optional) | |
| limit | No | Maximum number of documents to return (optional) |
Implementation Reference
- src/index.ts:449-489 (handler)Handler for the 'get_documents' tool call. Extracts parameters, validates doctype, calls erpnext.getDocList, and returns the JSON list or error.case "get_documents": { 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 fields = request.params.arguments?.fields as string[] | undefined; const filters = request.params.arguments?.filters as Record<string, any> | undefined; const limit = request.params.arguments?.limit as number | undefined; if (!doctype) { throw new McpError( ErrorCode.InvalidParams, "Doctype is required" ); } try { const documents = await erpnext.getDocList(doctype, filters, fields, limit); return { content: [{ type: "text", text: JSON.stringify(documents, null, 2) }] }; } catch (error: any) { return { content: [{ type: "text", text: `Failed to get ${doctype} documents: ${error?.message || 'Unknown error'}` }], isError: true }; } }
- src/index.ts:349-378 (registration)Registration of the 'get_documents' tool in listTools response, including name, description, and input schema.{ name: "get_documents", description: "Get a list of documents for a specific doctype", inputSchema: { type: "object", properties: { doctype: { type: "string", description: "ERPNext DocType (e.g., Customer, Item)" }, fields: { type: "array", items: { type: "string" }, description: "Fields to include (optional)" }, filters: { type: "object", additionalProperties: true, description: "Filters in the format {field: value} (optional)" }, limit: { type: "number", description: "Maximum number of documents to return (optional)" } }, required: ["doctype"] } },
- src/index.ts:352-377 (schema)Input schema definition for the 'get_documents' tool, specifying parameters like doctype, fields, filters, and limit.inputSchema: { type: "object", properties: { doctype: { type: "string", description: "ERPNext DocType (e.g., Customer, Item)" }, fields: { type: "array", items: { type: "string" }, description: "Fields to include (optional)" }, filters: { type: "object", additionalProperties: true, description: "Filters in the format {field: value} (optional)" }, limit: { type: "number", description: "Maximum number of documents to return (optional)" } }, required: ["doctype"] }
- src/index.ts:82-103 (helper)Core helper function getDocList in ERPNextClient class that performs the API call to fetch documents list based on doctype, filters, fields, and limit.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'}`); } }