Repository navigation
Description
What happened?
When an MCP server's tool-use sampling loop answers a ToolUseContent with a follow-up user SamplingMessage whose list content mixes the ToolResultContent with follow-up text — a shape SamplingMessage.content explicitly allows — OpenAISamplingHandler silently drops the tool result. No role="tool" message is sent to the OpenAI API, so the model never sees the tool output; and when the preceding assistant tool_calls message is in the same request, the API rejects it outright (an assistant message with tool_calls must be followed by tool messages responding to each tool_call_id).
Output of the MRE below on commit 258d5ef:
{"role": "assistant", "content": null, "tool_calls": [{"id": "call_1", "type": "function", "function": {"name": "get_weather", "arguments": "{\"city\": \"Paris\"}"}}]}
{"role": "user", "content": [{"type": "text", "text": "Now write the forecast."}]}
role=tool messages: 0
Expected: the tool result is forwarded as a role="tool" message before the user text — the way AnthropicSamplingHandler handles the identical input (it emits tool_result + text), and the way this handler already does when the tool result is the only block in the list. (One observation from debugging: the dropped branch is elif content_parts: at fastmcp_slim/fastmcp/client/sampling/handlers/openai.py:273, which appends the user message without emitting the tool_messages collected from that same list.)
Example Code
import json
from fastmcp.client.sampling.handlers.openai import OpenAISamplingHandler
from mcp_types import (
SamplingMessage,
TextContent,
ToolResultContent,
ToolUseContent,
)
messages = [
SamplingMessage(
role="assistant",
content=[
ToolUseContent(
type="tool_use",
id="call_1",
name="get_weather",
input={"city": "Paris"},
)
],
),
SamplingMessage(
role="user",
content=[
ToolResultContent(
type="tool_result",
tool_use_id="call_1",
content=[TextContent(type="text", text="18C sunny")],
),
TextContent(type="text", text="Now write the forecast."),
],
),
]
out = OpenAISamplingHandler._convert_to_openai_messages(
system_prompt=None, messages=messages
)
for m in out:
print(json.dumps(m, default=str))
print("role=tool messages:", sum(m.get("role") == "tool" for m in out))Version Information
FastMCP version: 4.0.4.dev21+258d5ef3 (commit 258d5ef)
MCP version: 2.0.0
Python version: 3.12.14
Platform: macOS-26.6.2-arm64
The handler code was introduced in #2551; I found no existing issue or open PR covering this. Happy to open a PR if welcome. (Prepared with AI assistance following the contributing guidelines; the MRE and the output above are from a real run.)
Activity
BlueX888 commentedon Sep 20, 2026
Reproduced this. Root cause: fastmcp_slim/fastmcp/client/sampling/handlers/openai.py:273 - the elif content_parts: user branch appends the user message without emitting the tool_messages collected from that same list, so the tool result is silently dropped.
Minimal reproduction:
Observed output:
Happy to open a PR with this fix, or to be assigned if you prefer to take it.