This sample demonstrates how to use the Genkit MCP (Model Context Protocol) plugin to integrate with MCP servers.
- Java 21+
- Maven 3.6+
- Node.js and npm (required for running MCP servers via
npx) - OpenAI API key
# Set your OpenAI API key
export OPENAI_API_KEY=your-api-key-here
# Navigate to the sample directory
cd java/samples/mcp
# Run the sample
./run.sh
# Or: mvn compile exec:java# Set your OpenAI API key
export OPENAI_API_KEY=your-api-key-here
# Navigate to the sample directory
cd java/samples/mcp
# Run with Genkit CLI
genkit start -- ./run.shThe Dev UI will be available at http://localhost:4000
The sample will start with:
- HTTP server on port 8080
- Reflection server on port 3100 (for Genkit Dev UI)
- MCP connections to filesystem and everything servers
Lists all available MCP tools from connected servers.
curl -X POST http://localhost:8080/listMcpTools \
-H 'Content-Type: application/json' -d 'null'AI-powered file operations assistant that can read, write, and list files.
curl -X POST http://localhost:8080/fileAssistant \
-H 'Content-Type: application/json' \
-d '"List all files in the temp directory"'Directly read a file using the MCP filesystem tool.
curl -X POST http://localhost:8080/readFile \
-H 'Content-Type: application/json' \
-d '"/tmp/test.txt"'List resources from a specific MCP server.
curl -X POST http://localhost:8080/listResources \
-H 'Content-Type: application/json' \
-d '"filesystem"'AI assistant with access to all MCP tools.
curl -X POST http://localhost:8080/toolExplorer \
-H 'Content-Type: application/json' \
-d '"Generate a random UUID"'Get the status of all connected MCP servers.
curl -X POST http://localhost:8080/mcpStatus \
-H 'Content-Type: application/json' -d 'null'Demo that writes content to a file and reads it back.
curl -X POST http://localhost:8080/writeReadDemo \
-H 'Content-Type: application/json' \
-d '"Hello from Genkit MCP!"'| Variable | Description | Default |
|---|---|---|
OPENAI_API_KEY |
OpenAI API key for model access | (required) |
MCP_ALLOWED_DIR |
Directory the filesystem server can access | /tmp |
This sample connects to two MCP servers:
-
filesystem (
@modelcontextprotocol/server-filesystem)- Provides file read/write/list operations
- Limited to the
MCP_ALLOWED_DIRdirectory for security
-
everything (
@modelcontextprotocol/server-everything)- Demo server with various tool types (echo, random, etc.)
- Useful for testing MCP integration
This sample also includes MCPServerSample, which demonstrates how to expose Genkit tools as an MCP server.
# Run directly
mvn exec:java -Dexec.mainClass="com.google.genkit.samples.MCPServerSample"
# Or build and run the JAR
mvn package -DskipTests
java -jar target/genkit-mcp-sample-1.0.0-SNAPSHOT-server.jarAdd to your ~/.config/claude/claude_desktop_config.json (macOS) or similar on other platforms:
{
"mcpServers": {
"genkit-tools": {
"command": "java",
"args": ["-jar", "/absolute/path/to/genkit-mcp-sample-1.0.0-SNAPSHOT-server.jar"]
}
}
}The MCP server exposes these demonstration tools:
| Tool | Description |
|---|---|
calculator |
Basic math operations (add, subtract, multiply, divide) |
get_weather |
Mock weather data for any location |
get_datetime |
Current date/time in various formats |
greet |
Personalized greeting generator |
translate_mock |
Mock translation tool |
MCPPluginOptions mcpOptions = MCPPluginOptions.builder()
.name("my-app")
.addServer("filesystem", MCPServerConfig.stdio(
"npx", "-y", "@modelcontextprotocol/server-filesystem", "/path/to/files"))
.addServer("github", MCPServerConfig.builder()
.command("npx")
.args("-y", "@modelcontextprotocol/server-github")
.env("GITHUB_TOKEN", System.getenv("GITHUB_TOKEN"))
.build())
.addServer("remote", MCPServerConfig.http("http://mcp-server.example.com:3001/mcp"))
.build();
MCPPlugin mcpPlugin = MCPPlugin.create(mcpOptions);// Define tools with Genkit
Genkit genkit = Genkit.builder().build();
genkit.defineTool("my_tool", "My custom tool",
Map.of("type", "object", "properties", Map.of(
"input", Map.of("type", "string")
)),
(Class<Map<String, Object>>) (Class<?>) Map.class,
(ctx, input) -> {
return Map.of("result", "processed: " + input.get("input"));
});
// Create and start MCP server
MCPServer mcpServer = new MCPServer(genkit.getRegistry(),
MCPServerOptions.builder()
.name("my-server")
.version("1.0.0")
.build());
mcpServer.start(); // Uses STDIO transport
(ctx, input) -> {
List<Tool<?, ?>> tools = mcpPlugin.getTools();
ModelResponse response = genkit.generate(GenerateOptions.builder()
.model("openai/gpt-4o")
.prompt(input)
.tools(tools)
.build());
return response.getText();
});// Write a file
mcpPlugin.callTool("filesystem", "write_file",
Map.of("path", "/tmp/hello.txt", "content", "Hello World!"));
// Read it back
Object content = mcpPlugin.callTool("filesystem", "read_file",
Map.of("path", "/tmp/hello.txt"));Ensure Node.js and npm are installed and in your PATH.
Check that the MCP server package can be downloaded via npm. You may need to configure proxy settings.
Make sure the MCP_ALLOWED_DIR directory exists and is writable.
Check the logs for MCP connection errors. Increase log level for more details:
<logger name="com.google.genkit.plugins.mcp" level="DEBUG"/>