47
47
logger = logging .getLogger ('google_adk.' + __name__ )
48
48
49
49
50
- class SseServerParams (BaseModel ):
50
+ class StdioConnectionParams (BaseModel ):
51
+ """Parameters for the MCP Stdio connection.
52
+
53
+ Attributes:
54
+ server_params: Parameters for the MCP Stdio server.
55
+ timeout: Timeout in seconds for establishing the connection to the MCP
56
+ stdio server.
57
+ """
58
+
59
+ server_params : StdioServerParameters
60
+ timeout : float = 5.0
61
+
62
+
63
+ class SseConnectionParams (BaseModel ):
51
64
"""Parameters for the MCP SSE connection.
52
65
53
66
See MCP SSE Client documentation for more details.
54
67
https://github.com/modelcontextprotocol/python-sdk/blob/main/src/mcp/client/sse.py
68
+
69
+ Attributes:
70
+ url: URL for the MCP SSE server.
71
+ headers: Headers for the MCP SSE connection.
72
+ timeout: Timeout in seconds for establishing the connection to the MCP SSE
73
+ server.
74
+ sse_read_timeout: Timeout in seconds for reading data from the MCP SSE
75
+ server.
55
76
"""
56
77
57
78
url : str
58
79
headers : dict [str , Any ] | None = None
59
- timeout : float = 5
60
- sse_read_timeout : float = 60 * 5
80
+ timeout : float = 5.0
81
+ sse_read_timeout : float = 60 * 5.0
61
82
62
83
63
- class StreamableHTTPServerParams (BaseModel ):
84
+ class StreamableHTTPConnectionParams (BaseModel ):
64
85
"""Parameters for the MCP SSE connection.
65
86
66
87
See MCP SSE Client documentation for more details.
67
88
https://github.com/modelcontextprotocol/python-sdk/blob/main/src/mcp/client/streamable_http.py
89
+
90
+ Attributes:
91
+ url: URL for the MCP Streamable HTTP server.
92
+ headers: Headers for the MCP Streamable HTTP connection.
93
+ timeout: Timeout in seconds for establishing the connection to the MCP
94
+ Streamable HTTP server.
95
+ sse_read_timeout: Timeout in seconds for reading data from the MCP
96
+ Streamable HTTP server.
97
+ terminate_on_close: Whether to terminate the MCP Streamable HTTP server
98
+ when the connection is closed.
68
99
"""
69
100
70
101
url : str
71
102
headers : dict [str , Any ] | None = None
72
- timeout : float = 5
73
- sse_read_timeout : float = 60 * 5
103
+ timeout : float = 5.0
104
+ sse_read_timeout : float = 60 * 5.0
74
105
terminate_on_close : bool = True
75
106
76
107
@@ -142,7 +173,10 @@ class MCPSessionManager:
142
173
def __init__ (
143
174
self ,
144
175
connection_params : Union [
145
- StdioServerParameters , SseServerParams , StreamableHTTPServerParams
176
+ StdioServerParameters ,
177
+ StdioConnectionParams ,
178
+ SseConnectionParams ,
179
+ StreamableHTTPConnectionParams ,
146
180
],
147
181
errlog : TextIO = sys .stderr ,
148
182
):
@@ -155,7 +189,20 @@ def __init__(
155
189
errlog: (Optional) TextIO stream for error logging. Use only for
156
190
initializing a local stdio MCP session.
157
191
"""
158
- self ._connection_params = connection_params
192
+ if isinstance (connection_params , StdioServerParameters ):
193
+ # So far timeout is not configurable. Given MCP is still evolving, we
194
+ # would expect stdio_client to evolve to accept timeout parameter like
195
+ # other client.
196
+ logger .warning (
197
+ 'StdioServerParameters is not recommended. Please use'
198
+ ' StdioConnectionParams.'
199
+ )
200
+ self ._connection_params = StdioConnectionParams (
201
+ server_params = connection_params ,
202
+ timeout = 5 ,
203
+ )
204
+ else :
205
+ self ._connection_params = connection_params
159
206
self ._errlog = errlog
160
207
# Each session manager maintains its own exit stack for proper cleanup
161
208
self ._exit_stack : Optional [AsyncExitStack ] = None
@@ -174,21 +221,19 @@ async def create_session(self) -> ClientSession:
174
221
self ._exit_stack = AsyncExitStack ()
175
222
176
223
try :
177
- if isinstance (self ._connection_params , StdioServerParameters ):
178
- # So far timeout is not configurable. Given MCP is still evolving, we
179
- # would expect stdio_client to evolve to accept timeout parameter like
180
- # other client.
224
+ if isinstance (self ._connection_params , StdioConnectionParams ):
181
225
client = stdio_client (
182
- server = self ._connection_params , errlog = self ._errlog
226
+ server = self ._connection_params .server_params ,
227
+ errlog = self ._errlog ,
183
228
)
184
- elif isinstance (self ._connection_params , SseServerParams ):
229
+ elif isinstance (self ._connection_params , SseConnectionParams ):
185
230
client = sse_client (
186
231
url = self ._connection_params .url ,
187
232
headers = self ._connection_params .headers ,
188
233
timeout = self ._connection_params .timeout ,
189
234
sse_read_timeout = self ._connection_params .sse_read_timeout ,
190
235
)
191
- elif isinstance (self ._connection_params , StreamableHTTPServerParams ):
236
+ elif isinstance (self ._connection_params , StreamableHTTPConnectionParams ):
192
237
client = streamablehttp_client (
193
238
url = self ._connection_params .url ,
194
239
headers = self ._connection_params .headers ,
@@ -208,24 +253,13 @@ async def create_session(self) -> ClientSession:
208
253
transports = await self ._exit_stack .enter_async_context (client )
209
254
# The streamable http client returns a GetSessionCallback in addition to the read/write MemoryObjectStreams
210
255
# needed to build the ClientSession, we limit then to the two first values to be compatible with all clients.
211
- # The StdioServerParameters does not provide a timeout parameter for the
212
- # session, so we need to set a default timeout for it. Other clients
213
- # (SseServerParams and StreamableHTTPServerParams) already provide a
214
- # timeout parameter in their configuration.
215
- if isinstance (self ._connection_params , StdioServerParameters ):
216
- # Default timeout for MCP session is 5 seconds, same as SseServerParams
217
- # and StreamableHTTPServerParams.
218
- # TODO :
219
- # 1. make timeout configurable
220
- # 2. Add StdioConnectionParams to include StdioServerParameters as a
221
- # field and rename other two params to XXXXConnetionParams. Ohter
222
- # two params are actually connection params, while stdio is
223
- # special, stdio_client takes the resposibility of starting the
224
- # server and working as a client.
256
+ if isinstance (self ._connection_params , StdioConnectionParams ):
225
257
session = await self ._exit_stack .enter_async_context (
226
258
ClientSession (
227
259
* transports [:2 ],
228
- read_timeout_seconds = timedelta (seconds = 5 ),
260
+ read_timeout_seconds = timedelta (
261
+ seconds = self ._connection_params .timeout
262
+ ),
229
263
)
230
264
)
231
265
else :
@@ -257,3 +291,8 @@ async def close(self):
257
291
finally :
258
292
self ._exit_stack = None
259
293
self ._session = None
294
+
295
+
296
+ SseServerParams = SseConnectionParams
297
+
298
+ StreamableHTTPServerParams = StreamableHTTPConnectionParams
0 commit comments