5656import time
5757import traceback
5858from typing import Any , Dict , Optional
59- from urllib .parse import urlparse
6059import uuid
6160
6261# Third-Party
@@ -1939,24 +1938,10 @@ async def generate_response(self, message: Dict[str, Any], transport: SSETranspo
19391938 session_id = transport .session_id
19401939
19411940 headers = {"Authorization" : f"Bearer { token } " , "Content-Type" : "application/json" , "x-mcp-session-id" : session_id }
1942- # Extract root URL from base_url (remove /servers/{id} path)
1943- parsed_url = urlparse (base_url )
1944- # Preserve the path up to the root path (before /servers/{id})
1945- path_parts = parsed_url .path .split ("/" )
1946- if "/servers/" in parsed_url .path :
1947- # Find the index of 'servers' and take everything before it
1948- try :
1949- servers_index = path_parts .index ("servers" )
1950- root_path = "/" + "/" .join (path_parts [1 :servers_index ]).strip ("/" )
1951- if root_path == "/" :
1952- root_path = ""
1953- except ValueError :
1954- root_path = ""
1955- else :
1956- root_path = parsed_url .path .rstrip ("/" )
1957-
1958- root_url = f"{ parsed_url .scheme } ://{ parsed_url .netloc } { root_path } "
1959- rpc_url = root_url + "/rpc"
1941+ # Internal RPC call must hit THIS worker (not go through load balancer)
1942+ # Use 127.0.0.1 if host is 0.0.0.0, otherwise use settings.host
1943+ internal_host = "127.0.0.1" if settings .host == "0.0.0.0" else settings .host
1944+ rpc_url = f"http://{ internal_host } :{ settings .port } /rpc"
19601945
19611946 # Pre-register session mapping for session affinity before making /rpc call
19621947 # This ensures the mapping is available when pool.acquire() is called
@@ -1967,7 +1952,7 @@ async def generate_response(self, message: Dict[str, Any], transport: SSETranspo
19671952 import os # pylint: disable=import-outside-toplevel
19681953 worker_id = str (os .getpid ())
19691954 session_short = session_id [:8 ] if len (session_id ) >= 8 else session_id
1970- logger .info (f"[AFFINITY] Worker { worker_id } | Session { session_short } ... | Method: { method } | SSE session making internal /rpc call" )
1955+ logger .info (f"[AFFINITY] Worker { worker_id } | Session { session_short } ... | Method: { method } | SSE session making internal /rpc call to { rpc_url } " )
19711956 logger .info (f"SSE RPC: Making call to { rpc_url } with method={ method } , params={ params } " )
19721957
19731958 async with ResilientHttpClient (client_args = {"timeout" : settings .federation_timeout , "verify" : not settings .skip_ssl_verify }) as client :
0 commit comments