-
Notifications
You must be signed in to change notification settings - Fork 593
Description
Describe the bug
I'm trying to use the new "Sampling with Tools" feature of the 2025-11-25 version of the spec, implemented in the C# SDK in #976.
My application fails at the point where the sampling handler returns the tool call result to the LLM. The error (which was difficult to catch) is:
I think this is a bug in the SDK. I fed the error message to GitHub Copilot and it says (extracting just the key bit):
The problem was that the default
samplingHandlerfromCreateSamplingHandler()wasn't properly converting between MCP protocol message formats and OpenAI API message formats. Specifically:
- MCP protocol uses
Role.AssistantwithToolUseContentBlockandRole.UserwithToolResultContentBlock- OpenAI API expects
Role.Assistant1 with tool_calls andRole.Tool]` with tool results
The code in CreateSamplingHandler() to set the Role is:
messages.Add(new ChatMessage(sm.Role is Role.Assistant ? ChatRole.Assistant : ChatRole.User, aiContents));So it sets tool results to ChatRole.User so OpenAI doesn't see them as tool results.
To Reproduce
Code for the repro is in the issue-repro branch of my SamplingWithToolsDemo repo.
You will need a GitHub PAT with models permissions, set in the GITHUB_TOKEN environment variable.
Start the server:
cd server
dotnet runStart the client in the debugger. Set a break point on the call to samplingHandler(c, p, t) -- line 51 of client/Program.cs.
At the Prompt, enter
Play a single game of craps. Report the results of each roll and explain how it affects the outcome of the game.
The break point will hit twice. When it hits the second time, enable break on "All Exceptions". Then continue.
Expected behavior
The call to OpenAI with tool call results should succeed.
Logs
Will be happy to provide any if needed.
Additional context
I made a copy of CreateSamplingHandler() and changed the code to set the ChatRole to Tool for any messages with tool call results and the app then worked successfully. I'll push a copy of this code up to my repo shortly.