refac: mcp spec/response handling

This commit is contained in:
Timothy Jaeryang Baek
2025-09-28 12:22:11 -05:00
parent 4aa41aa139
commit db9d3d386d

View File

@@ -1177,8 +1177,11 @@ async def process_chat_payload(request, form_data, user, metadata, model):
tool_function = make_tool_function(tool_spec["name"]) tool_function = make_tool_function(tool_spec["name"])
mcp_tools_dict[tool_spec["name"]] = { mcp_tools_dict[f"{server_id}_{tool_spec['name']}"] = {
"spec": tool_spec, "spec": {
**tool_spec,
"name": f"{server_id}_{tool_spec['name']}",
},
"callable": tool_function, "callable": tool_function,
"type": "mcp", "type": "mcp",
"client": mcp_client, "client": mcp_client,
@@ -1226,7 +1229,6 @@ async def process_chat_payload(request, form_data, user, metadata, model):
{"type": "function", "function": tool.get("spec", {})} {"type": "function", "function": tool.get("spec", {})}
for tool in tools_dict.values() for tool in tools_dict.values()
] ]
else: else:
# If the function calling is not native, then call the tools function calling handler # If the function calling is not native, then call the tools function calling handler
try: try:
@@ -2689,23 +2691,19 @@ async def process_chat_response(
tool_result_files = [] tool_result_files = []
if isinstance(tool_result, list): if isinstance(tool_result, list):
if tool.get("type") == "mcp": # MCP
response = []
for item in tool_result: for item in tool_result:
# check if string
if isinstance(item, str) and item.startswith("data:"):
tool_result_files.append(
{
"type": "data",
"content": item,
}
)
tool_result.remove(item)
if tool.get("type") == "mcp":
if isinstance(item, dict): if isinstance(item, dict):
if ( if item.get("type") == "text":
item.get("type") == "image" text = item.get("text", "")
or item.get("type") == "audio" if isinstance(text, str):
): try:
text = json.loads(text)
except json.JSONDecodeError:
pass
response.append(text)
elif item.get("type") in ["image", "audio"]:
file_url = get_file_url_from_base64( file_url = get_file_url_from_base64(
request, request,
f"data:{item.get('mimeType')};base64,{item.get('data', item.get('blob', ''))}", f"data:{item.get('mimeType')};base64,{item.get('data', item.get('blob', ''))}",
@@ -2730,6 +2728,21 @@ async def process_chat_response(
"url": file_url, "url": file_url,
} }
) )
tool_result = (
response[0] if len(response) == 1 else response
)
else: # OpenAPI
for item in tool_result:
# check if string
if isinstance(item, str) and item.startswith(
"data:"
):
tool_result_files.append(
{
"type": "data",
"content": item,
}
)
tool_result.remove(item) tool_result.remove(item)
if tool_result_files: if tool_result_files: