|
| 1 | +# MCP Use React Example |
| 2 | + |
| 3 | +This is a React example that demonstrates how to use the `mcp-use` library in a React/browser application. The example shows how to: |
| 4 | + |
| 5 | +- Import and initialize the MCPClient from `mcp-use/browser` |
| 6 | +- Connect to MCP servers via WebSocket or HTTP/SSE |
| 7 | +- Display available tools from connected servers |
| 8 | +- Handle loading states and errors |
| 9 | + |
| 10 | +## Browser Compatibility |
| 11 | + |
| 12 | +The browser version (`mcp-use/browser`) supports: |
| 13 | + |
| 14 | +- ✅ **WebSocket connections**: Connect to MCP servers via WebSocket |
| 15 | +- ✅ **HTTP/SSE connections**: Connect to MCP servers via HTTP or Server-Sent Events |
| 16 | +- ❌ **Stdio connections**: Not supported (requires Node.js child_process) |
| 17 | + |
| 18 | +Example configurations: |
| 19 | + |
| 20 | +```typescript |
| 21 | +// WebSocket connection |
| 22 | +const config = { |
| 23 | + mcpServers: { |
| 24 | + myServer: { |
| 25 | + ws_url: 'ws://localhost:8080', |
| 26 | + authToken: 'optional-token' |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// HTTP connection (with automatic SSE fallback) |
| 32 | +const config = { |
| 33 | + mcpServers: { |
| 34 | + myServer: { |
| 35 | + url: 'http://localhost:8080', |
| 36 | + authToken: 'optional-token', |
| 37 | + preferSse: false // Set to true to force SSE |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## Setup |
| 44 | + |
| 45 | +1. First, build the main `mcp-use` library: |
| 46 | + |
| 47 | + ```bash |
| 48 | + cd ../../ |
| 49 | + pnpm build |
| 50 | + ``` |
| 51 | + |
| 52 | +2. Install dependencies for the React example: |
| 53 | + |
| 54 | + ```bash |
| 55 | + cd examples/react |
| 56 | + pnpm install |
| 57 | + ``` |
| 58 | + |
| 59 | +3. Build the React example: |
| 60 | + |
| 61 | + ```bash |
| 62 | + pnpm build |
| 63 | + ``` |
| 64 | + |
| 65 | +4. Preview the example: |
| 66 | + ```bash |
| 67 | + pnpm preview |
| 68 | + # or use the simple server: |
| 69 | + pnpm serve |
| 70 | + ``` |
| 71 | + |
| 72 | +## Development |
| 73 | + |
| 74 | +To run in development mode: |
| 75 | + |
| 76 | +```bash |
| 77 | +pnpm dev |
| 78 | +``` |
| 79 | + |
| 80 | +This will start a development server with hot reloading. |
| 81 | + |
| 82 | +## Features |
| 83 | + |
| 84 | +The React example includes: |
| 85 | + |
| 86 | +- **MCPTools Component**: A React component that displays available tools from MCP servers |
| 87 | +- **Tool Display**: Shows tool names, descriptions, and input schemas |
| 88 | +- **Server Management**: Connect/disconnect from MCP servers |
| 89 | +- **Error Handling**: Displays connection errors and loading states |
| 90 | +- **Responsive UI**: Clean, modern interface for exploring MCP tools |
| 91 | + |
| 92 | +## Configuration |
| 93 | + |
| 94 | +The example uses a default configuration with a filesystem server. You can modify the `exampleConfig` in `react_example.tsx` to use different MCP servers. |
| 95 | + |
| 96 | +## File Structure |
| 97 | + |
| 98 | +- `index.tsx` - Entry point for the React application |
| 99 | +- `react_example.tsx` - Main React component with MCP integration |
| 100 | +- `react_example.html` - HTML template |
| 101 | +- `vite.config.ts` - Vite bundler configuration (includes browser polyfills) |
| 102 | +- `package.json` - Dependencies and scripts |
| 103 | +- `tsconfig.json` - TypeScript configuration |
| 104 | + |
| 105 | +## Important Notes |
| 106 | + |
| 107 | +### Vite Configuration |
| 108 | + |
| 109 | +The `vite.config.ts` includes necessary polyfills for browser compatibility: |
| 110 | + |
| 111 | +```typescript |
| 112 | +const config = { |
| 113 | + define: { |
| 114 | + 'global': 'globalThis', |
| 115 | + 'process.env.DEBUG': 'undefined', |
| 116 | + 'process.platform': '""', |
| 117 | + 'process.version': '""', |
| 118 | + 'process.argv': '[]', |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +These definitions ensure that Node.js-specific code paths are properly handled in the browser environment. |
| 124 | + |
| 125 | +### Real MCP Client |
| 126 | + |
| 127 | +This example uses the **actual** MCP client code from `mcp-use/browser`, not mocks. It includes: |
| 128 | + |
| 129 | +- Real WebSocket and HTTP/SSE connectors |
| 130 | +- Full MCP protocol implementation |
| 131 | +- Actual tool listing and execution capabilities |
| 132 | +- Browser-safe logging (falls back to console) |
0 commit comments