mirror of
https://github.com/arc53/DocsGPT.git
synced 2025-11-29 08:33:20 +00:00
feat: update MCP request ID generation and enhance response handling for event streams
This commit is contained in:
@@ -146,7 +146,7 @@ class MCPTool(Tool):
|
|||||||
mcp_message = {"jsonrpc": "2.0", "method": method}
|
mcp_message = {"jsonrpc": "2.0", "method": method}
|
||||||
|
|
||||||
if not method.startswith("notifications/"):
|
if not method.startswith("notifications/"):
|
||||||
mcp_message["id"] = 1
|
mcp_message["id"] = int(time.time() * 1000000)
|
||||||
if params:
|
if params:
|
||||||
mcp_message["params"] = params
|
mcp_message["params"] = params
|
||||||
return self._execute_mcp_request(mcp_message, method)
|
return self._execute_mcp_request(mcp_message, method)
|
||||||
@@ -181,7 +181,11 @@ class MCPTool(Tool):
|
|||||||
if method.startswith("notifications/"):
|
if method.startswith("notifications/"):
|
||||||
return {}
|
return {}
|
||||||
response_text = response.text.strip()
|
response_text = response.text.strip()
|
||||||
if response_text.startswith("event:") and "data:" in response_text:
|
if (
|
||||||
|
response.headers.get("content-type", "").startswith("text/event-stream")
|
||||||
|
or response_text.startswith("event:")
|
||||||
|
and "data:" in response_text
|
||||||
|
):
|
||||||
lines = response_text.split("\n")
|
lines = response_text.split("\n")
|
||||||
data_line = None
|
data_line = None
|
||||||
for line in lines:
|
for line in lines:
|
||||||
@@ -200,12 +204,15 @@ class MCPTool(Tool):
|
|||||||
result = response.json()
|
result = response.json()
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
raise Exception(f"Invalid JSON response: {response.text}")
|
raise Exception(f"Invalid JSON response: {response.text}")
|
||||||
|
|
||||||
if "error" in result:
|
if "error" in result:
|
||||||
error_msg = result["error"]
|
error_msg = result["error"]
|
||||||
if isinstance(error_msg, dict):
|
if isinstance(error_msg, dict):
|
||||||
error_msg = error_msg.get("message", str(error_msg))
|
error_msg = error_msg.get("message", str(error_msg))
|
||||||
raise Exception(f"MCP server error: {error_msg}")
|
raise Exception(f"MCP server error: {error_msg}")
|
||||||
|
|
||||||
return result.get("result", result)
|
return result.get("result", result)
|
||||||
|
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
if not is_retry and self._should_retry_with_new_session(e):
|
if not is_retry and self._should_retry_with_new_session(e):
|
||||||
self._invalidate_and_refresh_session()
|
self._invalidate_and_refresh_session()
|
||||||
|
|||||||
Reference in New Issue
Block a user