genkitx-mcp
Version:
A Genkit plugin that provides interoperability between Genkit and Model Context Protocol (MCP). Both client and server use cases are supported.
1 lines • 3.77 kB
Source Map (JSON)
{"version":3,"sources":["../../src/client/prompts.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { Client } from '@modelcontextprotocol/sdk/client/index.js' with { 'resolution-mode': 'import' };\nimport type { Prompt } from '@modelcontextprotocol/sdk/types.js' with { 'resolution-mode': 'import' };\nimport { Genkit, JSONSchema } from 'genkit';\nimport { logger } from 'genkit/logging';\nimport type { McpClientOptions } from '../index.js';\nimport { fromMcpPromptMessage } from './message.js';\n\nfunction toSchema(args: Prompt['arguments']) {\n if (!args) return {};\n const schema: JSONSchema = { type: 'object', properties: {}, required: [] };\n for (const arg of args) {\n schema.properties[arg.name] = {\n type: 'string',\n description: arg.description,\n };\n if (arg.required) schema.required.push(arg.name);\n }\n return schema;\n}\n\nfunction registerPrompt(\n ai: Genkit,\n client: Client,\n prompt: Prompt,\n params: McpClientOptions\n) {\n logger.debug(\n `[@genkit-ai/mcp] Registering MCP prompt ${params.name}/${prompt.name}`\n );\n ai.definePrompt({\n name: prompt.name,\n description: prompt.description || '',\n input: { jsonSchema: toSchema(prompt.arguments) },\n output: { format: 'text' },\n messages: async (args) => {\n logger.debug(\n `[@genkit-ai/mcp] Calling MCP prompt ${params.name}/${prompt.name} with arguments`,\n JSON.stringify(args)\n );\n const result = await client.getPrompt({\n name: prompt.name,\n arguments: args,\n });\n return result.messages.map(fromMcpPromptMessage);\n },\n });\n}\n\n/**\n * Lookup all tools available in the server and register each as a Genkit tool.\n */\nexport async function registerAllPrompts(\n ai: Genkit,\n client: Client,\n params: McpClientOptions\n): Promise<void> {\n let cursor: string | undefined;\n while (true) {\n const { nextCursor, prompts } = await client.listPrompts({ cursor });\n prompts.forEach((p) => registerPrompt(ai, client, p, params));\n cursor = nextCursor;\n if (!cursor) break;\n }\n}\n"],"mappings":"AAmBA,SAAS,cAAc;AAEvB,SAAS,4BAA4B;AAErC,SAAS,SAAS,MAA2B;AAC3C,MAAI,CAAC,KAAM,QAAO,CAAC;AACnB,QAAM,SAAqB,EAAE,MAAM,UAAU,YAAY,CAAC,GAAG,UAAU,CAAC,EAAE;AAC1E,aAAW,OAAO,MAAM;AACtB,WAAO,WAAW,IAAI,IAAI,IAAI;AAAA,MAC5B,MAAM;AAAA,MACN,aAAa,IAAI;AAAA,IACnB;AACA,QAAI,IAAI,SAAU,QAAO,SAAS,KAAK,IAAI,IAAI;AAAA,EACjD;AACA,SAAO;AACT;AAEA,SAAS,eACP,IACA,QACA,QACA,QACA;AACA,SAAO;AAAA,IACL,2CAA2C,OAAO,IAAI,IAAI,OAAO,IAAI;AAAA,EACvE;AACA,KAAG,aAAa;AAAA,IACd,MAAM,OAAO;AAAA,IACb,aAAa,OAAO,eAAe;AAAA,IACnC,OAAO,EAAE,YAAY,SAAS,OAAO,SAAS,EAAE;AAAA,IAChD,QAAQ,EAAE,QAAQ,OAAO;AAAA,IACzB,UAAU,OAAO,SAAS;AACxB,aAAO;AAAA,QACL,uCAAuC,OAAO,IAAI,IAAI,OAAO,IAAI;AAAA,QACjE,KAAK,UAAU,IAAI;AAAA,MACrB;AACA,YAAM,SAAS,MAAM,OAAO,UAAU;AAAA,QACpC,MAAM,OAAO;AAAA,QACb,WAAW;AAAA,MACb,CAAC;AACD,aAAO,OAAO,SAAS,IAAI,oBAAoB;AAAA,IACjD;AAAA,EACF,CAAC;AACH;AAKA,eAAsB,mBACpB,IACA,QACA,QACe;AACf,MAAI;AACJ,SAAO,MAAM;AACX,UAAM,EAAE,YAAY,QAAQ,IAAI,MAAM,OAAO,YAAY,EAAE,OAAO,CAAC;AACnE,YAAQ,QAAQ,CAAC,MAAM,eAAe,IAAI,QAAQ,GAAG,MAAM,CAAC;AAC5D,aAAS;AACT,QAAI,CAAC,OAAQ;AAAA,EACf;AACF;","names":[]}