Repository navigation
fix(cursor): enforce tool budget to prevent resource_exhausted with large catalogs - #192
alexanderxcmo wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8cf40bc303
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
|
||
| export function createCursorRequest(parsed: OcxParsedRequest): CursorRunRequest { | ||
| const rawTools = parsed.context.tools ?? []; | ||
| const { tools: budgetedTools, trimmed } = enforceCursorToolBudget(rawTools); |
There was a problem hiding this comment.
Preserve toolChoice-selected tools before trimming
When a large catalog has tool_choice targeting a namespaced tool in a namespace that sorts past the budget, this trims that namespace before Cursor tool definitions are built; buildCursorToolDefinitions() then filters the already-trimmed request.tools by request.toolChoice, so the forced/allowed tool is never registered and the model cannot satisfy the client's required tool call. The budget pass should reserve any named/allowed tools before dropping optional namespaces.
Useful? React with 👍 / 👎.
| system: [...(parsed.context.systemPrompt ?? [])], | ||
| system: [ | ||
| ...(parsed.context.systemPrompt ?? []), | ||
| ...(trimmed ? [CURSOR_DEFERRED_TOOLS_NOTE] : []), |
There was a problem hiding this comment.
Only suggest tool_search when it is advertised
For callers that send a large fully-enumerated MCP catalog without a tool_search tool, trimming still appends a system note telling the model that dropped tools are available via tool_search, but no such tool is registered because this code only keeps tools from the original catalog. In that scenario Cursor will reject/ignore the suggested call and the model has no way to load the dropped namespace, so the note should be conditional on an advertised/allowed tool_search or use different wording.
Useful? React with 👍 / 👎.
| const sorted = [...namespaceGroups.entries()].sort((a, b) => a[1].length - b[1].length); | ||
|
|
||
| let remaining = CURSOR_TOOL_BUDGET - native.length; | ||
| const kept: OcxTool[] = [...native]; |
There was a problem hiding this comment.
Cap oversized bare-function catalogs too
If a request contains more than 200 non-namespaced function/custom tools, native alone can exceed the Cursor registration limit and this keeps every one of them, leaving request.tools over budget and trimmed false when there are no namespace groups. Since the limit described here counts native plus namespace-inner tools, large flat catalogs can still hit the same resource_exhausted failure this change is meant to prevent.
Useful? React with 👍 / 👎.
| let trimmed = false; | ||
|
|
||
| for (const [, group] of sorted) { | ||
| if (group.length <= remaining) { |
There was a problem hiding this comment.
Keep searched tools even when siblings are trimmed
When the model follows the new note and tool_search_output loads a specific tool from a large namespace, the next request still re-groups by the original namespace and only keeps it if the whole namespace fits. For a previously dropped oversized namespace this drops the searched tool again, so the advertised recovery path via tool_search never makes that tool callable unless loaded tools are reserved independently of their full namespace group.
Useful? React with 👍 / 👎.
…arge catalogs Cursor Connect returns resource_exhausted when the aggregate tool catalog exceeds an internal registration limit (~340 tools fails, ~230 works). This is not a quota issue — it is a transport-level resource budget. Add a Cursor-specific tool budget (default 200) that: - Always preserves native (non-namespace) tools - Adds namespaces smallest-first until the budget is full - Drops remaining namespaces (model can still discover them via tool_search) - Appends a system-prompt note when trimming occurs so the model knows to use tool_search for unavailable tools Fixes lidge-jun#190
…rched tools, conditional note, cap native - Preserve tools targeted by toolChoice even when their namespace is trimmed - Keep previously-used tools (from tool_search) across turns - Only suggest tool_search in deferred note when it exists in kept catalog - Cap oversized bare-function (non-namespace) catalogs at budget - Add 4 new unit tests covering each review finding - Update existing deferred-note test to include tool_search in catalog
dbcb418 to
7741d91
Compare
Review notes - what should change before treating this as doneDirection is right as a crash mitigation (trim before 1. Do not claim
|
…-tool capacity Probe-verified 2026-07-21: Cursor Connect has two limits: - COUNT limit: ~332 tools (resource_exhausted at 333+) - BYTE limit: ~128 KB protobuf payload for mcp_tools Old approach (budget=200) dropped ~140 tools entirely. New hybrid approach: 1. Raise count budget to 330 (from 200) 2. Keep full schemas for tools within 120 KB byte budget 3. Stub inputSchema for tools beyond byte budget (name+desc only) 4. Inject text schema catalog into system prompt for stubbed tools 5. tool_search still available for on-demand full schema loading Result: all 340 tools visible to model, most with full schemas, remainder with stub+catalog. No tools permanently inaccessible. Addresses Wibias review: real post-tool_search reservation via stubbedToolNames tracking + text catalog fallback. 19 tests pass (4 new for hybrid behavior), tsc clean.
Probe results & hybrid approach (commit 5fff484)Live probe 2026-07-21Ran a controlled probe against
Finding: Cursor has TWO independent limits:
The old budget of 200 was conservative against the byte limit. With stub schemas we can reach 330 tools (just under the count cap). New hybrid approach (this commit)Instead of dropping tools beyond the budget, we now:
Result: all 340 tools visible to the model. Most have full structured schemas; the remainder have stub schemas + text catalog entry. No tools permanently inaccessible. Addresses review findings
Tests19 tests pass (4 new for hybrid behavior), New tests cover:
|
|
Please retarget this PR to the |
Adversarial Review FeedbackGood problem identification — the Cursor 332-tool count ceiling and ~128KB byte ceiling are real constraints. Blockers
Please retarget to |
|
Closing this PR — the problem identification (Cursor's ~332 tool count ceiling and ~128KB byte ceiling) is valuable research, but the implementation has three fundamental issues that need an architectural rethink:
The probe data on Cursor's limits is useful reference material. Thank you for the research — we'll tackle this with a serialization-aware budget approach. 🙏 Co-authored-by: alexanderxcmo |
Problem
Cursor Connect returns
resource_exhaustedwhen the aggregate tool catalog exceeds an internal registration limit. This is not a quota issue — the account has remaining credits and native Cursor works fine.Reproduced with 340 namespace-inner tools (443 KB serialized). Both halves of the catalog (230 and 110 tools) work independently, confirming the failure depends on the aggregate size, not a single malformed schema.
See #190 for full reproduction, measurements, and isolation results.
Fix
Add a Cursor-specific tool budget (default 200) in
createCursorRequest:exec_command,apply_patch,web_search,tool_search, etc.tool_search.tool_searchfor unavailable tools.Why 200?
Empirically determined: 230 tools succeed, 340 fail. 200 provides safe margin while still covering most real-world setups (typical Codex installs with a few MCP servers stay well under 200).
Testing
cursor-request-builder.test.ts:tsc --noEmitcleanFuture work
A more complete fix would implement full deferred loading for Cursor (send only tool names in the initial
mcp_toolsregistration, load full schemas ontool_search). This PR provides an immediate, safe mitigation that unblocks all users with large plugin/app catalogs.