Connecting
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.
Connect and disconnect
Section titled “Connect and disconnect”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();Connection state
Section titled “Connection state”getState() returns the current McpConnectionState:
disconnected → connecting → connected → authenticating → authenticated(or error). Helpers:
mcp.getState(); // current statemcp.isConnected(); // transport is upawait mcp.isAuthenticated(); // a valid Auth0 token is presentEvents
Section titled “Events”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),});| Event | Fires 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. |
Reconnection
Section titled “Reconnection”With autoReconnect: true (the default), dropped SSE connections reconnect with
exponential backoff up to maxReconnectAttempts. Tune the base delay and ceiling
through the constructor.
Server capabilities
Section titled “Server capabilities”After connecting, inspect what the server supports:
const caps = mcp.getServerCapabilities();console.log(caps?.name, caps?.version, caps?.protocolVersion);Health check
Section titled “Health check”await mcp.ping(); // throws if the connection is unhealthy