From 3f5e1c466ae0da40be6638fa61cbb8be9baad5f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CZilula=E2=80=9D?= Date: Fri, 22 Aug 2025 08:58:01 -1000 Subject: [PATCH 1/2] Add optiona parameter for callTool options including timeout. --- src/react/types.ts | 11 ++++++++++- src/react/useMcp.ts | 6 +++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/react/types.ts b/src/react/types.ts index b80df54..c451272 100644 --- a/src/react/types.ts +++ b/src/react/types.ts @@ -1,5 +1,13 @@ import { Tool, Resource, ResourceTemplate, Prompt } from '@modelcontextprotocol/sdk/types.js' +/** + * Options that can be passed to MCP client requests + */ +export type RequestOptions = { + /** Request timeout in milliseconds */ + timeout?: number +} + export type UseMcpOptions = { /** The /sse URL of your remote MCP server */ url: string @@ -71,10 +79,11 @@ export type UseMcpResult = { * Function to call a tool on the MCP server. * @param name The name of the tool to call. * @param args Optional arguments for the tool. + * @param options Optional request options (e.g., timeout). * @returns A promise that resolves with the tool's result. * @throws If the client is not in the 'ready' state or the call fails. */ - callTool: (name: string, args?: Record) => Promise + callTool: (name: string, args?: Record, options?: RequestOptions) => Promise /** * Function to list resources from the MCP server. * @returns A promise that resolves when resources are refreshed. diff --git a/src/react/useMcp.ts b/src/react/useMcp.ts index 697f74a..be9625e 100644 --- a/src/react/useMcp.ts +++ b/src/react/useMcp.ts @@ -21,7 +21,7 @@ import { auth, UnauthorizedError, OAuthClientProvider } from '@modelcontextproto import { sanitizeUrl } from 'strict-url-sanitise' import { BrowserOAuthClientProvider } from '../auth/browser-provider.js' // Adjust path import { assert } from '../utils/assert.js' // Adjust path -import type { UseMcpOptions, UseMcpResult } from './types.js' // Adjust path +import type { UseMcpOptions, UseMcpResult, RequestOptions } from './types.js' // Adjust path import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js' // Added for type safety const DEFAULT_RECONNECT_DELAY = 3000 @@ -506,14 +506,14 @@ export function useMcp(options: UseMcpOptions): UseMcpResult { // callTool is stable (depends on stable addLog, failConnection, connect, and URL) const callTool = useCallback( - async (name: string, args?: Record) => { + async (name: string, args?: Record, options?: RequestOptions) => { // Use stateRef for check, state for throwing error message if (stateRef.current !== 'ready' || !clientRef.current) { throw new Error(`MCP client is not ready (current state: ${state}). Cannot call tool "${name}".`) } addLog('info', `Calling tool: ${name}`, args) try { - const result = await clientRef.current.request({ method: 'tools/call', params: { name, arguments: args } }, CallToolResultSchema) + const result = await clientRef.current.request({ method: 'tools/call', params: { name, arguments: args } }, CallToolResultSchema, options) addLog('info', `Tool "${name}" call successful:`, result) return result } catch (err) { From 2c3dcdf55e60e097de2a4c8e7377ac3c27e73dd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CZilula=E2=80=9D?= Date: Fri, 22 Aug 2025 09:21:25 -1000 Subject: [PATCH 2/2] Reuse type from shared SDK --- src/react/types.ts | 10 +--------- src/react/useMcp.ts | 3 ++- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/react/types.ts b/src/react/types.ts index c451272..88edd7c 100644 --- a/src/react/types.ts +++ b/src/react/types.ts @@ -1,12 +1,4 @@ -import { Tool, Resource, ResourceTemplate, Prompt } from '@modelcontextprotocol/sdk/types.js' - -/** - * Options that can be passed to MCP client requests - */ -export type RequestOptions = { - /** Request timeout in milliseconds */ - timeout?: number -} +import { Tool, Resource, ResourceTemplate, Prompt, RequestOptions } from '@modelcontextprotocol/sdk/types.js' export type UseMcpOptions = { /** The /sse URL of your remote MCP server */ diff --git a/src/react/useMcp.ts b/src/react/useMcp.ts index be9625e..d408958 100644 --- a/src/react/useMcp.ts +++ b/src/react/useMcp.ts @@ -21,7 +21,8 @@ import { auth, UnauthorizedError, OAuthClientProvider } from '@modelcontextproto import { sanitizeUrl } from 'strict-url-sanitise' import { BrowserOAuthClientProvider } from '../auth/browser-provider.js' // Adjust path import { assert } from '../utils/assert.js' // Adjust path -import type { UseMcpOptions, UseMcpResult, RequestOptions } from './types.js' // Adjust path +import type { UseMcpOptions, UseMcpResult } from './types.js' // Adjust path +import type { RequestOptions } from '@modelcontextprotocol/sdk/types.js' import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js' // Added for type safety const DEFAULT_RECONNECT_DELAY = 3000