@@ -154,6 +154,90 @@ async def message_handler(message: IncomingMessage) -> None: # pragma: no cover
154154 assert isinstance (initialized_notification , InitializedNotification )
155155
156156
157+ @pytest .mark .anyio
158+ async def test_client_session_initialize_custom_protocol_version ():
159+ client_to_server_send , client_to_server_receive = anyio .create_memory_object_stream [SessionMessage ](1 )
160+ server_to_client_send , server_to_client_receive = anyio .create_memory_object_stream [SessionMessage ](1 )
161+
162+ initialized_notification = None
163+ result = None
164+
165+ async def mock_server ():
166+ nonlocal initialized_notification
167+
168+ session_message = await client_to_server_receive .receive ()
169+ jsonrpc_request = session_message .message
170+ assert isinstance (jsonrpc_request , JSONRPCRequest )
171+ request = client_request_adapter .validate_python (
172+ jsonrpc_request .model_dump (by_alias = True , mode = "json" , exclude_none = True )
173+ )
174+ assert isinstance (request , InitializeRequest )
175+ assert request .params .protocol_version == "2024-11-05"
176+
177+ result = InitializeResult (
178+ protocol_version = "2024-11-05" ,
179+ capabilities = ServerCapabilities (
180+ logging = None ,
181+ resources = None ,
182+ tools = None ,
183+ experimental = None ,
184+ prompts = None ,
185+ ),
186+ server_info = Implementation (name = "mock-server" , version = "0.1.0" ),
187+ instructions = "The server instructions." ,
188+ )
189+
190+ async with server_to_client_send :
191+ await server_to_client_send .send (
192+ SessionMessage (
193+ JSONRPCResponse (
194+ jsonrpc = "2.0" ,
195+ id = jsonrpc_request .id ,
196+ result = result .model_dump (by_alias = True , mode = "json" , exclude_none = True ),
197+ )
198+ )
199+ )
200+ session_notification = await client_to_server_receive .receive ()
201+ jsonrpc_notification = session_notification .message
202+ assert isinstance (jsonrpc_notification , JSONRPCNotification )
203+ initialized_notification = client_notification_adapter .validate_python (
204+ jsonrpc_notification .model_dump (by_alias = True , mode = "json" , exclude_none = True )
205+ )
206+
207+ # Create a message handler to catch exceptions
208+ async def message_handler ( # pragma: no cover
209+ message : RequestResponder [types .ServerRequest , types .ClientResult ] | types .ServerNotification | Exception ,
210+ ) -> None :
211+ if isinstance (message , Exception ):
212+ raise message
213+
214+ async with (
215+ ClientSession (
216+ server_to_client_receive ,
217+ client_to_server_send ,
218+ message_handler = message_handler ,
219+ ) as session ,
220+ anyio .create_task_group () as tg ,
221+ client_to_server_send ,
222+ client_to_server_receive ,
223+ server_to_client_send ,
224+ server_to_client_receive ,
225+ ):
226+ tg .start_soon (mock_server )
227+ result = await session .initialize (protocol_version = "2024-11-05" )
228+
229+ # Assert the result
230+ assert isinstance (result , InitializeResult )
231+ assert result .protocol_version == "2024-11-05"
232+ assert isinstance (result .capabilities , ServerCapabilities )
233+ assert result .server_info == Implementation (name = "mock-server" , version = "0.1.0" )
234+ assert result .instructions == "The server instructions."
235+
236+ # Check that the client sent the initialized notification
237+ assert initialized_notification
238+ assert isinstance (initialized_notification , InitializedNotification )
239+
240+
157241@pytest .mark .anyio
158242async def test_client_session_custom_client_info ():
159243 client_to_server_send , client_to_server_receive = anyio .create_memory_object_stream [SessionMessage ](1 )
0 commit comments