mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-27 19:54:13 +00:00
litellm mcp routes
This commit is contained in:
parent
dc33cfa074
commit
f44a5a86db
2 changed files with 92 additions and 16 deletions
74
litellm/proxy/_experimental/mcp_server/mcp_routes.py
Normal file
74
litellm/proxy/_experimental/mcp_server/mcp_routes.py
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
"""
|
||||||
|
LiteLLM MCP Server Routes
|
||||||
|
"""
|
||||||
|
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from fastapi import APIRouter, HTTPException
|
||||||
|
|
||||||
|
router = APIRouter(
|
||||||
|
prefix="/mcp",
|
||||||
|
tags=["mcp"],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
from litellm.proxy._experimental.mcp_server.tool_registry import (
|
||||||
|
global_mcp_tool_registry,
|
||||||
|
)
|
||||||
|
from litellm.types.mcp_server.tool_registry import *
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/tools/list", response_model=ListToolsResponse)
|
||||||
|
async def list_tools(cursor: Optional[str] = None):
|
||||||
|
"""
|
||||||
|
List all available tools
|
||||||
|
"""
|
||||||
|
tools = []
|
||||||
|
for tool in global_mcp_tool_registry.list_tools():
|
||||||
|
tools.append(
|
||||||
|
ToolSchema(
|
||||||
|
name=tool.name,
|
||||||
|
description=tool.description,
|
||||||
|
inputSchema=tool.input_schema,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
return ListToolsResponse(tools=tools)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/tools/call", response_model=CallToolResponse)
|
||||||
|
async def call_tool(request: CallToolRequest):
|
||||||
|
"""
|
||||||
|
Call a specific tool with the provided arguments
|
||||||
|
"""
|
||||||
|
if request.method != "tools/call":
|
||||||
|
raise HTTPException(status_code=400, detail="Invalid method")
|
||||||
|
|
||||||
|
if "name" not in request.params:
|
||||||
|
raise HTTPException(status_code=400, detail="Tool name is required")
|
||||||
|
|
||||||
|
tool_name = request.params["name"]
|
||||||
|
arguments = request.params.get("arguments", {})
|
||||||
|
|
||||||
|
tool = global_mcp_tool_registry.get_tool(tool_name)
|
||||||
|
if not tool:
|
||||||
|
raise HTTPException(status_code=404, detail=f"Tool '{tool_name}' not found")
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = tool.handler(**arguments)
|
||||||
|
|
||||||
|
# Convert result to text content
|
||||||
|
if isinstance(result, str):
|
||||||
|
content = [ContentItem(type="text", text=result)]
|
||||||
|
elif isinstance(result, dict):
|
||||||
|
content = [ContentItem(type="text", text=str(result))]
|
||||||
|
else:
|
||||||
|
content = [ContentItem(type="text", text=str(result))]
|
||||||
|
|
||||||
|
return CallToolResponse(content=content)
|
||||||
|
except Exception as e:
|
||||||
|
return CallToolResponse(
|
||||||
|
content=[ContentItem(type="text", text=f"Error: {str(e)}")], isError=True
|
||||||
|
)
|
|
@ -14,24 +14,26 @@ mcp_tools:
|
||||||
- name: "get_current_time"
|
- name: "get_current_time"
|
||||||
description: "Get the current time"
|
description: "Get the current time"
|
||||||
input_schema: {
|
input_schema: {
|
||||||
type: object
|
"type": "object",
|
||||||
properties:
|
"properties": {
|
||||||
format:
|
"format": {
|
||||||
type: string
|
"type": "string",
|
||||||
description: "The format of the time to return"
|
"description": "The format of the time to return",
|
||||||
enum:
|
"enum": ["short"]
|
||||||
- "short"
|
|
||||||
}
|
}
|
||||||
handler: "get_current_time"
|
}
|
||||||
|
}
|
||||||
|
handler: "mcp_tools.get_current_time"
|
||||||
- name: "get_current_date"
|
- name: "get_current_date"
|
||||||
description: "Get the current date"
|
description: "Get the current date"
|
||||||
input_schema: {
|
input_schema: {
|
||||||
type: object
|
"type": "object",
|
||||||
properties:
|
"properties": {
|
||||||
format:
|
"format": {
|
||||||
type: string
|
"type": "string",
|
||||||
description: "The format of the date to return"
|
"description": "The format of the date to return",
|
||||||
enum:
|
"enum": ["short"]
|
||||||
- "short"
|
|
||||||
}
|
}
|
||||||
handler: "get_current_date"
|
}
|
||||||
|
}
|
||||||
|
handler: "mcp_tools.get_current_date"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue