Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ModelContextProtocol.Core/Client/McpClientHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace ModelContextProtocol.Client;
/// is done based on an ordinal, case-sensitive string comparison.
/// </para>
/// </remarks>
public class McpClientHandlers
public sealed class McpClientHandlers
{
/// <summary>Gets or sets notification handlers to register with the client.</summary>
/// <remarks>
Expand Down
24 changes: 20 additions & 4 deletions src/ModelContextProtocol.Core/Server/McpMessageFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace ModelContextProtocol.Server;
public sealed class McpMessageFilters
{
/// <summary>
/// Gets the filters for all incoming JSON-RPC messages.
/// Gets or sets the filters for all incoming JSON-RPC messages.
/// </summary>
/// <remarks>
/// <para>
Expand All @@ -21,10 +21,18 @@ public sealed class McpMessageFilters
/// the next handler in the pipeline, the default handlers will not be executed.
/// </para>
/// </remarks>
public IList<McpMessageFilter> IncomingFilters { get; } = [];
public IList<McpMessageFilter> IncomingFilters
{
get => field ??= [];
set
{
Throw.IfNull(value);
field = value;
}
}

/// <summary>
/// Gets the filters for all outgoing JSON-RPC messages.
/// Gets or sets the filters for all outgoing JSON-RPC messages.
/// </summary>
/// <remarks>
/// <para>
Expand All @@ -38,5 +46,13 @@ public sealed class McpMessageFilters
/// server-to-client messages.
/// </para>
/// </remarks>
public IList<McpMessageFilter> OutgoingFilters { get; } = [];
public IList<McpMessageFilter> OutgoingFilters
{
get => field ??= [];
set
{
Throw.IfNull(value);
field = value;
}
}
}
132 changes: 110 additions & 22 deletions src/ModelContextProtocol.Core/Server/McpRequestFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace ModelContextProtocol.Server;
public sealed class McpRequestFilters
{
/// <summary>
/// Gets the filters for the list-tools handler pipeline.
/// Gets or sets the filters for the list-tools handler pipeline.
/// </summary>
/// <remarks>
/// <para>
Expand All @@ -22,20 +22,36 @@ public sealed class McpRequestFilters
/// Tools from both sources will be combined when returning results to clients.
/// </para>
/// </remarks>
public IList<McpRequestFilter<ListToolsRequestParams, ListToolsResult>> ListToolsFilters { get; } = [];
public IList<McpRequestFilter<ListToolsRequestParams, ListToolsResult>> ListToolsFilters
{
get => field ??= [];
set
{
Throw.IfNull(value);
field = value;
}
}

/// <summary>
/// Gets the filters for the call-tool handler pipeline.
/// Gets or sets the filters for the call-tool handler pipeline.
/// </summary>
/// <remarks>
/// These filters wrap handlers that are invoked when a client makes a call to a tool that isn't found in the <see cref="McpServerTool"/> collection.
/// The filters can modify, log, or perform additional operations on requests and responses for
/// <see cref="RequestMethods.ToolsCall"/> requests. The handler should implement logic to execute the requested tool and return appropriate results.
/// </remarks>
public IList<McpRequestFilter<CallToolRequestParams, CallToolResult>> CallToolFilters { get; } = [];
public IList<McpRequestFilter<CallToolRequestParams, CallToolResult>> CallToolFilters
{
get => field ??= [];
set
{
Throw.IfNull(value);
field = value;
}
}

/// <summary>
/// Gets the filters for the list-prompts handler pipeline.
/// Gets or sets the filters for the list-prompts handler pipeline.
/// </summary>
/// <remarks>
/// <para>
Expand All @@ -49,63 +65,111 @@ public sealed class McpRequestFilters
/// Prompts from both sources will be combined when returning results to clients.
/// </para>
/// </remarks>
public IList<McpRequestFilter<ListPromptsRequestParams, ListPromptsResult>> ListPromptsFilters { get; } = [];
public IList<McpRequestFilter<ListPromptsRequestParams, ListPromptsResult>> ListPromptsFilters
{
get => field ??= [];
set
{
Throw.IfNull(value);
field = value;
}
}

/// <summary>
/// Gets the filters for the get-prompt handler pipeline.
/// Gets or sets the filters for the get-prompt handler pipeline.
/// </summary>
/// <remarks>
/// These filters wrap handlers that are invoked when a client requests details for a specific prompt that isn't found in the <see cref="McpServerPrompt"/> collection.
/// The filters can modify, log, or perform additional operations on requests and responses for
/// <see cref="RequestMethods.PromptsGet"/> requests. The handler should implement logic to fetch or generate the requested prompt and return appropriate results.
/// </remarks>
public IList<McpRequestFilter<GetPromptRequestParams, GetPromptResult>> GetPromptFilters { get; } = [];
public IList<McpRequestFilter<GetPromptRequestParams, GetPromptResult>> GetPromptFilters
{
get => field ??= [];
set
{
Throw.IfNull(value);
field = value;
}
}

/// <summary>
/// Gets the filters for the list-resource-templates handler pipeline.
/// Gets or sets the filters for the list-resource-templates handler pipeline.
/// </summary>
/// <remarks>
/// These filters wrap handlers that return a list of available resource templates when requested by a client.
/// The filters can modify, log, or perform additional operations on requests and responses for
/// <see cref="RequestMethods.ResourcesTemplatesList"/> requests. It supports pagination through the cursor mechanism,
/// where the client can make repeated calls with the cursor returned by the previous call to retrieve more resource templates.
/// </remarks>
public IList<McpRequestFilter<ListResourceTemplatesRequestParams, ListResourceTemplatesResult>> ListResourceTemplatesFilters { get; } = [];
public IList<McpRequestFilter<ListResourceTemplatesRequestParams, ListResourceTemplatesResult>> ListResourceTemplatesFilters
{
get => field ??= [];
set
{
Throw.IfNull(value);
field = value;
}
}

/// <summary>
/// Gets the filters for the list-resources handler pipeline.
/// Gets or sets the filters for the list-resources handler pipeline.
/// </summary>
/// <remarks>
/// These filters wrap handlers that return a list of available resources when requested by a client.
/// The filters can modify, log, or perform additional operations on requests and responses for
/// <see cref="RequestMethods.ResourcesList"/> requests. It supports pagination through the cursor mechanism,
/// where the client can make repeated calls with the cursor returned by the previous call to retrieve more resources.
/// </remarks>
public IList<McpRequestFilter<ListResourcesRequestParams, ListResourcesResult>> ListResourcesFilters { get; } = [];
public IList<McpRequestFilter<ListResourcesRequestParams, ListResourcesResult>> ListResourcesFilters
{
get => field ??= [];
set
{
Throw.IfNull(value);
field = value;
}
}

/// <summary>
/// Gets the filters for the read-resource handler pipeline.
/// Gets or sets the filters for the read-resource handler pipeline.
/// </summary>
/// <remarks>
/// These filters wrap handlers that are invoked when a client requests the content of a specific resource identified by its URI.
/// The filters can modify, log, or perform additional operations on requests and responses for
/// <see cref="RequestMethods.ResourcesRead"/> requests. The handler should implement logic to locate and retrieve the requested resource.
/// </remarks>
public IList<McpRequestFilter<ReadResourceRequestParams, ReadResourceResult>> ReadResourceFilters { get; } = [];
public IList<McpRequestFilter<ReadResourceRequestParams, ReadResourceResult>> ReadResourceFilters
{
get => field ??= [];
set
{
Throw.IfNull(value);
field = value;
}
}

/// <summary>
/// Gets the filters for the complete-handler pipeline.
/// Gets or sets the filters for the complete-handler pipeline.
/// </summary>
/// <remarks>
/// These filters wrap handlers that provide auto-completion suggestions for prompt arguments or resource references in the Model Context Protocol.
/// The filters can modify, log, or perform additional operations on requests and responses for
/// <see cref="RequestMethods.CompletionComplete"/> requests. The handler processes auto-completion requests, returning a list of suggestions based on the
/// reference type and current argument value.
/// </remarks>
public IList<McpRequestFilter<CompleteRequestParams, CompleteResult>> CompleteFilters { get; } = [];
public IList<McpRequestFilter<CompleteRequestParams, CompleteResult>> CompleteFilters
{
get => field ??= [];
set
{
Throw.IfNull(value);
field = value;
}
}

/// <summary>
/// Gets the filters for the subscribe-to-resources handler pipeline.
/// Gets or sets the filters for the subscribe-to-resources handler pipeline.
/// </summary>
/// <remarks>
/// <para>
Expand All @@ -119,10 +183,18 @@ public sealed class McpRequestFilters
/// whenever a relevant resource is created, updated, or deleted.
/// </para>
/// </remarks>
public IList<McpRequestFilter<SubscribeRequestParams, EmptyResult>> SubscribeToResourcesFilters { get; } = [];
public IList<McpRequestFilter<SubscribeRequestParams, EmptyResult>> SubscribeToResourcesFilters
{
get => field ??= [];
set
{
Throw.IfNull(value);
field = value;
}
}

/// <summary>
/// Gets the filters for the unsubscribe-from-resources handler pipeline.
/// Gets or sets the filters for the unsubscribe-from-resources handler pipeline.
/// </summary>
/// <remarks>
/// <para>
Expand All @@ -136,10 +208,18 @@ public sealed class McpRequestFilters
/// to the client for the specified resources.
/// </para>
/// </remarks>
public IList<McpRequestFilter<UnsubscribeRequestParams, EmptyResult>> UnsubscribeFromResourcesFilters { get; } = [];
public IList<McpRequestFilter<UnsubscribeRequestParams, EmptyResult>> UnsubscribeFromResourcesFilters
{
get => field ??= [];
set
{
Throw.IfNull(value);
field = value;
}
}

/// <summary>
/// Gets the filters for the set-logging-level handler pipeline.
/// Gets or sets the filters for the set-logging-level handler pipeline.
/// </summary>
/// <remarks>
/// <para>
Expand All @@ -153,5 +233,13 @@ public sealed class McpRequestFilters
/// at or above the specified level to the client as notifications/message notifications.
/// </para>
/// </remarks>
public IList<McpRequestFilter<SetLevelRequestParams, EmptyResult>> SetLoggingLevelFilters { get; } = [];
public IList<McpRequestFilter<SetLevelRequestParams, EmptyResult>> SetLoggingLevelFilters
{
get => field ??= [];
set
{
Throw.IfNull(value);
field = value;
}
}
}
24 changes: 20 additions & 4 deletions src/ModelContextProtocol.Core/Server/McpServerFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@ namespace ModelContextProtocol.Server;
public sealed class McpServerFilters
{
/// <summary>
/// Gets the filters for incoming and outgoing JSON-RPC messages.
/// Gets or sets the filters for incoming and outgoing JSON-RPC messages.
/// </summary>
public McpMessageFilters Message { get; } = new();
public McpMessageFilters Message
{
get => field ??= new();
set
{
Throw.IfNull(value);
field = value;
}
}

/// <summary>
/// Gets the filters for request-specific MCP handler pipelines.
/// Gets or sets the filters for request-specific MCP handler pipelines.
/// </summary>
public McpRequestFilters Request { get; } = new();
public McpRequestFilters Request
{
get => field ??= new();
set
{
Throw.IfNull(value);
field = value;
}
}
}
22 changes: 19 additions & 3 deletions src/ModelContextProtocol.Core/Server/McpServerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,33 @@ public sealed class McpServerOptions
/// <summary>
/// Gets or sets the container of handlers used by the server for processing protocol messages.
/// </summary>
public McpServerHandlers Handlers { get; } = new();
public McpServerHandlers Handlers
{
get => field ??= new();
set
{
Throw.IfNull(value);
field = value;
}
}

/// <summary>
/// Gets the filter collections for MCP server handlers.
/// Gets or sets the filter collections for MCP server handlers.
/// </summary>
/// <remarks>
/// This property provides access to filter collections that can be used to modify the behavior
/// of various MCP server handlers. The first filter added is the outermost (first to execute),
/// and each subsequent filter wraps closer to the handler.
/// </remarks>
public McpServerFilters Filters { get; } = new();
public McpServerFilters Filters
{
get => field ??= new();
set
{
Throw.IfNull(value);
field = value;
}
}

/// <summary>
/// Gets or sets a collection of tools served by the server.
Expand Down
Loading