Skip to content

Commit

Permalink
fix: schema and run
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Pilar <[email protected]>
  • Loading branch information
pilartomas committed Jan 7, 2025
1 parent 3de4911 commit 6b57f6b
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/tools/mcp/mcpResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@
* 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";
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<ReadResourceResult> {
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<ToolInput<this>, MCPToolOutput<ReadResourceResult>> =
Expand Down Expand Up @@ -62,16 +63,23 @@ export class MCPResourceTool extends MCPTool<ReadResourceResult> {
} 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<this>, run: GetRunContext<typeof this>) {
protected async _run(
{ uri }: ToolInput<this>,
_options: BaseToolRunOptions,
run: GetRunContext<typeof this>,
) {
const result = await this.client.readResource({ uri }, { signal: run.signal });
return new MCPToolOutput(result);
}
Expand Down

0 comments on commit 6b57f6b

Please sign in to comment.