Skip to content

Commit c6de1b2

Browse files
authored
Update server.py
1 parent fff3c11 commit c6de1b2

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/agents/mcp/server.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async def call_tool(self, tool_name: str, arguments: dict[str, Any] | None) -> C
5555
class _MCPServerWithClientSession(MCPServer, abc.ABC):
5656
"""Base class for MCP servers that use a `ClientSession` to communicate with the server."""
5757

58-
def __init__(self, cache_tools_list: bool):
58+
def __init__(self, cache_tools_list: bool, client_session_timeout_seconds: float | None):
5959
"""
6060
Args:
6161
cache_tools_list: Whether to cache the tools list. If `True`, the tools list will be
@@ -70,8 +70,7 @@ def __init__(self, cache_tools_list: bool):
7070
self._cleanup_lock: asyncio.Lock = asyncio.Lock()
7171
self.cache_tools_list = cache_tools_list
7272

73-
self.timeout: float = 5
74-
"""The timeout for the MCP ClientSession. Defaults to 5 seconds."""
73+
self.client_session_timeout_seconds = client_session_timeout_seconds
7574

7675
# The cache is always dirty at startup, so that we fetch tools at least once
7776
self._cache_dirty = True
@@ -107,7 +106,9 @@ async def connect(self):
107106
read, write = transport
108107
session = await self.exit_stack.enter_async_context(
109108
ClientSession(
110-
read, write, read_timeout_seconds=timedelta(seconds=self.timeout)
109+
read,
110+
write,
111+
read_timeout_seconds=timedelta(seconds=self.client_session_timeout_seconds),
111112
)
112113
)
113114
await session.initialize()
@@ -191,6 +192,7 @@ def __init__(
191192
params: MCPServerStdioParams,
192193
cache_tools_list: bool = False,
193194
name: str | None = None,
195+
client_session_timeout_seconds: float | None = 5,
194196
):
195197
"""Create a new MCP server based on the stdio transport.
196198
@@ -208,7 +210,7 @@ def __init__(
208210
name: A readable name for the server. If not provided, we'll create one from the
209211
command.
210212
"""
211-
super().__init__(cache_tools_list)
213+
super().__init__(cache_tools_list, client_session_timeout_seconds)
212214

213215
self.params = StdioServerParameters(
214216
command=params["command"],
@@ -265,6 +267,7 @@ def __init__(
265267
params: MCPServerSseParams,
266268
cache_tools_list: bool = False,
267269
name: str | None = None,
270+
client_session_timeout_seconds: float | None = 5,
268271
):
269272
"""Create a new MCP server based on the HTTP with SSE transport.
270273
@@ -283,7 +286,7 @@ def __init__(
283286
name: A readable name for the server. If not provided, we'll create one from the
284287
URL.
285288
"""
286-
super().__init__(cache_tools_list)
289+
super().__init__(cache_tools_list, client_session_timeout_seconds)
287290

288291
self.params = params
289292
self._name = name or f"sse: {self.params['url']}"

0 commit comments

Comments
 (0)