Skip to content

Commit 6a110d6

Browse files
committed
fix(fetch): accept socks proxy alias
1 parent 275175c commit 6a110d6

4 files changed

Lines changed: 84 additions & 5 deletions

File tree

‎src/fetch/pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ classifiers = [
1616
"Programming Language :: Python :: 3.10",
1717
]
1818
dependencies = [
19-
"httpx>=0.27",
19+
"httpx[socks]>=0.27",
2020
"markdownify>=0.13.1",
2121
"mcp>=1.1.3",
2222
"protego>=0.3.1",

‎src/fetch/src/mcp_server_fetch/server.py‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from typing import Annotated, Tuple
23
from urllib.parse import urlparse, urlunparse
34

@@ -63,6 +64,37 @@ def get_robots_txt_url(url: str) -> str:
6364
return robots_url
6465

6566

67+
def normalize_proxy_url(proxy_url: str | None) -> str | None:
68+
if proxy_url is None:
69+
return None
70+
71+
if proxy_url.lower().startswith("socks://"):
72+
return f"socks5://{proxy_url[len('socks://'):]}"
73+
74+
return proxy_url
75+
76+
77+
def proxy_url_for_request(url: str, proxy_url: str | None = None) -> str | None:
78+
if proxy_url:
79+
return normalize_proxy_url(proxy_url)
80+
81+
scheme = urlparse(url).scheme.lower()
82+
proxy_keys = []
83+
if scheme == "https":
84+
proxy_keys.extend(("HTTPS_PROXY", "https_proxy"))
85+
elif scheme == "http":
86+
proxy_keys.extend(("HTTP_PROXY", "http_proxy"))
87+
proxy_keys.extend(("ALL_PROXY", "all_proxy"))
88+
89+
for key in proxy_keys:
90+
env_proxy = os.environ.get(key)
91+
normalized = normalize_proxy_url(env_proxy)
92+
if normalized != env_proxy:
93+
return normalized
94+
95+
return None
96+
97+
6698
async def check_may_autonomously_fetch_url(url: str, user_agent: str, proxy_url: str | None = None) -> None:
6799
"""
68100
Check if the URL can be fetched by the user agent according to the robots.txt file.
@@ -72,7 +104,7 @@ async def check_may_autonomously_fetch_url(url: str, user_agent: str, proxy_url:
72104

73105
robot_txt_url = get_robots_txt_url(url)
74106

75-
async with AsyncClient(proxy=proxy_url) as client:
107+
async with AsyncClient(proxy=proxy_url_for_request(robot_txt_url, proxy_url)) as client:
76108
try:
77109
response = await client.get(
78110
robot_txt_url,
@@ -116,7 +148,7 @@ async def fetch_url(
116148
"""
117149
from httpx import AsyncClient, HTTPError
118150

119-
async with AsyncClient(proxy=proxy_url) as client:
151+
async with AsyncClient(proxy=proxy_url_for_request(url, proxy_url)) as client:
120152
try:
121153
response = await client.get(
122154
url,

‎src/fetch/tests/test_server.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
check_may_autonomously_fetch_url,
1111
fetch_url,
1212
DEFAULT_USER_AGENT_AUTONOMOUS,
13+
proxy_url_for_request,
1314
)
1415

1516

@@ -324,3 +325,35 @@ async def test_fetch_with_proxy(self):
324325

325326
# Verify AsyncClient was called with proxy
326327
mock_client_class.assert_called_once_with(proxy="http://proxy.example.com:8080")
328+
329+
@pytest.mark.asyncio
330+
async def test_fetch_accepts_socks_proxy_alias(self):
331+
"""Test that socks:// proxy URLs are accepted as SOCKS5 proxies."""
332+
mock_response = MagicMock()
333+
mock_response.status_code = 200
334+
mock_response.text = '{"data": "test"}'
335+
mock_response.headers = {"content-type": "application/json"}
336+
337+
with patch("httpx.AsyncClient") as mock_client_class:
338+
mock_client = AsyncMock()
339+
mock_client.get = AsyncMock(return_value=mock_response)
340+
mock_client_class.return_value.__aenter__ = AsyncMock(return_value=mock_client)
341+
mock_client_class.return_value.__aexit__ = AsyncMock(return_value=None)
342+
343+
await fetch_url(
344+
"https://example.com/data",
345+
DEFAULT_USER_AGENT_AUTONOMOUS,
346+
proxy_url="socks://127.0.0.1:2080/"
347+
)
348+
349+
mock_client_class.assert_called_once_with(proxy="socks5://127.0.0.1:2080/")
350+
351+
def test_fetch_accepts_socks_proxy_from_environment(self, monkeypatch):
352+
"""Test that invalid socks:// environment proxies are normalized."""
353+
monkeypatch.delenv("HTTPS_PROXY", raising=False)
354+
monkeypatch.delenv("https_proxy", raising=False)
355+
monkeypatch.delenv("ALL_PROXY", raising=False)
356+
monkeypatch.delenv("all_proxy", raising=False)
357+
monkeypatch.setenv("ALL_PROXY", "socks://127.0.0.1:2080/")
358+
359+
assert proxy_url_for_request("https://example.com/data") == "socks5://127.0.0.1:2080/"

‎src/fetch/uv.lock‎

Lines changed: 16 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)