Implementation:BerriAI Litellm MCP Tools
| Attribute | Value |
|---|---|
| Sources | litellm/experimental_mcp_client/tools.py
|
| Domains | MCP, Tool Calling, OpenAI Compatibility, Experimental |
| Last Updated | 2026-02-15 16:00 GMT |
Overview
The MCP tools module provides functions for converting between MCP (Model Context Protocol) tool definitions and OpenAI-compatible tool formats, as well as loading and calling MCP tools through a client session.
Description
This module bridges the MCP tool ecosystem with OpenAI's function calling interface. It contains several key functions:
Tool Listing and Conversion:
transform_mcp_tool_to_openai_tool- Converts an MCPToolto an OpenAIChatCompletionToolParamwith aFunctionDefinition, normalizing the input schema to ensure it hastype: "object",properties, andadditionalProperties: falseas OpenAI requires.transform_mcp_tool_to_openai_responses_api_tool- Converts an MCPToolto an OpenAI Responses APIFunctionToolParamformat.load_mcp_tools- Async function that loads all available tools from an MCP session, optionally converting them to OpenAI format.
Tool Calling:
call_mcp_tool- Calls an MCP tool directly usingMCPCallToolRequestParams.call_openai_tool- Calls an MCP tool using an OpenAIChatCompletionMessageToolCall, automatically converting the request format.transform_openai_tool_call_request_to_mcp_tool_call_request- Converts an OpenAI tool call to MCP format, safely parsing JSON arguments.
The internal _normalize_mcp_input_schema helper ensures that MCP tool schemas conform to OpenAI's requirements, and _get_function_arguments safely parses function arguments from string or dict format.
Usage
Import these functions when integrating MCP tools with LiteLLM's OpenAI-compatible interface. Use load_mcp_tools to discover available tools, pass them to completion calls, and then use call_openai_tool to execute the tools that the LLM selects.
Code Reference
Source Location
litellm/experimental_mcp_client/tools.py
Signature
def transform_mcp_tool_to_openai_tool(mcp_tool: MCPTool) -> ChatCompletionToolParam
def transform_mcp_tool_to_openai_responses_api_tool(mcp_tool: MCPTool) -> FunctionToolParam
async def load_mcp_tools(session: ClientSession, format: Literal["mcp", "openai"] = "mcp") -> Union[List[MCPTool], List[ChatCompletionToolParam]]
async def call_mcp_tool(session: ClientSession, call_tool_request_params: MCPCallToolRequestParams) -> MCPCallToolResult
def transform_openai_tool_call_request_to_mcp_tool_call_request(openai_tool: Union[ChatCompletionMessageToolCall, Dict]) -> MCPCallToolRequestParams
async def call_openai_tool(session: ClientSession, openai_tool: ChatCompletionMessageToolCall) -> MCPCallToolResult
Import
from litellm.experimental_mcp_client.tools import (
load_mcp_tools,
call_openai_tool,
call_mcp_tool,
transform_mcp_tool_to_openai_tool,
transform_openai_tool_call_request_to_mcp_tool_call_request,
)
I/O Contract
Inputs
| Parameter | Type | Description |
|---|---|---|
session |
mcp.ClientSession |
An active MCP client session. |
format |
Literal["mcp", "openai"] |
Output format for loaded tools. Defaults to "mcp".
|
mcp_tool |
MCPTool |
An MCP tool definition to convert. |
openai_tool |
Union[ChatCompletionMessageToolCall, Dict] |
An OpenAI tool call to execute or convert. |
call_tool_request_params |
MCPCallToolRequestParams |
MCP tool call parameters (name and arguments). |
Outputs
| Function | Return Type | Description |
|---|---|---|
load_mcp_tools |
Union[List[MCPTool], List[ChatCompletionToolParam]] |
List of tools in MCP or OpenAI format. |
transform_mcp_tool_to_openai_tool |
ChatCompletionToolParam |
An OpenAI-compatible tool definition. |
call_mcp_tool / call_openai_tool |
MCPCallToolResult |
The result of executing the tool. |
transform_openai_tool_call_request_to_mcp_tool_call_request |
MCPCallToolRequestParams |
MCP-format call parameters. |
Usage Examples
import litellm
from litellm.experimental_mcp_client.tools import load_mcp_tools, call_openai_tool
from mcp import ClientSession
async def use_mcp_tools(session: ClientSession):
# Load MCP tools in OpenAI format
tools = await load_mcp_tools(session=session, format="openai")
# Use tools in a completion call
response = await litellm.acompletion(
model="gpt-4",
messages=[{"role": "user", "content": "What files are in the current directory?"}],
tools=tools,
)
# Execute the tool call if the model requests one
if response.choices[0].message.tool_calls:
tool_call = response.choices[0].message.tool_calls[0]
result = await call_openai_tool(session=session, openai_tool=tool_call)
print(result)
Related Pages
- BerriAI_Litellm_Custom_LLM_Handler - Custom LLM handler that may use tool calling