Skip to content

Commit 75b443b

Browse files
Compliance fixes
1 parent 0961b1d commit 75b443b

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

mcp_nexus/Controllers/McpController.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ public async Task<IActionResult> HandleMcpRequest()
7070
}
7171

7272
var response = await mcpProtocolService.ProcessRequest(requestElement);
73+
74+
// Handle notifications - they should not return responses
75+
if (response == null)
76+
{
77+
logger.LogInformation("Notification received for method '{Method}' - no response sent (Session: {SessionId})", method, sessionId);
78+
return NoContent(); // HTTP 204 No Content for notifications
79+
}
80+
7381
logger.LogInformation("ProcessRequest completed for method '{Method}' - Response type: {ResponseType}", method, response.GetType().Name);
7482

7583
// PERFORMANCE: Only serialize response in debug mode to avoid unnecessary allocations

mcp_nexus/Models/McpModels.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ public class McpRequest
2525
[JsonPropertyName("params")]
2626
public JsonElement? Params { get; set; }
2727
[JsonPropertyName("id")]
28-
public int Id { get; set; }
28+
public object? Id { get; set; }
2929
}
3030

3131
public class McpResponse
3232
{
3333
[JsonPropertyName("jsonrpc")]
3434
public string JsonRpc { get; set; } = "2.0";
3535
[JsonPropertyName("id")]
36-
public int Id { get; set; }
36+
public object? Id { get; set; }
3737
[JsonPropertyName("result")]
3838
public object? Result { get; set; }
3939
[JsonPropertyName("error")]
@@ -45,7 +45,7 @@ public class McpSuccessResponse
4545
[JsonPropertyName("jsonrpc")]
4646
public string JsonRpc { get; set; } = "2.0";
4747
[JsonPropertyName("id")]
48-
public int Id { get; set; }
48+
public object? Id { get; set; }
4949
[JsonPropertyName("result")]
5050
public object? Result { get; set; }
5151
}
@@ -55,7 +55,7 @@ public class McpErrorResponse
5555
[JsonPropertyName("jsonrpc")]
5656
public string JsonRpc { get; set; } = "2.0";
5757
[JsonPropertyName("id")]
58-
public int Id { get; set; }
58+
public object? Id { get; set; }
5959
[JsonPropertyName("error")]
6060
public McpError Error { get; set; } = new();
6161
}

mcp_nexus/Protocol/McpProtocolService.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)