Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:BerriAI Litellm MCP Client

From Leeroopedia
Revision as of 12:10, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/BerriAI_Litellm_MCP_Client.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Property Value
sources litellm/experimental_mcp_client/client.py
domains MCP, Model Context Protocol, Tool Calling, SSE, HTTP
last_updated 2026-02-15 16:00 GMT

Overview

The MCP Client module implements a Model Context Protocol (MCP) client that connects to MCP servers, supporting SSE, HTTP (streamable), and stdio transports with multiple authentication methods, and provides methods for listing tools, calling tools, managing prompts, and reading resources.

Description

The MCPClient class manages connections to MCP servers through three transport types: SSE (sse_client), HTTP (streamable_http_client), and stdio (stdio_client). Authentication supports five types: bearer token, basic auth, API key, raw authorization header, and OAuth2. The client follows a session-per-operation pattern via run_with_session(), which creates a transport context, initializes a ClientSession, runs the operation, and cleans up resources. Each public method (list_tools, call_tool, list_prompts, get_prompt, list_resources, list_resource_templates, read_resource) opens a new session for the operation. The client includes graceful degradation: list_tools, list_prompts, list_resources, and list_resource_templates return empty lists on failure, while call_tool returns an error result with isError=True. SSL configuration follows LiteLLM's standard SSL handling via get_ssl_configuration(). Tool call progress is reported through an optional callback.

Usage

Import this module when you need to connect to MCP servers for tool discovery and execution. It is used internally by the Responses API when MCP tools with server_url are detected, and can be used directly for standalone MCP interactions.

Code Reference

Source Location

Property Value
Repository github.com/BerriAI/litellm
File litellm/experimental_mcp_client/client.py
Lines 587
Module litellm.experimental_mcp_client.client

Signature

class MCPClient:
    def __init__(
        self,
        server_url: str = "",
        transport_type: MCPTransportType = MCPTransport.http,
        auth_type: MCPAuthType = None,
        auth_value: Optional[Union[str, Dict[str, str]]] = None,
        timeout: float = 60.0,
        stdio_config: Optional[MCPStdioConfig] = None,
        extra_headers: Optional[Dict[str, str]] = None,
        ssl_verify: Optional[VerifyTypes] = None,
    )

    async def list_tools(self) -> List[MCPTool]
    async def call_tool(
        self,
        call_tool_request_params: MCPCallToolRequestParams,
        host_progress_callback: Optional[Callable] = None,
    ) -> MCPCallToolResult
    async def list_prompts(self) -> List[Prompt]
    async def get_prompt(self, get_prompt_request_params: GetPromptRequestParams) -> GetPromptResult
    async def list_resources(self) -> list[Resource]
    async def list_resource_templates(self) -> list[ResourceTemplate]
    async def read_resource(self, url: AnyUrl) -> ReadResourceResult

Import

from litellm.experimental_mcp_client.client import MCPClient

I/O Contract

Inputs

Parameter Type Required Description
server_url str For SSE/HTTP The URL of the MCP server
transport_type MCPTransportType No Transport: MCPTransport.http (default), .sse, or .stdio
auth_type MCPAuthType No Authentication type: bearer_token, basic, api_key, authorization, oauth2
auth_value Optional[Union[str, Dict]] No Authentication credentials
timeout float No Request timeout in seconds (default: 60.0)
stdio_config Optional[MCPStdioConfig] For stdio Configuration for stdio transport (command, args, env)
ssl_verify Optional[VerifyTypes] No SSL verification setting (bool, path, or SSLContext)

Outputs

Method Return Type Description
list_tools List[MCPTool] Available tools from the MCP server (empty list on failure)
call_tool MCPCallToolResult Tool execution result (error result on failure)
list_prompts List[Prompt] Available prompts from the MCP server
get_prompt GetPromptResult A specific prompt definition
list_resources list[Resource] Available resources from the MCP server
read_resource ReadResourceResult Contents of a specific resource

Usage Examples

from litellm.experimental_mcp_client.client import MCPClient
from litellm.types.mcp import MCPTransport, MCPAuth

# Create an MCP client with HTTP transport and bearer auth
client = MCPClient(
    server_url="https://mcp-server.example.com",
    transport_type=MCPTransport.http,
    auth_type=MCPAuth.bearer_token,
    auth_value="your-api-key",
)

# List available tools
tools = await client.list_tools()
for tool in tools:
    print(f"Tool: {tool.name} - {tool.description}")
from mcp.types import CallToolRequestParams

# Call a tool
result = await client.call_tool(
    call_tool_request_params=CallToolRequestParams(
        name="search",
        arguments={"query": "LiteLLM documentation"},
    )
)
if not result.isError:
    for content in result.content:
        print(content.text)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment