@@ -94,6 +94,8 @@ def __init__(
9494 )
9595 self .sse_read_timeout = normalize_timeout_to_timedelta (sse_read_timeout )
9696
97+ self .forward_incoming_headers : bool = False
98+
9799 self ._get_session_id_cb : Callable [[], str | None ] | None = None
98100
99101 def _set_auth (self , auth : httpx .Auth | Literal ["oauth" ] | str | None ):
@@ -148,10 +150,14 @@ def factory(
148150 async def connect_session (
149151 self , ** session_kwargs : Unpack [SessionKwargs ]
150152 ) -> AsyncIterator [ClientSession ]:
151- # Load headers from an active HTTP request, if available. This will only be true
152- # if the client is used in a FastMCP Proxy, in which case the MCP client headers
153- # need to be forwarded to the remote server.
154- headers = get_http_headers (include = {"authorization" }) | self .headers
153+ # When used in a proxy, forward the inbound request's authorization
154+ # header to the upstream server. This is off by default so that a
155+ # plain Client used inside a server tool handler doesn't accidentally
156+ # leak the caller's credentials to an unrelated remote server.
157+ if self .forward_incoming_headers :
158+ headers = get_http_headers (include = {"authorization" }) | self .headers
159+ else :
160+ headers = dict (self .headers )
155161
156162 # Configure timeout if provided, preserving MCP's 30s connect default
157163 timeout : httpx .Timeout | None = None
0 commit comments