@@ -192,21 +192,69 @@ async def test_runner_handles_initialize_and_populates_connection(server: SrvT):
192192
193193@pytest .mark .anyio
194194async def test_runner_rejects_changed_duplicate_initialize (server : SrvT ):
195+ """A changed retry leaves negotiated state unchanged.
196+
197+ Raw requests are needed to emit the invalid second handshake.
198+ """
195199 async with connected_runner (server , initialized = False ) as (client , runner ):
196200 first_params = _initialize_params ()
197201 await client .send_raw_request ("initialize" , first_params )
198- assert runner .connection .client_params is not None
199- assert runner . connection . client_params . client_info . name == "test-client"
202+ initial_params = runner .connection .client_params
203+ assert initial_params is not None
200204
201205 second_params = _initialize_params ()
206+ second_params ["protocolVersion" ] = "2024-11-05"
207+ second_params ["capabilities" ] = {"sampling" : {}}
202208 second_params ["clientInfo" ] = {"name" : "second-client" , "version" : "1.0" }
203209 with pytest .raises (MCPError ) as exc :
204210 await client .send_raw_request ("initialize" , second_params )
205211
206- assert exc .value .error .code == INVALID_PARAMS
207- assert "already initialized" in exc .value .error .message
212+ assert exc .value .error == ErrorData (
213+ code = INVALID_PARAMS ,
214+ message = "Session already initialized with different parameters" ,
215+ data = None ,
216+ )
217+ assert runner .connection .client_params == initial_params
218+ tools = await client .send_raw_request ("tools/list" , None )
219+ assert [tool ["name" ] for tool in tools ["tools" ]] == ["t" ]
220+
221+
222+ @pytest .mark .anyio
223+ async def test_runner_accepts_identical_duplicate_initialize (server : SrvT ):
224+ """An identical retry is idempotent.
225+
226+ Raw requests are needed because the typed client cannot emit a second handshake.
227+ """
228+ async with connected_runner (server , initialized = False ) as (client , runner ):
229+ await client .send_raw_request ("initialize" , _initialize_params ())
230+ initial_params = runner .connection .client_params
231+ assert initial_params is not None
232+
233+ result = await client .send_raw_request ("initialize" , _initialize_params ())
234+
235+ assert result ["serverInfo" ]["name" ] == "test-server"
236+ assert runner .connection .client_params == initial_params
237+
238+
239+ @pytest .mark .anyio
240+ async def test_runner_duplicate_initialize_ignores_request_meta (server : SrvT ):
241+ """Request `_meta` is not negotiated state; raw requests are needed to retry with a different progress token."""
242+ async with connected_runner (server , initialized = False ) as (client , runner ):
243+ await client .send_raw_request ("initialize" , _initialize_params ())
244+ initial_params = runner .connection .client_params
245+ assert initial_params is not None
246+ initial_protocol = runner .connection .protocol_version
247+
248+ second_params = _initialize_params ()
249+ second_params ["_meta" ] = {"progressToken" : "second-token" }
250+ result = await client .send_raw_request ("initialize" , second_params )
251+
252+ assert result ["serverInfo" ]["name" ] == "test-server"
208253 assert runner .connection .client_params is not None
209- assert runner .connection .client_params .client_info .name == "test-client"
254+ assert runner .connection .client_params .model_dump (exclude = {"meta" }) == initial_params .model_dump (
255+ exclude = {"meta" }
256+ )
257+ assert runner .connection .protocol_version == initial_protocol
210258
211259
212260@pytest .mark .anyio
0 commit comments