add examples with notifications

This commit is contained in:
ihrpr 2025-04-09 21:38:57 +01:00
commit 71aeb9a3a6
No known key found for this signature in database

View file

@ -10,7 +10,8 @@ import {
GetPromptRequest,
GetPromptResultSchema,
ListResourcesRequest,
ListResourcesResultSchema
ListResourcesResultSchema,
LoggingMessageNotificationSchema
} from '../../types.js';
async function main(): Promise<void> {
@ -19,12 +20,17 @@ async function main(): Promise<void> {
name: 'example-client',
version: '1.0.0'
});
const transport = new StreamableHTTPClientTransport(
new URL('http://localhost:3000/mcp')
);
// Connect the client using the transport and initialize the server
await client.connect(transport);
client.setNotificationHandler(LoggingMessageNotificationSchema, (notification) => {
console.log(`Notification received: ${notification.params.level} - ${notification.params.data}`);
});
console.log('Connected to MCP server');
// List available tools
@ -46,6 +52,23 @@ async function main(): Promise<void> {
const greetResult = await client.request(greetRequest, CallToolResultSchema);
console.log('Greeting result:', greetResult.content[0].text);
// 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}`);
}
});
// List available prompts
try {
const promptsRequest: ListPromptsRequest = {