Skip to main content
Glama
AgentGuard

AgentGuard

by prachics
README.md
# AgentGuard

A gateway between an AI agent and its MCP tools. The agent connects to AgentGuard as if it were one MCP server; AgentGuard connects to the real ones and checks every tool call against a policy first.

![An agent told by a web page to email the customer list outside: open, it does; guarded, AgentGuard refuses the export and the email](docs/demo.gif)

```mermaid
flowchart LR
    A[AI agent<br/>MCP client] -->|tools/call| G
    subgraph G[AgentGuard]
        P[Policy<br/>rule, role, arg schema] --> AP{Approval?}
        AP -->|no, or approved| R[Rate limit]
    end
    R --> U[(Upstream MCP servers)]
    AP -->|held| DB[(Postgres<br/>approvals, audit log)]
    H[Approver] -->|admin API| DB
    G -.->|each decision| DB
```

## What it does

- **Per-tool policy:** a tool with no rule is neither listed nor callable.
- **Roles:** a rule can limit a tool to named roles. Listings show a caller only what its role may use.
- **Argument constraints:** arguments are validated against a JSON Schema (Ajv, strict mode) before anything is forwarded.
- **Rate limits:** a token bucket per tool and caller.
- **Human approval for writes:** a call waits in Postgres until a named approver approves or denies it on the admin API.
- **Append-only audit log:** every decision, allowed or refused, is written to Postgres. A refusal whose audit write fails is still refused.
- **Prompt-injection suite:** a harness that replays injected tool output against open and guarded runs. See [docs/INJECTION.md](docs/INJECTION.md).

## Policy

```yaml
servers:
  files: {command: npx, args: [tsx, examples/files-server.ts]}
tools:
  files__read_file: {allow: true, kind: read}
  files__write_file:
    allow: true
    kind: write
    roles: [writer]
    approval: true
    args:
      properties:
        path: {type: string, pattern: "^notes/[\\w.-]+$"}
        text: {type: string, maxLength: 10000}
      required: [path, text]
      additionalProperties: false
    rate: {per_minute: 10, burst: 3}
  files__delete_file: {allow: false, kind: write}
```

## Run it

Needs Node.js 22+.

```bash
npm install
npx tsx src/cli.ts --config examples/agentguard.yaml              # stdio
npx tsx src/cli.ts --config examples/agentguard.yaml --http 3333  # Streamable HTTP
```

For approvals and the audit log, start Postgres and the admin API:

```bash
docker compose up -d
export AGENTGUARD_DATABASE_URL='postgresql://agentguard:agentguard_dev_password@127.0.0.1:5435/agentguard'
export AGENTGUARD_ADMIN_KEYS='<16+ character secret>:alice:approver'
npx tsx src/cli.ts --config examples/agentguard.yaml --admin 3334
```

Routes: `GET /admin/approvals`, `POST /admin/approvals/ID/approve|deny`, `GET /admin/audit`.

## Worked example

[`examples/incidentiq`](examples/incidentiq) puts [IncidentIQ](https://github.com/prachics/incidentiq)'s tools behind AgentGuard.

## Layout

| Path | What is there |
|---|---|
| `src/` | The gateway: `gateway.ts` (call flow), `policy.ts` (rules), `admin.ts` (approval and audit API), `store.ts` (Postgres) |
| `examples/` | A sample policy, a sample upstream server, and IncidentIQ's tools |
| `injection/` | Prompt-injection harness and recorded runs |
| `test/` | Vitest suites |

## Tests

```bash
docker compose up -d   # approval and audit tests use Postgres
npm test
npm run typecheck
```

CI runs typecheck, the tests against Postgres, and the build. Full configuration reference: [docs/GUIDE.md](docs/GUIDE.md).

## Stack

TypeScript, Node.js, MCP SDK, PostgreSQL, Ajv, Vitest · MIT licensed