@modelcontextprotocol/sdk 1.10.1 → 1.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -21
- package/dist/cjs/examples/client/multipleClientsParallel.d.ts +2 -0
- package/dist/cjs/examples/client/multipleClientsParallel.d.ts.map +1 -0
- package/dist/cjs/examples/client/multipleClientsParallel.js +134 -0
- package/dist/cjs/examples/client/multipleClientsParallel.js.map +1 -0
- package/dist/cjs/examples/client/parallelToolCallsClient.d.ts +2 -0
- package/dist/cjs/examples/client/parallelToolCallsClient.d.ts.map +1 -0
- package/dist/cjs/examples/client/parallelToolCallsClient.js +175 -0
- package/dist/cjs/examples/client/parallelToolCallsClient.js.map +1 -0
- package/dist/cjs/examples/server/jsonResponseStreamableHttp.js +50 -47
- package/dist/cjs/examples/server/jsonResponseStreamableHttp.js.map +1 -1
- package/dist/cjs/examples/server/simpleSseServer.js +46 -43
- package/dist/cjs/examples/server/simpleSseServer.js.map +1 -1
- package/dist/cjs/examples/server/simpleStatelessStreamableHttp.js +78 -87
- package/dist/cjs/examples/server/simpleStatelessStreamableHttp.js.map +1 -1
- package/dist/cjs/examples/server/simpleStreamableHttp.js +104 -101
- package/dist/cjs/examples/server/simpleStreamableHttp.js.map +1 -1
- package/dist/cjs/examples/server/sseAndStreamableHttpCompatibleServer.js +41 -37
- package/dist/cjs/examples/server/sseAndStreamableHttpCompatibleServer.js.map +1 -1
- package/dist/cjs/server/streamableHttp.d.ts.map +1 -1
- package/dist/cjs/server/streamableHttp.js +5 -5
- package/dist/cjs/server/streamableHttp.js.map +1 -1
- package/dist/esm/examples/client/multipleClientsParallel.d.ts +2 -0
- package/dist/esm/examples/client/multipleClientsParallel.d.ts.map +1 -0
- package/dist/esm/examples/client/multipleClientsParallel.js +132 -0
- package/dist/esm/examples/client/multipleClientsParallel.js.map +1 -0
- package/dist/esm/examples/client/parallelToolCallsClient.d.ts +2 -0
- package/dist/esm/examples/client/parallelToolCallsClient.d.ts.map +1 -0
- package/dist/esm/examples/client/parallelToolCallsClient.js +173 -0
- package/dist/esm/examples/client/parallelToolCallsClient.js.map +1 -0
- package/dist/esm/examples/server/jsonResponseStreamableHttp.js +50 -47
- package/dist/esm/examples/server/jsonResponseStreamableHttp.js.map +1 -1
- package/dist/esm/examples/server/simpleSseServer.js +46 -43
- package/dist/esm/examples/server/simpleSseServer.js.map +1 -1
- package/dist/esm/examples/server/simpleStatelessStreamableHttp.js +78 -87
- package/dist/esm/examples/server/simpleStatelessStreamableHttp.js.map +1 -1
- package/dist/esm/examples/server/simpleStreamableHttp.js +104 -101
- package/dist/esm/examples/server/simpleStreamableHttp.js.map +1 -1
- package/dist/esm/examples/server/sseAndStreamableHttpCompatibleServer.js +41 -37
- package/dist/esm/examples/server/sseAndStreamableHttpCompatibleServer.js.map +1 -1
- package/dist/esm/server/streamableHttp.d.ts.map +1 -1
- package/dist/esm/server/streamableHttp.js +5 -5
- package/dist/esm/server/streamableHttp.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -221,7 +221,8 @@ import express from "express";
|
|
|
221
221
|
import { randomUUID } from "node:crypto";
|
|
222
222
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
223
223
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
224
|
-
import {
|
|
224
|
+
import { isInitializeRequest } from "@modelcontextprotocol/sdk/types.js"
|
|
225
|
+
|
|
225
226
|
|
|
226
227
|
|
|
227
228
|
const app = express();
|
|
@@ -244,7 +245,6 @@ app.post('/mcp', async (req, res) => {
|
|
|
244
245
|
const eventStore = new InMemoryEventStore();
|
|
245
246
|
transport = new StreamableHTTPServerTransport({
|
|
246
247
|
sessionIdGenerator: () => randomUUID(),
|
|
247
|
-
eventStore, // Enable resumability
|
|
248
248
|
onsessioninitialized: (sessionId) => {
|
|
249
249
|
// Store the transport by session ID
|
|
250
250
|
transports[sessionId] = transport;
|
|
@@ -312,19 +312,23 @@ For simpler use cases where session management isn't needed:
|
|
|
312
312
|
const app = express();
|
|
313
313
|
app.use(express.json());
|
|
314
314
|
|
|
315
|
-
const transport: StreamableHTTPServerTransport = new StreamableHTTPServerTransport({
|
|
316
|
-
sessionIdGenerator: undefined, // set to undefined for stateless servers
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
// Setup routes for the server
|
|
320
|
-
const setupServer = async () => {
|
|
321
|
-
await server.connect(transport);
|
|
322
|
-
};
|
|
323
|
-
|
|
324
315
|
app.post('/mcp', async (req: Request, res: Response) => {
|
|
325
|
-
|
|
316
|
+
// In stateless mode, create a new instance of transport and server for each request
|
|
317
|
+
// to ensure complete isolation. A single instance would cause request ID collisions
|
|
318
|
+
// when multiple clients connect concurrently.
|
|
319
|
+
|
|
326
320
|
try {
|
|
327
|
-
|
|
321
|
+
const server = getServer();
|
|
322
|
+
const transport: StreamableHTTPServerTransport = new StreamableHTTPServerTransport({
|
|
323
|
+
sessionIdGenerator: undefined,
|
|
324
|
+
});
|
|
325
|
+
await server.connect(transport);
|
|
326
|
+
await transport.handleRequest(req, res, req.body);
|
|
327
|
+
res.on('close', () => {
|
|
328
|
+
console.log('Request closed');
|
|
329
|
+
transport.close();
|
|
330
|
+
server.close();
|
|
331
|
+
});
|
|
328
332
|
} catch (error) {
|
|
329
333
|
console.error('Error handling MCP request:', error);
|
|
330
334
|
if (!res.headersSent) {
|
|
@@ -364,15 +368,11 @@ app.delete('/mcp', async (req: Request, res: Response) => {
|
|
|
364
368
|
}));
|
|
365
369
|
});
|
|
366
370
|
|
|
371
|
+
|
|
367
372
|
// Start the server
|
|
368
373
|
const PORT = 3000;
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
console.log(`MCP Streamable HTTP Server listening on port ${PORT}`);
|
|
372
|
-
});
|
|
373
|
-
}).catch(error => {
|
|
374
|
-
console.error('Failed to set up the server:', error);
|
|
375
|
-
process.exit(1);
|
|
374
|
+
app.listen(PORT, () => {
|
|
375
|
+
console.log(`MCP Stateless Streamable HTTP Server listening on port ${PORT}`);
|
|
376
376
|
});
|
|
377
377
|
|
|
378
378
|
```
|
|
@@ -773,7 +773,6 @@ import express from "express";
|
|
|
773
773
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
774
774
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
775
775
|
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
|
|
776
|
-
import { InMemoryEventStore } from "@modelcontextprotocol/sdk/inMemory.js";
|
|
777
776
|
|
|
778
777
|
const server = new McpServer({
|
|
779
778
|
name: "backwards-compatible-server",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multipleClientsParallel.d.ts","sourceRoot":"","sources":["../../../../src/examples/client/multipleClientsParallel.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const index_js_1 = require("../../client/index.js");
|
|
4
|
+
const streamableHttp_js_1 = require("../../client/streamableHttp.js");
|
|
5
|
+
const types_js_1 = require("../../types.js");
|
|
6
|
+
/**
|
|
7
|
+
* Multiple Clients MCP Example
|
|
8
|
+
*
|
|
9
|
+
* This client demonstrates how to:
|
|
10
|
+
* 1. Create multiple MCP clients in parallel
|
|
11
|
+
* 2. Each client calls a single tool
|
|
12
|
+
* 3. Track notifications from each client independently
|
|
13
|
+
*/
|
|
14
|
+
// Command line args processing
|
|
15
|
+
const args = process.argv.slice(2);
|
|
16
|
+
const serverUrl = args[0] || 'http://localhost:3000/mcp';
|
|
17
|
+
async function createAndRunClient(config) {
|
|
18
|
+
console.log(`[${config.id}] Creating client: ${config.name}`);
|
|
19
|
+
const client = new index_js_1.Client({
|
|
20
|
+
name: config.name,
|
|
21
|
+
version: '1.0.0'
|
|
22
|
+
});
|
|
23
|
+
const transport = new streamableHttp_js_1.StreamableHTTPClientTransport(new URL(serverUrl));
|
|
24
|
+
// Set up client-specific error handler
|
|
25
|
+
client.onerror = (error) => {
|
|
26
|
+
console.error(`[${config.id}] Client error:`, error);
|
|
27
|
+
};
|
|
28
|
+
// Set up client-specific notification handler
|
|
29
|
+
client.setNotificationHandler(types_js_1.LoggingMessageNotificationSchema, (notification) => {
|
|
30
|
+
console.log(`[${config.id}] Notification: ${notification.params.data}`);
|
|
31
|
+
});
|
|
32
|
+
try {
|
|
33
|
+
// Connect to the server
|
|
34
|
+
await client.connect(transport);
|
|
35
|
+
console.log(`[${config.id}] Connected to MCP server`);
|
|
36
|
+
// Call the specified tool
|
|
37
|
+
console.log(`[${config.id}] Calling tool: ${config.toolName}`);
|
|
38
|
+
const toolRequest = {
|
|
39
|
+
method: 'tools/call',
|
|
40
|
+
params: {
|
|
41
|
+
name: config.toolName,
|
|
42
|
+
arguments: {
|
|
43
|
+
...config.toolArguments,
|
|
44
|
+
// Add client ID to arguments for identification in notifications
|
|
45
|
+
caller: config.id
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const result = await client.request(toolRequest, types_js_1.CallToolResultSchema);
|
|
50
|
+
console.log(`[${config.id}] Tool call completed`);
|
|
51
|
+
// Keep the connection open for a bit to receive notifications
|
|
52
|
+
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
53
|
+
// Disconnect
|
|
54
|
+
await transport.close();
|
|
55
|
+
console.log(`[${config.id}] Disconnected from MCP server`);
|
|
56
|
+
return { id: config.id, result };
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
console.error(`[${config.id}] Error:`, error);
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async function main() {
|
|
64
|
+
console.log('MCP Multiple Clients Example');
|
|
65
|
+
console.log('============================');
|
|
66
|
+
console.log(`Server URL: ${serverUrl}`);
|
|
67
|
+
console.log('');
|
|
68
|
+
try {
|
|
69
|
+
// Define client configurations
|
|
70
|
+
const clientConfigs = [
|
|
71
|
+
{
|
|
72
|
+
id: 'client1',
|
|
73
|
+
name: 'basic-client-1',
|
|
74
|
+
toolName: 'start-notification-stream',
|
|
75
|
+
toolArguments: {
|
|
76
|
+
interval: 3, // 1 second between notifications
|
|
77
|
+
count: 5 // Send 5 notifications
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
id: 'client2',
|
|
82
|
+
name: 'basic-client-2',
|
|
83
|
+
toolName: 'start-notification-stream',
|
|
84
|
+
toolArguments: {
|
|
85
|
+
interval: 2, // 2 seconds between notifications
|
|
86
|
+
count: 3 // Send 3 notifications
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
id: 'client3',
|
|
91
|
+
name: 'basic-client-3',
|
|
92
|
+
toolName: 'start-notification-stream',
|
|
93
|
+
toolArguments: {
|
|
94
|
+
interval: 1, // 0.5 second between notifications
|
|
95
|
+
count: 8 // Send 8 notifications
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
];
|
|
99
|
+
// Start all clients in parallel
|
|
100
|
+
console.log(`Starting ${clientConfigs.length} clients in parallel...`);
|
|
101
|
+
console.log('');
|
|
102
|
+
const clientPromises = clientConfigs.map(config => createAndRunClient(config));
|
|
103
|
+
const results = await Promise.all(clientPromises);
|
|
104
|
+
// Display results from all clients
|
|
105
|
+
console.log('\n=== Final Results ===');
|
|
106
|
+
results.forEach(({ id, result }) => {
|
|
107
|
+
console.log(`\n[${id}] Tool result:`);
|
|
108
|
+
if (Array.isArray(result.content)) {
|
|
109
|
+
result.content.forEach((item) => {
|
|
110
|
+
if (item.type === 'text' && item.text) {
|
|
111
|
+
console.log(` ${item.text}`);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
console.log(` ${item.type} content:`, item);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
console.log(` Unexpected result format:`, result);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
console.log('\n=== All clients completed successfully ===');
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
console.error('Error running multiple clients:', error);
|
|
126
|
+
process.exit(1);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// Start the example
|
|
130
|
+
main().catch((error) => {
|
|
131
|
+
console.error('Error running MCP multiple clients example:', error);
|
|
132
|
+
process.exit(1);
|
|
133
|
+
});
|
|
134
|
+
//# sourceMappingURL=multipleClientsParallel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multipleClientsParallel.js","sourceRoot":"","sources":["../../../../src/examples/client/multipleClientsParallel.ts"],"names":[],"mappings":";;AAAA,oDAA+C;AAC/C,sEAA+E;AAC/E,6CAKwB;AAExB;;;;;;;GAOG;AAEH,+BAA+B;AAC/B,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACnC,MAAM,SAAS,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,2BAA2B,CAAC;AASzD,KAAK,UAAU,kBAAkB,CAAC,MAAoB;IACpD,OAAO,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC,EAAE,sBAAsB,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;IAE9D,MAAM,MAAM,GAAG,IAAI,iBAAM,CAAC;QACxB,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,OAAO,EAAE,OAAO;KACjB,CAAC,CAAC;IAEH,MAAM,SAAS,GAAG,IAAI,iDAA6B,CAAC,IAAI,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC;IAExE,uCAAuC;IACvC,MAAM,CAAC,OAAO,GAAG,CAAC,KAAK,EAAE,EAAE;QACzB,OAAO,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,EAAE,iBAAiB,EAAE,KAAK,CAAC,CAAC;IACvD,CAAC,CAAC;IAEF,8CAA8C;I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AE,KAAK,CAAC,CAAC;IACpE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parallelToolCallsClient.d.ts","sourceRoot":"","sources":["../../../../src/examples/client/parallelToolCallsClient.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const index_js_1 = require("../../client/index.js");
|
|
4
|
+
const streamableHttp_js_1 = require("../../client/streamableHttp.js");
|
|
5
|
+
const types_js_1 = require("../../types.js");
|
|
6
|
+
/**
|
|
7
|
+
* Parallel Tool Calls MCP Client
|
|
8
|
+
*
|
|
9
|
+
* This client demonstrates how to:
|
|
10
|
+
* 1. Start multiple tool calls in parallel
|
|
11
|
+
* 2. Track notifications from each tool call using a caller parameter
|
|
12
|
+
*/
|
|
13
|
+
// Command line args processing
|
|
14
|
+
const args = process.argv.slice(2);
|
|
15
|
+
const serverUrl = args[0] || 'http://localhost:3000/mcp';
|
|
16
|
+
async function main() {
|
|
17
|
+
console.log('MCP Parallel Tool Calls Client');
|
|
18
|
+
console.log('==============================');
|
|
19
|
+
console.log(`Connecting to server at: ${serverUrl}`);
|
|
20
|
+
let client;
|
|
21
|
+
let transport;
|
|
22
|
+
try {
|
|
23
|
+
// Create client with streamable HTTP transport
|
|
24
|
+
client = new index_js_1.Client({
|
|
25
|
+
name: 'parallel-tool-calls-client',
|
|
26
|
+
version: '1.0.0'
|
|
27
|
+
});
|
|
28
|
+
client.onerror = (error) => {
|
|
29
|
+
console.error('Client error:', error);
|
|
30
|
+
};
|
|
31
|
+
// Connect to the server
|
|
32
|
+
transport = new streamableHttp_js_1.StreamableHTTPClientTransport(new URL(serverUrl));
|
|
33
|
+
await client.connect(transport);
|
|
34
|
+
console.log('Successfully connected to MCP server');
|
|
35
|
+
// Set up notification handler with caller identification
|
|
36
|
+
client.setNotificationHandler(types_js_1.LoggingMessageNotificationSchema, (notification) => {
|
|
37
|
+
console.log(`Notification: ${notification.params.data}`);
|
|
38
|
+
});
|
|
39
|
+
console.log("List tools");
|
|
40
|
+
const toolsRequest = await listTools(client);
|
|
41
|
+
console.log("Tools: ", toolsRequest);
|
|
42
|
+
// 2. Start multiple notification tools in parallel
|
|
43
|
+
console.log('\n=== Starting Multiple Notification Streams in Parallel ===');
|
|
44
|
+
const toolResults = await startParallelNotificationTools(client);
|
|
45
|
+
// Log the results from each tool call
|
|
46
|
+
for (const [caller, result] of Object.entries(toolResults)) {
|
|
47
|
+
console.log(`\n=== Tool result for ${caller} ===`);
|
|
48
|
+
result.content.forEach((item) => {
|
|
49
|
+
if (item.type === 'text') {
|
|
50
|
+
console.log(` ${item.text}`);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
console.log(` ${item.type} content:`, item);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
// 3. Wait for all notifications (10 seconds)
|
|
58
|
+
console.log('\n=== Waiting for all notifications ===');
|
|
59
|
+
await new Promise(resolve => setTimeout(resolve, 10000));
|
|
60
|
+
// 4. Disconnect
|
|
61
|
+
console.log('\n=== Disconnecting ===');
|
|
62
|
+
await transport.close();
|
|
63
|
+
console.log('Disconnected from MCP server');
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
console.error('Error running client:', error);
|
|
67
|
+
process.exit(1);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* List available tools on the server
|
|
72
|
+
*/
|
|
73
|
+
async function listTools(client) {
|
|
74
|
+
try {
|
|
75
|
+
const toolsRequest = {
|
|
76
|
+
method: 'tools/list',
|
|
77
|
+
params: {}
|
|
78
|
+
};
|
|
79
|
+
const toolsResult = await client.request(toolsRequest, types_js_1.ListToolsResultSchema);
|
|
80
|
+
console.log('Available tools:');
|
|
81
|
+
if (toolsResult.tools.length === 0) {
|
|
82
|
+
console.log(' No tools available');
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
for (const tool of toolsResult.tools) {
|
|
86
|
+
console.log(` - ${tool.name}: ${tool.description}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
console.log(`Tools not supported by this server: ${error}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Start multiple notification tools in parallel with different configurations
|
|
96
|
+
* Each tool call includes a caller parameter to identify its notifications
|
|
97
|
+
*/
|
|
98
|
+
async function startParallelNotificationTools(client) {
|
|
99
|
+
try {
|
|
100
|
+
// Define multiple tool calls with different configurations
|
|
101
|
+
const toolCalls = [
|
|
102
|
+
{
|
|
103
|
+
caller: 'fast-notifier',
|
|
104
|
+
request: {
|
|
105
|
+
method: 'tools/call',
|
|
106
|
+
params: {
|
|
107
|
+
name: 'start-notification-stream',
|
|
108
|
+
arguments: {
|
|
109
|
+
interval: 2, // 0.5 second between notifications
|
|
110
|
+
count: 10, // Send 10 notifications
|
|
111
|
+
caller: 'fast-notifier' // Identify this tool call
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
caller: 'slow-notifier',
|
|
118
|
+
request: {
|
|
119
|
+
method: 'tools/call',
|
|
120
|
+
params: {
|
|
121
|
+
name: 'start-notification-stream',
|
|
122
|
+
arguments: {
|
|
123
|
+
interval: 5, // 2 seconds between notifications
|
|
124
|
+
count: 5, // Send 5 notifications
|
|
125
|
+
caller: 'slow-notifier' // Identify this tool call
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
caller: 'burst-notifier',
|
|
132
|
+
request: {
|
|
133
|
+
method: 'tools/call',
|
|
134
|
+
params: {
|
|
135
|
+
name: 'start-notification-stream',
|
|
136
|
+
arguments: {
|
|
137
|
+
interval: 1, // 0.1 second between notifications
|
|
138
|
+
count: 3, // Send just 3 notifications
|
|
139
|
+
caller: 'burst-notifier' // Identify this tool call
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
];
|
|
145
|
+
console.log(`Starting ${toolCalls.length} notification tools in parallel...`);
|
|
146
|
+
// Start all tool calls in parallel
|
|
147
|
+
const toolPromises = toolCalls.map(({ caller, request }) => {
|
|
148
|
+
console.log(`Starting tool call for ${caller}...`);
|
|
149
|
+
return client.request(request, types_js_1.CallToolResultSchema)
|
|
150
|
+
.then(result => ({ caller, result }))
|
|
151
|
+
.catch(error => {
|
|
152
|
+
console.error(`Error in tool call for ${caller}:`, error);
|
|
153
|
+
throw error;
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
// Wait for all tool calls to complete
|
|
157
|
+
const results = await Promise.all(toolPromises);
|
|
158
|
+
// Organize results by caller
|
|
159
|
+
const resultsByTool = {};
|
|
160
|
+
results.forEach(({ caller, result }) => {
|
|
161
|
+
resultsByTool[caller] = result;
|
|
162
|
+
});
|
|
163
|
+
return resultsByTool;
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
console.error(`Error starting parallel notification tools:`, error);
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
// Start the client
|
|
171
|
+
main().catch((error) => {
|
|
172
|
+
console.error('Error running MCP client:', error);
|
|
173
|
+
process.exit(1);
|
|
174
|
+
});
|
|
175
|
+
//# sourceMappingURL=parallelToolCallsClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parallelToolCallsClient.js","sourceRoot":"","sources":["../../../../src/examples/client/parallelToolCallsClient.ts"],"names":[],"mappings":";;AAAA,oDAA+C;AAC/C,sEAA+E;AAC/E,6CAMwB;AAExB;;;;;;GAMG;AAEH,+BAA+B;AAC/B,MAAM,IAAI,GAAG,OAA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ACjC,CAAC,CAAC,CAAC;QAEH,OAAO,aAAa,CAAC;IACvB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,6CAA6C,EAAE,KAAK,CAAC,CAAC;QACpE,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC;AAED,mBAAmB;AACnB,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;IAC9B,OAAO,CAAC,KAAK,CAAC,2BAA2B,EAAE,KAAK,CAAC,CAAC;IAClD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
|
|
@@ -10,55 +10,58 @@ const streamableHttp_js_1 = require("../../server/streamableHttp.js");
|
|
|
10
10
|
const zod_1 = require("zod");
|
|
11
11
|
const types_js_1 = require("../../types.js");
|
|
12
12
|
// Create an MCP server with implementation details
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
// Register a simple tool that returns a greeting
|
|
22
|
-
server.tool('greet', 'A simple greeting tool', {
|
|
23
|
-
name: zod_1.z.string().describe('Name to greet'),
|
|
24
|
-
}, async ({ name }) => {
|
|
25
|
-
return {
|
|
26
|
-
content: [
|
|
27
|
-
{
|
|
28
|
-
type: 'text',
|
|
29
|
-
text: `Hello, ${name}!`,
|
|
30
|
-
},
|
|
31
|
-
],
|
|
32
|
-
};
|
|
33
|
-
});
|
|
34
|
-
// Register a tool that sends multiple greetings with notifications
|
|
35
|
-
server.tool('multi-greet', 'A tool that sends different greetings with delays between them', {
|
|
36
|
-
name: zod_1.z.string().describe('Name to greet'),
|
|
37
|
-
}, async ({ name }, { sendNotification }) => {
|
|
38
|
-
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
39
|
-
await sendNotification({
|
|
40
|
-
method: "notifications/message",
|
|
41
|
-
params: { level: "debug", data: `Starting multi-greet for ${name}` }
|
|
13
|
+
const getServer = () => {
|
|
14
|
+
const server = new mcp_js_1.McpServer({
|
|
15
|
+
name: 'json-response-streamable-http-server',
|
|
16
|
+
version: '1.0.0',
|
|
17
|
+
}, {
|
|
18
|
+
capabilities: {
|
|
19
|
+
logging: {},
|
|
20
|
+
}
|
|
42
21
|
});
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
22
|
+
// Register a simple tool that returns a greeting
|
|
23
|
+
server.tool('greet', 'A simple greeting tool', {
|
|
24
|
+
name: zod_1.z.string().describe('Name to greet'),
|
|
25
|
+
}, async ({ name }) => {
|
|
26
|
+
return {
|
|
27
|
+
content: [
|
|
28
|
+
{
|
|
29
|
+
type: 'text',
|
|
30
|
+
text: `Hello, ${name}!`,
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
};
|
|
47
34
|
});
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
35
|
+
// Register a tool that sends multiple greetings with notifications
|
|
36
|
+
server.tool('multi-greet', 'A tool that sends different greetings with delays between them', {
|
|
37
|
+
name: zod_1.z.string().describe('Name to greet'),
|
|
38
|
+
}, async ({ name }, { sendNotification }) => {
|
|
39
|
+
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
40
|
+
await sendNotification({
|
|
41
|
+
method: "notifications/message",
|
|
42
|
+
params: { level: "debug", data: `Starting multi-greet for ${name}` }
|
|
43
|
+
});
|
|
44
|
+
await sleep(1000); // Wait 1 second before first greeting
|
|
45
|
+
await sendNotification({
|
|
46
|
+
method: "notifications/message",
|
|
47
|
+
params: { level: "info", data: `Sending first greeting to ${name}` }
|
|
48
|
+
});
|
|
49
|
+
await sleep(1000); // Wait another second before second greeting
|
|
50
|
+
await sendNotification({
|
|
51
|
+
method: "notifications/message",
|
|
52
|
+
params: { level: "info", data: `Sending second greeting to ${name}` }
|
|
53
|
+
});
|
|
54
|
+
return {
|
|
55
|
+
content: [
|
|
56
|
+
{
|
|
57
|
+
type: 'text',
|
|
58
|
+
text: `Good morning, ${name}!`,
|
|
59
|
+
}
|
|
60
|
+
],
|
|
61
|
+
};
|
|
52
62
|
});
|
|
53
|
-
return
|
|
54
|
-
|
|
55
|
-
{
|
|
56
|
-
type: 'text',
|
|
57
|
-
text: `Good morning, ${name}!`,
|
|
58
|
-
}
|
|
59
|
-
],
|
|
60
|
-
};
|
|
61
|
-
});
|
|
63
|
+
return server;
|
|
64
|
+
};
|
|
62
65
|
const app = (0, express_1.default)();
|
|
63
66
|
app.use(express_1.default.json());
|
|
64
67
|
// Map to store transports by session ID
|
|
@@ -86,6 +89,7 @@ app.post('/mcp', async (req, res) => {
|
|
|
86
89
|
}
|
|
87
90
|
});
|
|
88
91
|
// Connect the transport to the MCP server BEFORE handling the request
|
|
92
|
+
const server = getServer();
|
|
89
93
|
await server.connect(transport);
|
|
90
94
|
await transport.handleRequest(req, res, req.body);
|
|
91
95
|
return; // Already handled
|
|
@@ -133,7 +137,6 @@ app.listen(PORT, () => {
|
|
|
133
137
|
// Handle server shutdown
|
|
134
138
|
process.on('SIGINT', async () => {
|
|
135
139
|
console.log('Shutting down server...');
|
|
136
|
-
await server.close();
|
|
137
140
|
process.exit(0);
|
|
138
141
|
});
|
|
139
142
|
//# sourceMappingURL=jsonResponseStreamableHttp.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsonResponseStreamableHttp.js","sourceRoot":"","sources":["../../../../src/examples/server/jsonResponseStreamableHttp.ts"],"names":[],"mappings":";;;;;AAAA,sDAAqD;AACrD,6CAAyC;AACzC,gDAAgD;AAChD,sEAA+E;AAC/E,6BAAwB;AACxB,6CAAqE;
|
|
1
|
+
{"version":3,"file":"jsonResponseStreamableHttp.js","sourceRoot":"","sources":["../../../../src/examples/server/jsonResponseStreamableHttp.ts"],"names":[],"mappings":";;;;;AAAA,sDAAqD;AACrD,6CAAyC;AACzC,gDAAgD;AAChD,sEAA+E;AAC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