Restructure IMcpEndpoint, IMcpClient, and IMcpServer (#723)

* Remove `McpEndpoint` & `IMcpClient`

* Revise naming, add abstract classes

* Fix missing renames

* Move extension methods to instance methods

* Add back old API, but obsoleted

* Add back obsoleted factory classes

* Move deprecation attributes

* `SseClientTransport` -> `HttpClientTransport`

* Update src/ModelContextProtocol.Core/Server/AugmentedServiceProvider.cs

Co-authored-by: Stephen Halter <halter73@gmail.com>

* PR feedback

* PR feedback

* Add back dispose lock

* Fix docs projects

* PR feedback

* Undo `docs/*.md` changes

---------

Co-authored-by: Stephen Halter <halter73@gmail.com>
This commit is contained in:
Mackinnon Buck 2025-09-16 13:22:10 -04:00 • committed by GitHub
commit 38b4a26998
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 5169 additions and 3786 deletions

View file

@ -129,7 +129,7 @@ public class McpServerBuilderExtensionsFilterTests : ClientServerTestBase
[Fact]
public async Task AddListResourceTemplatesFilter_Logs_When_ListResourceTemplates_Called()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
await client.ListResourceTemplatesAsync(cancellationToken: TestContext.Current.CancellationToken);
@ -141,7 +141,7 @@ public class McpServerBuilderExtensionsFilterTests : ClientServerTestBase
[Fact]
public async Task AddListToolsFilter_Logs_When_ListTools_Called()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
@ -153,7 +153,7 @@ public class McpServerBuilderExtensionsFilterTests : ClientServerTestBase
[Fact]
public async Task AddCallToolFilter_Logs_When_CallTool_Called()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
await client.CallToolAsync("test_tool_method", cancellationToken: TestContext.Current.CancellationToken);
@ -165,7 +165,7 @@ public class McpServerBuilderExtensionsFilterTests : ClientServerTestBase
[Fact]
public async Task AddListPromptsFilter_Logs_When_ListPrompts_Called()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
await client.ListPromptsAsync(cancellationToken: TestContext.Current.CancellationToken);
@ -177,7 +177,7 @@ public class McpServerBuilderExtensionsFilterTests : ClientServerTestBase
[Fact]
public async Task AddGetPromptFilter_Logs_When_GetPrompt_Called()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
await client.GetPromptAsync("test_prompt_method", cancellationToken: TestContext.Current.CancellationToken);
@ -189,7 +189,7 @@ public class McpServerBuilderExtensionsFilterTests : ClientServerTestBase
[Fact]
public async Task AddListResourcesFilter_Logs_When_ListResources_Called()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
await client.ListResourcesAsync(cancellationToken: TestContext.Current.CancellationToken);
@ -201,7 +201,7 @@ public class McpServerBuilderExtensionsFilterTests : ClientServerTestBase
[Fact]
public async Task AddReadResourceFilter_Logs_When_ReadResource_Called()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
await client.ReadResourceAsync("test://resource/123", cancellationToken: TestContext.Current.CancellationToken);
@ -213,7 +213,7 @@ public class McpServerBuilderExtensionsFilterTests : ClientServerTestBase
[Fact]
public async Task AddCompleteFilter_Logs_When_Complete_Called()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var reference = new PromptReference { Name = "test_prompt_method" };
await client.CompleteAsync(reference, "argument", "value", cancellationToken: TestContext.Current.CancellationToken);
@ -226,7 +226,7 @@ public class McpServerBuilderExtensionsFilterTests : ClientServerTestBase
[Fact]
public async Task AddSubscribeToResourcesFilter_Logs_When_SubscribeToResources_Called()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
await client.SubscribeToResourceAsync("test://resource/123", cancellationToken: TestContext.Current.CancellationToken);
@ -238,7 +238,7 @@ public class McpServerBuilderExtensionsFilterTests : ClientServerTestBase
[Fact]
public async Task AddUnsubscribeFromResourcesFilter_Logs_When_UnsubscribeFromResources_Called()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
await client.UnsubscribeFromResourceAsync("test://resource/123", cancellationToken: TestContext.Current.CancellationToken);
@ -250,7 +250,7 @@ public class McpServerBuilderExtensionsFilterTests : ClientServerTestBase
[Fact]
public async Task AddSetLoggingLevelFilter_Logs_When_SetLoggingLevel_Called()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
await client.SetLoggingLevel(LoggingLevel.Info, cancellationToken: TestContext.Current.CancellationToken);
@ -262,7 +262,7 @@ public class McpServerBuilderExtensionsFilterTests : ClientServerTestBase
[Fact]
public async Task AddListToolsFilter_Multiple_Filters_Log_In_Expected_Order()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);

View file

@ -98,7 +98,7 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
[Fact]
public async Task Can_List_And_Call_Registered_Prompts()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var prompts = await client.ListPromptsAsync(TestContext.Current.CancellationToken);
Assert.Equal(6, prompts.Count);
@ -127,7 +127,7 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
[Fact]
public async Task Can_Be_Notified_Of_Prompt_Changes()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var prompts = await client.ListPromptsAsync(TestContext.Current.CancellationToken);
Assert.Equal(6, prompts.Count);
@ -168,7 +168,7 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
[Fact]
public async Task TitleAttributeProperty_PropagatedToTitle()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var prompts = await client.ListPromptsAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.NotNull(prompts);
@ -182,7 +182,7 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
[Fact]
public async Task Throws_When_Prompt_Fails()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
await Assert.ThrowsAsync<McpException>(async () => await client.GetPromptAsync(
nameof(SimplePrompts.ThrowsException),
@ -192,7 +192,7 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
[Fact]
public async Task Throws_Exception_On_Unknown_Prompt()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var e = await Assert.ThrowsAsync<McpException>(async () => await client.GetPromptAsync(
"NotRegisteredPrompt",
@ -204,7 +204,7 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
[Fact]
public async Task Throws_Exception_Missing_Parameter()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var e = await Assert.ThrowsAsync<McpException>(async () => await client.GetPromptAsync(
"returns_chat_messages",
@ -238,7 +238,7 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
sc.AddMcpServer().WithPrompts(target);
McpServerPrompt prompt = sc.BuildServiceProvider().GetServices<McpServerPrompt>().First(t => t.ProtocolPrompt.Name == "returns_string");
var result = await prompt.GetAsync(new RequestContext<GetPromptRequestParams>(new Mock<IMcpServer>().Object, new JsonRpcRequest { Method = "test", Id = new RequestId("1") })
var result = await prompt.GetAsync(new RequestContext<GetPromptRequestParams>(new Mock<McpServer>().Object, new JsonRpcRequest { Method = "test", Id = new RequestId("1") })
{
Params = new GetPromptRequestParams
{

View file

@ -126,7 +126,7 @@ public partial class McpServerBuilderExtensionsResourcesTests : ClientServerTest
[Fact]
public async Task Can_List_And_Call_Registered_Resources()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
Assert.NotNull(client.ServerCapabilities.Resources);
@ -145,7 +145,7 @@ public partial class McpServerBuilderExtensionsResourcesTests : ClientServerTest
[Fact]
public async Task Can_List_And_Call_Registered_ResourceTemplates()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var resources = await client.ListResourceTemplatesAsync(TestContext.Current.CancellationToken);
Assert.Equal(3, resources.Count);
@ -162,7 +162,7 @@ public partial class McpServerBuilderExtensionsResourcesTests : ClientServerTest
[Fact]
public async Task Can_Be_Notified_Of_Resource_Changes()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var resources = await client.ListResourcesAsync(TestContext.Current.CancellationToken);
Assert.Equal(5, resources.Count);
@ -203,7 +203,7 @@ public partial class McpServerBuilderExtensionsResourcesTests : ClientServerTest
[Fact]
public async Task TitleAttributeProperty_PropagatedToTitle()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var resources = await client.ListResourcesAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.NotNull(resources);
@ -221,7 +221,7 @@ public partial class McpServerBuilderExtensionsResourcesTests : ClientServerTest
[Fact]
public async Task Throws_When_Resource_Fails()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
await Assert.ThrowsAsync<McpException>(async () => await client.ReadResourceAsync(
$"resource://mcp/{nameof(SimpleResources.ThrowsException)}",
@ -231,7 +231,7 @@ public partial class McpServerBuilderExtensionsResourcesTests : ClientServerTest
[Fact]
public async Task Throws_Exception_On_Unknown_Resource()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var e = await Assert.ThrowsAsync<McpException>(async () => await client.ReadResourceAsync(
"test:///NotRegisteredResource",
@ -265,7 +265,7 @@ public partial class McpServerBuilderExtensionsResourcesTests : ClientServerTest
sc.AddMcpServer().WithResources(target);
McpServerResource resource = sc.BuildServiceProvider().GetServices<McpServerResource>().First(t => t.ProtocolResource?.Name == "returns_string");
var result = await resource.ReadAsync(new RequestContext<ReadResourceRequestParams>(new Mock<IMcpServer>().Object, new JsonRpcRequest { Method = "test", Id = new RequestId("1") })
var result = await resource.ReadAsync(new RequestContext<ReadResourceRequestParams>(new Mock<McpServer>().Object, new JsonRpcRequest { Method = "test", Id = new RequestId("1") })
{
Params = new()
{

View file

@ -127,7 +127,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
[Fact]
public async Task Can_List_Registered_Tools()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.Equal(16, tools.Count);
@ -156,10 +156,10 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
var stdoutPipe = new Pipe();
await using var transport = new StreamServerTransport(stdinPipe.Reader.AsStream(), stdoutPipe.Writer.AsStream());
await using var server = McpServerFactory.Create(transport, options, loggerFactory, ServiceProvider);
await using var server = McpServer.Create(transport, options, loggerFactory, ServiceProvider);
var serverRunTask = server.RunAsync(TestContext.Current.CancellationToken);
await using (var client = await McpClientFactory.CreateAsync(
await using (var client = await McpClient.CreateAsync(
new StreamClientTransport(
serverInput: stdinPipe.Writer.AsStream(),
serverOutput: stdoutPipe.Reader.AsStream(),
@ -191,7 +191,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
[Fact]
public async Task Can_Be_Notified_Of_Tool_Changes()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.Equal(16, tools.Count);
@ -232,7 +232,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
[Fact]
public async Task Can_Call_Registered_Tool()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var result = await client.CallToolAsync(
"echo",
@ -251,7 +251,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
[Fact]
public async Task Can_Call_Registered_Tool_With_Array_Result()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var result = await client.CallToolAsync(
"echo_array",
@ -274,7 +274,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
[Fact]
public async Task Can_Call_Registered_Tool_With_Null_Result()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var result = await client.CallToolAsync(
"return_null",
@ -288,7 +288,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
[Fact]
public async Task Can_Call_Registered_Tool_With_Json_Result()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var result = await client.CallToolAsync(
"return_json",
@ -305,7 +305,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
[Fact]
public async Task Can_Call_Registered_Tool_With_Int_Result()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var result = await client.CallToolAsync(
"return_integer",
@ -320,7 +320,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
[Fact]
public async Task Can_Call_Registered_Tool_And_Pass_ComplexType()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var result = await client.CallToolAsync(
"echo_complex",
@ -337,7 +337,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
[Fact]
public async Task Can_Call_Registered_Tool_With_Instance_Method()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
string[][] parts = new string[2][];
for (int i = 0; i < 2; i++)
@ -366,7 +366,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
[Fact]
public async Task Returns_IsError_Content_And_Logs_Error_When_Tool_Fails()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var result = await client.CallToolAsync(
"throw_exception",
@ -386,7 +386,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
[Fact]
public async Task Throws_Exception_On_Unknown_Tool()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var e = await Assert.ThrowsAsync<McpException>(async () => await client.CallToolAsync(
"NotRegisteredTool",
@ -398,7 +398,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
[Fact]
public async Task Returns_IsError_Missing_Parameter()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var result = await client.CallToolAsync(
"echo",
@ -525,7 +525,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
sc.AddMcpServer().WithTools(target, BuilderToolsJsonContext.Default.Options);
McpServerTool tool = sc.BuildServiceProvider().GetServices<McpServerTool>().First(t => t.ProtocolTool.Name == "get_ctor_parameter");
var result = await tool.InvokeAsync(new RequestContext<CallToolRequestParams>(new Mock<IMcpServer>().Object, new JsonRpcRequest { Method = "test", Id = new RequestId("1") }), TestContext.Current.CancellationToken);
var result = await tool.InvokeAsync(new RequestContext<CallToolRequestParams>(new Mock<McpServer>().Object, new JsonRpcRequest { Method = "test", Id = new RequestId("1") }), TestContext.Current.CancellationToken);
Assert.Equal(target.GetCtorParameter(), (result.Content[0] as TextContentBlock)?.Text);
}
@ -557,7 +557,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
[Fact]
public async Task Recognizes_Parameter_Types()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
@ -632,7 +632,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
[Fact]
public async Task TitleAttributeProperty_PropagatedToTitle()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.NotNull(tools);
@ -648,7 +648,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
[Fact]
public async Task HandlesIProgressParameter()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.NotNull(tools);
@ -702,7 +702,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
[Fact]
public async Task CancellationNotificationsPropagateToToolTokens()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.NotNull(tools);

View file

@ -22,7 +22,7 @@ public partial class McpServerScopedTests : ClientServerTestBase
[Fact]
public async Task InjectScopedServiceAsArgument()
{
await using IMcpClient client = await CreateMcpClientForServer();
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(McpServerScopedTestsJsonContext.Default.Options, TestContext.Current.CancellationToken);
var tool = tools.First(t => t.Name == "echo_complex");