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.

99 lines 3.2 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; var prompts_exports = {}; __export(prompts_exports, { registerAllPrompts: () => registerAllPrompts }); module.exports = __toCommonJS(prompts_exports); var import_logging = require("genkit/logging"); var import_message = require("./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) { import_logging.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" } }, (args) => __async(this, null, function* () { import_logging.logger.debug( `[@genkit-ai/mcp] Calling MCP prompt ${params.name}/${prompt.name} with arguments`, JSON.stringify(args) ); const result = yield client.getPrompt({ name: prompt.name, arguments: args }); return { messages: result.messages.map(import_message.fromMcpPromptMessage) }; }) ); } function registerAllPrompts(ai, client, params) { return __async(this, null, function* () { let cursor; while (true) { const { nextCursor, prompts } = yield client.listPrompts({ cursor }); prompts.forEach((p) => registerPrompt(ai, client, p, params)); cursor = nextCursor; if (!cursor) break; } }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { registerAllPrompts }); //# sourceMappingURL=prompts.js.map