transform_mcp_tool_to_openai_tool
This commit is contained in:
parent
d3279d114e
commit
1a56bb5bdd
@ -2,18 +2,26 @@ from typing import List, Literal, Union
|
||||
|
||||
from mcp import ClientSession
|
||||
from mcp.types import Tool as MCPTool
|
||||
|
||||
from litellm.types.llms.openai import Tool
|
||||
from openai.types.chat import ChatCompletionToolParam
|
||||
from openai.types.shared_params.function_definition import FunctionDefinition
|
||||
|
||||
|
||||
def transform_mcp_tool_to_openai_tool(tool: MCPTool) -> Tool:
|
||||
def transform_mcp_tool_to_openai_tool(mcp_tool: MCPTool) -> ChatCompletionToolParam:
|
||||
"""Convert an MCP tool to an OpenAI tool."""
|
||||
raise NotImplementedError("Not implemented")
|
||||
return ChatCompletionToolParam(
|
||||
type="function",
|
||||
function=FunctionDefinition(
|
||||
name=mcp_tool.name,
|
||||
description=mcp_tool.description or "",
|
||||
parameters=mcp_tool.inputSchema,
|
||||
strict=False,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def load_mcp_tools(
|
||||
session: ClientSession, format: Literal["mcp", "openai"] = "mcp"
|
||||
) -> Union[List[MCPTool], List[Tool]]:
|
||||
) -> Union[List[MCPTool], List[ChatCompletionToolParam]]:
|
||||
"""
|
||||
Load all available MCP tools
|
||||
|
||||
@ -26,5 +34,7 @@ async def load_mcp_tools(
|
||||
"""
|
||||
tools = await session.list_tools()
|
||||
if format == "openai":
|
||||
return [transform_mcp_tool_to_openai_tool(tool) for tool in tools.tools]
|
||||
return [
|
||||
transform_mcp_tool_to_openai_tool(mcp_tool=tool) for tool in tools.tools
|
||||
]
|
||||
return tools.tools
|
||||
|
||||
@ -30,7 +30,7 @@ async def test_mcp_agent():
|
||||
await session.initialize()
|
||||
|
||||
# Get tools
|
||||
tools = await load_mcp_tools(session)
|
||||
tools = await load_mcp_tools(session=session, format="openai")
|
||||
print("MCP TOOLS: ", tools)
|
||||
|
||||
# Create and run the agent
|
||||
|
||||
Loading…
Reference in New Issue
Block a user