Skip to content

OpenAISamplingHandler drops ToolResultContent when a user message's list content also carries text/image blocks #5182

Open

Description

@BlueX888

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

  1. added
    bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.
    clientRelated to the FastMCP client SDK or client-side functionality.
    on Sep 20, 2026
  2. BlueX888 commented on Sep 20, 2026

    @BlueX888
    ContributorAuthor

    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:

    python (MRE from the issue body: OpenAISamplingHandler._convert_to_openai_messages(system_prompt=None, messages=[assistant ToolUseContent id=call_1 get_weather; user [ToolResultContent tool_use_id=call_1 '18C sunny', TextContent 'Now write the forecast.']])
    

    Observed output:

    {"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
    

    Happy to open a PR with this fix, or to be assigned if you prefer to take it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.clientRelated to the FastMCP client SDK or client-side functionality.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions