diff --git a/src/strands_tools/http_request.py b/src/strands_tools/http_request.py index 884ff214..a017e598 100644 --- a/src/strands_tools/http_request.py +++ b/src/strands_tools/http_request.py @@ -52,7 +52,7 @@ "JWT, AWS SigV4, Digest auth, and enterprise authentication patterns. Automatically reads tokens from " "environment variables (GITHUB_TOKEN, GITLAB_TOKEN, AWS credentials, etc.) when auth_env_var is specified. " "Use environment(action='list') to view available variables. Includes session management, metrics, " - "streaming support, cookie handling, redirect control, and optional HTML to markdown conversion." + "streaming support, cookie handling, redirect control, proxy support, and optional HTML to markdown conversion." ), "inputSchema": { "json": { @@ -178,6 +178,15 @@ "expiry": {"type": "integer"}, }, }, + "proxies": { + "type": "object", + "description": "Dictionary mapping protocol or protocol and hostname to the URL of the proxy.", + "properties": { + "http": {"type": "string"}, + "https": {"type": "string"}, + "ftp": {"type": "string"}, + }, + }, }, "required": ["method", "url"], } @@ -608,6 +617,15 @@ def http_request(tool: ToolUse, **kwargs: Any) -> ToolResult: ) ``` + 7. Using proxy: + ```python + http_request( + method="GET", + url="https://example.com/api", + proxies={"https": "https://proxy.example.com:8080"}, + ) + ``` + Environment Variables: - Authentication tokens are read from environment when auth_env_var is specified - AWS credentials are automatically loaded from environment variables or credentials file @@ -747,6 +765,7 @@ def http_request(tool: ToolUse, **kwargs: Any) -> ToolResult: "verify": verify, "auth": auth, "allow_redirects": tool_input.get("allow_redirects", True), + "proxies": tool_input.get("proxies", None), } # Set max_redirects if specified diff --git a/tests/test_http_request.py b/tests/test_http_request.py index 5ddf9d12..e00105a3 100644 --- a/tests/test_http_request.py +++ b/tests/test_http_request.py @@ -1045,3 +1045,49 @@ def test_markdown_conversion_non_html(): result_text = extract_result_text(result) assert "Status Code: 200" in result_text assert '"message": "hello"' in result_text # Should still be JSON (no conversion for non-HTML) + + +def test_proxy_support(): + """Test HTTP proxy support functionality.""" + tool_use = { + "toolUseId": "test-proxy-id", + "input": { + "method": "GET", + "url": "https://example.com/api/proxy-test", + "proxies": {"https": "https://proxy.example.com:8080"}, + }, + } + + # Mock the session.request method to capture the proxies parameter + with ( + patch("strands_tools.http_request.get_user_input") as mock_input, + patch("requests.Session.request") as mock_request, + ): + # Configure mock response + mock_response = MagicMock() + mock_response.status_code = 200 + mock_response.text = '{"status": "success via proxy"}' + mock_response.content = b'{"status": "success via proxy"}' + mock_response.headers = {"Content-Type": "application/json"} + mock_response.history = [] + mock_response.url = "https://example.com/api/proxy-test" + mock_response.request = MagicMock() + mock_response.request.body = None + mock_request.return_value = mock_response + + # Mock user input + mock_input.return_value = "y" + + # Call the function + result = http_request.http_request(tool=tool_use) + + # Verify the proxy was actually passed to requests + assert mock_request.called + call_kwargs = mock_request.call_args[1] + assert "proxies" in call_kwargs + assert call_kwargs["proxies"] == {"https": "https://proxy.example.com:8080"} + + # Verify the result + assert result["status"] == "success" + result_text = extract_result_text(result) + assert "Status Code: 200" in result_text