@@ -10,6 +10,10 @@ import {
10
10
LATEST_PROTOCOL_VERSION ,
11
11
SUPPORTED_PROTOCOL_VERSIONS ,
12
12
CreateMessageRequestSchema ,
13
+ ListPromptsRequestSchema ,
14
+ ListResourcesRequestSchema ,
15
+ ListToolsRequestSchema ,
16
+ SetLevelRequestSchema ,
13
17
} from "../types.js" ;
14
18
import { Transport } from "../shared/transport.js" ;
15
19
import { InMemoryTransport } from "../inMemory.js" ;
@@ -293,6 +297,41 @@ test("should respect server notification capabilities", async () => {
293
297
) . rejects . toThrow ( / ^ S e r v e r d o e s n o t s u p p o r t / ) ;
294
298
} ) ;
295
299
300
+ test ( "should only allow setRequestHandler for declared capabilities" , ( ) => {
301
+ const server = new Server (
302
+ {
303
+ name : "test server" ,
304
+ version : "1.0" ,
305
+ } ,
306
+ {
307
+ capabilities : {
308
+ prompts : { } ,
309
+ resources : { } ,
310
+ } ,
311
+ } ,
312
+ ) ;
313
+
314
+ // These should work because the capabilities are declared
315
+ expect ( ( ) => {
316
+ server . setRequestHandler ( ListPromptsRequestSchema , ( ) => ( { prompts : [ ] } ) ) ;
317
+ } ) . not . toThrow ( ) ;
318
+
319
+ expect ( ( ) => {
320
+ server . setRequestHandler ( ListResourcesRequestSchema , ( ) => ( {
321
+ resources : [ ] ,
322
+ } ) ) ;
323
+ } ) . not . toThrow ( ) ;
324
+
325
+ // These should throw because the capabilities are not declared
326
+ expect ( ( ) => {
327
+ server . setRequestHandler ( ListToolsRequestSchema , ( ) => ( { tools : [ ] } ) ) ;
328
+ } ) . toThrow ( / ^ S e r v e r d o e s n o t s u p p o r t t o o l s / ) ;
329
+
330
+ expect ( ( ) => {
331
+ server . setRequestHandler ( SetLevelRequestSchema , ( ) => ( { } ) ) ;
332
+ } ) . toThrow ( / ^ S e r v e r d o e s n o t s u p p o r t l o g g i n g / ) ;
333
+ } ) ;
334
+
296
335
/*
297
336
Test that custom request/notification/result schemas can be used with the Server class.
298
337
*/
0 commit comments