MCP Gateway
by sbolla-ai
README.md
# MCP Gateway: governed tool access for enterprise agents
**Agents can read. Only humans can change things. Every call is checked, redacted and recorded.**
Every company rolling out AI agents hits the same wall: how do you let an agent reach ServiceNow, the CMDB and production logs without giving it the keys to production? This gateway sits between agents (Claude, GPT or any MCP client) and enterprise systems, and enforces the controls a bank or hospital security team will ask for before go-live.
```mermaid
flowchart LR
A[Agent / MCP client] -->|bearer JWT| G
subgraph G[MCP Gateway]
direction TB
Q[Tool registry<br/>poisoning scan + pinned hashes] --> R[Rate limit] --> Z[Per-tool RBAC<br/>human-only roles stripped from agents] --> V[Strict arg validation]
V -->|read| X[Execute] --> S[Redact secrets] --> I[Flag injected instructions<br/>mark output untrusted]
V -->|write| AP[Approval queue]
end
H[Human approver] -->|approve / reject<br/>never own request| AP --> C[(Change API)]
X --> L[(Logs)] & D[(CMDB)]
G -.-> AU[(Hash-chained audit log)]
G -.-> OT[(OpenTelemetry traces)]
```
## The controls, and why each exists
| Control | What it stops |
|---|---|
| **Writes are queued, never executed by an agent** | An agent (or an attacker steering it) changing production. A human approves every change. |
| **Separation of duties** | Approving your own request, whether you're an agent or a human. |
| **Human-only roles** | An IdP misconfiguration granting an agent `change_approver`: the gateway strips the role from any agent principal. |
| **Tokens must be minted for this gateway** | Reuse of a token issued for another service (audience check), expired or foreign-issuer tokens. |
| **Tool-poisoning scan + description pinning** | A tool whose description hides instructions to the model, or a reviewed tool that silently changes later (a "rug pull"). Both are quarantined. |
| **Output screening + untrusted label** | Prompt injection planted in data, like a log line telling the agent to disable the WAF. It's flagged, and any change the agent requests afterwards carries a warning to the approver. |
| **Secret redaction** | Passwords, API keys, bearer tokens and private keys in logs reaching the agent, or the audit log. |
| **Strict argument validation** | Unknown fields, oversized input, SQL-injection-shaped values. Backends also use parameterized queries. |
| **Per-principal rate limits** | Runaway agent loops and scraping. Agents get a tighter bucket than humans. |
| **Hash-chained audit log** | Quietly editing or deleting the record of what happened. `verify` names the first broken line. |
| **OpenTelemetry spans** | Blind spots: every call emits an `execute_tool` span with the tool, principal and outcome. |
A note on honesty: pattern-based injection and poisoning detection are tripwires, not guarantees. A determined attacker can phrase around regexes. That's why the load-bearing control is structural: **no write happens without a human**, no matter what the agent was told.
## Quickstart
```bash
pip install -e ".[dev]"
mcpgate demo # incident -> diagnose -> draft change -> agent can't self-approve -> human approves
pytest -q # 15 tests, including real MCP server calls and JWT checks
python redteam/run_redteam.py # 15 attack scenarios -> redteam/SCORECARD.md
```
Use it from an MCP client (Claude Desktop, Claude Code, any MCP 2.x client) over stdio:
```json
{
"mcpServers": {
"enterprise-gateway": {
"command": "mcpgate",
"args": ["serve"],
"env": { "MCPGATE_ALLOW_DEV_TOKENS": "1", "MCPGATE_DEV_TOKEN": "dev-agent-token" }
}
}
}
```
Production runs over streamable HTTP as an OAuth resource server (see `docs/deployment.md`):
```bash
MCPGATE_ISSUER=https://idp.example.com MCPGATE_AUDIENCE=https://gw.example.com/mcp \
MCPGATE_JWKS_URL=https://idp.example.com/.well-known/jwks.json mcpgate serve --transport streamable-http
```
## Red-team scorecard
`redteam/run_redteam.py` runs every scenario against a fresh gateway, and CI fails if any outcome changes. Current result: **15/15**.
| Category | Scenarios |
|---|---|
| Happy path | read logs · draft change (queued) · human approves |
| Privilege escalation | read-only agent writes · agent with mis-granted approver role approves |
| Separation of duties | human approves own request |
| Prompt injection | log line instructs agent · change after injection is flagged |
| Data leak | password in logs redacted |
| Tool poisoning | hidden SSH-key exfiltration instructions · rug-pull description change |
| Input validation | SQL injection in CMDB lookup |
| Abuse / integrity | call flooding · audit log tampering · unknown tool |
## Built on
- **MCP Python SDK 2.x** (`MCPServer`, the bearer `TokenVerifier` hook, tool annotations: `readOnlyHint` / `destructiveHint`).
- **PyJWT** for signature, issuer, audience and expiry checks (JWKS in production, HS256 in tests).
- **OpenTelemetry GenAI semantic conventions** (`execute_tool` spans). These are still at Development status, so attribute names live in one file.
## Status and roadmap
This is v0.1 with **mock backends** (in-memory logs, SQLite CMDB, ServiceNow-shaped change API).
- [ ] Filter `tools/list` per caller at the MCP layer (calls are already denied; listing should hide them too)
- [ ] Real connectors: ServiceNow Table API, Splunk/OpenSearch, Postgres
- [ ] End-to-end HTTP test against a real IdP (Keycloak) with RS256/JWKS
- [ ] Approver UI and Slack approval flow, with the injection flag shown prominently
- [ ] Persistent approval queue and audit sink (Postgres or WORM object storage)
- [ ] LLM-based injection classifier alongside the regex tripwires, measured on a labeled set
## License
MIT © Sreenivas Bolla
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues