@@ -11,7 +11,8 @@ import {
11
11
GetPromptResultSchema ,
12
12
ListResourcesRequest ,
13
13
ListResourcesResultSchema ,
14
- LoggingMessageNotificationSchema
14
+ LoggingMessageNotificationSchema ,
15
+ ResourceListChangedNotificationSchema
15
16
} from '../../types.js' ;
16
17
17
18
async function main ( ) : Promise < void > {
@@ -24,50 +25,79 @@ async function main(): Promise<void> {
24
25
const transport = new StreamableHTTPClientTransport (
25
26
new URL ( 'http://localhost:3000/mcp' )
26
27
) ;
28
+ const supportsStandaloneSse = false ;
27
29
28
30
// Connect the client using the transport and initialize the server
29
31
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
30
45
client . setNotificationHandler ( LoggingMessageNotificationSchema , ( notification ) => {
31
46
console . log ( `Notification received: ${ notification . params . level } - ${ notification . params . data } ` ) ;
32
47
} ) ;
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
+ } ) ;
33
57
34
-
35
- console . log ( 'Connected to MCP server' ) ;
36
58
// 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 ) ;
43
66
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 ) ;
54
80
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
+ } ) ;
62
97
}
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
+ }
71
101
72
102
// List available prompts
73
103
try {
@@ -107,9 +137,11 @@ async function main(): Promise<void> {
107
137
} catch ( error ) {
108
138
console . log ( `Resources not supported by this server (${ error } )` ) ;
109
139
}
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
+ }
110
144
111
- // Close the connection
112
- await client . close ( ) ;
113
145
}
114
146
115
147
main ( ) . catch ( ( error : unknown ) => {
0 commit comments