import { Genkit, PromptAction, ResourceAction } from 'genkit'; import { McpClientOptions, GenkitMcpClient, McpClientOptionsWithCache } from './client/client.mjs'; import { McpHostOptions, GenkitMcpHost, McpHostOptionsWithCache } from './client/host.mjs'; import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'; import { ListToolsRequest, ListToolsResult, CallToolRequest, CallToolResult, ListPromptsRequest, ListPromptsResult, ListResourcesRequest, ListResourcesResult, ListResourceTemplatesRequest, ListResourceTemplatesResult, ReadResourceRequest, ReadResourceResult, GetPromptRequest, GetPromptResult } from '@modelcontextprotocol/sdk/types.js'; import { ToolAction } from 'genkit/tool'; import '@modelcontextprotocol/sdk/client/index.js'; import '@modelcontextprotocol/sdk/client/stdio.js'; import '@modelcontextprotocol/sdk/client/streamableHttp.js'; import '@modelcontextprotocol/sdk/client/sse.js'; /** * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Represents an MCP (Model Context Protocol) server that exposes Genkit tools * and prompts. This class wraps a Genkit instance and makes its registered * actions (tools, prompts) available to MCP clients. It handles the translation * between Genkit's action definitions and MCP's expected formats. */ declare class GenkitMcpServer { ai: Genkit; options: McpServerOptions; server?: Server; actionsResolved: boolean; toolActions: ToolAction[]; promptActions: PromptAction[]; resourceActions: ResourceAction[]; /** * Creates an instance of GenkitMcpServer. * @param ai The Genkit instance whose actions will be exposed. * @param options Configuration options for the MCP server, like its name and version. */ constructor(ai: Genkit, options: McpServerOptions); /** * Initializes the MCP server instance and registers request handlers. It * dynamically imports MCP SDK components and sets up handlers for listing * tools, calling tools, listing prompts, and getting prompts. It also * resolves and stores all tool and prompt actions from the Genkit instance. * * This method is called by the constructor and ensures the server is ready * before any requests are handled. It's idempotent. */ setup(): Promise; /** * Handles MCP requests to list available tools. * It maps the resolved Genkit tool actions to the MCP Tool format. * @param req The MCP ListToolsRequest. * @returns A Promise resolving to an MCP ListToolsResult. */ listTools(req: ListToolsRequest): Promise; /** * Handles MCP requests to call a specific tool. It finds the corresponding * Genkit tool action and executes it with the provided arguments. The result * is then formatted as an MCP CallToolResult. * @param req The MCP CallToolRequest containing the tool name and arguments. * @returns A Promise resolving to an MCP CallToolResult. * @throws GenkitError if the requested tool is not found. */ callTool(req: CallToolRequest): Promise; /** * Handles MCP requests to list available prompts. * It maps the resolved Genkit prompt actions to the MCP Prompt format, * including converting input schemas to MCP argument definitions. * @param req The MCP ListPromptsRequest. * @returns A Promise resolving to an MCP ListPromptsResult. */ listPrompts(req: ListPromptsRequest): Promise; /** * Handles MCP requests to list available resources. * It maps the resolved Genkit resource actions to the MCP Resource format. * @param req The MCP ListResourcesRequest. * @returns A Promise resolving to an MCP ListResourcesResult. */ listResources(req: ListResourcesRequest): Promise; /** * Handles MCP requests to list available resources. * It maps the resolved Genkit resource actions to the MCP Resource format. * @param req The MCP ListResourcesRequest. * @returns A Promise resolving to an MCP ListResourcesResult. */ listResourceTemplates(req: ListResourceTemplatesRequest): Promise; /** * Handles MCP requests to list available resources. * It maps the resolved Genkit resource actions to the MCP Resource format. * @param req The MCP ListResourcesRequest. * @returns A Promise resolving to an MCP ListResourcesResult. */ readResource(req: ReadResourceRequest): Promise; /** * Handles MCP requests to get (render) a specific prompt. It finds the * corresponding Genkit prompt action, executes it with the provided * arguments, and then formats the resulting messages into the MCP * PromptMessage format. * @param req The MCP GetPromptRequest containing the prompt name and * arguments. * @returns A Promise resolving to an MCP GetPromptResult. * @throws GenkitError if the requested prompt is not found. */ getPrompt(req: GetPromptRequest): Promise; /** * Starts the MCP server with the specified transport or a default * StdioServerTransport. Ensures the server is set up before connecting the * transport. * @param transport Optional MCP transport instance. If not provided, a * StdioServerTransport will be created and used. */ start(transport?: Transport): Promise; } /** * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ interface McpServerOptions { /** The name you want to give your server for MCP inspection. */ name: string; /** The version you want the server to advertise to clients. Defaults to * 1.0.0. */ version?: string; } /** * Creates an MCP Client Host that connects to one or more MCP servers. * Each server is defined in the `mcpClients` option, where the key is a * client-side name for the server and the value is the server's configuration. * * By default, all servers in the config will be attempted to connect unless * their configuration includes `{disabled: true}`. * * ```ts * const clientHost = createMcpHost({ * name: "my-mcp-client-host", // Name for the host itself * mcpServers: { * // Each key is a name for this client/server configuration * // Each value is an McpServerConfig object * gitToolServer: { command: "uvx", args: ["mcp-server-git"] }, * customApiServer: { url: "http://localhost:1234/mcp" } * } * }); * ``` * * @param options Configuration for the MCP Client Host, including the definitions of MCP servers to connect to. * @returns A new instance of GenkitMcpHost. */ declare function createMcpHost(options: McpHostOptions): GenkitMcpHost; /** * Creates an MCP Client Host that connects to one or more MCP servers. * Each server is defined in the `mcpClients` option, where the key is a * client-side name for the server and the value is the server's configuration. * * By default, all servers in the config will be attempted to connect unless * their configuration includes `{disabled: true}`. * * ```ts * const clientHost = defineMcpHost(ai, { * name: "my-mcp-client-host", // Name for the host itself * mcpServers: { * // Each key is a name for this client/server configuration * // Each value is an McpServerConfig object * gitToolServer: { command: "uvx", args: ["mcp-server-git"] }, * customApiServer: { url: "http://localhost:1234/mcp" } * } * }); * ``` * * @param options Configuration for the MCP Client Host, including the definitions of MCP servers to connect to. * @returns A new instance of GenkitMcpHost. */ declare function defineMcpHost(ai: Genkit, options: McpHostOptionsWithCache): GenkitMcpHost; /** * Creates an MCP Client that connects to a single MCP server. * This is useful when you only need to interact with one MCP server, * or if you want to manage client instances individually. * * ```ts * const client = createMcpClient({ * name: "mySingleMcpClient", // A name for this client instance * command: "npx", // Example: Launching a local server * args: ["-y", "@modelcontextprotocol/server-everything", "/path/to/allowed/dir"], * }); * * // To get tools from this client: * // const tools = await client.getActiveTools(ai); * ``` * * @param options Configuration for the MCP Client, defining how it connects * to the MCP server and its behavior. * @returns A new instance of GenkitMcpClient. */ declare function createMcpClient(options: McpClientOptions): GenkitMcpClient; /** * Defines an MCP Client that connects to a single MCP server. * This is useful when you only need to interact with one MCP server, * or if you want to manage client instances individually. * * ```ts * const client = defineMcpClient(ai, { * name: "mySingleMcpClient", // A name for this client instance * command: "npx", // Example: Launching a local server * args: ["-y", "@modelcontextprotocol/server-everything", "/path/to/allowed/dir"], * }); * * // To get tools from this client: * // const tools = await client.getActiveTools(ai); * * // Or in a generate call you can use: * ai.generate({ prompt: ``, tools: ['mySingleMcpClient:tool/*'], }); * ``` * * @param options Configuration for the MCP Client, defining how it connects * to the MCP server and its behavior. * @returns A new instance of GenkitMcpClient. */ declare function defineMcpClient(ai: Genkit, options: McpClientOptionsWithCache): GenkitMcpClient; /** * Creates an MCP server based on the supplied Genkit instance. All tools and prompts * will be automatically converted to MCP compatibility. * * ```ts * const mcpServer = createMcpServer(ai, {name: 'my-mcp-server', version: '0.1.0'}); * * await mcpServer.start(); // starts a stdio transport, OR * await mcpServer.start(customMcpTransport); // starts server using supplied transport * ``` * * @param ai Your Genkit instance with registered tools and prompts. * @param options Configuration metadata for the server. * @returns GenkitMcpServer instance. */ declare function createMcpServer(ai: Genkit, options: McpServerOptions): GenkitMcpServer; export { GenkitMcpServer, type McpServerOptions as M, createMcpHost as a, createMcpServer as b, createMcpClient as c, defineMcpClient as d, defineMcpHost as e };