From e06f0c147b1214980ca0f64ca6fc79db9b336406 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Fri, 16 May 2025 17:57:55 -0700 Subject: [PATCH] Remove the hosting and options dependencies from the ModelContextProtocol package --- Directory.Packages.props | 2 +- .../EverythingServer/EverythingServer.csproj | 2 +- .../QuickstartWeatherServer.csproj | 2 +- .../TestServerWithHosting.csproj | 4 +- .../Configuration/DefaultMcpServerBuilder.cs | 4 +- .../Configuration/IMcpServerBuilder.cs | 0 .../McpServerBuilderExtensions.cs | 103 ++++++++++++------ .../Configuration}/McpServerHandlers.cs | 0 .../Configuration/McpServerOptionsSetup.cs | 5 +- .../McpServerServiceCollectionExtensions.cs | 1 + .../SingleSessionMcpServerHostedService.cs | 3 +- .../HttpMcpServerBuilderExtensions.cs | 38 ------- .../ModelContextProtocol.csproj | 2 +- .../Server/McpServerPrompt.cs | 3 +- .../Server/McpServerPromptAttribute.cs | 2 +- .../Server/McpServerPromptTypeAttribute.cs | 5 +- .../Server/McpServerResource.cs | 2 +- .../Server/McpServerResourceAttribute.cs | 2 +- .../Server/McpServerResourceTypeAttribute.cs | 6 +- .../Server/McpServerTool.cs | 3 +- .../Server/McpServerToolAttribute.cs | 2 +- .../Server/McpServerToolTypeAttribute.cs | 5 +- .../Server/RequestContext.cs | 2 +- .../Server/StdioServerTransport.cs | 12 -- ...pServerBuilderExtensionsTransportsTests.cs | 11 +- .../Transport/StdioServerTransportTests.cs | 1 - 26 files changed, 99 insertions(+), 123 deletions(-) rename src/{ModelContextProtocol => ModelContextProtocol.AspNetCore}/Configuration/DefaultMcpServerBuilder.cs (94%) rename src/{ModelContextProtocol => ModelContextProtocol.AspNetCore}/Configuration/IMcpServerBuilder.cs (100%) rename src/{ModelContextProtocol => ModelContextProtocol.AspNetCore}/Configuration/McpServerBuilderExtensions.cs (92%) rename src/{ModelContextProtocol/Server => ModelContextProtocol.AspNetCore/Configuration}/McpServerHandlers.cs (100%) rename src/{ModelContextProtocol => ModelContextProtocol.AspNetCore}/Configuration/McpServerOptionsSetup.cs (97%) rename src/{ModelContextProtocol => ModelContextProtocol.AspNetCore}/Configuration/McpServerServiceCollectionExtensions.cs (96%) rename src/{ModelContextProtocol/Server => ModelContextProtocol.AspNetCore/Configuration}/SingleSessionMcpServerHostedService.cs (89%) delete mode 100644 src/ModelContextProtocol.AspNetCore/HttpMcpServerBuilderExtensions.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index 6f5e8c2e2..0b81a2b2b 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -8,11 +8,11 @@ - + diff --git a/samples/EverythingServer/EverythingServer.csproj b/samples/EverythingServer/EverythingServer.csproj index d5046f7eb..fa630d819 100644 --- a/samples/EverythingServer/EverythingServer.csproj +++ b/samples/EverythingServer/EverythingServer.csproj @@ -15,7 +15,7 @@ - + diff --git a/samples/QuickstartWeatherServer/QuickstartWeatherServer.csproj b/samples/QuickstartWeatherServer/QuickstartWeatherServer.csproj index dc1108a8f..944e07216 100644 --- a/samples/QuickstartWeatherServer/QuickstartWeatherServer.csproj +++ b/samples/QuickstartWeatherServer/QuickstartWeatherServer.csproj @@ -13,7 +13,7 @@ - + diff --git a/samples/TestServerWithHosting/TestServerWithHosting.csproj b/samples/TestServerWithHosting/TestServerWithHosting.csproj index 9ddb6190d..3bec6f0c5 100644 --- a/samples/TestServerWithHosting/TestServerWithHosting.csproj +++ b/samples/TestServerWithHosting/TestServerWithHosting.csproj @@ -2,7 +2,7 @@ Exe - net9.0;net8.0;net472 + net9.0;net8.0 enable enable @@ -34,7 +35,6 @@ - diff --git a/src/ModelContextProtocol/Server/McpServerPrompt.cs b/src/ModelContextProtocol/Server/McpServerPrompt.cs index 2260617e0..4160fc3fb 100644 --- a/src/ModelContextProtocol/Server/McpServerPrompt.cs +++ b/src/ModelContextProtocol/Server/McpServerPrompt.cs @@ -21,8 +21,7 @@ namespace ModelContextProtocol.Server; /// /// Most commonly, instances are created using the static methods. /// These methods enable creating an for a method, specified via a or -/// , and are what are used implicitly by and -/// . The methods +/// , and are what are used implicitly by WithPromptsFromAssembly and WithPrompts. The methods /// create instances capable of working with a large variety of .NET method signatures, automatically handling /// how parameters are marshaled into the method from the JSON received from the MCP client, and how the return value is marshaled back /// into the that's then serialized and sent back to the client. diff --git a/src/ModelContextProtocol/Server/McpServerPromptAttribute.cs b/src/ModelContextProtocol/Server/McpServerPromptAttribute.cs index d47b02c67..8e7fdf050 100644 --- a/src/ModelContextProtocol/Server/McpServerPromptAttribute.cs +++ b/src/ModelContextProtocol/Server/McpServerPromptAttribute.cs @@ -11,7 +11,7 @@ namespace ModelContextProtocol.Server; /// /// /// This attribute is applied to methods that should be exposed as prompts in the Model Context Protocol. When a class -/// containing methods marked with this attribute is registered with , +/// containing methods marked with this attribute is registered with McpServerBuilderExtensions, /// these methods become available as prompts that can be called by MCP clients. /// /// diff --git a/src/ModelContextProtocol/Server/McpServerPromptTypeAttribute.cs b/src/ModelContextProtocol/Server/McpServerPromptTypeAttribute.cs index 31b524ecb..3f80b3c5a 100644 --- a/src/ModelContextProtocol/Server/McpServerPromptTypeAttribute.cs +++ b/src/ModelContextProtocol/Server/McpServerPromptTypeAttribute.cs @@ -9,9 +9,8 @@ namespace ModelContextProtocol.Server; /// /// This attribute is used to mark a class containing methods that should be automatically /// discovered and registered as s. When combined with discovery methods like -/// , it enables automatic registration -/// of prompts without explicitly listing each prompt class. The attribute is not necessary when a reference -/// to the type is provided directly to a method like . +/// WithPromptsFromAssembly, it enables automatic registration of prompts without explicitly listing each prompt class. +/// The attribute is not necessary when a reference to the type is provided directly to a method like WithPrompts. /// /// /// Within a class marked with this attribute, individual methods that should be exposed as diff --git a/src/ModelContextProtocol/Server/McpServerResource.cs b/src/ModelContextProtocol/Server/McpServerResource.cs index 346c0329a..e85ec7325 100644 --- a/src/ModelContextProtocol/Server/McpServerResource.cs +++ b/src/ModelContextProtocol/Server/McpServerResource.cs @@ -18,7 +18,7 @@ namespace ModelContextProtocol.Server; /// /// Most commonly, instances are created using the static methods. /// These methods enable creating an for a method, specified via a or -/// , and are what are used implicitly by and +/// , and are what are used implicitly by WithResourcesFromAssembly and /// . The methods /// create instances capable of working with a large variety of .NET method signatures, automatically handling /// how parameters are marshaled into the method from the URI received from the MCP client, and how the return value is marshaled back diff --git a/src/ModelContextProtocol/Server/McpServerResourceAttribute.cs b/src/ModelContextProtocol/Server/McpServerResourceAttribute.cs index 9c5347c79..5cc7a4efb 100644 --- a/src/ModelContextProtocol/Server/McpServerResourceAttribute.cs +++ b/src/ModelContextProtocol/Server/McpServerResourceAttribute.cs @@ -10,7 +10,7 @@ namespace ModelContextProtocol.Server; /// /// /// This attribute is applied to methods or properties that should be exposed as resources in the Model Context Protocol. When a class -/// containing methods marked with this attribute is registered with , +/// containing methods marked with this attribute is registered with McpServerBuilderExtensions, /// these methods or properties become available as resources that can be called by MCP clients. /// /// diff --git a/src/ModelContextProtocol/Server/McpServerResourceTypeAttribute.cs b/src/ModelContextProtocol/Server/McpServerResourceTypeAttribute.cs index b73bd081e..e287765cd 100644 --- a/src/ModelContextProtocol/Server/McpServerResourceTypeAttribute.cs +++ b/src/ModelContextProtocol/Server/McpServerResourceTypeAttribute.cs @@ -9,9 +9,9 @@ namespace ModelContextProtocol.Server; /// /// This attribute is used to mark a class containing members that should be automatically /// discovered and registered as s. When combined with discovery methods like -/// , it enables automatic registration -/// of resources without explicitly listing each resource class. The attribute is not necessary when a reference -/// to the type is provided directly to a method like . +/// WithResourcesFromAssembly, it enables automatic registration of resources without explicitly listing each +/// resource class. The attribute is not necessary when a reference to the type is provided directly to a method +/// like McpServerBuilderExtensions.WithResources. /// /// /// Within a class marked with this attribute, individual members that should be exposed as diff --git a/src/ModelContextProtocol/Server/McpServerTool.cs b/src/ModelContextProtocol/Server/McpServerTool.cs index f2aa2d021..a4b0cc587 100644 --- a/src/ModelContextProtocol/Server/McpServerTool.cs +++ b/src/ModelContextProtocol/Server/McpServerTool.cs @@ -21,8 +21,7 @@ namespace ModelContextProtocol.Server; /// /// Most commonly, instances are created using the static methods. /// These methods enable creating an for a method, specified via a or -/// , and are what are used implicitly by and -/// . The methods +/// , and are what are used implicitly by WithToolsFromAssembly and WithTools. The methods /// create instances capable of working with a large variety of .NET method signatures, automatically handling /// how parameters are marshaled into the method from the JSON received from the MCP client, and how the return value is marshaled back /// into the that's then serialized and sent back to the client. diff --git a/src/ModelContextProtocol/Server/McpServerToolAttribute.cs b/src/ModelContextProtocol/Server/McpServerToolAttribute.cs index e096a1505..73ee786b5 100644 --- a/src/ModelContextProtocol/Server/McpServerToolAttribute.cs +++ b/src/ModelContextProtocol/Server/McpServerToolAttribute.cs @@ -11,7 +11,7 @@ namespace ModelContextProtocol.Server; /// /// /// This attribute is applied to methods that should be exposed as tools in the Model Context Protocol. When a class -/// containing methods marked with this attribute is registered with , +/// containing methods marked with this attribute is registered with McpServerBuilderExtensions, /// these methods become available as tools that can be called by MCP clients. /// /// diff --git a/src/ModelContextProtocol/Server/McpServerToolTypeAttribute.cs b/src/ModelContextProtocol/Server/McpServerToolTypeAttribute.cs index 57ffb3d9e..ee13ddc63 100644 --- a/src/ModelContextProtocol/Server/McpServerToolTypeAttribute.cs +++ b/src/ModelContextProtocol/Server/McpServerToolTypeAttribute.cs @@ -9,9 +9,8 @@ namespace ModelContextProtocol.Server; /// /// This attribute is used to mark a class containing methods that should be automatically /// discovered and registered as s. When combined with discovery methods like -/// , it enables automatic registration -/// of tools without explicitly listing each tool class. The attribute is not necessary when a reference -/// to the type is provided directly to a method like . +/// WithToolsFromAssembly, it enables automatic registration of tools without explicitly listing each tool +/// class. The attribute is not necessary when a reference to the type is provided directly to a method like WithTools. /// /// /// Within a class marked with this attribute, individual methods that should be exposed as diff --git a/src/ModelContextProtocol/Server/RequestContext.cs b/src/ModelContextProtocol/Server/RequestContext.cs index 7437faea1..ee8d4a348 100644 --- a/src/ModelContextProtocol/Server/RequestContext.cs +++ b/src/ModelContextProtocol/Server/RequestContext.cs @@ -8,7 +8,7 @@ namespace ModelContextProtocol.Server; /// Type of the request parameters specific to each MCP operation. /// /// The encapsulates all contextual information for handling an MCP request. -/// This type is typically received as a parameter in handler delegates registered with , +/// This type is typically received as a parameter in handler delegates registered with IMcpServerBuilder, /// and may be injected as parameters into s. /// public sealed class RequestContext diff --git a/src/ModelContextProtocol/Server/StdioServerTransport.cs b/src/ModelContextProtocol/Server/StdioServerTransport.cs index df7b0087c..556a31159 100644 --- a/src/ModelContextProtocol/Server/StdioServerTransport.cs +++ b/src/ModelContextProtocol/Server/StdioServerTransport.cs @@ -1,5 +1,4 @@ using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; using ModelContextProtocol.Protocol; namespace ModelContextProtocol.Server; @@ -9,17 +8,6 @@ namespace ModelContextProtocol.Server; /// public sealed class StdioServerTransport : StreamServerTransport { - /// - /// Initializes a new instance of the class. - /// - /// The server options. - /// Optional logger factory used for logging employed by the transport. - /// is . - public StdioServerTransport(IOptions serverOptions, ILoggerFactory? loggerFactory = null) - : this(serverOptions?.Value!, loggerFactory: loggerFactory) - { - } - /// /// Initializes a new instance of the class. /// diff --git a/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsTransportsTests.cs b/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsTransportsTests.cs index 6183c35eb..f0dd2116b 100644 --- a/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsTransportsTests.cs +++ b/tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsTransportsTests.cs @@ -13,14 +13,13 @@ public class McpServerBuilderExtensionsTransportsTests public void WithStdioServerTransport_Sets_Transport() { var services = new ServiceCollection(); - var builder = new Mock(); - builder.SetupGet(b => b.Services).Returns(services); + services.AddMcpServer().WithStdioServerTransport(); - builder.Object.WithStdioServerTransport(); + var transportServiceType = services.FirstOrDefault(s => s.ServiceType == typeof(ITransport)); + Assert.NotNull(transportServiceType); - var transportType = services.FirstOrDefault(s => s.ServiceType == typeof(ITransport)); - Assert.NotNull(transportType); - Assert.Equal(typeof(StdioServerTransport), transportType.ImplementationType); + var serviceProvider = services.BuildServiceProvider(); + Assert.IsType(serviceProvider.GetRequiredService()); } [Fact] diff --git a/tests/ModelContextProtocol.Tests/Transport/StdioServerTransportTests.cs b/tests/ModelContextProtocol.Tests/Transport/StdioServerTransportTests.cs index 242874730..42e5e5052 100644 --- a/tests/ModelContextProtocol.Tests/Transport/StdioServerTransportTests.cs +++ b/tests/ModelContextProtocol.Tests/Transport/StdioServerTransportTests.cs @@ -39,7 +39,6 @@ public void Constructor_Throws_For_Null_Options() { Assert.Throws("serverName", () => new StdioServerTransport((string)null!)); - Assert.Throws("serverOptions", () => new StdioServerTransport((IOptions)null!)); Assert.Throws("serverOptions", () => new StdioServerTransport((McpServerOptions)null!)); }