MCP (Model Context Protocol) plugin
The MCP (Model Context Protocol) plugin enables integration with MCP servers and allows you to expose Genkit tools as MCP servers. You can connect to external MCP servers to use their tools and prompts, manage multiple server connections, or turn your Genkit application into an MCP server.
Prerequisites
Section titled “Prerequisites”This plugin requires MCP servers to be available. For testing and development, you can use:
mcp-server-time
- Spmple server Exposing time operations@modelcontextprotocol/server-everything
- A comprehensive MCP server for testing- Custom MCP servers written in Python, TypeScript, or other languages
Configuration
Section titled “Configuration”Single Server Connection
Section titled “Single Server Connection”To connect to a single MCP server, import the mcp
package and create a GenkitMCPClient
:
import "github.com/firebase/genkit/go/plugins/mcp"
ctx := context.Background()g, err := genkit.Init(ctx)if err != nil { log.Fatal(err)}
client, err := mcp.NewGenkitMCPClient(mcp.MCPClientOptions{ Name: "mcp-server-time", Stdio: &mcp.StdioConfig{ Command: "uvx", Args: []string{"mcp-server-time"}, },})if err != nil { log.Fatal(err)}
Multiple Server Management
Section titled “Multiple Server Management”To manage connections to multiple MCP servers, use GenkitMCPManager
:
import "github.com/firebase/genkit/go/plugins/mcp"
manager, err := mcp.NewMCPManager(mcp.MCPManagerOptions{ Name: "my-app", MCPServers: []mcp.MCPServerConfig{ { Name: "everything-server", Config: mcp.MCPClientOptions{ Name: "everything-server", Stdio: &mcp.StdioConfig{ Command: "npx", Args: []string{"-y", "@modelcontextprotocol/server-everything"}, }, }, }, { Name: "mcp-server-time", Config: mcp.MCPClientOptions{ Name: "mcp-server-time", Stdio: &mcp.StdioConfig{ Command: "uvx", Args: []string{"mcp-server-time"}, }, }, }, },})if err != nil { log.Fatal(err)}
Exposing as MCP Server
Section titled “Exposing as MCP Server”To expose your Genkit tools as an MCP server, create an MCPServer
:
import "github.com/firebase/genkit/go/plugins/mcp"
// Define your tools firstaddTool := genkit.DefineTool(g, "add", "Add two numbers", func(ctx *ai.ToolContext, input struct{A, B int}) (int, error) { return input.A + input.B, nil })
// Create MCP serverserver := mcp.NewMCPServer(g, mcp.MCPServerOptions{ Name: "genkit-calculator", Version: "1.0.0",})
Using Tools from MCP Servers
Section titled “Using Tools from MCP Servers”Once connected to an MCP server, you can retrieve and use its tools:
// Get a specific toolechoTool, err := client.GetTool(ctx, g, "echo")if err != nil { log.Fatal(err)}
// Use the tool in your workflowresp, err := genkit.Generate(ctx, g, ai.WithModel(myModel), ai.WithPrompt("Use the echo tool to repeat this message"), ai.WithTools(echoTool),)if err != nil { log.Fatal(err)}
Using Prompts from MCP Servers
Section titled “Using Prompts from MCP Servers”Retrieve and use prompts from connected MCP servers:
// Get a specific promptsimplePrompt, err := client.GetPrompt(ctx, g, "simple_prompt")if err != nil { log.Fatal(err)}
// Use the promptresp, err := genkit.Generate(ctx, g, ai.WithModel(myModel), ai.WithPrompt(simplePrompt),)
Managing Multiple Servers
Section titled “Managing Multiple Servers”With GenkitMCPManager
, you can dynamically manage server connections:
// Connect to a new server at runtimeerr = manager.Connect("weather", mcp.MCPClientOptions{ Name: "weather-server", Stdio: &mcp.StdioConfig{ Command: "python", Args: []string{"weather_server.py"}, },})if err != nil { log.Fatal(err)}
// Disconnect a server completelyerr = manager.Disconnect("weather")if err != nil { log.Fatal(err)}
// Get all tools from all active serverstools, err := manager.GetActiveTools(ctx, g)if err != nil { log.Fatal(err)}
// Get a specific prompt from a specific serverprompt, err := manager.GetPrompt(ctx, g, "mcp-server-time", "current_time", nil)if err != nil { log.Fatal(err)}
For individual client management (disable/enable without disconnecting), you would access the clients directly. The manager focuses on connection lifecycle management.
Running as MCP Server
Section titled “Running as MCP Server”To run your Genkit application as an MCP server:
// Option 1: Auto-expose all defined toolsserver := mcp.NewMCPServer(g, mcp.MCPServerOptions{ Name: "genkit-calculator", Version: "1.0.0",})
// Option 2: Expose only specific toolsserver = mcp.NewMCPServer(g, mcp.MCPServerOptions{ Name: "genkit-calculator", Version: "1.0.0", Tools: []ai.Tool{addTool, multiplyTool},})
// Start the MCP serverlog.Println("Starting MCP server...")if err := server.ServeStdio(ctx); err != nil { log.Fatal(err)}
Transport Options
Section titled “Transport Options”Stdio Transport
Section titled “Stdio Transport”You can use either Stdio or SSE
Stdio: &mcp.StdioConfig{ Command: "uvx", Args: []string{"mcp-server-time"}, Env: []string{"DEBUG=1"},}
SSE: &mcp.SSEConfig{ BaseURL: "http://localhost:3000/sse",}
Testing
Section titled “Testing”Testing Your MCP Server
Section titled “Testing Your MCP Server”To test your Genkit application as an MCP server:
# Run your servergo run main.go
# Test with MCP Inspector in another terminalnpx @modelcontextprotocol/inspector go run main.go
Configuration Options
Section titled “Configuration Options”MCPClientOptions
Section titled “MCPClientOptions”type MCPClientOptions struct { Name string // Server identifier Version string // Version number (defaults to "1.0.0") Disabled bool // Disabled flag to temporarily disable this client Stdio *StdioConfig // Stdio transport config SSE *SSEConfig // SSE transport config}
StdioConfig
Section titled “StdioConfig”type StdioConfig struct { Command string // Command to run Args []string // Command arguments Env []string // Environment variables}
MCPServerConfig
Section titled “MCPServerConfig”type MCPServerConfig struct { Name string // Name for this server Config MCPClientOptions // Client configuration options}
MCPManagerOptions
Section titled “MCPManagerOptions”type MCPManagerOptions struct { Name string // Manager instance name Version string // Manager version (defaults to "1.0.0") MCPServers []MCPServerConfig // Array of server configurations}
MCPServerOptions
Section titled “MCPServerOptions”type MCPServerOptions struct { Name string // Server name Version string // Server version Tools []ai.Tool // Specific tools to expose (optional)}