MCP (Model Context Protocol)
The MCP plugin supports both sides of the Model Context Protocol:
- Client — Connect your Genkit app to external MCP servers (filesystem, databases, APIs) and use their tools with AI models.
- Server — Expose your Genkit tools to external MCP clients like Claude Desktop.
Installation
Section titled “Installation”<dependency> <groupId>com.google.genkit</groupId> <artifactId>genkit-plugin-mcp</artifactId> <version>1.0.0-SNAPSHOT</version></dependency>MCP Client — Connecting to external servers
Section titled “MCP Client — Connecting to external servers”Use MCPPlugin to connect your Genkit app to one or more external MCP servers. The plugin automatically discovers tools from each server and makes them available as Genkit tools.
STDIO transport (local processes)
Section titled “STDIO transport (local processes)”Connect to MCP servers that run as local processes communicating via stdin/stdout:
import com.google.genkit.plugins.mcp.MCPPlugin;import com.google.genkit.plugins.mcp.MCPPluginOptions;import com.google.genkit.plugins.mcp.MCPServerConfig;
MCPPlugin mcpPlugin = new MCPPlugin(MCPPluginOptions.builder() .addServer("filesystem", MCPServerConfig.stdio( "npx", "-y", "@modelcontextprotocol/server-filesystem", "/tmp")) .build());
Genkit genkit = Genkit.builder() .plugin(mcpPlugin) .build();HTTP/SSE transport (remote servers)
Section titled “HTTP/SSE transport (remote servers)”Connect to remote MCP servers over HTTP with Server-Sent Events:
MCPPlugin mcpPlugin = new MCPPlugin(MCPPluginOptions.builder() .addServer("my-server", MCPServerConfig.http("https://my-mcp-server.com/sse")) .build());Streamable HTTP transport
Section titled “Streamable HTTP transport”For servers supporting the modern Streamable HTTP transport:
MCPPlugin mcpPlugin = new MCPPlugin(MCPPluginOptions.builder() .addServer("my-server", MCPServerConfig.streamableHttp("https://my-mcp-server.com/mcp")) .build());Using MCP tools with AI models
Section titled “Using MCP tools with AI models”Once connected, MCP tools are automatically registered as Genkit tools. AI models can use them directly:
ModelResponse response = genkit.generate(GenerateOptions.builder() .model("openai/gpt-4o-mini") .prompt("List all files in the /tmp directory") .tools("filesystem/list_directory", "filesystem/read_file") .build());Convenience factory methods
Section titled “Convenience factory methods”For quick single-server setups:
// STDIOGenkit genkit = Genkit.builder() .plugin(MCPPlugin.stdio("filesystem", "npx", "-y", "@modelcontextprotocol/server-filesystem", "/tmp")) .build();
// HTTPGenkit genkit = Genkit.builder() .plugin(MCPPlugin.http("my-server", "https://my-mcp-server.com/sse")) .build();Multi-server setup
Section titled “Multi-server setup”Connect to multiple MCP servers simultaneously:
MCPPlugin mcpPlugin = new MCPPlugin(MCPPluginOptions.builder() .addServer("filesystem", MCPServerConfig.stdio( "npx", "-y", "@modelcontextprotocol/server-filesystem", "/tmp")) .addServer("database", MCPServerConfig.http("https://db-mcp-server.com/sse")) .addServer("github", MCPServerConfig.stdio( "npx", "-y", "@modelcontextprotocol/server-github")) .build());Tools from each server are namespaced: filesystem/read_file, database/query, github/list_repos.
Direct tool invocation
Section titled “Direct tool invocation”Call MCP tools directly without going through an AI model:
Object result = mcpPlugin.callTool("filesystem", "read_file", Map.of("path", "/tmp/example.txt"));Accessing MCP resources
Section titled “Accessing MCP resources”Read resources exposed by MCP servers:
List<MCPResource> resources = mcpPlugin.getResources("filesystem");
MCPResourceContent content = mcpPlugin.readResource("filesystem", "file:///tmp/example.txt");Client configuration options
Section titled “Client configuration options”| Option | Default | Description |
|---|---|---|
name | "genkit-mcp" | Client name for identification |
requestTimeout | 30 seconds | Timeout per MCP request |
rawToolResponses | false | Return raw MCP responses |
Cleanup
Section titled “Cleanup”Disconnect from all MCP servers when done:
mcpPlugin.disconnect();MCP Server — Exposing Genkit tools
Section titled “MCP Server — Exposing Genkit tools”Use MCPServer to expose your Genkit tools to external MCP clients (like Claude Desktop, Cursor, or other MCP-compatible apps).
Creating an MCP server
Section titled “Creating an MCP server”import com.google.genkit.Genkit;import com.google.genkit.plugins.mcp.MCPServer;import com.google.genkit.plugins.mcp.MCPServerOptions;
Genkit genkit = Genkit.builder().build();
// Define toolsgenkit.defineTool("calculator", "Performs arithmetic", Map.of("expression", String.class), String.class, (ctx, input) -> { // evaluate expression... return result; });
genkit.defineTool("get_weather", "Gets current weather", Map.of("city", String.class), String.class, (ctx, input) -> { return "Sunny, 22°C in " + input.get("city"); });
// Create and start the MCP serverMCPServer server = new MCPServer(genkit.getRegistry(), MCPServerOptions.builder() .name("my-genkit-tools") .version("1.0.0") .build());
// Start with STDIO transport (default)server.start();The server automatically discovers all tools registered in the Genkit registry and exposes them via the MCP protocol.
Using with Claude Desktop
Section titled “Using with Claude Desktop”Add this to your Claude Desktop configuration (~/Library/Application Support/Claude/claude_desktop_config.json on macOS):
{ "mcpServers": { "genkit-tools": { "command": "java", "args": ["-jar", "/path/to/your-app.jar"] } }}Custom transport
Section titled “Custom transport”By default, the server uses STDIO transport. You can provide a custom transport provider:
server.start(customTransportProvider);Server options
Section titled “Server options”| Option | Default | Description |
|---|---|---|
name | "genkit-mcp-server" | Server name exposed to clients |
version | "1.0.0" | Server version |
Popular MCP servers
Section titled “Popular MCP servers”| Server | Install | Description |
|---|---|---|
| Filesystem | npx @modelcontextprotocol/server-filesystem | Read/write local files |
| GitHub | npx @modelcontextprotocol/server-github | GitHub API access |
| Postgres | npx @modelcontextprotocol/server-postgres | PostgreSQL queries |
| Brave Search | npx @modelcontextprotocol/server-brave-search | Web search |
| Everything | npx @modelcontextprotocol/server-everything | Demo/test server |
Browse more at mcp.so/servers.
Sample
Section titled “Sample”See the mcp sample for client and server examples.