Skip to content

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.
<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.

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();

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());

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());

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());

For quick single-server setups:

// STDIO
Genkit genkit = Genkit.builder()
.plugin(MCPPlugin.stdio("filesystem",
"npx", "-y", "@modelcontextprotocol/server-filesystem", "/tmp"))
.build();
// HTTP
Genkit genkit = Genkit.builder()
.plugin(MCPPlugin.http("my-server", "https://my-mcp-server.com/sse"))
.build();

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.

Call MCP tools directly without going through an AI model:

Object result = mcpPlugin.callTool("filesystem", "read_file",
Map.of("path", "/tmp/example.txt"));

Read resources exposed by MCP servers:

List<MCPResource> resources = mcpPlugin.getResources("filesystem");
MCPResourceContent content = mcpPlugin.readResource("filesystem",
"file:///tmp/example.txt");
OptionDefaultDescription
name"genkit-mcp"Client name for identification
requestTimeout30 secondsTimeout per MCP request
rawToolResponsesfalseReturn raw MCP responses

Disconnect from all MCP servers when done:

mcpPlugin.disconnect();

Use MCPServer to expose your Genkit tools to external MCP clients (like Claude Desktop, Cursor, or other MCP-compatible apps).

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 tools
genkit.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 server
MCPServer 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.

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"]
}
}
}

By default, the server uses STDIO transport. You can provide a custom transport provider:

server.start(customTransportProvider);
OptionDefaultDescription
name"genkit-mcp-server"Server name exposed to clients
version"1.0.0"Server version

ServerInstallDescription
Filesystemnpx @modelcontextprotocol/server-filesystemRead/write local files
GitHubnpx @modelcontextprotocol/server-githubGitHub API access
Postgresnpx @modelcontextprotocol/server-postgresPostgreSQL queries
Brave Searchnpx @modelcontextprotocol/server-brave-searchWeb search
Everythingnpx @modelcontextprotocol/server-everythingDemo/test server

Browse more at mcp.so/servers.

See the mcp sample for client and server examples.