Fix Gateway Error Propagation & Frontend JSON Parse Issues#2654
Merged
crivetimihai merged 1 commit intomainfrom Feb 7, 2026
Merged
Fix Gateway Error Propagation & Frontend JSON Parse Issues#2654crivetimihai merged 1 commit intomainfrom
crivetimihai merged 1 commit intomainfrom
Conversation
Member
|
Well-documented fix with clear root cause analysis. The LGTM — ready to merge. |
46c7ff4 to
d06418e
Compare
Member
Code Review Follow-upAddressed additional security and UX improvements during review: ✅ Fixed
⏭️ Not Addressed (by design)
These could be considered for future enhancements if needed. |
- Backend: Extract root cause from BaseExceptionGroup when MCP SDK uses TaskGroup, ensuring actual HTTP errors (401, 405, etc.) are shown instead of generic "Failed to initialize gateway" messages - Backend: Change HTTP status from 503 to 502 for GatewayConnectionError as 502 Bad Gateway more accurately represents upstream server failures - Backend: Include sanitized error details in GatewayConnectionError messages for better debugging while protecting sensitive URL params - Backend: Add userinfo (user:pass@host) redaction to sanitize_url_for_logging as defense-in-depth against credential leakage in error messages - Frontend: Add safeParseJsonResponse() helper to validate response status and Content-Type before parsing JSON, preventing crashes when proxies or auth redirects return HTML error pages - Frontend: Update extractApiError() to also check error.message field - Frontend: Detect HTML responses and show user-friendly message instead of raw HTML; truncate long text responses to 200 chars - Apply safeParseJsonResponse to 12 high-risk form handlers (POST/PUT): Gateway, Resource, Prompt, Server, A2A Agent, Tool (add & edit each) Closes #2562 Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com> Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
d06418e to
f33a13f
Compare
crivetimihai
approved these changes
Feb 7, 2026
kcostell06
pushed a commit
to kcostell06/mcp-context-forge
that referenced
this pull request
Feb 24, 2026
…nd (IBM#2654) - Backend: Extract root cause from BaseExceptionGroup when MCP SDK uses TaskGroup, ensuring actual HTTP errors (401, 405, etc.) are shown instead of generic "Failed to initialize gateway" messages - Backend: Change HTTP status from 503 to 502 for GatewayConnectionError as 502 Bad Gateway more accurately represents upstream server failures - Backend: Include sanitized error details in GatewayConnectionError messages for better debugging while protecting sensitive URL params - Backend: Add userinfo (user:pass@host) redaction to sanitize_url_for_logging as defense-in-depth against credential leakage in error messages - Frontend: Add safeParseJsonResponse() helper to validate response status and Content-Type before parsing JSON, preventing crashes when proxies or auth redirects return HTML error pages - Frontend: Update extractApiError() to also check error.message field - Frontend: Detect HTML responses and show user-friendly message instead of raw HTML; truncate long text responses to 200 chars - Apply safeParseJsonResponse to 12 high-risk form handlers (POST/PUT): Gateway, Resource, Prompt, Server, A2A Agent, Tool (add & edit each) Closes IBM#2562 Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com> Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🐛 Bug-fix PR
Closes #2562
📌 Summary
This PR addresses two issues:
Backend Error Propagation: Fixes an issue where specific backend errors (e.g.,
401 Unauthorizedor405 Method Not Allowed) were masked by generic "Failed to initialize gateway" messages. This prevented users from seeing the actual cause of gateway registration failures allowing users to find the root cause of failure and fix it, instead of relying on backend logs.Frontend JSON Parsing: Fixes a frontend silent crash/error with ("JSON.parse: unexpected character") that occurred when the server or a proxy returned an HTML response (e.g., error pages, auth redirects) instead of the expected JSON.
Root Cause documented here: [BUG]: JSON parse error when adding MCP server - missing response validation in admin.js #2562 (comment).
🔁 Reproduction Steps
1. Backend Error Propagation:
Failed to initialize gateway....Failed to initialize gateway... Client error '401 Unauthorized'.2. Frontend JSON Parse Error:
POST /admin/gateways) by simulating a proxy error (502/504) or session expiry redirect.SyntaxError: JSON.parse: unexpected character at line 1 column 1because the code tried to parse<html>...as JSON.🐞 Root Cause
Backend:
anyio.TaskGroupfor concurrency, which wraps raised exceptions in aBaseExceptionGroup.str(e)on the caught exception, which returned the wrapper's generic message instead of the inner exception's detail.Frontend:
admin.jsdirectly awaitedresponse.json()without validating the response status orContent-Type.response.json()would fail with a syntax error, breaking the error handling flow.💡 Fix Description
Backend (
gateway_service.py):_initialize_gatewayto inspect caught exceptions.BaseExceptionGroupand extract the root cause exception (matching patterns used elsewhere intool_service.py).GatewayConnectionError.Frontend (
admin.js):safeParseJsonResponse(response, fallbackError)that:response.okstatus first.Content-Typeheader includes result "application/json".response.json():Note: There can be more occurrences where the safeParseJsonResponse can be used, but currently have targeted to the high impact ones in this PR.
🧪 Verification
make lintmake testmake coverage📐 MCP Compliance (if relevant)
✅ Checklist
make black isort pre-commit)