@@ -122,7 +122,7 @@ public async Task RunAsync(CancellationToken cancellationToken)
122122 switch ( method )
123123 {
124124 case "initialize" :
125- HandleInitialize ( id ) ;
125+ HandleInitialize ( id , root ) ;
126126 break ;
127127
128128 case "notifications/initialized" :
@@ -167,22 +167,27 @@ public async Task RunAsync(CancellationToken cancellationToken)
167167 /// <param name="id">
168168 /// The request identifier extracted from the incoming JSON-RPC request. Used to correlate the response with the request.
169169 /// </param>
170+ /// <param name="root">The incoming initialize request payload.</param>
170171 /// <remarks>
171- /// This method constructs and writes the MCP "initialize" response to STDOUT. It uses the protocol version defined by <c>PROTOCOL_VERSION</c>
172- /// and includes supported capabilities and server information. No notifications are sent here; the server waits for the client to send
173- /// "notifications/initialized" before sending any notifications.
172+ /// This method constructs and writes the MCP "initialize" response to STDOUT. It negotiates the response protocol version from the
173+ /// server-supported version and client-requested version, and includes supported capabilities and server information. No notifications
174+ /// are sent here; the server waits for the client to send "notifications/initialized" before sending any notifications.
174175 /// </remarks>
175- private void HandleInitialize ( JsonElement ? id )
176+ private void HandleInitialize ( JsonElement ? id , JsonElement root )
176177 {
178+ string ? clientRequestedProtocolVersion = GetClientProtocolVersion ( root ) ;
179+ string negotiatedProtocolVersion =
180+ McpProtocolDefaults . ResolveInitializeResponseProtocolVersion ( _protocolVersion , clientRequestedProtocolVersion ) ;
181+
177182 // Get the description from runtime config if available
178- string ? instructions = null ;
183+ string ? description = null ;
179184 RuntimeConfigProvider ? runtimeConfigProvider = _serviceProvider . GetService < RuntimeConfigProvider > ( ) ;
180185 if ( runtimeConfigProvider != null )
181186 {
182187 try
183188 {
184189 RuntimeConfig runtimeConfig = runtimeConfigProvider . GetConfig ( ) ;
185- instructions = runtimeConfig . Runtime ? . Mcp ? . Description ;
190+ description = runtimeConfig . Runtime ? . Mcp ? . Description ;
186191 }
187192 catch ( Exception )
188193 {
@@ -191,13 +196,33 @@ private void HandleInitialize(JsonElement? id)
191196 }
192197 }
193198
194- // Create the initialize response - only include instructions if non-empty
199+ bool shouldUseServerInfoDescription = McpProtocolDefaults . ShouldUseServerInfoDescription ( negotiatedProtocolVersion ) ;
200+
201+ // Create the initialize response - only include description/instructions if non-empty
195202 object result ;
196- if ( ! string . IsNullOrWhiteSpace ( instructions ) )
203+ if ( ! string . IsNullOrWhiteSpace ( description ) && shouldUseServerInfoDescription )
197204 {
198205 result = new
199206 {
200- protocolVersion = _protocolVersion ,
207+ protocolVersion = negotiatedProtocolVersion ,
208+ capabilities = new
209+ {
210+ tools = new { listChanged = true } ,
211+ logging = new { }
212+ } ,
213+ serverInfo = new
214+ {
215+ name = McpProtocolDefaults . MCP_SERVER_NAME ,
216+ version = McpProtocolDefaults . MCP_SERVER_VERSION ,
217+ description = description
218+ }
219+ } ;
220+ }
221+ else if ( ! string . IsNullOrWhiteSpace ( description ) )
222+ {
223+ result = new
224+ {
225+ protocolVersion = negotiatedProtocolVersion ,
201226 capabilities = new
202227 {
203228 tools = new { listChanged = true } ,
@@ -208,14 +233,14 @@ private void HandleInitialize(JsonElement? id)
208233 name = McpProtocolDefaults . MCP_SERVER_NAME ,
209234 version = McpProtocolDefaults . MCP_SERVER_VERSION
210235 } ,
211- instructions = instructions
236+ instructions = description
212237 } ;
213238 }
214239 else
215240 {
216241 result = new
217242 {
218- protocolVersion = _protocolVersion ,
243+ protocolVersion = negotiatedProtocolVersion ,
219244 capabilities = new
220245 {
221246 tools = new { listChanged = true } ,
@@ -232,6 +257,22 @@ private void HandleInitialize(JsonElement? id)
232257 WriteResult ( id , result ) ;
233258 }
234259
260+ private static string ? GetClientProtocolVersion ( JsonElement root )
261+ {
262+ if ( ! root . TryGetProperty ( "params" , out JsonElement paramsElement ) || paramsElement . ValueKind != JsonValueKind . Object )
263+ {
264+ return null ;
265+ }
266+
267+ if ( ! paramsElement . TryGetProperty ( "protocolVersion" , out JsonElement protocolVersionElement ) ||
268+ protocolVersionElement . ValueKind != JsonValueKind . String )
269+ {
270+ return null ;
271+ }
272+
273+ return protocolVersionElement . GetString ( ) ;
274+ }
275+
235276 /// <summary>
236277 /// Handles the "tools/list" JSON-RPC method by sending the list of available tools to the client.
237278 /// </summary>
0 commit comments