Skip to content

MCP usage limits: add server-authoritative meter backend#592

Merged
saleh-mir merged 1 commit into
masterfrom
feat/mcp-server-usage-meter
Jun 25, 2026
Merged

MCP usage limits: add server-authoritative meter backend#592
saleh-mir merged 1 commit into
masterfrom
feat/mcp-server-usage-meter

Conversation

@saleh-mir

@saleh-mir saleh-mir commented Jun 25, 2026

Copy link
Copy Markdown
Member

Adds an opt-in "remote" backend to the MCP usage gate (jesse/mcp/usage_limits.py).

What

With MCP_USAGE_METER=remote, each gated research run (backtest / Monte Carlo / optimization / significance) checks its remaining daily allowance against the licensing backend instead of the local Redis counter:

  • The backend resolves the plan from the license token, holds the counter, and enforces the limit — so the limit is authoritative (the local counter is trivially bypassable, since the user controls their own Redis).
  • The gate reads as "ask the backend how many runs I have left today."

Fail-open

_remote_usage_decision(feature, action) returns None on any error / non-200 / unreachable backend, and the gate then runs the tool anyway — the limit check never disrupts usage.

Default unchanged / activation

Default stays "local" (the existing Redis counter + api1 plan read), so behavior is unchanged until activated. Flip MCP_USAGE_METER=remote in production.

Tests

6 new tests for the remote path (allowed, blocked-free, blocked-guest, fail-open, request/response shape, non-200 fail-open). All 26 pass.

@saleh-mir saleh-mir force-pushed the feat/mcp-server-usage-meter branch from 5f8a970 to 8352b5b Compare June 25, 2026 16:13
@saleh-mir saleh-mir changed the title MCP usage limits: server-authoritative meter via api2 /usage MCP usage limits: add server-authoritative meter backend Jun 25, 2026
Adds an opt-in "remote" backend to the MCP usage gate. With MCP_USAGE_METER=remote,
each gated research run (backtest / Monte Carlo / optimization / significance) checks
its remaining daily allowance against the licensing backend, which resolves the plan
from the license token, holds the counter, and enforces the limit. This makes the
limit authoritative — the local Redis counter is trivially bypassable since the user
controls their own Redis.

- New _remote_usage_decision(feature): asks the backend for the remaining daily
  allowance; FAILS OPEN (returns None) on any error, so the limit check never disrupts
  the feature.
- The local Redis meter + api1 plan read remain the default; activate with
  MCP_USAGE_METER=remote.

6 new tests; all 26 pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@saleh-mir saleh-mir force-pushed the feat/mcp-server-usage-meter branch from 8352b5b to 87aa6d4 Compare June 25, 2026 16:23
@saleh-mir saleh-mir merged commit 3076e36 into master Jun 25, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant