Skip to main content

Installation

npm install @frontmcp/react react react-dom

Wrap Your App

Every @frontmcp/react app starts with a FrontMcpProvider. It takes a pre-created DirectMcpServer and manages the MCP client lifecycle.
src/App.tsx
import { create } from '@frontmcp/sdk';
import { FrontMcpProvider } from '@frontmcp/react';
import { GreetTool } from './tools/greet.tool';
import { Dashboard } from './Dashboard';

// Create an in-process MCP server
const server = await create({
  info: { name: 'my-app', version: '1.0.0' },
  tools: [GreetTool],
});

export function App() {
  return (
    <FrontMcpProvider server={server}>
      <Dashboard />
    </FrontMcpProvider>
  );
}
By default the provider auto-connects on mount. Pass autoConnect={false} to connect manually later.

Your First Hook

Use useCallTool to invoke an MCP tool:
src/Dashboard.tsx
import { useCallTool } from '@frontmcp/react';

export function Dashboard() {
  const [callTool, { data, loading, error }] = useCallTool('greet');

  return (
    <div>
      <button onClick={() => callTool({ name: 'Alice' })} disabled={loading}>
        {loading ? 'Calling...' : 'Greet'}
      </button>
      {data && <pre>{JSON.stringify(data, null, 2)}</pre>}
      {error && <p style={{ color: 'red' }}>{error.message}</p>}
    </div>
  );
}

Multi-Server Setup

Connect to multiple MCP servers by passing a servers prop:
src/App.tsx
import { create } from '@frontmcp/sdk';
import { FrontMcpProvider } from '@frontmcp/react';

const mainServer = await create({ info: { name: 'main', version: '1.0.0' }, tools: mainTools });
const analyticsServer = await create({ info: { name: 'analytics', version: '1.0.0' }, tools: analyticsTools });

export function App() {
  return (
    <FrontMcpProvider
      server={mainServer}
      servers={{ analytics: analyticsServer }}
    >
      <Dashboard />
    </FrontMcpProvider>
  );
}
Target a specific server from any hook:
const [callTool, state] = useCallTool('track_event', { server: 'analytics' });

Connection Lifecycle

The provider status progresses through:
StatusDescription
idleNot connected yet (autoConnect={false})
connectingserver.connect() in progress
connectedClient ready, tools/resources/prompts populated
errorConnection failed — see error field
Monitor status with useFrontMcp():
import { useFrontMcp } from '@frontmcp/react';

function StatusBar() {
  const { status, error, tools } = useFrontMcp();
  return (
    <div>
      Status: {status} | Tools: {tools.length}
      {error && <span>{error.message}</span>}
    </div>
  );
}

What’s Next

Provider & Context

Deep dive into FrontMcpProvider props and ServerRegistry

Hooks Reference

Full API for all hooks