pi-cursor-sdk 0.1.16 → 0.1.18
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 +53 -1
- package/README.md +2 -2
- package/docs/cursor-live-smoke-checklist.md +54 -41
- package/docs/cursor-model-ux-spec.md +4 -3
- package/docs/cursor-testing-lessons.md +199 -0
- package/package.json +14 -5
- package/scripts/isolated-cursor-smoke.sh +226 -0
- package/scripts/steering-rpc-smoke.mjs +238 -0
- package/scripts/tmux-live-smoke.sh +418 -0
- package/scripts/validate-smoke-jsonl.mjs +207 -0
- package/src/cursor-context-tools.ts +6 -0
- package/src/cursor-display-text.ts +10 -0
- package/src/cursor-edit-diff.ts +11 -0
- package/src/cursor-env-boolean.ts +22 -0
- package/src/cursor-live-run-coordinator.ts +483 -0
- package/src/cursor-native-replay-routing.ts +48 -0
- package/src/cursor-native-replay-trace.ts +29 -0
- package/src/cursor-native-tool-display-registration.ts +103 -0
- package/src/cursor-native-tool-display-replay.ts +465 -0
- package/src/cursor-native-tool-display-state.ts +78 -0
- package/src/cursor-native-tool-display-tools.ts +102 -0
- package/src/cursor-native-tool-display.ts +10 -648
- package/src/cursor-partial-content-emitter.ts +121 -0
- package/src/cursor-pi-tool-bridge-abort.ts +133 -0
- package/src/cursor-pi-tool-bridge-diagnostics.ts +179 -0
- package/src/cursor-pi-tool-bridge-mcp.ts +118 -0
- package/src/cursor-pi-tool-bridge-run.ts +384 -0
- package/src/cursor-pi-tool-bridge-server.ts +182 -0
- package/src/cursor-pi-tool-bridge-snapshot.ts +88 -0
- package/src/cursor-pi-tool-bridge-types.ts +80 -0
- package/src/cursor-pi-tool-bridge.ts +42 -1104
- package/src/cursor-provider-live-run-drain.ts +405 -0
- package/src/cursor-provider-turn-coordinator.ts +460 -0
- package/src/cursor-provider.ts +77 -1103
- package/src/cursor-question-tool.ts +9 -1
- package/src/cursor-record-utils.ts +26 -0
- package/src/cursor-sdk-output-filter.ts +100 -0
- package/src/cursor-sensitive-text.ts +37 -0
- package/src/cursor-tool-transcript.ts +28 -1229
- package/src/cursor-transcript-tool-formatters.ts +641 -0
- package/src/cursor-transcript-tool-specs.ts +441 -0
- package/src/cursor-transcript-utils.ts +276 -0
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
3
|
+
import type { McpServerConfig } from "@cursor/sdk";
|
|
4
|
+
import type { Context, ToolResultMessage } from "@earendil-works/pi-ai";
|
|
5
|
+
import { Server as McpProtocolServer } from "@modelcontextprotocol/sdk/server/index.js";
|
|
6
|
+
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
7
|
+
import {
|
|
8
|
+
CallToolRequestSchema,
|
|
9
|
+
ListToolsRequestSchema,
|
|
10
|
+
type CallToolResult,
|
|
11
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
12
|
+
import {
|
|
13
|
+
type CursorPiToolBridgeDiagnosticEvent,
|
|
14
|
+
type CursorPiToolBridgeLifecycleDiagnosticFields,
|
|
15
|
+
type CursorPiToolBridgeRejectionKind,
|
|
16
|
+
type CursorPiToolBridgeRequestDiagnosticFields,
|
|
17
|
+
writeCursorPiToolBridgeDiagnostic,
|
|
18
|
+
} from "./cursor-pi-tool-bridge-diagnostics.js";
|
|
19
|
+
import type {
|
|
20
|
+
CursorPiBridgeToolRequest,
|
|
21
|
+
CursorPiToolBridgeRun,
|
|
22
|
+
CursorPiToolBridgeRunOptions,
|
|
23
|
+
CursorPiToolBridgeSnapshot,
|
|
24
|
+
} from "./cursor-pi-tool-bridge-types.js";
|
|
25
|
+
import {
|
|
26
|
+
asToolResultMessage,
|
|
27
|
+
containsKnownMcpToolName,
|
|
28
|
+
convertPiContentToMcpContent,
|
|
29
|
+
getStringField,
|
|
30
|
+
isRecord,
|
|
31
|
+
normalizeMcpArgs,
|
|
32
|
+
snapshotToolToMcpTool,
|
|
33
|
+
waitForProtocolFlush,
|
|
34
|
+
} from "./cursor-pi-tool-bridge-mcp.js";
|
|
35
|
+
|
|
36
|
+
export interface CursorPiToolBridgeRunHost {
|
|
37
|
+
registerRun(pathname: string, run: CursorPiToolBridgeRunImpl): Promise<string>;
|
|
38
|
+
unregisterRun(pathname: string, run: CursorPiToolBridgeRunImpl): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const MCP_SERVER_NAME = "pi_tools";
|
|
42
|
+
export const MCP_ENDPOINT_ROOT = "/cursor-pi-tool-bridge";
|
|
43
|
+
const MCP_SERVER_VERSION = "0.1.0";
|
|
44
|
+
|
|
45
|
+
interface PendingBridgeCall {
|
|
46
|
+
request: CursorPiBridgeToolRequest;
|
|
47
|
+
resolve: (result: CallToolResult) => void;
|
|
48
|
+
reject: (error: Error) => void;
|
|
49
|
+
signal?: AbortSignal;
|
|
50
|
+
onAbort?: () => void;
|
|
51
|
+
settled: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export class CursorPiToolBridgeRunImpl implements CursorPiToolBridgeRun {
|
|
55
|
+
readonly id: string;
|
|
56
|
+
readonly enabled: boolean;
|
|
57
|
+
readonly snapshot: CursorPiToolBridgeSnapshot;
|
|
58
|
+
mcpServers?: Record<string, McpServerConfig>;
|
|
59
|
+
|
|
60
|
+
private readonly registry: CursorPiToolBridgeRunHost;
|
|
61
|
+
private readonly env: Record<string, string | undefined>;
|
|
62
|
+
private readonly endpointPath: string;
|
|
63
|
+
private readonly knownMcpToolNames: ReadonlySet<string>;
|
|
64
|
+
private readonly knownCursorMcpCallIds = new Set<string>();
|
|
65
|
+
private readonly queuedRequests: CursorPiBridgeToolRequest[] = [];
|
|
66
|
+
private readonly pendingByPiToolCallId = new Map<string, PendingBridgeCall>();
|
|
67
|
+
private readonly pendingByBridgeCallId = new Map<string, PendingBridgeCall>();
|
|
68
|
+
private readonly pendingByCursorMcpCallId = new Map<string, PendingBridgeCall>();
|
|
69
|
+
private onToolRequest?: (request: CursorPiBridgeToolRequest) => void;
|
|
70
|
+
private liveRunHandlerDetached = false;
|
|
71
|
+
private mcpServer?: McpProtocolServer;
|
|
72
|
+
private mcpTransport?: StreamableHTTPServerTransport;
|
|
73
|
+
private toolCallCounter = 0;
|
|
74
|
+
private disposed = false;
|
|
75
|
+
|
|
76
|
+
constructor(
|
|
77
|
+
registry: CursorPiToolBridgeRunHost,
|
|
78
|
+
env: Record<string, string | undefined>,
|
|
79
|
+
snapshot: CursorPiToolBridgeSnapshot,
|
|
80
|
+
enabled: boolean,
|
|
81
|
+
options: CursorPiToolBridgeRunOptions = {},
|
|
82
|
+
) {
|
|
83
|
+
this.registry = registry;
|
|
84
|
+
this.env = env;
|
|
85
|
+
this.snapshot = snapshot;
|
|
86
|
+
this.enabled = enabled;
|
|
87
|
+
this.onToolRequest = options.onToolRequest;
|
|
88
|
+
this.id = `cursor-pi-bridge-run-${randomUUID()}`;
|
|
89
|
+
this.endpointPath = `${MCP_ENDPOINT_ROOT}/${randomUUID()}/mcp`;
|
|
90
|
+
this.knownMcpToolNames = new Set(snapshot.tools.map((tool) => tool.mcpToolName));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async start(): Promise<void> {
|
|
94
|
+
if (!this.enabled) return;
|
|
95
|
+
await this.createMcpServer();
|
|
96
|
+
const endpointUrl = await this.registry.registerRun(this.endpointPath, this);
|
|
97
|
+
this.mcpServers = { [MCP_SERVER_NAME]: { type: "http", url: endpointUrl } };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
emitStartDiagnostics(bridgeEnabled: boolean): void {
|
|
101
|
+
const base = this.lifecycleDiagnosticFields();
|
|
102
|
+
this.emitDiagnostic({ event: "run_created", ...base });
|
|
103
|
+
if (!this.enabled) {
|
|
104
|
+
this.emitDiagnostic({
|
|
105
|
+
event: "run_skipped",
|
|
106
|
+
...base,
|
|
107
|
+
reason: bridgeEnabled ? "no_exposed_tools" : "disabled",
|
|
108
|
+
});
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
this.emitDiagnostic({
|
|
112
|
+
event: "tools_exposed",
|
|
113
|
+
...base,
|
|
114
|
+
pairs: this.snapshot.tools.map((tool) => ({
|
|
115
|
+
piToolName: tool.piToolName,
|
|
116
|
+
mcpToolName: tool.mcpToolName,
|
|
117
|
+
})),
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async handleHttpRequest(req: IncomingMessage, res: ServerResponse): Promise<void> {
|
|
122
|
+
if (this.disposed || !this.mcpTransport) {
|
|
123
|
+
res.writeHead(410, { "content-type": "application/json" }).end(JSON.stringify({ error: "Cursor pi tool bridge run is disposed" }));
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
await this.mcpTransport.handleRequest(req, res);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
takeQueuedToolRequests(): CursorPiBridgeToolRequest[] {
|
|
130
|
+
return this.queuedRequests.splice(0);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
setOnToolRequest(handler?: (request: CursorPiBridgeToolRequest) => void): void {
|
|
134
|
+
if (!handler) {
|
|
135
|
+
this.liveRunHandlerDetached = true;
|
|
136
|
+
this.rejectQueuedToolRequestsWithoutHandler("Cursor pi tool bridge has no active live run");
|
|
137
|
+
} else {
|
|
138
|
+
this.liveRunHandlerDetached = false;
|
|
139
|
+
}
|
|
140
|
+
this.onToolRequest = handler;
|
|
141
|
+
if (handler) {
|
|
142
|
+
for (const request of this.queuedRequests.splice(0)) {
|
|
143
|
+
const pending = this.pendingByPiToolCallId.get(request.piToolCallId);
|
|
144
|
+
if (pending) this.dispatchPendingToolRequest(pending, handler);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
resolveToolResults(toolResults: readonly ToolResultMessage[]): void {
|
|
150
|
+
for (const toolResult of toolResults) {
|
|
151
|
+
const pending = this.pendingByPiToolCallId.get(toolResult.toolCallId);
|
|
152
|
+
if (!pending || pending.settled) continue;
|
|
153
|
+
this.resolvePending(pending, {
|
|
154
|
+
content: convertPiContentToMcpContent(toolResult.content),
|
|
155
|
+
isError: toolResult.isError || undefined,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
resolveToolResultsFromContext(context: Context): void {
|
|
161
|
+
this.resolveToolResults(context.messages.map(asToolResultMessage).filter((message): message is ToolResultMessage => message !== undefined));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
hasPendingPiToolCallId(piToolCallId: string): boolean {
|
|
165
|
+
return this.pendingByPiToolCallId.has(piToolCallId);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
cancelPendingPiToolCallId(piToolCallId: string, reason: string): boolean {
|
|
169
|
+
const pending = this.pendingByPiToolCallId.get(piToolCallId);
|
|
170
|
+
if (!pending) return false;
|
|
171
|
+
this.rejectPending(pending, new Error(reason), "cancelled");
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
isBridgeMcpToolCall(toolCall: unknown): boolean {
|
|
176
|
+
if (!isRecord(toolCall)) return false;
|
|
177
|
+
const toolName = getStringField(toolCall, ["name", "toolName", "mcpToolName"]);
|
|
178
|
+
if (toolName && this.knownMcpToolNames.has(toolName)) return true;
|
|
179
|
+
|
|
180
|
+
const isMcpEnvelope = toolName === "mcp" || toolName === MCP_SERVER_NAME;
|
|
181
|
+
const cursorMcpCallId = getStringField(toolCall, ["call_id", "callId", "id", "toolCallId", "requestId"]);
|
|
182
|
+
if (cursorMcpCallId && this.knownCursorMcpCallIds.has(cursorMcpCallId) && isMcpEnvelope) return true;
|
|
183
|
+
|
|
184
|
+
if (containsKnownMcpToolName(toolCall, this.knownMcpToolNames)) return true;
|
|
185
|
+
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
cancel(reason: string): void {
|
|
190
|
+
const error = new Error(reason);
|
|
191
|
+
const pendingCount = this.pendingCount();
|
|
192
|
+
const queuedCount = this.queuedRequests.length;
|
|
193
|
+
if (pendingCount > 0 || queuedCount > 0) {
|
|
194
|
+
this.emitDiagnostic({
|
|
195
|
+
event: "run_cancelled",
|
|
196
|
+
...this.lifecycleDiagnosticFields(pendingCount),
|
|
197
|
+
queuedCount,
|
|
198
|
+
cancelledRequestCount: pendingCount,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
this.queuedRequests.splice(0);
|
|
202
|
+
for (const pending of [...this.pendingByBridgeCallId.values()]) {
|
|
203
|
+
this.rejectPending(pending, error, "cancelled");
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async dispose(): Promise<void> {
|
|
208
|
+
if (this.disposed) return;
|
|
209
|
+
this.disposed = true;
|
|
210
|
+
this.cancel("Cursor pi tool bridge run disposed");
|
|
211
|
+
await waitForProtocolFlush();
|
|
212
|
+
await Promise.allSettled([
|
|
213
|
+
this.mcpTransport?.close(),
|
|
214
|
+
this.mcpServer?.close(),
|
|
215
|
+
]);
|
|
216
|
+
await this.registry.unregisterRun(this.endpointPath, this);
|
|
217
|
+
this.emitDiagnostic({
|
|
218
|
+
event: "run_disposed",
|
|
219
|
+
...this.lifecycleDiagnosticFields(),
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private async createMcpServer(): Promise<void> {
|
|
224
|
+
const server = new McpProtocolServer(
|
|
225
|
+
{ name: "pi-cursor-sdk-tool-bridge", version: MCP_SERVER_VERSION },
|
|
226
|
+
{ capabilities: { tools: {} } },
|
|
227
|
+
);
|
|
228
|
+
const transport = new StreamableHTTPServerTransport({
|
|
229
|
+
sessionIdGenerator: randomUUID,
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
233
|
+
tools: this.snapshot.tools.map(snapshotToolToMcpTool),
|
|
234
|
+
}));
|
|
235
|
+
server.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
|
|
236
|
+
return this.enqueueToolRequest(request.params.name, request.params.arguments, String(extra.requestId), extra.signal);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
this.mcpServer = server;
|
|
240
|
+
this.mcpTransport = transport;
|
|
241
|
+
await server.connect(transport);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
private enqueueToolRequest(mcpToolName: string, argsValue: unknown, cursorMcpCallId: string, signal?: AbortSignal): Promise<CallToolResult> {
|
|
245
|
+
const piToolName = this.snapshot.mcpToolNameToPiToolName.get(mcpToolName);
|
|
246
|
+
if (!piToolName) {
|
|
247
|
+
return Promise.resolve({
|
|
248
|
+
content: [{ type: "text", text: `Unknown pi bridge tool: ${mcpToolName}` }],
|
|
249
|
+
isError: true,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
if (this.disposed) return Promise.reject(new Error("Cursor pi tool bridge run is disposed"));
|
|
253
|
+
|
|
254
|
+
this.toolCallCounter += 1;
|
|
255
|
+
const bridgeCallId = `${this.id}-bridge-${this.toolCallCounter}`;
|
|
256
|
+
const request: CursorPiBridgeToolRequest = {
|
|
257
|
+
runId: this.id,
|
|
258
|
+
bridgeCallId,
|
|
259
|
+
cursorMcpCallId,
|
|
260
|
+
piToolCallId: `${this.id}-tool-${this.toolCallCounter}`,
|
|
261
|
+
piToolName,
|
|
262
|
+
mcpToolName,
|
|
263
|
+
args: normalizeMcpArgs(argsValue),
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
return new Promise<CallToolResult>((resolve, reject) => {
|
|
267
|
+
const pending: PendingBridgeCall = {
|
|
268
|
+
request,
|
|
269
|
+
resolve,
|
|
270
|
+
reject,
|
|
271
|
+
signal,
|
|
272
|
+
settled: false,
|
|
273
|
+
};
|
|
274
|
+
pending.onAbort = () => {
|
|
275
|
+
this.rejectPending(pending, new Error("Cursor MCP bridge tool request was aborted"), "cancelled");
|
|
276
|
+
};
|
|
277
|
+
if (signal?.aborted) {
|
|
278
|
+
pending.onAbort();
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
signal?.addEventListener("abort", pending.onAbort, { once: true });
|
|
282
|
+
this.pendingByPiToolCallId.set(request.piToolCallId, pending);
|
|
283
|
+
this.pendingByBridgeCallId.set(request.bridgeCallId, pending);
|
|
284
|
+
this.pendingByCursorMcpCallId.set(cursorMcpCallId, pending);
|
|
285
|
+
this.knownCursorMcpCallIds.add(cursorMcpCallId);
|
|
286
|
+
if (!this.onToolRequest) {
|
|
287
|
+
if (this.liveRunHandlerDetached) {
|
|
288
|
+
this.rejectPending(pending, new Error("Cursor pi tool bridge has no active live run"), "cancelled");
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
this.queuedRequests.push(request);
|
|
292
|
+
this.emitRequestQueuedDiagnostic(request);
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
this.emitRequestQueuedDiagnostic(request);
|
|
296
|
+
this.dispatchPendingToolRequest(pending, this.onToolRequest);
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
private dispatchPendingToolRequest(
|
|
301
|
+
pending: PendingBridgeCall,
|
|
302
|
+
handler: (request: CursorPiBridgeToolRequest) => void,
|
|
303
|
+
): void {
|
|
304
|
+
try {
|
|
305
|
+
handler(pending.request);
|
|
306
|
+
} catch (error) {
|
|
307
|
+
this.rejectPending(pending, error instanceof Error ? error : new Error(String(error)), "error");
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
private rejectQueuedToolRequestsWithoutHandler(reason: string): void {
|
|
312
|
+
while (this.queuedRequests.length > 0) {
|
|
313
|
+
const request = this.queuedRequests.shift()!;
|
|
314
|
+
const pending = this.pendingByPiToolCallId.get(request.piToolCallId);
|
|
315
|
+
if (pending) this.rejectPending(pending, new Error(reason), "cancelled");
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
private resolvePending(pending: PendingBridgeCall, result: CallToolResult): void {
|
|
320
|
+
if (pending.settled) return;
|
|
321
|
+
pending.settled = true;
|
|
322
|
+
this.removePending(pending);
|
|
323
|
+
this.emitRequestResolvedDiagnostic(pending.request, result.isError === true);
|
|
324
|
+
pending.resolve(result);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
private rejectPending(pending: PendingBridgeCall, error: Error, kind: "cancelled" | "error" = "error"): void {
|
|
328
|
+
if (pending.settled) return;
|
|
329
|
+
pending.settled = true;
|
|
330
|
+
this.removePending(pending);
|
|
331
|
+
this.emitRequestRejectedDiagnostic(pending.request, kind);
|
|
332
|
+
pending.reject(error);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
private lifecycleDiagnosticFields(pendingCount = this.pendingCount()): CursorPiToolBridgeLifecycleDiagnosticFields {
|
|
336
|
+
return {
|
|
337
|
+
runId: this.id,
|
|
338
|
+
enabled: this.enabled,
|
|
339
|
+
exposedToolCount: this.snapshot.tools.length,
|
|
340
|
+
pendingCount,
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
private requestDiagnosticFields(request: CursorPiBridgeToolRequest): CursorPiToolBridgeRequestDiagnosticFields {
|
|
345
|
+
return {
|
|
346
|
+
runId: this.id,
|
|
347
|
+
bridgeCallId: request.bridgeCallId,
|
|
348
|
+
cursorMcpCallId: request.cursorMcpCallId,
|
|
349
|
+
piToolCallId: request.piToolCallId,
|
|
350
|
+
mcpToolName: request.mcpToolName,
|
|
351
|
+
piToolName: request.piToolName,
|
|
352
|
+
pendingCount: this.pendingCount(),
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
private emitRequestQueuedDiagnostic(request: CursorPiBridgeToolRequest): void {
|
|
357
|
+
this.emitDiagnostic({ event: "request_queued", ...this.requestDiagnosticFields(request) });
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
private emitRequestResolvedDiagnostic(request: CursorPiBridgeToolRequest, isError: boolean): void {
|
|
361
|
+
this.emitDiagnostic({ event: "request_resolved", ...this.requestDiagnosticFields(request), isError });
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
private emitRequestRejectedDiagnostic(request: CursorPiBridgeToolRequest, rejectionKind: CursorPiToolBridgeRejectionKind): void {
|
|
365
|
+
this.emitDiagnostic({ event: "request_rejected", ...this.requestDiagnosticFields(request), rejectionKind });
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
private emitDiagnostic(event: CursorPiToolBridgeDiagnosticEvent): void {
|
|
369
|
+
writeCursorPiToolBridgeDiagnostic(this.env, event);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
private pendingCount(): number {
|
|
373
|
+
return this.pendingByBridgeCallId.size;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
private removePending(pending: PendingBridgeCall): void {
|
|
377
|
+
pending.signal?.removeEventListener("abort", pending.onAbort ?? (() => undefined));
|
|
378
|
+
this.pendingByPiToolCallId.delete(pending.request.piToolCallId);
|
|
379
|
+
this.pendingByBridgeCallId.delete(pending.request.bridgeCallId);
|
|
380
|
+
if (pending.request.cursorMcpCallId) this.pendingByCursorMcpCallId.delete(pending.request.cursorMcpCallId);
|
|
381
|
+
const queuedIndex = this.queuedRequests.findIndex((request) => request.bridgeCallId === pending.request.bridgeCallId);
|
|
382
|
+
if (queuedIndex >= 0) this.queuedRequests.splice(queuedIndex, 1);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { createServer, type IncomingMessage, type Server as HttpServer, type ServerResponse } from "node:http";
|
|
2
|
+
import type { AddressInfo } from "node:net";
|
|
3
|
+
import type {
|
|
4
|
+
CursorPiToolBridge,
|
|
5
|
+
CursorPiToolBridgeRun,
|
|
6
|
+
CursorPiToolBridgeRunOptions,
|
|
7
|
+
CursorPiToolBridgeSnapshotApi,
|
|
8
|
+
} from "./cursor-pi-tool-bridge-types.js";
|
|
9
|
+
import { isRecord } from "./cursor-pi-tool-bridge-mcp.js";
|
|
10
|
+
import { CursorPiToolBridgeRunImpl } from "./cursor-pi-tool-bridge-run.js";
|
|
11
|
+
import {
|
|
12
|
+
buildCursorPiToolBridgeSnapshot,
|
|
13
|
+
buildCursorPiToolBridgeSurfaceSignature,
|
|
14
|
+
createEmptySnapshot,
|
|
15
|
+
resolveCursorPiToolBridgeBuiltinsEnabled,
|
|
16
|
+
resolveCursorPiToolBridgeEnabled,
|
|
17
|
+
} from "./cursor-pi-tool-bridge-snapshot.js";
|
|
18
|
+
|
|
19
|
+
export const LOOPBACK_HOST = "127.0.0.1";
|
|
20
|
+
const HTTP_SERVER_CLOSE_GRACE_MS = 250;
|
|
21
|
+
|
|
22
|
+
export class CursorPiToolBridgeRegistry implements CursorPiToolBridge {
|
|
23
|
+
private readonly pi: CursorPiToolBridgeSnapshotApi;
|
|
24
|
+
private readonly env: Record<string, string | undefined>;
|
|
25
|
+
private readonly runs = new Set<CursorPiToolBridgeRunImpl>();
|
|
26
|
+
private readonly routes = new Map<string, CursorPiToolBridgeRunImpl>();
|
|
27
|
+
private httpServer?: HttpServer;
|
|
28
|
+
private listenPromise?: Promise<void>;
|
|
29
|
+
|
|
30
|
+
constructor(
|
|
31
|
+
pi: CursorPiToolBridgeSnapshotApi,
|
|
32
|
+
env: Record<string, string | undefined> = process.env,
|
|
33
|
+
) {
|
|
34
|
+
this.pi = pi;
|
|
35
|
+
this.env = env;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
isEnabled(): boolean {
|
|
39
|
+
return resolveCursorPiToolBridgeEnabled(this.env);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
getToolSurfaceSignature(): string {
|
|
43
|
+
if (!this.isEnabled()) return "bridge:off";
|
|
44
|
+
const snapshot = buildCursorPiToolBridgeSnapshot(this.pi, {
|
|
45
|
+
exposeOverlappingBuiltins: resolveCursorPiToolBridgeBuiltinsEnabled(this.env),
|
|
46
|
+
});
|
|
47
|
+
return buildCursorPiToolBridgeSurfaceSignature(snapshot);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async createRun(options: CursorPiToolBridgeRunOptions = {}): Promise<CursorPiToolBridgeRun> {
|
|
51
|
+
const bridgeEnabled = this.isEnabled();
|
|
52
|
+
const snapshot = bridgeEnabled
|
|
53
|
+
? buildCursorPiToolBridgeSnapshot(this.pi, {
|
|
54
|
+
exposeOverlappingBuiltins: resolveCursorPiToolBridgeBuiltinsEnabled(this.env),
|
|
55
|
+
})
|
|
56
|
+
: createEmptySnapshot();
|
|
57
|
+
const run = new CursorPiToolBridgeRunImpl(this, this.env, snapshot, bridgeEnabled && snapshot.tools.length > 0, options);
|
|
58
|
+
this.runs.add(run);
|
|
59
|
+
await run.start();
|
|
60
|
+
run.emitStartDiagnostics(bridgeEnabled);
|
|
61
|
+
return run;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async disposeAll(reason = "Cursor pi tool bridge disposed"): Promise<void> {
|
|
65
|
+
await Promise.all([...this.runs].map(async (run) => {
|
|
66
|
+
run.cancel(reason);
|
|
67
|
+
await run.dispose();
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async registerRun(pathname: string, run: CursorPiToolBridgeRunImpl): Promise<string> {
|
|
72
|
+
await this.ensureHttpServer();
|
|
73
|
+
this.routes.set(pathname, run);
|
|
74
|
+
const address = this.getHttpServerAddress();
|
|
75
|
+
if (!address) throw new Error("Cursor pi tool bridge HTTP server is not listening");
|
|
76
|
+
return `http://${LOOPBACK_HOST}:${address.port}${pathname}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async unregisterRun(pathname: string, run: CursorPiToolBridgeRunImpl): Promise<void> {
|
|
80
|
+
if (this.routes.get(pathname) === run) this.routes.delete(pathname);
|
|
81
|
+
this.runs.delete(run);
|
|
82
|
+
if (this.routes.size === 0) await this.closeHttpServer();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
getHttpServerAddress(): AddressInfo | undefined {
|
|
86
|
+
const address = this.httpServer?.address();
|
|
87
|
+
return isRecord(address) && typeof address.port === "number" ? address as AddressInfo : undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
getEndpointCount(): number {
|
|
91
|
+
return this.routes.size;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
hasPendingPiToolCallId(piToolCallId: string): boolean {
|
|
95
|
+
for (const run of this.runs) {
|
|
96
|
+
if (run.hasPendingPiToolCallId(piToolCallId)) return true;
|
|
97
|
+
}
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
cancelPendingPiToolCallId(piToolCallId: string, reason: string): boolean {
|
|
102
|
+
for (const run of this.runs) {
|
|
103
|
+
if (run.cancelPendingPiToolCallId(piToolCallId, reason)) return true;
|
|
104
|
+
}
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private async ensureHttpServer(): Promise<void> {
|
|
109
|
+
if (this.httpServer) {
|
|
110
|
+
await this.listenPromise;
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const server = createServer((req, res) => {
|
|
115
|
+
void this.handleHttpRequest(req, res);
|
|
116
|
+
});
|
|
117
|
+
this.httpServer = server;
|
|
118
|
+
this.listenPromise = new Promise<void>((resolve, reject) => {
|
|
119
|
+
const onError = (error: Error) => {
|
|
120
|
+
server.off("listening", onListening);
|
|
121
|
+
reject(error);
|
|
122
|
+
};
|
|
123
|
+
const onListening = () => {
|
|
124
|
+
server.off("error", onError);
|
|
125
|
+
resolve();
|
|
126
|
+
};
|
|
127
|
+
server.once("error", onError);
|
|
128
|
+
server.once("listening", onListening);
|
|
129
|
+
server.listen(0, LOOPBACK_HOST);
|
|
130
|
+
});
|
|
131
|
+
await this.listenPromise;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private async closeHttpServer(): Promise<void> {
|
|
135
|
+
const server = this.httpServer;
|
|
136
|
+
if (!server) return;
|
|
137
|
+
this.httpServer = undefined;
|
|
138
|
+
this.listenPromise = undefined;
|
|
139
|
+
await new Promise<void>((resolve, reject) => {
|
|
140
|
+
let settled = false;
|
|
141
|
+
let closeTimer: ReturnType<typeof setTimeout> | undefined;
|
|
142
|
+
const settle = (error?: Error): void => {
|
|
143
|
+
if (settled) return;
|
|
144
|
+
settled = true;
|
|
145
|
+
if (closeTimer) clearTimeout(closeTimer);
|
|
146
|
+
if (error) reject(error);
|
|
147
|
+
else resolve();
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
closeTimer = setTimeout(() => settle(), HTTP_SERVER_CLOSE_GRACE_MS);
|
|
151
|
+
closeTimer.unref?.();
|
|
152
|
+
|
|
153
|
+
server.close((error) => {
|
|
154
|
+
settle(error ?? undefined);
|
|
155
|
+
});
|
|
156
|
+
server.closeIdleConnections();
|
|
157
|
+
server.closeAllConnections();
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
private async handleHttpRequest(req: IncomingMessage, res: ServerResponse): Promise<void> {
|
|
162
|
+
if (req.socket.localAddress !== LOOPBACK_HOST) {
|
|
163
|
+
res.writeHead(403, { "content-type": "application/json" }).end(JSON.stringify({ error: "Cursor pi tool bridge only accepts loopback requests" }));
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const url = new URL(req.url ?? "/", `http://${LOOPBACK_HOST}`);
|
|
168
|
+
const run = this.routes.get(url.pathname);
|
|
169
|
+
if (!run) {
|
|
170
|
+
res.writeHead(404, { "content-type": "application/json" }).end(JSON.stringify({ error: "Cursor pi tool bridge endpoint not found" }));
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
try {
|
|
175
|
+
await run.handleHttpRequest(req, res);
|
|
176
|
+
} catch (error) {
|
|
177
|
+
if (!res.headersSent) {
|
|
178
|
+
res.writeHead(500, { "content-type": "application/json" }).end(JSON.stringify({ error: error instanceof Error ? error.message : String(error) }));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CursorPiBridgeToolDefinition,
|
|
3
|
+
CursorPiToolBridgeSnapshot,
|
|
4
|
+
CursorPiToolBridgeSnapshotApi,
|
|
5
|
+
CursorPiToolBridgeSnapshotOptions,
|
|
6
|
+
} from "./cursor-pi-tool-bridge-types.js";
|
|
7
|
+
import { parseEnvBoolean } from "./cursor-env-boolean.js";
|
|
8
|
+
import { createMcpToolName, normalizeMcpInputSchema, stableNameHash } from "./cursor-pi-tool-bridge-mcp.js";
|
|
9
|
+
import { isRegisteredCursorNativeToolName } from "./cursor-native-tool-display-state.js";
|
|
10
|
+
import { isExcludedFromCursorBridgeExposure } from "./cursor-tool-names.js";
|
|
11
|
+
|
|
12
|
+
export const CURSOR_PI_TOOL_BRIDGE_ENV = "PI_CURSOR_PI_TOOL_BRIDGE";
|
|
13
|
+
export const CURSOR_PI_TOOL_BRIDGE_BUILTINS_ENV = "PI_CURSOR_EXPOSE_BUILTIN_TOOLS";
|
|
14
|
+
|
|
15
|
+
const OVERLAPPING_CURSOR_NATIVE_PI_BUILTIN_TOOL_NAMES = new Set(["read", "bash", "write", "edit", "grep", "find", "ls"]);
|
|
16
|
+
|
|
17
|
+
export function createEmptySnapshot(): CursorPiToolBridgeSnapshot {
|
|
18
|
+
return {
|
|
19
|
+
tools: [],
|
|
20
|
+
mcpToolNameToPiToolName: new Map(),
|
|
21
|
+
piToolNameToMcpToolName: new Map(),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function resolveCursorPiToolBridgeEnabled(env: Record<string, string | undefined> = process.env): boolean {
|
|
26
|
+
return parseEnvBoolean(env[CURSOR_PI_TOOL_BRIDGE_ENV], true);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function resolveCursorPiToolBridgeBuiltinsEnabled(env: Record<string, string | undefined> = process.env): boolean {
|
|
30
|
+
return parseEnvBoolean(env[CURSOR_PI_TOOL_BRIDGE_BUILTINS_ENV], false);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function isOverlappingCursorNativePiToolName(toolName: string): boolean {
|
|
34
|
+
return OVERLAPPING_CURSOR_NATIVE_PI_BUILTIN_TOOL_NAMES.has(toolName);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function buildCursorPiToolBridgeSurfaceSignature(snapshot: CursorPiToolBridgeSnapshot): string {
|
|
38
|
+
if (snapshot.tools.length === 0) return "bridge:empty";
|
|
39
|
+
const serializedTools = snapshot.tools
|
|
40
|
+
.map((tool) =>
|
|
41
|
+
JSON.stringify({
|
|
42
|
+
piToolName: tool.piToolName,
|
|
43
|
+
mcpToolName: tool.mcpToolName,
|
|
44
|
+
description: tool.description,
|
|
45
|
+
inputSchema: tool.inputSchema,
|
|
46
|
+
source: tool.sourceInfo?.source,
|
|
47
|
+
path: tool.sourceInfo?.path,
|
|
48
|
+
scope: tool.sourceInfo?.scope,
|
|
49
|
+
}),
|
|
50
|
+
)
|
|
51
|
+
.sort()
|
|
52
|
+
.join("\0");
|
|
53
|
+
return `bridge:on:${stableNameHash(serializedTools)}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function buildCursorPiToolBridgeSnapshot(
|
|
57
|
+
pi: CursorPiToolBridgeSnapshotApi,
|
|
58
|
+
options: CursorPiToolBridgeSnapshotOptions = {},
|
|
59
|
+
): CursorPiToolBridgeSnapshot {
|
|
60
|
+
const activeToolNames = new Set(pi.getActiveTools());
|
|
61
|
+
const allTools = pi.getAllTools();
|
|
62
|
+
const usedMcpToolNames = new Set<string>();
|
|
63
|
+
const mcpToolNameToPiToolName = new Map<string, string>();
|
|
64
|
+
const piToolNameToMcpToolName = new Map<string, string>();
|
|
65
|
+
const tools: CursorPiBridgeToolDefinition[] = [];
|
|
66
|
+
|
|
67
|
+
const exposeOverlappingBuiltins = options.exposeOverlappingBuiltins === true;
|
|
68
|
+
|
|
69
|
+
for (const tool of allTools) {
|
|
70
|
+
if (!activeToolNames.has(tool.name)) continue;
|
|
71
|
+
if (isExcludedFromCursorBridgeExposure(tool.name) && isRegisteredCursorNativeToolName(tool.name)) continue;
|
|
72
|
+
if (!exposeOverlappingBuiltins && isOverlappingCursorNativePiToolName(tool.name)) continue;
|
|
73
|
+
|
|
74
|
+
const mcpToolName = createMcpToolName(tool.name, usedMcpToolNames);
|
|
75
|
+
const description = tool.description || `Run pi tool ${tool.name}`;
|
|
76
|
+
mcpToolNameToPiToolName.set(mcpToolName, tool.name);
|
|
77
|
+
piToolNameToMcpToolName.set(tool.name, mcpToolName);
|
|
78
|
+
tools.push({
|
|
79
|
+
piToolName: tool.name,
|
|
80
|
+
mcpToolName,
|
|
81
|
+
description,
|
|
82
|
+
inputSchema: normalizeMcpInputSchema(tool.parameters),
|
|
83
|
+
sourceInfo: tool.sourceInfo,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return { tools, mcpToolNameToPiToolName, piToolNameToMcpToolName };
|
|
88
|
+
}
|