π The fast, Pythonic way to build MCP servers and clients.
Map Anthropic stop_sequence to the MCP stopSequence stop reason (#5625)
Asem ahmedc41c388c0a53
diff --git a/fastmcp_slim/fastmcp/client/sampling/handlers/anthropic.py b/fastmcp_slim/fastmcp/client/sampling/handlers/anthropic.pyindex 09c7a57d8..847f34372 100644--- a/fastmcp_slim/fastmcp/client/sampling/handlers/anthropic.py+++ b/fastmcp_slim/fastmcp/client/sampling/handlers/anthropic.py@@ -420,7 +420,7 @@ class AnthropicSamplingHandler:elif message.stop_reason == "max_tokens":stop_reason = "maxTokens"elif message.stop_reason == "stop_sequence":- stop_reason = "endTurn"+ stop_reason = "stopSequence"else:stop_reason = "endTurn"diff --git a/tests/client/sampling/handlers/test_anthropic_handler.py b/tests/client/sampling/handlers/test_anthropic_handler.pyindex 1e1007cf3..4235818af 100644--- a/tests/client/sampling/handlers/test_anthropic_handler.py+++ b/tests/client/sampling/handlers/test_anthropic_handler.py@@ -267,6 +267,34 @@ def test_message_to_result_with_tools():)+ ("anthropic_stop_reason", "expected"),+ [+ ("end_turn", "endTurn"),+ ("stop_sequence", "stopSequence"),+ ("max_tokens", "maxTokens"),+ ("tool_use", "toolUse"),+ ],+)+def test_message_to_result_with_tools_maps_stop_reason(+ anthropic_stop_reason: Any, expected: str+):+ message = Message(+ id="msg_123",+ type="message",+ role="assistant",+ content=[TextBlock(type="text", text="done")],+ model="claude-3-5-sonnet-20241022",+ stop_reason=anthropic_stop_reason,+ stop_sequence="END" if anthropic_stop_reason == "stop_sequence" else None,+ usage=Usage(input_tokens=10, output_tokens=20),+ )++ result = AnthropicSamplingHandler._message_to_result_with_tools(message)++ assert result.stop_reason == expected++def test_convert_tool_choice_auto():result = AnthropicSamplingHandler._convert_tool_choice_to_anthropic(MagicMock(mode="auto")