1818from mcp .server .connection import Connection
1919from mcp .server .context import Context
2020from mcp .server .lowlevel .server import NotificationOptions , Server
21+ from mcp .server .models import InitializationOptions
2122from mcp .server .runner import ServerRunner , otel_middleware
2223from mcp .shared .direct_dispatcher import DirectDispatcher , create_direct_dispatcher_pair
2324from mcp .shared .dispatcher import DispatchMiddleware
2425from mcp .shared .exceptions import MCPError
26+ from mcp .shared .version import SUPPORTED_PROTOCOL_VERSIONS
2527from mcp .types import (
2628 INTERNAL_ERROR ,
2729 INVALID_PARAMS ,
@@ -77,6 +79,7 @@ async def connected_runner(
7779 initialized : bool = True ,
7880 stateless : bool = False ,
7981 has_standalone_channel : bool = True ,
82+ init_options : InitializationOptions | None = None ,
8083 session_id : str | None = None ,
8184 headers : Mapping [str , str ] | None = None ,
8285 dispatch_middleware : list [DispatchMiddleware ] | None = None ,
@@ -94,6 +97,7 @@ async def connected_runner(
9497 dispatcher = server_d ,
9598 lifespan_state = {},
9699 has_standalone_channel = has_standalone_channel ,
100+ init_options = init_options ,
97101 session_id = session_id ,
98102 stateless = stateless ,
99103 dispatch_middleware = dispatch_middleware or [],
@@ -327,20 +331,31 @@ async def greet(ctx: Any, params: GreetParams) -> dict[str, Any]:
327331
328332
329333@pytest .mark .anyio
330- async def test_server_capabilities_reflects_ctor_options_in_initialize_result ():
334+ async def test_runner_initialize_result_reflects_init_options ():
331335 async def list_tools (ctx : Any , params : PaginatedRequestParams | None ) -> ListToolsResult :
332336 raise NotImplementedError
333337
334- server : SrvT = Server (
335- name = "caps-test" ,
336- on_list_tools = list_tools ,
337- notification_options = NotificationOptions (tools_changed = True ),
338- experimental_capabilities = {"ext" : {"k" : "v" }},
339- )
340- async with connected_runner (server , initialized = False ) as (client , _ ):
338+ server : SrvT = Server (name = "caps-test" , on_list_tools = list_tools , instructions = "be nice" )
339+ init_options = server .create_initialization_options (NotificationOptions (tools_changed = True ), {"ext" : {"k" : "v" }})
340+ async with connected_runner (server , initialized = False , init_options = init_options ) as (client , _ ):
341341 result = await client .send_raw_request ("initialize" , _initialize_params ())
342342 assert result ["capabilities" ]["tools" ]["listChanged" ] is True
343343 assert result ["capabilities" ]["experimental" ] == {"ext" : {"k" : "v" }}
344+ assert result ["serverInfo" ]["name" ] == "caps-test"
345+ assert result ["instructions" ] == "be nice"
346+
347+
348+ @pytest .mark .anyio
349+ async def test_runner_initialize_echoes_supported_version_and_falls_back_to_latest (server : SrvT ):
350+ oldest = SUPPORTED_PROTOCOL_VERSIONS [0 ]
351+ async with connected_runner (server , initialized = False ) as (client , _ ):
352+ params = {** _initialize_params (), "protocolVersion" : oldest }
353+ result = await client .send_raw_request ("initialize" , params )
354+ assert result ["protocolVersion" ] == oldest
355+ async with connected_runner (server , initialized = False ) as (client , _ ):
356+ params = {** _initialize_params (), "protocolVersion" : "1999-01-01" }
357+ result = await client .send_raw_request ("initialize" , params )
358+ assert result ["protocolVersion" ] == LATEST_PROTOCOL_VERSION
344359
345360
346361@pytest .mark .anyio
0 commit comments