Add ResourceNotFound error code (-32002) (#1062)
This commit is contained in:
parent
4808ca0324
commit
d76bc16bd1
6 changed files with 36 additions and 26 deletions
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in a new issue