Skip to content

Commit 84667c2

Browse files
committed
fix(fetch): add configurable SSL certificate verification
Add MCP_FETCH_SSL_VERIFY environment variable to control SSL certificate verification, allowing the fetch server to work with internal servers that use self-signed certificates. Resolves modelcontextprotocol#508. - SSL verification remains enabled by default (fail-secure: only the literal value 'false' disables it) - Pass verify=SSL_VERIFY to both AsyncClient instances - Catch ssl.SSLError and SSL errors wrapped in httpx.ConnectError, with error messages guiding users to MCP_FETCH_SSL_VERIFY=false - Document the env var in README with a security warning - Add tests for env var parsing and SSL error handling
1 parent 599dafc commit 84667c2

3 files changed

Lines changed: 167 additions & 5 deletions

File tree

‎src/fetch/README.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,29 @@ This can be customized by adding the argument `--user-agent=YourUserAgent` to th
174174

175175
The server can be configured to use a proxy by using the `--proxy-url` argument.
176176

177+
### Customization - SSL Verification
178+
179+
By default, SSL certificate verification is enabled. If you need to fetch content from internal servers that use self-signed certificates, you can disable verification by setting the `MCP_FETCH_SSL_VERIFY` environment variable to `false`:
180+
181+
```json
182+
{
183+
"mcpServers": {
184+
"fetch": {
185+
"command": "uvx",
186+
"args": ["mcp-server-fetch"],
187+
"env": {
188+
"MCP_FETCH_SSL_VERIFY": "false"
189+
}
190+
}
191+
}
192+
}
193+
```
194+
195+
Parsing is fail-secure: any value other than `false` keeps SSL verification enabled.
196+
197+
> [!WARNING]
198+
> Disabling SSL verification reduces security. Only use this in trusted environments with internal servers that have self-signed certificates.
199+
177200
## Windows Configuration
178201

179202
If you're experiencing timeout issues on Windows, you may need to set the `PYTHONIOENCODING` environment variable to ensure proper character encoding:

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

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
import ssl
13
from typing import Annotated, Tuple
24
from urllib.parse import urlparse, urlunparse
35

@@ -23,6 +25,10 @@
2325
DEFAULT_USER_AGENT_AUTONOMOUS = "ModelContextProtocol/1.0 (Autonomous; +https://github.com/modelcontextprotocol/servers)"
2426
DEFAULT_USER_AGENT_MANUAL = "ModelContextProtocol/1.0 (User-Specified; +https://github.com/modelcontextprotocol/servers)"
2527

28+
# Set MCP_FETCH_SSL_VERIFY=false to disable SSL verification for internal/self-signed certificates.
29+
# Fail-secure: any value other than "false" keeps SSL verification enabled.
30+
SSL_VERIFY = os.getenv("MCP_FETCH_SSL_VERIFY", "true").strip().lower() != "false"
31+
2632

2733
def extract_content_from_html(html: str) -> str:
2834
"""Extract and convert HTML content to Markdown format.
@@ -68,17 +74,40 @@ async def check_may_autonomously_fetch_url(url: str, user_agent: str, proxy_url:
6874
Check if the URL can be fetched by the user agent according to the robots.txt file.
6975
Raises a McpError if not.
7076
"""
71-
from httpx import AsyncClient, HTTPError
77+
from httpx import AsyncClient, ConnectError, HTTPError
7278

7379
robot_txt_url = get_robots_txt_url(url)
7480

75-
async with AsyncClient(proxy=proxy_url) as client:
81+
async with AsyncClient(proxy=proxy_url, verify=SSL_VERIFY) as client:
7682
try:
7783
response = await client.get(
7884
robot_txt_url,
7985
follow_redirects=True,
8086
headers={"User-Agent": user_agent},
8187
)
88+
except ssl.SSLError as e:
89+
raise McpError(ErrorData(
90+
code=INTERNAL_ERROR,
91+
message=f"SSL certificate verification failed for {robot_txt_url}. "
92+
f"If this is an internal server with a self-signed certificate, "
93+
f"set MCP_FETCH_SSL_VERIFY=false in your environment. "
94+
f"Error details: {str(e)}",
95+
))
96+
except ConnectError as e:
97+
# httpx wraps SSL errors in ConnectError in some cases
98+
error_str = str(e).lower()
99+
if "ssl" in error_str or "certificate" in error_str or "verify" in error_str:
100+
raise McpError(ErrorData(
101+
code=INTERNAL_ERROR,
102+
message=f"SSL certificate verification failed for {robot_txt_url}. "
103+
f"If this is an internal server with a self-signed certificate, "
104+
f"set MCP_FETCH_SSL_VERIFY=false in your environment. "
105+
f"Error details: {str(e)}",
106+
))
107+
raise McpError(ErrorData(
108+
code=INTERNAL_ERROR,
109+
message=f"Failed to fetch robots.txt {robot_txt_url} due to a connection issue",
110+
))
82111
except HTTPError:
83112
raise McpError(ErrorData(
84113
code=INTERNAL_ERROR,
@@ -114,16 +143,36 @@ async def fetch_url(
114143
"""
115144
Fetch the URL and return the content in a form ready for the LLM, as well as a prefix string with status information.
116145
"""
117-
from httpx import AsyncClient, HTTPError
146+
from httpx import AsyncClient, ConnectError, HTTPError
118147

119-
async with AsyncClient(proxy=proxy_url) as client:
148+
async with AsyncClient(proxy=proxy_url, verify=SSL_VERIFY) as client:
120149
try:
121150
response = await client.get(
122151
url,
123152
follow_redirects=True,
124153
headers={"User-Agent": user_agent},
125154
timeout=30,
126155
)
156+
except ssl.SSLError as e:
157+
raise McpError(ErrorData(
158+
code=INTERNAL_ERROR,
159+
message=f"SSL certificate verification failed for {url}. "
160+
f"If this is an internal server with a self-signed certificate, "
161+
f"set MCP_FETCH_SSL_VERIFY=false in your environment. "
162+
f"Error details: {str(e)}",
163+
))
164+
except ConnectError as e:
165+
# httpx wraps SSL errors in ConnectError in some cases
166+
error_str = str(e).lower()
167+
if "ssl" in error_str or "certificate" in error_str or "verify" in error_str:
168+
raise McpError(ErrorData(
169+
code=INTERNAL_ERROR,
170+
message=f"SSL certificate verification failed for {url}. "
171+
f"If this is an internal server with a self-signed certificate, "
172+
f"set MCP_FETCH_SSL_VERIFY=false in your environment. "
173+
f"Error details: {str(e)}",
174+
))
175+
raise McpError(ErrorData(code=INTERNAL_ERROR, message=f"Failed to fetch {url}: {e!r}"))
127176
except HTTPError as e:
128177
raise McpError(ErrorData(code=INTERNAL_ERROR, message=f"Failed to fetch {url}: {e!r}"))
129178
if response.status_code >= 400:

‎src/fetch/tests/test_server.py‎

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,94 @@ async def test_fetch_with_proxy(self):
323323
)
324324

325325
# Verify AsyncClient was called with proxy
326-
mock_client_class.assert_called_once_with(proxy="http://proxy.example.com:8080")
326+
mock_client_class.assert_called_once_with(proxy="http://proxy.example.com:8080", verify=True)
327+
328+
329+
class TestSSLVerifyConfiguration:
330+
"""Tests for the MCP_FETCH_SSL_VERIFY environment variable."""
331+
332+
def _reload_ssl_verify(self, value):
333+
"""Reload the server module with MCP_FETCH_SSL_VERIFY set to value."""
334+
import importlib
335+
import os
336+
import mcp_server_fetch.server as server_module
337+
338+
env = {"MCP_FETCH_SSL_VERIFY": value} if value is not None else {}
339+
with patch.dict(os.environ, env, clear=False):
340+
if value is None:
341+
os.environ.pop("MCP_FETCH_SSL_VERIFY", None)
342+
importlib.reload(server_module)
343+
result = server_module.SSL_VERIFY
344+
importlib.reload(server_module)
345+
return result
346+
347+
def test_ssl_verify_enabled_by_default(self):
348+
"""SSL verification is enabled when the env var is not set."""
349+
assert self._reload_ssl_verify(None) is True
350+
351+
def test_ssl_verify_disabled_by_false(self):
352+
"""Setting MCP_FETCH_SSL_VERIFY=false disables verification."""
353+
assert self._reload_ssl_verify("false") is False
354+
355+
def test_ssl_verify_disabled_case_insensitive(self):
356+
"""Value parsing is case-insensitive."""
357+
assert self._reload_ssl_verify("FALSE") is False
358+
359+
def test_ssl_verify_stays_enabled_for_other_values(self):
360+
"""Fail-secure: any value other than 'false' keeps verification enabled."""
361+
for value in ("true", "0", "no", "off", "", "garbage"):
362+
assert self._reload_ssl_verify(value) is True
363+
364+
365+
class TestSSLErrorHandling:
366+
"""Tests for SSL error handling in fetch_url."""
367+
368+
@pytest.mark.asyncio
369+
async def test_ssl_error_message_mentions_env_var(self):
370+
"""SSL errors produce a helpful message pointing to MCP_FETCH_SSL_VERIFY."""
371+
import ssl
372+
373+
with patch("httpx.AsyncClient") as mock_client_class:
374+
mock_client = AsyncMock()
375+
mock_client.get = AsyncMock(side_effect=ssl.SSLError("certificate verify failed"))
376+
mock_client_class.return_value.__aenter__ = AsyncMock(return_value=mock_client)
377+
mock_client_class.return_value.__aexit__ = AsyncMock(return_value=None)
378+
379+
with pytest.raises(McpError) as exc_info:
380+
await fetch_url("https://example.com", DEFAULT_USER_AGENT_AUTONOMOUS)
381+
382+
assert "MCP_FETCH_SSL_VERIFY" in str(exc_info.value)
383+
384+
@pytest.mark.asyncio
385+
async def test_connect_error_wrapping_ssl_mentions_env_var(self):
386+
"""SSL errors wrapped in ConnectError are detected and explained."""
387+
from httpx import ConnectError
388+
389+
with patch("httpx.AsyncClient") as mock_client_class:
390+
mock_client = AsyncMock()
391+
mock_client.get = AsyncMock(
392+
side_effect=ConnectError("certificate verify failed: self-signed certificate")
393+
)
394+
mock_client_class.return_value.__aenter__ = AsyncMock(return_value=mock_client)
395+
mock_client_class.return_value.__aexit__ = AsyncMock(return_value=None)
396+
397+
with pytest.raises(McpError) as exc_info:
398+
await fetch_url("https://example.com", DEFAULT_USER_AGENT_AUTONOMOUS)
399+
400+
assert "MCP_FETCH_SSL_VERIFY" in str(exc_info.value)
401+
402+
@pytest.mark.asyncio
403+
async def test_non_ssl_connect_error_not_misreported(self):
404+
"""Non-SSL connection errors are not reported as SSL failures."""
405+
from httpx import ConnectError
406+
407+
with patch("httpx.AsyncClient") as mock_client_class:
408+
mock_client = AsyncMock()
409+
mock_client.get = AsyncMock(side_effect=ConnectError("connection refused"))
410+
mock_client_class.return_value.__aenter__ = AsyncMock(return_value=mock_client)
411+
mock_client_class.return_value.__aexit__ = AsyncMock(return_value=None)
412+
413+
with pytest.raises(McpError) as exc_info:
414+
await fetch_url("https://example.com", DEFAULT_USER_AGENT_AUTONOMOUS)
415+
416+
assert "MCP_FETCH_SSL_VERIFY" not in str(exc_info.value)

0 commit comments

Comments
 (0)