Agent-Hellboy / Agent-Hellboy/mcp-runtime

perf(policy): index gateway session, tool, and grant lookups

Aperta
#319 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Go
Stelle
6
Fork
1
Merge medio
11h 33m
PR unite (30g)
13

Descrizione

## Problem

Each MCP server gateway loads its rendered `policy.json` into memory, but authorization currently performs linear scans across the policy document on every tool call:

- `findSession` scans `policy.Sessions`
- `resolveToolMetadata` scans `policy.Tools`
- `matchingGrants` scans `policy.Grants`
- `bestGrantFor` scans matching grants and their tool rules

This is straightforward for small policies, but request cost grows linearly as a server accumulates hundreds or thousands of sessions, grants, and tool rules. We currently do not have benchmarks defining the practical limit.

There is a separate control-plane scaling concern: `renderGatewayPolicy` lists all `MCPAgentSession` and `MCPAccessGrant` resources visible to the operator, then filters them by server reference for each reconciliation.

## Proposed work

### Gateway data plane

- Add authorization benchmarks using representative policies with 100, 1,000, and 10,000 sessions/grants.
- Compile immutable indexes when `policy.json` is loaded, for example:
- sessions by session ID
- tools by tool name
- grants by subject identity/team key where semantics permit
- Atomically swap the compiled policy snapshot on successful reload.
- Keep the previous valid snapshot when reload or compilation fails.
- Preserve existing matching behavior for human ID, agent ID, team ID, optional sessions, revocation, expiration, trust, and policy versions.

### Operator control plane

- Measure reconciliation cost with many sessions, grants, and MCP servers.
- Investigate indexed cache queries, field indexes, or labels to avoid cluster-wide list-and-filter operations per server reconciliation.
- Keep this work separable from the gateway hot-path optimization if needed.

## Acceptance criteria

- Benchmarks cover successful and missing session lookups at 100, 1,000, and 10,000 entries.
- Request-time session and tool lookup no longer scan the complete corresponding slices.
- Existing `pkg/policy` tests pass without behavioral changes.
- Race tests confirm policy reload and concurrent authorization remain safe.
- Benchmark results before and after the change are included in the PR.
- Operator scaling findings are documented or split into a follow-up issue with measured evidence.

## Relevant code

- `pkg/policy/evaluator.go`
- `services/mcp-gateway/policy_cache.go`
- `internal/operator/policy.go`

This is performance hardening rather than a confirmed production incident until benchmarks establish current latency and throughput limits.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.