Skip to main content
Glama
mcp-approval-server

mcp-approval-server

by byte-haseeb
README.md
# mcp-approval-server

[![ci](https://github.com/byte-haseeb/mcp-approval-server/actions/workflows/ci.yml/badge.svg)](https://github.com/byte-haseeb/mcp-approval-server/actions/workflows/ci.yml)

An MCP tool server where the agent can read whatever it needs, but every change waits for a named person to approve it.

I've built operations copilots that work like this for clients. That code is under NDA, so this is a small version written from scratch to show the pattern. The "system" here is a toy ticket store.

## The rule

| Tool | Kind | What happens |
|---|---|---|
| `list_tickets`, `get_ticket` | read | Answers straight away. |
| `close_ticket`, `assign_ticket` | write | Creates a **request** and changes nothing. |
| `request_status` | read | Pending, approved, denied, executed or expired. |
| `execute_request` | write | Runs the request only if a person approved it, it hasn't expired, and it hasn't been used. |

There is no "approve" tool. The agent can't approve its own request. Approval sits behind your own UI and calls `gate.approve(id, "name")`, and an approver called "agent" or left blank is refused.

A few more guarantees, all covered by tests:

- `execute_request` runs the **stored** arguments, so the agent can't get "close T-101" approved and then run "close T-102".
- Requests expire after 15 minutes by default.
- Each request runs once.
- Every step (proposed, approved, denied, executed, rejected) goes into an audit log with who did it.
- Write tools check the ticket exists before creating a request, so people aren't asked to approve something impossible.
- Input is validated with zod before it reaches a handler.

## Demo

```
npm install
npm run demo
```

The demo plays the agent through a real MCP client, over an in-memory transport:

```
> agent calls close_ticket {"id":"T-101","reason":"fixed by raising the gateway timeout"}
  status: pending_approval ...
> agent calls execute_request {...}  [refused]
  request is pending, not approved
[person] sara (on-call) approved: Close T-101 (reason: fixed by raising the gateway timeout)
> agent calls execute_request {...}
  status: executed ...
> agent calls execute_request {...}  [refused]
  request is executed, not approved
```

followed by the audit log.

## Tests

```
npm test          # vitest: gate rules, plus the full flow over MCP
npm run typecheck
```

CI runs both, plus the demo, on every push.

## Layout

- `src/approval.ts`: the approval gate (requests, expiry, argument hashing, audit log)
- `src/tools.ts`: tool handlers as plain functions, easy to test
- `src/server.ts`: MCP wiring with `@modelcontextprotocol/sdk`
- `src/tickets.ts`: the toy ticket store
- `src/demo.ts`: the end-to-end walk-through

## License

MIT