Skip to content

Commit 2c2cf5b

Browse files
committed
cleam up example client
1 parent 18e4981 commit 2c2cf5b

File tree

1 file changed

+61
-43
lines changed

1 file changed

+61
-43
lines changed

Diff for: src/examples/client/simpleStreamableHttp.ts

+61-43
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
ListResourcesRequest,
1313
ListResourcesResultSchema,
1414
LoggingMessageNotificationSchema,
15-
ResourceListChangedNotificationSchema
15+
ResourceListChangedNotificationSchema,
1616
} from '../../types.js';
1717

1818
async function main(): Promise<void> {
@@ -25,7 +25,7 @@ async function main(): Promise<void> {
2525
const transport = new StreamableHTTPClientTransport(
2626
new URL('http://localhost:3000/mcp')
2727
);
28-
28+
2929
// Connect the client using the transport and initialize the server
3030
await client.connect(transport);
3131
console.log('Connected to MCP server');
@@ -44,49 +44,12 @@ async function main(): Promise<void> {
4444
console.log('Available resources count:', resourcesResult.resources.length);
4545
});
4646

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);
5549

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);
6952

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-
}
9053

9154
// List available prompts
9255
try {
@@ -130,6 +93,61 @@ async function main(): Promise<void> {
13093
console.log('\nKeeping connection open to receive notifications. Press Ctrl+C to exit.');
13194
}
13295

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+
133151
main().catch((error: unknown) => {
134152
console.error('Error running MCP client:', error);
135153
process.exit(1);

0 commit comments

Comments
 (0)