Docs edit pass (MCP.AspNetCore and a few other files) (#1034)
Co-authored-by: Stephen Toub <stoub@microsoft.com> Co-authored-by: Mike Kistler <mikekistler@microsoft.com>
This commit is contained in:
parent
6ed728d005
commit
af719fc576
11 changed files with 90 additions and 81 deletions
|
|
@ -1,7 +1,7 @@
|
|||
namespace ModelContextProtocol.AspNetCore.Authentication;
|
||||
|
||||
/// <summary>
|
||||
/// Default values used by MCP authentication.
|
||||
/// Represents default values used by MCP authentication.
|
||||
/// </summary>
|
||||
public static class McpAuthenticationDefaults
|
||||
{
|
||||
|
|
@ -14,4 +14,4 @@ public static class McpAuthenticationDefaults
|
|||
/// The default value used for authentication scheme display name.
|
||||
/// </summary>
|
||||
public const string DisplayName = "MCP Authentication";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
public class McpAuthenticationEvents
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the function that is invoked when resource metadata is requested.
|
||||
/// Gets or sets the function that's invoked when resource metadata is requested.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This function is called when a resource metadata request is made to the protected resource metadata endpoint.
|
||||
|
|
@ -14,4 +14,4 @@ public class McpAuthenticationEvents
|
|||
/// to provide the appropriate metadata for the current request.
|
||||
/// </remarks>
|
||||
public Func<ResourceMetadataRequestContext, Task> OnResourceMetadataRequest { get; set; } = context => Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ using System.Text.Encodings.Web;
|
|||
namespace ModelContextProtocol.AspNetCore.Authentication;
|
||||
|
||||
/// <summary>
|
||||
/// Authentication handler for MCP protocol that adds resource metadata to challenge responses
|
||||
/// Represents an authentication handler for MCP protocol that adds resource metadata to challenge responses
|
||||
/// and handles resource metadata endpoint requests.
|
||||
/// </summary>
|
||||
public class McpAuthenticationHandler : AuthenticationHandler<McpAuthenticationOptions>, IAuthenticationRequestHandler
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ using ModelContextProtocol.Authentication;
|
|||
namespace ModelContextProtocol.AspNetCore.Authentication;
|
||||
|
||||
/// <summary>
|
||||
/// Options for the MCP authentication handler.
|
||||
/// Represents options for the MCP authentication handler.
|
||||
/// </summary>
|
||||
public class McpAuthenticationOptions : AuthenticationSchemeOptions
|
||||
{
|
||||
|
|
@ -25,16 +25,16 @@ public class McpAuthenticationOptions : AuthenticationSchemeOptions
|
|||
/// Gets or sets the events used to handle authentication events.
|
||||
/// </summary>
|
||||
public new McpAuthenticationEvents Events
|
||||
{
|
||||
get => (McpAuthenticationEvents)base.Events!;
|
||||
{
|
||||
get => (McpAuthenticationEvents)base.Events!;
|
||||
set => base.Events = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The URI to the resource metadata document.
|
||||
/// Gets or sets the URI to the resource metadata document.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This URI will be included in the WWW-Authenticate header when a 401 response is returned.
|
||||
/// This URI is included in the WWW-Authenticate header when a 401 response is returned.
|
||||
/// </remarks>
|
||||
public Uri ResourceMetadataUri { get; set; }
|
||||
|
||||
|
|
@ -46,4 +46,4 @@ public class McpAuthenticationOptions : AuthenticationSchemeOptions
|
|||
/// supported scopes, and other information needed for clients to authenticate.
|
||||
/// </remarks>
|
||||
public ProtectedResourceMetadata? ResourceMetadata { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ using ModelContextProtocol.Authentication;
|
|||
namespace ModelContextProtocol.AspNetCore.Authentication;
|
||||
|
||||
/// <summary>
|
||||
/// Context for resource metadata request events.
|
||||
/// Represents the context for resource metadata request events.
|
||||
/// </summary>
|
||||
public class ResourceMetadataRequestContext : HandleRequestContext<McpAuthenticationOptions>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -13,13 +13,15 @@ public static class HttpMcpServerBuilderExtensions
|
|||
{
|
||||
/// <summary>
|
||||
/// Adds the services necessary for <see cref="M:McpEndpointRouteBuilderExtensions.MapMcp"/>
|
||||
/// to handle MCP requests and sessions using the MCP Streamable HTTP transport. For more information on configuring the underlying HTTP server
|
||||
/// to control things like port binding custom TLS certificates, see the <see href="https://learn.microsoft.com/aspnet/core/fundamentals/minimal-apis">Minimal APIs quick reference</see>.
|
||||
/// to handle MCP requests and sessions using the MCP Streamable HTTP transport.
|
||||
/// </summary>
|
||||
/// <param name="builder">The builder instance.</param>
|
||||
/// <param name="configureOptions">Configures options for the Streamable HTTP transport. This allows configuring per-session
|
||||
/// <see cref="McpServerOptions"/> and running logic before and after a session.</param>
|
||||
/// <returns>The builder provided in <paramref name="builder"/>.</returns>
|
||||
/// <remarks>For more information on configuring the underlying HTTP server
|
||||
/// to control things like port binding custom TLS certificates, see the <see href="https://learn.microsoft.com/aspnet/core/fundamentals/minimal-apis">Minimal APIs quick reference</see>.
|
||||
/// </remarks>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
|
||||
public static IMcpServerBuilder WithHttpTransport(this IMcpServerBuilder builder, Action<HttpServerTransportOptions>? configureOptions = null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,10 +4,13 @@ using ModelContextProtocol.Server;
|
|||
namespace ModelContextProtocol.AspNetCore;
|
||||
|
||||
/// <summary>
|
||||
/// Configuration options for <see cref="M:McpEndpointRouteBuilderExtensions.MapMcp"/>.
|
||||
/// Represents configuration options for <see cref="M:McpEndpointRouteBuilderExtensions.MapMcp"/>,
|
||||
/// which implements the Streaming HTTP transport for the Model Context Protocol.
|
||||
/// See the protocol specification for details on the Streamable HTTP transport. <see href="https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#streamable-http"/>
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// For details on the Streamable HTTP transport, see the <see href="https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#streamable-http">protocol specification</see>.
|
||||
/// </remarks>
|
||||
public class HttpServerTransportOptions
|
||||
{
|
||||
/// <summary>
|
||||
|
|
@ -18,61 +21,71 @@ public class HttpServerTransportOptions
|
|||
|
||||
/// <summary>
|
||||
/// Gets or sets an optional asynchronous callback for running new MCP sessions manually.
|
||||
/// This is useful for running logic before a sessions starts and after it completes.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This callback is useful for running logic before a sessions starts and after it completes.
|
||||
/// </remarks>
|
||||
public Func<HttpContext, McpServer, CancellationToken, Task>? RunSessionHandler { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether the server should run in a stateless mode which does not track state between requests
|
||||
/// Gets or sets a value that indicates whether the server runs in a stateless mode that doesn't track state between requests,
|
||||
/// allowing for load balancing without session affinity.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <see langword="true"/> if the server runs in a stateless mode; <see langword="false"/> if the server tracks state between requests. The default is <see langword="false"/>.
|
||||
/// </value>
|
||||
/// <remarks>
|
||||
/// If <see langword="true"/>, <see cref="McpSession.SessionId"/> will be null, and the "MCP-Session-Id" header will not be used,
|
||||
/// the <see cref="RunSessionHandler"/> will be called once for for each request, and the "/sse" endpoint will be disabled.
|
||||
/// Unsolicited server-to-client messages and all server-to-client requests are also unsupported, because any responses
|
||||
/// may arrive at another ASP.NET Core application process.
|
||||
/// might arrive at another ASP.NET Core application process.
|
||||
/// Client sampling and roots capabilities are also disabled in stateless mode, because the server cannot make requests.
|
||||
/// Defaults to <see langword="false"/>.
|
||||
/// </remarks>
|
||||
public bool Stateless { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether the server should use a single execution context for the entire session.
|
||||
/// Gets or sets a value that indicates whether the server uses a single execution context for the entire session.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <see langword="true"/> if the server uses a single execution context for the entire session; otherwise, <see langword="false"/>. The default is <see langword="false"/>.
|
||||
/// </value>
|
||||
/// <remarks>
|
||||
/// If <see langword="false"/>, handlers like tools get called with the <see cref="ExecutionContext"/>
|
||||
/// belonging to the corresponding HTTP request which can change throughout the MCP session.
|
||||
/// belonging to the corresponding HTTP request, which can change throughout the MCP session.
|
||||
/// If <see langword="true"/>, handlers will get called with the same <see cref="ExecutionContext"/>
|
||||
/// used to call <see cref="ConfigureSessionOptions" /> and <see cref="RunSessionHandler"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Enabling a per-session <see cref="ExecutionContext"/> can be useful for setting <see cref="AsyncLocal{T}"/> variables
|
||||
/// that persist for the entire session, but it prevents you from using IHttpContextAccessor in handlers.
|
||||
/// Defaults to <see langword="false"/>.
|
||||
/// </remarks>
|
||||
public bool PerSessionExecutionContext { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the duration of time the server will wait between any active requests before timing out an MCP session.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The amount of time the server waits between any active requests before timing out an MCP session. The default is 2 hours.
|
||||
/// </value>
|
||||
/// <remarks>
|
||||
/// This is checked in background every 5 seconds. A client trying to resume a session will receive a 404 status code
|
||||
/// This value is checked in the background every 5 seconds. A client trying to resume a session will receive a 404 status code
|
||||
/// and should restart their session. A client can keep their session open by keeping a GET request open.
|
||||
/// Defaults to 2 hours.
|
||||
/// </remarks>
|
||||
public TimeSpan IdleTimeout { get; set; } = TimeSpan.FromHours(2);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets maximum number of idle sessions to track in memory. This is used to limit the number of sessions that can be idle at once.
|
||||
/// Gets or sets maximum number of idle sessions to track in memory. This value is used to limit the number of sessions that can be idle at once.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The maximum number of idle sessions to track in memory. The default is 10,000 sessions.
|
||||
/// </value>
|
||||
/// <remarks>
|
||||
/// Past this limit, the server will log a critical error and terminate the oldest idle sessions even if they have not reached
|
||||
/// their <see cref="IdleTimeout"/> until the idle session count is below this limit. Clients that keep their session open by
|
||||
/// keeping a GET request open will not count towards this limit.
|
||||
/// Defaults to 10,000 sessions.
|
||||
/// Past this limit, the server logs a critical error and terminates the oldest idle sessions, even if they have not reached
|
||||
/// their <see cref="IdleTimeout"/>, until the idle session count is below this limit. Clients that keep their session open by
|
||||
/// keeping a GET request open don't count towards this limit.
|
||||
/// </remarks>
|
||||
public int MaxIdleSessionCount { get; set; } = 10_000;
|
||||
|
||||
/// <summary>
|
||||
/// Used for testing the <see cref="IdleTimeout"/>.
|
||||
/// Gets or sets the time provider that's used for testing the <see cref="IdleTimeout"/>.
|
||||
/// </summary>
|
||||
public TimeProvider TimeProvider { get; set; } = TimeProvider.System;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,12 +15,14 @@ public static class McpEndpointRouteBuilderExtensions
|
|||
{
|
||||
/// <summary>
|
||||
/// Sets up endpoints for handling MCP Streamable HTTP transport.
|
||||
/// See <see href="https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#streamable-http">the 2025-06-18 protocol specification</see> for details about the Streamable HTTP transport.
|
||||
/// Also maps legacy SSE endpoints for backward compatibility at the path "/sse" and "/message". <see href="https://modelcontextprotocol.io/specification/2024-11-05/basic/transports#http-with-sse">the 2024-11-05 protocol specification</see> for details about the HTTP with SSE transport.
|
||||
/// </summary>
|
||||
/// <param name="endpoints">The web application to attach MCP HTTP endpoints.</param>
|
||||
/// <param name="pattern">The route pattern prefix to map to.</param>
|
||||
/// <returns>Returns a builder for configuring additional endpoint conventions like authorization policies.</returns>
|
||||
/// <remarks>
|
||||
/// For details about the Streamable HTTP transport, see the <see href="https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#streamable-http">2025-06-18 protocol specification</see>.
|
||||
/// This method also maps legacy SSE endpoints for backward compatibility at the path "/sse" and "/message". For details about the HTTP with SSE transport, see the <see href="https://modelcontextprotocol.io/specification/2024-11-05/basic/transports#http-with-sse">2024-11-05 protocol specification</see>.
|
||||
/// </remarks>
|
||||
public static IEndpointConventionBuilder MapMcp(this IEndpointRouteBuilder endpoints, [StringSyntax("Route")] string pattern = "")
|
||||
{
|
||||
var streamableHttpHandler = endpoints.ServiceProvider.GetService<StreamableHttpHandler>() ??
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ public static partial class McpServerBuilderExtensions
|
|||
#region WithTools
|
||||
private const string WithToolsRequiresUnreferencedCodeMessage =
|
||||
$"The non-generic {nameof(WithTools)} and {nameof(WithToolsFromAssembly)} methods require dynamic lookup of method metadata" +
|
||||
$"and may not work in Native AOT. Use the generic {nameof(WithTools)} method instead.";
|
||||
$"and might not work in Native AOT. Use the generic {nameof(WithTools)} method instead.";
|
||||
|
||||
/// <summary>Adds <see cref="McpServerTool"/> instances to the service collection backing <paramref name="builder"/>.</summary>
|
||||
/// <typeparam name="TToolType">The tool type.</typeparam>
|
||||
|
|
@ -29,7 +29,7 @@ public static partial class McpServerBuilderExtensions
|
|||
/// <remarks>
|
||||
/// This method discovers all instance and static methods (public and non-public) on the specified <typeparamref name="TToolType"/>
|
||||
/// type, where the methods are attributed as <see cref="McpServerToolAttribute"/>, and adds an <see cref="McpServerTool"/>
|
||||
/// instance for each. For instance methods, an instance will be constructed for each invocation of the tool.
|
||||
/// instance for each. For instance methods, an instance is constructed for each invocation of the tool.
|
||||
/// </remarks>
|
||||
public static IMcpServerBuilder WithTools<[DynamicallyAccessedMembers(
|
||||
DynamicallyAccessedMemberTypes.PublicMethods |
|
||||
|
|
@ -68,7 +68,7 @@ public static partial class McpServerBuilderExtensions
|
|||
/// </para>
|
||||
/// <para>
|
||||
/// However, if <typeparamref name="TToolType"/> is itself an <see cref="IEnumerable{T}"/> of <see cref="McpServerTool"/>,
|
||||
/// this method will register those tools directly without scanning for methods on <typeparamref name="TToolType"/>.
|
||||
/// this method registers those tools directly without scanning for methods on <typeparamref name="TToolType"/>.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static IMcpServerBuilder WithTools<[DynamicallyAccessedMembers(
|
||||
|
|
@ -104,8 +104,7 @@ public static partial class McpServerBuilderExtensions
|
|||
/// <param name="builder">The builder instance.</param>
|
||||
/// <param name="tools">The <see cref="McpServerTool"/> instances to add to the server.</param>
|
||||
/// <returns>The builder provided in <paramref name="builder"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="tools"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> or <paramref name="tools"/> is <see langword="null"/>.</exception>
|
||||
public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, IEnumerable<McpServerTool> tools)
|
||||
{
|
||||
Throw.IfNull(builder);
|
||||
|
|
@ -127,12 +126,11 @@ public static partial class McpServerBuilderExtensions
|
|||
/// <param name="toolTypes">Types with <see cref="McpServerToolAttribute"/>-attributed methods to add as tools to the server.</param>
|
||||
/// <param name="serializerOptions">The serializer options governing tool parameter marshalling.</param>
|
||||
/// <returns>The builder provided in <paramref name="builder"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="toolTypes"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> or <paramref name="toolTypes"/> is <see langword="null"/>.</exception>
|
||||
/// <remarks>
|
||||
/// This method discovers all instance and static methods (public and non-public) on the specified <paramref name="toolTypes"/>
|
||||
/// types, where the methods are attributed as <see cref="McpServerToolAttribute"/>, and adds an <see cref="McpServerTool"/>
|
||||
/// instance for each. For instance methods, an instance will be constructed for each invocation of the tool.
|
||||
/// instance for each. For instance methods, an instance is constructed for each invocation of the tool.
|
||||
/// </remarks>
|
||||
[RequiresUnreferencedCode(WithToolsRequiresUnreferencedCodeMessage)]
|
||||
public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, IEnumerable<Type> toolTypes, JsonSerializerOptions? serializerOptions = null)
|
||||
|
|
@ -149,8 +147,8 @@ public static partial class McpServerBuilderExtensions
|
|||
if (toolMethod.GetCustomAttribute<McpServerToolAttribute>() is not null)
|
||||
{
|
||||
builder.Services.AddSingleton((Func<IServiceProvider, McpServerTool>)(toolMethod.IsStatic ?
|
||||
services => McpServerTool.Create(toolMethod, options: new() { Services = services , SerializerOptions = serializerOptions }) :
|
||||
services => McpServerTool.Create(toolMethod, r => CreateTarget(r.Services, toolType), new() { Services = services , SerializerOptions = serializerOptions })));
|
||||
services => McpServerTool.Create(toolMethod, options: new() { Services = services, SerializerOptions = serializerOptions }) :
|
||||
services => McpServerTool.Create(toolMethod, r => CreateTarget(r.Services, toolType), new() { Services = services, SerializerOptions = serializerOptions })));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -164,7 +162,7 @@ public static partial class McpServerBuilderExtensions
|
|||
/// </summary>
|
||||
/// <param name="builder">The builder instance.</param>
|
||||
/// <param name="serializerOptions">The serializer options governing tool parameter marshalling.</param>
|
||||
/// <param name="toolAssembly">The assembly to load the types from. If <see langword="null"/>, the calling assembly will be used.</param>
|
||||
/// <param name="toolAssembly">The assembly to load the types from. If <see langword="null"/>, the calling assembly is used.</param>
|
||||
/// <returns>The builder provided in <paramref name="builder"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
|
||||
/// <remarks>
|
||||
|
|
@ -176,14 +174,14 @@ public static partial class McpServerBuilderExtensions
|
|||
/// </para>
|
||||
/// <para>
|
||||
/// The method automatically handles both static and instance methods. For instance methods, a new instance
|
||||
/// of the containing class will be constructed for each invocation of the tool.
|
||||
/// of the containing class is constructed for each invocation of the tool.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Tools registered through this method can be discovered by clients using the <c>list_tools</c> request
|
||||
/// and invoked using the <c>call_tool</c> request.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Note that this method performs reflection at runtime and may not work in Native AOT scenarios. For
|
||||
/// Note that this method performs reflection at runtime and might not work in Native AOT scenarios. For
|
||||
/// Native AOT compatibility, consider using the generic <see cref="M:WithTools"/> method instead.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
|
|
@ -205,7 +203,7 @@ public static partial class McpServerBuilderExtensions
|
|||
#region WithPrompts
|
||||
private const string WithPromptsRequiresUnreferencedCodeMessage =
|
||||
$"The non-generic {nameof(WithPrompts)} and {nameof(WithPromptsFromAssembly)} methods require dynamic lookup of method metadata" +
|
||||
$"and may not work in Native AOT. Use the generic {nameof(WithPrompts)} method instead.";
|
||||
$"and might not work in Native AOT. Use the generic {nameof(WithPrompts)} method instead.";
|
||||
|
||||
/// <summary>Adds <see cref="McpServerPrompt"/> instances to the service collection backing <paramref name="builder"/>.</summary>
|
||||
/// <typeparam name="TPromptType">The prompt type.</typeparam>
|
||||
|
|
@ -216,7 +214,7 @@ public static partial class McpServerBuilderExtensions
|
|||
/// <remarks>
|
||||
/// This method discovers all instance and static methods (public and non-public) on the specified <typeparamref name="TPromptType"/>
|
||||
/// type, where the methods are attributed as <see cref="McpServerPromptAttribute"/>, and adds an <see cref="McpServerPrompt"/>
|
||||
/// instance for each. For instance methods, an instance will be constructed for each invocation of the prompt.
|
||||
/// instance for each. For instance methods, an instance is constructed for each invocation of the prompt.
|
||||
/// </remarks>
|
||||
public static IMcpServerBuilder WithPrompts<[DynamicallyAccessedMembers(
|
||||
DynamicallyAccessedMemberTypes.PublicMethods |
|
||||
|
|
@ -255,7 +253,7 @@ public static partial class McpServerBuilderExtensions
|
|||
/// </para>
|
||||
/// <para>
|
||||
/// However, if <typeparamref name="TPromptType"/> is itself an <see cref="IEnumerable{T}"/> of <see cref="McpServerPrompt"/>,
|
||||
/// this method will register those prompts directly without scanning for methods on <typeparamref name="TPromptType"/>.
|
||||
/// this method registers those prompts directly without scanning for methods on <typeparamref name="TPromptType"/>.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static IMcpServerBuilder WithPrompts<[DynamicallyAccessedMembers(
|
||||
|
|
@ -288,8 +286,7 @@ public static partial class McpServerBuilderExtensions
|
|||
/// <param name="builder">The builder instance.</param>
|
||||
/// <param name="prompts">The <see cref="McpServerPrompt"/> instances to add to the server.</param>
|
||||
/// <returns>The builder provided in <paramref name="builder"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="prompts"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> or <paramref name="prompts"/> is <see langword="null"/>.</exception>
|
||||
public static IMcpServerBuilder WithPrompts(this IMcpServerBuilder builder, IEnumerable<McpServerPrompt> prompts)
|
||||
{
|
||||
Throw.IfNull(builder);
|
||||
|
|
@ -311,12 +308,11 @@ public static partial class McpServerBuilderExtensions
|
|||
/// <param name="promptTypes">Types with marked methods to add as prompts to the server.</param>
|
||||
/// <param name="serializerOptions">The serializer options governing prompt parameter marshalling.</param>
|
||||
/// <returns>The builder provided in <paramref name="builder"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="promptTypes"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> or <paramref name="promptTypes"/> is <see langword="null"/>.</exception>
|
||||
/// <remarks>
|
||||
/// This method discovers all instance and static methods (public and non-public) on the specified <paramref name="promptTypes"/>
|
||||
/// types, where the methods are attributed as <see cref="McpServerPromptAttribute"/>, and adds an <see cref="McpServerPrompt"/>
|
||||
/// instance for each. For instance methods, an instance will be constructed for each invocation of the prompt.
|
||||
/// instance for each. For instance methods, an instance is constructed for each invocation of the prompt.
|
||||
/// </remarks>
|
||||
[RequiresUnreferencedCode(WithPromptsRequiresUnreferencedCodeMessage)]
|
||||
public static IMcpServerBuilder WithPrompts(this IMcpServerBuilder builder, IEnumerable<Type> promptTypes, JsonSerializerOptions? serializerOptions = null)
|
||||
|
|
@ -348,7 +344,7 @@ public static partial class McpServerBuilderExtensions
|
|||
/// </summary>
|
||||
/// <param name="builder">The builder instance.</param>
|
||||
/// <param name="serializerOptions">The serializer options governing prompt parameter marshalling.</param>
|
||||
/// <param name="promptAssembly">The assembly to load the types from. If <see langword="null"/>, the calling assembly will be used.</param>
|
||||
/// <param name="promptAssembly">The assembly to load the types from. If <see langword="null"/>, the calling assembly is used.</param>
|
||||
/// <returns>The builder provided in <paramref name="builder"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
|
||||
/// <remarks>
|
||||
|
|
@ -360,14 +356,14 @@ public static partial class McpServerBuilderExtensions
|
|||
/// </para>
|
||||
/// <para>
|
||||
/// The method automatically handles both static and instance methods. For instance methods, a new instance
|
||||
/// of the containing class will be constructed for each invocation of the prompt.
|
||||
/// of the containing class is constructed for each invocation of the prompt.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Prompts registered through this method can be discovered by clients using the <c>list_prompts</c> request
|
||||
/// and invoked using the <c>call_prompt</c> request.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Note that this method performs reflection at runtime and may not work in Native AOT scenarios. For
|
||||
/// Note that this method performs reflection at runtime and might not work in Native AOT scenarios. For
|
||||
/// Native AOT compatibility, consider using the generic <see cref="M:WithPrompts"/> method instead.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
|
|
@ -389,7 +385,7 @@ public static partial class McpServerBuilderExtensions
|
|||
#region WithResources
|
||||
private const string WithResourcesRequiresUnreferencedCodeMessage =
|
||||
$"The non-generic {nameof(WithResources)} and {nameof(WithResourcesFromAssembly)} methods require dynamic lookup of member metadata" +
|
||||
$"and may not work in Native AOT. Use the generic {nameof(WithResources)} method instead.";
|
||||
$"and might not work in Native AOT. Use the generic {nameof(WithResources)} method instead.";
|
||||
|
||||
/// <summary>Adds <see cref="McpServerResource"/> instances to the service collection backing <paramref name="builder"/>.</summary>
|
||||
/// <typeparam name="TResourceType">The resource type.</typeparam>
|
||||
|
|
@ -399,7 +395,7 @@ public static partial class McpServerBuilderExtensions
|
|||
/// <remarks>
|
||||
/// This method discovers all instance and static methods (public and non-public) on the specified <typeparamref name="TResourceType"/>
|
||||
/// type, where the members are attributed as <see cref="McpServerResourceAttribute"/>, and adds an <see cref="McpServerResource"/>
|
||||
/// instance for each. For instance members, an instance will be constructed for each invocation of the resource.
|
||||
/// instance for each. For instance members, an instance is constructed for each invocation of the resource.
|
||||
/// </remarks>
|
||||
public static IMcpServerBuilder WithResources<[DynamicallyAccessedMembers(
|
||||
DynamicallyAccessedMemberTypes.PublicMethods |
|
||||
|
|
@ -436,7 +432,7 @@ public static partial class McpServerBuilderExtensions
|
|||
/// </para>
|
||||
/// <para>
|
||||
/// However, if <typeparamref name="TResourceType"/> is itself an <see cref="IEnumerable{T}"/> of <see cref="McpServerResource"/>,
|
||||
/// this method will register those resources directly without scanning for methods on <typeparamref name="TResourceType"/>.
|
||||
/// this method registers those resources directly without scanning for methods on <typeparamref name="TResourceType"/>.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static IMcpServerBuilder WithResources<[DynamicallyAccessedMembers(
|
||||
|
|
@ -468,8 +464,7 @@ public static partial class McpServerBuilderExtensions
|
|||
/// <param name="builder">The builder instance.</param>
|
||||
/// <param name="resourceTemplates">The <see cref="McpServerResource"/> instances to add to the server.</param>
|
||||
/// <returns>The builder provided in <paramref name="builder"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="resourceTemplates"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> or <paramref name="resourceTemplates"/> is <see langword="null"/>.</exception>
|
||||
public static IMcpServerBuilder WithResources(this IMcpServerBuilder builder, IEnumerable<McpServerResource> resourceTemplates)
|
||||
{
|
||||
Throw.IfNull(builder);
|
||||
|
|
@ -490,12 +485,11 @@ public static partial class McpServerBuilderExtensions
|
|||
/// <param name="builder">The builder instance.</param>
|
||||
/// <param name="resourceTemplateTypes">Types with marked methods to add as resources to the server.</param>
|
||||
/// <returns>The builder provided in <paramref name="builder"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="resourceTemplateTypes"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> or <paramref name="resourceTemplateTypes"/> is <see langword="null"/>.</exception>
|
||||
/// <remarks>
|
||||
/// This method discovers all instance and static methods (public and non-public) on the specified <paramref name="resourceTemplateTypes"/>
|
||||
/// types, where the methods are attributed as <see cref="McpServerResourceAttribute"/>, and adds an <see cref="McpServerResource"/>
|
||||
/// instance for each. For instance methods, an instance will be constructed for each invocation of the resource.
|
||||
/// instance for each. For instance methods, an instance is constructed for each invocation of the resource.
|
||||
/// </remarks>
|
||||
[RequiresUnreferencedCode(WithResourcesRequiresUnreferencedCodeMessage)]
|
||||
public static IMcpServerBuilder WithResources(this IMcpServerBuilder builder, IEnumerable<Type> resourceTemplateTypes)
|
||||
|
|
@ -526,7 +520,7 @@ public static partial class McpServerBuilderExtensions
|
|||
/// Adds types marked with the <see cref="McpServerResourceTypeAttribute"/> attribute from the given assembly as resources to the server.
|
||||
/// </summary>
|
||||
/// <param name="builder">The builder instance.</param>
|
||||
/// <param name="resourceAssembly">The assembly to load the types from. If <see langword="null"/>, the calling assembly will be used.</param>
|
||||
/// <param name="resourceAssembly">The assembly to load the types from. If <see langword="null"/>, the calling assembly is used.</param>
|
||||
/// <returns>The builder provided in <paramref name="builder"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
|
||||
/// <remarks>
|
||||
|
|
@ -538,14 +532,14 @@ public static partial class McpServerBuilderExtensions
|
|||
/// </para>
|
||||
/// <para>
|
||||
/// The method automatically handles both static and instance members. For instance members, a new instance
|
||||
/// of the containing class will be constructed for each invocation of the resource.
|
||||
/// of the containing class is constructed for each invocation of the resource.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Resource templates registered through this method can be discovered by clients using the <c>list_resourceTemplates</c> request
|
||||
/// and invoked using the <c>read_resource</c> request.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Note that this method performs reflection at runtime and may not work in Native AOT scenarios. For
|
||||
/// Note that this method performs reflection at runtime and might not work in Native AOT scenarios. For
|
||||
/// Native AOT compatibility, consider using the generic <see cref="M:WithResources"/> method instead.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
|
|
@ -604,7 +598,7 @@ public static partial class McpServerBuilderExtensions
|
|||
/// <para>
|
||||
/// This handler is called when a client requests a list of available tools. It should return all tools
|
||||
/// that can be invoked through the server, including their names, descriptions, and parameter specifications.
|
||||
/// The handler can optionally support pagination via the cursor mechanism for large or dynamically-generated
|
||||
/// The handler can optionally support pagination via the cursor mechanism for large or dynamically generated
|
||||
/// tool collections.
|
||||
/// </para>
|
||||
/// <para>
|
||||
|
|
@ -824,7 +818,7 @@ public static partial class McpServerBuilderExtensions
|
|||
/// <para>
|
||||
/// When a client sends a <c>logging/setLevel</c> request, this handler will be invoked to process
|
||||
/// the requested level change. The server typically adjusts its internal logging level threshold
|
||||
/// and may begin sending log messages at or above the specified level to the client.
|
||||
/// and might begin sending log messages at or above the specified level to the client.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Regardless of whether a handler is provided, an <see cref="McpServer"/> should itself handle
|
||||
|
|
@ -1032,7 +1026,7 @@ public static partial class McpServerBuilderExtensions
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a filter to the subscribe to resources handler pipeline.
|
||||
/// Adds a filter to the subscribe-to-resources handler pipeline.
|
||||
/// </summary>
|
||||
/// <param name="builder">The builder instance.</param>
|
||||
/// <param name="filter">The filter function that wraps the handler.</param>
|
||||
|
|
@ -1059,7 +1053,7 @@ public static partial class McpServerBuilderExtensions
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a filter to the unsubscribe from resources handler pipeline.
|
||||
/// Adds a filter to the unsubscribe-from-resources handler pipeline.
|
||||
/// </summary>
|
||||
/// <param name="builder">The builder instance.</param>
|
||||
/// <param name="filter">The filter function that wraps the handler.</param>
|
||||
|
|
@ -1101,7 +1095,7 @@ public static partial class McpServerBuilderExtensions
|
|||
/// </para>
|
||||
/// <para>
|
||||
/// After handling a level change request, the server typically begins sending log messages
|
||||
/// at or above the specified level to the client as notifications/message notifications.
|
||||
/// at or above the specified level to the client as notifications or message notifications.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static IMcpServerBuilder AddSetLoggingLevelFilter(this IMcpServerBuilder builder, McpRequestFilter<SetLevelRequestParams, EmptyResult> filter)
|
||||
|
|
@ -1154,9 +1148,7 @@ public static partial class McpServerBuilderExtensions
|
|||
/// <param name="inputStream">The input <see cref="Stream"/> to use as standard input.</param>
|
||||
/// <param name="outputStream">The output <see cref="Stream"/> to use as standard output.</param>
|
||||
/// <returns>The builder provided in <paramref name="builder"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="inputStream"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="outputStream"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> or <paramref name="inputStream"/> or <paramref name="outputStream"/> is <see langword="null"/>.</exception>
|
||||
public static IMcpServerBuilder WithStreamServerTransport(
|
||||
this IMcpServerBuilder builder,
|
||||
Stream inputStream,
|
||||
|
|
|
|||
|
|
@ -8,9 +8,9 @@ namespace ModelContextProtocol;
|
|||
/// Configures the McpServerOptions using addition services from DI.
|
||||
/// </summary>
|
||||
/// <param name="serverHandlers">The server handlers configuration options.</param>
|
||||
/// <param name="serverTools">Tools individually registered.</param>
|
||||
/// <param name="serverPrompts">Prompts individually registered.</param>
|
||||
/// <param name="serverResources">Resources individually registered.</param>
|
||||
/// <param name="serverTools">The individually registered tools.</param>
|
||||
/// <param name="serverPrompts">The individually registered prompts.</param>
|
||||
/// <param name="serverResources">The individually registered resources.</param>
|
||||
internal sealed class McpServerOptionsSetup(
|
||||
IOptions<McpServerHandlers> serverHandlers,
|
||||
IEnumerable<McpServerTool> serverTools,
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ public static class McpServerServiceCollectionExtensions
|
|||
/// 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>
|
||||
/// <param name="configureOptions">An 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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue