Skip to content

Count tool calls per workspace against plan limits (dry-run) - #1996

Merged
helpcodeai merged 3 commits into
mainfrom
feat/mcp-workspace-limits
Dec 5, 2026
Merged

helpcodeai merged 3 commits into
mainfrom
feat/mcp-workspace-limits

Conversation

@helpcodeai

Copy link
Copy Markdown
Contributor

Summary

Adds per-workspace limits on tool calls, by plan, in dry-run by default: calls are counted and logged, never refused.

Today one workspace can load the shared backend for hours. On Cloud the only general limit is McpPrincipalRateLimitGuard, 1,000 MCP requests per minute per caller. Per-tool limits exist only where an admin set them on a role. On 2026-12-05 one trial workspace made 25,000 tool calls in a day, more than 4,000 in some hours. The backend's median /mcp time was highest in exactly those hours.

Three limits per workspace (workspace-call-limits.ts):

Plan per minute per hour at once
trial (no card) 120 1,000 10
starter 240 2,000 15
team (also card trial) 480 5,000 30
business 1,000 15,000 60
enterprise, no licence, unknown no limit

The values come from the peaks of 2026-11-28 to 12-05 on Cloud. Per minute, only 2 trial bursts would have gone over. Per hour, 2 trial and 3 starter workspaces would have gone over, most of them sustained polling. No team or business workspace would have.

MCP_WORKSPACE_LIMITS:

  • off: nothing is counted.
  • dry-run: counted and logged, never refused. This is the default on Cloud.
  • on: a call over a limit gets a tool error with retryAfter, and the refusal is logged like the existing per-tool rate limit. A refused call uses no quota.

Self-hosted defaults to off.

The dry-run also logs, once an hour, how many calls repeated the same tool and arguments within 10 s. That tells us whether a default short cache for repeated reads is worth building.

Changes

  • mcp-server/workspace-call-limits.ts: WorkspaceCallLimiter.
    • Counters live in the process, so no Redis round trip is added to each call.
    • The plan comes from LicenseGuardService.codeRunPlanForOrg, cached for 60 s, with in-flight lookups shared.
    • It fails open: no plan, an error or no workspace lets the call through.
    • Memory is bounded at 20k workspaces and 50k repeat keys.
  • dynamic-mcp-tools.ts:
    • executeTool becomes a thin wrapper: admit, run executeToolUnlimited, release in finally.
    • The context and result types are extracted unchanged as ExecuteToolContext and ExecuteToolResult.
    • The limiter is @Optional, so existing fixtures keep working.
  • mcp-server.module.ts: registers the provider.
  • docker-compose.cloud.yml: passes MCP_WORKSPACE_LIMITS through. Empty means dry-run.
  • Docs: docs/deployment.md, .env.example, regenerated llms-full.txt.

Log lines to watch in dry-run:

  • [workspace limits dry-run] org …abc123 (trial): more than 1000 tool calls in an hour
  • ... would have refused N calls ... in the hour that just ended
  • [workspace limits] repeats in the last hour: ...

Ids are shortened. No emails, arguments or results are logged.

Type

  • New feature (behind a flag; dry-run changes no response)

Testing

  • Added/updated unit tests:
    • workspace-call-limits.spec.ts: modes; plans; dry-run never refuses; per-minute, per-hour and concurrency refusals in on; refusals use no quota; windows reset; workspaces stay apart; plan lookup failure lets the call through; plan cache; repeat statistics.
    • dynamic-mcp-tools.workspace-limits.spec.ts: the slot is released after success and after a throw; a refusal does not run the tool and is logged; it works without the limiter.
  • In packages/backend after prisma generate:
    • npx tsc --noEmit -p tsconfig.json: clean.
    • npx eslint on the changed files: clean.
    • npx jest --maxWorkers=4 src/mcp-server src/license: 85 suites passed, 3 skipped (baseline), 1,984 tests.
  • Root: node scripts/generate-llms-txt.mjs --check: fresh. docker compose -f docker-compose.cloud.yml config: the variable is present and empty.

Checklist

  • My code follows the existing code style
  • I have tested my changes locally
  • I have updated documentation (if applicable)
  • All existing tests pass (npm test): full suite in CI
  • This PR has a descriptive title

Cloud: dry-run after deploy; nothing is refused. on comes later, by setting MCP_WORKSPACE_LIMITS=on in .env after a week of logs.
Self-hosted: off by default. An admin can opt in.

No database migration. No change under ee/.

Related Issues

Daily check 2026-12-05 (backend slow at peak hours; one workspace polling for hours).

@helpcodeai
helpcodeai merged commit 242cebe into main Dec 5, 2026
16 checks passed
@helpcodeai
helpcodeai deleted the feat/mcp-workspace-limits branch December 5, 2026 21:24
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant