View the source code for this module on GitHub: https://github.com/mcp-use/mcp-use/blob/main/libraries/typescript/packages/mcp-use/src/session.ts
MCPSession represents one live connection to one MCP server. It wraps a connector and exposes high-level methods for lifecycle, tools, resources, prompts, completion, roots, notifications, and raw requests.
Sessions are usually created with MCPClient.createSession().
Constructor
Creates a session around a connector. This is an advanced path; most applications should create sessions throughMCPClient.
Parameters
SignatureConnector that owns the transport and protocol client.Whentrue,initialize()connects before running the MCP initialize handshake.
Lifecycle
connect
Starts the underlying transport without running the MCP initialize handshake. ReturnsSignature
initialize
Runs the MCP initialize handshake. IfautoConnect is true and the session is not connected, this method connects first.
Returns
Example
disconnect
Closes the underlying transport and releases connection resources. ReturnsSignature
isConnected
Returns whether the underlying connector is connected. ReturnsSignature
Tools
tools
Returns the connector’s cached tool list. ReturnsSignature
listTools
Fetches the current tool list from the server. ParametersReturnsOptional MCP SDK request options.
Signature
callTool
Calls a tool on the server. ParametersReturnsTool name.Tool arguments.Optional MCP SDK request options, such as timeout and progress handling.
Example
Resources
listResources
Lists resources from the server with optional pagination. ParametersReturnsPagination cursor returned by a previous response.Optional MCP SDK request options.
Signature
listAllResources
Lists every resource from the server by following pagination. ParametersReturnsOptional MCP SDK request options.
Signature
listResourceTemplates
Lists parameterized resource templates exposed by the server. ParametersReturnsOptional MCP SDK request options.
Signature
readResource
Reads a resource by URI. ParametersReturnsResource URI.Optional MCP SDK request options.
Example
subscribeToResource
Subscribes to resource update notifications for a URI. ParametersReturnsResource URI to subscribe to.Optional MCP SDK request options.
Signature
unsubscribeFromResource
Unsubscribes from resource update notifications for a URI. ParametersReturnsResource URI to unsubscribe from.Optional MCP SDK request options.
Signature
Prompts and completion
listPrompts
Lists prompt templates exposed by the server. ReturnsSignature
getPrompt
Fetches a prompt by name with arguments. ParametersReturnsPrompt name.Prompt arguments.
Signature
complete
Requests completion suggestions for a prompt or resource template argument. ParametersReturnsCompletion request parameters.Optional MCP SDK request options.
Example
Roots and notifications
setRoots
Sets the roots advertised to the server and sends the corresponding notification. ParametersReturnsRoots withfile://URIs and optional names.
Example
getRoots
Returns the roots currently advertised to the server. ReturnsSignature
on
Registers a session event handler. The current public event is"notification".
Parameters
ReturnsEvent name.Handler called with each notification.
Example
Server metadata
serverCapabilities
Returns server capabilities from the initialize response. ReturnsSignature
serverInfo
Returns server name and version from the initialize response, ornull when unavailable.
Returns
Signature
Raw requests
request
Sends a raw JSON-RPC request through the connector. Use this for custom MCP methods when no helper exists. ParametersReturnsMCP method name.Request parameters.Optional MCP SDK request options.
Signature
Types
Root
Directory or file root advertised to a server. FieldsRoot URI. Usefile://URIs.Optional display name.
Tool
Tool metadata returned bylistTools() and the tools getter. This type is re-exported from the MCP SDK.
CallToolResult
Result returned bycallTool(). This type is re-exported from the MCP SDK.
Notification
Server notification object passed to notification handlers. This type is re-exported from the MCP SDK.Related
MCPClient: create sessions and manage client configuration.- Tools: guide to listing and calling tools.
- Resources: guide to reading resources.
- Prompts: guide to listing and getting prompts.
- Notifications: guide to notification handlers and roots.