@@ -11,21 +11,28 @@ public class McpProtocolService(
1111 McpResourceService m_resourceService ,
1212 ILogger < McpProtocolService > m_logger )
1313 {
14- public async Task < object > ProcessRequest ( JsonElement requestElement )
14+ public async Task < object ? > ProcessRequest ( JsonElement requestElement )
1515 {
16- int requestId = 0 ;
16+ object ? requestId = null ;
1717 try
1818 {
1919 var request = ParseRequest ( requestElement ) ;
2020 if ( request == null )
2121 {
22- return CreateErrorResponse ( 0 , - 32600 , "Invalid Request - malformed JSON-RPC" ) ;
22+ return CreateErrorResponse ( null , - 32600 , "Invalid Request - malformed JSON-RPC" ) ;
2323 }
2424
2525 requestId = request . Id ;
2626 m_logger . LogDebug ( "Processing MCP request: {Method}" , request . Method ) ;
2727
2828 var result = await ExecuteMethod ( request ) ;
29+
30+ // Handle notifications - they should not return responses
31+ if ( result == null )
32+ {
33+ return null ; // No response for notifications
34+ }
35+
2936 return CreateSuccessResponse ( request . Id , result ) ;
3037 }
3138 catch ( McpToolException ex )
@@ -51,8 +58,13 @@ public async Task<object> ProcessRequest(JsonElement requestElement)
5158 {
5259 Method = methodProperty . GetString ( ) ?? string . Empty ,
5360 Id = requestElement . TryGetProperty ( "id" , out var idProperty )
54- ? ( idProperty . ValueKind == JsonValueKind . Number ? idProperty . GetInt32 ( ) : 0 )
55- : 0
61+ ? idProperty . ValueKind switch
62+ {
63+ JsonValueKind . Number => idProperty . GetInt32 ( ) ,
64+ JsonValueKind . String => idProperty . GetString ( ) ,
65+ _ => null
66+ }
67+ : null
5668 } ;
5769
5870 if ( requestElement . TryGetProperty ( "params" , out var paramsProperty ) )
@@ -68,7 +80,7 @@ public async Task<object> ProcessRequest(JsonElement requestElement)
6880 }
6981 }
7082
71- private async Task < object > ExecuteMethod ( McpRequest request )
83+ private async Task < object ? > ExecuteMethod ( McpRequest request )
7284 {
7385 return request . Method switch
7486 {
@@ -89,14 +101,14 @@ private object HandleInitialize()
89101 return new McpInitializeResult ( ) ;
90102 }
91103
92- private object HandleNotificationInitialized ( )
104+ private object ? HandleNotificationInitialized ( )
93105 {
94106 m_logger . LogDebug ( "Received MCP initialization notification" ) ;
95107 // Notifications should not return responses according to JSON-RPC spec
96- return new { } ; // TODO: Consider if this should return null for true notification handling
108+ return null ; // Return null to indicate no response should be sent
97109 }
98110
99- private object HandleNotificationCancelled ( JsonElement ? paramsElement )
111+ private object ? HandleNotificationCancelled ( JsonElement ? paramsElement )
100112 {
101113 if ( paramsElement != null && paramsElement . Value . TryGetProperty ( "requestId" , out var requestIdProp ) )
102114 {
@@ -118,8 +130,8 @@ private object HandleNotificationCancelled(JsonElement? paramsElement)
118130 m_logger . LogDebug ( "Received cancellation notification without request ID" ) ;
119131 }
120132
121- // Return empty success response for notifications
122- return new { } ;
133+ // Notifications should not return responses according to JSON-RPC spec
134+ return null ; // Return null to indicate no response should be sent
123135 }
124136
125137 private object HandleToolsList ( )
@@ -177,7 +189,7 @@ private async Task<object> HandleToolsCall(JsonElement? paramsElement)
177189 return await m_toolExecutionService . ExecuteTool ( toolName , arguments ) ;
178190 }
179191
180- private static McpSuccessResponse CreateSuccessResponse ( int id , object result )
192+ private static McpSuccessResponse CreateSuccessResponse ( object ? id , object result )
181193 {
182194 return new McpSuccessResponse
183195 {
@@ -186,7 +198,7 @@ private static McpSuccessResponse CreateSuccessResponse(int id, object result)
186198 } ;
187199 }
188200
189- private static McpErrorResponse CreateErrorResponse ( int id , int code , string message , object ? data = null )
201+ private static McpErrorResponse CreateErrorResponse ( object ? id , int code , string message , object ? data = null )
190202 {
191203 return new McpErrorResponse
192204 {
0 commit comments