Skip to content

Commit 7d5ad06

Browse files
authored
Merge branch 'main' into proxy_tool
2 parents 80e6013 + 163ea02 commit 7d5ad06

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

src/fastmcp/client/client.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from mcp import ClientSession
1010
from pydantic import AnyUrl
1111

12+
import fastmcp
1213
from fastmcp.client.logging import (
1314
LogHandler,
1415
MessageHandler,
@@ -45,8 +46,8 @@ class Client:
4546
MCP client that delegates connection management to a Transport instance.
4647
4748
The Client class is responsible for MCP protocol logic, while the Transport
48-
handles connection establishment and management. Client provides methods
49-
for working with resources, prompts, tools and other MCP capabilities.
49+
handles connection establishment and management. Client provides methods for
50+
working with resources, prompts, tools and other MCP capabilities.
5051
5152
Args:
5253
transport: Connection source specification, which can be:
@@ -62,18 +63,18 @@ class Client:
6263
message_handler: Optional handler for protocol messages
6364
progress_handler: Optional handler for progress notifications
6465
timeout: Optional timeout for requests (seconds or timedelta)
66+
init_timeout: Optional timeout for initial connection (seconds or timedelta).
67+
Set to 0 to disable. If None, uses the value in the FastMCP global settings.
6568
6669
Examples:
67-
```python
68-
# Connect to FastMCP server
69-
client = Client("http://localhost:8080")
70+
```python # Connect to FastMCP server client =
71+
Client("http://localhost:8080")
7072
7173
async with client:
72-
# List available resources
73-
resources = await client.list_resources()
74+
# List available resources resources = await client.list_resources()
7475
75-
# Call a tool
76-
result = await client.call_tool("my_tool", {"param": "value"})
76+
# Call a tool result = await client.call_tool("my_tool", {"param":
77+
"value"})
7778
```
7879
"""
7980

@@ -93,6 +94,7 @@ def __init__(
9394
message_handler: MessageHandler | None = None,
9495
progress_handler: ProgressHandler | None = None,
9596
timeout: datetime.timedelta | float | int | None = None,
97+
init_timeout: datetime.timedelta | float | int | None = None,
9698
):
9799
self.transport = infer_transport(transport)
98100
self._session: ClientSession | None = None
@@ -111,6 +113,17 @@ def __init__(
111113
if isinstance(timeout, int | float):
112114
timeout = datetime.timedelta(seconds=timeout)
113115

116+
# handle init handshake timeout
117+
if init_timeout is None:
118+
init_timeout = fastmcp.settings.settings.client_init_timeout
119+
if isinstance(init_timeout, datetime.timedelta):
120+
init_timeout = init_timeout.total_seconds()
121+
elif not init_timeout:
122+
init_timeout = None
123+
else:
124+
init_timeout = float(init_timeout)
125+
self._init_timeout = init_timeout
126+
114127
self._session_kwargs: SessionKwargs = {
115128
"sampling_callback": None,
116129
"list_roots_callback": None,
@@ -168,7 +181,7 @@ async def _context_manager(self):
168181
self._session = session
169182
# Initialize the session
170183
try:
171-
with anyio.fail_after(1):
184+
with anyio.fail_after(self._init_timeout):
172185
self._initialize_result = await self._session.initialize()
173186
yield
174187
except TimeoutError:

src/fastmcp/settings.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ class Settings(BaseSettings):
8787
),
8888
] = False
8989

90+
client_init_timeout: Annotated[
91+
float | None,
92+
Field(
93+
description="The timeout for the client's initialization handshake, in seconds. Set to None or 0 to disable.",
94+
),
95+
] = None
96+
9097
@model_validator(mode="after")
9198
def setup_logging(self) -> Self:
9299
"""Finalize the settings."""

0 commit comments

Comments
 (0)