Skip to main content
View the source code for this module on GitHub: https://github.com/mcp-use/mcp-use/blob/main/mcp_use/session.py
Session manager for MCP connections. This module provides a session manager for MCP connections, which handles authentication, initialization, and tool discovery.

MCPSession

from mcp_use.session import MCPSession

method init

Initialize a new MCP session.Parameters
connector
mcp_use.connectors.base.BaseConnector
required
The connector to use for communicating with the MCP implementation.
auto_connect
bool
default:"True"
Whether to automatically connect to the MCP implementation.
Signature
def __init__(connector: mcp_use.connectors.base.BaseConnector, auto_connect: bool = True):

method call_tool

Call an MCP tool.Parameters
name
str
required
The name of the tool to call.
arguments
dict[str, Any]
required
The arguments to pass to the tool.
read_timeout_seconds
datetime.timedelta | None
default:"None"
Optional timeout for the tool call.
Returns
returns
mcp.types.CallToolResult
The result of the tool call.
Signature
def call_tool(
name: str,
    arguments: dict[str,
    Any],
    read_timeout_seconds: datetime.timedelta | None = None
):

method connect

Connect to the MCP implementation.Signature
def connect():

method disconnect

Disconnect from the MCP implementation.Signature
def disconnect():

method get_prompt

Get a prompt by name.Parameters
name
str
required
The name of the prompt to get.
arguments
dict[str, typing.Any] | None
default:"None"
Optional arguments for the prompt.
Returns
returns
mcp.types.GetPromptResult
The prompt result with messages.
Signature
def get_prompt(name: str, arguments: dict[str, typing.Any] | None = None):

method initialize

Initialize the MCP session and discover available tools.Returns
returns
dict[str, Any]
The session information returned by the MCP implementation.
Signature
def initialize():

property is_connected

Check if the connector is connected.Returns
returns
bool
Signature
def is_connected():

method list_prompts

List all available prompts from the MCP server.Returns
returns
list[mcp.types.Prompt]
List of available prompts.
Signature
def list_prompts():

method list_resources

List all available resources from the MCP server.Returns
returns
list[mcp.types.Resource]
List of available resources.
Signature
def list_resources():

method list_tools

List all available tools from the MCP server.Returns
returns
list[mcp.types.Tool]
List of available tools.
Signature
def list_tools():

method read_resource

Read a resource by URI.Parameters
uri
pydantic.networks.AnyUrl
required
The URI of the resource to read.
Returns
returns
mcp.types.ReadResourceResult
The resource content.
Signature
def read_resource(uri: pydantic.networks.AnyUrl):
I