@@ -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+
12631280test ( "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+
13101352test ( "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 )
0 commit comments