@@ -10,9 +10,11 @@ import { Config } from "../config"
1010import { ConfigMCP } from "../config/mcp"
1111import { Credential } from "../credential"
1212import { EventV2 } from "../event"
13+ import { Form } from "../form"
1314import { Integration } from "../integration"
1415import { IntegrationConnection } from "../integration/connection"
1516import { Location } from "../location"
17+ import { waitForAbort } from "../process"
1618import { MCPClient } from "./client"
1719import { MCPOAuth } from "./oauth"
1820
@@ -145,6 +147,10 @@ type ServerEntry = {
145147 integrationID ?: Integration . ID
146148}
147149
150+ // MCP elicitations are Location-scoped, not Session-scoped: the server cannot attribute them to a
151+ // persisted session row, so their forms are owned by this opaque sentinel session identifier.
152+ const GLOBAL_ELICITATION_SESSION_ID = "global"
153+
148154export interface Interface {
149155 readonly servers : ( ) => Effect . Effect < ServerInfo [ ] >
150156 readonly tools : ( ) => Effect . Effect < Tool [ ] >
@@ -175,6 +181,7 @@ export const layer = Layer.effect(
175181 const config = yield * Config . Service
176182 const location = yield * Location . Service
177183 const events = yield * EventV2 . Service
184+ const forms = yield * Form . Service
178185 const integration = yield * Integration . Service
179186 const credentials = yield * Credential . Service
180187 const root = yield * Scope . make ( )
@@ -189,6 +196,7 @@ export const layer = Layer.effect(
189196 )
190197 // Later config files win for duplicate server names; per-server timeout overrides globals.
191198 const runtime = new Map < ServerName , ServerEntry > ( )
199+ const urlElicitations = new Map < string , Form . ID > ( )
192200 for ( const entry of documents ) {
193201 for ( const [ name , server ] of Object . entries ( entry . info . mcp ?. servers ?? { } ) ) {
194202 runtime . set ( ServerName . make ( name ) , {
@@ -308,6 +316,76 @@ export const layer = Layer.effect(
308316 } )
309317 } )
310318
319+ const elicitation = {
320+ create : ( input : {
321+ readonly server : string
322+ readonly params : MCPClient . ElicitationParams
323+ readonly signal : AbortSignal
324+ } ) =>
325+ Effect . gen ( function * ( ) {
326+ if ( input . params . mode === "url" ) {
327+ const formID = Form . ID . create ( )
328+ const key = input . server + "\u0000" + input . params . elicitationId
329+ urlElicitations . set ( key , formID )
330+ return yield * forms
331+ . ask ( {
332+ id : formID ,
333+ sessionID : GLOBAL_ELICITATION_SESSION_ID ,
334+ title : `${ input . server } is requesting input` ,
335+ metadata : {
336+ kind : "mcp-elicitation" ,
337+ server : input . server ,
338+ elicitationID : input . params . elicitationId ,
339+ message : input . params . message ,
340+ } ,
341+ mode : "url" ,
342+ url : input . params . url ,
343+ } )
344+ . pipe (
345+ Effect . raceFirst ( waitForAbort ( input . signal ) ) ,
346+ Effect . ensuring ( Effect . sync ( ( ) => urlElicitations . delete ( key ) ) ) ,
347+ Effect . map (
348+ ( state ) : MCPClient . ElicitationResult => ( {
349+ action : state . status === "answered" ? "accept" : "cancel" ,
350+ } ) ,
351+ ) ,
352+ )
353+ }
354+ const params = input . params
355+ return yield * forms
356+ . ask ( {
357+ sessionID : GLOBAL_ELICITATION_SESSION_ID ,
358+ title : `${ input . server } is requesting input` ,
359+ metadata : { kind : "mcp-elicitation" , server : input . server , message : params . message } ,
360+ mode : "form" ,
361+ fields : Object . entries ( params . requestedSchema . properties ) . map ( ( [ key , property ] ) =>
362+ toElicitationField ( key , property , params . requestedSchema . required ?. includes ( key ) === true ) ,
363+ ) ,
364+ } )
365+ . pipe (
366+ Effect . raceFirst ( waitForAbort ( input . signal ) ) ,
367+ Effect . map ( ( state ) : MCPClient . ElicitationResult => {
368+ if ( state . status !== "answered" ) return { action : "cancel" }
369+ return {
370+ action : "accept" ,
371+ content : Object . fromEntries (
372+ Object . entries ( state . answer ) . map (
373+ ( [ key , value ] ) : [ string , NonNullable < MCPClient . ElicitationResult [ "content" ] > [ string ] ] =>
374+ typeof value === "object" ? [ key , Array . from ( value ) ] : [ key , value ] ,
375+ ) ,
376+ ) ,
377+ }
378+ } ) ,
379+ )
380+ } ) ,
381+ complete : ( input : { readonly server : string ; readonly elicitationID : string } ) =>
382+ Effect . gen ( function * ( ) {
383+ const formID = urlElicitations . get ( input . server + "\u0000" + input . elicitationID )
384+ if ( ! formID ) return
385+ yield * forms . reply ( { id : formID , answer : { } } ) . pipe ( Effect . ignore )
386+ } ) ,
387+ } satisfies MCPClient . ElicitationHandler
388+
311389 const toTool = ( server : ServerName , def : MCPClient . ToolDefinition ) =>
312390 new Tool ( { server, name : def . name , description : def . description , inputSchema : def . inputSchema } )
313391
@@ -396,7 +474,7 @@ export const layer = Layer.effect(
396474 const authProvider = yield * connectProvider ( entry )
397475 // List tools as part of connect so a failure here marks the server failed rather than
398476 // leaving it connected with a silently empty tool list and no path to recover.
399- const result = yield * MCPClient . connect ( name , entry . config , location . directory , authProvider ) . pipe (
477+ const result = yield * MCPClient . connect ( name , entry . config , location . directory , authProvider , elicitation ) . pipe (
400478 Effect . flatMap ( ( connection ) => connection . tools ( ) . pipe ( Effect . map ( ( tools ) => ( { connection, tools } ) ) ) ) ,
401479 Scope . provide ( scope ) ,
402480 Effect . exit ,
@@ -561,5 +639,69 @@ export const layer = Layer.effect(
561639export const node = makeLocationNode ( {
562640 service : Service ,
563641 layer,
564- deps : [ Config . node , Location . node , EventV2 . node , Integration . node , Credential . node ] ,
642+ deps : [ Config . node , Location . node , EventV2 . node , Form . node , Integration . node , Credential . node ] ,
565643} )
644+
645+ // Schema `optional` strips undefined-valued properties on encode, so fields can assign
646+ // optional properties directly instead of conditionally spreading them.
647+ function toElicitationField ( key : string , property : ElicitationProperty , required : boolean ) : Form . Field {
648+ // Some servers emit machine titles like "string with format email"; prefer description/key over those.
649+ const machineTitle = / ^ ( b o o l e a n | s t r i n g | n u m b e r | i n t e g e r | a r r a y | o b j e c t ) ( \s + w i t h \b .* | \s + i n \b .* ) ? $ / i
650+ const title =
651+ property . title && ! machineTitle . test ( property . title . trim ( ) ) ? property . title : ( property . description ?? key )
652+ const base = {
653+ key,
654+ title,
655+ description : property . description === title ? undefined : property . description ,
656+ required : required || undefined ,
657+ }
658+ switch ( property . type ) {
659+ case "boolean" :
660+ return { ...base , type : "boolean" , default : property . default }
661+ case "number" :
662+ case "integer" :
663+ return {
664+ ...base ,
665+ type : property . type ,
666+ minimum : property . minimum ,
667+ maximum : property . maximum ,
668+ default : property . default ,
669+ }
670+ case "array" :
671+ return {
672+ ...base ,
673+ type : "multiselect" ,
674+ options :
675+ "anyOf" in property . items
676+ ? property . items . anyOf . map ( ( option ) => ( { value : option . const , label : option . title } ) )
677+ : property . items . enum . map ( ( value ) => ( { value, label : value } ) ) ,
678+ custom : false ,
679+ minItems : property . minItems ,
680+ maxItems : property . maxItems ,
681+ default : property . default ,
682+ }
683+ case "string" : {
684+ const options =
685+ "oneOf" in property
686+ ? property . oneOf . map ( ( option ) => ( { value : option . const , label : option . title } ) )
687+ : "enum" in property
688+ ? property . enum . map ( ( value , index ) => ( {
689+ value,
690+ label : ( "enumNames" in property ? property . enumNames ?. [ index ] : undefined ) ?? value ,
691+ } ) )
692+ : undefined
693+ return {
694+ ...base ,
695+ type : "string" ,
696+ format : "format" in property ? property . format : undefined ,
697+ minLength : "minLength" in property ? property . minLength : undefined ,
698+ maxLength : "maxLength" in property ? property . maxLength : undefined ,
699+ default : property . default ,
700+ options,
701+ custom : options ? false : undefined ,
702+ }
703+ }
704+ }
705+ }
706+
707+ type ElicitationProperty = MCPClient . ElicitationFormParams [ "requestedSchema" ] [ "properties" ] [ string ]
0 commit comments