@@ -1924,6 +1924,66 @@ describe('createMessage validation', () => {
19241924 } ) ;
19251925} ) ;
19261926
1927+ describe ( 'createMessage backwards compatibility' , ( ) => {
1928+ test ( 'createMessage without tools returns single content (backwards compat)' , async ( ) => {
1929+ const server = new Server ( { name : 'test server' , version : '1.0' } , { capabilities : { } } ) ;
1930+
1931+ const client = new Client ( { name : 'test client' , version : '1.0' } , { capabilities : { sampling : { } } } ) ;
1932+
1933+ // Mock client returns single text content
1934+ client . setRequestHandler ( CreateMessageRequestSchema , async ( ) => ( {
1935+ model : 'test-model' ,
1936+ role : 'assistant' ,
1937+ content : { type : 'text' , text : 'Hello from LLM' }
1938+ } ) ) ;
1939+
1940+ const [ clientTransport , serverTransport ] = InMemoryTransport . createLinkedPair ( ) ;
1941+ await Promise . all ( [ client . connect ( clientTransport ) , server . connect ( serverTransport ) ] ) ;
1942+
1943+ // Call createMessage WITHOUT tools
1944+ const result = await server . createMessage ( {
1945+ messages : [ { role : 'user' , content : { type : 'text' , text : 'hello' } } ] ,
1946+ maxTokens : 100
1947+ } ) ;
1948+
1949+ // Backwards compat: result.content should be single (not array)
1950+ expect ( result . model ) . toBe ( 'test-model' ) ;
1951+ expect ( Array . isArray ( result . content ) ) . toBe ( false ) ;
1952+ expect ( result . content . type ) . toBe ( 'text' ) ;
1953+ if ( result . content . type === 'text' ) {
1954+ expect ( result . content . text ) . toBe ( 'Hello from LLM' ) ;
1955+ }
1956+ } ) ;
1957+
1958+ test ( 'createMessage with tools accepts request and returns result' , async ( ) => {
1959+ const server = new Server ( { name : 'test server' , version : '1.0' } , { capabilities : { } } ) ;
1960+
1961+ const client = new Client ( { name : 'test client' , version : '1.0' } , { capabilities : { sampling : { tools : { } } } } ) ;
1962+
1963+ // Mock client returns text content (tool_use schema validation is tested in types.test.ts)
1964+ client . setRequestHandler ( CreateMessageRequestSchema , async ( ) => ( {
1965+ model : 'test-model' ,
1966+ role : 'assistant' ,
1967+ content : { type : 'text' , text : 'I will use the weather tool' } ,
1968+ stopReason : 'endTurn'
1969+ } ) ) ;
1970+
1971+ const [ clientTransport , serverTransport ] = InMemoryTransport . createLinkedPair ( ) ;
1972+ await Promise . all ( [ client . connect ( clientTransport ) , server . connect ( serverTransport ) ] ) ;
1973+
1974+ // Call createMessage WITH tools - verifies the overload works
1975+ const result = await server . createMessage ( {
1976+ messages : [ { role : 'user' , content : { type : 'text' , text : 'hello' } } ] ,
1977+ maxTokens : 100 ,
1978+ tools : [ { name : 'get_weather' , inputSchema : { type : 'object' } } ]
1979+ } ) ;
1980+
1981+ // Verify result is returned correctly
1982+ expect ( result . model ) . toBe ( 'test-model' ) ;
1983+ // With tools param, CreateMessageResultWithToolsSchema is used (types.test.ts verifies it accepts tool_use)
1984+ } ) ;
1985+ } ) ;
1986+
19271987test ( 'should respect log level for transport with sessionId' , async ( ) => {
19281988 const server = new Server (
19291989 {
0 commit comments