diff --git a/src/tools/mcp/mcpResource.ts b/src/tools/mcp/mcpResource.ts index 3f9d5a9a..a141ff61 100644 --- a/src/tools/mcp/mcpResource.ts +++ b/src/tools/mcp/mcpResource.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { ToolEmitter, ToolInput } from "@/tools/base.js"; +import { BaseToolRunOptions, ToolEmitter, ToolInput } from "@/tools/base.js"; import { z } from "zod"; import { Emitter } from "@/emitter/emitter.js"; import { ReadResourceResult } from "@modelcontextprotocol/sdk/types.js"; @@ -22,13 +22,14 @@ import { MCPTool, MCPToolInput, MCPToolOutput } from "./base.js"; import { GetRunContext } from "@/context.js"; import { paginateWithCursor } from "@/internals/helpers/paginate.js"; import { Cache } from "@/cache/decoratorCache.js"; +import { hasAtLeast } from "remeda"; export interface MCPResourceToolInput extends MCPToolInput { resourceLimit?: number; } export class MCPResourceTool extends MCPTool { - name = "Resource"; + name = "MCPResource"; description = `The Resource tool provides the ability to read external resources. Use it to read contents of available resources listed below.`; public readonly emitter: ToolEmitter, MCPToolOutput> = @@ -62,16 +63,23 @@ export class MCPResourceTool extends MCPTool { } as const); }, }); + const resourceLiterals = resources.map(({ uri, name, description }) => + z.literal(uri).describe(`${name}${description && `\n${description}`}`), + ); return z.object({ - uri: z - .string() - .describe( - `URI of the resource to read, ${resources.length > 0 ? `available resources are:\n\n${resources.map(({ uri, name, description }) => JSON.stringify({ uri, name, description })).join("\n\n")}` : "no resources available at the moment"}.`, - ), + uri: hasAtLeast(resourceLiterals, 2) + ? z.union(resourceLiterals).describe("Resource to read.") + : hasAtLeast(resourceLiterals, 1) + ? resourceLiterals[0].describe(`Resource to read`) + : z.literal("non_existing_resource").describe("No resources available."), }); } - protected async _run({ uri }: ToolInput, run: GetRunContext) { + protected async _run( + { uri }: ToolInput, + _options: BaseToolRunOptions, + run: GetRunContext, + ) { const result = await this.client.readResource({ uri }, { signal: run.signal }); return new MCPToolOutput(result); }