Skip to content

Connecting

Copy page

The MCP client connects to the server over an SSE transport: a long-lived Server-Sent Events stream for server→client messages, plus a message endpoint for client→server requests. The client handles session setup and reconnection for you.

import { McpClient } from "@divinci-ai/mcp";
const mcp = new McpClient({
serverUrl: "https://mcp.divinci.app",
autoReconnect: true,
maxReconnectAttempts: 5,
});
await mcp.connect();
// ... use mcp.tools, mcp.listResources(), etc.
mcp.disconnect();

getState() returns the current McpConnectionState:

disconnected → connecting → connected → authenticating → authenticated

(or error). Helpers:

mcp.getState(); // current state
mcp.isConnected(); // transport is up
await mcp.isAuthenticated(); // a valid Auth0 token is present

Register handlers with on():

mcp.on({
onStateChange: (state) => console.log("state:", state),
onToolsChanged: (tools) => refreshTools(tools),
onResourcesChanged: (resources) => refreshResources(resources),
onError: (err) => console.error(err),
});
EventFires when
onStateChange(state)Connection state changes.
onToolsChanged(tools)The server’s tool catalog changes.
onResourcesChanged(resources)The server’s resource list changes.
onError(error)A transport or protocol error occurs.

With autoReconnect: true (the default), dropped SSE connections reconnect with exponential backoff up to maxReconnectAttempts. Tune the base delay and ceiling through the constructor.

After connecting, inspect what the server supports:

const caps = mcp.getServerCapabilities();
console.log(caps?.name, caps?.version, caps?.protocolVersion);
await mcp.ping(); // throws if the connection is unhealthy