cleam up example client

This commit is contained in:
ihrpr 2025-04-11 14:15:43 +01:00
commit 2c2cf5b4a2
No known key found for this signature in database

View file

@ -12,7 +12,7 @@ import {
ListResourcesRequest,
ListResourcesResultSchema,
LoggingMessageNotificationSchema,
ResourceListChangedNotificationSchema
ResourceListChangedNotificationSchema,
} from '../../types.js';
async function main(): Promise<void> {
@ -25,7 +25,7 @@ async function main(): Promise<void> {
const transport = new StreamableHTTPClientTransport(
new URL('http://localhost:3000/mcp')
);
// Connect the client using the transport and initialize the server
await client.connect(transport);
console.log('Connected to MCP server');
@ -44,49 +44,12 @@ async function main(): Promise<void> {
console.log('Available resources count:', resourcesResult.resources.length);
});
// List available tools
try {
const toolsRequest: ListToolsRequest = {
method: 'tools/list',
params: {}
};
const toolsResult = await client.request(toolsRequest, ListToolsResultSchema);
console.log('Available tools:', toolsResult.tools);
// List and call tools
await listTools(client);
if (toolsResult.tools.length === 0) {
console.log('No tools available from the server');
} else {
// Call the 'greet' tool
const greetRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'greet',
arguments: { name: 'MCP User' }
}
};
const greetResult = await client.request(greetRequest, CallToolResultSchema);
console.log('Greeting result:', greetResult.content[0].text);
await callGreetTool(client);
await callMultiGreetTool(client);
// Call the new 'multi-greet' tool
console.log('\nCalling multi-greet tool (with notifications)...');
const multiGreetRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'multi-greet',
arguments: { name: 'MCP User' }
}
};
const multiGreetResult = await client.request(multiGreetRequest, CallToolResultSchema);
console.log('Multi-greet results:');
multiGreetResult.content.forEach(item => {
if (item.type === 'text') {
console.log(`- ${item.text}`);
}
});
}
} catch (error) {
console.log(`Tools not supported by this server (${error})`);
}
// List available prompts
try {
@ -130,6 +93,61 @@ async function main(): Promise<void> {
console.log('\nKeeping connection open to receive notifications. Press Ctrl+C to exit.');
}
async function listTools(client: Client): Promise<void> {
try {
const toolsRequest: ListToolsRequest = {
method: 'tools/list',
params: {}
};
const toolsResult = await client.request(toolsRequest, ListToolsResultSchema);
console.log('Available tools:', toolsResult.tools);
if (toolsResult.tools.length === 0) {
console.log('No tools available from the server');
}
} catch (error) {
console.log(`Tools not supported by this server (${error})`);
return
}
}
async function callGreetTool(client: Client): Promise<void> {
try {
const greetRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'greet',
arguments: { name: 'MCP User' }
}
};
const greetResult = await client.request(greetRequest, CallToolResultSchema);
console.log('Greeting result:', greetResult.content[0].text);
} catch (error) {
console.log(`Error calling greet tool: ${error}`);
}
}
async function callMultiGreetTool(client: Client): Promise<void> {
try {
console.log('\nCalling multi-greet tool (with notifications)...');
const multiGreetRequest: CallToolRequest = {
method: 'tools/call',
params: {
name: 'multi-greet',
arguments: { name: 'MCP User' }
}
};
const multiGreetResult = await client.request(multiGreetRequest, CallToolResultSchema);
console.log('Multi-greet results:');
multiGreetResult.content.forEach(item => {
if (item.type === 'text') {
console.log(`- ${item.text}`);
}
});
} catch (error) {
console.log(`Error calling multi-greet tool: ${error}`);
}
}
main().catch((error: unknown) => {
console.error('Error running MCP client:', error);
process.exit(1);