forked from modelcontextprotocol/csharp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMcpServerServiceCollectionExtensions.cs
More file actions
31 lines (27 loc) · 1.32 KB
/
McpServerServiceCollectionExtensions.cs
File metadata and controls
31 lines (27 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using ModelContextProtocol.AspNetCore.Configuration;
using ModelContextProtocol.Server;
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);
}
}