genkitx-mcp
Version:
A Genkit plugin that provides interoperability between Genkit and Model Context Protocol (MCP). Both client and server use cases are supported.
106 lines • 4.35 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
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 src_exports = {};
__export(src_exports, {
mcpClient: () => mcpClient,
mcpServer: () => mcpServer
});
module.exports = __toCommonJS(src_exports);
var import_genkit = require("genkit");
var import_plugin = require("genkit/plugin");
var import_prompts = require("./client/prompts.js");
var import_resources = require("./client/resources.js");
var import_tools = require("./client/tools.js");
var import_server = require("./server.js");
function transportFrom(params) {
return __async(this, null, function* () {
if (params.transport) return params.transport;
if (params.serverUrl) {
const { SSEClientTransport } = yield import("@modelcontextprotocol/sdk/client/sse.js");
return new SSEClientTransport(URL.parse(params.serverUrl));
}
if (params.serverProcess) {
const { StdioClientTransport } = yield import("@modelcontextprotocol/sdk/client/stdio.js");
return new StdioClientTransport(params.serverProcess);
}
throw new import_genkit.GenkitError({
status: "INVALID_ARGUMENT",
message: "Unable to create a server connection with supplied options. Must provide transport, stdio, or sseUrl."
});
});
}
function mcpClient(params) {
return (0, import_plugin.genkitPlugin)(params.name, (ai) => __async(this, null, function* () {
const { Client } = yield import("@modelcontextprotocol/sdk/client/index.js");
const transport = yield transportFrom(params);
ai.options.model;
const client = new Client(
{ name: params.name, version: params.version || "1.0.0" },
{ capabilities: {} }
);
yield client.connect(transport);
const capabilties = yield client.getServerCapabilities();
const promises = [];
if (capabilties == null ? void 0 : capabilties.tools) promises.push((0, import_tools.registerAllTools)(ai, client, params));
if (capabilties == null ? void 0 : capabilties.prompts)
promises.push((0, import_prompts.registerAllPrompts)(ai, client, params));
if (capabilties == null ? void 0 : capabilties.resources)
promises.push((0, import_resources.registerResourceTools)(ai, client, params));
yield Promise.all(promises);
}));
}
function mcpServer(ai, options) {
return new import_server.GenkitMcpServer(ai, options);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
mcpClient,
mcpServer
});
//# sourceMappingURL=index.js.map
;