@copilotkit/runtime 1.8.5-next.1 → 1.8.5-next.3

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.
Files changed (46) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/{chunk-EH5E2IX3.mjs → chunk-2PVUPC44.mjs} +2 -2
  3. package/dist/{chunk-FZJAYGIR.mjs → chunk-DNI7KA7Y.mjs} +2 -2
  4. package/dist/chunk-Q6JA6YY3.mjs +1 -0
  5. package/dist/{chunk-CFWU64MY.mjs → chunk-TDPCQIMB.mjs} +2 -2
  6. package/dist/{chunk-AQHJGUTA.mjs → chunk-UOCZJTXN.mjs} +2 -2
  7. package/dist/{chunk-KH52RHVR.mjs → chunk-UTUGZIQI.mjs} +165 -10
  8. package/dist/chunk-UTUGZIQI.mjs.map +1 -0
  9. package/dist/index.d.ts +8 -8
  10. package/dist/index.js +2882 -2719
  11. package/dist/index.js.map +1 -1
  12. package/dist/index.mjs +10 -6
  13. package/dist/index.mjs.map +1 -1
  14. package/dist/lib/index.d.ts +7 -7
  15. package/dist/lib/index.js +3876 -3713
  16. package/dist/lib/index.js.map +1 -1
  17. package/dist/lib/index.mjs +10 -6
  18. package/dist/lib/integrations/index.d.ts +4 -4
  19. package/dist/lib/integrations/index.js +1 -1
  20. package/dist/lib/integrations/index.js.map +1 -1
  21. package/dist/lib/integrations/index.mjs +5 -5
  22. package/dist/lib/integrations/nest/index.d.ts +3 -3
  23. package/dist/lib/integrations/nest/index.js +1 -1
  24. package/dist/lib/integrations/nest/index.js.map +1 -1
  25. package/dist/lib/integrations/nest/index.mjs +3 -3
  26. package/dist/lib/integrations/node-express/index.d.ts +3 -3
  27. package/dist/lib/integrations/node-express/index.js +1 -1
  28. package/dist/lib/integrations/node-express/index.js.map +1 -1
  29. package/dist/lib/integrations/node-express/index.mjs +3 -3
  30. package/dist/lib/integrations/node-http/index.d.ts +3 -3
  31. package/dist/lib/integrations/node-http/index.js +1 -1
  32. package/dist/lib/integrations/node-http/index.js.map +1 -1
  33. package/dist/lib/integrations/node-http/index.mjs +2 -2
  34. package/dist/service-adapters/index.mjs +1 -1
  35. package/dist/{copilot-runtime-9347bd66.d.ts → shared-86ec42e7.d.ts} +130 -45
  36. package/package.json +2 -2
  37. package/src/lib/index.ts +2 -1
  38. package/src/lib/runtime/copilot-runtime.ts +218 -11
  39. package/src/lib/runtime/mcp-tools-utils.ts +117 -0
  40. package/dist/chunk-KH52RHVR.mjs.map +0 -1
  41. package/dist/chunk-PTC5JN3P.mjs +0 -1
  42. /package/dist/{chunk-EH5E2IX3.mjs.map → chunk-2PVUPC44.mjs.map} +0 -0
  43. /package/dist/{chunk-FZJAYGIR.mjs.map → chunk-DNI7KA7Y.mjs.map} +0 -0
  44. /package/dist/{chunk-PTC5JN3P.mjs.map → chunk-Q6JA6YY3.mjs.map} +0 -0
  45. /package/dist/{chunk-CFWU64MY.mjs.map → chunk-TDPCQIMB.mjs.map} +0 -0
  46. /package/dist/{chunk-AQHJGUTA.mjs.map → chunk-UOCZJTXN.mjs.map} +0 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @copilotkit/runtime
2
2
 
3
+ ## 1.8.5-next.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 77a7457: - feat: Add Model Context Protocol (MCP) support
8
+ - @copilotkit/shared@1.8.5-next.3
9
+
10
+ ## 1.8.5-next.2
11
+
12
+ ### Patch Changes
13
+
14
+ - @copilotkit/shared@1.8.5-next.2
15
+
3
16
  ## 1.8.5-next.1
4
17
 
5
18
  ### Patch Changes
@@ -2,7 +2,7 @@ import {
2
2
  copilotRuntimeNodeHttpEndpoint,
3
3
  getRuntimeInstanceTelemetryInfo,
4
4
  telemetry_client_default
5
- } from "./chunk-KH52RHVR.mjs";
5
+ } from "./chunk-UTUGZIQI.mjs";
6
6
  import {
7
7
  __name
8
8
  } from "./chunk-FHD4JECV.mjs";
@@ -22,4 +22,4 @@ __name(copilotRuntimeNodeExpressEndpoint, "copilotRuntimeNodeExpressEndpoint");
22
22
  export {
23
23
  copilotRuntimeNodeExpressEndpoint
24
24
  };
25
- //# sourceMappingURL=chunk-EH5E2IX3.mjs.map
25
+ //# sourceMappingURL=chunk-2PVUPC44.mjs.map
@@ -1439,7 +1439,6 @@ __name(EmptyAdapter, "EmptyAdapter");
1439
1439
  var ExperimentalEmptyAdapter = EmptyAdapter;
1440
1440
 
1441
1441
  export {
1442
- RemoteChain,
1443
1442
  OpenAIAdapter,
1444
1443
  streamLangChainResponse,
1445
1444
  LangChainAdapter,
@@ -1447,9 +1446,10 @@ export {
1447
1446
  OpenAIAssistantAdapter,
1448
1447
  UnifyAdapter,
1449
1448
  GroqAdapter,
1449
+ RemoteChain,
1450
1450
  AnthropicAdapter,
1451
1451
  ExperimentalOllamaAdapter,
1452
1452
  EmptyAdapter,
1453
1453
  ExperimentalEmptyAdapter
1454
1454
  };
1455
- //# sourceMappingURL=chunk-FZJAYGIR.mjs.map
1455
+ //# sourceMappingURL=chunk-DNI7KA7Y.mjs.map
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=chunk-Q6JA6YY3.mjs.map
@@ -2,7 +2,7 @@ import {
2
2
  copilotRuntimeNodeHttpEndpoint,
3
3
  getRuntimeInstanceTelemetryInfo,
4
4
  telemetry_client_default
5
- } from "./chunk-KH52RHVR.mjs";
5
+ } from "./chunk-UTUGZIQI.mjs";
6
6
  import {
7
7
  __name
8
8
  } from "./chunk-FHD4JECV.mjs";
@@ -22,4 +22,4 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
22
22
  export {
23
23
  copilotRuntimeNestEndpoint
24
24
  };
25
- //# sourceMappingURL=chunk-CFWU64MY.mjs.map
25
+ //# sourceMappingURL=chunk-TDPCQIMB.mjs.map
@@ -2,7 +2,7 @@ import {
2
2
  getCommonConfig,
3
3
  getRuntimeInstanceTelemetryInfo,
4
4
  telemetry_client_default
5
- } from "./chunk-KH52RHVR.mjs";
5
+ } from "./chunk-UTUGZIQI.mjs";
6
6
  import {
7
7
  __name
8
8
  } from "./chunk-FHD4JECV.mjs";
@@ -77,4 +77,4 @@ export {
77
77
  config,
78
78
  copilotRuntimeNextJSPagesRouterEndpoint
79
79
  };
80
- //# sourceMappingURL=chunk-AQHJGUTA.mjs.map
80
+ //# sourceMappingURL=chunk-UOCZJTXN.mjs.map
@@ -2,7 +2,7 @@ import {
2
2
  EmptyAdapter,
3
3
  RemoteChain,
4
4
  streamLangChainResponse
5
- } from "./chunk-FZJAYGIR.mjs";
5
+ } from "./chunk-DNI7KA7Y.mjs";
6
6
  import {
7
7
  GuardrailsValidationFailureResponse,
8
8
  MessageStreamInterruptedResponse,
@@ -39,7 +39,7 @@ var require_package = __commonJS({
39
39
  publishConfig: {
40
40
  access: "public"
41
41
  },
42
- version: "1.8.5-next.1",
42
+ version: "1.8.5-next.3",
43
43
  sideEffects: false,
44
44
  main: "./dist/index.js",
45
45
  module: "./dist/index.mjs",
@@ -2729,6 +2729,62 @@ __name(convertGqlInputToMessages, "convertGqlInputToMessages");
2729
2729
  // src/lib/runtime/copilot-runtime.ts
2730
2730
  import { from } from "rxjs";
2731
2731
  import { Client as LangGraphClient2 } from "@langchain/langgraph-sdk";
2732
+
2733
+ // src/lib/runtime/mcp-tools-utils.ts
2734
+ function extractParametersFromSchema(toolSchema) {
2735
+ var _a, _b;
2736
+ const parameters = [];
2737
+ const properties = (_a = toolSchema == null ? void 0 : toolSchema.parameters) == null ? void 0 : _a.properties;
2738
+ const requiredParams = new Set(((_b = toolSchema == null ? void 0 : toolSchema.parameters) == null ? void 0 : _b.required) || []);
2739
+ if (!properties) {
2740
+ return parameters;
2741
+ }
2742
+ for (const paramName in properties) {
2743
+ if (Object.prototype.hasOwnProperty.call(properties, paramName)) {
2744
+ const paramDef = properties[paramName];
2745
+ parameters.push({
2746
+ name: paramName,
2747
+ // Infer type, default to string. MCP schemas might have more complex types.
2748
+ // This might need refinement based on common MCP schema practices.
2749
+ type: paramDef.type || "string",
2750
+ description: paramDef.description,
2751
+ required: requiredParams.has(paramName)
2752
+ });
2753
+ }
2754
+ }
2755
+ return parameters;
2756
+ }
2757
+ __name(extractParametersFromSchema, "extractParametersFromSchema");
2758
+ function convertMCPToolsToActions(mcpTools, mcpEndpoint) {
2759
+ const actions = [];
2760
+ for (const [toolName, tool] of Object.entries(mcpTools)) {
2761
+ const parameters = extractParametersFromSchema(tool.schema);
2762
+ const handler = /* @__PURE__ */ __name(async (params) => {
2763
+ try {
2764
+ const result = await tool.execute({
2765
+ params
2766
+ });
2767
+ return typeof result === "string" ? result : JSON.stringify(result);
2768
+ } catch (error) {
2769
+ console.error(`Error executing MCP tool '${toolName}' from endpoint ${mcpEndpoint}:`, error);
2770
+ throw new Error(`Execution failed for MCP tool '${toolName}': ${error instanceof Error ? error.message : String(error)}`);
2771
+ }
2772
+ }, "handler");
2773
+ actions.push({
2774
+ name: toolName,
2775
+ description: tool.description || `MCP tool: ${toolName} (from ${mcpEndpoint})`,
2776
+ parameters,
2777
+ handler,
2778
+ // Add metadata for easier identification/debugging
2779
+ _isMCPTool: true,
2780
+ _mcpEndpoint: mcpEndpoint
2781
+ });
2782
+ }
2783
+ return actions;
2784
+ }
2785
+ __name(convertMCPToolsToActions, "convertMCPToolsToActions");
2786
+
2787
+ // src/lib/runtime/copilot-runtime.ts
2732
2788
  var CopilotRuntime = class {
2733
2789
  actions;
2734
2790
  remoteEndpointDefinitions;
@@ -2738,8 +2794,15 @@ var CopilotRuntime = class {
2738
2794
  delegateAgentProcessingToServiceAdapter;
2739
2795
  observability;
2740
2796
  availableAgents;
2797
+ // +++ MCP Properties +++
2798
+ mcpEndpointsConfig;
2799
+ mcpActionCache = /* @__PURE__ */ new Map();
2800
+ // --- MCP Properties ---
2801
+ // +++ MCP Client Factory +++
2802
+ createMCPClientImpl;
2803
+ // --- MCP Client Factory ---
2741
2804
  constructor(params) {
2742
- var _a, _b;
2805
+ var _a, _b, _c, _d;
2743
2806
  if ((params == null ? void 0 : params.actions) && (params == null ? void 0 : params.remoteEndpoints) && (params == null ? void 0 : params.remoteEndpoints.some((e) => e.type === EndpointType.LangGraphPlatform))) {
2744
2807
  console.warn("Actions set in runtime instance will not be available for the agent");
2745
2808
  }
@@ -2754,7 +2817,59 @@ var CopilotRuntime = class {
2754
2817
  this.onAfterRequest = (_b = params == null ? void 0 : params.middleware) == null ? void 0 : _b.onAfterRequest;
2755
2818
  this.delegateAgentProcessingToServiceAdapter = (params == null ? void 0 : params.delegateAgentProcessingToServiceAdapter) || false;
2756
2819
  this.observability = params == null ? void 0 : params.observability_c;
2820
+ this.mcpEndpointsConfig = params == null ? void 0 : params.mcpEndpoints;
2821
+ this.createMCPClientImpl = params == null ? void 0 : params.createMCPClient;
2822
+ if (this.mcpEndpointsConfig && this.mcpEndpointsConfig.length > 0 && !this.createMCPClientImpl) {
2823
+ throw new CopilotKitMisuseError({
2824
+ message: "MCP Integration Error: `mcpEndpoints` were provided, but the `createMCPClient` function was not passed to the CopilotRuntime constructor. Please provide an implementation for `createMCPClient`."
2825
+ });
2826
+ }
2827
+ if ((params == null ? void 0 : params.actions) && (((_c = params == null ? void 0 : params.remoteEndpoints) == null ? void 0 : _c.some((e) => e.type === EndpointType.LangGraphPlatform)) || ((_d = this.mcpEndpointsConfig) == null ? void 0 : _d.length))) {
2828
+ console.warn("Local 'actions' defined in CopilotRuntime might not be available to remote agents (LangGraph, MCP). Consider defining actions closer to the agent implementation if needed.");
2829
+ }
2757
2830
  }
2831
+ // +++ MCP Instruction Injection Method +++
2832
+ injectMCPToolInstructions(messages, currentActions) {
2833
+ const mcpActionsForRequest = currentActions.filter((action) => action._isMCPTool);
2834
+ if (!mcpActionsForRequest || mcpActionsForRequest.length === 0) {
2835
+ return messages;
2836
+ }
2837
+ const mcpToolInstructions = mcpActionsForRequest.map((action) => {
2838
+ const paramsString = action.parameters && action.parameters.length > 0 ? ` Parameters: ${action.parameters.map((p) => `${p.name}${p.required ? "*" : ""}(${p.type})`).join(", ")}` : "";
2839
+ return `- ${action.name}:${paramsString} ${action.description || ""}`;
2840
+ }).join("\n");
2841
+ if (!mcpToolInstructions) {
2842
+ return messages;
2843
+ }
2844
+ const instructions = "You have access to the following tools provided by external Model Context Protocol (MCP) servers:\n" + mcpToolInstructions + "\nUse them when appropriate to fulfill the user's request.";
2845
+ const systemMessageIndex = messages.findIndex((msg) => {
2846
+ var _a;
2847
+ return ((_a = msg.textMessage) == null ? void 0 : _a.role) === "system";
2848
+ });
2849
+ const newMessages = [
2850
+ ...messages
2851
+ ];
2852
+ if (systemMessageIndex !== -1) {
2853
+ const existingMsg = newMessages[systemMessageIndex];
2854
+ if (existingMsg.textMessage) {
2855
+ existingMsg.textMessage.content = (existingMsg.textMessage.content ? existingMsg.textMessage.content + "\n\n" : "") + instructions;
2856
+ }
2857
+ } else {
2858
+ newMessages.unshift({
2859
+ id: randomId2(),
2860
+ createdAt: /* @__PURE__ */ new Date(),
2861
+ textMessage: {
2862
+ role: MessageRole.system,
2863
+ content: instructions
2864
+ },
2865
+ actionExecutionMessage: void 0,
2866
+ resultMessage: void 0,
2867
+ agentStateMessage: void 0
2868
+ });
2869
+ }
2870
+ return newMessages;
2871
+ }
2872
+ // --- MCP Instruction Injection Method ---
2758
2873
  async processRuntimeRequest(request) {
2759
2874
  var _a, _b, _c, _d, _e;
2760
2875
  const { serviceAdapter, messages: rawMessages, actions: clientSideActionsInput, threadId, runId, outputMessagesPromise, graphqlContext, forwardedParameters, url, extensions, agentSession, agentStates, publicApiKey } = request;
@@ -2772,9 +2887,10 @@ For non-agent components like useCopilotChatSuggestions, CopilotTextarea, or Cop
2772
2887
  please use an LLM adapter instead.`
2773
2888
  });
2774
2889
  }
2775
- const messages = rawMessages.filter((message) => !message.agentStateMessage);
2776
- const inputMessages = convertGqlInputToMessages(messages);
2777
2890
  const serverSideActions = await this.getServerSideActions(request);
2891
+ const filteredRawMessages = rawMessages.filter((message) => !message.agentStateMessage);
2892
+ const messagesWithInjectedInstructions = this.injectMCPToolInstructions(filteredRawMessages, serverSideActions);
2893
+ const inputMessages = convertGqlInputToMessages(messagesWithInjectedInstructions);
2778
2894
  if (((_a = this.observability) == null ? void 0 : _a.enabled) && publicApiKey) {
2779
2895
  try {
2780
2896
  const requestData = {
@@ -3301,7 +3417,8 @@ please use an LLM adapter instead.`
3301
3417
  }
3302
3418
  }
3303
3419
  async getServerSideActions(request) {
3304
- const { messages: rawMessages, graphqlContext, agentStates, url } = request;
3420
+ var _a;
3421
+ const { graphqlContext, messages: rawMessages, agentStates, url } = request;
3305
3422
  const inputMessages = convertGqlInputToMessages(rawMessages);
3306
3423
  const langserveFunctions = [];
3307
3424
  for (const chainPromise of this.langserve) {
@@ -3327,10 +3444,46 @@ please use an LLM adapter instead.`
3327
3444
  properties: graphqlContext.properties,
3328
3445
  url
3329
3446
  }) : this.actions;
3447
+ const requestSpecificMCPActions = [];
3448
+ if (this.createMCPClientImpl) {
3449
+ const baseEndpoints = this.mcpEndpointsConfig || [];
3450
+ const requestEndpoints = ((_a = graphqlContext.properties) == null ? void 0 : _a.mcpEndpoints) || [];
3451
+ const effectiveEndpointsMap = /* @__PURE__ */ new Map();
3452
+ [
3453
+ ...baseEndpoints,
3454
+ ...requestEndpoints
3455
+ ].forEach((ep) => {
3456
+ if (ep && ep.endpoint) {
3457
+ effectiveEndpointsMap.set(ep.endpoint, ep);
3458
+ }
3459
+ });
3460
+ const effectiveEndpoints = Array.from(effectiveEndpointsMap.values());
3461
+ for (const config of effectiveEndpoints) {
3462
+ const endpointUrl = config.endpoint;
3463
+ let actionsForEndpoint = this.mcpActionCache.get(endpointUrl);
3464
+ if (!actionsForEndpoint) {
3465
+ let client = null;
3466
+ try {
3467
+ console.log(`MCP: Cache miss. Fetching tools for endpoint: ${endpointUrl}`);
3468
+ client = await this.createMCPClientImpl(config);
3469
+ const tools = await client.tools();
3470
+ actionsForEndpoint = convertMCPToolsToActions(tools, endpointUrl);
3471
+ this.mcpActionCache.set(endpointUrl, actionsForEndpoint);
3472
+ console.log(`MCP: Fetched and cached ${actionsForEndpoint.length} tools for ${endpointUrl}`);
3473
+ } catch (error) {
3474
+ console.error(`MCP: Failed to fetch tools from endpoint ${endpointUrl}. Skipping. Error:`, error);
3475
+ actionsForEndpoint = [];
3476
+ this.mcpActionCache.set(endpointUrl, actionsForEndpoint);
3477
+ }
3478
+ }
3479
+ requestSpecificMCPActions.push(...actionsForEndpoint || []);
3480
+ }
3481
+ }
3330
3482
  return [
3331
3483
  ...configuredActions,
3332
3484
  ...langserveFunctions,
3333
- ...remoteActions
3485
+ ...remoteActions,
3486
+ ...requestSpecificMCPActions
3334
3487
  ];
3335
3488
  }
3336
3489
  // Add helper method to detect provider
@@ -4588,13 +4741,15 @@ function copilotRuntimeNodeHttpEndpoint(options) {
4588
4741
  __name(copilotRuntimeNodeHttpEndpoint, "copilotRuntimeNodeHttpEndpoint");
4589
4742
 
4590
4743
  export {
4591
- getRuntimeInstanceTelemetryInfo,
4592
- telemetry_client_default,
4744
+ extractParametersFromSchema,
4745
+ convertMCPToolsToActions,
4593
4746
  CopilotRuntime,
4594
4747
  flattenToolCallsNoDuplicates,
4595
4748
  copilotKitEndpoint,
4596
4749
  langGraphPlatformEndpoint,
4597
4750
  resolveEndpointType,
4751
+ getRuntimeInstanceTelemetryInfo,
4752
+ telemetry_client_default,
4598
4753
  createLogger,
4599
4754
  addCustomHeaderPlugin,
4600
4755
  createContext,
@@ -4602,4 +4757,4 @@ export {
4602
4757
  getCommonConfig,
4603
4758
  copilotRuntimeNodeHttpEndpoint
4604
4759
  };
4605
- //# sourceMappingURL=chunk-KH52RHVR.mjs.map
4760
+ //# sourceMappingURL=chunk-UTUGZIQI.mjs.map