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 • 5.01 kB
Source Map (JSON)
{"version":3,"sources":["../../src/client/resources.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 {\n Resource,\n ResourceTemplate,\n} from '@modelcontextprotocol/sdk/types.js' with { 'resolution-mode': 'import' };\nimport { Genkit, z } from 'genkit';\nimport type { McpClientOptions } from '../index.js';\n\nexport async function registerResourceTools(\n ai: Genkit,\n client: Client,\n params: McpClientOptions\n) {\n ai.defineTool(\n {\n name: `${params.name}/list_resources`,\n description: `list all available resources for '${params.name}'`,\n inputSchema: z.object({\n /** Provide a cursor for accessing additional paginated results. */\n cursor: z.string().optional(),\n /** When specified, automatically paginate and fetch all resources. */\n all: z.boolean().optional(),\n }),\n },\n async ({ cursor, all }) => {\n if (!all) {\n return client.listResources();\n }\n\n let currentCursor: string | undefined = cursor;\n const resources: Resource[] = [];\n while (true) {\n const { nextCursor, resources: newResources } =\n await client.listResources({ cursor: currentCursor });\n resources.push(...newResources);\n currentCursor = nextCursor;\n if (!currentCursor) break;\n }\n return { resources };\n }\n );\n\n ai.defineTool(\n {\n name: `${params.name}/list_resource_templates`,\n description: `list all available resource templates for '${params.name}'`,\n inputSchema: z.object({\n /** Provide a cursor for accessing additional paginated results. */\n cursor: z.string().optional(),\n /** When specified, automatically paginate and fetch all resources. */\n all: z.boolean().optional(),\n }),\n },\n async ({ cursor, all }) => {\n if (!all) {\n return client.listResourceTemplates();\n }\n\n let currentCursor: string | undefined = cursor;\n const resourceTemplates: ResourceTemplate[] = [];\n while (true) {\n const { nextCursor, resourceTemplates: newResourceTemplates } =\n await client.listResourceTemplates({ cursor: currentCursor });\n resourceTemplates.push(...newResourceTemplates);\n currentCursor = nextCursor;\n if (!currentCursor) break;\n }\n return { resourceTemplates };\n }\n );\n\n ai.defineTool(\n {\n name: `${params.name}/read_resource`,\n description: `this tool can read resources from '${params.name}'`,\n inputSchema: z.object({\n uri: z.string().describe('the URI of the resource to retrieve'),\n }),\n },\n async ({ uri }) => {\n return client.readResource({ uri });\n }\n );\n}\n"],"mappings":";;;AAqBA,SAAiB,SAAS;AAG1B,SAAsB,sBACpB,IACA,QACA,QACA;AAAA;AACA,OAAG;AAAA,MACD;AAAA,QACE,MAAM,GAAG,OAAO,IAAI;AAAA,QACpB,aAAa,qCAAqC,OAAO,IAAI;AAAA,QAC7D,aAAa,EAAE,OAAO;AAAA;AAAA,UAEpB,QAAQ,EAAE,OAAO,EAAE,SAAS;AAAA;AAAA,UAE5B,KAAK,EAAE,QAAQ,EAAE,SAAS;AAAA,QAC5B,CAAC;AAAA,MACH;AAAA,MACA,CAAO,OAAoB,eAApB,KAAoB,WAApB,EAAE,QAAQ,IAAI,GAAM;AACzB,YAAI,CAAC,KAAK;AACR,iBAAO,OAAO,cAAc;AAAA,QAC9B;AAEA,YAAI,gBAAoC;AACxC,cAAM,YAAwB,CAAC;AAC/B,eAAO,MAAM;AACX,gBAAM,EAAE,YAAY,WAAW,aAAa,IAC1C,MAAM,OAAO,cAAc,EAAE,QAAQ,cAAc,CAAC;AACtD,oBAAU,KAAK,GAAG,YAAY;AAC9B,0BAAgB;AAChB,cAAI,CAAC,cAAe;AAAA,QACtB;AACA,eAAO,EAAE,UAAU;AAAA,MACrB;AAAA,IACF;AAEA,OAAG;AAAA,MACD;AAAA,QACE,MAAM,GAAG,OAAO,IAAI;AAAA,QACpB,aAAa,8CAA8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