This plugin provides integration between Genkit and the Model Context Protocol (MCP), allowing you to consume MCP tools, resources, and prompts as a client, or expose your Genkit tools and prompts as an MCP server. MCP is an open standard that facilitates communication between clients and servers providing AI-related capabilities.
To get started with Genkit MCP, install the necessary packages:
npm i genkit @genkit-ai/mcp
There are two ways to connect to MCP servers: using a host for multiple servers or a client for a single server.
The createMcpHost
function lets you connect to multiple MCP servers at once:
import { googleAI } from '@genkit-ai/google-genai';
import { createMcpHost } from '@genkit-ai/mcp';
import { genkit } from 'genkit';
const mcpHost = createMcpHost({
name: 'myMcpClients',
mcpServers: {
fs: {
command: 'npx',
args: ['-y', '@modelcontextprotocol/server-filesystem', process.cwd()],
},
memory: {
command: 'npx',
args: ['-y', '@modelcontextprotocol/server-memory'],
},
},
});
const ai = genkit({
plugins: [googleAI()],
});
(async () => {
const { text } = await ai.generate({
model: googleAI.model('gemini-2.0-flash'),
prompt: `Analyze all files in ${process.cwd()}.`,
tools: await mcpHost.getActiveTools(ai),
resources: await mcpHost.getActiveResources(ai),
});
console.log(text);
await mcpHost.close();
})();
For connecting to just one MCP server, use createMcpClient
:
import { googleAI } from '@genkit-ai/google-genai';
import { createMcpClient } from '@genkit-ai/mcp';
import { genkit } from 'genkit';
const myFsClient = createMcpClient({
name: 'myFileSystemClient',
mcpServer: {
command: 'npx',
args: ['-y', '@modelcontextprotocol/server-filesystem', process.cwd()],
},
});
const ai = genkit({
plugins: [googleAI()],
});
(async () => {
await myFsClient.ready();
const fsTools = await myFsClient.getActiveTools(ai);
const { text } = await ai.generate({
model: googleAI.model('gemini-2.0-flash'),
prompt: 'List files in ' + process.cwd(),
tools: fsTools,
});
console.log(text);
await myFsClient.disable();
})();
When using createMcpHost
, you can configure:
name
: A name for the MCP host plugin (default: 'genkitx-mcp')version
: The version of the MCP host plugin (default: "1.0.0")rawToolResponses
: When true
, returns tool responses in raw MCP format (default: false
)mcpServers
: An object mapping server names to their configurationsServer configurations can include:
disabled
: If true
, this server connection will not be attempted (default: false
)command
and args
to launch a serverurl
for connecting via HTTPtransport
for an existing MCP transport objectFor createMcpClient
, the options are similar but focused on a single server connection.
MCP tools are available through:
mcpHost.getActiveTools(ai)
- gets tools from all connected serversmcpClient.getActiveTools(ai)
- gets tools from a specific clientMCP prompts can be fetched using:
mcpHost.getPrompt(serverName, promptName)
mcpClient.getPrompt(promptName)
All MCP actions are namespaced:
serverName/toolName
(e.g., fs/read_file
)clientName/toolName
(e.g., myFileSystemClient/list_resources
)You can expose your Genkit tools and prompts as an MCP server:
import { googleAI } from '@genkit-ai/google-genai';
import { createMcpServer } from '@genkit-ai/mcp';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { genkit, z } from 'genkit/beta';
const ai = genkit({
plugins: [googleAI()],
});
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;
}
);
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}}.`
);
// Define resources
ai.defineResource(
{
name: 'my resouces',
uri: 'my://resource',
},
async () => {
return {
content: [
{
text: 'my resource',
},
],
};
}
);
// Create and start the MCP server
const server = createMcpServer(ai, {
name: 'example_server',
version: '0.0.1',
});
server.setup().then(async () => {
await server.start();
const transport = new StdioServerTransport();
await server.server?.connect(transport);
});
You can test your MCP server using the official inspector:
npx @modelcontextprotocol/inspector dist/index.js
This inspector allows you to list and test prompts and actions manually.
user
and model
messages (no system
messages)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