Package com.google.genkit.plugins.mcp
Class MCPPlugin
java.lang.Object
com.google.genkit.plugins.mcp.MCPPlugin
- All Implemented Interfaces:
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 Summary
ConstructorsConstructorDescriptionMCPPlugin(MCPPluginOptions options) Creates a new MCP plugin with the given options. -
Method Summary
Modifier and TypeMethodDescriptionCalls an MCP tool directly.static MCPPlugincreate(MCPPluginOptions options) Creates an MCP plugin with the given options.voidDisconnects all MCP clients.Gets the client for a specific server.Gets all connected clients.getName()Returns the unique identifier for the plugin.Gets the plugin options.getResources(String serverName) Gets resources from a specific MCP server.getTools()Gets all tools from all connected MCP servers.Gets tools from a specific MCP server.static MCPPluginCreates an MCP plugin with a single HTTP server.init()Initializes the plugin.Initializes the plugin with access to the registry.booleanChecks if the plugin is initialized.readResource(String serverName, String uri) Reads a resource from an MCP server.static MCPPluginCreates an MCP plugin with a single STDIO server.
-
Constructor Details
-
MCPPlugin
Creates a new MCP plugin with the given options.- Parameters:
options- the plugin options
-
-
Method Details
-
create
Creates an MCP plugin with the given options.- Parameters:
options- the plugin options- Returns:
- a new MCPPlugin
-
stdio
Creates an MCP plugin with a single STDIO server.- Parameters:
serverName- the name for the servercommand- the command to executeargs- the command arguments- Returns:
- a new MCPPlugin
-
http
Creates an MCP plugin with a single HTTP server.- Parameters:
serverName- the name for the serverurl- the server URL- Returns:
- a new MCPPlugin
-
getName
Description copied from interface:PluginReturns the unique identifier for the plugin. This name is used for registration and lookup. -
init
Description copied from interface:PluginInitializes the plugin. This method is called once during Genkit initialization. The plugin should return a list of actions that it provides. -
init
Description copied from interface:PluginInitializes 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). -
getTools
Gets all tools from all connected MCP servers.- Returns:
- list of tools
-
getTools
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
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
Reads a resource from an MCP server.- Parameters:
serverName- the server nameuri- 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 GenkitExceptionCalls an MCP tool directly.- Parameters:
serverName- the server nametoolName- the tool name (without server prefix)arguments- the tool arguments- Returns:
- the tool result
- Throws:
GenkitException- if the call fails
-
getClient
Gets the client for a specific server.- Parameters:
serverName- the server name- Returns:
- the client, or null if not found
-
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
Gets the plugin options.- Returns:
- the options
-