import '@modelcontextprotocol/sdk/client/sse.js'; import '@modelcontextprotocol/sdk/client/stdio.js'; import '@modelcontextprotocol/sdk/shared/transport.js'; import { McpServerConfig, GenkitMcpClient } from './client.js'; import { Root } from '@modelcontextprotocol/sdk/types.js'; import { DynamicActionProviderAction, Genkit, MultipartToolAction, ToolAction, DynamicResourceAction, ExecutablePrompt, PromptGenerateOptions } from 'genkit'; import '@modelcontextprotocol/sdk/client/index.js'; import '@modelcontextprotocol/sdk/client/streamableHttp.js'; /** * Copyright 2025 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 McpHostOptions { /** * An optional client name for this MCP host. This name is advertised to MCP Servers * as the connecting client name. Defaults to 'genkit-mcp'. */ name?: string; /** * An optional version for this MCP host. Primarily for * logging and identification within Genkit. * Defaults to '1.0.0'. */ version?: string; /** * A record for configuring multiple MCP servers. Each server connection is * controlled by a `GenkitMcpClient` instance managed by `GenkitMcpHost`. * The key in the record is used as the identifier for the MCP server. */ mcpServers?: Record; /** * If true, tool responses from the MCP server will be returned in their raw * MCP format. Otherwise (default), they are processed and potentially * simplified for better compatibility with Genkit's typical data structures. */ rawToolResponses?: boolean; /** If true, tools will be registered as multipart tool.v2 actions. */ multipart?: M; /** * When provided, each connected MCP server will be sent the roots specified here. Overridden by any specific roots sent in the `mcpServers` config for a given server. */ roots?: Root[]; } type McpHostOptionsWithCache = Omit, 'name'> & { /** * A client name for this MCP host. This name is advertised to MCP Servers * as the connecting client name. */ name: string; /** * Cache TTL. The dynamic action provider has a cache for the available actions * The default TTL is 3 seconds. * The cache will automatically be invalidated if any connections change. * Negative = no caching (expect noisy traces and slower resolution) * Zero or undefined = use the default 3000 millis (3 seconds) * Positive: The number of milliseconds to keep the cache. */ cacheTTLMillis?: number; }; /** * Manages connections to multiple MCP (Model Context Protocol) servers. * Each server connection is individually configured and managed by an instance of `GenkitMcpClient`. * This host provides a centralized way to initialize, update, and interact with these clients. * * It allows for dynamic registration of tools from all connected and enabled MCP servers * into a Genkit instance. */ declare class GenkitMcpHost { name: string; private _clients; private _clientStates; private _readyListeners; private _ready; private _dynamicActionProvider; private roots; rawToolResponses?: boolean; multipart?: Multipart; constructor(options: McpHostOptions); set dynamicActionProvider(dap: DynamicActionProviderAction); _invalidateCache(): void; /** * Returns a Promise that resolves when the host has attempted to connect * to all configured clients, or rejects if a critical error occurs during * the initial connection phase. */ ready(): Promise; /** * Connects to a single MCP server defined by the provided configuration. * If a server with the same name already exists, it will be disconnected first. * Stores the client and transport references internally. Handles connection errors * by marking the server as disabled. * @param serverName The name to assign to this server connection. * @param config The configuration object for the server. */ connect(serverName: string, config: McpServerConfig): Promise; /** * Disconnects the specified MCP server and removes its registration * from this client instance. * @param serverName The name of the server to disconnect. */ disconnect(serverName: string): Promise; /** * Temporarily disables a server connection. Closes the underlying transport * but retains the server's configuration. Does nothing if the server is * already disabled. * @param serverName The name of the server to disable. */ disable(serverName: string): Promise; /** * Enables a server connection, including previously disabled ones. Attempts to reconnect * using the stored transport. Does nothing if the server is not disabled. * @param serverName The name of the server to re-enable. */ enable(serverName: string): Promise; /** * Closes and then restarts the transport connection for the specified server. * Useful for attempting to recover from connection issues without full * reconfiguration. * @param serverName The name of the server to reconnect. */ reconnect(serverName: string): Promise; /** * Updates the connections based on a provided map of server configurations. * - Connects any new servers defined in `mcpServers`. * - Disconnects any servers currently connected but not present in `mcpServers`. * - Reconnects existing servers if their configuration appears to have changed (implicitly handled by `connectServer`). * Sets the client's ready state once all connection attempts are complete. * @param mcpServers A record mapping server names to their configurations. */ updateServers(mcpServers: Record): void; /** * Retrieves all tools from all connected and enabled MCP clients managed by * this instance. This method waits for the host to be ready (all initial * connection attempts made) before fetching tools. * * It iterates through each managed `GenkitMcpClient`, and if the client is * not disabled, it calls the client's `getTools` method to fetch its * available tools. These are then aggregated into a single array. * * This is useful for dynamically providing a list of all available MCP tools * to Genkit, for example, when setting up a Genkit plugin. * * ```ts * const mcpHost = createMcpHost({ ... }); * // In your Genkit configuration: * // const allMcpTools = await McpHost.getActiveTools(ai); * // Then, these tools can be used or registered with Genkit. * ``` * * @param ai The Genkit instance, used by individual clients to define dynamic * tools. * @returns A Promise that resolves to an array of `ToolAction` from all * active MCP clients. */ getActiveTools(ai: Genkit): Promise<(Multipart extends true ? MultipartToolAction : ToolAction)[]>; /** * Retrieves all resources from all connected and enabled MCP clients managed by * this instance. This method waits for the host to be ready (all initial * connection attempts made) before fetching resources. * * It iterates through each managed `GenkitMcpClient`, and if the client is * not disabled, it calls the client's `getActiveResources` method to fetch its * available resources. These are then aggregated into a single array. * * This is useful for dynamically providing a list of all available MCP resources * to Genkit, for example, when setting up a Genkit plugin. * * @param ai The Genkit instance, used by individual clients to define dynamic * resources. * @returns A Promise that resolves to an array of `DynamicResourceAction` from all * active MCP clients. */ getActiveResources(ai: Genkit): Promise; /** * Retrieves all prompts from all connected and enabled MCP clients managed by * this instance. This method waits for the host to be ready (all initial * connection attempts made) before fetching prompts. * * It iterates through each managed `GenkitMcpClient`, and if the client is * not disabled, it calls the client's `getActivePrompts` method to fetch its * available prompts. These are then aggregated into a single array. * * This is useful for dynamically providing a list of all available MCP prompts * to Genkit, for example, when setting up a Genkit plugin. * * @param ai The Genkit instance, used by individual clients to define dynamic * prompts. * @returns A Promise that resolves to an array of `ExecutablePrompt` from all * active MCP clients. */ getActivePrompts(ai: Genkit): Promise; /** * Get the specified prompt as an `ExecutablePrompt` available through the * specified server. If no such prompt is found, return undefined. */ getPrompt(ai: Genkit, serverName: string, promptName: string, opts?: PromptGenerateOptions): Promise | undefined>; close(): Promise; /** Helper method to track and log client errors. */ private setError; private hasError; /** * Returns an array of all active clients. */ get activeClients(): GenkitMcpClient[]; /** * Returns the client by name. */ getClient(name: string): GenkitMcpClient; } export { GenkitMcpHost, type McpHostOptions, type McpHostOptionsWithCache };