Skip to content

Add optional parameter for callTool options including timeout. #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/react/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tool, Resource, ResourceTemplate, Prompt } from '@modelcontextprotocol/sdk/types.js'
import { Tool, Resource, ResourceTemplate, Prompt, RequestOptions } from '@modelcontextprotocol/sdk/types.js'

export type UseMcpOptions = {
/** The /sse URL of your remote MCP server */
Expand Down Expand Up @@ -71,10 +71,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<string, unknown>) => Promise<any>
callTool: (name: string, args?: Record<string, unknown>, options?: RequestOptions) => Promise<any>
/**
* Function to list resources from the MCP server.
* @returns A promise that resolves when resources are refreshed.
Expand Down
5 changes: 3 additions & 2 deletions src/react/useMcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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 { RequestOptions } from '@modelcontextprotocol/sdk/types.js'
import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js' // Added for type safety

const DEFAULT_RECONNECT_DELAY = 3000
Expand Down Expand Up @@ -506,14 +507,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<string, unknown>) => {
async (name: string, args?: Record<string, unknown>, 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) {
Expand Down