Class MCPPlugin

java.lang.Object
com.google.genkit.plugins.mcp.MCPPlugin
All Implemented Interfaces:
Plugin

public class MCPPlugin extends Object implements Plugin
MCP Plugin for Genkit.

This plugin enables Genkit to connect to MCP (Model Context Protocol) servers and use their tools. MCP provides a standardized way for AI applications to interact with external tools and data sources.

Features:

  • Connect to multiple MCP servers (STDIO or HTTP transports)
  • Automatic conversion of MCP tools to Genkit tools
  • Access MCP resources programmatically
  • Support for tool caching and lazy loading

Example usage:


 // Create the MCP plugin with server configurations
 MCPPlugin mcpPlugin = MCPPlugin.create(MCPPluginOptions.builder().name("my-mcp-host")
 		.addServer("filesystem",
 				MCPServerConfig.stdio("npx", "-y", "@modelcontextprotocol/server-filesystem", "/tmp"))
 		.addServer("weather", MCPServerConfig.http("http://localhost:3001/mcp")).build());

 // Create Genkit with the MCP plugin
 Genkit genkit = Genkit.builder().plugin(mcpPlugin).build();

 // Use MCP tools in flows
 Flow<String, String, Void> myFlow = genkit.defineFlow("myFlow", String.class, String.class, (ctx, input) -> {
 	// MCP tools are available as: "serverName/toolName"
 	// e.g., "filesystem/readFile", "weather/getForecast"
 	ModelResponse response = genkit.generate(
 			GenerateOptions.builder().model("openai/gpt-4o").prompt(input).tools(mcpPlugin.getTools()).build());
 	return response.getText();
 });
 
See Also:
  • Constructor Details

    • MCPPlugin

      public MCPPlugin(MCPPluginOptions options)
      Creates a new MCP plugin with the given options.
      Parameters:
      options - the plugin options
  • Method Details

    • create

      public static MCPPlugin create(MCPPluginOptions options)
      Creates an MCP plugin with the given options.
      Parameters:
      options - the plugin options
      Returns:
      a new MCPPlugin
    • stdio

      public static MCPPlugin stdio(String serverName, String command, String... args)
      Creates an MCP plugin with a single STDIO server.
      Parameters:
      serverName - the name for the server
      command - the command to execute
      args - the command arguments
      Returns:
      a new MCPPlugin
    • http

      public static MCPPlugin http(String serverName, String url)
      Creates an MCP plugin with a single HTTP server.
      Parameters:
      serverName - the name for the server
      url - the server URL
      Returns:
      a new MCPPlugin
    • getName

      public String getName()
      Description copied from interface: Plugin
      Returns the unique identifier for the plugin. This name is used for registration and lookup.
      Specified by:
      getName in interface Plugin
      Returns:
      the plugin name
    • init

      public List<Action<?,?,?>> init()
      Description copied from interface: Plugin
      Initializes the plugin. This method is called once during Genkit initialization. The plugin should return a list of actions that it provides.
      Specified by:
      init in interface Plugin
      Returns:
      list of actions provided by this plugin
    • init

      public List<Action<?,?,?>> init(Registry registry)
      Description copied from interface: Plugin
      Initializes the plugin with access to the registry. This method is called once during Genkit initialization. The plugin should return a list of actions that it provides.

      Override this method instead of Plugin.init() when your plugin needs to resolve dependencies from the registry (e.g., embedders, models).

      Specified by:
      init in interface Plugin
      Parameters:
      registry - the Genkit registry for resolving dependencies
      Returns:
      list of actions provided by this plugin
    • getTools

      public List<Tool<?,?>> getTools()
      Gets all tools from all connected MCP servers.
      Returns:
      list of tools
    • getTools

      public List<Tool<?,?>> getTools(String serverName) throws GenkitException
      Gets tools from a specific MCP server.
      Parameters:
      serverName - the server name
      Returns:
      list of tools from that server
      Throws:
      GenkitException - if the server is not found or not connected
    • getResources

      public List<MCPResource> getResources(String serverName) throws GenkitException
      Gets resources from a specific MCP server.
      Parameters:
      serverName - the server name
      Returns:
      list of resources
      Throws:
      GenkitException - if the server is not found or not connected
    • readResource

      public MCPResourceContent readResource(String serverName, String uri) throws GenkitException
      Reads a resource from an MCP server.
      Parameters:
      serverName - the server name
      uri - the resource URI
      Returns:
      the resource content
      Throws:
      GenkitException - if reading fails
    • callTool

      public Object callTool(String serverName, String toolName, Map<String,Object> arguments) throws GenkitException
      Calls an MCP tool directly.
      Parameters:
      serverName - the server name
      toolName - the tool name (without server prefix)
      arguments - the tool arguments
      Returns:
      the tool result
      Throws:
      GenkitException - if the call fails
    • getClient

      public MCPClient getClient(String serverName)
      Gets the client for a specific server.
      Parameters:
      serverName - the server name
      Returns:
      the client, or null if not found
    • getClients

      public Map<String,MCPClient> getClients()
      Gets all connected clients.
      Returns:
      map of server name to client
    • disconnect

      public void disconnect()
      Disconnects all MCP clients.
    • isInitialized

      public boolean isInitialized()
      Checks if the plugin is initialized.
      Returns:
      true if initialized
    • getOptions

      public MCPPluginOptions getOptions()
      Gets the plugin options.
      Returns:
      the options