Fix MCP tool execution to use proper JSON-RPC 2.0 format
All checks were successful
OpenWebUI Discord Bot / Build-and-Push (push) Successful in 53s

The /mcp/call_tool endpoint expects JSON-RPC 2.0 format requests.
Updated to send proper RPC structure and parse RPC responses.

Request format:
{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "name": "tool_name",
    "arguments": {...}
  },
  "id": 1
}

Response parsing updated to extract result from JSON-RPC envelope:
result.result.content[0].text

This fixes the 400 validation error:
"Field required: JSONRPCRequest.method, jsonrpc, id"

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-12 11:44:48 -08:00
parent 4f7b48c03b
commit dc95e5ac55

View File

@@ -99,8 +99,13 @@ async def execute_mcp_tool(tool_name: str, arguments: dict) -> str:
f"{base_url}/mcp/call_tool",
headers=headers,
json={
"name": tool_name,
"arguments": arguments
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": tool_name,
"arguments": arguments
},
"id": 1
}
)
@@ -110,12 +115,26 @@ async def execute_mcp_tool(tool_name: str, arguments: dict) -> str:
result = response.json()
debug_log(f"MCP tool result: {str(result)[:200]}...")
# MCP returns content in various formats, extract the text
# Handle JSON-RPC response format
if isinstance(result, dict):
# Check for JSON-RPC result
if "result" in result:
rpc_result = result["result"]
# MCP tool results have a "content" array
if isinstance(rpc_result, dict) and "content" in rpc_result:
content = rpc_result["content"]
if isinstance(content, list) and len(content) > 0:
# Handle text content blocks
first_content = content[0]
if isinstance(first_content, dict) and "text" in first_content:
return first_content["text"]
return json.dumps(content)
return json.dumps(content) if content else "Tool executed successfully"
return json.dumps(rpc_result)
# Fallback for non-RPC format
if "content" in result:
content = result["content"]
if isinstance(content, list) and len(content) > 0:
# Handle text content blocks
first_content = content[0]
if isinstance(first_content, dict) and "text" in first_content:
return first_content["text"]