Creates a new MCP session.
The connector to use for communication (Stdio, HTTP, WebSocket)
Whether to automatically connect during initialization (default: true)
ReadonlyconnectorThe underlying connector managing the transport layer. This is the Stdio, HTTP, or WebSocket connector handling actual communication.
Normalized server metadata for this ready connection.
The negotiated protocol version string for this session's connection.
The negotiated protocol era for this session's connection:
"legacy" (2025-era) or "modern" (2026-07-28-era).
undefined before the connection has negotiated.
Get the server capabilities advertised during initialization.
Server capabilities object
Get the server information (name and version).
Server info object or null if not available
Call a tool on the server.
Name of the tool to call
Arguments to pass to the tool (defaults to empty object)
Optionaloptions: RequestOptions
Optional request options (timeout, progress handlers, etc.)
Result from the tool execution
Request completion suggestions for a prompt or resource template argument.
Completion request parameters
Optionaloptions: RequestOptions
Request options
Completion suggestions from the server
Establishes the connection to the MCP server.
This method starts the underlying transport (spawns process for Stdio, opens WebSocket, etc.) but does not perform the MCP initialization handshake. Call initialize after connecting.
Promise that resolves when connected
Closes the connection to the MCP server.
This method gracefully shuts down the transport and cleans up resources. After disconnecting, the session cannot be used until reconnected.
Promise that resolves when disconnected
connect for establishing connections
Get a specific prompt with arguments.
Name of the prompt to get
Arguments for the prompt
Prompt result
Gets the current roots advertised to the server.
Roots represent directories or files that the client has provided access to. The server may use this information to scope its operations.
Array of Root objects
const roots = session.getRoots();
console.log(`Current roots: ${roots.map(r => r.uri).join(', ')}`);
setRoots for updating roots
Initializes the MCP session with the server.
This method performs the MCP initialization handshake, exchanging
capabilities and metadata with the server. If autoConnect is true
and the session is not yet connected, it will connect first.
After initialization, you can list and call tools, read resources, etc.
Promise that resolves when initialized
const session = await client.createSession('my-server', false);
await session.connect();
await session.initialize();
// Now ready to use
const tools = await session.listTools();
connect for establishing the connection first
List all resources from the server, automatically handling pagination.
Optionaloptions: RequestOptions
Request options
Complete list of all resources
List resources from the server with optional pagination.
Optionalcursor: string
Optional cursor for pagination
Optionaloptions: RequestOptions
Request options
Resource list with optional nextCursor for pagination
List resource templates from the server.
Optionaloptions: RequestOptions
Request options
List of available resource templates
List all available tools from the MCP server.
This method fetches fresh tools from the server, unlike the tools getter which returns cached tools.
Optionaloptions: RequestOptions
Optional request options
Array of available tools
Register an event handler for session events
The event type to listen for
The handler function to call when the event occurs
Send a raw request through the client.
MCP method name
Request parameters
Optionaloptions: RequestOptions
Request options
Response from the server
Set roots and notify the server. Roots represent directories or files that the client has access to.
Array of Root objects with uri (must start with "file://") and optional name
Whether the server advertised a named MCP capability.
A top-level capability name such as "tools" or
"resources".
A ready connection to an MCP server.
The connection has the same public API for legacy, sessionful MCP servers and modern, sessionless MCP servers. The underlying SDK owns the lifecycle distinction and protocol negotiation.
Sessions handle:
Sessions are typically created by
MCPClient.createSession()rather than being instantiated directly.Example
Example
See
BaseConnector for connector implementations