Skip to content

Introduce a function to create a standard AsyncClient with options #655

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

Merged
merged 5 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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: 3 additions & 3 deletions examples/servers/simple-auth/mcp_simple_auth/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from typing import Any

import click
import httpx
from pydantic import AnyHttpUrl
from pydantic_settings import BaseSettings, SettingsConfigDict
from starlette.exceptions import HTTPException
Expand All @@ -25,6 +24,7 @@
from mcp.server.auth.settings import AuthSettings, ClientRegistrationOptions
from mcp.server.fastmcp.server import FastMCP
from mcp.shared.auth import OAuthClientInformationFull, OAuthToken
from mcp.shared.httpx_utils import create_mcp_http_client

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -123,7 +123,7 @@ async def handle_github_callback(self, code: str, state: str) -> str:
client_id = state_data["client_id"]

# Exchange code for token with GitHub
async with httpx.AsyncClient() as client:
async with create_mcp_http_client() as client:
response = await client.post(
self.settings.github_token_url,
data={
Expand Down Expand Up @@ -325,7 +325,7 @@ async def get_user_profile() -> dict[str, Any]:
"""
github_token = get_github_token()

async with httpx.AsyncClient() as client:
async with create_mcp_http_client() as client:
response = await client.get(
"https://api.github.com/user",
headers={
Expand Down
4 changes: 2 additions & 2 deletions examples/servers/simple-tool/mcp_simple_tool/server.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import anyio
import click
import httpx
import mcp.types as types
from mcp.server.lowlevel import Server
from mcp.shared.httpx_utils import create_mcp_http_client


async def fetch_website(
Expand All @@ -11,7 +11,7 @@ async def fetch_website(
headers = {
"User-Agent": "MCP Test Server (github.com/modelcontextprotocol/python-sdk)"
}
async with httpx.AsyncClient(follow_redirects=True, headers=headers) as client:
async with create_mcp_http_client(headers=headers) as client:
response = await client.get(url)
response.raise_for_status()
return [types.TextContent(type="text", text=response.text)]
Expand Down
3 changes: 2 additions & 1 deletion src/mcp/client/sse.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from httpx_sse import aconnect_sse

import mcp.types as types
from mcp.shared.httpx_utils import create_mcp_http_client
from mcp.shared.message import SessionMessage

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -44,7 +45,7 @@ async def sse_client(
async with anyio.create_task_group() as tg:
try:
logger.info(f"Connecting to SSE endpoint: {remove_request_params(url)}")
async with httpx.AsyncClient(headers=headers) as client:
async with create_mcp_http_client(headers=headers) as client:
async with aconnect_sse(
client,
"GET",
Expand Down
4 changes: 2 additions & 2 deletions src/mcp/client/streamable_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
from httpx_sse import EventSource, ServerSentEvent, aconnect_sse

from mcp.shared.httpx_utils import create_mcp_http_client
from mcp.shared.message import ClientMessageMetadata, SessionMessage
from mcp.types import (
ErrorData,
Expand Down Expand Up @@ -446,12 +447,11 @@ async def streamablehttp_client(
try:
logger.info(f"Connecting to StreamableHTTP endpoint: {url}")

async with httpx.AsyncClient(
async with create_mcp_http_client(
headers=transport.request_headers,
timeout=httpx.Timeout(
transport.timeout.seconds, read=transport.sse_read_timeout.seconds
),
follow_redirects=True,
) as client:
# Define callbacks that need access to tg
def start_get_stream() -> None:
Expand Down
64 changes: 64 additions & 0 deletions src/mcp/shared/httpx_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""Utilities for creating standardized httpx AsyncClient instances."""

from __future__ import annotations

from typing import Any

import httpx

__all__ = ["create_mcp_http_client"]


def create_mcp_http_client(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this repository follow private/public APIs properly, but maybe it should start...

Suggested change
def create_mcp_http_client(
def _create_mcp_http_client(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to use create_mcp_http_client across different client transport implementations, so making it private would cause Pyright warnings about accessing private members from other modules.

If we want to indicate that the utils module contains internal implementation details, we could consider renaming the file to something like _internal_httpx_utils.py. This would signal that the module itself is internal while keeping the functions public for cross-module usage.

However, given that this function is in shared/ and genuinely needs to be shared across modules, keeping it public seems more appropriate. Happy to change it if you prefer the internal module approach though or I'm missing something very obvious.

Copy link
Member

@Kludex Kludex May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to use create_mcp_http_client across different client transport implementations, so making it private would cause Pyright warnings about accessing private members from other modules.

Sorry, I meant making the module private _httpx_utils.py, and keeping create_mcp_http_client.

If we want to indicate that the utils module contains internal implementation details, we could consider renaming the file to something like _internal_httpx_utils.py. This would signal that the module itself is internal while keeping the functions public for cross-module usage.

Exactly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not strong, since the rest here doesn't follow it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, thank you! Applied the change. Agree, we need to start somewhere

headers: dict[str, Any] | None = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
headers: dict[str, Any] | None = None,
headers: dict[str, str] | None = None,

ref.: https://github.com/encode/httpx/blob/6c7af967734bafd011164f2a1653abc87905a62b/httpx/_models.py#L139

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
headers: dict[str, Any] | None = None,
headers: dict[str, str] | None = None,

ref.: https://github.com/encode/httpx/blob/6c7af967734bafd011164f2a1653abc87905a62b/httpx/_models.py#L139

timeout: httpx.Timeout | None = None,
) -> httpx.AsyncClient:
"""Create a standardized httpx AsyncClient with MCP defaults.

This function provides common defaults used throughout the MCP codebase:
- follow_redirects=True (always enabled)
- Default timeout of 30 seconds if not specified

Args:
headers: Optional headers to include with all requests.
timeout: Request timeout as httpx.Timeout object.
Defaults to 30 seconds if not specified.

Returns:
Configured httpx.AsyncClient instance with MCP defaults.

Note:
The returned AsyncClient must be used as a context manager to ensure
proper cleanup of connections.

Examples:
# Basic usage with MCP defaults
async with create_mcp_http_client() as client:
response = await client.get("https://api.example.com")

# With custom headers
headers = {"Authorization": "Bearer token"}
async with create_mcp_http_client(headers) as client:
response = await client.get("/endpoint")

# With both custom headers and timeout
timeout = httpx.Timeout(60.0, read=300.0)
async with create_mcp_http_client(headers, timeout) as client:
response = await client.get("/long-request")
"""
# Set MCP defaults
kwargs: dict[str, Any] = {
"follow_redirects": True,
}

# Handle timeout
if timeout is None:
kwargs["timeout"] = httpx.Timeout(30.0)
else:
kwargs["timeout"] = timeout

# Handle headers
if headers is not None:
kwargs["headers"] = headers

return httpx.AsyncClient(**kwargs)
24 changes: 24 additions & 0 deletions tests/shared/test_httpx_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Tests for httpx utility functions."""

import httpx

from mcp.shared.httpx_utils import create_mcp_http_client


def test_default_settings():
"""Test that default settings are applied correctly."""
client = create_mcp_http_client()

assert client.follow_redirects is True
assert client.timeout.connect == 30.0


def test_custom_parameters():
"""Test custom headers and timeout are set correctly."""
headers = {"Authorization": "Bearer token"}
timeout = httpx.Timeout(60.0)

client = create_mcp_http_client(headers, timeout)

assert client.headers["Authorization"] == "Bearer token"
assert client.timeout.connect == 60.0
Loading