Skip to content

Commit 91d6925

Browse files
committed
fix(cursor): resolve client tool choices before catalog filtering
1 parent 6d83a52 commit 91d6925

3 files changed

Lines changed: 79 additions & 5 deletions

File tree

‎src/adapters/cursor/request-builder.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,21 @@ export function createCursorRequest(
509509
): CursorRunRequest {
510510
const messages = cursorRequestMessagesFromRaw(parsed.context.messages);
511511
const activeText = [...messages].reverse().find(message => message.role === "user" || message.role === "developer")?.content ?? "";
512-
const visibleTools = cursorToolsForActivePrompt(parsed.context.tools, activeText, parsed.options.toolChoice);
513-
const budget = applyCursorToolBudget(visibleTools, parsed.options.toolChoice);
512+
const catalog = parsed.context.tools ?? [];
513+
const originalChoice = parsed.options.toolChoice;
514+
// Resolve accepted bare wire aliases before filtering can remove their shell-bridge context.
515+
// Keep the original selection so a semantic name cannot widen to a namespaced sibling.
516+
const selectedTools = catalog.filter(tool => cursorToolAllowedByChoice(tool, originalChoice, catalog));
517+
const semanticChoiceName = (name: string): string => selectedTools.find(tool =>
518+
!tool.namespace && cursorToolWireName(tool, catalog) === name
519+
&& cursorToolAllowedByChoice(tool, { name }, catalog))?.name ?? name;
520+
const toolChoice = originalChoice && typeof originalChoice === "object"
521+
? "name" in originalChoice
522+
? { ...originalChoice, name: semanticChoiceName(originalChoice.name) }
523+
: { ...originalChoice, allowedTools: originalChoice.allowedTools.map(semanticChoiceName) }
524+
: originalChoice;
525+
const visibleTools = cursorToolsForActivePrompt(selectedTools, activeText, toolChoice);
526+
const budget = applyCursorToolBudget(visibleTools, toolChoice);
514527
const limitNote = catalogLimitNote(budget.tools, budget.omitted);
515528
const model = normalizeCursorModelId(
516529
parsed.modelId,
@@ -536,7 +549,7 @@ export function createCursorRequest(
536549
...(budget.tools.length === 0 && !cursorClientThreadOwner(parsed)
537550
? { suppressDefaultCursorToolCatalog: true }
538551
: {}),
539-
...(parsed.options.toolChoice ? { toolChoice: parsed.options.toolChoice } : {}),
552+
...(toolChoice ? { toolChoice } : {}),
540553
...(parsed.options.parallelToolCalls !== undefined ? { parallelToolCalls: parsed.options.parallelToolCalls } : {}),
541554
};
542555
const resolved = resolveCursorCheckpoint(parsed, request, options);

‎structure/providers/cursor.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ Cursor-private tool collisions. Other non-proxy-owned bare tools always receive
6262
proxy-owned execution/edit tools and namespaced MCP tools retain their existing identities.
6363
Registration, guidance, denied-native-exec redirects, live call mapping, and protobuf history
6464
replay use this catalog-dependent rule. Return mapping restores the client's semantic tool name.
65+
Request construction resolves accepted bare wire aliases in forced and allowed-tool choices to
66+
semantic names against the original catalog, retaining only the originally selected identities
67+
before prompt and budget filtering so a removed bridge cannot invalidate registration or widen
68+
the choice to a namespaced sibling.
6569

6670
`src/adapters/cursor/tool-definitions.ts` measures actual repeated protobuf entries using the
6771
same catalog context for tool-choice matching and wire naming as registration.

‎tests/providers/cursor/cursor-blob-claude-client-tools.test.ts‎

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import {
77
} from "../../../src/adapters/cursor/native-exec";
88
import { resetAppOwnedMemoryForTests } from "../../../src/lib/app-owned-memory";
99
import { encodeCursorRunRequest } from "../../../src/adapters/cursor/protobuf-request";
10-
import { applyCursorToolBudget, CURSOR_TOOL_BYTES_LIMIT } from "../../../src/adapters/cursor/request-builder";
11-
import { cursorMcpToolEncodedSize, cursorMcpToolsEncodedSize } from "../../../src/adapters/cursor/tool-definitions";
10+
import { applyCursorToolBudget, createCursorRequest, CURSOR_TOOL_BYTES_LIMIT } from "../../../src/adapters/cursor/request-builder";
11+
import { buildCursorToolDefinitions, cursorMcpToolEncodedSize, cursorMcpToolsEncodedSize } from "../../../src/adapters/cursor/tool-definitions";
12+
import { parseRequest } from "../../../src/responses/parser";
1213
import type { OcxTool } from "../../../src/types";
1314
import { resetCursorCallIdProvenanceForTests } from "../../../src/adapters/cursor/call-id";
1415
import {
@@ -107,6 +108,62 @@ describe("Cursor blob handshake", () => {
107108
});
108109

109110
describe("Cursor Claude client tool catalog budget", () => {
111+
test.each([
112+
{ bridge: true, name: "Read", expected: ["Read"] },
113+
{ bridge: true, name: "ocx_client_Read", expected: ["Read"] },
114+
{ bridge: false, name: "Read", expected: ["Read"] },
115+
{ bridge: false, name: "ocx_client_Read", expected: [] },
116+
])("registers forced $name with bridge=$bridge after choice filtering", ({ bridge, name, expected }) => {
117+
const parsed = parseRequest({
118+
model: "cursor/auto", input: "read a file",
119+
tools: [...(bridge ? ["exec_command"] : []), "Read", "Write"].map(name => ({ type: "function", name, parameters: {} })),
120+
tool_choice: { type: "function", name },
121+
});
122+
const request = createCursorRequest(parsed);
123+
expect(buildCursorToolDefinitions(request.tools, request.toolChoice).map(tool => tool.toolName)).toEqual(expected);
124+
});
125+
126+
test.each([
127+
{ allowed: ["ocx_client_Read"], expected: ["Read"] },
128+
{ allowed: ["exec_command", "ocx_client_Read"], expected: ["exec_command", "ocx_client_Read"] },
129+
])("registers allowed aliases $allowed after choice filtering", ({ allowed, expected }) => {
130+
const parsed = parseRequest({
131+
model: "cursor/auto", input: "read a file",
132+
tools: ["exec_command", "Read", "Write"].map(name => ({ type: "function", name, parameters: {} })),
133+
tool_choice: { type: "allowed_tools", mode: "required", tools: allowed.map(name => ({ type: "function", name })) },
134+
});
135+
const request = createCursorRequest(parsed);
136+
expect(buildCursorToolDefinitions(request.tools, request.toolChoice).map(tool => tool.toolName)).toEqual(expected);
137+
});
138+
139+
test.each(["function", "allowed_tools"])("keeps an aliased %s choice scoped to its original bare identity", type => {
140+
const parsed = parseRequest({
141+
model: "cursor/auto", input: "read a file",
142+
tools: [
143+
{ type: "function", name: "exec_command", parameters: {} },
144+
{ type: "function", name: "Read", parameters: {} },
145+
{ type: "namespace", name: "mcp__remote", tools: [{ type: "function", name: "Read", parameters: {} }] },
146+
],
147+
tool_choice: type === "function"
148+
? { type, name: "ocx_client_Read" }
149+
: { type, mode: "required", tools: [{ type: "function", name: "ocx_client_Read" }] },
150+
});
151+
const request = createCursorRequest(parsed);
152+
expect(request.tools?.map(tool => [tool.namespace, tool.name])).toEqual([[undefined, "Read"]]);
153+
expect(buildCursorToolDefinitions(request.tools, request.toolChoice).map(tool => tool.toolName)).toEqual(["Read"]);
154+
});
155+
156+
test("preserves both allowed semantic names when a literal name shadows a generated alias", () => {
157+
const parsed = parseRequest({
158+
model: "cursor/auto", input: "read a file",
159+
tools: ["exec_command", "Read", "ocx_client_Read"].map(name => ({ type: "function", name, parameters: {} })),
160+
tool_choice: { type: "allowed_tools", mode: "required", tools: ["Read", "ocx_client_Read"].map(name => ({ type: "function", name })) },
161+
});
162+
const request = createCursorRequest(parsed);
163+
expect(buildCursorToolDefinitions(request.tools, request.toolChoice).map(tool => tool.toolName))
164+
.toEqual(["Read", "ocx_client_ocx_client_Read"]);
165+
});
166+
110167
test("individual sizes use catalog wire names and aliased tool choices", () => {
111168
const bridge: OcxTool = { name: "exec_command", parameters: {} };
112169
const read: OcxTool = { name: "Read", parameters: {} };

0 commit comments

Comments
 (0)