Skip to content

Commit 889abb4

Browse files
rekram1-nodedimitriskourgchrischall
authored
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>
1 parent 4535146 commit 889abb4

3 files changed

Lines changed: 88 additions & 15 deletions

File tree

‎packages/core/src/mcp/client.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,11 @@ export const connect = Effect.fnUntraced(function* (
193193
)
194194

195195
const initialize = Effect.fnUntraced(function* (transport: Transport) {
196-
const runFork = Effect.runForkWith(yield* Effect.context())
196+
// SDK callbacks run outside any fiber. They must run with this context so events they publish keep
197+
// the server's Location; clients drop unlocated `form.created` events and elicitations then hang.
198+
const context = yield* Effect.context()
199+
const runFork = Effect.runForkWith(context)
200+
const runPromise = Effect.runPromiseWith(context)
197201
const client = new Client(clientInfo, {
198202
capabilities: {
199203
...(elicitation ? { elicitation: { form: {}, url: {} } } : {}),
@@ -217,10 +221,10 @@ export const connect = Effect.fnUntraced(function* (
217221
client.setRequestHandler("roots/list", () => ({ roots: [{ uri: pathToFileURL(directory).href }] }))
218222
if (elicitation) {
219223
client.setRequestHandler("elicitation/create", (request, ctx) =>
220-
Effect.runPromise(elicitation.create({ server, params: request.params, signal: ctx.mcpReq.signal })),
224+
runPromise(elicitation.create({ server, params: request.params, signal: ctx.mcpReq.signal })),
221225
)
222226
client.setNotificationHandler("notifications/elicitation/complete", (notification) =>
223-
Effect.runPromise(elicitation.complete({ server, elicitationID: notification.params.elicitationId })),
227+
runPromise(elicitation.complete({ server, elicitationID: notification.params.elicitationId })),
224228
)
225229
}
226230

‎packages/core/test/mcp.test.ts‎

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
Exit,
4646
Fiber,
4747
Layer,
48+
Option,
4849
PubSub,
4950
Ref,
5051
Schedule,
@@ -97,6 +98,8 @@ function resourceServer(
9798
listChanged?: boolean
9899
emptyElicitation?: boolean
99100
urlElicitation?: boolean
101+
/** Legacy servers give up on the URL elicitation after this many milliseconds. */
102+
elicitationTimeout?: number
100103
respond?: (request: Request) => Response | undefined | Promise<Response | undefined>
101104
} = {},
102105
) {
@@ -174,12 +177,15 @@ function resourceServer(
174177
const response = inputResponse(responses, "auth")
175178
const result = input.modern
176179
? { action: response.kind === "elicit" ? response.action : "cancel" }
177-
: await protocol.elicitInput({
178-
mode: "url",
179-
message: "Authorize access",
180-
url,
181-
elicitationId: "elicitation-test",
182-
})
180+
: await protocol.elicitInput(
181+
{
182+
mode: "url",
183+
message: "Authorize access",
184+
url,
185+
elicitationId: "elicitation-test",
186+
},
187+
input.elicitationTimeout === undefined ? undefined : { timeout: input.elicitationTimeout },
188+
)
183189
return {
184190
content: [{ type: "text", text: JSON.stringify(result) }],
185191
structuredContent: result,
@@ -1260,6 +1266,17 @@ test("skips MCP resource requests when the capability is absent", async () => {
12601266
)
12611267
})
12621268

1269+
type CreatedForm = { readonly form: Form.Info; readonly directory: string | undefined }
1270+
1271+
// Clients drop `form.created` events without a Location, so the form must be published inside the server's Location.
1272+
const recordForm = (created: Deferred.Deferred<CreatedForm>, form: Form.Info) =>
1273+
Effect.serviceOption(Location.Service).pipe(
1274+
Effect.flatMap((location) =>
1275+
Deferred.succeed(created, { form, directory: Option.getOrUndefined(location)?.directory }),
1276+
),
1277+
Effect.asVoid,
1278+
)
1279+
12631280
test("accepts empty MCP elicitations without creating forms", async () => {
12641281
await Effect.runPromise(
12651282
Effect.scoped(
@@ -1284,21 +1301,22 @@ test("acknowledges completed MCP URL elicitations without returning internal con
12841301
Effect.scoped(
12851302
Effect.gen(function* () {
12861303
const server = yield* resourceServer({ resources: false, urlElicitation: true })
1287-
const created = yield* Deferred.make<Form.Info>()
1304+
const created = yield* Deferred.make<CreatedForm>()
12881305
const result = yield* Effect.gen(function* () {
12891306
const service = yield* Mcp.Service
12901307
const forms = yield* Form.Service
12911308
const call = yield* service.callTool({ server: "resources", name: "url-elicitation" }).pipe(Effect.forkScoped)
12921309

1293-
const form = yield* Deferred.await(created)
1310+
const { form, directory } = yield* Deferred.await(created)
1311+
expect(directory).toBe(import.meta.dir)
12941312
expect(form.fields).toEqual([{ key: "elicitation", type: "external", url: "https://example.com/authorize" }])
12951313

12961314
yield* Effect.promise(server.completeElicitation)
12971315
const result = yield* Fiber.join(call)
12981316
expect(yield* forms.state(form.id)).toEqual({ status: "answered", answer: { elicitation: true } })
12991317
return result
13001318
}).pipe(
1301-
Effect.provide(resourceMcpLayer(server.url, (form) => Deferred.succeed(created, form).pipe(Effect.asVoid))),
1319+
Effect.provide(resourceMcpLayer(server.url, (form) => recordForm(created, form))),
13021320
)
13031321

13041322
expect(result.structured).toEqual({ action: "accept" })
@@ -1307,26 +1325,51 @@ test("acknowledges completed MCP URL elicitations without returning internal con
13071325
)
13081326
})
13091327

1328+
test("cancels MCP elicitation forms when the server gives up on them", async () => {
1329+
await Effect.runPromise(
1330+
Effect.scoped(
1331+
Effect.gen(function* () {
1332+
const server = yield* resourceServer({ resources: false, urlElicitation: true, elicitationTimeout: 200 })
1333+
const created = yield* Deferred.make<CreatedForm>()
1334+
yield* Effect.gen(function* () {
1335+
const service = yield* Mcp.Service
1336+
const forms = yield* Form.Service
1337+
const call = yield* service.callTool({ server: "resources", name: "url-elicitation" }).pipe(Effect.forkScoped)
1338+
const { form } = yield* Deferred.await(created)
1339+
yield* Fiber.await(call)
1340+
// The server's cancellation and its tool error race; the form settles shortly after either.
1341+
const state = yield* forms.state(form.id).pipe(
1342+
Effect.repeat({ until: (state) => state.status !== "pending", schedule: Schedule.spaced("10 millis") }),
1343+
Effect.timeout("2 seconds"),
1344+
)
1345+
expect(state.status).toBe("cancelled")
1346+
}).pipe(Effect.provide(resourceMcpLayer(server.url, (form) => recordForm(created, form))))
1347+
}),
1348+
),
1349+
)
1350+
})
1351+
13101352
test("settles modern MCP URL elicitations when the user confirms", async () => {
13111353
await Effect.runPromise(
13121354
Effect.scoped(
13131355
Effect.gen(function* () {
13141356
const server = yield* resourceServer({ modern: true, resources: false, urlElicitation: true })
1315-
const created = yield* Deferred.make<Form.Info>()
1357+
const created = yield* Deferred.make<CreatedForm>()
13161358
const result = yield* Effect.gen(function* () {
13171359
const service = yield* Mcp.Service
13181360
const forms = yield* Form.Service
13191361
const call = yield* service.callTool({ server: "resources", name: "url-elicitation" }).pipe(Effect.forkScoped)
13201362

1321-
const form = yield* Deferred.await(created)
1363+
const { form, directory } = yield* Deferred.await(created)
1364+
expect(directory).toBe(import.meta.dir)
13221365
expect(form.metadata).not.toHaveProperty("elicitationID")
13231366
yield* forms.reply({ id: form.id, answer: { elicitation: true } })
13241367
return yield* Fiber.join(call)
13251368
}).pipe(
13261369
Effect.provide(
13271370
resourceMcpLayer(
13281371
new ConfigMCP.Remote({ type: "remote", url: server.url, oauth: false, protocol: "2026-07-28" }),
1329-
(form) => Deferred.succeed(created, form).pipe(Effect.asVoid),
1372+
(form) => recordForm(created, form),
13301373
),
13311374
),
13321375
)

‎patches/@modelcontextprotocol%2Fclient@2.0.0.patch‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,29 @@ index f02ce3ca394e826fc27c9848dbe9a214bf6ba7a9..3508b8e841bb8d3d724fa5ff573902db
5656
if (resource) authorizationUrl.searchParams.set("resource", resource.href);
5757
return {
5858
authorizationUrl,
59+
diff --git a/dist/src-NAgB4Mp8.cjs b/dist/src-NAgB4Mp8.cjs
60+
index e8f2499..07f6e5c 100644
61+
--- a/dist/src-NAgB4Mp8.cjs
62+
+++ b/dist/src-NAgB4Mp8.cjs
63+
@@ -5698,7 +5698,7 @@ var Protocol = class {
64+
return this._requestHandlers.get(method);
65+
}
66+
async _oncancel(notification) {
67+
- if (!notification.params.requestId) return;
68+
+ if (notification.params.requestId === void 0) return;
69+
this._requestHandlerAbortControllers.get(notification.params.requestId)?.abort(notification.params.reason);
70+
}
71+
_setupTimeout(messageId, timeout, maxTotalTimeout, onTimeout, resetTimeoutOnProgress = false) {
72+
diff --git a/dist/src-D_zzAWoS.mjs b/dist/src-D_zzAWoS.mjs
73+
index 6ac3d53..82e0aaf 100644
74+
--- a/dist/src-D_zzAWoS.mjs
75+
+++ b/dist/src-D_zzAWoS.mjs
76+
@@ -5697,7 +5697,7 @@ var Protocol = class {
77+
return this._requestHandlers.get(method);
78+
}
79+
async _oncancel(notification) {
80+
- if (!notification.params.requestId) return;
81+
+ if (notification.params.requestId === void 0) return;
82+
this._requestHandlerAbortControllers.get(notification.params.requestId)?.abort(notification.params.reason);
83+
}
84+
_setupTimeout(messageId, timeout, maxTotalTimeout, onTimeout, resetTimeoutOnProgress = false) {

0 commit comments

Comments
 (0)