Skip to content

Commit 186cb93

Browse files
Copilotstephentoub
andauthored
Update McpErrorCode XML docs and fix error code usage to align with MCP spec (#1291)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 5651e9e commit 186cb93

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
lines changed

docs/concepts/tasks/tasks.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,8 @@ Task operations may throw <xref:ModelContextProtocol.McpException> with these er
424424

425425
| Error Code | Scenario |
426426
|------------|----------|
427-
| `InvalidParams` | Invalid or nonexistent task ID |
428-
| `InvalidRequest` | Tool with `taskSupport: forbidden` called with task metadata, or tool with `taskSupport: required` called without task metadata |
427+
| `InvalidParams` | Invalid or nonexistent task ID or invalid cursor |
428+
| `InvalidParams` | Tool with `taskSupport: forbidden` called with task metadata, or tool with `taskSupport: required` called without task metadata |
429429
| `InternalError` | Task execution failure or result unavailable |
430430

431431
Example error handling:

src/ModelContextProtocol.Core/McpErrorCode.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,19 @@ public enum McpErrorCode
3939
InvalidRequest = -32600,
4040

4141
/// <summary>
42-
/// Indicates that the requested method does not exist or is not available on the server.
42+
/// Indicates that the requested method does not exist or is not available.
4343
/// </summary>
4444
/// <remarks>
45-
/// This error is returned when the method name specified in the request cannot be found.
45+
/// <para>
46+
/// In MCP, this error is returned when a request is made for a method that requires a capability
47+
/// that has not been declared. This can occur in either direction:
48+
/// </para>
49+
/// <list type="bullet">
50+
/// <item><description>A server returning this error when the client requests a capability it doesn't support
51+
/// (for example, requesting completions when the <c>completions</c> capability was not advertised).</description></item>
52+
/// <item><description>A client returning this error when the server requests a capability it doesn't support
53+
/// (for example, requesting roots when the client did not declare the <c>roots</c> capability).</description></item>
54+
/// </list>
4655
/// </remarks>
4756
MethodNotFound = -32601,
4857

@@ -51,15 +60,19 @@ public enum McpErrorCode
5160
/// </summary>
5261
/// <remarks>
5362
/// <para>
54-
/// This error is returned for protocol-level parameter issues, such as:
63+
/// In MCP, this error is returned for protocol-level parameter validation failures in various contexts:
5564
/// </para>
5665
/// <list type="bullet">
57-
/// <item><description>Malformed requests that fail to satisfy the request schema (for example, CallToolRequest)</description></item>
58-
/// <item><description>Unknown or unrecognized primitive names (for example, tool, prompt, or resource names)</description></item>
59-
/// <item><description>Missing required protocol-level parameters</description></item>
66+
/// <item><description><b>Tools</b>: Unknown tool name or invalid protocol-level tool arguments.</description></item>
67+
/// <item><description><b>Prompts</b>: Unknown prompt name or missing required protocol-level arguments.</description></item>
68+
/// <item><description><b>Pagination</b>: Invalid or expired cursor values.</description></item>
69+
/// <item><description><b>Logging</b>: Invalid log level.</description></item>
70+
/// <item><description><b>Tasks</b>: Invalid or nonexistent task ID or invalid cursor.</description></item>
71+
/// <item><description><b>Elicitation</b>: Server requests an elicitation mode not declared in client capabilities.</description></item>
72+
/// <item><description><b>Sampling</b>: Missing tool result or tool results mixed with other content.</description></item>
6073
/// </list>
6174
/// <para>
62-
/// Note: Input validation errors within tool/prompt/resource arguments should be reported as execution errors
75+
/// Note: Application-layer validation errors within tool/prompt/resource arguments should be reported as execution errors
6376
/// (for example, via <see cref="Protocol.CallToolResult.IsError"/>) rather than as protocol errors, allowing language
6477
/// models to receive error feedback and self-correct.
6578
/// </para>

src/ModelContextProtocol.Core/Server/McpServerImpl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ await originalListToolsHandler(request, cancellationToken).ConfigureAwait(false)
591591
{
592592
throw new McpProtocolException(
593593
$"Tool '{tool.ProtocolTool.Name}' does not support task-augmented execution.",
594-
McpErrorCode.MethodNotFound);
594+
McpErrorCode.InvalidParams);
595595
}
596596

597597
// Task augmentation requested - return CreateTaskResult
@@ -604,7 +604,7 @@ await originalListToolsHandler(request, cancellationToken).ConfigureAwait(false)
604604
throw new McpProtocolException(
605605
$"Tool '{tool.ProtocolTool.Name}' requires task-augmented execution. " +
606606
"Include a 'task' parameter with the request.",
607-
McpErrorCode.MethodNotFound);
607+
McpErrorCode.InvalidParams);
608608
}
609609

610610
// Normal synchronous execution

tests/ModelContextProtocol.Tests/Server/McpServerTaskAugmentedValidationTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ await client.CallToolAsync(
100100
TestContext.Current.CancellationToken));
101101

102102
Assert.Contains("does not support task-augmented execution", exception.Message, StringComparison.OrdinalIgnoreCase);
103-
Assert.Equal(McpErrorCode.MethodNotFound, exception.ErrorCode);
103+
Assert.Equal(McpErrorCode.InvalidParams, exception.ErrorCode);
104104
}
105105

106106
[Fact]
@@ -227,7 +227,7 @@ await client.CallToolAsync(
227227
TestContext.Current.CancellationToken));
228228

229229
Assert.Contains("requires task-augmented execution", exception.Message, StringComparison.OrdinalIgnoreCase);
230-
Assert.Equal(McpErrorCode.MethodNotFound, exception.ErrorCode);
230+
Assert.Equal(McpErrorCode.InvalidParams, exception.ErrorCode);
231231
}
232232

233233
[Fact]

tests/ModelContextProtocol.Tests/Server/TaskCancellationIntegrationTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ public async Task CompletedTask_CannotTransitionToOtherStatus()
460460

461461
Assert.Equal(McpTaskStatus.Completed, taskStatus.Status);
462462

463-
// Act - Try to cancel a completed task
463+
// Act - Try to cancel a completed task (should be idempotent)
464464
var cancelResult = await client.CancelTaskAsync(taskId, cancellationToken: TestContext.Current.CancellationToken);
465465

466466
// Assert - Status should still be completed (not cancelled)
@@ -500,7 +500,7 @@ public async Task FailedTask_CannotTransitionToOtherStatus()
500500

501501
Assert.Equal(McpTaskStatus.Failed, taskStatus.Status);
502502

503-
// Act - Try to cancel a failed task
503+
// Act - Try to cancel a failed task (should be idempotent)
504504
var cancelResult = await client.CancelTaskAsync(taskId, cancellationToken: TestContext.Current.CancellationToken);
505505

506506
// Assert - Status should still be failed

tests/ModelContextProtocol.Tests/Server/ToolTaskSupportTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,8 @@ public async Task SyncTool_WithRequiredTaskSupport_CannotBeCalledDirectly()
526526
arguments: new Dictionary<string, object?> { ["input"] = "test" },
527527
cancellationToken: TestContext.Current.CancellationToken).AsTask());
528528

529-
// The server returns MethodNotFound because direct invocation is not allowed for required-task tools
530-
Assert.Equal(McpErrorCode.MethodNotFound, exception.ErrorCode);
529+
// The server returns InvalidParams because direct invocation is not allowed for required-task tools
530+
Assert.Equal(McpErrorCode.InvalidParams, exception.ErrorCode);
531531
Assert.Contains("task", exception.Message, StringComparison.OrdinalIgnoreCase);
532532
}
533533

0 commit comments

Comments
 (0)