diff --git a/litellm/completion_extras/litellm_responses_transformation/transformation.py b/litellm/completion_extras/litellm_responses_transformation/transformation.py index bb59b4617af..1453a86d31d 100644 --- a/litellm/completion_extras/litellm_responses_transformation/transformation.py +++ b/litellm/completion_extras/litellm_responses_transformation/transformation.py @@ -411,6 +411,17 @@ def replayed_tool_search_tool_names(request: object) -> tuple[str, ...]: return tuple(chain.from_iterable(_tool_definition_name(tool) for tool in replayed_tool_search_tools(request))) +def with_namespace_members(tools: Sequence[object]) -> tuple[object, ...]: + """A Responses ``namespace`` tool stands for its member tools, as in a replayed search.""" + return tuple(chain.from_iterable(_namespace_members(tool) for tool in tools)) + + +def input_tool_search_tools(request: object) -> tuple[object, ...]: + """The tools a ``tool_search_output`` item in a Responses ``input`` loads.""" + items: Final = _tool_search_items_from_output_items(_json_field(request, "input")) + return with_namespace_members(tuple(chain.from_iterable(_loaded_tools(item) for item in items))) + + def _loads_any_tool(message: object, names: frozenset[str]) -> bool: loaded_names: Final = chain.from_iterable(_tool_definition_name(tool) for tool in _message_loaded_tools(message)) return any(name in names for name in loaded_names) diff --git a/litellm/llms/openai/responses/guardrail_translation/handler.py b/litellm/llms/openai/responses/guardrail_translation/handler.py index 559daf767c2..4ebe0f066ac 100644 --- a/litellm/llms/openai/responses/guardrail_translation/handler.py +++ b/litellm/llms/openai/responses/guardrail_translation/handler.py @@ -45,7 +45,9 @@ from litellm._logging import verbose_proxy_logger from litellm.completion_extras.litellm_responses_transformation.transformation import ( LiteLLMResponsesTransformationHandler, OpenAiResponsesToChatCompletionStreamIterator, + input_tool_search_tools, tool_call_dict_from_output_item, + with_namespace_members, ) from litellm.litellm_core_utils.core_helpers import has_attributes from litellm.llms.base_llm.guardrail_translation.base_translation import ( @@ -929,9 +931,10 @@ class OpenAIResponsesHandler(BaseTranslation): def extract_request_tool_names(self, data: Mapping[str, object]) -> list[str]: """Extract tool names from Responses API request (tools[].name for function - and custom, tools[].server_label for mcp).""" + and custom, tools[].server_label for mcp), opening namespace tools into their + members and adding the tools a tool_search_output in input[] loads.""" names: Final[list[str]] = [] - for tool in list_field(data, "tools"): + for tool in (*with_namespace_members(list_field(data, "tools")), *input_tool_search_tools(data)): if not isinstance(tool, dict): continue if tool.get("type") in ("function", "custom") and tool.get("name"): diff --git a/tests/unit/proxy/test_tools_allowlist_enforcement.py b/tests/unit/proxy/test_tools_allowlist_enforcement.py index e306f7be4f8..5822c19002d 100644 --- a/tests/unit/proxy/test_tools_allowlist_enforcement.py +++ b/tests/unit/proxy/test_tools_allowlist_enforcement.py @@ -58,9 +58,7 @@ class TestExtractRequestToolNames: {"type": "function", "name": "get_current_weather", "description": "x"}, ] } - assert extract_request_tool_names("/v1/responses", data) == [ - "get_current_weather" - ] + assert extract_request_tool_names("/v1/responses", data) == ["get_current_weather"] def test_openai_responses_mcp_tools(self): data = { @@ -86,6 +84,42 @@ class TestExtractRequestToolNames: "get_current_weather", ] + def test_openai_responses_namespace_tools(self): + """Codex sends MCP tools grouped in a namespace; the allowlist must see each member.""" + data = { + "tools": [ + {"type": "function", "name": "exec_command"}, + { + "type": "namespace", + "name": "mcp__mock", + "tools": [{"type": "function", "name": "search_issues"}, {"type": "function", "name": "get_issue"}], + }, + ] + } + assert extract_request_tool_names("/v1/responses", data) == ["exec_command", "search_issues", "get_issue"] + + def test_openai_responses_tool_search_output_in_input(self): + """With tool_search, deferred MCP tools arrive only in input[], never in tools[].""" + data = { + "tools": [{"type": "tool_search"}], + "input": [ + {"role": "user", "content": "find the issue"}, + {"type": "tool_search_call", "call_id": "ts_1", "execution": "client"}, + { + "type": "tool_search_output", + "call_id": "ts_1", + "tools": [ + { + "type": "namespace", + "name": "mcp__mock", + "tools": [{"type": "function", "name": "search_issues", "defer_loading": True}], + } + ], + }, + ], + } + assert extract_request_tool_names("/v1/responses", data) == ["search_issues"] + def test_anthropic_tools(self): data = {"tools": [{"name": "get_weather"}, {"name": "run_sql"}]} assert extract_request_tool_names("/v1/messages", data) == [ @@ -129,9 +163,7 @@ class TestExtractRequestToolNames: }, ] } - assert extract_request_tool_names("/generate_content", data) == [ - "schedule_meeting" - ] + assert extract_request_tool_names("/generate_content", data) == ["schedule_meeting"] def test_mcp_call_tool_name(self): data = {"name": "my_tool", "arguments": {}} @@ -242,6 +274,46 @@ class TestCheckToolsAllowlist: assert exc_info.value.type == ProxyErrorTypes.tool_access_denied assert "restricted_tool" in str(exc_info.value.message) + @pytest.mark.parametrize( + "body", + [ + { + "tools": [ + {"type": "namespace", "name": "mcp__mock", "tools": [{"type": "function", "name": "run_shell"}]} + ] + }, + { + "tools": [{"type": "tool_search"}], + "input": [ + { + "type": "tool_search_output", + "call_id": "ts_1", + "tools": [ + { + "type": "namespace", + "name": "mcp__mock", + "tools": [{"type": "function", "name": "run_shell", "defer_loading": True}], + } + ], + } + ], + }, + ], + ids=["namespace_in_tools", "tool_search_output_in_input"], + ) + @pytest.mark.asyncio + async def test_disallowed_namespace_member_raises_on_responses_route(self, body): + token = _token(metadata={"allowed_tools": ["get_weather"]}) + with pytest.raises(ProxyException) as exc_info: + await check_tools_allowlist( + request_body=body, + valid_token=token, + team_object=None, + route="/v1/responses", + ) + assert exc_info.value.type == ProxyErrorTypes.tool_access_denied + assert "run_shell" in str(exc_info.value.message) + @pytest.mark.parametrize( "loaded_tool", [