Genkit MCP 变相器
见 Genkit MCP 文档。 。 。 .
用编码代理? 先安装基因组JS技能.
npx skills add genkit-ai/skills --skill developing-genkit-js它教你的代理人 现任Genkit JS APIs 和普通的gitchas. 其他语言的来源、人工安装和技能: 技能/技能。 。 。 .
此插件可提供 Genkit 和 Genkit 之间的整合 示范背景协议 (中文(简体) ). MCP是一个开放标准,允许开发者建立"服务器",为客户端提供工具,资源并提供提示. Genkit MCP允许Genkit开发者:
- 使用 MCP 工具、 提示和资源作为客户端
createMcpHost或者说createMcpClient。 。 。 . - 使用 Genkit 作为 MCP 服务器提供 Genkit 工具和提示
createMcpServer。 。 。 .
安装
要启动, 您需要 Genkit 和 MCP 插件:
npm i genkit @genkit-ai/mcp
MCP 主机
要连接一个或多个 MCP 服务器,请使用 createMcpHost 函数。 此函数返回 a GenkitMcpHost 实例管理已配置的 MCP 服务器的连接.
import { googleAI } from '@genkit-ai/google-genai';
import { createMcpHost } from '@genkit-ai/mcp';
import { genkit } from 'genkit';
const mcpHost = createMcpHost({
name: 'myMcpClients', // A name for the host plugin itself
mcpServers: {
// Each key (e.g., 'fs', 'git') becomes a namespace for the server's tools.
fs: {
command: 'npx',
args: ['-y', '@modelcontextprotocol/server-filesystem', process.cwd()],
},
memory: {
command: 'npx',
args: ['-y', '@modelcontextprotocol/server-memory'],
},
},
});
const ai = genkit({
plugins: [googleAI()],
});
(async () => {
// Provide MCP tools to the model of your choice.
const { text } = await ai.generate({
model: googleAI.model('gemini-flash-latest'),
prompt: `Analyze all files in ${process.cwd()}.`,
tools: await mcpHost.getActiveTools(ai),
resources: await mcpHost.getActiveResources(ai),
});
console.log(text);
await mcpHost.close();
})();
这个 createMcpHost 函数初始化 a GenkitMcpHost 例如,它处理生命周期,并与定义的MCP服务器进行通信.
createMcpHost() 选项
-
name: (可选,字符串) MCP 主机插件本身的名称. 默认为“ genkitx- mcp ” . -
version: (可选,字符串) MCP主机插件的版本. 默认为"1.0". -
rawToolResponses: (可选,布尔) 何时true,工具响应以原始的MCP格式返回;否则,它们会被处理成Genkit兼容性. 默认为false。 。 。 . -
mcpServers: (需要,对象) 每个密钥都是MCP服务器客户端名称(namespace)的对象,其值是该服务器的配置.每个服务器配置对象可以包括:
disabled: (可选,布尔)如果true,此服务器连接将不会尝试 。 默认为false。 。 。 .- 下列服务器连接配置之一:
- 使用 stdio MCP 传输启动本地服务器进程的参数.
command: (需要,字符串) 启动 MCP 服务器的 shell 命令路径( 例如 ),npx, (中文(简体) ),python) (中文(简体) ).args: (可选的,字符串[]) 要传递到命令的字符串参数组 .env: (可选, 记录 < string, string > ) 环境变量的关键值对象.
url: (字符串)一个远程服务器的URL可以连接到使用可流式HTTP MCP传输.transport:用于连接服务器的现有MCP传输对象.
- 使用 stdio MCP 传输启动本地服务器进程的参数.
MCP 客户端( 单一服务器)
对于只需要连接到单一的 MCP 服务器,或者更愿意单独管理客户端实例的情景,您可以使用 createMcpClient。 。 。 .
import { googleAI } from '@genkit-ai/google-genai';
import { createMcpClient } from '@genkit-ai/mcp';
import { genkit } from 'genkit';
const myFsClient = createMcpClient({
name: 'myFileSystemClient', // A unique name for this client instance
mcpServer: {
command: 'npx',
args: ['-y', '@modelcontextprotocol/server-filesystem', process.cwd()],
},
// rawToolResponses: true, // Optional: get raw MCP responses
});
// In your Genkit configuration:
const ai = genkit({
plugins: [googleAI()],
});
(async () => {
await myFsClient.ready();
// Retrieve tools from this specific client
const fsTools = await myFsClient.getActiveTools(ai);
const { text } = await ai.generate({
model: googleAI.model('gemini-flash-latest'), // Replace with your model
prompt: 'List files in ' + process.cwd(),
tools: fsTools,
});
console.log(text);
await myFsClient.disable();
})();
createMcpClient() 选项
这个 createMcpClient 函数需要 McpClientOptions 对象 :
name: (需要,字符串) 此客户端实例的独有名称 。 此名称将用作其工具和提示的命名空间 .version: (可选,字符串) 此客户端实例的版本 。 默认为"1.0".- 此外,它还支持所有来自
McpServerConfig(例如,disabled, (中文(简体) ),rawToolResponses和运输配置),详见createMcpHost选项部分.
使用 MCP 动作( 工具, 提示)
两者 GenkitMcpHost (通过) getActiveTools()和 GenkitMcpClient (通过) getActiveTools()从连接和启用的MCP服务器中发现可用的工具。 这些工具是标准的"玄子" ToolAction 实例和可以提供给基因组模型.
MCP 提示可以使用 McpHost.getPrompt(serverName, promptName) 或者说 mcpClient.getPrompt(promptName)这些返回 ExecutablePrompt。 。 。 .
所有 MCP 动作( 工具, 提示, 资源) 都被命名 .
- 对于
createMcpHost,命名空间是您为服务器提供的密钥mcpServers配置(例如,localFs/read_file) (中文(简体) ). - 对于
createMcpClient,命名空间是name您在其选项中提供(例如,myFileSystemClient/list_resources) (中文(简体) ).
工具响应
MCP 工具返回 a content 阵列,而不是像大多数基因组工具那样的结构化响应。 Genkit MCP 插件试图解析并强制返回的内容 :
- 如果内容为文本并有效JSON,则解析后作为JSON对象返回.
- 如果内容是文本但无效的JSON,则原始文本被返回.
- 如果内容包含一个单一的非文本部分(例如图像),则直接返回该部分.
- 如果内容包含多部分或混合部分(如文本和图像),则完整内容响应阵列被返回.
MCP 服务器
您也可以使用 MCP 服务器曝光 Genkit 实例中的所有工具和提示 createMcpServer 函数.
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}}.`
);
ai.defineResource(
{
name: 'my resouces',
uri: 'my://resource',
},
async () => {
return {
content: [
{
text: 'my resource',
},
],
};
}
);
ai.defineResource(
{
name: 'file',
template: 'file://{path}',
},
async ({ uri }) => {
return {
content: [
{
text: `file contents for ${uri}`,
},
],
};
}
);
// Use createMcpServer
const server = createMcpServer(ai, {
name: 'example_server',
version: '0.0.1',
});
// Setup (async) then starts with stdio transport by default
server.setup().then(async () => {
await server.start();
const transport = new StdioServerTransport();
await server!.server?.connect(transport);
});
这个 createMcpServer 函数返回 a GenkitMcpServer 举个例子 这个 start() 在此实例上的方法将启动一个 MCP 服务器( 默认使用 stdio 传输) , 该服务器将曝光所有已注册的 Genkit 工具和提示 。 要启动不同的 MCP 传输, 您可以将传输实例传递到 start() 方法(例如, server.start(customMcpTransport)) (中文(简体) ).
createMcpServer() 选项
name: (需要,字符串) 您想要为 MCP 检查提供服务器的名称 .version: (可选,字符串) 您的服务器将向客户端发布广告版本 。 默认为"1.0".
已知的限制
- MCP 提示只能取字符串参数,因此对schemas的输入必须是只有字符串属性值的对象.
- MCP 只提示支持
user财务报告和审定财务报表model留言.system不支持信件. - MCP 提示只支持消息中的单个"类型",所以不能在同一消息中混合媒体和文本.
测试您的 MCP 服务器
您可以使用官方检查员测试您的 MCP 服务器 。 例如,如果您的服务器代码编译成 dist/index.js,你可以运行:
npx @modelcontextprotocol/inspector dist/index.js
一旦你开始检查, 你可以列出提示和行动 并手动测试出来.