Skip to content

vllm_mlx.mcp.client

MCP client for connecting to individual MCP servers.

View the complete module source at #L1-L328.

API details

Each callable below includes its exact signature, type annotations, inputs, defaults, return contract, documented exceptions, implementation source, and parsed docstring sections when the source provides them.

vllm_mlx.mcp.client

MCP client for connecting to individual MCP servers.

vllm_mlx.mcp.client.logger module-attribute

logger = logging.getLogger(__name__)

vllm_mlx.mcp.client.MCPClient

MCPClient(config: MCPServerConfig)

Client for connecting to a single MCP server.

Supports both stdio and SSE transports.

Initialize MCP client.

Parameters:

Source code in vllm_mlx/mcp/client.py
def __init__(self, config: MCPServerConfig):
    """
    Initialize MCP client.

    Args:
        config: Server configuration
    """
    self.config = config
    self._session = None
    self._read = None
    self._write = None
    self._tools: List[MCPTool] = []
    self._state = MCPServerState.DISCONNECTED
    self._error: Optional[str] = None
    self._last_connected: Optional[float] = None
    self._lock = asyncio.Lock()

vllm_mlx.mcp.client.MCPClient.config instance-attribute

config = config

vllm_mlx.mcp.client.MCPClient._session instance-attribute

_session = None

vllm_mlx.mcp.client.MCPClient._read instance-attribute

_read = None

vllm_mlx.mcp.client.MCPClient._write instance-attribute

_write = None

vllm_mlx.mcp.client.MCPClient._tools instance-attribute

_tools: List[MCPTool] = []

vllm_mlx.mcp.client.MCPClient._state instance-attribute

vllm_mlx.mcp.client.MCPClient._error instance-attribute

_error: Optional[str] = None

vllm_mlx.mcp.client.MCPClient._last_connected instance-attribute

_last_connected: Optional[float] = None

vllm_mlx.mcp.client.MCPClient._lock instance-attribute

_lock = asyncio.Lock()

vllm_mlx.mcp.client.MCPClient.name property

name: str

Get server name.

vllm_mlx.mcp.client.MCPClient.state property

Get current connection state.

vllm_mlx.mcp.client.MCPClient.is_connected property

is_connected: bool

Check if connected to server.

vllm_mlx.mcp.client.MCPClient.tools property

tools: List[MCPTool]

Get discovered tools.

vllm_mlx.mcp.client.MCPClient.get_status

get_status() -> MCPServerStatus

Get server status.

Source code in vllm_mlx/mcp/client.py
def get_status(self) -> MCPServerStatus:
    """Get server status."""
    return MCPServerStatus(
        name=self.name,
        state=self._state,
        transport=self.config.transport,
        tools_count=len(self._tools),
        error=self._error,
        last_connected=self._last_connected,
    )

vllm_mlx.mcp.client.MCPClient.connect async

connect() -> bool

Connect to the MCP server.

Returns:

  • bool

    True if connection successful, False otherwise

Source code in vllm_mlx/mcp/client.py
async def connect(self) -> bool:
    """
    Connect to the MCP server.

    Returns:
        True if connection successful, False otherwise
    """
    async with self._lock:
        if self._state == MCPServerState.CONNECTED:
            return True

        if not self.config.enabled:
            logger.info(f"MCP server '{self.name}' is disabled")
            return False

        self._state = MCPServerState.CONNECTING
        self._error = None

        try:
            if self.config.transport == MCPTransport.STDIO:
                await self._connect_stdio()
            elif self.config.transport == MCPTransport.SSE:
                await self._connect_sse()
            else:
                raise ValueError(f"Unknown transport: {self.config.transport}")

            # Initialize session
            await self._initialize_session()

            # Discover tools
            await self._discover_tools()

            self._state = MCPServerState.CONNECTED
            self._last_connected = time.time()
            logger.info(
                f"Connected to MCP server '{self.name}' "
                f"({len(self._tools)} tools available)"
            )
            return True

        except Exception as e:
            self._state = MCPServerState.ERROR
            self._error = str(e)
            logger.error(f"Failed to connect to MCP server '{self.name}': {e}")
            return False

vllm_mlx.mcp.client.MCPClient._connect_stdio async

_connect_stdio()

Connect via stdio transport.

Source code in vllm_mlx/mcp/client.py
async def _connect_stdio(self):
    """Connect via stdio transport."""
    try:
        from mcp import ClientSession, StdioServerParameters
        from mcp.client.stdio import stdio_client
    except ImportError:
        raise ImportError(
            "MCP SDK required for MCP support. Install with: pip install mcp"
        )

    # Security: Log the command being executed for audit trail
    logger.info(
        f"MCP SECURITY AUDIT: Server '{self.name}' executing command: "
        f"{self.config.command} {' '.join(self.config.args or [])}"
    )

    server_params = StdioServerParameters(
        command=self.config.command,
        args=self.config.args or [],
        env=self.config.env,
    )

    # Create stdio client context
    self._stdio_client = stdio_client(server_params)
    self._read, self._write = await self._stdio_client.__aenter__()

    # Create session
    self._session = ClientSession(self._read, self._write)
    await self._session.__aenter__()

vllm_mlx.mcp.client.MCPClient._connect_sse async

_connect_sse()

Connect via SSE transport.

Source code in vllm_mlx/mcp/client.py
async def _connect_sse(self):
    """Connect via SSE transport."""
    try:
        from mcp import ClientSession
        from mcp.client.sse import sse_client
    except ImportError:
        raise ImportError(
            "MCP SDK required for MCP support. Install with: pip install mcp"
        )

    # Create SSE client context
    self._sse_client = sse_client(self.config.url)
    self._read, self._write = await self._sse_client.__aenter__()

    # Create session
    self._session = ClientSession(self._read, self._write)
    await self._session.__aenter__()

vllm_mlx.mcp.client.MCPClient._initialize_session async

_initialize_session()

Initialize the MCP session.

Source code in vllm_mlx/mcp/client.py
async def _initialize_session(self):
    """Initialize the MCP session."""
    if self._session is None:
        raise RuntimeError("Session not created")

    # Initialize with capabilities
    result = await self._session.initialize()
    logger.debug(
        f"MCP server '{self.name}' initialized: "
        f"protocol={result.protocolVersion}, "
        f"server={result.serverInfo.name if result.serverInfo else 'unknown'}"
    )

vllm_mlx.mcp.client.MCPClient._discover_tools async

_discover_tools()

Discover available tools from the server.

Source code in vllm_mlx/mcp/client.py
async def _discover_tools(self):
    """Discover available tools from the server."""
    if self._session is None:
        raise RuntimeError("Session not initialized")

    try:
        result = await self._session.list_tools()
        self._tools = []

        for tool in result.tools:
            mcp_tool = MCPTool(
                server_name=self.name,
                name=tool.name,
                description=tool.description or "",
                input_schema=(
                    tool.inputSchema if hasattr(tool, "inputSchema") else {}
                ),
            )
            self._tools.append(mcp_tool)
            logger.debug(f"Discovered tool: {mcp_tool.full_name}")

    except Exception as e:
        logger.warning(f"Failed to discover tools from '{self.name}': {e}")
        self._tools = []

vllm_mlx.mcp.client.MCPClient.disconnect async

disconnect()

Disconnect from the MCP server.

Source code in vllm_mlx/mcp/client.py
async def disconnect(self):
    """Disconnect from the MCP server."""
    async with self._lock:
        if self._state == MCPServerState.DISCONNECTED:
            return

        try:
            if self._session:
                await self._session.__aexit__(None, None, None)
                self._session = None

            if hasattr(self, "_stdio_client") and self._stdio_client:
                await self._stdio_client.__aexit__(None, None, None)
                self._stdio_client = None

            if hasattr(self, "_sse_client") and self._sse_client:
                await self._sse_client.__aexit__(None, None, None)
                self._sse_client = None

        except Exception as e:
            logger.warning(f"Error disconnecting from '{self.name}': {e}")

        finally:
            self._state = MCPServerState.DISCONNECTED
            self._tools = []
            logger.info(f"Disconnected from MCP server '{self.name}'")

vllm_mlx.mcp.client.MCPClient.call_tool async

call_tool(tool_name: str, arguments: Dict[str, Any], timeout: Optional[float] = None) -> MCPToolResult

Call a tool on the MCP server.

Parameters:

  • tool_name (str) –

    Name of the tool (without server prefix)

  • arguments (Dict[str, Any]) –

    Tool arguments

  • timeout (Optional[float], default: None ) –

    Optional timeout in seconds

Returns:

Source code in vllm_mlx/mcp/client.py
async def call_tool(
    self,
    tool_name: str,
    arguments: Dict[str, Any],
    timeout: Optional[float] = None,
) -> MCPToolResult:
    """
    Call a tool on the MCP server.

    Args:
        tool_name: Name of the tool (without server prefix)
        arguments: Tool arguments
        timeout: Optional timeout in seconds

    Returns:
        MCPToolResult with the result or error
    """
    if not self.is_connected:
        return MCPToolResult(
            tool_name=tool_name,
            content=None,
            is_error=True,
            error_message=f"Not connected to server '{self.name}'",
        )

    if self._session is None:
        return MCPToolResult(
            tool_name=tool_name,
            content=None,
            is_error=True,
            error_message="Session not initialized",
        )

    try:
        # Call with timeout
        timeout = timeout or self.config.timeout

        result = await asyncio.wait_for(
            self._session.call_tool(tool_name, arguments),
            timeout=timeout,
        )

        # Extract content from result
        content = self._extract_content(result)

        return MCPToolResult(
            tool_name=tool_name,
            content=content,
            is_error=result.isError if hasattr(result, "isError") else False,
        )

    except asyncio.TimeoutError:
        return MCPToolResult(
            tool_name=tool_name,
            content=None,
            is_error=True,
            error_message=f"Tool call timed out after {timeout}s",
        )
    except Exception as e:
        return MCPToolResult(
            tool_name=tool_name,
            content=None,
            is_error=True,
            error_message=str(e),
        )

vllm_mlx.mcp.client.MCPClient._extract_content

_extract_content(result) -> Any

Extract content from MCP tool result.

Source code in vllm_mlx/mcp/client.py
def _extract_content(self, result) -> Any:
    """Extract content from MCP tool result."""
    if not hasattr(result, "content") or not result.content:
        return None

    # Handle list of content items
    contents = []
    for item in result.content:
        if hasattr(item, "text"):
            contents.append(item.text)
        elif hasattr(item, "data"):
            contents.append(item.data)
        else:
            contents.append(str(item))

    # Return single item or list
    if len(contents) == 1:
        return contents[0]
    return contents

vllm_mlx.mcp.client.MCPClient.refresh_tools async

refresh_tools()

Refresh the list of available tools.

Source code in vllm_mlx/mcp/client.py
async def refresh_tools(self):
    """Refresh the list of available tools."""
    if not self.is_connected:
        return

    await self._discover_tools()

Complete contract reference

Expand any definition for its exact inputs, annotations, defaults, return contract, directly raised exceptions, source-grounded behavior, and immutable line link. This section includes private and nested definitions that ordinary API generators omit.

vllm_mlx.mcp.client.MCPClient · class
vllm_mlx.mcp.client.MCPClient(config: MCPServerConfig)

Client for connecting to a single MCP server.

Parameters

Name Type Required Default Description
config MCPServerConfig yes none Server configuration

Returns

  • Constructs: vllm_mlx.mcp.client.MCPClient

Exceptions and behavior

Class MCPClient declares 15 direct member(s). No direct raise statement appears in this definition.

View source #L23-L328.

vllm_mlx.mcp.client.MCPClient.__init__ · method
vllm_mlx.mcp.client.MCPClient.__init__(config: MCPServerConfig) -> not annotated

Initialize MCP client.

Parameters

Name Type Required Default Description
config MCPServerConfig yes none Server configuration

Returns

  • Type: not annotated

Exceptions and behavior

Method MCPClient.__init__ updates self.config, self._session, self._read, self._write; calls asyncio.Lock. No direct raise statement appears in this definition.

View source #L30-L45.

vllm_mlx.mcp.client.MCPClient.name · method
vllm_mlx.mcp.client.MCPClient.name() -> str

Get server name.

Parameters

This callable has no explicit inputs.

Returns

  • Type: str
  • Direct return expressions: self.config.name

Exceptions and behavior

Method MCPClient.name returns self.config.name. No direct raise statement appears in this definition.

View source #L48-L50.

vllm_mlx.mcp.client.MCPClient.state · method
vllm_mlx.mcp.client.MCPClient.state() -> MCPServerState

Get current connection state.

Parameters

This callable has no explicit inputs.

Returns

  • Type: MCPServerState
  • Direct return expressions: self._state

Exceptions and behavior

Method MCPClient.state returns self._state. No direct raise statement appears in this definition.

View source #L53-L55.

vllm_mlx.mcp.client.MCPClient.is_connected · method
vllm_mlx.mcp.client.MCPClient.is_connected() -> bool

Check if connected to server.

Parameters

This callable has no explicit inputs.

Returns

  • Type: bool
  • Direct return expressions: self._state == MCPServerState.CONNECTED

Exceptions and behavior

Method MCPClient.is_connected returns self._state == MCPServerState.CONNECTED. No direct raise statement appears in this definition.

View source #L58-L60.

vllm_mlx.mcp.client.MCPClient.tools · method
vllm_mlx.mcp.client.MCPClient.tools() -> List[MCPTool]

Get discovered tools.

Parameters

This callable has no explicit inputs.

Returns

  • Type: List[MCPTool]
  • Direct return expressions: self._tools

Exceptions and behavior

Method MCPClient.tools returns self._tools. No direct raise statement appears in this definition.

View source #L63-L65.

vllm_mlx.mcp.client.MCPClient.get_status · method
vllm_mlx.mcp.client.MCPClient.get_status() -> MCPServerStatus

Get server status.

Parameters

This callable has no explicit inputs.

Returns

  • Type: MCPServerStatus
  • Direct return expressions: MCPServerStatus(name=self.name, state=self._state, transport=self.config.transport, tools_count=len(self._tools), error…

Exceptions and behavior

Method MCPClient.get_status calls MCPServerStatus, len; returns MCPServerStatus(name=self.name, state=self._state, transport=self.config.transport, tools_count=len(self._tools), error…. No direct raise statement appears in this definition.

View source #L67-L76.

vllm_mlx.mcp.client.MCPClient.connect · method
async vllm_mlx.mcp.client.MCPClient.connect() -> bool

Connect to the MCP server.

Parameters

This callable has no explicit inputs.

Returns

  • Type: bool
  • Direct return expressions: True; False

Exceptions and behavior

Method MCPClient.connect updates self._state, self._error, self._last_connected; calls logger.info, self._connect_stdio, self._connect_sse, ValueError; awaits asynchronous work; can raise ValueError; has 2 explicit return paths. Directly raised exceptions: ValueError.

View source #L78-L122.

vllm_mlx.mcp.client.MCPClient._connect_stdio · method
async vllm_mlx.mcp.client.MCPClient._connect_stdio() -> not annotated

Connect via stdio transport.

Parameters

This callable has no explicit inputs.

Returns

  • Type: not annotated

Exceptions and behavior

Method MCPClient._connect_stdio updates self._stdio_client, self._read, self._write, self._session; calls ImportError, logger.info, ' '.join, StdioServerParameters; awaits asynchronous work; can raise ImportError. Directly raised exceptions: ImportError.

View source #L124-L152.

vllm_mlx.mcp.client.MCPClient._connect_sse · method
async vllm_mlx.mcp.client.MCPClient._connect_sse() -> not annotated

Connect via SSE transport.

Parameters

This callable has no explicit inputs.

Returns

  • Type: not annotated

Exceptions and behavior

Method MCPClient._connect_sse updates self._sse_client, self._read, self._write, self._session; calls ImportError, sse_client, self._sse_client.__aenter__, ClientSession; awaits asynchronous work; can raise ImportError. Directly raised exceptions: ImportError.

View source #L154-L170.

vllm_mlx.mcp.client.MCPClient._initialize_session · method
async vllm_mlx.mcp.client.MCPClient._initialize_session() -> not annotated

Initialize the MCP session.

Parameters

This callable has no explicit inputs.

Returns

  • Type: not annotated

Exceptions and behavior

Method MCPClient._initialize_session calls RuntimeError, self._session.initialize, logger.debug; awaits asynchronous work; can raise RuntimeError. Directly raised exceptions: RuntimeError.

View source #L172-L183.

vllm_mlx.mcp.client.MCPClient._discover_tools · method
async vllm_mlx.mcp.client.MCPClient._discover_tools() -> not annotated

Discover available tools from the server.

Parameters

This callable has no explicit inputs.

Returns

  • Type: not annotated

Exceptions and behavior

Method MCPClient._discover_tools updates self._tools; calls RuntimeError, self._session.list_tools, MCPTool, hasattr; awaits asynchronous work; can raise RuntimeError. Directly raised exceptions: RuntimeError.

View source #L185-L208.

vllm_mlx.mcp.client.MCPClient.disconnect · method
async vllm_mlx.mcp.client.MCPClient.disconnect() -> not annotated

Disconnect from the MCP server.

Parameters

This callable has no explicit inputs.

Returns

  • Type: not annotated
  • Direct return expressions: None

Exceptions and behavior

Method MCPClient.disconnect updates self._session, self._stdio_client, self._sse_client, self._state; calls self._session.__aexit__, hasattr, self._stdio_client.__aexit__, self._sse_client.__aexit__; awaits asynchronous work; returns None. No direct raise statement appears in this definition.

View source #L210-L235.

vllm_mlx.mcp.client.MCPClient.call_tool · method
async vllm_mlx.mcp.client.MCPClient.call_tool(tool_name: str, arguments: Dict[str, Any], timeout: Optional[float] = None) -> MCPToolResult

Call a tool on the MCP server.

Parameters

Name Type Required Default Description
tool_name str yes none Name of the tool (without server prefix)
arguments Dict[str, Any] yes none Tool arguments
timeout Optional[float] no None Optional timeout in seconds

Returns

  • Type: MCPToolResult
  • Direct return expressions: MCPToolResult(tool_name=tool_name, content=None, is_error=True, error_message=f"Not connected to server '{self.name}'"); MCPToolResult(tool_name=tool_name, content=None, is_error=True, error_message='Session not initialized'); MCPToolResult(tool_name=tool_name, content=content, is_error=result.isError if hasattr(result, 'isError') else False); MCPToolResult(tool_name=tool_name, content=None, is_error=True, error_message=f'Tool call timed out after {timeout}s'); MCPToolResult(tool_name=tool_name, content=None, is_error=True, error_message=str(e))

Exceptions and behavior

Method MCPClient.call_tool calls MCPToolResult, asyncio.wait_for, self._session.call_tool, self._extract_content; awaits asynchronous work; has 5 explicit return paths. No direct raise statement appears in this definition.

View source #L237-L301.

vllm_mlx.mcp.client.MCPClient._extract_content · method
vllm_mlx.mcp.client.MCPClient._extract_content(result) -> Any

Extract content from MCP tool result.

Parameters

Name Type Required Default Description
result not annotated yes none Required positional or keyword input.

Returns

  • Type: Any
  • Direct return expressions: None; contents[0]; contents

Exceptions and behavior

Method MCPClient._extract_content calls hasattr, contents.append, str, len; has 3 explicit return paths. No direct raise statement appears in this definition.

View source #L303-L321.

vllm_mlx.mcp.client.MCPClient.refresh_tools · method
async vllm_mlx.mcp.client.MCPClient.refresh_tools() -> not annotated

Refresh the list of available tools.

Parameters

This callable has no explicit inputs.

Returns

  • Type: not annotated
  • Direct return expressions: None

Exceptions and behavior

Method MCPClient.refresh_tools calls self._discover_tools; awaits asynchronous work; returns None. No direct raise statement appears in this definition.

View source #L323-L328.

Complete symbol map

This map also includes private definitions and nested helpers. The signature column exposes every explicit input even when an internal helper has no dedicated parameter prose.

Symbol Kind Signature and inputs What it does Source
MCPClient class MCPClient(config: MCPServerConfig) Client for connecting to a single MCP server. #L23-L328
MCPClient.__init__ method MCPClient.__init__(config: MCPServerConfig) -> not annotated Initialize MCP client. #L30-L45
MCPClient.name method MCPClient.name() -> str Get server name. #L48-L50
MCPClient.state method MCPClient.state() -> MCPServerState Get current connection state. #L53-L55
MCPClient.is_connected method MCPClient.is_connected() -> bool Check if connected to server. #L58-L60
MCPClient.tools method MCPClient.tools() -> List[MCPTool] Get discovered tools. #L63-L65
MCPClient.get_status method MCPClient.get_status() -> MCPServerStatus Get server status. #L67-L76
MCPClient.connect method async MCPClient.connect() -> bool Connect to the MCP server. #L78-L122
MCPClient._connect_stdio method async MCPClient._connect_stdio() -> not annotated Connect via stdio transport. #L124-L152
MCPClient._connect_sse method async MCPClient._connect_sse() -> not annotated Connect via SSE transport. #L154-L170
MCPClient._initialize_session method async MCPClient._initialize_session() -> not annotated Initialize the MCP session. #L172-L183
MCPClient._discover_tools method async MCPClient._discover_tools() -> not annotated Discover available tools from the server. #L185-L208
MCPClient.disconnect method async MCPClient.disconnect() -> not annotated Disconnect from the MCP server. #L210-L235
MCPClient.call_tool method async MCPClient.call_tool(tool_name: str, arguments: Dict[str, Any], timeout: Optional[float] = None) -> MCPToolResult Call a tool on the MCP server. #L237-L301
MCPClient._extract_content method MCPClient._extract_content(result) -> Any Extract content from MCP tool result. #L303-L321
MCPClient.refresh_tools method async MCPClient.refresh_tools() -> not annotated Refresh the list of available tools. #L323-L328