support sse though get

This commit is contained in:
ihrpr 2025-04-10 14:23:34 +01:00
commit 111586c523
No known key found for this signature in database

View file

@ -11,7 +11,8 @@ import {
GetPromptResultSchema,
ListResourcesRequest,
ListResourcesResultSchema,
LoggingMessageNotificationSchema
LoggingMessageNotificationSchema,
ResourceListChangedNotificationSchema
} from '../../types.js';
async function main(): Promise<void> {
@ -24,51 +25,80 @@ async function main(): Promise<void> {
const transport = new StreamableHTTPClientTransport(
new URL('http://localhost:3000/mcp')
);
const supportsStandaloneSse = false;
// Connect the client using the transport and initialize the server
await client.connect(transport);
console.log('Connected to MCP server');
// Open a standalone SSE stream to receive server-initiated messages
console.log('Opening SSE stream to receive server notifications...');
try {
await transport.openSseStream();
const supportsStandaloneSse = false;
console.log('SSE stream established successfully. Waiting for notifications...');
}
catch (error) {
console.error('Failed to open SSE stream:', error);
}
// Set up notification handlers for server-initiated messages
client.setNotificationHandler(LoggingMessageNotificationSchema, (notification) => {
console.log(`Notification received: ${notification.params.level} - ${notification.params.data}`);
});
console.log('Connected to MCP server');
// List available tools
const toolsRequest: ListToolsRequest = {
method: 'tools/list',
params: {}
};
const toolsResult = await client.request(toolsRequest, ListToolsResultSchema);
console.log('Available tools:', toolsResult.tools);
// 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);
// 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}`);
}
client.setNotificationHandler(ResourceListChangedNotificationSchema, async (_) => {
console.log(`Resource list changed notification received!`);
const resourcesRequest: ListResourcesRequest = {
method: 'resources/list',
params: {}
};
const resourcesResult = await client.request(resourcesRequest, ListResourcesResultSchema);
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);
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);
// 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 {
const promptsRequest: ListPromptsRequest = {
@ -107,9 +137,11 @@ async function main(): Promise<void> {
} catch (error) {
console.log(`Resources not supported by this server (${error})`);
}
if (supportsStandaloneSse) {
// Instead of closing immediately, keep the connection open to receive notifications
console.log('\nKeeping connection open to receive notifications. Press Ctrl+C to exit.');
}
// Close the connection
await client.close();
}
main().catch((error: unknown) => {