-
Notifications
You must be signed in to change notification settings - Fork 793
Streamable Http - fix server initialization validation in Stateless servers #377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f0fbfc7
StreamableHTTPServerTransport should only check init status when ther…
qikaigao 3048a6f
Merge branch 'main' into patch-1
cliffhall d88308b
Merge branch 'main' into patch-1
cliffhall 0058c30
Update src/server/streamableHttp.ts
qikaigao 1df3487
Update streamableHttp.ts
qikaigao 88f962d
Merge branch 'main' into patch-1
cliffhall 70608b5
Add more test clients
ihrpr 39f7fdf
fix stateless example
ihrpr a51fea3
Merge remote-tracking branch 'qikaigao/patch-1' into ihrpr/example-cl…
ihrpr 37fc7d2
fix check to skip session validation for stateless mode
ihrpr eeda5d6
readme update
ihrpr 6050aa9
lint
ihrpr 4b8efae
Merge branch 'main' into ihrpr/example-clients
ihrpr ded0ce3
improve readme
ihrpr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import { Client } from '../../client/index.js'; | ||
import { StreamableHTTPClientTransport } from '../../client/streamableHttp.js'; | ||
import { | ||
CallToolRequest, | ||
CallToolResultSchema, | ||
LoggingMessageNotificationSchema, | ||
CallToolResult, | ||
} from '../../types.js'; | ||
|
||
/** | ||
* Multiple Clients MCP Example | ||
* | ||
* This client demonstrates how to: | ||
* 1. Create multiple MCP clients in parallel | ||
* 2. Each client calls a single tool | ||
* 3. Track notifications from each client independently | ||
*/ | ||
|
||
// Command line args processing | ||
const args = process.argv.slice(2); | ||
const serverUrl = args[0] || 'http://localhost:3000/mcp'; | ||
|
||
interface ClientConfig { | ||
id: string; | ||
name: string; | ||
toolName: string; | ||
toolArguments: Record<string, string | number | boolean>; | ||
} | ||
|
||
async function createAndRunClient(config: ClientConfig): Promise<{ id: string; result: CallToolResult }> { | ||
console.log(`[${config.id}] Creating client: ${config.name}`); | ||
|
||
const client = new Client({ | ||
name: config.name, | ||
version: '1.0.0' | ||
}); | ||
|
||
const transport = new StreamableHTTPClientTransport(new URL(serverUrl)); | ||
|
||
// Set up client-specific error handler | ||
client.onerror = (error) => { | ||
console.error(`[${config.id}] Client error:`, error); | ||
}; | ||
|
||
// Set up client-specific notification handler | ||
client.setNotificationHandler(LoggingMessageNotificationSchema, (notification) => { | ||
console.log(`[${config.id}] Notification: ${notification.params.data}`); | ||
}); | ||
|
||
try { | ||
// Connect to the server | ||
await client.connect(transport); | ||
console.log(`[${config.id}] Connected to MCP server`); | ||
|
||
// Call the specified tool | ||
console.log(`[${config.id}] Calling tool: ${config.toolName}`); | ||
const toolRequest: CallToolRequest = { | ||
method: 'tools/call', | ||
params: { | ||
name: config.toolName, | ||
arguments: { | ||
...config.toolArguments, | ||
// Add client ID to arguments for identification in notifications | ||
caller: config.id | ||
} | ||
} | ||
}; | ||
|
||
const result = await client.request(toolRequest, CallToolResultSchema); | ||
console.log(`[${config.id}] Tool call completed`); | ||
|
||
// Keep the connection open for a bit to receive notifications | ||
await new Promise(resolve => setTimeout(resolve, 5000)); | ||
|
||
// Disconnect | ||
await transport.close(); | ||
console.log(`[${config.id}] Disconnected from MCP server`); | ||
|
||
return { id: config.id, result }; | ||
} catch (error) { | ||
console.error(`[${config.id}] Error:`, error); | ||
throw error; | ||
} | ||
} | ||
|
||
async function main(): Promise<void> { | ||
console.log('MCP Multiple Clients Example'); | ||
console.log('============================'); | ||
console.log(`Server URL: ${serverUrl}`); | ||
console.log(''); | ||
|
||
try { | ||
// Define client configurations | ||
const clientConfigs: ClientConfig[] = [ | ||
{ | ||
id: 'client1', | ||
name: 'basic-client-1', | ||
toolName: 'start-notification-stream', | ||
toolArguments: { | ||
interval: 3, // 1 second between notifications | ||
count: 5 // Send 5 notifications | ||
} | ||
}, | ||
{ | ||
id: 'client2', | ||
name: 'basic-client-2', | ||
toolName: 'start-notification-stream', | ||
toolArguments: { | ||
interval: 2, // 2 seconds between notifications | ||
count: 3 // Send 3 notifications | ||
} | ||
}, | ||
{ | ||
id: 'client3', | ||
name: 'basic-client-3', | ||
toolName: 'start-notification-stream', | ||
toolArguments: { | ||
interval: 1, // 0.5 second between notifications | ||
count: 8 // Send 8 notifications | ||
} | ||
} | ||
]; | ||
|
||
// Start all clients in parallel | ||
console.log(`Starting ${clientConfigs.length} clients in parallel...`); | ||
console.log(''); | ||
|
||
const clientPromises = clientConfigs.map(config => createAndRunClient(config)); | ||
const results = await Promise.all(clientPromises); | ||
|
||
// Display results from all clients | ||
console.log('\n=== Final Results ==='); | ||
results.forEach(({ id, result }) => { | ||
console.log(`\n[${id}] Tool result:`); | ||
if (Array.isArray(result.content)) { | ||
result.content.forEach((item: { type: string; text?: string }) => { | ||
if (item.type === 'text' && item.text) { | ||
console.log(` ${item.text}`); | ||
} else { | ||
console.log(` ${item.type} content:`, item); | ||
} | ||
}); | ||
} else { | ||
console.log(` Unexpected result format:`, result); | ||
} | ||
}); | ||
|
||
console.log('\n=== All clients completed successfully ==='); | ||
|
||
} catch (error) { | ||
console.error('Error running multiple clients:', error); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
// Start the example | ||
main().catch((error: unknown) => { | ||
console.error('Error running MCP multiple clients example:', error); | ||
process.exit(1); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.