Genkit MCP server

Consume MCP resources or expose Genkit tools as server.
Back to servers
Setup instructions
Provider
Firebase
Release date
Apr 29, 2024
Language
TypeScript
Package
Stats
8.7K downloads
2.6K stars

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.

Installation

To install Genkit MCP, run:

npm i genkit @genkit-ai/mcp

Connecting to MCP Servers

Using MCP Host for Multiple Servers

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

Host Configuration Options

  • 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 connection
    • Server connection specified via one of:
      • Command execution parameters: command (required) and args (optional)
      • Environment variables: env (optional)
      • Remote URL: url string for HTTP connections
      • Custom transport: transport object for specialized connections

Using MCP Client for a Single Server

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

Client Configuration Options

  • name: Required string for the client instance (used as namespace for tools)
  • version: Optional version string (defaults to "1.0.0")
  • Plus transport configuration options (same as in mcpServers entries for createMcpHost)

Working with MCP Actions

Using Tools and Prompts

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:

  • For mcpHost, tools are prefixed with the server name (e.g., fs/read_file)
  • For individual clients, tools are prefixed with the client name (e.g., myFileSystemClient/list_resources)

Tool Responses

MCP tools return responses in a content array format, which the plugin processes as follows:

  1. JSON text content is parsed into objects
  2. Non-JSON text is returned as strings
  3. Single non-text items are returned directly
  4. Multiple/mixed content parts are returned as the full content array

Creating an MCP Server

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

Server Configuration Options

  • name: Required string for identifying your server
  • version: Optional version string (defaults to "1.0.0")

Testing Your MCP Server

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.

How to install this MCP server

For Claude Code

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.

For Cursor

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.

Adding an MCP server to Cursor globally

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

Adding an MCP server to a project

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.

How to use the MCP server

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.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.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

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later