Skip to main content
Base connector for MCP implementations. This module provides the base connector interface that all MCP connectors must implement.

BaseConnector

from mcp_use.client.connectors.base import BaseConnector

method init

Initialize base connector with common attributes.Parameters
sampling_callback
mcp.client.session.SamplingFnT | None
default:"None"
Optional callback to handle sampling requests from servers.
elicitation_callback
mcp.client.session.ElicitationFnT | None
default:"None"
Optional callback to handle elicitation requests from servers.
message_handler
mcp.client.session.MessageHandlerFnT | None
default:"None"
Optional callback to handle messages from servers.
logging_callback
mcp.client.session.LoggingFnT | None
default:"None"
Optional callback to handle log messages from servers.
middleware
list[mcp_use.client.middleware.middleware.Middleware] | None
default:"None"
Optional list of middleware to apply to requests.
roots
list[mcp.types.Root] | None
default:"None"
Optional initial list of roots to advertise to the server.
list_roots_callback
mcp.client.session.ListRootsFnT | None
default:"None"
Optional custom callback to handle roots/list requests.
Signature
def __init__(sampling_callback: mcp.client.session.SamplingFnT | None = None, elicitation_callback: mcp.client.session.ElicitationFnT | None = None, message_handler: mcp.client.session.MessageHandlerFnT | None = None, logging_callback: mcp.client.session.LoggingFnT | None = None, middleware: list[mcp_use.client.middleware.middleware.Middleware] | None = None, roots: list[mcp.types.Root] | None = None, list_roots_callback: mcp.client.session.ListRootsFnT | None = None):

method call_tool

Call an MCP tool with automatic reconnection handling.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"
timeout seconds when calling tool
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
):

property client_info

Get the client info for the connector.Returns
returns
mcp.types.Implementation
Signature
def client_info():

method connect

Establish a connection to the MCP implementation.Signature
def connect():

method disconnect

Close the connection to the MCP implementation.Signature
def disconnect():

method get_prompt

Get a prompt by name.Parameters
name
str
required
Name identifier
arguments
dict[str, typing.Any] | None
default:"None"
Dictionary of key-value pairs
Returns
returns
mcp.types.GetPromptResult
Signature
def get_prompt(name: str, arguments: dict[str, typing.Any] | None = None):

method get_roots

Get the current list of roots.Returns
returns
list[mcp.types.Root]
A copy of the current roots list.
Signature
def get_roots():

method initialize

Initialize the MCP session and return session information.Returns
returns
mcp.types.InitializeResult | None
Signature
def initialize():

property is_connected

Check if the connector is actually connected and the connection is alive.This property checks not only the connected flag but also verifies that the underlying connection manager and streams are still active.Returns
returns
bool
Signature
def is_connected():

method list_prompts

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

method list_resources

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

property list_roots_callback

Get the list_roots_callback to pass to ClientSession.This always returns a callback to ensure the roots capability is advertised.Returns
returns
mcp.client.session.ListRootsFnT
Signature
def list_roots_callback():

method list_tools

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

property prompts

Get the list of available prompts... deprecated:: This property is deprecated because it may return stale data when the server sends list change notifications. Use `await list_prompts()’ instead to ensure you always get the latest data.Returns
returns
list[mcp.types.Prompt]
Signature
def prompts():

property public_identifier

Get the identifier for the connector.Returns
returns
str
Signature
def public_identifier():

method read_resource

Read a resource by URI.Parameters
uri
pydantic.networks.AnyUrl
required
Parameter value
Returns
returns
mcp.types.ReadResourceResult
Signature
def read_resource(uri: pydantic.networks.AnyUrl):

property resources

Get the list of available resources... deprecated:: This property is deprecated because it may return stale data when the server sends list change notifications. Use await list_resources() instead to ensure you always get the latest data.Returns
returns
list[mcp.types.Resource]
Signature
def resources():

method set_roots

Set the roots and notify the server if connected.Roots represent directories or files that the client has access to.Example:
await connector.set_roots([
    Root(uri="file:///home/user/project", name="My Project"),
    Root(uri="file:///home/user/data"),
])
Parameters
roots
list[mcp.types.Root]
required
Array of Root objects with uri (must start with “file://”) and optional name.
Signature
def set_roots(roots: list[mcp.types.Root]):

property tools

Get the list of available tools... deprecated:: This property is deprecated because it may return stale data when the server sends list change notifications. Use await list_tools() instead to ensure you always get the latest data.Returns
returns
list[mcp.types.Tool]
Signature
def tools():