UNPKG

genkitx-mcp

Version:

A Genkit plugin that provides interoperability between Genkit and Model Context Protocol (MCP). Both client and server use cases are supported.

49 lines 1.45 kB
import { logger } from "genkit/logging"; import { fromMcpPromptMessage } from "./message.js"; function toSchema(args) { if (!args) return {}; const schema = { type: "object", properties: {}, required: [] }; for (const arg of args) { schema.properties[arg.name] = { type: "string", description: arg.description }; if (arg.required) schema.required.push(arg.name); } return schema; } function registerPrompt(ai, client, prompt, params) { logger.debug( `[@genkit-ai/mcp] Registering MCP prompt ${params.name}/${prompt.name}` ); ai.definePrompt({ name: prompt.name, description: prompt.description || "", input: { jsonSchema: toSchema(prompt.arguments) }, output: { format: "text" }, messages: async (args) => { logger.debug( `[@genkit-ai/mcp] Calling MCP prompt ${params.name}/${prompt.name} with arguments`, JSON.stringify(args) ); const result = await client.getPrompt({ name: prompt.name, arguments: args }); return result.messages.map(fromMcpPromptMessage); } }); } async function registerAllPrompts(ai, client, params) { let cursor; while (true) { const { nextCursor, prompts } = await client.listPrompts({ cursor }); prompts.forEach((p) => registerPrompt(ai, client, p, params)); cursor = nextCursor; if (!cursor) break; } } export { registerAllPrompts }; //# sourceMappingURL=prompts.mjs.map