Skip to main content

FrontMcpProvider

The provider manages the MCP client lifecycle and populates the shared ServerRegistry.
import { FrontMcpProvider } from '@frontmcp/react';

<FrontMcpProvider
  server={server}
  name="main"
  servers={{ analytics: analyticsServer }}
  components={{ 'component://Chart': ChartComponent }}
  autoConnect={true}
  onConnected={(client) => console.log('Connected', client)}
  onError={(err) => console.error('Failed', err)}
>
  {children}
</FrontMcpProvider>

Props

PropTypeDefaultDescription
serverDirectMcpServerRequired. Primary MCP server instance
namestring'default'Logical name for the primary server in the registry
serversRecord<string, DirectMcpServer>Additional named servers
componentsRecord<string, ComponentType>Components for DynamicRenderer and ComponentRegistry
autoConnectbooleantrueConnect on mount
childrenReactNodeRequired. Child elements
onConnected(client: DirectClient) => voidCalled after successful connection
onError(error: Error) => voidCalled if connection fails

Lifecycle

  1. On mount, registers the primary server (and any servers) in ServerRegistry
  2. If autoConnect is true, calls server.connect() and discovers tools/resources/prompts
  3. Updates registry entry with status: 'connected' and populates tool/resource/prompt lists
  4. Also auto-connects additional servers from servers prop
  5. On unmount, unregisters all servers from the registry

FrontMcpContext

The React context carries a slim value — no state, just pointers:
interface FrontMcpContextValue {
  name: string;                    // Server name for this provider
  registry: ComponentRegistry;     // Component registry for DynamicRenderer
  connect: () => Promise<void>;    // Manual connect trigger
}
All state (status, tools, etc.) lives in ServerRegistry and is accessed via hooks.

ServerRegistry

A singleton that stores all server entries and notifies subscribers on change.
import { serverRegistry } from '@frontmcp/react';

// Check if a server exists
serverRegistry.has('analytics'); // boolean

// Get a server entry
const entry = serverRegistry.get('analytics');
// entry.status, entry.client, entry.tools, etc.

// Subscribe to changes
const unsub = serverRegistry.subscribe(() => {
  console.log('Registry changed');
});

ServerEntry

FieldTypeDescription
serverDirectMcpServerThe server instance
clientDirectClient | nullConnected client (null until connected)
statusFrontMcpStatus'idle' | 'connecting' | 'connected' | 'error'
errorError | nullConnection error
toolsToolInfo[]Discovered tools
resourcesResourceInfo[]Discovered resources
resourceTemplatesResourceTemplateInfo[]Discovered resource templates
promptsPromptInfo[]Discovered prompts

useFrontMcp

The primary hook for reading the resolved server state:
import { useFrontMcp } from '@frontmcp/react';

function MyComponent() {
  const {
    name,            // 'default'
    server,          // DirectMcpServer | null
    client,          // DirectClient | null
    status,          // 'idle' | 'connecting' | 'connected' | 'error'
    error,           // Error | null
    tools,           // ToolInfo[]
    resources,       // ResourceInfo[]
    resourceTemplates, // ResourceTemplateInfo[]
    prompts,         // PromptInfo[]
    registry,        // ComponentRegistry
    connect,         // () => Promise<void>
  } = useFrontMcp();
}
Pass a server name to target a specific server:
const analytics = useFrontMcp('analytics');