{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { Genkit } from 'genkit';\nimport {\n  GenkitMcpClient,\n  McpClientOptions,\n  McpClientOptionsWithCache,\n  McpServerConfig,\n  McpStdioServerConfig,\n} from './client/client.mjs';\nimport {\n  GenkitMcpHost,\n  McpHostOptions,\n  McpHostOptionsWithCache,\n} from './client/index.mjs';\nimport { GenkitMcpServer } from './server.mjs';\nexport {\n  GenkitMcpClient,\n  GenkitMcpHost,\n  type McpClientOptions,\n  type McpClientOptionsWithCache,\n  type McpHostOptions,\n  type McpHostOptionsWithCache,\n  type McpServerConfig,\n  type McpStdioServerConfig,\n};\n\nexport interface McpServerOptions {\n  /** The name you want to give your server for MCP inspection. */\n  name: string;\n  /** The version you want the server to advertise to clients. Defaults to\n   * 1.0.0. */\n  version?: string;\n}\n\n/**\n * Creates an MCP Client Host that connects to one or more MCP servers.\n * Each server is defined in the `mcpClients` option, where the key is a\n * client-side name for the server and the value is the server's configuration.\n *\n * By default, all servers in the config will be attempted to connect unless\n * their configuration includes `{disabled: true}`.\n *\n * ```ts\n * const clientHost = createMcpHost({\n *   name: \"my-mcp-client-host\", // Name for the host itself\n *   mcpServers: {\n *     // Each key is a name for this client/server configuration\n *     // Each value is an McpServerConfig object\n *     gitToolServer: { command: \"uvx\", args: [\"mcp-server-git\"] },\n *     customApiServer: { url: \"http://localhost:1234/mcp\" }\n *   }\n * });\n * ```\n *\n * @param options Configuration for the MCP Client Host, including the definitions of MCP servers to connect to.\n * @returns A new instance of GenkitMcpHost.\n */\nexport function createMcpHost<M extends boolean = false>(\n  options: McpHostOptions<M>\n) {\n  return new GenkitMcpHost<M>(options);\n}\n\n/**\n * Creates an MCP Client Host that connects to one or more MCP servers.\n * Each server is defined in the `mcpClients` option, where the key is a\n * client-side name for the server and the value is the server's configuration.\n *\n * By default, all servers in the config will be attempted to connect unless\n * their configuration includes `{disabled: true}`.\n *\n * ```ts\n * const clientHost = defineMcpHost(ai, {\n *   name: \"my-mcp-client-host\", // Name for the host itself\n *   mcpServers: {\n *     // Each key is a name for this client/server configuration\n *     // Each value is an McpServerConfig object\n *     gitToolServer: { command: \"uvx\", args: [\"mcp-server-git\"] },\n *     customApiServer: { url: \"http://localhost:1234/mcp\" }\n *   }\n * });\n * ```\n *\n * @param options Configuration for the MCP Client Host, including the definitions of MCP servers to connect to.\n * @returns A new instance of GenkitMcpHost.\n */\nexport function defineMcpHost<M extends boolean = false>(\n  ai: Genkit,\n  options: McpHostOptionsWithCache<M>\n) {\n  const mcpHost = new GenkitMcpHost<M>(options);\n  const dap = ai.defineDynamicActionProvider(\n    {\n      name: options.name,\n      cacheConfig: {\n        ttlMillis: options.cacheTTLMillis,\n      },\n    },\n    async () => ({\n      tool: await mcpHost.getActiveTools(ai),\n      resource: await mcpHost.getActiveResources(ai),\n    })\n  );\n  mcpHost.dynamicActionProvider = dap;\n  return mcpHost;\n}\n\n/**\n * Creates an MCP Client that connects to a single MCP server.\n * This is useful when you only need to interact with one MCP server,\n * or if you want to manage client instances individually.\n *\n * ```ts\n * const client = createMcpClient({\n *   name: \"mySingleMcpClient\", // A name for this client instance\n *   command: \"npx\", // Example: Launching a local server\n *   args: [\"-y\", \"@modelcontextprotocol/server-everything\", \"/path/to/allowed/dir\"],\n * });\n *\n * // To get tools from this client:\n * // const tools = await client.getActiveTools(ai);\n * ```\n *\n * @param options Configuration for the MCP Client, defining how it connects\n *                to the MCP server and its behavior.\n * @returns A new instance of GenkitMcpClient.\n */\nexport function createMcpClient<M extends boolean = false>(\n  options: McpClientOptions<M>\n) {\n  return new GenkitMcpClient<M>(options);\n}\n\n/**\n * Defines an MCP Client that connects to a single MCP server.\n * This is useful when you only need to interact with one MCP server,\n * or if you want to manage client instances individually.\n *\n * ```ts\n * const client = defineMcpClient(ai, {\n *   name: \"mySingleMcpClient\", // A name for this client instance\n *   command: \"npx\", // Example: Launching a local server\n *   args: [\"-y\", \"@modelcontextprotocol/server-everything\", \"/path/to/allowed/dir\"],\n * });\n *\n * // To get tools from this client:\n * // const tools = await client.getActiveTools(ai);\n *\n * // Or in a generate call you can use:\n * ai.generate({\n    prompt: `<a prompt requiring tools>`,\n    tools: ['mySingleMcpClient:tool/*'],\n  });\n * ```\n *\n * @param options Configuration for the MCP Client, defining how it connects\n *                to the MCP server and its behavior.\n * @returns A new instance of GenkitMcpClient.\n */\nexport function defineMcpClient<M extends boolean = false>(\n  ai: Genkit,\n  options: McpClientOptionsWithCache<M>\n) {\n  const mcpClient = new GenkitMcpClient<M>(options);\n  const dap = ai.defineDynamicActionProvider(\n    {\n      name: options.name,\n      cacheConfig: {\n        ttlMillis: options.cacheTtlMillis,\n      },\n    },\n    async () => {\n      return {\n        tool: await mcpClient.getActiveTools(ai),\n        resource: await mcpClient.getActiveResources(ai),\n      };\n    }\n  );\n  mcpClient.dynamicActionProvider = dap;\n  return mcpClient;\n}\n\n/**\n * Creates an MCP server based on the supplied Genkit instance. All tools and prompts\n * will be automatically converted to MCP compatibility.\n *\n * ```ts\n * const mcpServer = createMcpServer(ai, {name: 'my-mcp-server', version: '0.1.0'});\n *\n * await mcpServer.start(); // starts a stdio transport, OR\n * await mcpServer.start(customMcpTransport); // starts server using supplied transport\n * ```\n *\n * @param ai Your Genkit instance with registered tools and prompts.\n * @param options Configuration metadata for the server.\n * @returns GenkitMcpServer instance.\n */\nexport function createMcpServer(\n  ai: Genkit,\n  options: McpServerOptions\n): GenkitMcpServer {\n  return new GenkitMcpServer(ai, options);\n}\n"],"mappings":"AAiBA;AAAA,EACE;AAAA,OAKK;AACP;AAAA,EACE;AAAA,OAGK;AACP,SAAS,uBAAuB;AA2CzB,SAAS,cACd,SACA;AACA,SAAO,IAAI,cAAiB,OAAO;AACrC;AAyBO,SAAS,cACd,IACA,SACA;AACA,QAAM,UAAU,IAAI,cAAiB,OAAO;AAC5C,QAAM,MAAM,GAAG;AAAA,IACb;AAAA,MACE,MAAM,QAAQ;AAAA,MACd,aAAa;AAAA,QACX,WAAW,QAAQ;AAAA,MACrB;AAAA,IACF;AAAA,IACA,aAAa;AAAA,MACX,MAAM,MAAM,QAAQ,eAAe,EAAE;AAAA,MACrC,UAAU,MAAM,QAAQ,mBAAmB,EAAE;AAAA,IAC/C;AAAA,EACF;AACA,UAAQ,wBAAwB;AAChC,SAAO;AACT;AAsBO,SAAS,gBACd,SACA;AACA,SAAO,IAAI,gBAAmB,OAAO;AACvC;AA4BO,SAAS,gBACd,IACA,SACA;AACA,QAAM,YAAY,IAAI,gBAAmB,OAAO;AAChD,QAAM,MAAM,GAAG;AAAA,IACb;AAAA,MACE,MAAM,QAAQ;AAAA,MACd,aAAa;AAAA,QACX,WAAW,QAAQ;AAAA,MACrB;AAAA,IACF;AAAA,IACA,YAAY;AACV,aAAO;AAAA,QACL,MAAM,MAAM,UAAU,eAAe,EAAE;AAAA,QACvC,UAAU,MAAM,UAAU,mBAAmB,EAAE;AAAA,MACjD;AAAA,IACF;AAAA,EACF;AACA,YAAU,wBAAwB;AAClC,SAAO;AACT;AAiBO,SAAS,gBACd,IACA,SACiB;AACjB,SAAO,IAAI,gBAAgB,IAAI,OAAO;AACxC;","names":[]}