Files
agent-framework/python/packages/foundry_hosting
T
Evan Mattson 317ef4491e Python: Fix hosted MCP replay producing orphan function_call_output (#5581)
* Python: Fix hosted MCP replay producing orphan function_call_output

Resolves part of #5546. After a turn ran a hosted MCP / Foundry-toolbox-MCP
tool, the next turn's replayed input array carried a function_call_output
with an mcp_* call_id and no matching function_call, and the Responses API
returned a 400.

Two layers covered here:

* Chat-client serialize layer (packages/openai): adds mcp_server_tool_call
  and mcp_server_tool_result cases to _prepare_message_for_openai and
  _prepare_content_for_openai. Pairs are coalesced via a post-pass into a
  single mcp_call input item carrying both arguments and output. Orphan
  results are dropped (debug-logged) rather than serialized as orphan
  function_call_output, which is what the Responses API rejected.

* Host read layer (packages/foundry_hosting): _item_to_message and
  _output_item_to_message now route custom_tool_call_output whose
  call_id.startswith("mcp_") to Content.from_mcp_server_tool_result.
  Non-mcp_ call_ids continue to produce Content.from_function_result.
  Symmetric with the host write-side choice for hosted-MCP results.

Two further fixes (agentserver SDK additions, host write-side single-item
emission) remain tracked on the issue and depend on an SDK release.

* Python: Fix pyright unknown-type in _stringify_mcp_output

cast(Sequence[Any], output) after the isinstance check so pyright stops
flagging the loop variable as unknown. Also normalizes a couple of
em-dashes in docstrings I introduced in the prior commit.

* Python: Harden _stringify_mcp_output for dict-shaped MCP outputs

Address Copilot review on PR #5581. Today the helper falls back to
str() for any non-string, non-text-attribute entry, which produces
Python repr (single-quoted dicts) for the canonical MCP raw-JSON
text-content shape `{"type": "text", "text": "..."}` and any other
dict-shaped output.

Three small changes:

* List-entry path: prefer plain string entries, then `.text` attribute
  (Content objects), then `entry["text"]` for Mapping entries in the
  canonical MCP shape, then JSON-encode anything else.
* Final fallback: `json.dumps(output, default=str)` so Mappings and
  scalars produce valid JSON rather than Python repr.
* Two new unit tests covering the dict-with-text shape and the
  non-text-dict JSON fallback.

* Python: Suppress mypy redundant-cast on _stringify_mcp_output narrowing

The cast is needed by pyright (reportUnknownVariableType) but mypy
considers it redundant after the preceding isinstance narrowing.
Pyright's behavior is correct for the strict-mode reporting we run,
so keep the cast and silence mypy on the line.
317ef4491e ยท 2026-04-30 21:01:52 +00:00
History
..

Foundry Hosting

This package provides the integration of Agent Framework agents and workflows with the Foundry Agent Server, which can be hosted on Foundry infrastructure.