Skip to content

Support SSL certificate verification option when creating httpx AsyncClient #1280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/mcp/client/sse.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ async def sse_client(
sse_read_timeout: float = 60 * 5,
httpx_client_factory: McpHttpClientFactory = create_mcp_http_client,
auth: httpx.Auth | None = None,
verify: bool | None = None,
):
"""
Client transport for SSE.
Expand Down Expand Up @@ -55,7 +56,10 @@ async def sse_client(
try:
logger.debug(f"Connecting to SSE endpoint: {remove_request_params(url)}")
async with httpx_client_factory(
headers=headers, auth=auth, timeout=httpx.Timeout(timeout, read=sse_read_timeout)
headers=headers,
timeout=httpx.Timeout(timeout, read=sse_read_timeout),
auth=auth,
verify=verify,
) as client:
async with aconnect_sse(
client,
Expand Down
2 changes: 2 additions & 0 deletions src/mcp/client/streamable_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ async def streamablehttp_client(
terminate_on_close: bool = True,
httpx_client_factory: McpHttpClientFactory = create_mcp_http_client,
auth: httpx.Auth | None = None,
verify: bool | None = None,
) -> AsyncGenerator[
tuple[
MemoryObjectReceiveStream[SessionMessage | Exception],
Expand Down Expand Up @@ -481,6 +482,7 @@ async def streamablehttp_client(
headers=transport.request_headers,
timeout=httpx.Timeout(transport.timeout, read=transport.sse_read_timeout),
auth=transport.auth,
verify=verify,
) as client:
# Define callbacks that need access to tg
def start_get_stream() -> None:
Expand Down
12 changes: 12 additions & 0 deletions src/mcp/shared/_httpx_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ def __call__(
headers: dict[str, str] | None = None,
timeout: httpx.Timeout | None = None,
auth: httpx.Auth | None = None,
verify: bool | None = None,
) -> httpx.AsyncClient: ...


def create_mcp_http_client(
headers: dict[str, str] | None = None,
timeout: httpx.Timeout | None = None,
auth: httpx.Auth | None = None,
verify: bool | None = None,
) -> httpx.AsyncClient:
"""Create a standardized httpx AsyncClient with MCP defaults.

Expand All @@ -32,6 +34,8 @@ def create_mcp_http_client(
timeout: Request timeout as httpx.Timeout object.
Defaults to 30 seconds if not specified.
auth: Optional authentication handler.
verify: Either True to use default CA bundle, False to disable verification, or an instance of ssl.SSLContext.


Returns:
Configured httpx.AsyncClient instance with MCP defaults.
Expand Down Expand Up @@ -60,6 +64,10 @@ def create_mcp_http_client(
auth = BasicAuth(username="user", password="pass")
async with create_mcp_http_client(headers, timeout, auth) as client:
response = await client.get("/protected-endpoint")

# With SSL verification disabled
async with create_mcp_http_client(verify=False) as client:
response = await client.get("/insecure-endpoint")
"""
# Set MCP defaults
kwargs: dict[str, Any] = {
Expand All @@ -80,4 +88,8 @@ def create_mcp_http_client(
if auth is not None:
kwargs["auth"] = auth

# Handle SSL verification
if verify is not None:
kwargs["verify"] = verify

return httpx.AsyncClient(**kwargs)
Loading