跳到主要内容

/mcp[测试版]- 模型上下文协议

在 LiteLLM 代理服务器上暴露 MCP 工具

这允许您定义可由任何 MCP 兼容客户端调用的工具。使用 LiteLLM 定义您的 mcp_servers,您的所有客户端都可以列出和调用可用的工具。

LiteLLM MCP 架构:将 MCP 工具与所有 LiteLLM 支持的模型一起使用

工作原理

LiteLLM 暴露以下 MCP 端点

  • /mcp/tools/list - 列出所有可用工具
  • /mcp/tools/call - 使用提供的参数调用特定工具

当 MCP 客户端连接到 LiteLLM 时,它们可以遵循以下工作流程

  1. 连接到 LiteLLM MCP 服务器
  2. 列出 LiteLLM 上的所有可用工具
  3. 客户端发出包含工具调用的 LLM API 请求
  4. LLM API 返回要调用的工具及其参数
  5. MCP 客户端向 LiteLLM 发起 MCP 工具调用
  6. LiteLLM 向适当的 MCP 服务器发起工具调用
  7. LiteLLM 将工具调用结果返回给 MCP 客户端

用法

1. 在您的 config.yaml 文件中的 mcp_servers 下定义您的工具。

LiteLLM 允许您在 config.yaml 文件中的 mcp_servers 部分定义您的工具。此处列出的所有工具都将可供 MCP 客户端使用(当它们连接到 LiteLLM 并调用 list_tools 时)。

config.yaml
model_list:
- model_name: gpt-4o
litellm_params:
model: openai/gpt-4o
api_key: sk-xxxxxxx

mcp_servers:
zapier_mcp:
url: "https://actions.zapier.com/mcp/sk-akxxxxx/sse"
fetch:
url: "http://localhost:8000/sse"

2. 启动 LiteLLM 网关

Docker 运行
docker run -d \
-p 4000:4000 \
-e OPENAI_API_KEY=$OPENAI_API_KEY \
--name my-app \
-v $(pwd)/my_config.yaml:/app/config.yaml \
my-app:latest \
--config /app/config.yaml \
--port 4000 \
--detailed_debug \

3. 发出 LLM API 请求

在此示例中,我们将执行以下操作

  1. 使用 MCP 客户端列出 LiteLLM 代理上的 MCP 工具
  2. 使用 transform_mcp_tool_to_openai_tool 将 MCP 工具转换为 OpenAI 工具
  3. 将 MCP 工具提供给 gpt-4o
  4. 处理来自 gpt-4o 的工具调用
  5. 将 OpenAI 工具调用转换为 MCP 工具调用
  6. 在 MCP 服务器上执行工具调用
MCP 客户端列出工具
import asyncio
from openai import AsyncOpenAI
from openai.types.chat import ChatCompletionUserMessageParam
from mcp import ClientSession
from mcp.client.sse import sse_client
from litellm.experimental_mcp_client.tools import (
transform_mcp_tool_to_openai_tool,
transform_openai_tool_call_request_to_mcp_tool_call_request,
)


async def main():
# Initialize clients

# point OpenAI client to LiteLLM Proxy
client = AsyncOpenAI(api_key="sk-1234", base_url="http://localhost:4000")

# Point MCP client to LiteLLM Proxy
async with sse_client("http://localhost:4000/mcp/") as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()

# 1. List MCP tools on LiteLLM Proxy
mcp_tools = await session.list_tools()
print("List of MCP tools for MCP server:", mcp_tools.tools)

# Create message
messages = [
ChatCompletionUserMessageParam(
content="Send an email about LiteLLM supporting MCP", role="user"
)
]

# 2. Use `transform_mcp_tool_to_openai_tool` to convert MCP tools to OpenAI tools
# Since OpenAI only supports tools in the OpenAI format, we need to convert the MCP tools to the OpenAI format.
openai_tools = [
transform_mcp_tool_to_openai_tool(tool) for tool in mcp_tools.tools
]

# 3. Provide the MCP tools to `gpt-4o`
response = await client.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=openai_tools,
tool_choice="auto",
)

# 4. Handle tool call from `gpt-4o`
if response.choices[0].message.tool_calls:
tool_call = response.choices[0].message.tool_calls[0]
if tool_call:

# 5. Convert OpenAI tool call to MCP tool call
# Since MCP servers expect tools in the MCP format, we need to convert the OpenAI tool call to the MCP format.
# This is done using litellm.experimental_mcp_client.tools.transform_openai_tool_call_request_to_mcp_tool_call_request
mcp_call = (
transform_openai_tool_call_request_to_mcp_tool_call_request(
openai_tool=tool_call.model_dump()
)
)

# 6. Execute tool call on MCP server
result = await session.call_tool(
name=mcp_call.name, arguments=mcp_call.arguments
)

print("Result:", result)


# Run it
asyncio.run(main())

LiteLLM Python SDK MCP 桥接

LiteLLM Python SDK 作为 MCP 桥接,可与所有 LiteLLM 支持的模型一起使用 MCP 工具。LiteLLM 为使用 MCP 提供以下功能

  • 列出可用的 MCP 工具:OpenAI 客户端可以查看所有可用的 MCP 工具
    • 使用 litellm.experimental_mcp_client.load_mcp_tools 列出所有可用的 MCP 工具
  • 调用 MCP 工具:OpenAI 客户端可以调用 MCP 工具
    • 使用 litellm.experimental_mcp_client.call_openai_tool 在 MCP 服务器上调用 OpenAI 工具

1. 列出可用的 MCP 工具

在此示例中,我们将使用 litellm.experimental_mcp_client.load_mcp_tools 列出任何 MCP 服务器上的所有可用 MCP 工具。此方法有两种用法

  • format="mcp" - (默认)返回 MCP 工具
    • 返回:mcp.types.Tool
  • format="openai" - 返回转换为 OpenAI API 兼容工具的 MCP 工具。允许与 OpenAI 端点一起使用。
    • 返回:openai.types.chat.ChatCompletionToolParam
MCP 客户端列出工具
# Create server parameters for stdio connection
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
import os
import litellm
from litellm import experimental_mcp_client


server_params = StdioServerParameters(
command="python3",
# Make sure to update to the full absolute path to your mcp_server.py file
args=["./mcp_server.py"],
)

async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
# Initialize the connection
await session.initialize()

# Get tools
tools = await experimental_mcp_client.load_mcp_tools(session=session, format="openai")
print("MCP TOOLS: ", tools)

messages = [{"role": "user", "content": "what's (3 + 5)"}]
llm_response = await litellm.acompletion(
model="gpt-4o",
api_key=os.getenv("OPENAI_API_KEY"),
messages=messages,
tools=tools,
)
print("LLM RESPONSE: ", json.dumps(llm_response, indent=4, default=str))

2. 列出并调用 MCP 工具

在此示例中,我们将使用

  • litellm.experimental_mcp_client.load_mcp_tools 列出任何 MCP 服务器上的所有可用 MCP 工具
  • 使用 litellm.experimental_mcp_client.call_openai_tool 在 MCP 服务器上调用 OpenAI 工具

第一个 LLM 响应返回一个 OpenAI 工具列表。我们从 LLM 响应中获取第一个工具调用,并将其传递给 litellm.experimental_mcp_client.call_openai_tool,以便在 MCP 服务器上调用该工具。

litellm.experimental_mcp_client.call_openai_tool 的工作原理

  • 接受来自 LLM 响应的 OpenAI 工具调用
  • 将 OpenAI 工具调用转换为 MCP 工具
  • 在 MCP 服务器上调用 MCP 工具
  • 返回 MCP 工具调用的结果
MCP 客户端列出并调用工具
# Create server parameters for stdio connection
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
import os
import litellm
from litellm import experimental_mcp_client


server_params = StdioServerParameters(
command="python3",
# Make sure to update to the full absolute path to your mcp_server.py file
args=["./mcp_server.py"],
)

async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
# Initialize the connection
await session.initialize()

# Get tools
tools = await experimental_mcp_client.load_mcp_tools(session=session, format="openai")
print("MCP TOOLS: ", tools)

messages = [{"role": "user", "content": "what's (3 + 5)"}]
llm_response = await litellm.acompletion(
model="gpt-4o",
api_key=os.getenv("OPENAI_API_KEY"),
messages=messages,
tools=tools,
)
print("LLM RESPONSE: ", json.dumps(llm_response, indent=4, default=str))

openai_tool = llm_response["choices"][0]["message"]["tool_calls"][0]
# Call the tool using MCP client
call_result = await experimental_mcp_client.call_openai_tool(
session=session,
openai_tool=openai_tool,
)
print("MCP TOOL CALL RESULT: ", call_result)

# send the tool result to the LLM
messages.append(llm_response["choices"][0]["message"])
messages.append(
{
"role": "tool",
"content": str(call_result.content[0].text),
"tool_call_id": openai_tool["id"],
}
)
print("final messages with tool result: ", messages)
llm_response = await litellm.acompletion(
model="gpt-4o",
api_key=os.getenv("OPENAI_API_KEY"),
messages=messages,
tools=tools,
)
print(
"FINAL LLM RESPONSE: ", json.dumps(llm_response, indent=4, default=str)
)

权限管理

目前,所有虚拟密钥都可以访问 MCP 端点。我们正在开发一项功能,以允许按密钥/团队/用户/组织限制 MCP 访问。

加入讨论这里