pi-cursor-sdk 0.1.13 → 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -0
- package/README.md +71 -32
- package/docs/cursor-model-ux-spec.md +23 -9
- package/docs/cursor-native-tool-replay.md +88 -0
- package/docs/cursor-native-tool-visual-audit.md +183 -0
- package/package.json +5 -2
- package/src/bundled-context-windows.ts +5 -2
- package/src/context.ts +34 -11
- package/src/cursor-fallback-models.generated.ts +4068 -71
- package/src/cursor-mcp-timeout-override.ts +111 -0
- package/src/cursor-native-tool-display.ts +397 -46
- package/src/cursor-pi-tool-bridge.ts +637 -0
- package/src/cursor-provider.ts +477 -81
- package/src/cursor-question-tool.ts +247 -0
- package/src/cursor-session-cwd.ts +33 -0
- package/src/cursor-tool-names.ts +67 -0
- package/src/cursor-tool-transcript.ts +730 -61
- package/src/index.ts +7 -0
|
@@ -0,0 +1,637 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { createServer, type IncomingMessage, type Server as HttpServer, type ServerResponse } from "node:http";
|
|
3
|
+
import type { AddressInfo } from "node:net";
|
|
4
|
+
import type { McpServerConfig } from "@cursor/sdk";
|
|
5
|
+
import type { Context } from "@earendil-works/pi-ai";
|
|
6
|
+
import type { ExtensionAPI, ToolInfo } from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import { Server as McpProtocolServer } from "@modelcontextprotocol/sdk/server/index.js";
|
|
8
|
+
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
9
|
+
import {
|
|
10
|
+
CallToolRequestSchema,
|
|
11
|
+
ListToolsRequestSchema,
|
|
12
|
+
type CallToolResult,
|
|
13
|
+
type Tool,
|
|
14
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
15
|
+
import { isExcludedFromCursorBridgeExposure } from "./cursor-tool-names.js";
|
|
16
|
+
|
|
17
|
+
const CURSOR_PI_TOOL_BRIDGE_ENV = "PI_CURSOR_PI_TOOL_BRIDGE";
|
|
18
|
+
const CURSOR_PI_TOOL_BRIDGE_BUILTINS_ENV = "PI_CURSOR_EXPOSE_BUILTIN_TOOLS";
|
|
19
|
+
const LOOPBACK_HOST = "127.0.0.1";
|
|
20
|
+
const MCP_SERVER_NAME = "pi_tools";
|
|
21
|
+
const MCP_ENDPOINT_ROOT = "/cursor-pi-tool-bridge";
|
|
22
|
+
const MCP_SERVER_VERSION = "0.1.0";
|
|
23
|
+
const HTTP_SERVER_CLOSE_GRACE_MS = 250;
|
|
24
|
+
const DISABLED_ENV_VALUES = new Set(["0", "false", "off", "none", "no", "disabled"]);
|
|
25
|
+
const ENABLED_ENV_VALUES = new Set(["1", "true", "on", "yes", "enabled"]);
|
|
26
|
+
const OVERLAPPING_CURSOR_NATIVE_PI_BUILTIN_TOOL_NAMES = new Set(["read", "bash", "write", "edit", "grep", "find", "ls"]);
|
|
27
|
+
|
|
28
|
+
export interface CursorPiMcpInputSchema {
|
|
29
|
+
type: "object";
|
|
30
|
+
properties?: Record<string, object>;
|
|
31
|
+
required?: string[];
|
|
32
|
+
[key: string]: unknown;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface CursorPiBridgeToolDefinition {
|
|
36
|
+
piToolName: string;
|
|
37
|
+
mcpToolName: string;
|
|
38
|
+
description: string;
|
|
39
|
+
inputSchema: CursorPiMcpInputSchema;
|
|
40
|
+
sourceInfo: ToolInfo["sourceInfo"];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface CursorPiToolBridgeSnapshot {
|
|
44
|
+
tools: CursorPiBridgeToolDefinition[];
|
|
45
|
+
mcpToolNameToPiToolName: ReadonlyMap<string, string>;
|
|
46
|
+
piToolNameToMcpToolName: ReadonlyMap<string, string>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface CursorPiToolBridgeSnapshotOptions {
|
|
50
|
+
exposeOverlappingBuiltins?: boolean;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface CursorPiBridgeToolRequest {
|
|
54
|
+
runId: string;
|
|
55
|
+
bridgeCallId: string;
|
|
56
|
+
cursorMcpCallId?: string;
|
|
57
|
+
piToolCallId: string;
|
|
58
|
+
piToolName: string;
|
|
59
|
+
mcpToolName: string;
|
|
60
|
+
args: Record<string, unknown>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface CursorPiToolBridgeRun {
|
|
64
|
+
id: string;
|
|
65
|
+
enabled: boolean;
|
|
66
|
+
mcpServers?: Record<string, McpServerConfig>;
|
|
67
|
+
snapshot: CursorPiToolBridgeSnapshot;
|
|
68
|
+
takeQueuedToolRequests(): CursorPiBridgeToolRequest[];
|
|
69
|
+
resolveToolResultsFromContext(context: Context): void;
|
|
70
|
+
hasPendingPiToolCallId(piToolCallId: string): boolean;
|
|
71
|
+
isBridgeMcpToolCall(toolCall: unknown): boolean;
|
|
72
|
+
cancel(reason: string): void;
|
|
73
|
+
dispose(): Promise<void>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface CursorPiToolBridgeRunOptions {
|
|
77
|
+
onToolRequest?: (request: CursorPiBridgeToolRequest) => void;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface CursorPiToolBridge {
|
|
81
|
+
isEnabled(): boolean;
|
|
82
|
+
createRun(options?: CursorPiToolBridgeRunOptions): Promise<CursorPiToolBridgeRun>;
|
|
83
|
+
disposeAll(reason?: string): Promise<void>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface PendingBridgeCall {
|
|
87
|
+
request: CursorPiBridgeToolRequest;
|
|
88
|
+
resolve: (result: CallToolResult) => void;
|
|
89
|
+
reject: (error: Error) => void;
|
|
90
|
+
signal?: AbortSignal;
|
|
91
|
+
onAbort?: () => void;
|
|
92
|
+
settled: boolean;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
96
|
+
return typeof value === "object" && value !== null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function normalizeMcpInputSchema(schema: unknown): CursorPiMcpInputSchema {
|
|
100
|
+
if (isRecord(schema) && schema.type === "object") return schema as CursorPiMcpInputSchema;
|
|
101
|
+
return { type: "object", properties: {} };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function normalizeMcpArgs(args: unknown): Record<string, unknown> {
|
|
105
|
+
return isRecord(args) ? { ...args } : {};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function waitForProtocolFlush(): Promise<void> {
|
|
109
|
+
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function sanitizeMcpToolNameStem(toolName: string): string {
|
|
113
|
+
const stem = toolName
|
|
114
|
+
.trim()
|
|
115
|
+
.replace(/[^A-Za-z0-9_-]+/g, "_")
|
|
116
|
+
.replace(/^_+|_+$/g, "");
|
|
117
|
+
return stem || "tool";
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function stableNameHash(value: string): string {
|
|
121
|
+
return createHash("sha256").update(value).digest("hex").slice(0, 8);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function createMcpToolName(piToolName: string, usedMcpToolNames: Set<string>): string {
|
|
125
|
+
const baseName = `pi__${sanitizeMcpToolNameStem(piToolName)}`;
|
|
126
|
+
if (!usedMcpToolNames.has(baseName)) {
|
|
127
|
+
usedMcpToolNames.add(baseName);
|
|
128
|
+
return baseName;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const hashedName = `${baseName}__${stableNameHash(piToolName)}`;
|
|
132
|
+
if (!usedMcpToolNames.has(hashedName)) {
|
|
133
|
+
usedMcpToolNames.add(hashedName);
|
|
134
|
+
return hashedName;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let counter = 2;
|
|
138
|
+
let candidate = `${hashedName}_${counter}`;
|
|
139
|
+
while (usedMcpToolNames.has(candidate)) {
|
|
140
|
+
counter += 1;
|
|
141
|
+
candidate = `${hashedName}_${counter}`;
|
|
142
|
+
}
|
|
143
|
+
usedMcpToolNames.add(candidate);
|
|
144
|
+
return candidate;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function createEmptySnapshot(): CursorPiToolBridgeSnapshot {
|
|
148
|
+
return {
|
|
149
|
+
tools: [],
|
|
150
|
+
mcpToolNameToPiToolName: new Map(),
|
|
151
|
+
piToolNameToMcpToolName: new Map(),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function resolveCursorPiToolBridgeEnabled(env: Record<string, string | undefined> = process.env): boolean {
|
|
156
|
+
const raw = env[CURSOR_PI_TOOL_BRIDGE_ENV]?.trim().toLowerCase();
|
|
157
|
+
if (!raw) return true;
|
|
158
|
+
if (DISABLED_ENV_VALUES.has(raw)) return false;
|
|
159
|
+
if (ENABLED_ENV_VALUES.has(raw)) return true;
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function resolveCursorPiToolBridgeBuiltinsEnabled(env: Record<string, string | undefined> = process.env): boolean {
|
|
164
|
+
const raw = env[CURSOR_PI_TOOL_BRIDGE_BUILTINS_ENV]?.trim().toLowerCase();
|
|
165
|
+
if (!raw) return false;
|
|
166
|
+
if (ENABLED_ENV_VALUES.has(raw)) return true;
|
|
167
|
+
if (DISABLED_ENV_VALUES.has(raw)) return false;
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function isOverlappingCursorNativePiToolName(toolName: string): boolean {
|
|
172
|
+
return OVERLAPPING_CURSOR_NATIVE_PI_BUILTIN_TOOL_NAMES.has(toolName);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function buildCursorPiToolBridgeSnapshot(
|
|
176
|
+
pi: Pick<ExtensionAPI, "getActiveTools" | "getAllTools">,
|
|
177
|
+
options: CursorPiToolBridgeSnapshotOptions = {},
|
|
178
|
+
): CursorPiToolBridgeSnapshot {
|
|
179
|
+
const activeToolNames = new Set(pi.getActiveTools());
|
|
180
|
+
const allTools = pi.getAllTools();
|
|
181
|
+
const usedMcpToolNames = new Set<string>();
|
|
182
|
+
const mcpToolNameToPiToolName = new Map<string, string>();
|
|
183
|
+
const piToolNameToMcpToolName = new Map<string, string>();
|
|
184
|
+
const tools: CursorPiBridgeToolDefinition[] = [];
|
|
185
|
+
|
|
186
|
+
const exposeOverlappingBuiltins = options.exposeOverlappingBuiltins === true;
|
|
187
|
+
|
|
188
|
+
for (const tool of allTools) {
|
|
189
|
+
if (!activeToolNames.has(tool.name)) continue;
|
|
190
|
+
if (isExcludedFromCursorBridgeExposure(tool.name)) continue;
|
|
191
|
+
if (!exposeOverlappingBuiltins && isOverlappingCursorNativePiToolName(tool.name)) continue;
|
|
192
|
+
|
|
193
|
+
const mcpToolName = createMcpToolName(tool.name, usedMcpToolNames);
|
|
194
|
+
const description = tool.description || `Run pi tool ${tool.name}`;
|
|
195
|
+
mcpToolNameToPiToolName.set(mcpToolName, tool.name);
|
|
196
|
+
piToolNameToMcpToolName.set(tool.name, mcpToolName);
|
|
197
|
+
tools.push({
|
|
198
|
+
piToolName: tool.name,
|
|
199
|
+
mcpToolName,
|
|
200
|
+
description,
|
|
201
|
+
inputSchema: normalizeMcpInputSchema(tool.parameters),
|
|
202
|
+
sourceInfo: tool.sourceInfo,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return { tools, mcpToolNameToPiToolName, piToolNameToMcpToolName };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function snapshotToolToMcpTool(tool: CursorPiBridgeToolDefinition): Tool {
|
|
210
|
+
return {
|
|
211
|
+
name: tool.mcpToolName,
|
|
212
|
+
description: tool.description,
|
|
213
|
+
inputSchema: tool.inputSchema,
|
|
214
|
+
_meta: { piToolName: tool.piToolName },
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function convertPiContentToMcpContent(content: unknown): CallToolResult["content"] {
|
|
219
|
+
if (!Array.isArray(content)) {
|
|
220
|
+
return [{ type: "text", text: typeof content === "string" ? content : JSON.stringify(content) }];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const mcpContent: CallToolResult["content"] = [];
|
|
224
|
+
for (const block of content) {
|
|
225
|
+
if (!isRecord(block)) continue;
|
|
226
|
+
if (block.type === "text" && typeof block.text === "string") {
|
|
227
|
+
mcpContent.push({ type: "text", text: block.text });
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
if (block.type === "image" && typeof block.data === "string" && typeof block.mimeType === "string") {
|
|
231
|
+
mcpContent.push({ type: "image", data: block.data, mimeType: block.mimeType });
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
mcpContent.push({ type: "text", text: JSON.stringify(block) });
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return mcpContent.length > 0 ? mcpContent : [{ type: "text", text: "" }];
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function asToolResultMessage(value: Context["messages"][number]): Extract<Context["messages"][number], { role: "toolResult" }> | undefined {
|
|
241
|
+
return value.role === "toolResult" ? value : undefined;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function getStringField(record: Record<string, unknown>, fields: string[]): string | undefined {
|
|
245
|
+
for (const field of fields) {
|
|
246
|
+
const value = record[field];
|
|
247
|
+
if (typeof value === "string" && value) return value;
|
|
248
|
+
}
|
|
249
|
+
return undefined;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function containsKnownMcpToolName(value: unknown, knownMcpToolNames: ReadonlySet<string>, depth = 0): boolean {
|
|
253
|
+
if (depth > 4) return false;
|
|
254
|
+
if (Array.isArray(value)) return value.some((entry) => containsKnownMcpToolName(entry, knownMcpToolNames, depth + 1));
|
|
255
|
+
if (!isRecord(value)) return false;
|
|
256
|
+
|
|
257
|
+
for (const field of ["tool", "toolName", "name", "mcpToolName", "serverToolName"]) {
|
|
258
|
+
const fieldValue = value[field];
|
|
259
|
+
if (typeof fieldValue === "string" && knownMcpToolNames.has(fieldValue)) return true;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
for (const nestedField of ["args", "arguments", "input"]) {
|
|
263
|
+
if (containsKnownMcpToolName(value[nestedField], knownMcpToolNames, depth + 1)) return true;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
class CursorPiToolBridgeRunImpl implements CursorPiToolBridgeRun {
|
|
270
|
+
readonly id: string;
|
|
271
|
+
readonly enabled: boolean;
|
|
272
|
+
readonly snapshot: CursorPiToolBridgeSnapshot;
|
|
273
|
+
mcpServers?: Record<string, McpServerConfig>;
|
|
274
|
+
|
|
275
|
+
private readonly registry: CursorPiToolBridgeRegistry;
|
|
276
|
+
private readonly endpointPath: string;
|
|
277
|
+
private readonly knownMcpToolNames: ReadonlySet<string>;
|
|
278
|
+
private readonly knownCursorMcpCallIds = new Set<string>();
|
|
279
|
+
private readonly queuedRequests: CursorPiBridgeToolRequest[] = [];
|
|
280
|
+
private readonly pendingByPiToolCallId = new Map<string, PendingBridgeCall>();
|
|
281
|
+
private readonly pendingByBridgeCallId = new Map<string, PendingBridgeCall>();
|
|
282
|
+
private readonly pendingByCursorMcpCallId = new Map<string, PendingBridgeCall>();
|
|
283
|
+
private readonly onToolRequest?: (request: CursorPiBridgeToolRequest) => void;
|
|
284
|
+
private mcpServer?: McpProtocolServer;
|
|
285
|
+
private mcpTransport?: StreamableHTTPServerTransport;
|
|
286
|
+
private toolCallCounter = 0;
|
|
287
|
+
private disposed = false;
|
|
288
|
+
|
|
289
|
+
constructor(
|
|
290
|
+
registry: CursorPiToolBridgeRegistry,
|
|
291
|
+
snapshot: CursorPiToolBridgeSnapshot,
|
|
292
|
+
enabled: boolean,
|
|
293
|
+
options: CursorPiToolBridgeRunOptions = {},
|
|
294
|
+
) {
|
|
295
|
+
this.registry = registry;
|
|
296
|
+
this.snapshot = snapshot;
|
|
297
|
+
this.enabled = enabled;
|
|
298
|
+
this.onToolRequest = options.onToolRequest;
|
|
299
|
+
this.id = `cursor-pi-bridge-${randomUUID()}`;
|
|
300
|
+
this.endpointPath = `${MCP_ENDPOINT_ROOT}/${this.id}/${randomUUID()}/mcp`;
|
|
301
|
+
this.knownMcpToolNames = new Set(snapshot.tools.map((tool) => tool.mcpToolName));
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
async start(): Promise<void> {
|
|
305
|
+
if (!this.enabled) return;
|
|
306
|
+
await this.createMcpServer();
|
|
307
|
+
const endpointUrl = await this.registry.registerRun(this.endpointPath, this);
|
|
308
|
+
this.mcpServers = { [MCP_SERVER_NAME]: { type: "http", url: endpointUrl } };
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
async handleHttpRequest(req: IncomingMessage, res: ServerResponse): Promise<void> {
|
|
312
|
+
if (this.disposed || !this.mcpTransport) {
|
|
313
|
+
res.writeHead(410, { "content-type": "application/json" }).end(JSON.stringify({ error: "Cursor pi tool bridge run is disposed" }));
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
await this.mcpTransport.handleRequest(req, res);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
takeQueuedToolRequests(): CursorPiBridgeToolRequest[] {
|
|
320
|
+
return this.queuedRequests.splice(0);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
resolveToolResultsFromContext(context: Context): void {
|
|
324
|
+
for (const message of context.messages) {
|
|
325
|
+
const toolResult = asToolResultMessage(message);
|
|
326
|
+
if (!toolResult) continue;
|
|
327
|
+
const pending = this.pendingByPiToolCallId.get(toolResult.toolCallId);
|
|
328
|
+
if (!pending || pending.settled) continue;
|
|
329
|
+
this.resolvePending(pending, {
|
|
330
|
+
content: convertPiContentToMcpContent(toolResult.content),
|
|
331
|
+
isError: toolResult.isError || undefined,
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
hasPendingPiToolCallId(piToolCallId: string): boolean {
|
|
337
|
+
return this.pendingByPiToolCallId.has(piToolCallId);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
isBridgeMcpToolCall(toolCall: unknown): boolean {
|
|
341
|
+
if (!isRecord(toolCall)) return false;
|
|
342
|
+
const toolName = getStringField(toolCall, ["name", "toolName", "mcpToolName"]);
|
|
343
|
+
if (toolName && this.knownMcpToolNames.has(toolName)) return true;
|
|
344
|
+
|
|
345
|
+
const cursorMcpCallId = getStringField(toolCall, ["call_id", "callId", "id", "toolCallId", "requestId"]);
|
|
346
|
+
if (cursorMcpCallId && this.knownCursorMcpCallIds.has(cursorMcpCallId)) return true;
|
|
347
|
+
|
|
348
|
+
if (containsKnownMcpToolName(toolCall, this.knownMcpToolNames)) return true;
|
|
349
|
+
|
|
350
|
+
return false;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
cancel(reason: string): void {
|
|
354
|
+
const error = new Error(reason);
|
|
355
|
+
this.queuedRequests.splice(0);
|
|
356
|
+
for (const pending of [...this.pendingByBridgeCallId.values()]) {
|
|
357
|
+
this.rejectPending(pending, error);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
async dispose(): Promise<void> {
|
|
362
|
+
if (this.disposed) return;
|
|
363
|
+
this.disposed = true;
|
|
364
|
+
this.cancel("Cursor pi tool bridge run disposed");
|
|
365
|
+
await waitForProtocolFlush();
|
|
366
|
+
await Promise.allSettled([
|
|
367
|
+
this.mcpTransport?.close(),
|
|
368
|
+
this.mcpServer?.close(),
|
|
369
|
+
]);
|
|
370
|
+
await this.registry.unregisterRun(this.endpointPath, this);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
private async createMcpServer(): Promise<void> {
|
|
374
|
+
const server = new McpProtocolServer(
|
|
375
|
+
{ name: "pi-cursor-sdk-tool-bridge", version: MCP_SERVER_VERSION },
|
|
376
|
+
{ capabilities: { tools: {} } },
|
|
377
|
+
);
|
|
378
|
+
const transport = new StreamableHTTPServerTransport({
|
|
379
|
+
sessionIdGenerator: randomUUID,
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
383
|
+
tools: this.snapshot.tools.map(snapshotToolToMcpTool),
|
|
384
|
+
}));
|
|
385
|
+
server.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
|
|
386
|
+
return this.enqueueToolRequest(request.params.name, request.params.arguments, String(extra.requestId), extra.signal);
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
this.mcpServer = server;
|
|
390
|
+
this.mcpTransport = transport;
|
|
391
|
+
await server.connect(transport);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
private enqueueToolRequest(mcpToolName: string, argsValue: unknown, cursorMcpCallId: string, signal?: AbortSignal): Promise<CallToolResult> {
|
|
395
|
+
const piToolName = this.snapshot.mcpToolNameToPiToolName.get(mcpToolName);
|
|
396
|
+
if (!piToolName) {
|
|
397
|
+
return Promise.resolve({
|
|
398
|
+
content: [{ type: "text", text: `Unknown pi bridge tool: ${mcpToolName}` }],
|
|
399
|
+
isError: true,
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
if (this.disposed) return Promise.reject(new Error("Cursor pi tool bridge run is disposed"));
|
|
403
|
+
|
|
404
|
+
this.toolCallCounter += 1;
|
|
405
|
+
const bridgeCallId = `${this.id}-bridge-${this.toolCallCounter}`;
|
|
406
|
+
const request: CursorPiBridgeToolRequest = {
|
|
407
|
+
runId: this.id,
|
|
408
|
+
bridgeCallId,
|
|
409
|
+
cursorMcpCallId,
|
|
410
|
+
piToolCallId: `${this.id}-tool-${this.toolCallCounter}`,
|
|
411
|
+
piToolName,
|
|
412
|
+
mcpToolName,
|
|
413
|
+
args: normalizeMcpArgs(argsValue),
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
return new Promise<CallToolResult>((resolve, reject) => {
|
|
417
|
+
const pending: PendingBridgeCall = {
|
|
418
|
+
request,
|
|
419
|
+
resolve,
|
|
420
|
+
reject,
|
|
421
|
+
signal,
|
|
422
|
+
settled: false,
|
|
423
|
+
};
|
|
424
|
+
pending.onAbort = () => {
|
|
425
|
+
this.rejectPending(pending, new Error("Cursor MCP bridge tool request was aborted"));
|
|
426
|
+
};
|
|
427
|
+
if (signal?.aborted) {
|
|
428
|
+
pending.onAbort();
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
signal?.addEventListener("abort", pending.onAbort, { once: true });
|
|
432
|
+
this.pendingByPiToolCallId.set(request.piToolCallId, pending);
|
|
433
|
+
this.pendingByBridgeCallId.set(request.bridgeCallId, pending);
|
|
434
|
+
this.pendingByCursorMcpCallId.set(cursorMcpCallId, pending);
|
|
435
|
+
this.knownCursorMcpCallIds.add(cursorMcpCallId);
|
|
436
|
+
this.queuedRequests.push(request);
|
|
437
|
+
this.onToolRequest?.(request);
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
private resolvePending(pending: PendingBridgeCall, result: CallToolResult): void {
|
|
442
|
+
if (pending.settled) return;
|
|
443
|
+
pending.settled = true;
|
|
444
|
+
this.removePending(pending);
|
|
445
|
+
pending.resolve(result);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
private rejectPending(pending: PendingBridgeCall, error: Error): void {
|
|
449
|
+
if (pending.settled) return;
|
|
450
|
+
pending.settled = true;
|
|
451
|
+
this.removePending(pending);
|
|
452
|
+
pending.reject(error);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
private removePending(pending: PendingBridgeCall): void {
|
|
456
|
+
pending.signal?.removeEventListener("abort", pending.onAbort ?? (() => undefined));
|
|
457
|
+
this.pendingByPiToolCallId.delete(pending.request.piToolCallId);
|
|
458
|
+
this.pendingByBridgeCallId.delete(pending.request.bridgeCallId);
|
|
459
|
+
if (pending.request.cursorMcpCallId) this.pendingByCursorMcpCallId.delete(pending.request.cursorMcpCallId);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
class CursorPiToolBridgeRegistry implements CursorPiToolBridge {
|
|
464
|
+
private readonly pi: Pick<ExtensionAPI, "getActiveTools" | "getAllTools">;
|
|
465
|
+
private readonly env: Record<string, string | undefined>;
|
|
466
|
+
private readonly runs = new Set<CursorPiToolBridgeRunImpl>();
|
|
467
|
+
private readonly routes = new Map<string, CursorPiToolBridgeRunImpl>();
|
|
468
|
+
private httpServer?: HttpServer;
|
|
469
|
+
private listenPromise?: Promise<void>;
|
|
470
|
+
|
|
471
|
+
constructor(
|
|
472
|
+
pi: Pick<ExtensionAPI, "getActiveTools" | "getAllTools">,
|
|
473
|
+
env: Record<string, string | undefined> = process.env,
|
|
474
|
+
) {
|
|
475
|
+
this.pi = pi;
|
|
476
|
+
this.env = env;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
isEnabled(): boolean {
|
|
480
|
+
return resolveCursorPiToolBridgeEnabled(this.env);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
async createRun(options: CursorPiToolBridgeRunOptions = {}): Promise<CursorPiToolBridgeRun> {
|
|
484
|
+
const bridgeEnabled = this.isEnabled();
|
|
485
|
+
const snapshot = bridgeEnabled
|
|
486
|
+
? buildCursorPiToolBridgeSnapshot(this.pi, {
|
|
487
|
+
exposeOverlappingBuiltins: resolveCursorPiToolBridgeBuiltinsEnabled(this.env),
|
|
488
|
+
})
|
|
489
|
+
: createEmptySnapshot();
|
|
490
|
+
const run = new CursorPiToolBridgeRunImpl(this, snapshot, bridgeEnabled && snapshot.tools.length > 0, options);
|
|
491
|
+
this.runs.add(run);
|
|
492
|
+
await run.start();
|
|
493
|
+
return run;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
async disposeAll(reason = "Cursor pi tool bridge disposed"): Promise<void> {
|
|
497
|
+
await Promise.all([...this.runs].map(async (run) => {
|
|
498
|
+
run.cancel(reason);
|
|
499
|
+
await run.dispose();
|
|
500
|
+
}));
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
async registerRun(pathname: string, run: CursorPiToolBridgeRunImpl): Promise<string> {
|
|
504
|
+
await this.ensureHttpServer();
|
|
505
|
+
this.routes.set(pathname, run);
|
|
506
|
+
const address = this.getHttpServerAddress();
|
|
507
|
+
if (!address) throw new Error("Cursor pi tool bridge HTTP server is not listening");
|
|
508
|
+
return `http://${LOOPBACK_HOST}:${address.port}${pathname}`;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
async unregisterRun(pathname: string, run: CursorPiToolBridgeRunImpl): Promise<void> {
|
|
512
|
+
if (this.routes.get(pathname) === run) this.routes.delete(pathname);
|
|
513
|
+
this.runs.delete(run);
|
|
514
|
+
if (this.routes.size === 0) await this.closeHttpServer();
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
getHttpServerAddress(): AddressInfo | undefined {
|
|
518
|
+
const address = this.httpServer?.address();
|
|
519
|
+
return isRecord(address) && typeof address.port === "number" ? address as AddressInfo : undefined;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
getEndpointCount(): number {
|
|
523
|
+
return this.routes.size;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
private async ensureHttpServer(): Promise<void> {
|
|
527
|
+
if (this.httpServer) {
|
|
528
|
+
await this.listenPromise;
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
const server = createServer((req, res) => {
|
|
533
|
+
void this.handleHttpRequest(req, res);
|
|
534
|
+
});
|
|
535
|
+
this.httpServer = server;
|
|
536
|
+
this.listenPromise = new Promise<void>((resolve, reject) => {
|
|
537
|
+
const onError = (error: Error) => {
|
|
538
|
+
server.off("listening", onListening);
|
|
539
|
+
reject(error);
|
|
540
|
+
};
|
|
541
|
+
const onListening = () => {
|
|
542
|
+
server.off("error", onError);
|
|
543
|
+
resolve();
|
|
544
|
+
};
|
|
545
|
+
server.once("error", onError);
|
|
546
|
+
server.once("listening", onListening);
|
|
547
|
+
server.listen(0, LOOPBACK_HOST);
|
|
548
|
+
});
|
|
549
|
+
await this.listenPromise;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
private async closeHttpServer(): Promise<void> {
|
|
553
|
+
const server = this.httpServer;
|
|
554
|
+
if (!server) return;
|
|
555
|
+
this.httpServer = undefined;
|
|
556
|
+
this.listenPromise = undefined;
|
|
557
|
+
await new Promise<void>((resolve, reject) => {
|
|
558
|
+
let settled = false;
|
|
559
|
+
let closeTimer: ReturnType<typeof setTimeout> | undefined;
|
|
560
|
+
const settle = (error?: Error): void => {
|
|
561
|
+
if (settled) return;
|
|
562
|
+
settled = true;
|
|
563
|
+
if (closeTimer) clearTimeout(closeTimer);
|
|
564
|
+
if (error) reject(error);
|
|
565
|
+
else resolve();
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
closeTimer = setTimeout(() => settle(), HTTP_SERVER_CLOSE_GRACE_MS);
|
|
569
|
+
closeTimer.unref?.();
|
|
570
|
+
|
|
571
|
+
server.close((error) => {
|
|
572
|
+
settle(error ?? undefined);
|
|
573
|
+
});
|
|
574
|
+
server.closeIdleConnections();
|
|
575
|
+
server.closeAllConnections();
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
private async handleHttpRequest(req: IncomingMessage, res: ServerResponse): Promise<void> {
|
|
580
|
+
if (req.socket.localAddress !== LOOPBACK_HOST) {
|
|
581
|
+
res.writeHead(403, { "content-type": "application/json" }).end(JSON.stringify({ error: "Cursor pi tool bridge only accepts loopback requests" }));
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
const url = new URL(req.url ?? "/", `http://${LOOPBACK_HOST}`);
|
|
586
|
+
const run = this.routes.get(url.pathname);
|
|
587
|
+
if (!run) {
|
|
588
|
+
res.writeHead(404, { "content-type": "application/json" }).end(JSON.stringify({ error: "Cursor pi tool bridge endpoint not found" }));
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
try {
|
|
593
|
+
await run.handleHttpRequest(req, res);
|
|
594
|
+
} catch (error) {
|
|
595
|
+
if (!res.headersSent) {
|
|
596
|
+
res.writeHead(500, { "content-type": "application/json" }).end(JSON.stringify({ error: error instanceof Error ? error.message : String(error) }));
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
let registeredCursorPiToolBridge: CursorPiToolBridgeRegistry | undefined;
|
|
603
|
+
|
|
604
|
+
export function registerCursorPiToolBridge(pi: ExtensionAPI): CursorPiToolBridge {
|
|
605
|
+
void registeredCursorPiToolBridge?.disposeAll("Cursor pi tool bridge extension reloaded");
|
|
606
|
+
const bridge = new CursorPiToolBridgeRegistry(pi);
|
|
607
|
+
registeredCursorPiToolBridge = bridge;
|
|
608
|
+
pi.on("session_shutdown", async (event) => {
|
|
609
|
+
await bridge.disposeAll(`Cursor pi tool bridge session shutdown: ${event.reason}`);
|
|
610
|
+
});
|
|
611
|
+
return bridge;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
export function getRegisteredCursorPiToolBridge(): CursorPiToolBridge | undefined {
|
|
615
|
+
return registeredCursorPiToolBridge;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
export const __testUtils = {
|
|
619
|
+
CURSOR_PI_TOOL_BRIDGE_ENV,
|
|
620
|
+
CURSOR_PI_TOOL_BRIDGE_BUILTINS_ENV,
|
|
621
|
+
LOOPBACK_HOST,
|
|
622
|
+
MCP_SERVER_NAME,
|
|
623
|
+
createRegistry(
|
|
624
|
+
pi: Pick<ExtensionAPI, "getActiveTools" | "getAllTools">,
|
|
625
|
+
env: Record<string, string | undefined> = process.env,
|
|
626
|
+
) {
|
|
627
|
+
return new CursorPiToolBridgeRegistry(pi, env);
|
|
628
|
+
},
|
|
629
|
+
getRegisteredBridgeForTests() {
|
|
630
|
+
return registeredCursorPiToolBridge;
|
|
631
|
+
},
|
|
632
|
+
resetRegisteredBridgeForTests() {
|
|
633
|
+
const bridge = registeredCursorPiToolBridge;
|
|
634
|
+
registeredCursorPiToolBridge = undefined;
|
|
635
|
+
return bridge?.disposeAll("Cursor pi tool bridge test reset") ?? Promise.resolve();
|
|
636
|
+
},
|
|
637
|
+
};
|