mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
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>
This commit is contained in:
committed by
GitHub
Unverified
parent
4535146139
commit
889abb4657
@@ -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 })),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Response | undefined>
|
||||
} = {},
|
||||
) {
|
||||
@@ -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<CreatedForm>, 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<Form.Info>()
|
||||
const created = yield* Deferred.make<CreatedForm>()
|
||||
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<CreatedForm>()
|
||||
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<Form.Info>()
|
||||
const created = yield* Deferred.make<CreatedForm>()
|
||||
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),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user