@elizaos/plugin-mcp 1.8.1 → 1.8.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/dist/index.js CHANGED
@@ -7954,7 +7954,7 @@ var provider = {
7954
7954
  };
7955
7955
 
7956
7956
  // src/service.ts
7957
- import { Service, logger as logger6 } from "@elizaos/core";
7957
+ import { Service, logger as logger6, getRequestContext } from "@elizaos/core";
7958
7958
  import { Client } from "@modelcontextprotocol/sdk/client/index.js";
7959
7959
  import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
7960
7960
  import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
@@ -8747,7 +8747,17 @@ class McpService extends Service {
8747
8747
  if (!config.url)
8748
8748
  throw new Error(`Missing URL for server ${name}`);
8749
8749
  const url = new URL(config.url);
8750
- const opts = config.headers ? { requestInit: { headers: config.headers } } : undefined;
8750
+ const headers = { ...config.headers };
8751
+ const apiKey = this.runtime.getSetting("ELIZAOS_API_KEY");
8752
+ if (apiKey && typeof apiKey === "string" && !headers["X-API-Key"]) {
8753
+ const baseUrl = process.env.NEXT_PUBLIC_APP_URL || process.env.APP_URL || "http://localhost:3000";
8754
+ try {
8755
+ if (url.origin === new URL(baseUrl).origin) {
8756
+ headers["X-API-Key"] = apiKey;
8757
+ }
8758
+ } catch {}
8759
+ }
8760
+ const opts = Object.keys(headers).length > 0 ? { requestInit: { headers } } : undefined;
8751
8761
  return config.type === "sse" ? new SSEClientTransport(url, opts) : new StreamableHTTPClientTransport(url, opts);
8752
8762
  }
8753
8763
  setupTransportHandlers(name, conn, state, isStdio) {
@@ -8897,25 +8907,38 @@ class McpService extends Service {
8897
8907
  isLazyConnection(serverName) {
8898
8908
  return this.lazyConnections.has(serverName);
8899
8909
  }
8910
+ getConnectionKey(serverName) {
8911
+ const config = this.lazyConnections.get(serverName);
8912
+ if (config && config.type !== "stdio") {
8913
+ const ctx = getRequestContext();
8914
+ if (ctx?.entityId)
8915
+ return `${serverName}:${ctx.entityId}`;
8916
+ }
8917
+ return serverName;
8918
+ }
8919
+ getConnection(serverName) {
8920
+ const key = this.getConnectionKey(serverName);
8921
+ return this.connections.get(key) || this.connections.get(serverName);
8922
+ }
8900
8923
  async ensureConnected(serverName) {
8901
- if (this.connections.has(serverName))
8924
+ const connectionKey = this.getConnectionKey(serverName);
8925
+ if (this.connections.has(connectionKey) || this.connections.has(serverName))
8902
8926
  return;
8903
8927
  const config = this.lazyConnections.get(serverName);
8904
8928
  if (!config)
8905
8929
  throw new Error(`Unknown server: ${serverName}`);
8906
8930
  const start = Date.now();
8907
- await this.connect(serverName, config);
8908
- this.lazyConnections.delete(serverName);
8909
- const server = this.connections.get(serverName)?.server;
8931
+ await this.connect(connectionKey, config);
8932
+ const server = this.connections.get(connectionKey)?.server;
8910
8933
  if (this.schemaCache.isEnabled && server?.tools?.length) {
8911
8934
  await this.schemaCache.setSchemas(this.runtime.agentId, serverName, this.schemaCache.hashConfig(config), server.tools);
8912
8935
  }
8913
- logger6.info(`[MCP] Lazy connected: ${serverName} in ${Date.now() - start}ms`);
8936
+ logger6.info(`[MCP] Connected: ${connectionKey} in ${Date.now() - start}ms`);
8914
8937
  this.mcpProvider = buildMcpProviderData(this.getServers());
8915
8938
  }
8916
8939
  async callTool(serverName, toolName, args) {
8917
8940
  await this.ensureConnected(serverName);
8918
- const conn = this.connections.get(serverName);
8941
+ const conn = this.getConnection(serverName);
8919
8942
  if (!conn)
8920
8943
  throw new Error(`No connection: ${serverName}`);
8921
8944
  if (conn.server.disabled)
@@ -8929,7 +8952,7 @@ class McpService extends Service {
8929
8952
  }
8930
8953
  async readResource(serverName, uri) {
8931
8954
  await this.ensureConnected(serverName);
8932
- const conn = this.connections.get(serverName);
8955
+ const conn = this.getConnection(serverName);
8933
8956
  if (!conn)
8934
8957
  throw new Error(`No connection: ${serverName}`);
8935
8958
  if (conn.server.disabled)
@@ -8937,12 +8960,12 @@ class McpService extends Service {
8937
8960
  return conn.client.readResource({ uri });
8938
8961
  }
8939
8962
  async restartConnection(serverName) {
8940
- const conn = this.connections.get(serverName);
8963
+ const connectionKey = this.getConnectionKey(serverName);
8964
+ const conn = this.getConnection(serverName);
8941
8965
  if (!conn)
8942
8966
  throw new Error(`No connection: ${serverName}`);
8943
- const config = conn.server.config;
8944
- await this.disconnect(serverName);
8945
- await this.connect(serverName, JSON.parse(config));
8967
+ await this.disconnect(connectionKey);
8968
+ await this.connect(connectionKey, JSON.parse(conn.server.config));
8946
8969
  }
8947
8970
  }
8948
8971
 
@@ -8981,4 +9004,4 @@ export {
8981
9004
  BACKOFF_MULTIPLIER
8982
9005
  };
8983
9006
 
8984
- //# debugId=FF2F732BBC24508A64756E2164756E21
9007
+ //# debugId=711DD608A619744C64756E2164756E21