genkitx-mcp
Version:
A Genkit plugin that provides interoperability between Genkit and Model Context Protocol (MCP). Both client and server use cases are supported.
73 lines • 2.53 kB
JavaScript
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 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" },
messages: async (args) => {
import_logging.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(import_message.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;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
registerAllPrompts
});
//# sourceMappingURL=prompts.js.map
;