Genkit MCP

Official
Apache 2.0
197
1,663

hybrid server

The server is able to function both locally and remotely, depending on the configuration or use case.

Integrations

  • Allows integration with the filesystem server through MCP, enabling reading files from allowed directories

Genkit MCP

[!警告]
该插件是实验性的,这意味着它可能不会得到长期支持,并且 API 可能会经常发生重大变化。

此插件提供 Genkit 与模型上下文协议(MCP) 之间的集成。MCP 是一个开放标准,允许开发者构建“服务器”,向客户端提供工具、资源和提示。Genkit MCP 允许 Genkit 开发者既可以作为客户端使用 MCP 工具、提示和资源,也可以作为服务器提供工具和提示。

安装

首先,您需要 Genkit 和 MCP 插件:

npm i genkit genkitx-mcp

MCP 客户端

要创建 MCP 客户端,请调用mcpClient函数为 MCP 服务器生成 Genkit 插件。例如,要使用 MCP 的示例文件系统服务器

import { genkit } from 'genkit'; import { mcpClient } from 'genkitx-mcp'; // the filesystem server requires one or more allowed directories const ALLOWED_DIRS = ['/Users/yourusername/Desktop']; const filesystemClient = mcpClient({ name: 'filesystem', serverProcess: { command: 'npx', args: ['-y', '@modelcontextprotocol/server-everything', ...ALLOWED_DIRS], }, }); const ai = genkit({ plugins: [ filesystemClient /* ... other plugins such as model providers ...*/, ], });

大多数 MCP 服务器都构建为在同一台机器上使用stdio传输方式作为子进程运行。当您提供serverProcess选项时,您将指定将服务器作为子进程生成的命令、参数和环境变量。

mcpClient() 选项

  • name :(必需)此客户端的名称,其工具和提示的命名空间。
  • version :(可选)客户端版本号。默认为“1.0.0”。
  • 您必须提供以下之一:
    • serverProcess :使用 stdio MCP 传输启动本地服务器进程的参数。
      • command :用于启动 MCP 服务器的 Shell 命令路径。例如,可以使用npxuvx命令从包管理器下载并运行服务器。
      • args :(可选)传递给命令的字符串参数数组。
      • env :(可选)传递给命令的环境变量的键值对象。
    • serverUrl :使用 SSE MCP 传输连接的远程服务器的 URL。
    • ** serverWebsocketUrl :使用 WebSocket MCP 传输连接的远程服务器的 URL。
    • transport :用于连接服务器的现有 MCP 传输对象。
  • rawToolResponses :(可选)布尔值标志。如果为true ,则工具响应将以原始 MCP 格式返回;否则,将对其进行处理以兼容 Genkit。

使用 MCP 操作

Genkit MCP 客户端会自动发现可用的工具和提示符,并将其注册到 Genkit,以便在其他工具和提示符可以使用的任何地方使用它们。为了访问资源,需要注册特殊的list_resourcesread_resource工具来访问服务器的资源。

所有 MCP 操作都以您提供的名称命名,因此名为filesystem的客户端将注册诸如filesystem/read_file之类的工具。

工具响应

MCP 工具返回的是content数组,而不是像大多数 Genkit 工具那样返回结构化的响应。Genkit MCP 插件会尝试解析并强制执行返回的内容:

  1. 如果内容是文本和有效的 JSON,则解析并返回 JSON。
  2. 如果内容是文本并且不是有效的 JSON,则返回文本。
  3. 如果内容有一个非文本部分,则返回该部分。
  4. 如果内容有多个/混合部分,则返回完整的内容响应。

MCP 服务器

您还可以将 Genkit 实例中的所有工具和提示公开为 MCP 服务器:

import { genkit, z } from 'genkit'; import { mcpServer } from 'genkitx-mcp'; const ai = genkit({}); 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().optional() }), default: { action: 'clap your hands' }, }, }, `If you're happy and you know it, {{action}}.` ); mcpServer(ai, { name: 'example_server', version: '0.0.1' }).start();

上述代码将启动一个带有 stdio 传输的 MCP 服务器,该服务器会暴露一个名为add的工具和一个名为happy的提示符。要使用其他传输方式启动服务器,请使用mcpServer(...).start(otherTransport)

已知限制

  • MCP 提示只能采用字符串参数,因此模式的输入必须是仅具有字符串属性值的对象。
  • MCP 提示仅支持usermodel消息。不支持system消息。
  • MCP 提示仅支持消息中的单一“类型”,因此您不能在同一消息中混合媒体和文本。

测试您的 MCP 服务器

你可以使用官方检查器测试你的 MCP 服务器。例如,如果你的服务器代码编译成了dist/index.js ,你可以运行:

npx @modelcontextprotocol/inspector dist/index.js

一旦启动检查器,您就可以列出提示和操作并手动测试它们。

You must be authenticated.

A
security – no known vulnerabilities
A
license - permissive license
A
quality - confirmed to work

提供Genkit和模型上下文协议 (MCP) 之间的集成。

  1. Installation
    1. MCP Client
      1. mcpClient() Options
      2. Using MCP Actions
      3. Tool Responses
    2. MCP Server
      1. Known Limitations
      2. Testing your MCP server
    ID: m6x141u6rd