Skip to content

Commit 29aa1e4

Browse files
author
Your Name
committed
declaring HttpClientFactory interface on utils
1 parent 489a00f commit 29aa1e4

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/agents/mcp/server.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
from mcp import ClientSession, StdioServerParameters, Tool as MCPTool, stdio_client
1414
from mcp.client.sse import sse_client
1515
from mcp.client.streamable_http import GetSessionIdCallback, streamablehttp_client
16-
from mcp.shared._httpx_utils import McpHttpClientFactory
1716
from mcp.shared.message import SessionMessage
1817
from mcp.types import CallToolResult, GetPromptResult, InitializeResult, ListPromptsResult
1918
from typing_extensions import NotRequired, TypedDict
2019

2120
from ..exceptions import UserError
2221
from ..logger import logger
2322
from ..run_context import RunContextWrapper
24-
from .util import ToolFilter, ToolFilterContext, ToolFilterStatic
23+
from .util import HttpClientFactory, ToolFilter, ToolFilterContext, ToolFilterStatic
2524

2625
T = TypeVar("T")
2726

@@ -576,7 +575,7 @@ class MCPServerStreamableHttpParams(TypedDict):
576575
terminate_on_close: NotRequired[bool]
577576
"""Terminate on close"""
578577

579-
httpx_client_factory: NotRequired[McpHttpClientFactory]
578+
httpx_client_factory: NotRequired[HttpClientFactory]
580579
"""Custom HTTP client factory for configuring httpx.AsyncClient behavior."""
581580

582581

src/agents/mcp/util.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import functools
22
import json
33
from dataclasses import dataclass
4-
from typing import TYPE_CHECKING, Any, Callable, Optional, Union
4+
from typing import TYPE_CHECKING, Any, Callable, Optional, Protocol, Union
55

6+
import httpx
67
from typing_extensions import NotRequired, TypedDict
78

89
from .. import _debug
@@ -21,6 +22,21 @@
2122
from .server import MCPServer
2223

2324

25+
class HttpClientFactory(Protocol):
26+
"""Protocol for HTTP client factory functions.
27+
28+
This interface matches the MCP SDK's McpHttpClientFactory but is defined locally
29+
to avoid accessing internal MCP SDK modules.
30+
"""
31+
32+
def __call__(
33+
self,
34+
headers: dict[str, str] | None = None,
35+
timeout: httpx.Timeout | None = None,
36+
auth: httpx.Auth | None = None,
37+
) -> httpx.AsyncClient: ...
38+
39+
2440
@dataclass
2541
class ToolFilterContext:
2642
"""Context information available to tool filter functions."""

tests/mcp/test_streamable_http_client_factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def test_httpx_client_factory_type_annotation(self):
204204

205205
# The annotation should contain the string representation of the type
206206
annotation_str = str(annotations["httpx_client_factory"])
207-
assert "McpHttpClientFactory" in annotation_str
207+
assert "HttpClientFactory" in annotation_str
208208

209209
@pytest.mark.asyncio
210210
async def test_all_parameters_with_custom_factory(self):

0 commit comments

Comments
 (0)