Skip to content

Commit 7be453d

Browse files
committed
fix(fetch): add configurable SSL certificate verification
Resolves modelcontextprotocol#508 Added MCP_FETCH_SSL_VERIFY environment variable to control SSL certificate verification. This allows the fetch server to work with internal servers that use self-signed certificates. ## Changes - Added SSL_VERIFY configuration via MCP_FETCH_SSL_VERIFY env var (default: true) - Added verify=SSL_VERIFY to both AsyncClient instances - Added comprehensive SSL error handling with helpful error messages - Error messages guide users to set MCP_FETCH_SSL_VERIFY=false for self-signed certs ## Usage ```bash # For servers with self-signed certificates: export MCP_FETCH_SSL_VERIFY=false ``` ## Security Note Disabling SSL verification reduces security. Only use in trusted environments with internal servers that have self-signed certificates.
1 parent 862e717 commit 7be453d

2 files changed

Lines changed: 82 additions & 6 deletions

File tree

‎src/fetch/README.md‎

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

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

173+
### Customization - SSL Verification
174+
175+
By default, the server verifies SSL certificates for all HTTPS requests. For internal servers with self-signed certificates, you can disable SSL verification by setting the `MCP_FETCH_SSL_VERIFY` environment variable to `false`:
176+
177+
```json
178+
{
179+
"mcpServers": {
180+
"fetch": {
181+
"command": "uvx",
182+
"args": ["mcp-server-fetch"],
183+
"env": {
184+
"MCP_FETCH_SSL_VERIFY": "false"
185+
}
186+
}
187+
}
188+
}
189+
```
190+
191+
> [!WARNING]
192+
> Disabling SSL verification reduces security. Only use this option in trusted environments with internal servers that have self-signed certificates.
193+
173194
## Windows Configuration
174195

175196
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: 61 additions & 6 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

@@ -20,6 +22,14 @@
2022
from protego import Protego
2123
from pydantic import BaseModel, Field, AnyUrl
2224

25+
# =============================================================================
26+
# SSL CONFIGURATION
27+
# =============================================================================
28+
# Set MCP_FETCH_SSL_VERIFY=false to disable SSL certificate verification.
29+
# This is useful for internal servers with self-signed certificates.
30+
# WARNING: Disabling SSL verification reduces security. Only use in trusted environments.
31+
SSL_VERIFY = os.getenv("MCP_FETCH_SSL_VERIFY", "true").lower() == "true"
32+
2333
DEFAULT_USER_AGENT_AUTONOMOUS = "ModelContextProtocol/1.0 (Autonomous; +https://github.com/modelcontextprotocol/servers)"
2434
DEFAULT_USER_AGENT_MANUAL = "ModelContextProtocol/1.0 (User-Specified; +https://github.com/modelcontextprotocol/servers)"
2535

@@ -68,18 +78,41 @@ async def check_may_autonomously_fetch_url(url: str, user_agent: str, proxy_url:
6878
Check if the URL can be fetched by the user agent according to the robots.txt file.
6979
Raises a McpError if not.
7080
"""
71-
from httpx import AsyncClient, HTTPError
81+
import httpx
7282

7383
robot_txt_url = get_robots_txt_url(url)
7484

75-
async with AsyncClient(proxies=proxy_url) as client:
85+
async with httpx.AsyncClient(proxies=proxy_url, verify=SSL_VERIFY) as client:
7686
try:
7787
response = await client.get(
7888
robot_txt_url,
7989
follow_redirects=True,
8090
headers={"User-Agent": user_agent},
91+
timeout=30,
8192
)
82-
except HTTPError:
93+
except ssl.SSLError as e:
94+
raise McpError(ErrorData(
95+
code=INTERNAL_ERROR,
96+
message=f"SSL Certificate verification failed for {robot_txt_url}. "
97+
f"If this is an internal server with a self-signed certificate, "
98+
f"set MCP_FETCH_SSL_VERIFY=false in your environment. "
99+
f"Error details: {str(e)}",
100+
))
101+
except httpx.ConnectError as e:
102+
error_str = str(e).lower()
103+
if "ssl" in error_str or "certificate" in error_str or "verify" in error_str:
104+
raise McpError(ErrorData(
105+
code=INTERNAL_ERROR,
106+
message=f"SSL Certificate verification failed for {robot_txt_url}. "
107+
f"If this is an internal server with a self-signed certificate, "
108+
f"set MCP_FETCH_SSL_VERIFY=false in your environment. "
109+
f"Error details: {str(e)}",
110+
))
111+
raise McpError(ErrorData(
112+
code=INTERNAL_ERROR,
113+
message=f"Failed to connect to {robot_txt_url}: {str(e)}",
114+
))
115+
except httpx.HTTPError:
83116
raise McpError(ErrorData(
84117
code=INTERNAL_ERROR,
85118
message=f"Failed to fetch robots.txt {robot_txt_url} due to a connection issue",
@@ -114,17 +147,39 @@ async def fetch_url(
114147
"""
115148
Fetch the URL and return the content in a form ready for the LLM, as well as a prefix string with status information.
116149
"""
117-
from httpx import AsyncClient, HTTPError
150+
import httpx
118151

119-
async with AsyncClient(proxies=proxy_url) as client:
152+
async with httpx.AsyncClient(proxies=proxy_url, verify=SSL_VERIFY) as client:
120153
try:
121154
response = await client.get(
122155
url,
123156
follow_redirects=True,
124157
headers={"User-Agent": user_agent},
125158
timeout=30,
126159
)
127-
except HTTPError as e:
160+
except ssl.SSLError as e:
161+
raise McpError(ErrorData(
162+
code=INTERNAL_ERROR,
163+
message=f"SSL Certificate verification failed for {url}. "
164+
f"If this is an internal server with a self-signed certificate, "
165+
f"set MCP_FETCH_SSL_VERIFY=false in your environment. "
166+
f"Error details: {str(e)}",
167+
))
168+
except httpx.ConnectError as e:
169+
error_str = str(e).lower()
170+
if "ssl" in error_str or "certificate" in error_str or "verify" in error_str:
171+
raise McpError(ErrorData(
172+
code=INTERNAL_ERROR,
173+
message=f"SSL Certificate verification failed for {url}. "
174+
f"If this is an internal server with a self-signed certificate, "
175+
f"set MCP_FETCH_SSL_VERIFY=false in your environment. "
176+
f"Error details: {str(e)}",
177+
))
178+
raise McpError(ErrorData(
179+
code=INTERNAL_ERROR,
180+
message=f"Failed to connect to {url}: {str(e)}",
181+
))
182+
except httpx.HTTPError as e:
128183
raise McpError(ErrorData(code=INTERNAL_ERROR, message=f"Failed to fetch {url}: {e!r}"))
129184
if response.status_code >= 400:
130185
raise McpError(ErrorData(

0 commit comments

Comments
 (0)