{"record":{"id":"864799a213248611","repo":"PrefectHQ/fastmcp","slug":"unsupported-tool-choice-mode-tool-choice-mode-r","errorCode":null,"errorMessage":"Unsupported tool_choice mode: {tool_choice.mode!r}","messagePattern":"Unsupported tool_choice mode: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/client/sampling/handlers/anthropic.py","lineNumber":404,"sourceCode":"    @staticmethod\n    def _convert_tool_choice_to_anthropic(\n        tool_choice: ToolChoice,\n    ) -> ToolChoiceParam | None:\n        \"\"\"Convert MCP tool_choice to Anthropic format.\n\n        Returns None for \"none\" mode, signaling that tools should be omitted\n        from the request entirely (Anthropic doesn't have an explicit \"none\" option).\n        \"\"\"\n        if tool_choice.mode == \"auto\":\n            return ToolChoiceAutoParam(type=\"auto\")\n        elif tool_choice.mode == \"required\":\n            return ToolChoiceAnyParam(type=\"any\")\n        elif tool_choice.mode == \"none\":\n            # Anthropic doesn't have a \"none\" option - return None to signal\n            # that tools should be omitted from the request entirely\n            return None\n        else:\n            raise ValueError(f\"Unsupported tool_choice mode: {tool_choice.mode!r}\")\n\n    @staticmethod\n    def _message_to_result_with_tools(\n        message: Message,\n    ) -> CreateMessageResultWithTools:\n        \"\"\"Convert Anthropic response to CreateMessageResultWithTools.\"\"\"\n        if len(message.content) == 0:\n            raise ValueError(\"No content in response from Anthropic\")\n\n        # Determine stop reason\n        stop_reason: StopReason\n        if message.stop_reason == \"tool_use\":\n            stop_reason = \"toolUse\"\n        elif message.stop_reason == \"end_turn\":\n            stop_reason = \"endTurn\"\n        elif message.stop_reason == \"max_tokens\":\n            stop_reason = \"maxTokens\"\n        elif message.stop_reason == \"stop_sequence\":","sourceCodeStart":386,"sourceCodeEnd":422,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/client/sampling/handlers/anthropic.py#L386-L422","documentation":"_convert_tool_choice_to_anthropic raises ValueError for a ToolChoice mode it does not recognize. Anthropic supports auto/any (required)/none mappings; anything else (e.g. a new MCP mode) is rejected rather than silently mis-mapped.","triggerScenarios":"A server sends a sampling request with tool_choice.mode set to a value other than 'auto', 'required', or 'none' (or a newer MCP spec mode the handler predates).","commonSituations":"MCP spec updates introducing new tool_choice modes; hand-constructed SamplingParams with a typo'd mode string; forwarding a tool_choice from another provider unmodified.","solutions":["Set tool_choice.mode to 'auto', 'required', or 'none' before calling the handler.","Upgrade fastmcp-slim in case a newer MCP mode has gained support.","Catch ValueError and default tool_choice to auto in your sampling callback.","Check the server implementation producing the sampling request for non-standard modes."],"exampleFix":"// before\nparams.tool_choice = ToolChoice(mode=\"specific\")\n// after\nparams.tool_choice = ToolChoice(mode=\"auto\")  # or 'required' / 'none'","handlingStrategy":"validation","validationCode":"VALID_MODES = {\"auto\", \"required\", \"none\"}\nif params.tool_choice and params.tool_choice.mode not in VALID_MODES:\n    params.tool_choice = None  # or ToolChoice(mode=\"auto\")","typeGuard":"def is_valid_tool_choice(tc) -> bool:\n    return tc is None or getattr(tc, \"mode\", None) in {\"auto\", \"required\", \"none\"}","tryCatchPattern":"try:\n    result = await handler(messages, params, context)\nexcept ValueError as e:\n    if \"Unsupported tool_choice mode\" in str(e):\n        params.tool_choice = ToolChoice(mode=\"auto\")\n        result = await handler(messages, params, context)\n    else:\n        raise","preventionTips":["Only use auto/required/none with the Anthropic handler","Never forward another provider's tool_choice values verbatim","Keep fastmcp-slim updated for new MCP tool_choice modes","Validate SamplingParams server-side before sending"],"tags":["anthropic","sampling","tool-choice","valueerror"],"backgroundTag":"unsupported-parameter-value","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}