@@ -11,7 +11,8 @@ import {
1111 GetPromptResultSchema ,
1212 ListResourcesRequest ,
1313 ListResourcesResultSchema ,
14- LoggingMessageNotificationSchema
14+ LoggingMessageNotificationSchema ,
15+ ResourceListChangedNotificationSchema
1516} from '../../types.js' ;
1617
1718async function main ( ) : Promise < void > {
@@ -24,50 +25,79 @@ async function main(): Promise<void> {
2425 const transport = new StreamableHTTPClientTransport (
2526 new URL ( 'http://localhost:3000/mcp' )
2627 ) ;
28+ const supportsStandaloneSse = false ;
2729
2830 // Connect the client using the transport and initialize the server
2931 await client . connect ( transport ) ;
32+ console . log ( 'Connected to MCP server' ) ;
33+ // Open a standalone SSE stream to receive server-initiated messages
34+ console . log ( 'Opening SSE stream to receive server notifications...' ) ;
35+ try {
36+ await transport . openSseStream ( ) ;
37+ const supportsStandaloneSse = false ;
38+ console . log ( 'SSE stream established successfully. Waiting for notifications...' ) ;
39+ }
40+ catch ( error ) {
41+ console . error ( 'Failed to open SSE stream:' , error ) ;
42+ }
43+
44+ // Set up notification handlers for server-initiated messages
3045 client . setNotificationHandler ( LoggingMessageNotificationSchema , ( notification ) => {
3146 console . log ( `Notification received: ${ notification . params . level } - ${ notification . params . data } ` ) ;
3247 } ) ;
48+ client . setNotificationHandler ( ResourceListChangedNotificationSchema , async ( _ ) => {
49+ console . log ( `Resource list changed notification received!` ) ;
50+ const resourcesRequest : ListResourcesRequest = {
51+ method : 'resources/list' ,
52+ params : { }
53+ } ;
54+ const resourcesResult = await client . request ( resourcesRequest , ListResourcesResultSchema ) ;
55+ console . log ( 'Available resources count:' , resourcesResult . resources . length ) ;
56+ } ) ;
3357
34-
35- console . log ( 'Connected to MCP server' ) ;
3658 // List available tools
37- const toolsRequest : ListToolsRequest = {
38- method : 'tools/list' ,
39- params : { }
40- } ;
41- const toolsResult = await client . request ( toolsRequest , ListToolsResultSchema ) ;
42- console . log ( 'Available tools:' , toolsResult . tools ) ;
59+ try {
60+ const toolsRequest : ListToolsRequest = {
61+ method : 'tools/list' ,
62+ params : { }
63+ } ;
64+ const toolsResult = await client . request ( toolsRequest , ListToolsResultSchema ) ;
65+ console . log ( 'Available tools:' , toolsResult . tools ) ;
4366
44- // Call the 'greet' tool
45- const greetRequest : CallToolRequest = {
46- method : 'tools/call' ,
47- params : {
48- name : 'greet' ,
49- arguments : { name : 'MCP User' }
50- }
51- } ;
52- const greetResult = await client . request ( greetRequest , CallToolResultSchema ) ;
53- console . log ( 'Greeting result:' , greetResult . content [ 0 ] . text ) ;
67+ if ( toolsResult . tools . length === 0 ) {
68+ console . log ( 'No tools available from the server' ) ;
69+ } else {
70+ // Call the 'greet' tool
71+ const greetRequest : CallToolRequest = {
72+ method : 'tools/call' ,
73+ params : {
74+ name : 'greet' ,
75+ arguments : { name : 'MCP User' }
76+ }
77+ } ;
78+ const greetResult = await client . request ( greetRequest , CallToolResultSchema ) ;
79+ console . log ( 'Greeting result:' , greetResult . content [ 0 ] . text ) ;
5480
55- // Call the new 'multi-greet' tool
56- console . log ( '\nCalling multi-greet tool (with notifications)...' ) ;
57- const multiGreetRequest : CallToolRequest = {
58- method : 'tools/call' ,
59- params : {
60- name : 'multi-greet' ,
61- arguments : { name : 'MCP User' }
81+ // Call the new 'multi-greet' tool
82+ console . log ( '\nCalling multi-greet tool (with notifications)...' ) ;
83+ const multiGreetRequest : CallToolRequest = {
84+ method : 'tools/call' ,
85+ params : {
86+ name : 'multi-greet' ,
87+ arguments : { name : 'MCP User' }
88+ }
89+ } ;
90+ const multiGreetResult = await client . request ( multiGreetRequest , CallToolResultSchema ) ;
91+ console . log ( 'Multi-greet results:' ) ;
92+ multiGreetResult . content . forEach ( item => {
93+ if ( item . type === 'text' ) {
94+ console . log ( `- ${ item . text } ` ) ;
95+ }
96+ } ) ;
6297 }
63- } ;
64- const multiGreetResult = await client . request ( multiGreetRequest , CallToolResultSchema ) ;
65- console . log ( 'Multi-greet results:' ) ;
66- multiGreetResult . content . forEach ( item => {
67- if ( item . type === 'text' ) {
68- console . log ( `- ${ item . text } ` ) ;
69- }
70- } ) ;
98+ } catch ( error ) {
99+ console . log ( `Tools not supported by this server (${ error } )` ) ;
100+ }
71101
72102 // List available prompts
73103 try {
@@ -107,9 +137,11 @@ async function main(): Promise<void> {
107137 } catch ( error ) {
108138 console . log ( `Resources not supported by this server (${ error } )` ) ;
109139 }
140+ if ( supportsStandaloneSse ) {
141+ // Instead of closing immediately, keep the connection open to receive notifications
142+ console . log ( '\nKeeping connection open to receive notifications. Press Ctrl+C to exit.' ) ;
143+ }
110144
111- // Close the connection
112- await client . close ( ) ;
113145}
114146
115147main ( ) . catch ( ( error : unknown ) => {
0 commit comments