How casys-ai/mcp-erpnext works
Purpose and Entry Points
@casys/mcp-erpnext is an MCP server that lets AI agents operate ERPNext/Frappe through domain-specific tools and embedded interactive MCP App viewers. It supports stdio and HTTP deployment, self-hosted and Frappe Cloud ERPNext.
Primary entry points are server.ts (Deno/stdio launch), mod.ts (package export), and src/client.ts (application/server assembly). Runtime-specific bindings isolate Node and Deno differences in src/runtime.ts, src/runtime.node.ts, and src/runtime.deno.ts.
MCP Server Composition
The server is built on @casys/mcp-server, which supplies MCP transport, concurrency, auth support, MCP Apps, and observability. src/client.ts composes ERPNext configuration, REST access, tool registration, viewer resources, caching, and optional MRTR behavior. Category filtering limits which tool groups are registered.
HTTP hosting is container-ready through Dockerfile and docker-compose.yml; Node builds are produced by scripts/build-node.sh.
ERPNext/Frappe Integration Boundary
All ERPNext interaction crosses a dependency-free Frappe REST boundary in src/api/frappe-client.ts. It uses resolved configuration and URL behavior from src/api/resolve.ts, with shared API types in src/api/types.ts.
Credentials and base URL originate from environment configuration in src/auth/config.ts. Authentication can be composed through src/auth/composite-provider.ts; OAuth/MCP resource metadata is exposed through src/auth/resource-metadata-route.ts. This boundary separates MCP-facing inputs from ERPNext document, workflow, attachment, and query APIs.
Business Tool Layer
Tool modules organize ERPNext operations by domain: sales, inventory, accounting, HR, projects, purchasing, manufacturing, CRM, assets, and generic operations. Core registrations live in src/tools/mod.ts; representative modules are src/tools/sales.ts, src/tools/inventory.ts, and src/tools/operations.ts.
Tools translate MCP arguments into Frappe API operations, including document CRUD, submit/cancel workflows, assignment, uploads, and analytics. Shared tool contracts and workflow behavior are centralized in src/tools/types.ts and src/tools/submit-helpers.ts.
Caching and Safe Mutation
A cache abstraction permits memory-backed, no-op, and warming strategies: src/cache/types.ts, src/cache/cache.ts, and src/cache/warm.ts. This is a distinct performance boundary around repeated metadata/list reads, rather than a replacement for ERPNext state.
MRTR support handles ambiguous link resolution and signed retries for mutation flows in src/mrtr/config.ts and src/mrtr/link-disambiguation.ts. Its default replay protection is process-local; multi-instance deployment requires an external shared atomic replay store, such as Redis.
Viewer Resource and Interaction Pipeline
Tool results can attach registered ui://mcp-erpnext/{name} resources. Resource mapping and registration are handled by src/ui/viewers.ts, src/ui/viewer-resource-paths.ts, and src/ui/mod.ts. The server injects navigation and drill-down metadata into results, while viewers use MCP sendMessage to request follow-up tool calls and callServerTool refreshes.
Each viewer is independently built React UI. Key applications include generic document lists (src/ui/doclist-viewer/src/DoclistViewer.tsx), invoices (src/ui/invoice-viewer/src/InvoiceViewer.tsx), analytics charts (src/ui/chart-viewer/src/ChartViewer.tsx), and read/write boards (src/ui/kanban-viewer/src/KanbanViewer.tsx).
Kanban and Analytics Specializations
Kanban converts Task, Opportunity, and Issue documents into a shared board model through adapters in src/kanban/adapters/task.ts, src/kanban/adapters/opportunity.ts, and src/kanban/adapters/issue.ts. Tool endpoints in src/tools/kanban.ts drive mutation; client-side state, drag interactions, and refresh behavior reside in src/ui/shared/kanban/useKanbanBoard.ts.
Analytics tools in src/tools/analytics.ts produce structured KPI, funnel, and chart payloads consumed by dedicated viewers, keeping ERPNext aggregation/tool logic separate from React rendering.
Main entry points: client.ts Server assembly · frappe-client.ts Frappe REST client · mod.ts Tool registration · viewers.ts Viewer resources
Components 20
MCP Server
- Runtime entry points · Stdio launch and package export.
server.ts - Server assembly · Composes transport, tools, resources, cache, and optional MRTR.
src/client.ts - Runtime bindings · Isolates runtime-specific bindings.
src/runtime.ts - MCP server framework · Provides transport, auth, Apps, concurrency, and observability.
- Cache strategies · Memory, no-op, and warming cache for repeated metadata and list reads.
src/cache/cache.ts - MRTR mutation safety · Handles ambiguous links and signed mutation retries.
src/mrtr/link-disambiguation.ts
ERPNext Boundary
- Auth configuration · Resolves environment credentials and optional composite auth.
src/auth/config.ts - OAuth resource metadata
src/auth/resource-metadata-route.ts - API resolution
src/api/resolve.ts - Frappe REST client · Dependency-free boundary for documents, workflows, attachments, and queries.
src/api/frappe-client.ts - ERPNext / Frappe
Domain Tools
- Tool registration · Registers filtered ERPNext domain tool groups.
src/tools/mod.ts - Domain tool modules · Sales, inventory, accounting, HR, projects, purchasing, manufacturing, CRM, assets, and operations.
src/tools/sales.ts - Tool workflow contracts · Centralizes tool contracts and submit/cancel behavior.
src/tools/submit-helpers.ts - Analytics tools · Produces KPI, funnel, and chart payloads.
src/tools/analytics.ts - Kanban tools · Drives board mutations.
src/tools/kanban.ts
Interactive Viewers
- Viewer resources · Maps and registers ui:// viewer resources.
src/ui/viewers.ts - React viewer apps · Document, invoice, chart, and Kanban viewers render results and request follow-up tools.
src/ui/doclist-viewer/src/DoclistViewer.tsx - Kanban adapters · Converts Task, Opportunity, and Issue documents into a shared board model.
src/kanban/adapters/task.ts - Kanban board state · Owns drag interactions and refresh behavior.
src/ui/shared/kanban/useKanbanBoard.ts
Connections 28
- Runtime entry points → Server assembly: starts/exports
- Server assembly → Runtime bindings: uses
- Server assembly → MCP server framework: built on
- Server assembly → Auth configuration: loads auth
- Server assembly → Tool registration: registers
- Server assembly → Viewer resources: registers
- Server assembly → Cache strategies: uses
- Server assembly → MRTR mutation safety: optional
- Auth configuration → API resolution: supplies configuration
- MCP server framework → OAuth resource metadata: serves
- API resolution → Frappe REST client: configures
- Tool registration → Domain tool modules: includes
- Tool registration → Analytics tools: includes
- Tool registration → Kanban tools: includes
- Domain tool modules → Tool workflow contracts: shares
- Domain tool modules → Frappe REST client: CRUD and workflows
- Analytics tools → Frappe REST client: aggregates data
- Kanban tools → Frappe REST client: mutates documents
- Cache strategies → Frappe REST client: caches reads around
- MRTR mutation safety → Frappe REST client: protects retries
- Frappe REST client → ERPNext / Frappe: REST
- Domain tool modules → Viewer resources: attaches viewer results
- Analytics tools → Viewer resources: attaches chart results
- Kanban tools → Viewer resources: attaches board results
- Viewer resources → React viewer apps: loads
- React viewer apps → Tool registration: follow-up tool calls
- Kanban adapters → Kanban board state: feeds board model
- Kanban board state → Kanban tools: refreshes and mutates