Skip to content

Commit 4c91d83

Browse files
authored
fix(gateway): Correct validation for STREAMABLEHTTP transport (#662)
When registering a gateway with transport: "STREAMABLEHTTP", the registration fails with a 502 Bad Gateway error. The underlying tool server logs show it rejects the connection attempt with a 406 Not Acceptable status. This occurs because the gateway's validation logic in `_validate_gateway_url` sends a GET request to the tool server's endpoint to check for liveness. However, the `streamable-http` protocol specification requires an initial POST request to establish a session. The tool server correctly rejects the unexpected GET request. This commit resolves the issue by bypassing the incorrect GET-based validation for the STREAMABLEHTTP transport. Instead, it proceeds directly to establishing a connection using the `streamablehttp_client`, which correctly performs the POST handshake. The existing `try...except` block in `_initialize_gateway` is sufficient to handle any genuine connection failures. Signed-off-by: Jimmy Liao <[email protected]>
1 parent 13c6a93 commit 4c91d83

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

mcpgateway/services/gateway_service.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,26 +1262,23 @@ async def connect_to_streamablehttp_server(server_url: str, authentication: Opti
12621262
authentication = {}
12631263
# Store the context managers so they stay alive
12641264
decoded_auth = decode_auth(authentication)
1265-
if await self._validate_gateway_url(url=server_url, headers=decoded_auth, transport_type="STREAMABLEHTTP"):
1266-
# Use async with for both streamablehttp_client and ClientSession
1267-
async with streamablehttp_client(url=server_url, headers=decoded_auth) as (read_stream, write_stream, _get_session_id):
1268-
async with ClientSession(read_stream, write_stream) as session:
1269-
# Initialize the session
1270-
response = await session.initialize()
1271-
# if get_session_id:
1272-
# session_id = get_session_id()
1273-
# if session_id:
1274-
# print(f"Session ID: {session_id}")
1275-
capabilities = response.capabilities.model_dump(by_alias=True, exclude_none=True)
1276-
response = await session.list_tools()
1277-
tools = response.tools
1278-
tools = [tool.model_dump(by_alias=True, exclude_none=True) for tool in tools]
1279-
tools = [ToolCreate.model_validate(tool) for tool in tools]
1280-
for tool in tools:
1281-
tool.request_type = "STREAMABLEHTTP"
1282-
1283-
return capabilities, tools
1284-
raise GatewayConnectionError(f"Failed to initialize gateway at {url}")
1265+
# The _validate_gateway_url logic is flawed for streamablehttp, so we bypass it
1266+
# and go straight to the client connection. The outer try/except in
1267+
# _initialize_gateway will handle any connection errors.
1268+
async with streamablehttp_client(url=server_url, headers=decoded_auth) as (read_stream, write_stream, _get_session_id):
1269+
async with ClientSession(read_stream, write_stream) as session:
1270+
# Initialize the session
1271+
response = await session.initialize()
1272+
capabilities = response.capabilities.model_dump(by_alias=True, exclude_none=True)
1273+
1274+
response = await session.list_tools()
1275+
tools = response.tools
1276+
tools = [tool.model_dump(by_alias=True, exclude_none=True) for tool in tools]
1277+
1278+
tools = [ToolCreate.model_validate(tool) for tool in tools]
1279+
for tool in tools:
1280+
tool.request_type = "STREAMABLEHTTP"
1281+
return capabilities, tools
12851282

12861283
capabilities = {}
12871284
tools = []

0 commit comments

Comments
 (0)