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
11.6K downloads
3.7K stars

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.

Installation

To get started with Genkit MCP, install the necessary packages:

npm i genkit @genkit-ai/mcp

Using as an MCP Client

There are two ways to connect to MCP servers: using a host for multiple servers or a client for a single server.

Using MCP Host for Multiple Servers

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

Using MCP Client for a Single Server

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

Configuration Options

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 configurations

Server configurations can include:

  • disabled: If true, this server connection will not be attempted (default: false)
  • Connection options (one of):
    • Local server process: command and args to launch a server
    • Remote server: url for connecting via HTTP
    • Custom: transport for an existing MCP transport object

For createMcpClient, the options are similar but focused on a single server connection.

Working with MCP Tools and Prompts

MCP tools are available through:

  • mcpHost.getActiveTools(ai) - gets tools from all connected servers
  • mcpClient.getActiveTools(ai) - gets tools from a specific client

MCP prompts can be fetched using:

  • mcpHost.getPrompt(serverName, promptName)
  • mcpClient.getPrompt(promptName)

All MCP actions are namespaced:

  • For hosts: serverName/toolName (e.g., fs/read_file)
  • For clients: clientName/toolName (e.g., myFileSystemClient/list_resources)

Creating an MCP Server

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

Testing Your MCP Server

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.

Limitations

  • MCP prompts can only take string parameters (object properties must be strings)
  • MCP prompts only support user and model messages (no system messages)
  • MCP prompts can only include one media type per message (can't mix text and images)

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