Clean up (use of) McpException and McpTransportException (#321)

* Clean up (use of) McpException

* Replace McpTransportException with InvalidOperationException

* Add cancellation test
This commit is contained in:
Stephen Toub 2025-04-18 12:25:29 -04:00 • committed by GitHub
commit 7d77be68cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 231 additions and 219 deletions

View file

@ -31,10 +31,10 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
{
NextCursor = "abc",
Prompts = [new()
{
Name = "FirstCustomPrompt",
Description = "First prompt returned by custom handler",
}],
{
Name = "FirstCustomPrompt",
Description = "First prompt returned by custom handler",
}],
};
case "abc":
@ -42,10 +42,10 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
{
NextCursor = "def",
Prompts = [new()
{
Name = "SecondCustomPrompt",
Description = "Second prompt returned by custom handler",
}],
{
Name = "SecondCustomPrompt",
Description = "Second prompt returned by custom handler",
}],
};
case "def":
@ -53,14 +53,14 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
{
NextCursor = null,
Prompts = [new()
{
Name = "FinalCustomPrompt",
Description = "Final prompt returned by custom handler",
}],
{
Name = "FinalCustomPrompt",
Description = "Final prompt returned by custom handler",
}],
};
default:
throw new Exception("Unexpected cursor");
throw new McpException($"Unexpected cursor: '{cursor}'", McpErrorCode.InvalidParams);
}
})
.WithGetPromptHandler(async (request, cancellationToken) =>
@ -76,7 +76,7 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
};
default:
throw new Exception($"Unknown prompt '{request.Params?.Name}'");
throw new McpException($"Unknown prompt '{request.Params?.Name}'", McpErrorCode.InvalidParams);
}
})
.WithPrompts<SimplePrompts>();
@ -194,7 +194,7 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
nameof(SimplePrompts.ReturnsChatMessages),
cancellationToken: TestContext.Current.CancellationToken));
Assert.Contains("Missing required parameter", e.Message);
Assert.Equal(McpErrorCode.InternalError, e.ErrorCode);
}
[Fact]

View file

@ -88,7 +88,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
};
default:
throw new Exception("Unexpected cursor");
throw new McpException($"Unexpected cursor: '{cursor}'", McpErrorCode.InvalidParams);
}
})
.WithCallToolHandler(async (request, cancellationToken) =>
@ -104,7 +104,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
};
default:
throw new Exception($"Unknown tool '{request.Params?.Name}'");
throw new McpException($"Unknown tool: '{request.Params?.Name}'", McpErrorCode.InvalidParams);
}
})
.WithTools<EchoTool>(serializerOptions: BuilderToolsJsonContext.Default.Options);