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