Add ResourceNotFound error code (-32002) (#1062)

This commit is contained in:
Copilot 2025-12-05 10:39:30 -08:00 • committed by GitHub
commit d76bc16bd1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 36 additions and 26 deletions

View file

@ -434,7 +434,7 @@ internal static class Program
}
ResourceContents contents = resourceContents.FirstOrDefault(r => r.Uri == request.Params.Uri)
?? throw new McpProtocolException($"Resource not found: '{request.Params.Uri}'", McpErrorCode.InvalidParams);
?? throw new McpProtocolException($"Resource not found: '{request.Params.Uri}'", McpErrorCode.ResourceNotFound);
return new ReadResourceResult
{

View file

@ -273,7 +273,7 @@ public class Program
}
ResourceContents? contents = resourceContents.FirstOrDefault(r => r.Uri == request.Params.Uri) ??
throw new McpProtocolException($"Resource not found: '{request.Params.Uri}'", McpErrorCode.InvalidParams);
throw new McpProtocolException($"Resource not found: '{request.Params.Uri}'", McpErrorCode.ResourceNotFound);
return new ReadResourceResult
{

View file

@ -108,7 +108,7 @@ public partial class McpServerBuilderExtensionsResourcesTests : ClientServerTest
};
}
throw new McpProtocolException($"Resource not found: {request.Params?.Uri}");
throw new McpProtocolException($"Resource not found: {request.Params?.Uri}", McpErrorCode.ResourceNotFound);
})
.WithResources<SimpleResources>();
}
@ -249,6 +249,7 @@ public partial class McpServerBuilderExtensionsResourcesTests : ClientServerTest
cancellationToken: TestContext.Current.CancellationToken));
Assert.Contains("Resource not found", e.Message);
Assert.Equal(McpErrorCode.ResourceNotFound, e.ErrorCode);
}
[Fact]

View file

@ -36,7 +36,7 @@ public sealed class McpServerResourceRoutingTests(ITestOutputHelper testOutputHe
Assert.Equal("params: a, b, c", ((TextResourceContents)paramsResult.Contents[0]).Text);
var mcpEx = await Assert.ThrowsAsync<McpProtocolException>(async () => await client.ReadResourceAsync("test://params{?a1,a2,a3}", null, TestContext.Current.CancellationToken));
Assert.Equal(McpErrorCode.InvalidParams, mcpEx.ErrorCode);
Assert.Equal(McpErrorCode.ResourceNotFound, mcpEx.ErrorCode);
Assert.Equal("Request failed (remote): Unknown resource URI: 'test://params{?a1,a2,a3}'", mcpEx.Message);
}
}

View file

@ -33,7 +33,7 @@ public class McpProtocolExceptionDataTests : ClientServerTestBase
switch (toolName)
{
case "throw_with_serializable_data":
throw new McpProtocolException("Resource not found", (McpErrorCode)(-32002))
throw new McpProtocolException("Resource not found", McpErrorCode.ResourceNotFound)
{
Data =
{
@ -43,7 +43,7 @@ public class McpProtocolExceptionDataTests : ClientServerTestBase
};
case "throw_with_nonserializable_data":
throw new McpProtocolException("Resource not found", (McpErrorCode)(-32002))
throw new McpProtocolException("Resource not found", McpErrorCode.ResourceNotFound)
{
Data =
{
@ -55,7 +55,7 @@ public class McpProtocolExceptionDataTests : ClientServerTestBase
};
case "throw_with_only_nonserializable_data":
throw new McpProtocolException("Resource not found", (McpErrorCode)(-32002))
throw new McpProtocolException("Resource not found", McpErrorCode.ResourceNotFound)
{
Data =
{
@ -79,7 +79,7 @@ public class McpProtocolExceptionDataTests : ClientServerTestBase
await client.CallToolAsync("throw_with_serializable_data", cancellationToken: TestContext.Current.CancellationToken));
Assert.Equal("Request failed (remote): Resource not found", exception.Message);
Assert.Equal((McpErrorCode)(-32002), exception.ErrorCode);
Assert.Equal(McpErrorCode.ResourceNotFound, exception.ErrorCode);
// Verify the data was propagated to the exception
// The Data collection should contain the expected keys
@ -113,7 +113,7 @@ public class McpProtocolExceptionDataTests : ClientServerTestBase
await client.CallToolAsync("throw_with_nonserializable_data", cancellationToken: TestContext.Current.CancellationToken));
Assert.Equal("Request failed (remote): Resource not found", exception.Message);
Assert.Equal((McpErrorCode)(-32002), exception.ErrorCode);
Assert.Equal(McpErrorCode.ResourceNotFound, exception.ErrorCode);
// Verify that only the serializable data was propagated (non-serializable was filtered out)
var hasUri = false;
@ -142,7 +142,7 @@ public class McpProtocolExceptionDataTests : ClientServerTestBase
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)(-32002), exception.ErrorCode);
Assert.Equal(McpErrorCode.ResourceNotFound, 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)

View file

@ -681,7 +681,7 @@ public class McpServerTests : LoggedTest
public async Task Can_Handle_Call_Tool_Requests_With_McpProtocolException_And_Data()
{
const string ErrorMessage = "Resource not found";
const McpErrorCode ErrorCode = (McpErrorCode)(-32002);
const McpErrorCode ErrorCode = McpErrorCode.ResourceNotFound;
const string ResourceUri = "file:///path/to/resource";
await using var transport = new TestServerTransport();