@@ -44,24 +44,29 @@ class StdioConnection(TypedDict):
44
44
args : list [str ]
45
45
"""Command line arguments to pass to the executable."""
46
46
47
- env : dict [str , str ] | None
47
+ env : NotRequired [ dict [str , str ] | None ]
48
48
"""The environment to use when spawning the process."""
49
49
50
- cwd : str | Path | None
50
+ cwd : NotRequired [ str | Path | None ]
51
51
"""The working directory to use when spawning the process."""
52
52
53
- encoding : str
54
- """The text encoding used when sending/receiving messages to the server."""
53
+ encoding : NotRequired [ str ]
54
+ """The text encoding used when sending/receiving messages to the server.
55
55
56
- encoding_error_handler : EncodingErrorHandler
56
+ Default is 'utf-8'.
57
+ """
58
+
59
+ encoding_error_handler : NotRequired [EncodingErrorHandler ]
57
60
"""
58
61
The text encoding error handler.
59
62
60
63
See https://docs.python.org/3/library/codecs.html#codec-base-classes for
61
64
explanations of possible values.
65
+
66
+ Default is 'strict', which raises an error on encoding/decoding errors.
62
67
"""
63
68
64
- session_kwargs : dict [str , Any ] | None
69
+ session_kwargs : NotRequired [ dict [str , Any ] | None ]
65
70
"""Additional keyword arguments to pass to the ClientSession."""
66
71
67
72
@@ -71,19 +76,27 @@ class SSEConnection(TypedDict):
71
76
url : str
72
77
"""The URL of the SSE endpoint to connect to."""
73
78
74
- headers : dict [str , Any ] | None
79
+ headers : NotRequired [ dict [str , Any ] | None ]
75
80
"""HTTP headers to send to the SSE endpoint."""
76
81
77
- timeout : float
78
- """HTTP timeout."""
82
+ timeout : NotRequired [float ]
83
+ """HTTP timeout.
84
+
85
+ Default is 5 seconds. If the server takes longer to respond,
86
+ you can increase this value.
87
+ """
88
+
89
+ sse_read_timeout : NotRequired [float ]
90
+ """SSE read timeout.
79
91
80
- sse_read_timeout : float
81
- """SSE read timeout."""
92
+ Default is 300 seconds (5 minutes). This is how long the client will
93
+ wait for a new event before disconnecting.
94
+ """
82
95
83
- session_kwargs : dict [str , Any ] | None
96
+ session_kwargs : NotRequired [ dict [str , Any ] | None ]
84
97
"""Additional keyword arguments to pass to the ClientSession."""
85
98
86
- httpx_client_factory : McpHttpClientFactory | None
99
+ httpx_client_factory : NotRequired [ McpHttpClientFactory | None ]
87
100
"""Custom factory for httpx.AsyncClient (optional)."""
88
101
89
102
auth : NotRequired [httpx .Auth ]
@@ -92,27 +105,28 @@ class SSEConnection(TypedDict):
92
105
93
106
class StreamableHttpConnection (TypedDict ):
94
107
transport : Literal ["streamable_http" ]
108
+ """Connection configuration for Streamable HTTP transport."""
95
109
96
110
url : str
97
111
"""The URL of the endpoint to connect to."""
98
112
99
- headers : dict [str , Any ] | None
113
+ headers : NotRequired [ dict [str , Any ] | None ]
100
114
"""HTTP headers to send to the endpoint."""
101
115
102
- timeout : timedelta
116
+ timeout : NotRequired [ timedelta ]
103
117
"""HTTP timeout."""
104
118
105
- sse_read_timeout : timedelta
119
+ sse_read_timeout : NotRequired [ timedelta ]
106
120
"""How long (in seconds) the client will wait for a new event before disconnecting.
107
121
All other HTTP operations are controlled by `timeout`."""
108
122
109
- terminate_on_close : bool
123
+ terminate_on_close : NotRequired [ bool ]
110
124
"""Whether to terminate the session on close."""
111
125
112
- session_kwargs : dict [str , Any ] | None
126
+ session_kwargs : NotRequired [ dict [str , Any ] | None ]
113
127
"""Additional keyword arguments to pass to the ClientSession."""
114
128
115
- httpx_client_factory : McpHttpClientFactory | None
129
+ httpx_client_factory : NotRequired [ McpHttpClientFactory | None ]
116
130
"""Custom factory for httpx.AsyncClient (optional)."""
117
131
118
132
auth : NotRequired [httpx .Auth ]
@@ -125,7 +139,7 @@ class WebsocketConnection(TypedDict):
125
139
url : str
126
140
"""The URL of the Websocket endpoint to connect to."""
127
141
128
- session_kwargs : dict [str , Any ] | None
142
+ session_kwargs : NotRequired [ dict [str , Any ] | None ]
129
143
"""Additional keyword arguments to pass to the ClientSession"""
130
144
131
145
@@ -291,7 +305,6 @@ async def create_session(connection: Connection) -> AsyncIterator[ClientSession]
291
305
292
306
Yields:
293
307
A ClientSession
294
-
295
308
"""
296
309
if "transport" not in connection :
297
310
raise ValueError (
@@ -302,57 +315,29 @@ async def create_session(connection: Connection) -> AsyncIterator[ClientSession]
302
315
)
303
316
304
317
transport = connection ["transport" ]
318
+ params = {k : v for k , v in connection .items () if k != "transport" }
319
+
305
320
if transport == "sse" :
306
- if "url" not in connection :
321
+ if "url" not in params :
307
322
raise ValueError ("'url' parameter is required for SSE connection" )
308
- async with _create_sse_session (
309
- url = connection ["url" ],
310
- headers = connection .get ("headers" ),
311
- timeout = connection .get ("timeout" , DEFAULT_HTTP_TIMEOUT ),
312
- sse_read_timeout = connection .get ("sse_read_timeout" , DEFAULT_SSE_READ_TIMEOUT ),
313
- session_kwargs = connection .get ("session_kwargs" ),
314
- httpx_client_factory = connection .get ("httpx_client_factory" ),
315
- auth = connection .get ("auth" ),
316
- ) as session :
323
+ async with _create_sse_session (** params ) as session :
317
324
yield session
318
325
elif transport == "streamable_http" :
319
- if "url" not in connection :
326
+ if "url" not in params :
320
327
raise ValueError ("'url' parameter is required for Streamable HTTP connection" )
321
- async with _create_streamable_http_session (
322
- url = connection ["url" ],
323
- headers = connection .get ("headers" ),
324
- timeout = connection .get ("timeout" , DEFAULT_STREAMABLE_HTTP_TIMEOUT ),
325
- sse_read_timeout = connection .get (
326
- "sse_read_timeout" , DEFAULT_STREAMABLE_HTTP_SSE_READ_TIMEOUT
327
- ),
328
- session_kwargs = connection .get ("session_kwargs" ),
329
- httpx_client_factory = connection .get ("httpx_client_factory" ),
330
- auth = connection .get ("auth" ),
331
- ) as session :
328
+ async with _create_streamable_http_session (** params ) as session :
332
329
yield session
333
330
elif transport == "stdio" :
334
- if "command" not in connection :
331
+ if "command" not in params :
335
332
raise ValueError ("'command' parameter is required for stdio connection" )
336
- if "args" not in connection :
333
+ if "args" not in params :
337
334
raise ValueError ("'args' parameter is required for stdio connection" )
338
- async with _create_stdio_session (
339
- command = connection ["command" ],
340
- args = connection ["args" ],
341
- env = connection .get ("env" ),
342
- cwd = connection .get ("cwd" ),
343
- encoding = connection .get ("encoding" , DEFAULT_ENCODING ),
344
- encoding_error_handler = connection .get (
345
- "encoding_error_handler" , DEFAULT_ENCODING_ERROR_HANDLER
346
- ),
347
- session_kwargs = connection .get ("session_kwargs" ),
348
- ) as session :
335
+ async with _create_stdio_session (** params ) as session :
349
336
yield session
350
337
elif transport == "websocket" :
351
- if "url" not in connection :
338
+ if "url" not in params :
352
339
raise ValueError ("'url' parameter is required for Websocket connection" )
353
- async with _create_websocket_session (
354
- url = connection ["url" ], session_kwargs = connection .get ("session_kwargs" )
355
- ) as session :
340
+ async with _create_websocket_session (** params ) as session :
356
341
yield session
357
342
else :
358
343
raise ValueError (
0 commit comments