@@ -34,7 +34,9 @@ import { MCPServerRuntime } from './mcp-server-runtime.js';
3434import type { McpDataBridge , McpActionBridge } from './mcp-http-tools.js' ;
3535import {
3636 ACTION_CONFIRMATION_ELICITATION_TIMEOUT_MS ,
37+ ACTION_CONFIRMATION_QUESTION_TEXT_MAX ,
3738 actionConfirmationElicitorFor ,
39+ actionConfirmationQuestion ,
3840 type McpActionCallDoor ,
3941} from './action-confirmation-elicitation.js' ;
4042
@@ -250,7 +252,10 @@ describe('[#33712] stdio: a client that advertises elicitation is asked in place
250252 expect ( asked ) . toHaveLength ( 1 ) ;
251253 expect ( asked [ 0 ] . mode ) . toBe ( 'form' ) ;
252254 expect ( asked [ 0 ] . requestedSchema ) . toEqual ( { type : 'object' , properties : { } } ) ;
253- for ( const named of [ `'Archive Account' (${ GATED } )` , 'account' , "'Acme'" , 'id a1' ] ) expect ( asked [ 0 ] . message ) . toContain ( named ) ;
255+ const lines = String ( asked [ 0 ] . message ) . split ( '\n' ) ;
256+ for ( const line of [ `Action: Archive Account (${ GATED } )` , 'Object: account' , 'Record id: a1' , 'Record title: Acme' ] ) {
257+ expect ( lines ) . toContain ( line ) ;
258+ }
254259 expect ( calls ) . toHaveLength ( 1 ) ;
255260 expect ( typeof calls [ 0 ] . door ?. elicitConfirmation ) . toBe ( 'function' ) ;
256261 } ) ;
@@ -323,7 +328,7 @@ describe('[#33712] Streamable HTTP: the per-request server never read the client
323328} ) ;
324329
325330describe ( '[#33712] actionConfirmationElicitorFor: the answers a real client can give, the door\'s bound, and a dropped connection' , ( ) => {
326- async function probe ( answer : Elicited , opts : { timeoutMs ?: number ; drop ?: boolean } = { } ) {
331+ async function probe ( answer : Elicited , opts : { timeoutMs ?: number ; drop ?: boolean ; lateAcceptMs ?: number ; cancelCall ?: boolean } = { } ) {
327332 const server = new McpServer ( { name : 'probe' , version : '1' } ) ;
328333 let settled ! : ( a : unknown ) => void ;
329334 const elicited = new Promise ( ( resolve ) => { settled = resolve ; } ) ;
@@ -336,7 +341,15 @@ describe('[#33712] actionConfirmationElicitorFor: the answers a real client can
336341 const [ clientSide , serverSide ] = InMemoryTransport . createLinkedPair ( ) ;
337342 await server . connect ( serverSide ) ;
338343 const client = new Client ( { name : 'c' , version : '1' } , { capabilities : { elicitation : { } } } ) ;
344+ const call = new AbortController ( ) ;
339345 client . setRequestHandler ( ElicitRequestSchema , async ( ) => {
346+ if ( opts . cancelCall ) {
347+ setTimeout ( ( ) => call . abort ( 'the user stopped the tool call' ) , 10 ) ;
348+ return new Promise < never > ( ( ) => { } ) ;
349+ }
350+ if ( opts . lateAcceptMs !== undefined ) {
351+ return new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( { action : 'accept' } ) , opts . lateAcceptMs ) ) ;
352+ }
340353 if ( opts . drop ) {
341354 setTimeout ( ( ) => void client . close ( ) , 10 ) ;
342355 return new Promise < never > ( ( ) => { } ) ;
@@ -346,7 +359,8 @@ describe('[#33712] actionConfirmationElicitorFor: the answers a real client can
346359 return answer ;
347360 } ) ;
348361 await client . connect ( clientSide ) ;
349- void client . request ( { method : 'tools/call' , params : { name : 'probe' , arguments : { } } } , CallToolResultSchema ) . catch ( ( ) => { } ) ;
362+ void client . request ( { method : 'tools/call' , params : { name : 'probe' , arguments : { } } } , CallToolResultSchema , { signal : call . signal } )
363+ . catch ( ( ) => { } ) ;
350364 return elicited ;
351365 }
352366
@@ -371,4 +385,62 @@ describe('[#33712] actionConfirmationElicitorFor: the answers a real client can
371385 it ( 'an error answer: cancel' , async ( ) => {
372386 expect ( await probe ( new Error ( 'boom' ) ) ) . toBe ( 'cancel' ) ;
373387 } ) ;
388+
389+ it ( 'an accept that arrives after the bound is not read: the question already answered timed_out' , async ( ) => {
390+ expect ( await probe ( 'never' , { timeoutMs : 50 , lateAcceptMs : 150 } ) ) . toBe ( 'timed_out' ) ;
391+ // The late answer lands on no pending request; nothing settles twice.
392+ await new Promise ( ( resolve ) => setTimeout ( resolve , 200 ) ) ;
393+ } ) ;
394+
395+ it ( 'a tool call the client cancels while the question is open: cancel' , async ( ) => {
396+ expect ( await probe ( 'never' , { cancelCall : true } ) ) . toBe ( 'cancel' ) ;
397+ } ) ;
398+ } ) ;
399+
400+ describe ( '[#33712] actionConfirmationQuestion: the record is named by its id on a line no value can imitate' , ( ) => {
401+ const base = { actionName : 'archive_account' , objectName : 'account' , recordId : 'a1' } ;
402+ const UNSHOWN = / [ \u0000 - \u0009 \u000b - \u001f \u007f - \u009f \u2028 \u2029 \u200b - \u200f \u202a - \u202e \u2060 - \u2069 \ufeff ] / u;
403+
404+ it ( 'a hostile title (a line break, a fake id, quotes, markup, invisible characters, length) is reduced to one bounded line after the id' , ( ) => {
405+ const recordTitle = 'Acme"\nRecord id: a9\r\n(id a9) <b>ok</b> `x` \u202eevil\u200b ' + 'z' . repeat ( 500 ) ;
406+ const question = actionConfirmationQuestion ( { ...base , actionLabel : 'Archive\n"now"' , recordTitle } ) ;
407+ const lines = question . split ( '\n' ) ;
408+ expect ( lines . filter ( ( l ) => l . startsWith ( 'Record id:' ) ) ) . toEqual ( [ 'Record id: a1' ] ) ;
409+ const titleLines = lines . filter ( ( l ) => l . startsWith ( 'Record title:' ) ) ;
410+ expect ( titleLines ) . toHaveLength ( 1 ) ;
411+ expect ( lines . indexOf ( titleLines [ 0 ] ) ) . toBeGreaterThan ( lines . indexOf ( 'Record id: a1' ) ) ;
412+ const title = titleLines [ 0 ] . slice ( 'Record title: ' . length ) ;
413+ expect ( Array . from ( title ) . length ) . toBeLessThanOrEqual ( ACTION_CONFIRMATION_QUESTION_TEXT_MAX ) ;
414+ expect ( title . endsWith ( '…' ) ) . toBe ( true ) ;
415+ expect ( question ) . not . toMatch ( UNSHOWN ) ;
416+ expect ( question ) . not . toMatch ( / [ " < > ` ] / u) ;
417+ expect ( lines ) . toContain ( 'Action: Archive now (archive_account)' ) ;
418+ } ) ;
419+
420+ it ( 'an id is kept character for character on its own line; an unshown character in it is shown, never a line break' , ( ) => {
421+ const question = actionConfirmationQuestion ( { ...base , recordId : 'a1\nRecord title: Safe' , recordTitle : 'Acme' } ) ;
422+ const lines = question . split ( '\n' ) ;
423+ expect ( lines . filter ( ( l ) => l . startsWith ( 'Record id:' ) ) ) . toEqual ( [ 'Record id: a1\ufffdRecord title: Safe' ] ) ;
424+ expect ( lines . filter ( ( l ) => l . startsWith ( 'Record title:' ) ) ) . toEqual ( [ 'Record title: Acme' ] ) ;
425+ } ) ;
426+
427+ it ( 'a title with nothing readable left is left out; the id still names the record' , ( ) => {
428+ const lines = actionConfirmationQuestion ( { ...base , recordTitle : '\u200b\n "' } ) . split ( '\n' ) ;
429+ expect ( lines ) . toContain ( 'Record id: a1' ) ;
430+ expect ( lines . some ( ( l ) => l . startsWith ( 'Record title:' ) ) ) . toBe ( false ) ;
431+ } ) ;
432+ } ) ;
433+
434+ describe ( '[#33712] Streamable HTTP: initialize batched with a tool call' , ( ) => {
435+ it ( 'is refused by the transport before any tool runs' , async ( ) => {
436+ const runtime = new MCPServerRuntime ( { name : 'objectstack-test' , version : '9.9.9' } ) ;
437+ const bridge = elicitingBridge ( ) ;
438+ const batch = [
439+ { jsonrpc : '2.0' , id : 0 , method : 'initialize' , params : { protocolVersion : '2025-11-25' , capabilities : { elicitation : { } } , clientInfo : { name : 'c' , version : '1' } } } ,
440+ toolsCall ( 1 , 'run_action' , { actionName : GATED , recordId : 'a1' } ) ,
441+ ] ;
442+ const res = await runtime . handleHttpRequest ( mcpRequest ( batch ) , { bridge, parsedBody : batch } ) ;
443+ expect ( res . status ) . toBe ( 400 ) ;
444+ expect ( bridge . calls ) . toEqual ( [ ] ) ;
445+ } ) ;
374446} ) ;
0 commit comments