Skip to content

Commit f47c378

Browse files
authored
Python: Clear stale Foundry toolbox request context (#8717)
1 parent 3b27cd3 commit f47c378

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎python/packages/foundry_hosting/agent_framework_foundry_hosting/_toolbox.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
)
2323
from agent_framework._telemetry import mark_feature_used
2424
from azure.ai.agentserver.core import get_request_context
25+
from azure.ai.agentserver.core.platform_headers import FOUNDRY_CALL_ID
2526
from typing_extensions import override
2627

2728
from ._feature_usage import FeatureIndex
@@ -121,6 +122,8 @@ def __init__(self, credential: AzureCredentialTypes, scope: str) -> None:
121122

122123
def _apply_headers(self, request: httpx.Request, token: AccessToken) -> None:
123124
request.headers["Authorization"] = f"Bearer {token.token}"
125+
# A Request may be retried or reused after an earlier auth flow, so absence must clear prior caller context.
126+
request.headers.pop(FOUNDRY_CALL_ID, None)
124127
for key, value in get_request_context().platform_headers().items():
125128
request.headers[key] = value
126129
request.headers["Foundry-Features"] = self._features_header

‎python/packages/foundry_hosting/tests/test_toolbox.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,38 @@ async def test_auth_flow_omits_call_id_when_absent() -> None:
253253
assert "x-agent-foundry-call-id" not in prepared.headers
254254

255255

256+
async def test_async_auth_flow_removes_call_id_from_reused_request_when_context_absent() -> None:
257+
auth = _ToolboxAuth(_FakeCredential(), "scope") # type: ignore
258+
request = httpx.Request("POST", "https://h/toolboxes/tb/mcp")
259+
260+
token = set_request_context(FoundryAgentRequestContext(call_id="canary-call-id"))
261+
try:
262+
prepared = await anext(auth.async_auth_flow(request))
263+
assert prepared.headers["x-agent-foundry-call-id"] == "canary-call-id"
264+
finally:
265+
reset_request_context(token)
266+
267+
prepared = await anext(auth.async_auth_flow(request))
268+
269+
assert "x-agent-foundry-call-id" not in prepared.headers
270+
271+
272+
def test_sync_auth_flow_removes_call_id_from_reused_request_when_context_absent() -> None:
273+
auth = _ToolboxAuth(_FakeCredential(), "scope") # type: ignore
274+
request = httpx.Request("POST", "https://h/toolboxes/tb/mcp")
275+
276+
token = set_request_context(FoundryAgentRequestContext(call_id="canary-call-id"))
277+
try:
278+
prepared = next(auth.sync_auth_flow(request))
279+
assert prepared.headers["x-agent-foundry-call-id"] == "canary-call-id"
280+
finally:
281+
reset_request_context(token)
282+
283+
prepared = next(auth.sync_auth_flow(request))
284+
285+
assert "x-agent-foundry-call-id" not in prepared.headers
286+
287+
256288
async def test_close_closes_owned_http_client() -> None:
257289
toolbox = FoundryToolbox(
258290
_FakeCredential(), # type: ignore

0 commit comments

Comments
 (0)