> ## Documentation Index
> Fetch the complete documentation index at: https://mcp-use.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

<AgentInstructions>

## Submitting Feedback

If you encounter incorrect, outdated, or confusing documentation on this page, submit feedback:

POST https://mcp-use.com/docs/feedback

```json
{
  "path": "/python/api-reference/mcp_use_client_connectors_base",
  "feedback": "Description of the issue"
}
```

Only submit feedback when you have something specific and actionable to report.

</AgentInstructions>

# Base

> Base connector for MCP implementations API Documentation

export const RandomGradientBackground = ({className, color, children, grayscaled = false}) => {
  const saturation = useMemo(() => {
    if (color) {
      const values = color.split("(")[1].split(")")[0].trim().split(/\s+/);
      return parseFloat(values[1] || "0");
    }
    return grayscaled ? 0 : 0.2;
  }, [color, grayscaled]);
  const lightness = useMemo(() => {
    if (color) {
      const values = color.split("(")[1].split(")")[0].trim().split(/\s+/);
      return parseFloat(values[0] || "0.5");
    }
    return grayscaled ? 0.3 : 0.4;
  }, [color, grayscaled]);
  const randomHue = useMemo(() => {
    if (color) {
      const values = color.split("(")[1].split(")")[0].trim().split(/\s+/);
      return parseFloat(values[2] || "0");
    }
    return Math.floor(Math.random() * 360);
  }, [color]);
  const randomColor = useMemo(() => {
    if (color) {
      return color;
    }
    return `oklch(${Math.min(lightness, 1)} ${saturation} ${randomHue})`;
  }, [randomHue, saturation, lightness]);
  const lightColor = useMemo(() => {
    return `oklch(${Math.min(lightness * 2, 1)} ${saturation} ${randomHue})`;
  }, [randomHue, saturation, lightness, color]);
  const direction = useMemo(() => {
    return Math.floor(Math.random() * 360);
  }, [randomHue]);
  const brightnessFilter = useMemo(() => {
    return "1000%";
  }, []);
  return <div className={`relative overflow-hidden ${className || ""}`} style={{
    background: `${lightColor}`,
    minHeight: '100%',
    width: '100%'
  }}>
      <div className="absolute inset-0 w-full h-full" style={{
    background: `linear-gradient(${direction}deg, ${randomColor}, transparent), url(https://grainy-gradients.vercel.app/noise.svg)`,
    filter: `contrast(120%) brightness(${brightnessFilter})`,
    backgroundSize: 'cover',
    backgroundRepeat: 'no-repeat'
  }} />
      {children && <div className="relative z-10 w-full h-full">{children}</div>}
    </div>;
};

<Callout type="info" title="Source Code">
  View the source code for this module on GitHub: <a href="https://github.com/mcp-use/mcp-use/blob/main/libraries/python/mcp_use/client/connectors/base.py" target="_blank" rel="noopener noreferrer">[https://github.com/mcp-use/mcp-use/blob/main/libraries/python/mcp\_use/client/connectors/base.py](https://github.com/mcp-use/mcp-use/blob/main/libraries/python/mcp_use/client/connectors/base.py)</a>
</Callout>

Base connector for MCP implementations.

This module provides the base connector interface that all MCP connectors
must implement.

## BaseConnector

<div>
  <RandomGradientBackground className="rounded-lg p-4 w-full h-full rounded-full">
    <div className="text-black">
      <div className="text-black font-bold text-xl mb-2 mt-8"><code className="!text-black">class</code> BaseConnector</div>

      Base class for MCP connectors.

      This class defines the interface that all MCP connectors must implement.
    </div>
  </RandomGradientBackground>

  ```python theme={null}
  from mcp_use.client.connectors.base import BaseConnector
  ```

  <Card type="info">
    ### `method` **init**

    Initialize base connector with common attributes.

    **Parameters**

    > <ParamField body="sampling_callback" type="mcp.client.session.SamplingFnT | None" default="None">   Optional callback to handle sampling requests from servers. </ParamField>
    > <ParamField body="elicitation_callback" type="mcp.client.session.ElicitationFnT | None" default="None">   Optional callback to handle elicitation requests from servers. </ParamField>
    > <ParamField body="message_handler" type="mcp.client.session.MessageHandlerFnT | None" default="None">   Optional callback to handle messages from servers. </ParamField>
    > <ParamField body="logging_callback" type="mcp.client.session.LoggingFnT | None" default="None">   Optional callback to handle log messages from servers. </ParamField>
    > <ParamField body="middleware" type="list[mcp_use.client.middleware.middleware.Middleware] | None" default="None">   Optional list of middleware to apply to requests. </ParamField>
    > <ParamField body="roots" type="list[mcp.types.Root] | None" default="None">   Optional initial list of roots to advertise to the server. </ParamField>
    > <ParamField body="list_roots_callback" type="mcp.client.session.ListRootsFnT | None" default="None">   Optional custom callback to handle roots/list requests. </ParamField>

    **Signature**

    ```python wrap theme={null}
    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):
    ```
  </Card>

  <Card type="info">
    ### `method` call\_tool

    Call an MCP tool with automatic reconnection handling.

    **Parameters**

    > <ParamField body="name" type="str" required="True">   The name of the tool to call. </ParamField>
    > <ParamField body="arguments" type="dict[str, Any]" required="True">   The arguments to pass to the tool. </ParamField>
    > <ParamField body="read_timeout_seconds" type="datetime.timedelta | None" default="None">   timeout seconds when calling tool </ParamField>

    **Returns**

    > <ResponseField name="returns" type="mcp.types.CallToolResult">The result of the tool call.</ResponseField>

    **Signature**

    ```python wrap theme={null}
    def call_tool(
    name: str,
        arguments: dict[str,
        Any],
        read_timeout_seconds: datetime.timedelta | None = None
    ):
    ```
  </Card>

  <Card type="info">
    ### `property` client\_info

    Get the client info for the connector.

    **Returns**

    >     <ResponseField name="returns" type="mcp.types.Implementation" />

    **Signature**

    ```python wrap theme={null}
    def client_info():
    ```
  </Card>

  <Card type="info">
    ### `method` connect

    Establish a connection to the MCP implementation.

    **Signature**

    ```python wrap theme={null}
    def connect():
    ```
  </Card>

  <Card type="info">
    ### `method` disconnect

    Close the connection to the MCP implementation.

    **Signature**

    ```python wrap theme={null}
    def disconnect():
    ```
  </Card>

  <Card type="info">
    ### `method` get\_prompt

    Get a prompt by name.

    **Parameters**

    > <ParamField body="name" type="str" required="True">   Name identifier </ParamField>
    > <ParamField body="arguments" type="dict[str, typing.Any] | None" default="None">   Dictionary of key-value pairs </ParamField>

    **Returns**

    >     <ResponseField name="returns" type="mcp.types.GetPromptResult" />

    **Signature**

    ```python wrap theme={null}
    def get_prompt(name: str, arguments: dict[str, typing.Any] | None = None):
    ```
  </Card>

  <Card type="info">
    ### `method` get\_roots

    Get the current list of roots.

    **Returns**

    > <ResponseField name="returns" type="list[mcp.types.Root]">A copy of the current roots list.</ResponseField>

    **Signature**

    ```python wrap theme={null}
    def get_roots():
    ```
  </Card>

  <Card type="info">
    ### `method` initialize

    Initialize the MCP session and return session information.

    **Returns**

    >     <ResponseField name="returns" type="mcp.types.InitializeResult | None" />

    **Signature**

    ```python wrap theme={null}
    def initialize():
    ```
  </Card>

  <Card type="info">
    ### `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**

    >     <ResponseField name="returns" type="bool" />

    **Signature**

    ```python wrap theme={null}
    def is_connected():
    ```
  </Card>

  <Card type="info">
    ### `method` list\_prompts

    List all available prompts from the MCP implementation.

    **Returns**

    >     <ResponseField name="returns" type="list[mcp.types.Prompt]" />

    **Signature**

    ```python wrap theme={null}
    def list_prompts():
    ```
  </Card>

  <Card type="info">
    ### `method` list\_resources

    List all available resources from the MCP implementation.

    **Returns**

    >     <ResponseField name="returns" type="list[mcp.types.Resource]" />

    **Signature**

    ```python wrap theme={null}
    def list_resources():
    ```
  </Card>

  <Card type="info">
    ### `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**

    >     <ResponseField name="returns" type="mcp.client.session.ListRootsFnT" />

    **Signature**

    ```python wrap theme={null}
    def list_roots_callback():
    ```
  </Card>

  <Card type="info">
    ### `method` list\_tools

    List all available tools from the MCP implementation.

    **Returns**

    >     <ResponseField name="returns" type="list[mcp.types.Tool]" />

    **Signature**

    ```python wrap theme={null}
    def list_tools():
    ```
  </Card>

  <Card type="info">
    ### `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**

    >     <ResponseField name="returns" type="list[mcp.types.Prompt]" />

    **Signature**

    ```python wrap theme={null}
    def prompts():
    ```
  </Card>

  <Card type="info">
    ### `property` public\_identifier

    Get the identifier for the connector.

    **Returns**

    >     <ResponseField name="returns" type="str" />

    **Signature**

    ```python wrap theme={null}
    def public_identifier():
    ```
  </Card>

  <Card type="info">
    ### `method` read\_resource

    Read a resource by URI.

    **Parameters**

    > <ParamField body="uri" type="pydantic.networks.AnyUrl" required="True">   Parameter value </ParamField>

    **Returns**

    >     <ResponseField name="returns" type="mcp.types.ReadResourceResult" />

    **Signature**

    ```python wrap theme={null}
    def read_resource(uri: pydantic.networks.AnyUrl):
    ```
  </Card>

  <Card type="info">
    ### `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**

    >     <ResponseField name="returns" type="list[mcp.types.Resource]" />

    **Signature**

    ```python wrap theme={null}
    def resources():
    ```
  </Card>

  <Card type="info">
    ### `method` set\_roots

    Set the roots and notify the server if connected.

    Roots represent directories or files that the client has access to.

    Example:

    ```python theme={null}
    await connector.set_roots([
        Root(uri="file:///home/user/project", name="My Project"),
        Root(uri="file:///home/user/data"),
    ])
    ```

    **Parameters**

    > <ParamField body="roots" type="list[mcp.types.Root]" required="True">   Array of Root objects with `uri` (must start with "file://") and optional `name`. </ParamField>

    **Signature**

    ```python wrap theme={null}
    def set_roots(roots: list[mcp.types.Root]):
    ```
  </Card>

  <Card type="info">
    ### `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**

    >     <ResponseField name="returns" type="list[mcp.types.Tool]" />

    **Signature**

    ```python wrap theme={null}
    def tools():
    ```
  </Card>
</div>
