# sriram687/agent_lease architecture

> Architecture diagram of the GitHub repository sriram687/agent_lease, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sriram687/agent_lease
- Repository: https://github.com/sriram687/agent_lease

## Overview

Agent Leash is a sandbox payment-safety gateway between autonomous agents and PayPal. Its principal flow is: an agent searches for products and requests a purchase through MCP; Leash resolves the agent’s mandate, evaluates policy, then denies, escalates for human review, or authorizes payment. Delivery events govern capture or voiding, while the cockpit provides escalation, mandate-control, and ledger views. The graph preserves documented policy and settlement stages; several integrations and handlers are TODOs or unsampled, so those links are shown only where documentation supports them.

Main entry points: [`apps/gateway/src/mcp/server.ts`](https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/mcp/server.ts) (MCP tools), [`packages/db/src/schema/mandates.ts`](https://github.com/sriram687/agent_lease/blob/master/packages/db/src/schema/mandates.ts) (Postgres data), [`packages/policy-engine/src/engine.ts`](https://github.com/sriram687/agent_lease/blob/master/packages/policy-engine/src/engine.ts) (Policy evaluation), [`apps/gateway/src/orchestrator/payment-orchestrator.ts`](https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/orchestrator/payment-orchestrator.ts) (Payment orchestration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_gateway["Gateway and access"]
  node_mcp["MCP tools<br/>[server.ts]"]
  node_mandate_auth["Mandate resolution<br/>[resolve-mandate.ts]"]
  node_product_search["Product search<br/>[channel3.ts]"]
end

subgraph group_policy["Purchase policy"]
  node_policy_engine["Policy evaluation<br/>[engine.ts]"]
  node_deterministic["Rule checks<br/>[deterministic.ts]"]
  node_semantic_dedup["Semantic dedup<br/>[semantic-dedup.ts]"]
  node_llm_judge["Intent judge<br/>[llm-judge.ts]"]
  node_injection_screen["Injection screen"]
end

subgraph group_payments["Payment lifecycle"]
  node_payment_orchestrator["Payment orchestration"]
  node_webhook_routes["Webhook handling<br/>[webhooks.ts]"]
  node_escalations["Escalation API<br/>[escalations.ts]"]
  node_mandate_routes["Mandate controls<br/>[mandates.ts]"]
end

subgraph group_cockpit["Human cockpit"]
  node_cockpit_escalations["Escalation inbox"]
  node_kill_switch["Mandate kill switch<br/>[KillSwitch.tsx]"]
  node_ledger_view["Ledger view<br/>[page.tsx]"]
  node_ledger_grid["Ledger grid<br/>[LedgerGrid.tsx]"]
end

subgraph group_data["Persistence and audit"]
  node_ledger_service["Ledger service<br/>[ledger-service.ts]"]
  node_database[("Postgres data<br/>[mandates.ts]")]
  node_mandate_schema["Mandate records<br/>[mandates.ts]"]
  node_purchase_schema["Purchase records"]
end

node_agent(("AI agent"))
node_channel3["Channel3"]
node_openai["OpenAI"]
node_paypal["PayPal sandbox"]
node_demo_store["Demo store<br/>[server.ts]"]
node_human(("Human owner"))

node_agent -->|"requests via MCP"| node_mcp
node_mcp -->|"resolves mandate"| node_mandate_auth
node_mcp -->|"searches products"| node_product_search
node_product_search -->|"fetches products"| node_channel3
node_mcp -->|"evaluates purchase"| node_policy_engine
node_policy_engine -->|"runs rules"| node_deterministic
node_policy_engine -->|"checks similarity"| node_semantic_dedup
node_policy_engine -->|"judges intent"| node_llm_judge
node_policy_engine -->|"screens output"| node_injection_screen
node_llm_judge -->|"requests judgment"| node_openai
node_semantic_dedup -->|"generates embeddings"| node_openai
node_deterministic -->|"reads purchase history"| node_database
node_semantic_dedup -->|"queries similar items"| node_database
node_mcp -->|"records and settles"| node_payment_orchestrator
node_payment_orchestrator -.->|"authorizes then captures"| node_paypal
node_paypal -->|"sends payment events"| node_webhook_routes
node_demo_store -->|"sends delivery events"| node_webhook_routes
node_webhook_routes -.->|"triggers settlement"| node_payment_orchestrator
node_mcp -->|"appends decision"| node_ledger_service
node_ledger_service -.->|"writes ledger entries"| node_database
node_human -->|"reviews requests"| node_cockpit_escalations
node_cockpit_escalations -->|"approves or rejects"| node_escalations
node_escalations -.->|"resolves escalation"| node_payment_orchestrator
node_human -->|"revokes mandate"| node_kill_switch
node_kill_switch -->|"requests revocation"| node_mandate_routes
node_mandate_routes -.->|"voids open authorizations"| node_payment_orchestrator
node_ledger_view -->|"renders entries"| node_ledger_grid
node_ledger_view -->|"loads ledger data"| node_database
node_mandate_schema -->|"defines mandate table"| node_database
node_purchase_schema -->|"defines request table"| node_database

click node_mcp "https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/mcp/server.ts"
click node_mandate_auth "https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/auth/resolve-mandate.ts"
click node_product_search "https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/integrations/channel3.ts"
click node_policy_engine "https://github.com/sriram687/agent_lease/blob/master/packages/policy-engine/src/engine.ts"
click node_deterministic "https://github.com/sriram687/agent_lease/blob/master/packages/policy-engine/src/layers/deterministic.ts"
click node_semantic_dedup "https://github.com/sriram687/agent_lease/blob/master/packages/policy-engine/src/layers/semantic-dedup.ts"
click node_llm_judge "https://github.com/sriram687/agent_lease/blob/master/packages/policy-engine/src/layers/llm-judge.ts"
click node_injection_screen "https://github.com/sriram687/agent_lease/blob/master/packages/policy-engine/src/layers/injection-screen.ts"
click node_payment_orchestrator "https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/orchestrator/payment-orchestrator.ts"
click node_webhook_routes "https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/routes/webhooks.ts"
click node_demo_store "https://github.com/sriram687/agent_lease/blob/master/apps/demo-store/src/server.ts"
click node_escalations "https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/routes/escalations.ts"
click node_mandate_routes "https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/routes/mandates.ts"
click node_cockpit_escalations "https://github.com/sriram687/agent_lease/blob/master/apps/cockpit/src/components/escalations/EscalationInbox.tsx"
click node_kill_switch "https://github.com/sriram687/agent_lease/blob/master/apps/cockpit/src/components/mandates/KillSwitch.tsx"
click node_ledger_view "https://github.com/sriram687/agent_lease/blob/master/apps/cockpit/src/app/ledger/page.tsx"
click node_ledger_grid "https://github.com/sriram687/agent_lease/blob/master/apps/cockpit/src/components/ledger/LedgerGrid.tsx"
click node_ledger_service "https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/ledger/ledger-service.ts"
click node_database "https://github.com/sriram687/agent_lease/blob/master/packages/db/src/schema/mandates.ts"
click node_mandate_schema "https://github.com/sriram687/agent_lease/blob/master/packages/db/src/schema/mandates.ts"
click node_purchase_schema "https://github.com/sriram687/agent_lease/blob/master/packages/db/src/schema/purchase-requests.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_mcp,node_mandate_auth,node_product_search toneBlue
class node_policy_engine,node_deterministic,node_semantic_dedup,node_llm_judge,node_injection_screen toneAmber
class node_payment_orchestrator,node_webhook_routes,node_escalations,node_mandate_routes toneMint
class node_cockpit_escalations,node_kill_switch,node_ledger_view,node_ledger_grid toneRose
class node_ledger_service,node_database,node_mandate_schema,node_purchase_schema,node_agent,node_channel3,node_openai,node_paypal,node_human toneIndigo
class node_demo_store toneTeal
```

## Components

### Gateway and access

- [MCP tools](https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/mcp/server.ts): `apps/gateway/src/mcp/server.ts`
- [Mandate resolution](https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/auth/resolve-mandate.ts): `apps/gateway/src/auth/resolve-mandate.ts`
- [Product search](https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/integrations/channel3.ts): `apps/gateway/src/integrations/channel3.ts`

### Purchase policy

- [Policy evaluation](https://github.com/sriram687/agent_lease/blob/master/packages/policy-engine/src/engine.ts): `packages/policy-engine/src/engine.ts`
- [Rule checks](https://github.com/sriram687/agent_lease/blob/master/packages/policy-engine/src/layers/deterministic.ts): `packages/policy-engine/src/layers/deterministic.ts`
- [Semantic dedup](https://github.com/sriram687/agent_lease/blob/master/packages/policy-engine/src/layers/semantic-dedup.ts): `packages/policy-engine/src/layers/semantic-dedup.ts`
- [Intent judge](https://github.com/sriram687/agent_lease/blob/master/packages/policy-engine/src/layers/llm-judge.ts): `packages/policy-engine/src/layers/llm-judge.ts`
- [Injection screen](https://github.com/sriram687/agent_lease/blob/master/packages/policy-engine/src/layers/injection-screen.ts): `packages/policy-engine/src/layers/injection-screen.ts`

### Payment lifecycle

- [Payment orchestration](https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/orchestrator/payment-orchestrator.ts): `apps/gateway/src/orchestrator/payment-orchestrator.ts`
- [Webhook handling](https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/routes/webhooks.ts): `apps/gateway/src/routes/webhooks.ts`
- [Escalation API](https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/routes/escalations.ts): `apps/gateway/src/routes/escalations.ts`
- [Mandate controls](https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/routes/mandates.ts): `apps/gateway/src/routes/mandates.ts`

### Human cockpit

- [Escalation inbox](https://github.com/sriram687/agent_lease/blob/master/apps/cockpit/src/components/escalations/EscalationInbox.tsx): `apps/cockpit/src/components/escalations/EscalationInbox.tsx`
- [Mandate kill switch](https://github.com/sriram687/agent_lease/blob/master/apps/cockpit/src/components/mandates/KillSwitch.tsx): `apps/cockpit/src/components/mandates/KillSwitch.tsx`
- [Ledger view](https://github.com/sriram687/agent_lease/blob/master/apps/cockpit/src/app/ledger/page.tsx): `apps/cockpit/src/app/ledger/page.tsx`
- [Ledger grid](https://github.com/sriram687/agent_lease/blob/master/apps/cockpit/src/components/ledger/LedgerGrid.tsx): `apps/cockpit/src/components/ledger/LedgerGrid.tsx`

### Persistence and audit

- [Ledger service](https://github.com/sriram687/agent_lease/blob/master/apps/gateway/src/ledger/ledger-service.ts): `apps/gateway/src/ledger/ledger-service.ts`
- [Postgres data](https://github.com/sriram687/agent_lease/blob/master/packages/db/src/schema/mandates.ts): `packages/db/src/schema/mandates.ts`
- [Mandate records](https://github.com/sriram687/agent_lease/blob/master/packages/db/src/schema/mandates.ts): `packages/db/src/schema/mandates.ts`
- [Purchase records](https://github.com/sriram687/agent_lease/blob/master/packages/db/src/schema/purchase-requests.ts): `packages/db/src/schema/purchase-requests.ts`

### Other components

- **AI agent**
- **Channel3**
- **OpenAI**
- **PayPal sandbox**
- [Demo store](https://github.com/sriram687/agent_lease/blob/master/apps/demo-store/src/server.ts): `apps/demo-store/src/server.ts`
- **Human owner**

## Connections

- AI agent → MCP tools: requests via MCP
- MCP tools → Mandate resolution: resolves mandate (evidence: `apps/gateway/src/mcp/server.ts`)
- MCP tools → Product search: searches products (evidence: `apps/gateway/src/mcp/server.ts`)
- Product search → Channel3: fetches products
- MCP tools → Policy evaluation: evaluates purchase (evidence: `apps/gateway/src/mcp/server.ts`)
- Policy evaluation → Rule checks: runs rules (evidence: `packages/policy-engine/src/engine.ts`)
- Policy evaluation → Semantic dedup: checks similarity (evidence: `packages/policy-engine/src/engine.ts`)
- Policy evaluation → Intent judge: judges intent (evidence: `packages/policy-engine/src/engine.ts`)
- Policy evaluation → Injection screen: screens output (evidence: `packages/policy-engine/src/engine.ts`)
- Intent judge → OpenAI: requests judgment (evidence: `packages/policy-engine/src/layers/llm-judge.ts`)
- Semantic dedup → OpenAI: generates embeddings (evidence: `packages/policy-engine/src/layers/semantic-dedup.ts`)
- Rule checks → Postgres data: reads purchase history (evidence: `packages/policy-engine/src/layers/deterministic.ts`)
- Semantic dedup → Postgres data: queries similar items (evidence: `packages/policy-engine/src/layers/semantic-dedup.ts`)
- MCP tools → Payment orchestration: records and settles (evidence: `apps/gateway/src/mcp/server.ts`)
- Payment orchestration → PayPal sandbox: authorizes then captures
- PayPal sandbox → Webhook handling: sends payment events
- Demo store → Webhook handling: sends delivery events
- Webhook handling → Payment orchestration: triggers settlement
- MCP tools → Ledger service: appends decision (evidence: `apps/gateway/src/mcp/server.ts`)
- Ledger service → Postgres data: writes ledger entries
- Human owner → Escalation inbox: reviews requests
- Escalation inbox → Escalation API: approves or rejects
- Escalation API → Payment orchestration: resolves escalation
- Human owner → Mandate kill switch: revokes mandate
- Mandate kill switch → Mandate controls: requests revocation
- Mandate controls → Payment orchestration: voids open authorizations
- Ledger view → Ledger grid: renders entries (evidence: `apps/cockpit/src/app/ledger/page.tsx`)
- Ledger view → Postgres data: loads ledger data (evidence: `apps/cockpit/src/app/ledger/page.tsx`)
- Mandate records → Postgres data: defines mandate table (evidence: `packages/db/src/schema/mandates.ts`)
- Purchase records → Postgres data: defines request table (evidence: `packages/db/src/schema/purchase-requests.ts`)
