@buildwithlayer/react-mcp-client-hooks
v0.0.6
Published
React hooks to use MCP Client functionality within your React app
Downloads
4
Keywords
Readme
React MCP Client Hooks
React hooks for settuping up a Model Context Protocol (MCP) client.
Installation
npm install @buildwithlayer/react-mcp-client-hooks
# or
yarn add @buildwithlayer/react-mcp-client-hooksUsage
Wrap your application with the MCP context provider:
import { MCPServerContextProvider } from '@buildwithlayer/react-mcp-client-hooks';
function App() {
return (
<MCPServerContextProvider>
<YourComponent />
</MCPServerContextProvider>
);
}Use the hooks in your components:
import {
useMCPConnection,
useMCPListTools,
} from '@buildwithlayer/react-mcp-client-hooks';
function YourComponent() {
const { connectSSE, status } = useMCPConnection({
url: new URL('your-mcp-server-url'),
});
const { tools, listTools } = useMCPListTools();
// Your component logic
}Available Hooks
useMCPConnection: Manages the connection to an MCP server using SSE (Server-Sent Events).statusProvides connection status (CONNECTED, CONNECTING, DISCONNECTED, etc.)- Methods for connecting to and disconnecting from the server (
connectSSEanddisconnect) - Exposes server capabilities once connected (
capabilities) - Handles connection errors (
error)
useMCPListTools: Retrieves the list of available tools from the MCP server.listToolsReturns the current tool list and a method to refresh it- Provides loading state (
loading) and error handling (error) toolsProvides the tools returned by listTools
useMCPCallTool: Calls a tool on the MCP server with provided arguments.callToolMethod to call a tool with a name and arguments- Tracks tool call history (
toolCallResults) with status tracking (PENDING, COMPLETED, ERROR) clearToolCallResultsMethod to clear call historylatestToolCallResultEasy access to the most recent tool call result- Provides error handling within each call result
useMCPListPrompts: Retrieves available prompts from the MCP server.listPromptsMethod to fetch and refresh the prompt list- Provides loading state (
loading) and error handling (error) promptsAccess to the current list of available prompts- Automatically updates the shared context with prompt data
useMCPGetPrompt: Retrieves a specific prompt from the MCP server.getPromptMethod to fetch a prompt with name and arguments- Tracks prompt request history (
getPromptResults) with status tracking clearGetPromptResultsMethod to clear request historylatestGetPromptResultEasy access to the most recent prompt result- Provides error handling within each prompt result
useMCPListResources: Retrieves available resources from the MCP server.listResourcesMethod to fetch and refresh the resource list- Provides loading state (
loading) and error handling (error) resourcesAccess to the current list of available resources- Automatically updates the shared context with resource data
useMCPReadResource: Reads content of a specific resource from the MCP server.readResourceMethod to fetch a resource's content by URI- Tracks resource read history (
readResourceResults) with status tracking clearReadResourceResultsMethod to clear read historylatestReadResourceResultEasy access to the most recent read result- Provides error handling within each read result
useMCPResourceSubscriptions: Manages subscriptions to resources for real-time updates.subscribedResourcesAccess to current list of subscribed resourcessubscribeToResourceMethod to subscribe to a resource by URIunsubscribeFromResourceMethod to unsubscribe from a resource- Tracks subscription history (
resourceSubscriptionResults) with status tracking clearResourceSubscriptionResultsMethod to clear subscription historylatestResourceSubscriptionResultEasy access to the most recent subscription action result
