parallelToolCallsClient.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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
  150. .request(request, types_js_1.CallToolResultSchema)
  151. .then(result => ({ caller, result }))
  152. .catch(error => {
  153. console.error(`Error in tool call for ${caller}:`, error);
  154. throw error;
  155. });
  156. });
  157. // Wait for all tool calls to complete
  158. const results = await Promise.all(toolPromises);
  159. // Organize results by caller
  160. const resultsByTool = {};
  161. results.forEach(({ caller, result }) => {
  162. resultsByTool[caller] = result;
  163. });
  164. return resultsByTool;
  165. }
  166. catch (error) {
  167. console.error(`Error starting parallel notification tools:`, error);
  168. throw error;
  169. }
  170. }
  171. // Start the client
  172. main().catch((error) => {
  173. console.error('Error running MCP client:', error);
  174. process.exit(1);
  175. });
  176. //# sourceMappingURL=parallelToolCallsClient.js.map