From 889abb4657e9a944a6ddcaaf5bc41eab5cd9a4c6 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Thu, 22 Oct 2026 14:42:02 -0500 Subject: [PATCH] fix(core): fix MCP elicitation forms never appearing (#58184) Co-authored-by: Dimitris Kourgiantakis <64262257+dimitriskourg@users.noreply.github.com> Co-authored-by: chrischall <10564245+chrischall@users.noreply.github.com> --- packages/core/src/mcp/client.ts | 10 ++- packages/core/test/mcp.test.ts | 67 +++++++++++++++---- ...@modelcontextprotocol%2Fclient@2.0.0.patch | 26 +++++++ 3 files changed, 88 insertions(+), 15 deletions(-) diff --git a/packages/core/src/mcp/client.ts b/packages/core/src/mcp/client.ts index 668ca04699e..6ab869b9a6e 100644 --- a/packages/core/src/mcp/client.ts +++ b/packages/core/src/mcp/client.ts @@ -193,7 +193,11 @@ export const connect = Effect.fnUntraced(function* ( ) const initialize = Effect.fnUntraced(function* (transport: Transport) { - const runFork = Effect.runForkWith(yield* Effect.context()) + // SDK callbacks run outside any fiber. They must run with this context so events they publish keep + // the server's Location; clients drop unlocated `form.created` events and elicitations then hang. + const context = yield* Effect.context() + const runFork = Effect.runForkWith(context) + const runPromise = Effect.runPromiseWith(context) const client = new Client(clientInfo, { capabilities: { ...(elicitation ? { elicitation: { form: {}, url: {} } } : {}), @@ -217,10 +221,10 @@ export const connect = Effect.fnUntraced(function* ( client.setRequestHandler("roots/list", () => ({ roots: [{ uri: pathToFileURL(directory).href }] })) if (elicitation) { client.setRequestHandler("elicitation/create", (request, ctx) => - Effect.runPromise(elicitation.create({ server, params: request.params, signal: ctx.mcpReq.signal })), + runPromise(elicitation.create({ server, params: request.params, signal: ctx.mcpReq.signal })), ) client.setNotificationHandler("notifications/elicitation/complete", (notification) => - Effect.runPromise(elicitation.complete({ server, elicitationID: notification.params.elicitationId })), + runPromise(elicitation.complete({ server, elicitationID: notification.params.elicitationId })), ) } diff --git a/packages/core/test/mcp.test.ts b/packages/core/test/mcp.test.ts index bee57c0a0e0..d21e53befe0 100644 --- a/packages/core/test/mcp.test.ts +++ b/packages/core/test/mcp.test.ts @@ -45,6 +45,7 @@ import { Exit, Fiber, Layer, + Option, PubSub, Ref, Schedule, @@ -97,6 +98,8 @@ function resourceServer( listChanged?: boolean emptyElicitation?: boolean urlElicitation?: boolean + /** Legacy servers give up on the URL elicitation after this many milliseconds. */ + elicitationTimeout?: number respond?: (request: Request) => Response | undefined | Promise } = {}, ) { @@ -174,12 +177,15 @@ function resourceServer( const response = inputResponse(responses, "auth") const result = input.modern ? { action: response.kind === "elicit" ? response.action : "cancel" } - : await protocol.elicitInput({ - mode: "url", - message: "Authorize access", - url, - elicitationId: "elicitation-test", - }) + : await protocol.elicitInput( + { + mode: "url", + message: "Authorize access", + url, + elicitationId: "elicitation-test", + }, + input.elicitationTimeout === undefined ? undefined : { timeout: input.elicitationTimeout }, + ) return { content: [{ type: "text", text: JSON.stringify(result) }], structuredContent: result, @@ -1260,6 +1266,17 @@ test("skips MCP resource requests when the capability is absent", async () => { ) }) +type CreatedForm = { readonly form: Form.Info; readonly directory: string | undefined } + +// Clients drop `form.created` events without a Location, so the form must be published inside the server's Location. +const recordForm = (created: Deferred.Deferred, form: Form.Info) => + Effect.serviceOption(Location.Service).pipe( + Effect.flatMap((location) => + Deferred.succeed(created, { form, directory: Option.getOrUndefined(location)?.directory }), + ), + Effect.asVoid, + ) + test("accepts empty MCP elicitations without creating forms", async () => { await Effect.runPromise( Effect.scoped( @@ -1284,13 +1301,14 @@ test("acknowledges completed MCP URL elicitations without returning internal con Effect.scoped( Effect.gen(function* () { const server = yield* resourceServer({ resources: false, urlElicitation: true }) - const created = yield* Deferred.make() + const created = yield* Deferred.make() const result = yield* Effect.gen(function* () { const service = yield* Mcp.Service const forms = yield* Form.Service const call = yield* service.callTool({ server: "resources", name: "url-elicitation" }).pipe(Effect.forkScoped) - const form = yield* Deferred.await(created) + const { form, directory } = yield* Deferred.await(created) + expect(directory).toBe(import.meta.dir) expect(form.fields).toEqual([{ key: "elicitation", type: "external", url: "https://example.com/authorize" }]) yield* Effect.promise(server.completeElicitation) @@ -1298,7 +1316,7 @@ test("acknowledges completed MCP URL elicitations without returning internal con expect(yield* forms.state(form.id)).toEqual({ status: "answered", answer: { elicitation: true } }) return result }).pipe( - Effect.provide(resourceMcpLayer(server.url, (form) => Deferred.succeed(created, form).pipe(Effect.asVoid))), + Effect.provide(resourceMcpLayer(server.url, (form) => recordForm(created, form))), ) expect(result.structured).toEqual({ action: "accept" }) @@ -1307,18 +1325,43 @@ test("acknowledges completed MCP URL elicitations without returning internal con ) }) +test("cancels MCP elicitation forms when the server gives up on them", async () => { + await Effect.runPromise( + Effect.scoped( + Effect.gen(function* () { + const server = yield* resourceServer({ resources: false, urlElicitation: true, elicitationTimeout: 200 }) + const created = yield* Deferred.make() + yield* Effect.gen(function* () { + const service = yield* Mcp.Service + const forms = yield* Form.Service + const call = yield* service.callTool({ server: "resources", name: "url-elicitation" }).pipe(Effect.forkScoped) + const { form } = yield* Deferred.await(created) + yield* Fiber.await(call) + // The server's cancellation and its tool error race; the form settles shortly after either. + const state = yield* forms.state(form.id).pipe( + Effect.repeat({ until: (state) => state.status !== "pending", schedule: Schedule.spaced("10 millis") }), + Effect.timeout("2 seconds"), + ) + expect(state.status).toBe("cancelled") + }).pipe(Effect.provide(resourceMcpLayer(server.url, (form) => recordForm(created, form)))) + }), + ), + ) +}) + test("settles modern MCP URL elicitations when the user confirms", async () => { await Effect.runPromise( Effect.scoped( Effect.gen(function* () { const server = yield* resourceServer({ modern: true, resources: false, urlElicitation: true }) - const created = yield* Deferred.make() + const created = yield* Deferred.make() const result = yield* Effect.gen(function* () { const service = yield* Mcp.Service const forms = yield* Form.Service const call = yield* service.callTool({ server: "resources", name: "url-elicitation" }).pipe(Effect.forkScoped) - const form = yield* Deferred.await(created) + const { form, directory } = yield* Deferred.await(created) + expect(directory).toBe(import.meta.dir) expect(form.metadata).not.toHaveProperty("elicitationID") yield* forms.reply({ id: form.id, answer: { elicitation: true } }) return yield* Fiber.join(call) @@ -1326,7 +1369,7 @@ test("settles modern MCP URL elicitations when the user confirms", async () => { Effect.provide( resourceMcpLayer( new ConfigMCP.Remote({ type: "remote", url: server.url, oauth: false, protocol: "2026-07-28" }), - (form) => Deferred.succeed(created, form).pipe(Effect.asVoid), + (form) => recordForm(created, form), ), ), ) diff --git a/patches/@modelcontextprotocol%2Fclient@2.0.0.patch b/patches/@modelcontextprotocol%2Fclient@2.0.0.patch index 6701e99c1de..5b56c39f7cf 100644 --- a/patches/@modelcontextprotocol%2Fclient@2.0.0.patch +++ b/patches/@modelcontextprotocol%2Fclient@2.0.0.patch @@ -56,3 +56,29 @@ index f02ce3ca394e826fc27c9848dbe9a214bf6ba7a9..3508b8e841bb8d3d724fa5ff573902db if (resource) authorizationUrl.searchParams.set("resource", resource.href); return { authorizationUrl, +diff --git a/dist/src-NAgB4Mp8.cjs b/dist/src-NAgB4Mp8.cjs +index e8f2499..07f6e5c 100644 +--- a/dist/src-NAgB4Mp8.cjs ++++ b/dist/src-NAgB4Mp8.cjs +@@ -5698,7 +5698,7 @@ var Protocol = class { + return this._requestHandlers.get(method); + } + async _oncancel(notification) { +- if (!notification.params.requestId) return; ++ if (notification.params.requestId === void 0) return; + this._requestHandlerAbortControllers.get(notification.params.requestId)?.abort(notification.params.reason); + } + _setupTimeout(messageId, timeout, maxTotalTimeout, onTimeout, resetTimeoutOnProgress = false) { +diff --git a/dist/src-D_zzAWoS.mjs b/dist/src-D_zzAWoS.mjs +index 6ac3d53..82e0aaf 100644 +--- a/dist/src-D_zzAWoS.mjs ++++ b/dist/src-D_zzAWoS.mjs +@@ -5697,7 +5697,7 @@ var Protocol = class { + return this._requestHandlers.get(method); + } + async _oncancel(notification) { +- if (!notification.params.requestId) return; ++ if (notification.params.requestId === void 0) return; + this._requestHandlerAbortControllers.get(notification.params.requestId)?.abort(notification.params.reason); + } + _setupTimeout(messageId, timeout, maxTotalTimeout, onTimeout, resetTimeoutOnProgress = false) {