From 467ebdd79d42dbae09454254866c5fd6798dc838 Mon Sep 17 00:00:00 2001 From: TCWTEAM <30012317+TCWTEAM@users.noreply.github.com> Date: Fri, 6 Nov 2026 02:30:52 -0600 Subject: [PATCH 1/5] perf(cursor): turns stop reconnecting every MCP server --- .../orchestrator-mcp-server.md | 13 ++++++---- .../src/server/adapter.test.ts | 24 +++++++++++-------- .../provider-cursor/src/server/adapter.ts | 18 +++++++++++--- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/docs/orchestration-v2/orchestrator-mcp-server.md b/docs/orchestration-v2/orchestrator-mcp-server.md index 2f6ba5363703a..91cb7b35d464c 100644 --- a/docs/orchestration-v2/orchestrator-mcp-server.md +++ b/docs/orchestration-v2/orchestrator-mcp-server.md @@ -100,11 +100,14 @@ server headers or token. ### Cursor Agent SDK V2 Cursor receives the same authenticated HTTP MCP endpoint through the SDK's -`mcpServers` on `Agent.create` / `Agent.resume` and each `send`. With -`@cursor/sdk` 1.0.39+, send-level servers replace only the create-time inline -set; ambient servers from `settingSources` (`project`, `user`, `team`, `mdm`, -`plugins`) stay available. The adapter passes the authorization header to the -SDK but projects only redacted option metadata into protocol diagnostics. +`mcpServers` on `Agent.create` / `Agent.resume`, and on a `send` only when the +endpoint or token changed since the agent opened. With `@cursor/sdk` 1.0.39+, +send-level servers replace only the create-time inline set; ambient servers from +`settingSources` (`project`, `user`, `team`, `mdm`, `plugins`) stay available. +A send that carries `mcpServers` reconnects every server, ambient ones included, +which respawns stdio servers and delays the run. The adapter passes the +authorization header to the SDK but projects only redacted option metadata into +protocol diagnostics. ### Grok ACP V2 diff --git a/packages/provider-cursor/src/server/adapter.test.ts b/packages/provider-cursor/src/server/adapter.test.ts index cc8afd31311bb..b38f44267f2ee 100644 --- a/packages/provider-cursor/src/server/adapter.test.ts +++ b/packages/provider-cursor/src/server/adapter.test.ts @@ -40,14 +40,14 @@ const { ConfigurationError } = loadCursorSdk(); describe("CursorAdapterV2", () => { it.effect.each([ - { status: "finished", model: undefined, lateModel: undefined }, - { status: "cancelled", model: "claude-opus-4-6", lateModel: undefined }, - { status: "error", model: "custom-fable", lateModel: undefined }, - { status: "finished", model: undefined, lateModel: "gpt-6-sol" }, - { status: "finished", model: "gpt-6-sol", lateModel: null }, + { status: "finished", model: undefined, lateModel: undefined, rotateMcpSession: false }, + { status: "cancelled", model: "claude-opus-4-6", lateModel: undefined, rotateMcpSession: true }, + { status: "error", model: "custom-fable", lateModel: undefined, rotateMcpSession: false }, + { status: "finished", model: undefined, lateModel: "gpt-6-sol", rotateMcpSession: false }, + { status: "finished", model: "gpt-6-sol", lateModel: null, rotateMcpSession: false }, ] as const)( "projects Cursor tasks and unfinished tools: $status, late model $lateModel", - ({ status, model, lateModel }) => + ({ status, model, lateModel, rotateMcpSession }) => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const workspace = yield* fileSystem.makeTempDirectoryScoped({ @@ -56,7 +56,7 @@ describe("CursorAdapterV2", () => { const instanceId = ProviderInstanceId.make("cursor"); const threadId = ThreadId.make("cursor-lifecycle-thread"); const mcpSessions = yield* McpProviderSessions.McpProviderSessions; - yield* mcpSessions.set({ + const mcpSession = { environmentId: EnvironmentId.make("cursor-lifecycle-environment"), threadId, providerSessionId: "cursor-lifecycle-session", @@ -64,8 +64,9 @@ describe("CursorAdapterV2", () => { endpoint: "http://127.0.0.1:43123/mcp", authorizationHeader: "Bearer synthetic-cursor-token", browserToolsAvailable: true, - }); - let sentWithMcpOverride = true; + }; + yield* mcpSessions.set(mcpSession); + let sentWithMcpOverride: boolean | undefined; const modelSelection = { instanceId, model: "composer-2.5" }; const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ runtimeMode: "full-access", @@ -174,6 +175,9 @@ describe("CursorAdapterV2", () => { modelSelection, runtimePolicy, }); + if (rotateMcpSession) { + yield* mcpSessions.set({ ...mcpSession, authorizationHeader: "Bearer rotated-token" }); + } const now = yield* DateTime.now; yield* runtime.startTurn({ threadId, @@ -220,7 +224,7 @@ describe("CursorAdapterV2", () => { Stream.takeUntil((event) => event.type === "turn.terminal"), Stream.runCollect, ); - assert.isTrue(sentWithMcpOverride); + assert.equal(sentWithMcpOverride, rotateMcpSession); const rows = events.filter((event) => event.type === "subagent.updated"); assert.equal(rows[0]?.subagent.status, "running"); assert.equal(rows[0]?.subagent.model, model ?? null); diff --git a/packages/provider-cursor/src/server/adapter.ts b/packages/provider-cursor/src/server/adapter.ts index e5b77e1041d4a..a4477a86b229b 100644 --- a/packages/provider-cursor/src/server/adapter.ts +++ b/packages/provider-cursor/src/server/adapter.ts @@ -216,6 +216,11 @@ export function cursorMcpServers( }; } +const sameMcpEndpoint = ( + left: McpProviderSession.McpProviderSessionConfig | undefined, + right: McpProviderSession.McpProviderSessionConfig | undefined, +) => left?.endpoint === right?.endpoint && left?.authorizationHeader === right?.authorizationHeader; + function providerSession(input: { readonly providerSessionId: OrchestrationV2ProviderSession["id"]; readonly providerInstanceId: ProviderInstanceId; @@ -955,6 +960,7 @@ interface ActiveCursorTurn { interface CursorLiveAgent { readonly nativeThreadId: string; readonly session: CursorAgentSdk.CursorAgentSdkSession; + readonly mcpSession: McpProviderSession.McpProviderSessionConfig | undefined; } export interface CursorAdapterV2Options { @@ -2242,13 +2248,14 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( yield* existing.session.close.pipe(Effect.ignore); yield* Ref.set(liveAgent, null); } + const mcpSession = yield* mcpSessions.read(openInput.threadId); const options = makeCursorAgentOptions({ ...(apiKey === undefined ? {} : { apiKey }), modelSelection: openInput.modelSelection, runtimePolicy: openInput.runtimePolicy, platform, threadId: openInput.threadId, - mcpSession: yield* mcpSessions.read(openInput.threadId), + mcpSession, }); if ( platform === "win32" && @@ -2273,6 +2280,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( const next = { nativeThreadId: sdkSession.agentId, session: sdkSession, + mcpSession, } satisfies CursorLiveAgent; yield* Ref.set(liveAgent, next); return next; @@ -2399,7 +2407,11 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( const compacted = message === "/compress"; const pendingUpdates: Array = []; let context: ActiveCursorTurn | null = null; - // Create-time and send-time mcpServers merge with settingSources (SDK >= 1.0.39). + // Send-time mcpServers make the SDK reconnect every MCP server, ambient ones included, + // so send them only when they no longer match the set the agent was opened with. + const sendMcpServers = sameMcpEndpoint(agent.mcpSession, mcpSession) + ? undefined + : mcpServers; const sdkRun = yield* agent.session .send({ message, @@ -2407,7 +2419,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( options: { model: cursorSdkModelSelection(turnInput.modelSelection), mode: turnInput.runtimePolicy.interactionMode === "plan" ? "plan" : "agent", - ...(mcpServers === undefined ? {} : { mcpServers }), + ...(sendMcpServers === undefined ? {} : { mcpServers: sendMcpServers }), }, onDelta: (update) => { if (context === null) { From 5bb06303c706216ad1003b685e75c5f35f9e6ee2 Mon Sep 17 00:00:00 2001 From: TCWTEAM <30012317+TCWTEAM@users.noreply.github.com> Date: Sat, 7 Nov 2026 10:57:15 -0600 Subject: [PATCH 2/5] perf(cursor): reopen the agent when its MCP session changes A rotated MCP token used to make every later send carry mcpServers and reconnect. Reopening once keeps later sends cheap. --- .../orchestrator-mcp-server.md | 15 +++---- .../src/server/adapter.test.ts | 45 +++++++++++-------- .../provider-cursor/src/server/adapter.ts | 13 +++--- 3 files changed, 38 insertions(+), 35 deletions(-) diff --git a/docs/orchestration-v2/orchestrator-mcp-server.md b/docs/orchestration-v2/orchestrator-mcp-server.md index 91cb7b35d464c..7bcbc4fcc0cbf 100644 --- a/docs/orchestration-v2/orchestrator-mcp-server.md +++ b/docs/orchestration-v2/orchestrator-mcp-server.md @@ -100,14 +100,13 @@ server headers or token. ### Cursor Agent SDK V2 Cursor receives the same authenticated HTTP MCP endpoint through the SDK's -`mcpServers` on `Agent.create` / `Agent.resume`, and on a `send` only when the -endpoint or token changed since the agent opened. With `@cursor/sdk` 1.0.39+, -send-level servers replace only the create-time inline set; ambient servers from -`settingSources` (`project`, `user`, `team`, `mdm`, `plugins`) stay available. -A send that carries `mcpServers` reconnects every server, ambient ones included, -which respawns stdio servers and delays the run. The adapter passes the -authorization header to the SDK but projects only redacted option metadata into -protocol diagnostics. +`mcpServers` on `Agent.create` / `Agent.resume`. Ambient servers from +`settingSources` (`project`, `user`, `team`, `mdm`, `plugins`) load alongside it. +Sends never carry `mcpServers`: a send that does makes the SDK reconnect every +server, ambient ones included, which respawns stdio servers and delays the run. +When the endpoint or token changes, the adapter reopens the agent with the new +set instead. The adapter passes the authorization header to the SDK but projects +only redacted option metadata into protocol diagnostics. ### Grok ACP V2 diff --git a/packages/provider-cursor/src/server/adapter.test.ts b/packages/provider-cursor/src/server/adapter.test.ts index b38f44267f2ee..093ebea700e48 100644 --- a/packages/provider-cursor/src/server/adapter.test.ts +++ b/packages/provider-cursor/src/server/adapter.test.ts @@ -40,14 +40,14 @@ const { ConfigurationError } = loadCursorSdk(); describe("CursorAdapterV2", () => { it.effect.each([ - { status: "finished", model: undefined, lateModel: undefined, rotateMcpSession: false }, - { status: "cancelled", model: "claude-opus-4-6", lateModel: undefined, rotateMcpSession: true }, - { status: "error", model: "custom-fable", lateModel: undefined, rotateMcpSession: false }, - { status: "finished", model: undefined, lateModel: "gpt-6-sol", rotateMcpSession: false }, - { status: "finished", model: "gpt-6-sol", lateModel: null, rotateMcpSession: false }, + { status: "finished", model: undefined, lateModel: undefined }, + { status: "cancelled", model: "claude-opus-4-6", lateModel: undefined }, + { status: "error", model: "custom-fable", lateModel: undefined }, + { status: "finished", model: undefined, lateModel: "gpt-6-sol" }, + { status: "finished", model: "gpt-6-sol", lateModel: null }, ] as const)( "projects Cursor tasks and unfinished tools: $status, late model $lateModel", - ({ status, model, lateModel, rotateMcpSession }) => + ({ status, model, lateModel }) => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const workspace = yield* fileSystem.makeTempDirectoryScoped({ @@ -56,7 +56,7 @@ describe("CursorAdapterV2", () => { const instanceId = ProviderInstanceId.make("cursor"); const threadId = ThreadId.make("cursor-lifecycle-thread"); const mcpSessions = yield* McpProviderSessions.McpProviderSessions; - const mcpSession = { + yield* mcpSessions.set({ environmentId: EnvironmentId.make("cursor-lifecycle-environment"), threadId, providerSessionId: "cursor-lifecycle-session", @@ -64,9 +64,8 @@ describe("CursorAdapterV2", () => { endpoint: "http://127.0.0.1:43123/mcp", authorizationHeader: "Bearer synthetic-cursor-token", browserToolsAvailable: true, - }; - yield* mcpSessions.set(mcpSession); - let sentWithMcpOverride: boolean | undefined; + }); + let sentWithMcpOverride = true; const modelSelection = { instanceId, model: "composer-2.5" }; const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ runtimeMode: "full-access", @@ -175,9 +174,6 @@ describe("CursorAdapterV2", () => { modelSelection, runtimePolicy, }); - if (rotateMcpSession) { - yield* mcpSessions.set({ ...mcpSession, authorizationHeader: "Bearer rotated-token" }); - } const now = yield* DateTime.now; yield* runtime.startTurn({ threadId, @@ -224,7 +220,7 @@ describe("CursorAdapterV2", () => { Stream.takeUntil((event) => event.type === "turn.terminal"), Stream.runCollect, ); - assert.equal(sentWithMcpOverride, rotateMcpSession); + assert.isFalse(sentWithMcpOverride); const rows = events.filter((event) => event.type === "subagent.updated"); assert.equal(rows[0]?.subagent.status, "running"); assert.equal(rows[0]?.subagent.model, model ?? null); @@ -669,17 +665,19 @@ describe("CursorAdapterV2", () => { const fileSystem = yield* FileSystem.FileSystem; const workspace = yield* fileSystem.makeTempDirectoryScoped({ prefix: "cursor-v2-device-" }); const sentMessages: Array = []; + const sentMcpOverrides: Array = []; + let opens = 0; const instanceId = ProviderInstanceId.make("cursor"); const threadId = ThreadId.make("cursor-device-thread"); const mcpSessions = yield* McpProviderSessions.McpProviderSessions; - const setDeviceAccess = (device: boolean) => + const setDeviceAccess = (device: boolean, token = "synthetic-cursor-token") => mcpSessions.set({ environmentId: EnvironmentId.make("cursor-device-environment"), threadId, providerSessionId: "cursor-device-session", providerInstanceId: instanceId, endpoint: "http://127.0.0.1:43123/mcp", - authorizationHeader: "Bearer synthetic-cursor-token", + authorizationHeader: `Bearer ${token}`, browserToolsAvailable: true, capabilities: new Set(device ? ["device"] : []), }); @@ -695,13 +693,15 @@ describe("CursorAdapterV2", () => { }).pipe( Effect.provideService(CursorAgentSdk.CursorAgentSdkRunner, { assertComplete: Effect.void, - open: () => - Effect.succeed({ + open: () => { + opens += 1; + return Effect.succeed({ agentId: "native-cursor-device", listMessages: Effect.succeed([]), close: Effect.void, send: (input) => Effect.sync(() => { + sentMcpOverrides.push(Object.hasOwn(input.options ?? {}, "mcpServers")); sentMessages.push( typeof input.message === "string" ? input.message : input.message.text, ); @@ -719,7 +719,8 @@ describe("CursorAdapterV2", () => { cancel: Effect.void, }; }), - }), + }); + }, }), Effect.provide(TestProviderHost.layer({ cwd: workspace })), ); @@ -808,6 +809,12 @@ describe("CursorAdapterV2", () => { yield* setDeviceAccess(true); assert.deepEqual(yield* send(), told(false, true), "device access turned on later"); assert.deepEqual(yield* send(), told(false, false)); + const opensBeforeRotation = opens; + yield* setDeviceAccess(true, "rotated-cursor-token"); + yield* send(); + yield* send(); + assert.equal(opens, opensBeforeRotation + 1, "a rotated MCP token reopens the agent once"); + assert.notInclude(sentMcpOverrides, true); }).pipe( Effect.scoped, Effect.provide( diff --git a/packages/provider-cursor/src/server/adapter.ts b/packages/provider-cursor/src/server/adapter.ts index a4477a86b229b..bae5395671961 100644 --- a/packages/provider-cursor/src/server/adapter.ts +++ b/packages/provider-cursor/src/server/adapter.ts @@ -2237,10 +2237,14 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( readonly agentId?: string; }) { const existing = yield* Ref.get(liveAgent); + const mcpSession = yield* mcpSessions.read(openInput.threadId); + // A send that carries mcpServers makes the SDK reconnect every MCP server, ambient ones + // included, so an agent whose MCP session changed is reopened rather than overridden. if ( existing !== null && openInput.operation === "resume" && - existing.nativeThreadId === openInput.agentId + existing.nativeThreadId === openInput.agentId && + sameMcpEndpoint(existing.mcpSession, mcpSession) ) { return existing; } @@ -2248,7 +2252,6 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( yield* existing.session.close.pipe(Effect.ignore); yield* Ref.set(liveAgent, null); } - const mcpSession = yield* mcpSessions.read(openInput.threadId); const options = makeCursorAgentOptions({ ...(apiKey === undefined ? {} : { apiKey }), modelSelection: openInput.modelSelection, @@ -2407,11 +2410,6 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( const compacted = message === "/compress"; const pendingUpdates: Array = []; let context: ActiveCursorTurn | null = null; - // Send-time mcpServers make the SDK reconnect every MCP server, ambient ones included, - // so send them only when they no longer match the set the agent was opened with. - const sendMcpServers = sameMcpEndpoint(agent.mcpSession, mcpSession) - ? undefined - : mcpServers; const sdkRun = yield* agent.session .send({ message, @@ -2419,7 +2417,6 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( options: { model: cursorSdkModelSelection(turnInput.modelSelection), mode: turnInput.runtimePolicy.interactionMode === "plan" ? "plan" : "agent", - ...(sendMcpServers === undefined ? {} : { mcpServers: sendMcpServers }), }, onDelta: (update) => { if (context === null) { From 643e551ac45e5a999be0d985725c1dfa36d7e7a1 Mon Sep 17 00:00:00 2001 From: TCWTEAM <30012317+TCWTEAM@users.noreply.github.com> Date: Sat, 7 Nov 2026 11:04:29 -0600 Subject: [PATCH 3/5] test(cursor): the reopened agent carries the rotated MCP token --- packages/provider-cursor/src/server/adapter.test.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/provider-cursor/src/server/adapter.test.ts b/packages/provider-cursor/src/server/adapter.test.ts index 093ebea700e48..02fdda841f05b 100644 --- a/packages/provider-cursor/src/server/adapter.test.ts +++ b/packages/provider-cursor/src/server/adapter.test.ts @@ -667,6 +667,7 @@ describe("CursorAdapterV2", () => { const sentMessages: Array = []; const sentMcpOverrides: Array = []; let opens = 0; + let openedMcpServers: ReturnType; const instanceId = ProviderInstanceId.make("cursor"); const threadId = ThreadId.make("cursor-device-thread"); const mcpSessions = yield* McpProviderSessions.McpProviderSessions; @@ -693,8 +694,9 @@ describe("CursorAdapterV2", () => { }).pipe( Effect.provideService(CursorAgentSdk.CursorAgentSdkRunner, { assertComplete: Effect.void, - open: () => { + open: (input) => { opens += 1; + openedMcpServers = input.options.mcpServers; return Effect.succeed({ agentId: "native-cursor-device", listMessages: Effect.succeed([]), @@ -811,9 +813,11 @@ describe("CursorAdapterV2", () => { assert.deepEqual(yield* send(), told(false, false)); const opensBeforeRotation = opens; yield* setDeviceAccess(true, "rotated-cursor-token"); + const rotated = yield* mcpSessions.read(threadId); yield* send(); yield* send(); assert.equal(opens, opensBeforeRotation + 1, "a rotated MCP token reopens the agent once"); + assert.deepEqual(openedMcpServers, cursorMcpServers(rotated)); assert.notInclude(sentMcpOverrides, true); }).pipe( Effect.scoped, From ed85c2feff3e8e91910127de7713633121d737e7 Mon Sep 17 00:00:00 2001 From: TCWTEAM <30012317+TCWTEAM@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:49:01 -0600 Subject: [PATCH 4/5] fix(cursor): an edited mcp.json reaches the live agent The SDK rereads the Cursor MCP files only when it builds a new MCP lease, so an agent that keeps its lease is reopened when mcp.json or mcp-disabled.json change. --- .../orchestrator-mcp-server.md | 10 +++++--- .../src/server/adapter.test.ts | 23 +++++++++++++---- .../provider-cursor/src/server/adapter.ts | 25 ++++++++++++------- .../src/server/workspaceMcpServers.ts | 20 +++++++++++---- 4 files changed, 55 insertions(+), 23 deletions(-) diff --git a/docs/orchestration-v2/orchestrator-mcp-server.md b/docs/orchestration-v2/orchestrator-mcp-server.md index f73970e50a9a0..93ca2934d994e 100644 --- a/docs/orchestration-v2/orchestrator-mcp-server.md +++ b/docs/orchestration-v2/orchestrator-mcp-server.md @@ -103,10 +103,12 @@ Cursor receives the same authenticated HTTP MCP endpoint through the SDK's `mcpServers` on `Agent.create` / `Agent.resume`, alongside `mcp.json` servers whose `${workspaceFolder}` paths the SDK cannot start as written. Ambient servers from `settingSources` (`project`, `user`, `team`, `mdm`, `plugins`) load with them. -Sends never carry `mcpServers`: a send that does makes the SDK reconnect every -server, ambient ones included, which respawns stdio servers and delays the run. -The adapter resolves the set on every turn and reopens the agent when it changes, -such as a new endpoint or token, or an edit to `mcp.json` or `mcp-disabled.json`. +Sends never carry `mcpServers`: a send that does makes the SDK reread the Cursor +MCP files and reconnect every server, ambient ones included, which respawns stdio +servers and delays the run. A live agent keeps the files it opened with, so the +adapter reads them and resolves the set on every turn, and reopens the agent when +either changes, such as a new endpoint or token, or an edit to `mcp.json` or +`mcp-disabled.json`. The adapter passes the authorization header to the SDK but projects only redacted option metadata into protocol diagnostics. diff --git a/packages/provider-cursor/src/server/adapter.test.ts b/packages/provider-cursor/src/server/adapter.test.ts index a6d33f15cb2a1..ac09187335d70 100644 --- a/packages/provider-cursor/src/server/adapter.test.ts +++ b/packages/provider-cursor/src/server/adapter.test.ts @@ -2257,12 +2257,25 @@ describe("CursorAdapterV2", () => { assert.deepEqual(openedWith?.mcpServers, { ...expectedServers, ...t3Servers }); yield* sendTurn(1); assert.equal(opens, 1, "an unchanged mcp.json keeps the open agent"); + // The SDK loads the servers that are not inline from the files, so editing those reopens it. + yield* fileSystem.writeFileString( + path.join(home, ".cursor", "mcp.json"), + JSON.stringify({ mcpServers: { docs: { url: "https://docs.example/v2" } } }), + ); + yield* sendTurn(2); + yield* fileSystem.writeFileString( + path.join(home, ".cursor", "projects", projectDirectory, "mcp-disabled.json"), + JSON.stringify(["off", "plain"]), + ); + yield* sendTurn(3); + assert.deepEqual(openedWith?.mcpServers, { ...expectedServers, ...t3Servers }); + assert.equal(opens, 3, "edits to servers the files define reopen the agent"); // A server switched off between turns stays off on the next one. yield* fileSystem.writeFileString( path.join(home, ".cursor", "projects", projectDirectory, "mcp-disabled.json"), JSON.stringify(["off", "repro"]), ); - yield* sendTurn(2); + yield* sendTurn(4); const { repro: _disabled, ...stillEnabled } = expectedServers; assert.deepEqual(openedWith?.mcpServers, { ...stillEnabled, ...t3Servers }); // Unreadable disabled state starts none of the overrides, and the turn still goes out. @@ -2270,16 +2283,16 @@ describe("CursorAdapterV2", () => { path.join(home, ".cursor", "projects", projectDirectory, "mcp-disabled.json"), "[", ); - yield* sendTurn(3); + yield* sendTurn(5); assert.deepEqual(openedWith?.mcpServers, t3Servers); - assert.equal(opens, 3, "each change to the servers reopens the agent once"); - assert.deepEqual(sentWith, [undefined, undefined, undefined]); + assert.equal(opens, 5, "each change to the servers or files reopens the agent once"); + assert.deepEqual(sentWith, [undefined, undefined, undefined, undefined, undefined]); yield* fileSystem.writeFileString( path.join(workspace, ".cursor", "mcp.json"), JSON.stringify({ mcpServers: { plain: { command: "node", args: ["server.mjs"] } } }), ); yield* fileSystem.remove(path.join(home, ".cursor", "mcp.json")); - assert.deepEqual(yield* loadServers(), {}); + assert.deepEqual((yield* loadServers()).servers, {}); }).pipe( Effect.scoped, Effect.provide( diff --git a/packages/provider-cursor/src/server/adapter.ts b/packages/provider-cursor/src/server/adapter.ts index 0619f69fabc22..f8b022219352a 100644 --- a/packages/provider-cursor/src/server/adapter.ts +++ b/packages/provider-cursor/src/server/adapter.ts @@ -57,7 +57,7 @@ import { hasCursorSkillMention, rewriteCursorSkillMentions, } from "./skills.ts"; -import { cursorWorkspaceMcpServers } from "./workspaceMcpServers.ts"; +import { cursorWorkspaceMcpServers, type CursorWorkspaceMcp } from "./workspaceMcpServers.ts"; import { t3OrchestrationPrompt } from "@t3tools/provider-core/server/orchestrationInstructions"; import { buildRuntimeInstructions } from "@t3tools/provider-core/server/runtimeInstructions"; import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; @@ -305,6 +305,8 @@ const CURSOR_AGENT_SETTING_SOURCES = [ "plugins", ] as const satisfies ReadonlyArray; +const NO_WORKSPACE_MCP: CursorWorkspaceMcp = { servers: {}, configFiles: [] }; + export function makeCursorAgentOptions(input: { readonly apiKey?: string; readonly modelSelection: ModelSelection; @@ -972,6 +974,7 @@ interface CursorLiveAgent { readonly storeCwd: string; readonly session: CursorAgentSdk.CursorAgentSdkSession; readonly mcpServers: Record | undefined; + readonly mcpConfigFiles: CursorWorkspaceMcp["configFiles"]; } export interface CursorAdapterV2Options { @@ -2251,9 +2254,9 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( } }; - const resolveWorkspaceMcpServers = (cwd: string | null) => + const resolveWorkspaceMcp = (cwd: string | null) => cwd === null - ? Effect.succeed({}) + ? Effect.succeed(NO_WORKSPACE_MCP) : cursorWorkspaceMcpServers(cwd, adapterOptions.environment).pipe( Effect.provideService(FileSystem.FileSystem, fileSystem), Effect.provideService(Path.Path, path), @@ -2263,7 +2266,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( Effect.logWarning("orchestration-v2.cursor-workspace-mcp-unresolved", { cwd, cause, - }).pipe(Effect.as({})), + }).pipe(Effect.as(NO_WORKSPACE_MCP)), ), ); @@ -2285,6 +2288,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( : input.resumeFromSession?.cwd : undefined) ?? cwd; + const workspaceMcp = yield* resolveWorkspaceMcp(openInput.runtimePolicy.cwd); const options = makeCursorAgentOptions({ ...(apiKey === undefined ? {} : { apiKey }), modelSelection: openInput.modelSelection, @@ -2292,16 +2296,18 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( platform, threadId: openInput.threadId, mcpSession: yield* mcpSessions.read(openInput.threadId), - workspaceMcpServers: yield* resolveWorkspaceMcpServers(openInput.runtimePolicy.cwd), + workspaceMcpServers: workspaceMcp.servers, }); - // A send that carries mcpServers makes the SDK reconnect every MCP server, ambient ones - // included. The agent is reopened instead when its servers change: a reissued T3 token, or - // an edit to mcp.json or mcp-disabled.json, which are read again on every open. + // A send that carries mcpServers makes the SDK reread the Cursor MCP files and reconnect + // every server, ambient ones included. A live agent keeps the files it opened with, so it + // is reopened instead when its servers or those files change: a reissued T3 token, or an + // edit to mcp.json or mcp-disabled.json, which are read again on every open. if ( existing !== null && openInput.operation === "resume" && existing.nativeThreadId === openInput.agentId && - Equal.equals(existing.mcpServers, options.mcpServers) + Equal.equals(existing.mcpServers, options.mcpServers) && + Equal.equals(existing.mcpConfigFiles, workspaceMcp.configFiles) ) { return existing; } @@ -2335,6 +2341,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( storeCwd, session: sdkSession, mcpServers: options.mcpServers, + mcpConfigFiles: workspaceMcp.configFiles, } satisfies CursorLiveAgent; yield* Ref.set(liveAgent, next); return next; diff --git a/packages/provider-cursor/src/server/workspaceMcpServers.ts b/packages/provider-cursor/src/server/workspaceMcpServers.ts index f3e53b508e3b3..0167d6b4be971 100644 --- a/packages/provider-cursor/src/server/workspaceMcpServers.ts +++ b/packages/provider-cursor/src/server/workspaceMcpServers.ts @@ -80,6 +80,13 @@ function cursorProjectSlug(projectRoot: string): string { .replace(/^-+|-+$/g, ""); } +/** The servers T3 Code passes inline, and the Cursor MCP files read to find them. */ +export interface CursorWorkspaceMcp { + readonly servers: Record; + /** `mcp-disabled.json` as text, then the project and user `mcp.json` as parsed. */ + readonly configFiles: ReadonlyArray; +} + export const cursorWorkspaceMcpServers = Effect.fn("cursorWorkspaceMcpServers")(function* ( cwd: string, environment: NodeJS.ProcessEnv, @@ -107,7 +114,7 @@ export const cursorWorkspaceMcpServers = Effect.fn("cursorWorkspaceMcpServers")( const projectRoot = Option.isSome(root) && root.value.code === 0 ? root.value.stdout.trim() : cwd; const userHome = yield* HostProcess.HomeDirectory; const cursorDataDirectory = hostEnvironment.CURSOR_DATA_DIR || path.join(userHome, ".cursor"); - const loadDisabled = fileSystem + const disabledFile = yield* fileSystem .readFileString( path.join( cursorDataDirectory, @@ -119,11 +126,12 @@ export const cursorWorkspaceMcpServers = Effect.fn("cursorWorkspaceMcpServers")( .pipe( Effect.catchIf( (error) => error.reason._tag === "NotFound", - () => Effect.succeed("[]"), + () => Effect.undefined, ), - Effect.flatMap(decodeDisabledServers), - Effect.map((names) => new Set(names)), ); + const loadDisabled = decodeDisabledServers(disabledFile ?? "[]").pipe( + Effect.map((names) => new Set(names)), + ); let disabled: Set | undefined; const variables = new Map([ ["workspaceFolder", projectRoot], @@ -143,11 +151,13 @@ export const cursorWorkspaceMcpServers = Effect.fn("cursorWorkspaceMcpServers")( const seen = new Set(); const servers: Record = {}; + const configFiles: Array = [disabledFile]; for (const file of [ path.join(projectRoot, ".cursor", "mcp.json"), path.join(userHome, ".cursor", "mcp.json"), ]) { const contents = yield* readJsonc(file); + configFiles.push(contents); // Like the SDK, expand before validating, so `"type": "${env:TYPE}"` is valid. const config = decodeCursorMcpFile(expand(contents, resolve)); if (Option.isNone(config)) continue; @@ -171,5 +181,5 @@ export const cursorWorkspaceMcpServers = Effect.fn("cursorWorkspaceMcpServers")( } } } - return servers; + return { servers, configFiles } satisfies CursorWorkspaceMcp; }); From b73c670c698460b37146fb24c9f9381838781602 Mon Sep 17 00:00:00 2001 From: TCWTEAM <30012317+TCWTEAM@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:58:15 -0600 Subject: [PATCH 5/5] fix(cursor): an agent without a cwd also notices mcp.json edits The SDK falls back to process.cwd() for the files it reads, so the adapter resolves them from the same directory. --- .../src/server/adapter.test.ts | 28 +++++++++++++++-- .../provider-cursor/src/server/adapter.ts | 31 +++++++++---------- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/packages/provider-cursor/src/server/adapter.test.ts b/packages/provider-cursor/src/server/adapter.test.ts index ac09187335d70..b15f2e317745e 100644 --- a/packages/provider-cursor/src/server/adapter.test.ts +++ b/packages/provider-cursor/src/server/adapter.test.ts @@ -2191,13 +2191,13 @@ describe("CursorAdapterV2", () => { Effect.provideService(HostProcess.Environment, {}), ); const now = yield* DateTime.now; - const sendTurn = (ordinal: number) => + const sendTurn = (ordinal: number, policy = runtimePolicy) => runtime .startTurn({ threadId, providerThread, modelSelection, - runtimePolicy, + runtimePolicy: policy, runId: RunId.make(`cursor-mcp-run-${ordinal}`), runOrdinal: ordinal, providerTurnOrdinal: ordinal, @@ -2286,7 +2286,29 @@ describe("CursorAdapterV2", () => { yield* sendTurn(5); assert.deepEqual(openedWith?.mcpServers, t3Servers); assert.equal(opens, 5, "each change to the servers or files reopens the agent once"); - assert.deepEqual(sentWith, [undefined, undefined, undefined, undefined, undefined]); + // Without a cwd the SDK works in the server's directory, and still reads the user mcp.json. + const noCwdPolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "full-access", + interactionMode: "default", + cwd: null, + }); + yield* sendTurn(6, noCwdPolicy); + yield* sendTurn(7, noCwdPolicy); + assert.equal( + opens, + 6, + "an agent without a cwd reopens for the files it reads, then stays open", + ); + yield* fileSystem.writeFileString( + path.join(home, ".cursor", "mcp.json"), + JSON.stringify({ mcpServers: { docs: { url: "https://docs.example/v3" } } }), + ); + yield* sendTurn(8, noCwdPolicy); + assert.equal(opens, 7, "an edited mcp.json reopens an agent without a cwd"); + assert.deepEqual( + sentWith, + Array.from({ length: 8 }, () => undefined), + ); yield* fileSystem.writeFileString( path.join(workspace, ".cursor", "mcp.json"), JSON.stringify({ mcpServers: { plain: { command: "node", args: ["server.mjs"] } } }), diff --git a/packages/provider-cursor/src/server/adapter.ts b/packages/provider-cursor/src/server/adapter.ts index f8b022219352a..1fec7b6788af7 100644 --- a/packages/provider-cursor/src/server/adapter.ts +++ b/packages/provider-cursor/src/server/adapter.ts @@ -2254,21 +2254,19 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( } }; - const resolveWorkspaceMcp = (cwd: string | null) => - cwd === null - ? Effect.succeed(NO_WORKSPACE_MCP) - : cursorWorkspaceMcpServers(cwd, adapterOptions.environment).pipe( - Effect.provideService(FileSystem.FileSystem, fileSystem), - Effect.provideService(Path.Path, path), - Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), - // Unreadable Cursor state leaves startup to the SDK rather than failing the turn. - Effect.catch((cause) => - Effect.logWarning("orchestration-v2.cursor-workspace-mcp-unresolved", { - cwd, - cause, - }).pipe(Effect.as(NO_WORKSPACE_MCP)), - ), - ); + const resolveWorkspaceMcp = (cwd: string) => + cursorWorkspaceMcpServers(cwd, adapterOptions.environment).pipe( + Effect.provideService(FileSystem.FileSystem, fileSystem), + Effect.provideService(Path.Path, path), + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + // Unreadable Cursor state leaves startup to the SDK rather than failing the turn. + Effect.catch((cause) => + Effect.logWarning("orchestration-v2.cursor-workspace-mcp-unresolved", { + cwd, + cause, + }).pipe(Effect.as(NO_WORKSPACE_MCP)), + ), + ); const openAgent = Effect.fnUntraced(function* (openInput: { readonly operation: "create" | "resume"; @@ -2288,7 +2286,8 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( : input.resumeFromSession?.cwd : undefined) ?? cwd; - const workspaceMcp = yield* resolveWorkspaceMcp(openInput.runtimePolicy.cwd); + // Without a cwd the SDK works in, and reads the Cursor MCP files from, process.cwd(). + const workspaceMcp = yield* resolveWorkspaceMcp(cwd); const options = makeCursorAgentOptions({ ...(apiKey === undefined ? {} : { apiKey }), modelSelection: openInput.modelSelection,