-
Notifications
You must be signed in to change notification settings - Fork 613
[BUG]: Non-standard redirect handling in _validate_gateway_url #1280
Description
Description
The _validate_gateway_url method in mcpgateway/services/gateway_service.py (lines 357-407) has non-standard redirect handling for STREAMABLEHTTP transport that may cause confusion and maintenance issues.
Current Behavior
When transport_type="STREAMABLEHTTP", the validation logic:
- Makes a GET request to the gateway URL
- Checks for a
locationheader in the response (regardless of HTTP status code) - If a
locationheader is present, treats it as a redirect and makes a second request to that URL - Validates the redirected response has
mcp-session-idheader andapplication/jsoncontent-type
Code Reference
This block occurs after the function has already made an initial request. Notice that if there is no response, the if logic falls through and the verification eventually returns False.
if transport_type == "STREAMABLEHTTP":
if location:
async with validation_client.client.stream("GET", location, headers=headers, timeout=timeout) as response_redirect:
response_headers = dict(response_redirect.headers)
mcp_session_id = response_headers.get("mcp-session-id")
content_type = response_headers.get("content-type")
if response_redirect.status_code in (401, 403):
logger.debug(f"Authentication failed at redirect location {location}")
return False
if mcp_session_id is not None and mcp_session_id != "":
if content_type is not None and content_type != "" and "application/json" in content_type:
return TrueIssues
-
Non-standard HTTP behavior: The code treats any response with a
locationheader as a redirect, without checking for proper 3xx status codes (301, 302, 307, 308, etc.) -
Undocumented protocol: Is this "200 OK with location header" pattern documented in the MCP specification or StreamableHTTP protocol? If not, it creates confusion about expected server behavior.
-
Reinventing redirect handling: httpx library already has robust redirect-following capabilities that handle:
- Proper 3xx status code checking
- Redirect loop detection
- Maximum redirect limits
- Preservation of request methods and bodies per RFC standards
Questions
- Is the requirement for a 200 HTTP response with a "location" header part of a documented standard for StreamableHTTP?
- If this is custom behavior specific to this implementation, should it be documented?
Recommendation
If this isn't a documented standard, consider:
-
Use httpx's built-in redirect handling:
validation_client = ResilientHttpClient( client_args={ "timeout": settings.gateway_validation_timeout, "verify": not settings.skip_ssl_verify, "follow_redirects": True, # Let httpx handle redirects properly "max_redirects": 5 } )
-
Simplify the validation logic to check final response attributes after httpx has followed any proper HTTP redirects
-
Document the expected behavior if the current "location header without 3xx status" pattern is intentional
Impact
- Confusion for developers implementing MCP gateway servers
- Potential incompatibility with standard HTTP redirect behavior
- Maintenance burden of custom redirect logic
- Test complexity (as evidenced by the recent test refactoring in PR #XXX)
Environment
- Component:
mcpgateway/services/gateway_service.py - Method:
_validate_gateway_url(lines 357-407) - Transport: STREAMABLEHTTP