diff --git a/src/ModelContextProtocol.Core/McpErrorCode.cs b/src/ModelContextProtocol.Core/McpErrorCode.cs index 33cd74a82..d085f47ff 100644 --- a/src/ModelContextProtocol.Core/McpErrorCode.cs +++ b/src/ModelContextProtocol.Core/McpErrorCode.cs @@ -9,9 +9,16 @@ public enum McpErrorCode /// Indicates that the requested resource could not be found. /// /// - /// This error should be used when a resource URI does not match any available resource on the server. - /// It allows clients to distinguish between missing resources and other types of errors. + /// + /// Deprecated per SEP-2164 (MCP spec 2026-06-30). Use (-32602) instead, + /// which is the standard JSON-RPC code for unknown or unresolvable resource URIs. + /// + /// + /// This value (-32002) is retained for backward compatibility with pre-2026-06-30 clients and servers, + /// but new code should use . + /// /// + [Obsolete("ResourceNotFound (-32002) is deprecated per SEP-2164. Use McpErrorCode.InvalidParams (-32602) instead.")] ResourceNotFound = -32002, /// @@ -65,6 +72,7 @@ public enum McpErrorCode /// /// Tools: Unknown tool name or invalid protocol-level tool arguments. /// Prompts: Unknown prompt name or missing required protocol-level arguments. + /// Resources: Unknown or unresolvable resource URI. /// Pagination: Invalid or expired cursor values. /// Logging: Invalid log level. /// Tasks: Invalid or nonexistent task ID or invalid cursor. diff --git a/src/ModelContextProtocol.Core/McpProtocolException.cs b/src/ModelContextProtocol.Core/McpProtocolException.cs index 3fbef91c0..7bcc4d0a8 100644 --- a/src/ModelContextProtocol.Core/McpProtocolException.cs +++ b/src/ModelContextProtocol.Core/McpProtocolException.cs @@ -76,7 +76,7 @@ public McpProtocolException(string message, Exception? innerException, McpErrorC /// -32700: Parse error - Invalid JSON received /// -32600: Invalid request - The JSON is not a valid Request object /// -32601: Method not found - The method does not exist or is not available - /// -32602: Invalid params - Malformed request or unknown primitive name (tool/prompt/resource) + /// -32602: Invalid params - Malformed request, unknown primitive name (tool/prompt/resource), or unresolvable resource URI /// -32603: Internal error - Internal JSON-RPC error /// /// diff --git a/src/ModelContextProtocol.Core/Server/McpServerImpl.cs b/src/ModelContextProtocol.Core/Server/McpServerImpl.cs index 04d11e016..1aafb8100 100644 --- a/src/ModelContextProtocol.Core/Server/McpServerImpl.cs +++ b/src/ModelContextProtocol.Core/Server/McpServerImpl.cs @@ -421,7 +421,7 @@ subscribeHandler is null && unsubscribeHandler is null && resources is null && listResourcesHandler ??= (static async (_, __) => new ListResourcesResult()); listResourceTemplatesHandler ??= (static async (_, __) => new ListResourceTemplatesResult()); - readResourceHandler ??= (static async (request, _) => throw new McpProtocolException($"Unknown resource URI: '{request.Params?.Uri}'", McpErrorCode.ResourceNotFound)); + readResourceHandler ??= (static async (request, _) => throw new McpProtocolException($"Unknown resource URI: '{request.Params?.Uri}'", McpErrorCode.InvalidParams)); subscribeHandler ??= (static async (_, __) => new EmptyResult()); unsubscribeHandler ??= (static async (_, __) => new EmptyResult()); var listChanged = resourcesCapability?.ListChanged; diff --git a/tests/ModelContextProtocol.TestServer/Program.cs b/tests/ModelContextProtocol.TestServer/Program.cs index 9cb963a96..aaa6104cc 100644 --- a/tests/ModelContextProtocol.TestServer/Program.cs +++ b/tests/ModelContextProtocol.TestServer/Program.cs @@ -503,7 +503,7 @@ private static void ConfigureResources(McpServerOptions options) } ResourceContents contents = resourceContents.FirstOrDefault(r => r.Uri == request.Params.Uri) - ?? throw new McpProtocolException($"Resource not found: '{request.Params.Uri}'", McpErrorCode.ResourceNotFound); + ?? throw new McpProtocolException($"Resource not found: '{request.Params.Uri}'", McpErrorCode.InvalidParams); return new ReadResourceResult { diff --git a/tests/ModelContextProtocol.TestSseServer/Program.cs b/tests/ModelContextProtocol.TestSseServer/Program.cs index a36a0a6e0..e52a8ff1f 100644 --- a/tests/ModelContextProtocol.TestSseServer/Program.cs +++ b/tests/ModelContextProtocol.TestSseServer/Program.cs @@ -307,7 +307,7 @@ static CreateMessageRequestParams CreateRequestSamplingParams(string context, st } ResourceContents? contents = resourceContents.FirstOrDefault(r => r.Uri == request.Params.Uri) ?? - throw new McpProtocolException($"Resource not found: '{request.Params.Uri}'", McpErrorCode.ResourceNotFound); + throw new McpProtocolException($"Resource not found: '{request.Params.Uri}'", McpErrorCode.InvalidParams); return new ReadResourceResult { diff --git a/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsResourcesTests.cs b/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsResourcesTests.cs index 4b03cadb2..d6bb239d7 100644 --- a/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsResourcesTests.cs +++ b/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsResourcesTests.cs @@ -109,7 +109,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer }; } - throw new McpProtocolException($"Resource not found: {request.Params.Uri}", McpErrorCode.ResourceNotFound); + throw new McpProtocolException($"Resource not found: {request.Params.Uri}", McpErrorCode.InvalidParams); }) .WithResources(); } @@ -317,7 +317,7 @@ public async Task Throws_Exception_On_Unknown_Resource() cancellationToken: TestContext.Current.CancellationToken)); Assert.Contains("Resource not found", e.Message); - Assert.Equal(McpErrorCode.ResourceNotFound, e.ErrorCode); + Assert.Equal(McpErrorCode.InvalidParams, e.ErrorCode); } [Fact] diff --git a/tests/ModelContextProtocol.Tests/Configuration/McpServerResourceRoutingTests.cs b/tests/ModelContextProtocol.Tests/Configuration/McpServerResourceRoutingTests.cs index 19e0f1bbe..61a770715 100644 --- a/tests/ModelContextProtocol.Tests/Configuration/McpServerResourceRoutingTests.cs +++ b/tests/ModelContextProtocol.Tests/Configuration/McpServerResourceRoutingTests.cs @@ -53,7 +53,7 @@ private async Task AssertNoMatchAsync( var ex = await Assert.ThrowsAsync(async () => await client.ReadResourceAsync(uri, null, TestContext.Current.CancellationToken)); - Assert.Equal(McpErrorCode.ResourceNotFound, ex.ErrorCode); + Assert.Equal(McpErrorCode.InvalidParams, ex.ErrorCode); } /// @@ -92,7 +92,7 @@ public async Task MultipleTemplatedResources_MatchesCorrectResource() // Literal template braces in URI should not match (template literal is not a valid URI) var mcpEx = await Assert.ThrowsAsync(async () => await client.ReadResourceAsync("test://params{?a1,a2,a3}", null, TestContext.Current.CancellationToken)); - Assert.Equal(McpErrorCode.ResourceNotFound, mcpEx.ErrorCode); + Assert.Equal(McpErrorCode.InvalidParams, mcpEx.ErrorCode); Assert.Equal("Request failed (remote): Unknown resource URI: 'test://params{?a1,a2,a3}'", mcpEx.Message); } diff --git a/tests/ModelContextProtocol.Tests/McpProtocolExceptionDataTests.cs b/tests/ModelContextProtocol.Tests/McpProtocolExceptionDataTests.cs index 7d50a3044..336f58382 100644 --- a/tests/ModelContextProtocol.Tests/McpProtocolExceptionDataTests.cs +++ b/tests/ModelContextProtocol.Tests/McpProtocolExceptionDataTests.cs @@ -33,7 +33,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer switch (toolName) { case "throw_with_serializable_data": - throw new McpProtocolException("Resource not found", McpErrorCode.ResourceNotFound) + throw new McpProtocolException("Resource not found", McpErrorCode.InvalidParams) { Data = { @@ -43,7 +43,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer }; case "throw_with_nonserializable_data": - throw new McpProtocolException("Resource not found", McpErrorCode.ResourceNotFound) + throw new McpProtocolException("Resource not found", McpErrorCode.InvalidParams) { Data = { @@ -55,7 +55,7 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer }; case "throw_with_only_nonserializable_data": - throw new McpProtocolException("Resource not found", McpErrorCode.ResourceNotFound) + throw new McpProtocolException("Resource not found", McpErrorCode.InvalidParams) { Data = { @@ -79,7 +79,7 @@ public async Task Exception_With_Serializable_Data_Propagates_To_Client() await client.CallToolAsync("throw_with_serializable_data", cancellationToken: TestContext.Current.CancellationToken)); Assert.Equal("Request failed (remote): Resource not found", exception.Message); - Assert.Equal(McpErrorCode.ResourceNotFound, exception.ErrorCode); + Assert.Equal(McpErrorCode.InvalidParams, exception.ErrorCode); // Verify the data was propagated to the exception // The Data collection should contain the expected keys @@ -113,7 +113,7 @@ public async Task Exception_With_NonSerializable_Data_Still_Propagates_Error_To_ await client.CallToolAsync("throw_with_nonserializable_data", cancellationToken: TestContext.Current.CancellationToken)); Assert.Equal("Request failed (remote): Resource not found", exception.Message); - Assert.Equal(McpErrorCode.ResourceNotFound, exception.ErrorCode); + Assert.Equal(McpErrorCode.InvalidParams, exception.ErrorCode); // Verify that only the serializable data was propagated (non-serializable was filtered out) var hasUri = false; @@ -142,7 +142,7 @@ public async Task Exception_With_Only_NonSerializable_Data_Still_Propagates_Erro await client.CallToolAsync("throw_with_only_nonserializable_data", cancellationToken: TestContext.Current.CancellationToken)); Assert.Equal("Request failed (remote): Resource not found", exception.Message); - Assert.Equal(McpErrorCode.ResourceNotFound, exception.ErrorCode); + Assert.Equal(McpErrorCode.InvalidParams, exception.ErrorCode); // When all data is non-serializable, the Data collection should be empty // (the server's ConvertExceptionData returns null when no serializable data exists) diff --git a/tests/ModelContextProtocol.Tests/Server/McpServerTests.cs b/tests/ModelContextProtocol.Tests/Server/McpServerTests.cs index d9febd721..b8bd57b02 100644 --- a/tests/ModelContextProtocol.Tests/Server/McpServerTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/McpServerTests.cs @@ -1033,7 +1033,7 @@ await transport.SendMessageAsync( public async Task Can_Handle_Call_Tool_Requests_With_McpProtocolException_And_Data() { const string ErrorMessage = "Resource not found"; - const McpErrorCode ErrorCode = McpErrorCode.ResourceNotFound; + const McpErrorCode ErrorCode = McpErrorCode.InvalidParams; const string ResourceUri = "file:///path/to/resource"; await using var transport = new TestServerTransport();