|
1 | | -import { Client, type Tool as MCPToolDef } from "@modelcontextprotocol/client" |
| 1 | +import { Client, type CallToolResult, type Tool as MCPToolDef } from "@modelcontextprotocol/client" |
2 | 2 | import { dynamicTool, jsonSchema, type JSONSchema7, type Tool } from "ai" |
3 | 3 | import { Effect } from "effect" |
4 | 4 |
|
5 | 5 | const DEFAULT_TIMEOUT = 30_000 |
| 6 | + |
| 7 | +export interface McpTool { |
| 8 | + readonly def: MCPToolDef |
| 9 | + readonly client: Client |
| 10 | + readonly timeout?: number |
| 11 | +} |
| 12 | + |
| 13 | +export async function callTool( |
| 14 | + tool: McpTool, |
| 15 | + args: Record<string, unknown>, |
| 16 | + signal?: AbortSignal, |
| 17 | +): Promise<CallToolResult> { |
| 18 | + const result = await tool.client.callTool( |
| 19 | + { name: tool.def.name, arguments: args }, |
| 20 | + { |
| 21 | + resetTimeoutOnProgress: true, |
| 22 | + signal, |
| 23 | + timeout: tool.timeout, |
| 24 | + // The MCP SDK only sends a progress token when this hook is present, enabling timeout resets. |
| 25 | + onprogress: () => {}, |
| 26 | + }, |
| 27 | + ) |
| 28 | + if (result.isError) |
| 29 | + throw new Error( |
| 30 | + result.content |
| 31 | + .flatMap((item) => (item.type === "text" ? [item.text] : [])) |
| 32 | + .filter((text) => text.trim()) |
| 33 | + .join("\n\n") || "MCP tool returned an error", |
| 34 | + ) |
| 35 | + return result |
| 36 | +} |
| 37 | + |
6 | 38 | export function defs(client: Client, timeout?: number) { |
7 | 39 | return listTools(client, timeout ?? DEFAULT_TIMEOUT).pipe(Effect.catch(() => Effect.void)) |
8 | 40 | } |
9 | 41 |
|
10 | | -export function convertTool(mcpTool: MCPToolDef, client: Client, timeout?: number): Tool { |
| 42 | +export function convertTool(tool: McpTool): Tool { |
11 | 43 | const inputSchema: JSONSchema7 = { |
12 | | - ...(mcpTool.inputSchema as JSONSchema7), |
| 44 | + ...(tool.def.inputSchema as JSONSchema7), |
13 | 45 | type: "object", |
14 | | - properties: (mcpTool.inputSchema.properties ?? {}) as JSONSchema7["properties"], |
| 46 | + properties: (tool.def.inputSchema.properties ?? {}) as JSONSchema7["properties"], |
15 | 47 | additionalProperties: false, |
16 | 48 | } |
17 | 49 |
|
18 | 50 | return dynamicTool({ |
19 | | - description: mcpTool.description ?? "", |
| 51 | + description: tool.def.description ?? "", |
20 | 52 | inputSchema: jsonSchema(inputSchema), |
21 | 53 | execute: async (args: unknown, options) => { |
22 | | - const result = await client.callTool( |
23 | | - { |
24 | | - name: mcpTool.name, |
25 | | - arguments: (args || {}) as Record<string, unknown>, |
26 | | - }, |
27 | | - { |
28 | | - resetTimeoutOnProgress: true, |
29 | | - signal: options.abortSignal, |
30 | | - timeout, |
31 | | - // The MCP SDK only sends a progress token when this hook is present, enabling timeout resets. |
32 | | - onprogress: () => {}, |
33 | | - }, |
34 | | - ) |
35 | | - if (result.isError) |
36 | | - throw new Error( |
37 | | - result.content |
38 | | - .flatMap((item) => (item.type === "text" ? [item.text] : [])) |
39 | | - .filter((text) => text.trim()) |
40 | | - .join("\n\n") || "MCP tool returned an error", |
41 | | - ) |
| 54 | + const result = await callTool(tool, (args || {}) as Record<string, unknown>, options.abortSignal) |
42 | 55 | if (result.content.length > 0 || result.structuredContent === undefined || result.structuredContent === null) |
43 | 56 | return result |
44 | 57 | return { |
|
0 commit comments