# @mcp-fe/react-tools
> React hooks and components for seamless integration with MCP (Model Context Protocol) tools.
Simplify dynamic MCP tool integration in React applications with automatic lifecycle management, reference counting, and full access to React features.
> π **Requires**: [@mcp-fe/mcp-worker](https://www.npmjs.com/package/@mcp-fe/mcp-worker) - The browser-based MCP server that powers these React hooks.
## β¨ Key Features
- π **Automatic Lifecycle** - Register on mount, unregister on unmount
- π **Reference Counting** - Share tools across multiple components safely
- β‘ **Re-render Safe** - Smart refs prevent duplicate registrations
- π― **Full React Access** - Handlers have complete access to state, props, and context
- π **Optional Context** - Works standalone or with Provider
- π **TypeScript First** - Complete type safety out of the box
## β οΈ Prerequisites
This library requires **[@mcp-fe/mcp-worker](https://www.npmjs.com/package/@mcp-fe/mcp-worker)** to function. The worker library runs an MCP server in your browser that enables AI agents to interact with your application.
**You must install and initialize the worker library first:**
```bash
npm install @mcp-fe/mcp-worker @mcp-fe/react-tools
```
```bash
pnpm add @mcp-fe/mcp-worker @mcp-fe/react-tools
```
```bash
yarn add @mcp-fe/mcp-worker @mcp-fe/react-tools
```
> π‘ **New to MCP Worker?** Check out the [@mcp-fe/mcp-worker documentation](https://www.npmjs.com/package/@mcp-fe/mcp-worker) to set up the browser-based MCP server first.
## π Quick Example
**Step 1:** Wrap your app with `MCPToolsProvider`:
```tsx
import { MCPToolsProvider } from '@mcp-fe/react-tools';
function App() {
return (
<MCPToolsProvider
backendWsUrl="ws://localhost:3001"
authToken="your-auth-token"
onInitialized={() => console.log('MCP ready!')}
>
<MyApp />
</MCPToolsProvider>
);
}
```
**Step 2:** Register tools with React hooks in your components:
```tsx
import { useMCPTool } from '@mcp-fe/react-tools';
function MyComponent() {
const user = useUser();
useMCPTool({
name: 'get_user_profile',
description: 'Get current user profile',
inputSchema: {
type: 'object',
properties: {}
},
handler: async () => ({
content: [{
type: 'text',
text: JSON.stringify({
userId: user.id,
name: user.name,
email: user.email
})
}]
})
});
return <div>Profile tool is active!</div>;
}
```
> π‘ **Alternative:** You can also initialize the worker client manually outside React. See the [Getting Started Guide](./docs/getting-started.md) for details.
> π **Need more details?** See the [Complete Setup Guide](./docs/getting-started.md) for step-by-step instructions.
## π Documentation
**[π View Complete Documentation β](./docs/index.md)**
Comprehensive documentation is available in the [`docs/`](./docs/) directory:
- **[Getting Started](./docs/getting-started.md)** - Installation, quick start, and basic usage
- **[API Reference](./docs/api-reference.md)** - Complete API documentation for all hooks and utilities
- **[Guides](./docs/guides.md)** - Advanced patterns and best practices
- **[Examples](./docs/examples.md)** - Real-world implementation examples
- **[Architecture](./docs/architecture.md)** - Internal design and data flow
- **[Troubleshooting](./docs/troubleshooting.md)** - Common issues and solutions
## π― Core Hooks
### `useMCPTool`
The main hook for registering MCP tools with full lifecycle management.
### `useMCPGetter`
Simplified hook for getter tools (no input parameters).
### `useMCPAction`
Hook for action tools that accept inputs and perform operations.
[β See full API documentation](./docs/api-reference.md)
## ποΈ Project Links
- [π¦ npm Package](https://www.npmjs.com/package/@mcp-fe/react-tools)
- [π GitHub Repository](https://github.com/mcp-fe/mcp-fe)
- [π Homepage](https://mcp-fe.ai)
- [π Changelog](../../CHANGELOG.md)
## π License
Apache-2.0 - See [LICENSE](../../LICENSE) for details.
---
**Ready to get started?** Check out the [Getting Started Guide](./docs/getting-started.md)!