Skip to content

Commit cc9af18

Browse files
authored
Merge branch 'main' into feature/add-enable-disable-methods-tools
2 parents 7b32590 + 10cf0f7 commit cc9af18

File tree

5 files changed

+25
-16
lines changed

5 files changed

+25
-16
lines changed

examples/clients/simple-chatbot/mcp_simple_chatbot/main.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def get_response(self, messages: list[dict[str, str]]) -> str:
245245
}
246246
payload = {
247247
"messages": messages,
248-
"model": "llama-3.2-90b-vision-preview",
248+
"model": "meta-llama/llama-4-scout-17b-16e-instruct",
249249
"temperature": 0.7,
250250
"max_tokens": 4096,
251251
"top_p": 1,
@@ -284,12 +284,9 @@ def __init__(self, servers: list[Server], llm_client: LLMClient) -> None:
284284

285285
async def cleanup_servers(self) -> None:
286286
"""Clean up all servers properly."""
287-
cleanup_tasks = [
288-
asyncio.create_task(server.cleanup()) for server in self.servers
289-
]
290-
if cleanup_tasks:
287+
for server in reversed(self.servers):
291288
try:
292-
await asyncio.gather(*cleanup_tasks, return_exceptions=True)
289+
await server.cleanup()
293290
except Exception as e:
294291
logging.warning(f"Warning during final cleanup: {e}")
295292

src/mcp/client/session_group.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ class ClientSessionGroup:
7777
the client and can be accessed via the session.
7878
7979
Example Usage:
80-
name_fn = lambda name, server_info: f"{(server_info.name)}-{name}"
80+
name_fn = lambda name, server_info: f"{(server_info.name)}_{name}"
8181
async with ClientSessionGroup(component_name_hook=name_fn) as group:
8282
for server_params in server_params:
83-
group.connect_to_server(server_param)
83+
await group.connect_to_server(server_param)
8484
...
8585
8686
"""
@@ -145,14 +145,15 @@ async def __aexit__(
145145
) -> bool | None:
146146
"""Closes session exit stacks and main exit stack upon completion."""
147147

148+
# Only close the main exit stack if we created it
149+
if self._owns_exit_stack:
150+
await self._exit_stack.aclose()
151+
148152
# Concurrently close session stacks.
149153
async with anyio.create_task_group() as tg:
150154
for exit_stack in self._session_exit_stacks.values():
151155
tg.start_soon(exit_stack.aclose)
152156

153-
# Only close the main exit stack if we created it
154-
if self._owns_exit_stack:
155-
await self._exit_stack.aclose()
156157

157158
@property
158159
def sessions(self) -> list[mcp.ClientSession]:

src/mcp/client/sse.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from httpx_sse import aconnect_sse
1111

1212
import mcp.types as types
13-
from mcp.shared._httpx_utils import create_mcp_http_client
13+
from mcp.shared._httpx_utils import McpHttpClientFactory, create_mcp_http_client
1414
from mcp.shared.message import SessionMessage
1515

1616
logger = logging.getLogger(__name__)
@@ -26,6 +26,7 @@ async def sse_client(
2626
headers: dict[str, Any] | None = None,
2727
timeout: float = 5,
2828
sse_read_timeout: float = 60 * 5,
29+
httpx_client_factory: McpHttpClientFactory = create_mcp_http_client,
2930
auth: httpx.Auth | None = None,
3031
):
3132
"""
@@ -53,7 +54,7 @@ async def sse_client(
5354
async with anyio.create_task_group() as tg:
5455
try:
5556
logger.info(f"Connecting to SSE endpoint: {remove_request_params(url)}")
56-
async with create_mcp_http_client(headers=headers, auth=auth) as client:
57+
async with httpx_client_factory(headers=headers, auth=auth) as client:
5758
async with aconnect_sse(
5859
client,
5960
"GET",

src/mcp/client/streamable_http.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
2020
from httpx_sse import EventSource, ServerSentEvent, aconnect_sse
2121

22-
from mcp.shared._httpx_utils import create_mcp_http_client
22+
from mcp.shared._httpx_utils import McpHttpClientFactory, create_mcp_http_client
2323
from mcp.shared.message import ClientMessageMetadata, SessionMessage
2424
from mcp.types import (
2525
ErrorData,
@@ -430,6 +430,7 @@ async def streamablehttp_client(
430430
timeout: timedelta = timedelta(seconds=30),
431431
sse_read_timeout: timedelta = timedelta(seconds=60 * 5),
432432
terminate_on_close: bool = True,
433+
httpx_client_factory: McpHttpClientFactory = create_mcp_http_client,
433434
auth: httpx.Auth | None = None,
434435
) -> AsyncGenerator[
435436
tuple[
@@ -464,7 +465,7 @@ async def streamablehttp_client(
464465
try:
465466
logger.info(f"Connecting to StreamableHTTP endpoint: {url}")
466467

467-
async with create_mcp_http_client(
468+
async with httpx_client_factory(
468469
headers=transport.request_headers,
469470
timeout=httpx.Timeout(
470471
transport.timeout.seconds, read=transport.sse_read_timeout.seconds

src/mcp/shared/_httpx_utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
"""Utilities for creating standardized httpx AsyncClient instances."""
22

3-
from typing import Any
3+
from typing import Any, Protocol
44

55
import httpx
66

77
__all__ = ["create_mcp_http_client"]
88

99

10+
class McpHttpClientFactory(Protocol):
11+
def __call__(
12+
self,
13+
headers: dict[str, str] | None = None,
14+
timeout: httpx.Timeout | None = None,
15+
auth: httpx.Auth | None = None,
16+
) -> httpx.AsyncClient: ...
17+
18+
1019
def create_mcp_http_client(
1120
headers: dict[str, str] | None = None,
1221
timeout: httpx.Timeout | None = None,

0 commit comments

Comments
 (0)