@@ -6,10 +6,89 @@ import type {
66 OcxToolCall ,
77 OcxToolResultMessage ,
88} from "../../types" ;
9- import { namespacedToolName } from "../../types" ;
9+ import { isAllowedToolChoice , namespacedToolName , toolChoiceAliases , type OcxTool , type OcxToolChoice } from "../../types" ;
1010import type { CursorRequestMessage , CursorRunRequest } from "./types" ;
1111import { cursorCodexToWireModelId } from "./discovery" ;
1212import { cursorEffortSuffix } from "./effort-map" ;
13+ import {
14+ cursorMcpToolEncodedSize ,
15+ cursorMcpToolsEncodedSize ,
16+ cursorToolAllowedByChoice ,
17+ cursorToolWireName ,
18+ cursorToolsForActivePrompt ,
19+ } from "./tool-definitions" ;
20+
21+ /** Probe-verified Cursor Connect boundaries, with byte headroom for the enclosing field. */
22+ export const CURSOR_TOOL_COUNT_LIMIT = 330 ;
23+ export const CURSOR_TOOL_BYTES_LIMIT = 120_000 ;
24+
25+ interface CursorToolBudgetResult {
26+ tools : OcxTool [ ] ;
27+ omitted : OcxTool [ ] ;
28+ }
29+
30+ function explicitlySelectedNames ( choice : OcxToolChoice | undefined ) : Set < string > {
31+ if ( ! choice || choice === "auto" || choice === "none" || choice === "required" ) return new Set ( ) ;
32+ return new Set ( "name" in choice ? [ choice . name ] : isAllowedToolChoice ( choice ) ? choice . allowedTools : [ ] ) ;
33+ }
34+
35+ function toolPriority ( tool : OcxTool , selectedNames : ReadonlySet < string > ) : number {
36+ if ( toolChoiceAliases ( tool ) . some ( name => selectedNames . has ( name ) ) ) return 0 ;
37+ if ( tool . loadedFromToolSearch ) return 1 ;
38+ if ( ! tool . namespace ) return 2 ;
39+ return 3 ;
40+ }
41+
42+ /**
43+ * Select one catalog used by both Cursor protobuf registration and call recognition.
44+ * Actual McpTools serialization is measured after every candidate so descriptions,
45+ * names, provider identifiers, and schemas all count toward the byte ceiling.
46+ */
47+ export function applyCursorToolBudget (
48+ tools : readonly OcxTool [ ] | undefined ,
49+ toolChoice : OcxToolChoice | undefined ,
50+ ) : CursorToolBudgetResult {
51+ const eligible = ( tools ?? [ ] ) . filter ( tool => cursorToolAllowedByChoice ( tool , toolChoice ) ) ;
52+ if (
53+ eligible . length <= CURSOR_TOOL_COUNT_LIMIT
54+ && cursorMcpToolsEncodedSize ( eligible , toolChoice ) <= CURSOR_TOOL_BYTES_LIMIT
55+ ) return { tools : [ ...eligible ] , omitted : [ ] } ;
56+
57+ const selectedNames = explicitlySelectedNames ( toolChoice ) ;
58+ const candidates = eligible
59+ . map ( ( tool , index ) => ( { tool, index, priority : toolPriority ( tool , selectedNames ) } ) )
60+ . sort ( ( a , b ) => a . priority - b . priority || a . index - b . index ) ;
61+ const kept : OcxTool [ ] = [ ] ;
62+ const keptSet = new Set < OcxTool > ( ) ;
63+ let keptBytes = 0 ;
64+
65+ for ( const candidate of candidates ) {
66+ if ( kept . length >= CURSOR_TOOL_COUNT_LIMIT ) continue ;
67+ // Repeated protobuf message fields serialize as concatenated tag/length/value entries,
68+ // so each one-entry wrapper size is the exact additive contribution to McpTools.
69+ const candidateBytes = cursorMcpToolEncodedSize ( candidate . tool , toolChoice ) ;
70+ if ( keptBytes + candidateBytes > CURSOR_TOOL_BYTES_LIMIT ) continue ;
71+ kept . push ( candidate . tool ) ;
72+ keptSet . add ( candidate . tool ) ;
73+ keptBytes += candidateBytes ;
74+ }
75+
76+ return {
77+ tools : eligible . filter ( tool => keptSet . has ( tool ) ) ,
78+ omitted : eligible . filter ( tool => ! keptSet . has ( tool ) ) ,
79+ } ;
80+ }
81+
82+ function catalogLimitNote ( kept : readonly OcxTool [ ] , omitted : readonly OcxTool [ ] ) : string | undefined {
83+ if ( omitted . length === 0 ) return undefined ;
84+ const recoverable = kept . some ( tool => tool . toolSearch || cursorToolWireName ( tool ) === "tool_search" ) ;
85+ const names = omitted . slice ( 0 , 12 ) . map ( cursorToolWireName ) ;
86+ const remainder = omitted . length - names . length ;
87+ const omittedSummary = `${ names . join ( ", " ) } ${ remainder > 0 ? `, and ${ remainder } more` : "" } ` ;
88+ return recoverable
89+ ? `[opencodex] Cursor's transport limit allows ${ kept . length } of ${ kept . length + omitted . length } client tools this turn. Omitted: ${ omittedSummary } . Use tool_search for a needed omitted tool; tools returned by tool_search are prioritized on the next turn.`
90+ : `[opencodex] Cursor's transport limit allows ${ kept . length } of ${ kept . length + omitted . length } client tools this turn. Omitted and unavailable this turn: ${ omittedSummary } .` ;
91+ }
1392
1493/**
1594 * Resolve a `cursor/<model>` selection + Codex reasoning effort to the actual Cursor model id. Cursor
@@ -80,19 +159,24 @@ export function generatedCursorConversationId(): string {
80159}
81160
82161export function createCursorRequest ( parsed : OcxParsedRequest ) : CursorRunRequest {
162+ const messages = parsed . context . messages
163+ . map ( requestMessage )
164+ . filter ( ( message ) : message is CursorRequestMessage => ! ! message && message . content . length > 0 ) ;
165+ const activeText = [ ...messages ] . reverse ( ) . find ( message => message . role === "user" || message . role === "developer" ) ?. content ?? "" ;
166+ const visibleTools = cursorToolsForActivePrompt ( parsed . context . tools , activeText , parsed . options . toolChoice ) ;
167+ const budget = applyCursorToolBudget ( visibleTools , parsed . options . toolChoice ) ;
168+ const limitNote = catalogLimitNote ( budget . tools , budget . omitted ) ;
83169 return {
84170 modelId : normalizeCursorModelId ( parsed . modelId , parsed . options . reasoning ) ,
85171 // The Cursor conversation id comes ONLY from remembered state (_cursorConversationId). Do NOT fall
86172 // back to the OpenAI Responses previous_response_id (resp_*): that is a Responses-chain id in a
87173 // different namespace and would start an unrelated Cursor conversation, breaking tool-result
88174 // continuation. If we have no remembered Cursor conversation, start a fresh one.
89175 conversationId : parsed . _cursorConversationId ?? generatedCursorConversationId ( ) ,
90- system : [ ...( parsed . context . systemPrompt ?? [ ] ) ] ,
91- messages : parsed . context . messages
92- . map ( requestMessage )
93- . filter ( ( message ) : message is CursorRequestMessage => ! ! message && message . content . length > 0 ) ,
176+ system : [ ...( parsed . context . systemPrompt ?? [ ] ) , ...( limitNote ? [ limitNote ] : [ ] ) ] ,
177+ messages,
94178 rawMessages : parsed . context . messages ,
95- ...( parsed . context . tools ? .length ? { tools : parsed . context . tools } : { } ) ,
179+ ...( budget . tools . length ? { tools : budget . tools } : { } ) ,
96180 ...( parsed . options . toolChoice ? { toolChoice : parsed . options . toolChoice } : { } ) ,
97181 ...( parsed . options . parallelToolCalls !== undefined ? { parallelToolCalls : parsed . options . parallelToolCalls } : { } ) ,
98182 } ;
0 commit comments