Fix: handle MCP notifications/initialized without 400

This commit is contained in:
2026-06-14 22:03:07 +00:00
parent 208e4195b0
commit 741714a4bc
+18 -3
View File
@@ -285,10 +285,10 @@ class MCPSummaryHandler(BaseHTTPRequestHandler):
}) })
return return
# Some clients probe for OpenAPI spec; safe to return 404.
self.send_error(404, "Not Found") self.send_error(404, "Not Found")
except Exception as e: except Exception as e:
logger.error(f"GET error: {e}", exc_info=True) logger.error(f"GET error: {e}", exc_info=True)
# Ensure we still send something
try: try:
self.send_error(500, "Internal Server Error") self.send_error(500, "Internal Server Error")
except Exception: except Exception:
@@ -322,6 +322,22 @@ class MCPSummaryHandler(BaseHTTPRequestHandler):
logger.info(f"MCP request: method={method}, id={req_id}") logger.info(f"MCP request: method={method}, id={req_id}")
# MCP: notifications (e.g. notifications/initialized)
# These are one-way; respond with 200 and no result, do not error.
if isinstance(method, str) and method.startswith("notifications/"):
# If there's an id, respond with empty result; otherwise just 200.
if req_id is not None:
self._send_json(200, {
"jsonrpc": "2.0",
"id": req_id,
"result": {}
})
else:
self.send_response(200)
self.send_header("Content-Length", "0")
self.end_headers()
return
# MCP: initialize # MCP: initialize
if method == "initialize": if method == "initialize":
self._send_json(200, { self._send_json(200, {
@@ -376,12 +392,11 @@ class MCPSummaryHandler(BaseHTTPRequestHandler):
}) })
return return
# Unknown method # Unknown method (non-notification) -> 400
self._send_json(400, {"error": "Unknown method: " + str(method)}) self._send_json(400, {"error": "Unknown method: " + str(method)})
except Exception as e: except Exception as e:
logger.error(f"POST error: {e}", exc_info=True) logger.error(f"POST error: {e}", exc_info=True)
# Fallback response to avoid silent drop
try: try:
self.send_error(500, "Internal Server Error") self.send_error(500, "Internal Server Error")
except Exception: except Exception: