@@ -12,7 +12,7 @@ import {
12
12
ListResourcesRequest ,
13
13
ListResourcesResultSchema ,
14
14
LoggingMessageNotificationSchema ,
15
- ResourceListChangedNotificationSchema
15
+ ResourceListChangedNotificationSchema ,
16
16
} from '../../types.js' ;
17
17
18
18
async function main ( ) : Promise < void > {
@@ -25,7 +25,7 @@ async function main(): Promise<void> {
25
25
const transport = new StreamableHTTPClientTransport (
26
26
new URL ( 'http://localhost:3000/mcp' )
27
27
) ;
28
-
28
+
29
29
// Connect the client using the transport and initialize the server
30
30
await client . connect ( transport ) ;
31
31
console . log ( 'Connected to MCP server' ) ;
@@ -44,49 +44,12 @@ async function main(): Promise<void> {
44
44
console . log ( 'Available resources count:' , resourcesResult . resources . length ) ;
45
45
} ) ;
46
46
47
- // List available tools
48
- try {
49
- const toolsRequest : ListToolsRequest = {
50
- method : 'tools/list' ,
51
- params : { }
52
- } ;
53
- const toolsResult = await client . request ( toolsRequest , ListToolsResultSchema ) ;
54
- console . log ( 'Available tools:' , toolsResult . tools ) ;
47
+ // List and call tools
48
+ await listTools ( client ) ;
55
49
56
- if ( toolsResult . tools . length === 0 ) {
57
- console . log ( 'No tools available from the server' ) ;
58
- } else {
59
- // Call the 'greet' tool
60
- const greetRequest : CallToolRequest = {
61
- method : 'tools/call' ,
62
- params : {
63
- name : 'greet' ,
64
- arguments : { name : 'MCP User' }
65
- }
66
- } ;
67
- const greetResult = await client . request ( greetRequest , CallToolResultSchema ) ;
68
- console . log ( 'Greeting result:' , greetResult . content [ 0 ] . text ) ;
50
+ await callGreetTool ( client ) ;
51
+ await callMultiGreetTool ( client ) ;
69
52
70
- // Call the new 'multi-greet' tool
71
- console . log ( '\nCalling multi-greet tool (with notifications)...' ) ;
72
- const multiGreetRequest : CallToolRequest = {
73
- method : 'tools/call' ,
74
- params : {
75
- name : 'multi-greet' ,
76
- arguments : { name : 'MCP User' }
77
- }
78
- } ;
79
- const multiGreetResult = await client . request ( multiGreetRequest , CallToolResultSchema ) ;
80
- console . log ( 'Multi-greet results:' ) ;
81
- multiGreetResult . content . forEach ( item => {
82
- if ( item . type === 'text' ) {
83
- console . log ( `- ${ item . text } ` ) ;
84
- }
85
- } ) ;
86
- }
87
- } catch ( error ) {
88
- console . log ( `Tools not supported by this server (${ error } )` ) ;
89
- }
90
53
91
54
// List available prompts
92
55
try {
@@ -130,6 +93,61 @@ async function main(): Promise<void> {
130
93
console . log ( '\nKeeping connection open to receive notifications. Press Ctrl+C to exit.' ) ;
131
94
}
132
95
96
+ async function listTools ( client : Client ) : Promise < void > {
97
+ try {
98
+ const toolsRequest : ListToolsRequest = {
99
+ method : 'tools/list' ,
100
+ params : { }
101
+ } ;
102
+ const toolsResult = await client . request ( toolsRequest , ListToolsResultSchema ) ;
103
+ console . log ( 'Available tools:' , toolsResult . tools ) ;
104
+ if ( toolsResult . tools . length === 0 ) {
105
+ console . log ( 'No tools available from the server' ) ;
106
+ }
107
+ } catch ( error ) {
108
+ console . log ( `Tools not supported by this server (${ error } )` ) ;
109
+ return
110
+ }
111
+ }
112
+
113
+ async function callGreetTool ( client : Client ) : Promise < void > {
114
+ try {
115
+ const greetRequest : CallToolRequest = {
116
+ method : 'tools/call' ,
117
+ params : {
118
+ name : 'greet' ,
119
+ arguments : { name : 'MCP User' }
120
+ }
121
+ } ;
122
+ const greetResult = await client . request ( greetRequest , CallToolResultSchema ) ;
123
+ console . log ( 'Greeting result:' , greetResult . content [ 0 ] . text ) ;
124
+ } catch ( error ) {
125
+ console . log ( `Error calling greet tool: ${ error } ` ) ;
126
+ }
127
+ }
128
+
129
+ async function callMultiGreetTool ( client : Client ) : Promise < void > {
130
+ try {
131
+ console . log ( '\nCalling multi-greet tool (with notifications)...' ) ;
132
+ const multiGreetRequest : CallToolRequest = {
133
+ method : 'tools/call' ,
134
+ params : {
135
+ name : 'multi-greet' ,
136
+ arguments : { name : 'MCP User' }
137
+ }
138
+ } ;
139
+ const multiGreetResult = await client . request ( multiGreetRequest , CallToolResultSchema ) ;
140
+ console . log ( 'Multi-greet results:' ) ;
141
+ multiGreetResult . content . forEach ( item => {
142
+ if ( item . type === 'text' ) {
143
+ console . log ( `- ${ item . text } ` ) ;
144
+ }
145
+ } ) ;
146
+ } catch ( error ) {
147
+ console . log ( `Error calling multi-greet tool: ${ error } ` ) ;
148
+ }
149
+ }
150
+
133
151
main ( ) . catch ( ( error : unknown ) => {
134
152
console . error ( 'Error running MCP client:' , error ) ;
135
153
process . exit ( 1 ) ;
0 commit comments