{"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,eAAsB,sBACpB,IACA,QACA,QACA;AACA,KAAG;AAAA,IACD;AAAA,MACE,MAAM,GAAG,OAAO,IAAI;AAAA,MACpB,aAAa,qCAAqC,OAAO,IAAI;AAAA,MAC7D,aAAa,EAAE,OAAO;AAAA;AAAA,QAEpB,QAAQ,EAAE,OAAO,EAAE,SAAS;AAAA;AAAA,QAE5B,KAAK,EAAE,QAAQ,EAAE,SAAS;AAAA,MAC5B,CAAC;AAAA,IACH;AAAA,IACA,OAAO,EAAE,QAAQ,IAAI,MAAM;AACzB,UAAI,CAAC,KAAK;AACR,eAAO,OAAO,cAAc;AAAA,MAC9B;AAEA,UAAI,gBAAoC;AACxC,YAAM,YAAwB,CAAC;AAC/B,aAAO,MAAM;AACX,cAAM,EAAE,YAAY,WAAW,aAAa,IAC1C,MAAM,OAAO,cAAc,EAAE,QAAQ,cAAc,CAAC;AACtD,kBAAU,KAAK,GAAG,YAAY;AAC9B,wBAAgB;AAChB,YAAI,CAAC,cAAe;AAAA,MACtB;AACA,aAAO,EAAE,UAAU;AAAA,IACrB;AAAA,EACF;AAEA,KAAG;AAAA,IACD;AAAA,MACE,MAAM,GAAG,OAAO,IAAI;AAAA,MACpB,aAAa,8CAA8C,OAAO,IAAI;AAAA,MACtE,aAAa,EAAE,OAAO;AAAA;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