genkitx-mcp
Version:
A Genkit plugin that provides interoperability between Genkit and Model Context Protocol (MCP). Both client and server use cases are supported.
58 lines • 1.62 kB
JavaScript
import {
__async
} from "../chunk-E3LOUS7X.mjs";
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" }
},
(args) => __async(this, null, function* () {
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(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;
}
});
}
export {
registerAllPrompts
};
//# sourceMappingURL=prompts.mjs.map