Genkit MCP provides integration between Genkit and the Model Context Protocol (MCP), an open standard that enables interaction between clients and servers that provide tools, resources, and prompts. This plugin allows you to consume MCP capabilities from servers or expose your Genkit tools as an MCP server.
To install Genkit MCP, run:
npm i genkit @genkit-ai/mcp
The createMcpHost
function allows you to connect to multiple MCP servers simultaneously:
import { genkit } from 'genkit';
import { createMcpHost } from '@genkit-ai/mcp';
// Configure a MCP host with multiple servers
const ALLOWED_DIRS = ['/Users/yourusername/Desktop'];
const mcpHost = createMcpHost({
name: 'myMcpClients',
mcpServers: {
// File system server
fs: {
command: 'npx',
args: ['-y', '@modelcontextprotocol/server-filesystem', ...ALLOWED_DIRS],
},
// A remote server
git: {
url: 'http://localhost:8080/mcp',
// disabled: true, // Optionally disable a server
},
},
});
const ai = genkit({
plugins: [
/* your model providers and other plugins */
],
});
// Retrieve tools from all active MCP servers
const mcpTools = await mcpHost.getActiveTools(genkit);
// Use the MCP tools with your model
const response = await ai.generate({
model: googleAI.model('gemini-2.0-flash'),
prompt: 'What are the last 5 commits in the repo `/home/yourusername/Desktop/test-repo/?`',
tools: mcpTools,
});
console.log(response.text);
name
: Optional string to name the MCP host plugin (defaults to 'genkitx-mcp')version
: Optional version string (defaults to "1.0.0")rawToolResponses
: Boolean that determines if tool responses are returned in raw MCP format (defaults to false
)mcpServers
: Required object mapping server names to their configurations, which can include:
disabled
: Optional boolean to disable a server connectioncommand
(required) and args
(optional)env
(optional)url
string for HTTP connectionstransport
object for specialized connectionsFor connecting to just one MCP server:
import { genkit } from 'genkit';
import { createMcpClient } from '@genkit-ai/mcp';
const myFsClient = createMcpClient({
name: 'myFileSystemClient',
command: 'npx',
args: ['-y', '@modelcontextprotocol/server-everything', '/Users/yourusername/Documents'],
});
// Set up Genkit
const ai = genkit({
plugins: [
/* your plugins */
],
});
async function runSingleClient() {
// Get tools from this specific client
const fsTools = await myFsClient.getActiveTools(ai);
const response = await ai.generate({
model: googleAI.model('gemini-2.0-flash'),
prompt: 'List files in my Documents folder.',
tools: fsTools,
});
console.log(response.text);
}
runSingleClient();
name
: Required string for the client instance (used as namespace for tools)version
: Optional version string (defaults to "1.0.0")mcpServers
entries for createMcpHost
)To work with MCP tools and prompts:
// Get all available tools from MCP servers
const tools = await mcpHost.getActiveTools(genkit);
// Get a specific prompt from a server
const prompt = await mcpHost.getPrompt('serverName', 'promptName');
// For a single client, get tools and prompts directly
const clientTools = await myFsClient.getActiveTools(genkit);
const clientPrompt = await myFsClient.getPrompt('promptName');
All MCP actions are namespaced:
mcpHost
, tools are prefixed with the server name (e.g., fs/read_file
)myFileSystemClient/list_resources
)MCP tools return responses in a content
array format, which the plugin processes as follows:
You can expose your Genkit tools and prompts as an MCP server:
import { genkit, z } from 'genkit';
import { createMcpServer } from '@genkit-ai/mcp';
const ai = genkit({});
// Define a tool
ai.defineTool(
{
name: 'add',
description: 'add two numbers together',
inputSchema: z.object({ a: z.number(), b: z.number() }),
outputSchema: z.number(),
},
async ({ a, b }) => {
return a + b;
}
);
// Define a prompt
ai.definePrompt(
{
name: "happy",
description: "everybody together now",
input: {
schema: z.object({
action: z.string().default("clap your hands").optional(),
}),
},
},
`If you're happy and you know it, {{action}}.`
);
// Create and start an MCP server
const server = createMcpServer(ai, {
name: 'example_server',
version: '0.0.1'
});
// Set up and start the server
server.setup().then(() => server.start());
name
: Required string for identifying your serverversion
: Optional version string (defaults to "1.0.0")You can test your MCP server using the official inspector tool:
npx @modelcontextprotocol/inspector dist/index.js
This allows you to interactively explore and test the prompts and actions your server provides.
To add this MCP server to Claude Code, run this command in your terminal:
claude mcp add-json "genkit" '{"command":"npx","args":["-y","genkitx-mcp"]}'
See the official Claude Code MCP documentation for more details.
There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json
file so that it is available in all of your projects.
If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json
file.
To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".
When you click that button the ~/.cursor/mcp.json
file will be opened and you can add your server like this:
{
"mcpServers": {
"genkit": {
"command": "npx",
"args": [
"-y",
"genkitx-mcp"
]
}
}
}
To add an MCP server to a project you can create a new .cursor/mcp.json
file or add it to the existing one. This will look exactly the same as the global MCP server example above.
Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.
The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.
You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.
To add this MCP server to Claude Desktop:
1. Find your configuration file:
~/Library/Application Support/Claude/claude_desktop_config.json
%APPDATA%\Claude\claude_desktop_config.json
~/.config/Claude/claude_desktop_config.json
2. Add this to your configuration file:
{
"mcpServers": {
"genkit": {
"command": "npx",
"args": [
"-y",
"genkitx-mcp"
]
}
}
}
3. Restart Claude Desktop for the changes to take effect