@@ -217,11 +217,11 @@ test("should connect new client to old, supported server version", async () => {
217
217
const [ clientTransport , serverTransport ] =
218
218
InMemoryTransport . createLinkedPair ( ) ;
219
219
220
+ // Client initialization automatically uses LATEST_PROTOCOL_VERSION in the current SDK
220
221
const client = new Client (
221
222
{
222
223
name : "new client" ,
223
224
version : "1.0" ,
224
- protocolVersion : LATEST_PROTOCOL_VERSION ,
225
225
} ,
226
226
{
227
227
capabilities : {
@@ -287,7 +287,6 @@ test("should negotiate version when client is old, and newer server supports its
287
287
{
288
288
name : "old client" ,
289
289
version : "1.0" ,
290
- protocolVersion : OLD_VERSION ,
291
290
} ,
292
291
{
293
292
capabilities : {
@@ -297,6 +296,17 @@ test("should negotiate version when client is old, and newer server supports its
297
296
} ,
298
297
) ;
299
298
299
+ // Mock the request method to simulate an old client sending OLD_VERSION
300
+ const originalRequest = client . request . bind ( client ) ;
301
+ client . request = jest . fn ( async ( request , schema , options ) => {
302
+ // If this is the initialize request, modify the protocol version to simulate old client
303
+ if ( request . method === "initialize" && request . params ) {
304
+ request . params . protocolVersion = OLD_VERSION ;
305
+ }
306
+ // Call the original request method with the potentially modified request
307
+ return originalRequest ( request , schema , options ) ;
308
+ } ) ;
309
+
300
310
await Promise . all ( [
301
311
client . connect ( clientTransport ) ,
302
312
server . connect ( serverTransport ) ,
@@ -350,11 +360,13 @@ test("should throw when client is old, and server doesn't support its version",
350
360
const [ clientTransport , serverTransport ] =
351
361
InMemoryTransport . createLinkedPair ( ) ;
352
362
363
+ // Client uses LATEST_PROTOCOL_VERSION by default, which is sufficient for this test
364
+ // The error occurs because the server returns FUTURE_VERSION (unsupported),
365
+ // not because of the client's version. Any client version would fail here.
353
366
const client = new Client (
354
367
{
355
368
name : "old client" ,
356
369
version : "1.0" ,
357
- protocolVersion : OLD_VERSION ,
358
370
} ,
359
371
{
360
372
capabilities : {
@@ -880,6 +892,7 @@ describe('outputSchema validation', () => {
880
892
server . setRequestHandler ( CallToolRequestSchema , async ( request ) => {
881
893
if ( request . params . name === 'test-tool' ) {
882
894
return {
895
+ content : [ ] , // Required field for CallToolResult
883
896
structuredContent : { result : 'success' , count : 42 } ,
884
897
} ;
885
898
}
@@ -903,7 +916,11 @@ describe('outputSchema validation', () => {
903
916
904
917
// Call the tool - should validate successfully
905
918
const result = await client . callTool ( { name : 'test-tool' } ) ;
906
- expect ( result . structuredContent ) . toEqual ( { result : 'success' , count : 42 } ) ;
919
+ // Type narrowing: check if structuredContent exists before accessing
920
+ expect ( 'structuredContent' in result ) . toBe ( true ) ;
921
+ if ( 'structuredContent' in result ) {
922
+ expect ( result . structuredContent ) . toEqual ( { result : 'success' , count : 42 } ) ;
923
+ }
907
924
} ) ;
908
925
909
926
/***
@@ -955,6 +972,7 @@ describe('outputSchema validation', () => {
955
972
if ( request . params . name === 'test-tool' ) {
956
973
// Return invalid structured content (count is string instead of number)
957
974
return {
975
+ content : [ ] , // Required field for CallToolResult
958
976
structuredContent : { result : 'success' , count : 'not a number' } ,
959
977
} ;
960
978
}
@@ -1120,7 +1138,11 @@ describe('outputSchema validation', () => {
1120
1138
1121
1139
// Call the tool - should work normally without validation
1122
1140
const result = await client . callTool ( { name : 'test-tool' } ) ;
1123
- expect ( result . content ) . toEqual ( [ { type : 'text' , text : 'Normal response' } ] ) ;
1141
+ // Type narrowing: check if content exists before accessing
1142
+ expect ( 'content' in result ) . toBe ( true ) ;
1143
+ if ( 'content' in result ) {
1144
+ expect ( result . content ) . toEqual ( [ { type : 'text' , text : 'Normal response' } ] ) ;
1145
+ }
1124
1146
} ) ;
1125
1147
1126
1148
/***
@@ -1184,6 +1206,7 @@ describe('outputSchema validation', () => {
1184
1206
server . setRequestHandler ( CallToolRequestSchema , async ( request ) => {
1185
1207
if ( request . params . name === 'complex-tool' ) {
1186
1208
return {
1209
+ content : [ ] , // Required field for CallToolResult
1187
1210
structuredContent : {
1188
1211
name : 'John Doe' ,
1189
1212
age : 30 ,
@@ -1215,10 +1238,14 @@ describe('outputSchema validation', () => {
1215
1238
1216
1239
// Call the tool - should validate successfully
1217
1240
const result = await client . callTool ( { name : 'complex-tool' } ) ;
1218
- expect ( result . structuredContent ) . toBeDefined ( ) ;
1219
- const structuredContent = result . structuredContent as { name : string ; age : number } ;
1220
- expect ( structuredContent . name ) . toBe ( 'John Doe' ) ;
1221
- expect ( structuredContent . age ) . toBe ( 30 ) ;
1241
+ // Type narrowing: check if structuredContent exists before accessing
1242
+ expect ( 'structuredContent' in result ) . toBe ( true ) ;
1243
+ if ( 'structuredContent' in result ) {
1244
+ expect ( result . structuredContent ) . toBeDefined ( ) ;
1245
+ const structuredContent = result . structuredContent as { name : string ; age : number } ;
1246
+ expect ( structuredContent . name ) . toBe ( 'John Doe' ) ;
1247
+ expect ( structuredContent . age ) . toBe ( 30 ) ;
1248
+ }
1222
1249
} ) ;
1223
1250
1224
1251
/***
@@ -1269,6 +1296,7 @@ describe('outputSchema validation', () => {
1269
1296
if ( request . params . name === 'strict-tool' ) {
1270
1297
// Return structured content with extra property
1271
1298
return {
1299
+ content : [ ] , // Required field for CallToolResult
1272
1300
structuredContent : {
1273
1301
name : 'John' ,
1274
1302
extraField : 'not allowed' ,
0 commit comments