MCP Hooks
React hooks for interacting with MCP servers from within your App components. All hooks work within anAppProvider or GPTAppProvider context.
useTool
The primary hook for calling MCP tools with full state management, retries, and result transformation.Basic Usage
With Arguments
With Options
Return Value
| Property | Type | Description |
|---|---|---|
call | (args?) => Promise<T> | Execute the tool |
mutate | (args) => Promise<T> | Alias for call (semantic) |
loading | boolean | Whether tool is executing |
state | 'idle' | 'loading' | 'success' | 'error' | Current state |
result | T | null | Last successful result |
error | Error | null | Last error |
reset | () => void | Reset to initial state |
retry | () => Promise<T> | Retry last call |
abort | () => void | Abort current call |
Options
| Option | Type | Description |
|---|---|---|
defaultArgs | Record<string, unknown> | Default arguments merged with call args |
transform | (result) => T | Transform the raw tool result |
retry | number | { count, delay } | Retry configuration |
onStart | () => void | Called when tool starts |
onSuccess | (result) => void | Called on success |
onError | (error) => void | Called on error |
onComplete | () => void | Called after success or error |
useResource
Read MCP server resources with auto-refresh and subscription support.Basic Usage
With Auto-Refresh
Return Value
| Property | Type | Description |
|---|---|---|
data | T | null | Resource data |
loading | boolean | Whether loading |
error | Error | null | Error if any |
refresh | () => Promise<T> | Manual refresh |
lastUpdated | Date | null | Last update timestamp |
Options
| Option | Type | Description |
|---|---|---|
refreshInterval | number | Auto-refresh interval in ms |
subscribe | boolean | Enable subscription (when supported) |
transform | (data) => T | Transform resource data |
skip | boolean | Skip initial fetch |
useMessage
Send messages to the host chat interface.Return Value
| Property | Type | Description |
|---|---|---|
sendMessage | (text: string) => Promise<void> | Send message to host |
useHostContext
Access host environment information like theme, viewport, and display mode.Return Value
| Property | Type | Description |
|---|---|---|
theme | 'light' | 'dark' | Current theme |
displayMode | string | Current display mode |
viewport | { width, height } | Viewport dimensions |
styles | object | Host style variables |