csharp-sdk/src/ModelContextProtocol/Configuration/McpServerServiceCollectionExtensions.cs
Rob Chambers 77871c5aac
AI seeded comments from robch and then edited by stephentoub (#238)
* AI generated `///` comment updates

* Fix compilation issues

* Fix incorrect XML comment structure

* Update all Client comments

* Update all Configuration comments

* Update all Messages comments

* Update all Transport comments

* Update all Types comments

* Update all Server comments

* Undo some commenting

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
2025-04-10 01:30:44 -04:00

30 lines
1.3 KiB
C#

using ModelContextProtocol.Server;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Provides extension methods for configuring MCP servers with dependency injection.
/// </summary>
public static class McpServerServiceCollectionExtensions
{
/// <summary>
/// Adds the Model Context Protocol (MCP) server to the service collection with default options.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the server to.</param>
/// <param name="configureOptions">Optional callback to configure the <see cref="McpServerOptions"/>.</param>
/// <returns>An <see cref="IMcpServerBuilder"/> that can be used to further configure the MCP server.</returns>
public static IMcpServerBuilder AddMcpServer(this IServiceCollection services, Action<McpServerOptions>? configureOptions = null)
{
services.AddOptions();
services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<McpServerOptions>, McpServerOptionsSetup>());
if (configureOptions is not null)
{
services.Configure(configureOptions);
}
return new DefaultMcpServerBuilder(services);
}
}