Install
Quick start
createKontextClient is the main entry point. It returns a KontextClient that handles authentication, tool discovery, and tool execution.
Two modes
The client operates in one of two modes depending on whether you pass aurl.
Single-endpoint mode
Passurl to connect directly to a specific MCP server:
Hybrid mode
Omiturl to use the Kontext gateway, which aggregates tools from multiple integrations:
client.tools.list().
Configuration reference
| Option | Type | Required | Description |
|---|---|---|---|
clientId | string | Yes | Your application’s client ID from the Kontext dashboard. |
redirectUri | string | Yes | Where OAuth redirects after authorization. Must match your dashboard config. |
url | string | No | MCP server endpoint. When set, enables single-endpoint mode. |
serverUrl | string | No | Kontext API base URL. Defaults to https://api.kontext.dev. |
storage | KontextStorage | No | Token persistence backend. Defaults to MemoryStorage (in-memory). |
sessionKey | string | No | Namespace for storage keys. Use this to isolate tokens per user. |
onAuthRequired | (url: URL) => string | URL | void | Promise<string | URL | void> | Yes | Called when the user needs to authenticate. See Authentication. |
onIntegrationRequired | (url: string, info: { id: string; name?: string }) => void | Promise<void> | No | Called when an integration needs to be connected by the user. |
onStateChange | (state: ClientState) => void | No | Called on every state transition. |
Callbacks
onAuthRequired
Fires when the client needs the user to log in. You receive aURL pointing to the OAuth authorization page. Return the callback URL (as a string or URL) to complete the flow inline, or return void to handle the callback separately via client.auth.handleCallback().
onIntegrationRequired
Fires when a tool call fails because the user has not connected the underlying integration. You receive the connect URL and integration metadata. Open this URL so the user can authorize the integration.onStateChange
Fires on every state transition. Useful for driving loading indicators and error UI.Client state
The client tracks astate property that follows this lifecycle:
| State | Meaning |
|---|---|
idle | Client created, not yet connected. |
connecting | Connection in progress. |
ready | Connected and authenticated. Tools are available. |
needs_auth | Authentication required before proceeding. |
failed | Connection failed due to a non-auth error. |
Event listeners
Subscribe to state changes and errors withclient.on(). The function returns an unsubscribe callback.
on("stateChange") and the onStateChange config callback fire on every transition. Use whichever pattern fits your application — on() is better when you need to add and remove listeners dynamically.
Next steps
- Tools — Discover and execute MCP tools.
- Authentication — OAuth sign-in, callbacks, and session management.
- Storage — Persist tokens with custom storage backends.
- Client Types — Full type reference for
KontextClient,KontextClientConfig, andKontextTool.