Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -89,10 +89,13 @@ TOOLS = [
|
|
| 89 |
}
|
| 90 |
]
|
| 91 |
|
| 92 |
-
def call_mcp_tool(tool_name, parameters):
|
| 93 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
try:
|
| 95 |
-
# MCP uses JSON-RPC 2.0 protocol
|
| 96 |
payload = {
|
| 97 |
"jsonrpc": "2.0",
|
| 98 |
"id": 1,
|
|
@@ -103,19 +106,54 @@ def call_mcp_tool(tool_name, parameters):
|
|
| 103 |
}
|
| 104 |
}
|
| 105 |
|
| 106 |
-
response = requests.post(
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
except Exception as e:
|
| 118 |
-
return {"error": str(e)}
|
| 119 |
|
| 120 |
def process_tool_calls(tool_calls):
|
| 121 |
"""Process tool calls and return results"""
|
|
@@ -132,11 +170,21 @@ def process_tool_calls(tool_calls):
|
|
| 132 |
pass
|
| 133 |
|
| 134 |
result = call_mcp_tool(func_name, func_args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
results.append({
|
| 136 |
"tool_call_id": tool_call.get("id", "call_0"),
|
| 137 |
"role": "tool",
|
| 138 |
"name": func_name,
|
| 139 |
-
"content":
|
| 140 |
})
|
| 141 |
return results
|
| 142 |
|
|
|
|
| 89 |
}
|
| 90 |
]
|
| 91 |
|
| 92 |
+
def call_mcp_tool(tool_name, parameters, timeout=60):
|
| 93 |
+
"""
|
| 94 |
+
Call MCP tool via Streamable HTTP (SSE).
|
| 95 |
+
Extracts JSON responses from 'data:' events.
|
| 96 |
+
Returns parsed JSON dict.
|
| 97 |
+
"""
|
| 98 |
try:
|
|
|
|
| 99 |
payload = {
|
| 100 |
"jsonrpc": "2.0",
|
| 101 |
"id": 1,
|
|
|
|
| 106 |
}
|
| 107 |
}
|
| 108 |
|
| 109 |
+
response = requests.post(
|
| 110 |
+
MCP_URL,
|
| 111 |
+
json=payload,
|
| 112 |
+
headers={
|
| 113 |
+
"Content-Type": "application/json",
|
| 114 |
+
"Accept": "application/json, text/event-stream"
|
| 115 |
+
},
|
| 116 |
+
timeout=timeout,
|
| 117 |
+
stream=False
|
| 118 |
+
)
|
| 119 |
|
| 120 |
+
if response.status_code != 200:
|
| 121 |
+
return {"error": f"HTTP {response.status_code}: {response.text}"}
|
| 122 |
+
|
| 123 |
+
# Parse SSE chunks
|
| 124 |
+
data_events = []
|
| 125 |
+
for line in response.text.splitlines():
|
| 126 |
+
line = line.strip()
|
| 127 |
+
if line.startswith("data:"):
|
| 128 |
+
json_str = line.replace("data:", "").strip()
|
| 129 |
+
try:
|
| 130 |
+
data_events.append(json.loads(json_str))
|
| 131 |
+
except json.JSONDecodeError:
|
| 132 |
+
pass # skip invalid chunks
|
| 133 |
+
|
| 134 |
+
if not data_events:
|
| 135 |
+
return {"error": "No valid JSON data events found in SSE response"}
|
| 136 |
+
|
| 137 |
+
# Return the final event (most tools return a single event)
|
| 138 |
+
final_result = data_events[-1]
|
| 139 |
+
|
| 140 |
+
# Extract content from result
|
| 141 |
+
if "result" in final_result:
|
| 142 |
+
result = final_result["result"]
|
| 143 |
+
# Extract text content if available
|
| 144 |
+
if isinstance(result, dict) and "content" in result:
|
| 145 |
+
content = result["content"]
|
| 146 |
+
if isinstance(content, list) and len(content) > 0:
|
| 147 |
+
if content[0].get("type") == "text":
|
| 148 |
+
return {"output": content[0].get("text", "")}
|
| 149 |
return result
|
| 150 |
+
|
| 151 |
+
return final_result
|
| 152 |
+
|
| 153 |
+
except requests.exceptions.Timeout:
|
| 154 |
+
return {"error": "Request timeout"}
|
| 155 |
except Exception as e:
|
| 156 |
+
return {"error": f"MCP call failed: {str(e)}"}
|
| 157 |
|
| 158 |
def process_tool_calls(tool_calls):
|
| 159 |
"""Process tool calls and return results"""
|
|
|
|
| 170 |
pass
|
| 171 |
|
| 172 |
result = call_mcp_tool(func_name, func_args)
|
| 173 |
+
|
| 174 |
+
# Format result for display
|
| 175 |
+
result_text = ""
|
| 176 |
+
if "error" in result:
|
| 177 |
+
result_text = f"❌ Error: {result['error']}"
|
| 178 |
+
elif "output" in result:
|
| 179 |
+
result_text = result["output"]
|
| 180 |
+
else:
|
| 181 |
+
result_text = json.dumps(result, ensure_ascii=False, indent=2)
|
| 182 |
+
|
| 183 |
results.append({
|
| 184 |
"tool_call_id": tool_call.get("id", "call_0"),
|
| 185 |
"role": "tool",
|
| 186 |
"name": func_name,
|
| 187 |
+
"content": result_text
|
| 188 |
})
|
| 189 |
return results
|
| 190 |
|