@@ -4,6 +4,7 @@ import { describe, expect, test } from "bun:test"
44import { Client , InMemoryTransport , StreamableHTTPClientTransport } from "@modelcontextprotocol/client"
55import {
66 createMcpHandler ,
7+ type ElicitRequestFormParams ,
78 inputRequired ,
89 inputResponse ,
910 Server ,
@@ -95,6 +96,7 @@ function resourceServer(
9596 listChanged ?: boolean
9697 emptyElicitation ?: boolean
9798 urlElicitation ?: boolean
99+ formElicitation ?: boolean
98100 respond ?: ( request : Request ) => Response | undefined | Promise < Response | undefined >
99101 } = { } ,
100102) {
@@ -146,7 +148,9 @@ function resourceServer(
146148 ? [ { name : "empty-elicitation" , inputSchema : { type : "object" as const , properties : { } } } ]
147149 : input . urlElicitation
148150 ? [ { name : "url-elicitation" , inputSchema : { type : "object" as const , properties : { } } } ]
149- : [ { name : "echo" , inputSchema : { type : "object" as const , properties : { } } } ] ,
151+ : input . formElicitation
152+ ? [ { name : "form-elicitation" , inputSchema : { type : "object" as const , properties : { } } } ]
153+ : [ { name : "echo" , inputSchema : { type : "object" as const , properties : { } } } ] ,
150154 } )
151155 } )
152156 if ( input . emptyElicitation ) {
@@ -184,7 +188,51 @@ function resourceServer(
184188 }
185189 } )
186190 }
187- if ( ! input . emptyElicitation && ! input . urlElicitation ) {
191+ if ( input . formElicitation ) {
192+ // The primitive schema kinds the elicitation specification allows (client/elicitation#requested-schema):
193+ // string with constraints and a default, integer with bounds, a single-select enum, and a boolean.
194+ const requestedSchema = {
195+ type : "object" ,
196+ properties : {
197+ name : {
198+ type : "string" ,
199+ title : "Full name" ,
200+ description : "Your display name" ,
201+ minLength : 2 ,
202+ maxLength : 40 ,
203+ default : "Octocat" ,
204+ } ,
205+ age : { type : "integer" , title : "Age" , minimum : 0 , maximum : 130 } ,
206+ colour : { type : "string" , enum : [ "Red" , "Green" , "Blue" ] , default : "Green" } ,
207+ newsletter : { type : "boolean" , title : "Newsletter" , default : true } ,
208+ } ,
209+ required : [ "name" ] ,
210+ } satisfies ElicitRequestFormParams [ "requestedSchema" ]
211+ const message = "Tell us about yourself"
212+ // Modern servers cannot call elicitInput; they return input_required and the SDK client
213+ // retries the call with inputResponses (basic/patterns/mrtr).
214+ protocol . setRequestHandler ( "tools/call" , async ( request , ctx ) => {
215+ const responses = ctx . mcpReq . inputResponses
216+ if ( input . modern && ! responses ) {
217+ return inputRequired ( {
218+ inputRequests : { profile : inputRequired . elicit ( { message, requestedSchema } ) } ,
219+ } )
220+ }
221+ const result = input . modern
222+ ? ( ( ) => {
223+ const response = inputResponse ( responses , "profile" )
224+ return response . kind === "elicit"
225+ ? { action : response . action , content : response . content }
226+ : { action : "cancel" }
227+ } ) ( )
228+ : await protocol . elicitInput ( { mode : "form" , message, requestedSchema } )
229+ return {
230+ content : [ { type : "text" , text : JSON . stringify ( result ) } ] ,
231+ structuredContent : result ,
232+ }
233+ } )
234+ }
235+ if ( ! input . emptyElicitation && ! input . urlElicitation && ! input . formElicitation ) {
188236 protocol . setRequestHandler ( "tools/call" , ( request ) => {
189237 state . toolCalls . push ( {
190238 name : request . params . name ,
@@ -334,7 +382,10 @@ function resourceMcpLayer(
334382 } as Payload < typeof definition >
335383 overrides ?. published ?. push ( event . type )
336384 if ( event . type !== Form . Event . Created . type || ! onFormCreated ) return Effect . succeed ( event )
337- return onFormCreated ( Schema . decodeUnknownSync ( Form . Event . Created . data ) ( data ) . form ) . pipe ( Effect . as ( event ) )
385+ // Subscribers receive events after a JSON hop, where undefined-valued optional members drop.
386+ // Roundtrip so this mock decodes the same shape a real subscriber sees.
387+ const encoded = Schema . encodeUnknownSync ( Form . Event . Created . data ) ( data as { readonly form : Form . Info } )
388+ return onFormCreated ( Schema . decodeUnknownSync ( Form . Event . Created . data ) ( encoded ) . form ) . pipe ( Effect . as ( event ) )
338389 } ,
339390 } ) ,
340391 Layer . mock ( Integration . Service , {
@@ -1306,6 +1357,112 @@ test("settles modern MCP URL elicitations when the user confirms", async () => {
13061357 )
13071358} )
13081359
1360+ // Shared by the form elicitation tests below. The form service maps the requested schema onto these
1361+ // fields, preserving titles, constraints, enum options, and defaults (`toElicitationField`).
1362+ const profileFormFields : Form . Fields = [
1363+ {
1364+ key : "name" ,
1365+ title : "Full name" ,
1366+ description : "Your display name" ,
1367+ required : true ,
1368+ type : "string" ,
1369+ minLength : 2 ,
1370+ maxLength : 40 ,
1371+ default : "Octocat" ,
1372+ } ,
1373+ { key : "age" , title : "Age" , type : "integer" , minimum : 0 , maximum : 130 } ,
1374+ {
1375+ key : "colour" ,
1376+ title : "colour" ,
1377+ type : "string" ,
1378+ default : "Green" ,
1379+ options : [
1380+ { value : "Red" , label : "Red" } ,
1381+ { value : "Green" , label : "Green" } ,
1382+ { value : "Blue" , label : "Blue" } ,
1383+ ] ,
1384+ custom : false ,
1385+ } ,
1386+ { key : "newsletter" , title : "Newsletter" , type : "boolean" , default : true } ,
1387+ ]
1388+
1389+ test ( "answers a modern MCP form elicitation through the multi-round-trip flow" , async ( ) => {
1390+ await Effect . runPromise (
1391+ Effect . scoped (
1392+ Effect . gen ( function * ( ) {
1393+ const server = yield * resourceServer ( { modern : true , resources : false , formElicitation : true } )
1394+ const created = yield * Deferred . make < Form . Info > ( )
1395+ const result = yield * Effect . gen ( function * ( ) {
1396+ const service = yield * Mcp . Service
1397+ const forms = yield * Form . Service
1398+ const call = yield * service
1399+ . callTool ( { server : "resources" , name : "form-elicitation" } )
1400+ . pipe ( Effect . forkScoped )
1401+
1402+ const form = yield * Deferred . await ( created )
1403+ expect ( form . metadata ) . toEqual ( {
1404+ kind : "mcp-elicitation" ,
1405+ server : "resources" ,
1406+ message : "Tell us about yourself" ,
1407+ } )
1408+ expect ( form . fields ) . toEqual ( profileFormFields )
1409+
1410+ yield * forms . reply ( {
1411+ id : form . id ,
1412+ answer : { name : "Mona" , age : 30 , colour : "Blue" , newsletter : false } ,
1413+ } )
1414+ return yield * Fiber . join ( call )
1415+ } ) . pipe (
1416+ Effect . provide (
1417+ resourceMcpLayer (
1418+ new ConfigMCP . Remote ( { type : "remote" , url : server . url , oauth : false , protocol : "2026-07-28" } ) ,
1419+ ( form ) => Deferred . succeed ( created , form ) . pipe ( Effect . asVoid ) ,
1420+ ) ,
1421+ ) ,
1422+ )
1423+
1424+ expect ( result . structured ) . toEqual ( {
1425+ action : "accept" ,
1426+ content : { name : "Mona" , age : 30 , colour : "Blue" , newsletter : false } ,
1427+ } )
1428+ } ) ,
1429+ ) ,
1430+ )
1431+ } )
1432+
1433+ test ( "answers a legacy MCP form elicitation with fields" , async ( ) => {
1434+ await Effect . runPromise (
1435+ Effect . scoped (
1436+ Effect . gen ( function * ( ) {
1437+ const server = yield * resourceServer ( { resources : false , formElicitation : true } )
1438+ const created = yield * Deferred . make < Form . Info > ( )
1439+ const result = yield * Effect . gen ( function * ( ) {
1440+ const service = yield * Mcp . Service
1441+ const forms = yield * Form . Service
1442+ const call = yield * service
1443+ . callTool ( { server : "resources" , name : "form-elicitation" } )
1444+ . pipe ( Effect . forkScoped )
1445+
1446+ const form = yield * Deferred . await ( created )
1447+ expect ( form . fields ) . toEqual ( profileFormFields )
1448+ yield * forms . reply ( {
1449+ id : form . id ,
1450+ answer : { name : "Mona" , age : 30 , colour : "Blue" , newsletter : false } ,
1451+ } )
1452+ return yield * Fiber . join ( call )
1453+ } ) . pipe (
1454+ Effect . provide ( resourceMcpLayer ( server . url , ( form ) => Deferred . succeed ( created , form ) . pipe ( Effect . asVoid ) ) ) ,
1455+ )
1456+
1457+ expect ( result . structured ) . toEqual ( {
1458+ action : "accept" ,
1459+ content : { name : "Mona" , age : 30 , colour : "Blue" , newsletter : false } ,
1460+ } )
1461+ } ) ,
1462+ ) ,
1463+ )
1464+ } )
1465+
13091466test ( "loads and reads MCP resources" , async ( ) => {
13101467 await Effect . runPromise (
13111468 Effect . scoped (
0 commit comments