# casys-ai/mcp-erpnext architecture

> Architecture diagram of the GitHub repository casys-ai/mcp-erpnext, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/casys-ai/mcp-erpnext
- Repository: https://github.com/casys-ai/mcp-erpnext

## Overview

#### 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: [`src/client.ts`](https://github.com/casys-ai/mcp-erpnext/blob/main/src/client.ts) (Server assembly), [`src/api/frappe-client.ts`](https://github.com/casys-ai/mcp-erpnext/blob/main/src/api/frappe-client.ts) (Frappe REST client), [`src/tools/mod.ts`](https://github.com/casys-ai/mcp-erpnext/blob/main/src/tools/mod.ts) (Tool registration), [`src/ui/viewers.ts`](https://github.com/casys-ai/mcp-erpnext/blob/main/src/ui/viewers.ts) (Viewer resources)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_server["MCP Server"]
  node_entry["Runtime entry points<br/>launch/export<br/>[server.ts]"]
  node_client["Server assembly<br/>MCP composition<br/>[client.ts]"]
  node_runtime["Runtime bindings<br/>Node/Deno adapter<br/>[runtime.ts]"]
  node_platform{{"MCP server framework<br/>dependency"}}
  node_cache[("Cache strategies<br/>read cache<br/>[cache.ts]")]
  node_mrtr["MRTR mutation safety<br/>retry/replay protection"]
end

subgraph group_erp["ERPNext Boundary"]
  node_auth["Auth configuration<br/>credentials/auth<br/>[config.ts]"]
  node_metadata["OAuth resource metadata<br/>HTTP metadata route"]
  node_resolve["API resolution<br/>URL/config resolver<br/>[resolve.ts]"]
  node_frappe["Frappe REST client<br/>ERPNext API boundary<br/>[frappe-client.ts]"]
  node_erpnext[("ERPNext / Frappe<br/>external system")]
end

subgraph group_tools["Domain Tools"]
  node_toolreg["Tool registration<br/>tool registry<br/>[mod.ts]"]
  node_domains["Domain tool modules<br/>MCP tools<br/>[sales.ts]"]
  node_workflows["Tool workflow contracts<br/>shared mutation logic<br/>[submit-helpers.ts]"]
  node_analytics["Analytics tools<br/>MCP tools<br/>[analytics.ts]"]
  node_kanban_tools["Kanban tools<br/>MCP tools<br/>[kanban.ts]"]
end

subgraph group_ui["Interactive Viewers"]
  node_resources["Viewer resources<br/>MCP App resources<br/>[viewers.ts]"]
  node_viewers["React viewer apps<br/>MCP App UI<br/>[DoclistViewer.tsx]"]
  node_kanban_model["Kanban adapters<br/>board model adapters<br/>[task.ts]"]
  node_kanban_state["Kanban board state<br/>client interaction<br/>[useKanbanBoard.ts]"]
end

node_entry -->|"starts/exports"| node_client
node_client -->|"uses"| node_runtime
node_client -->|"built on"| node_platform
node_client -->|"loads auth"| node_auth
node_client -->|"registers"| node_toolreg
node_client -->|"registers"| node_resources
node_client -->|"uses"| node_cache
node_client -.->|"optional"| node_mrtr
node_auth -->|"supplies configuration"| node_resolve
node_platform -->|"serves"| node_metadata
node_resolve -->|"configures"| node_frappe
node_toolreg -->|"includes"| node_domains
node_toolreg -->|"includes"| node_analytics
node_toolreg -->|"includes"| node_kanban_tools
node_domains -->|"shares"| node_workflows
node_domains -->|"CRUD and workflows"| node_frappe
node_analytics -->|"aggregates data"| node_frappe
node_kanban_tools -->|"mutates documents"| node_frappe
node_cache -.->|"caches reads around"| node_frappe
node_mrtr -.->|"protects retries"| node_frappe
node_frappe -->|"REST"| node_erpnext
node_domains -->|"attaches viewer results"| node_resources
node_analytics -->|"attaches chart results"| node_resources
node_kanban_tools -->|"attaches board results"| node_resources
node_resources -->|"loads"| node_viewers
node_viewers -.->|"follow-up tool calls"| node_toolreg
node_kanban_model -->|"feeds board model"| node_kanban_state
node_kanban_state -.->|"refreshes and mutates"| node_kanban_tools

click node_entry "https://github.com/casys-ai/mcp-erpnext/blob/main/server.ts"
click node_client "https://github.com/casys-ai/mcp-erpnext/blob/main/src/client.ts"
click node_runtime "https://github.com/casys-ai/mcp-erpnext/blob/main/src/runtime.ts"
click node_auth "https://github.com/casys-ai/mcp-erpnext/blob/main/src/auth/config.ts"
click node_metadata "https://github.com/casys-ai/mcp-erpnext/blob/main/src/auth/resource-metadata-route.ts"
click node_resolve "https://github.com/casys-ai/mcp-erpnext/blob/main/src/api/resolve.ts"
click node_frappe "https://github.com/casys-ai/mcp-erpnext/blob/main/src/api/frappe-client.ts"
click node_toolreg "https://github.com/casys-ai/mcp-erpnext/blob/main/src/tools/mod.ts"
click node_domains "https://github.com/casys-ai/mcp-erpnext/blob/main/src/tools/sales.ts"
click node_workflows "https://github.com/casys-ai/mcp-erpnext/blob/main/src/tools/submit-helpers.ts"
click node_analytics "https://github.com/casys-ai/mcp-erpnext/blob/main/src/tools/analytics.ts"
click node_kanban_tools "https://github.com/casys-ai/mcp-erpnext/blob/main/src/tools/kanban.ts"
click node_cache "https://github.com/casys-ai/mcp-erpnext/blob/main/src/cache/cache.ts"
click node_mrtr "https://github.com/casys-ai/mcp-erpnext/blob/main/src/mrtr/link-disambiguation.ts"
click node_resources "https://github.com/casys-ai/mcp-erpnext/blob/main/src/ui/viewers.ts"
click node_viewers "https://github.com/casys-ai/mcp-erpnext/blob/main/src/ui/doclist-viewer/src/DoclistViewer.tsx"
click node_kanban_model "https://github.com/casys-ai/mcp-erpnext/blob/main/src/kanban/adapters/task.ts"
click node_kanban_state "https://github.com/casys-ai/mcp-erpnext/blob/main/src/ui/shared/kanban/useKanbanBoard.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry,node_client,node_runtime,node_platform,node_cache,node_mrtr toneBlue
class node_auth,node_metadata,node_resolve,node_frappe,node_erpnext toneAmber
class node_toolreg,node_domains,node_workflows,node_analytics,node_kanban_tools toneMint
class node_resources,node_viewers,node_kanban_model,node_kanban_state toneRose
```

## Components

### MCP Server

- [Runtime entry points](https://github.com/casys-ai/mcp-erpnext/blob/main/server.ts): launch/export · Stdio launch and package export. · `server.ts`
- [Server assembly](https://github.com/casys-ai/mcp-erpnext/blob/main/src/client.ts): MCP composition · Composes transport, tools, resources, cache, and optional MRTR. · `src/client.ts`
- [Runtime bindings](https://github.com/casys-ai/mcp-erpnext/blob/main/src/runtime.ts): Node/Deno adapter · Isolates runtime-specific bindings. · `src/runtime.ts`
- **MCP server framework**: dependency · Provides transport, auth, Apps, concurrency, and observability.
- [Cache strategies](https://github.com/casys-ai/mcp-erpnext/blob/main/src/cache/cache.ts): read cache · Memory, no-op, and warming cache for repeated metadata and list reads. · `src/cache/cache.ts`
- [MRTR mutation safety](https://github.com/casys-ai/mcp-erpnext/blob/main/src/mrtr/link-disambiguation.ts): retry/replay protection · Handles ambiguous links and signed mutation retries. · `src/mrtr/link-disambiguation.ts`

### ERPNext Boundary

- [Auth configuration](https://github.com/casys-ai/mcp-erpnext/blob/main/src/auth/config.ts): credentials/auth · Resolves environment credentials and optional composite auth. · `src/auth/config.ts`
- [OAuth resource metadata](https://github.com/casys-ai/mcp-erpnext/blob/main/src/auth/resource-metadata-route.ts): HTTP metadata route · `src/auth/resource-metadata-route.ts`
- [API resolution](https://github.com/casys-ai/mcp-erpnext/blob/main/src/api/resolve.ts): URL/config resolver · `src/api/resolve.ts`
- [Frappe REST client](https://github.com/casys-ai/mcp-erpnext/blob/main/src/api/frappe-client.ts): ERPNext API boundary · Dependency-free boundary for documents, workflows, attachments, and queries. · `src/api/frappe-client.ts`
- **ERPNext / Frappe**: external system

### Domain Tools

- [Tool registration](https://github.com/casys-ai/mcp-erpnext/blob/main/src/tools/mod.ts): tool registry · Registers filtered ERPNext domain tool groups. · `src/tools/mod.ts`
- [Domain tool modules](https://github.com/casys-ai/mcp-erpnext/blob/main/src/tools/sales.ts): MCP tools · Sales, inventory, accounting, HR, projects, purchasing, manufacturing, CRM, assets, and operations. · `src/tools/sales.ts`
- [Tool workflow contracts](https://github.com/casys-ai/mcp-erpnext/blob/main/src/tools/submit-helpers.ts): shared mutation logic · Centralizes tool contracts and submit/cancel behavior. · `src/tools/submit-helpers.ts`
- [Analytics tools](https://github.com/casys-ai/mcp-erpnext/blob/main/src/tools/analytics.ts): MCP tools · Produces KPI, funnel, and chart payloads. · `src/tools/analytics.ts`
- [Kanban tools](https://github.com/casys-ai/mcp-erpnext/blob/main/src/tools/kanban.ts): MCP tools · Drives board mutations. · `src/tools/kanban.ts`

### Interactive Viewers

- [Viewer resources](https://github.com/casys-ai/mcp-erpnext/blob/main/src/ui/viewers.ts): MCP App resources · Maps and registers ui:// viewer resources. · `src/ui/viewers.ts`
- [React viewer apps](https://github.com/casys-ai/mcp-erpnext/blob/main/src/ui/doclist-viewer/src/DoclistViewer.tsx): MCP App UI · Document, invoice, chart, and Kanban viewers render results and request follow-up tools. · `src/ui/doclist-viewer/src/DoclistViewer.tsx`
- [Kanban adapters](https://github.com/casys-ai/mcp-erpnext/blob/main/src/kanban/adapters/task.ts): board model adapters · Converts Task, Opportunity, and Issue documents into a shared board model. · `src/kanban/adapters/task.ts`
- [Kanban board state](https://github.com/casys-ai/mcp-erpnext/blob/main/src/ui/shared/kanban/useKanbanBoard.ts): client interaction · Owns drag interactions and refresh behavior. · `src/ui/shared/kanban/useKanbanBoard.ts`

## Connections

- 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
