Class McpDocumentFilter
- Assembly
- Codebelt.Extensions.Swashbuckle.AspNetCore.ModelContextProtocol.dll
A Swashbuckle IDocumentFilter that injects the MCP Streamable HTTP transport endpoint (and optionally the legacy SSE endpoints) into the generated OpenAPI document.
public class McpDocumentFilter : DocumentFilter<McpDocumentOptions>, IDocumentFilter, IConfigurable<McpDocumentOptions>
- Inheritance
-
McpDocumentFilter
- Implements
- Inherited Members
Examples
To add MCP server documentation to your OpenAPI specification, you can either use the AddMcpServer extension method (recommended for standard setup), or directly instantiate and configure McpDocumentFilter with custom options when you need fine-grained control over the filter.
using Codebelt.Extensions.Swashbuckle.AspNetCore.ModelContextProtocol;
using Microsoft.OpenApi;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace YourApp.Configuration;
/// <summary>
/// Example showing how to use McpDocumentFilter directly.
/// </summary>
public class McpDocumentFilterExample
{
public void ConfigureMcp()
{
// Create custom MCP options
var mcpOptions = new McpDocumentOptions
{
Pattern = "/api/mcp",
TagName = "AI Server",
IncludeTools = true,
EnableLegacySse = false
};
// Instantiate the document filter directly
var mcpFilter = new McpDocumentFilter(mcpOptions);
// Register the filter in Swagger configuration
var swaggerOptions = new SwaggerGenOptions();
swaggerOptions.DocumentFilterDescriptors.Add(
new FilterDescriptor
{
Type = typeof(McpDocumentFilter),
Arguments = new object[] { mcpOptions }
}
);
}
}
When McpDocumentFilter is applied during OpenAPI document generation, it injects the MCP endpoint paths and operations. If tool discovery is enabled, the filter automatically discovers and documents available MCP tools.
Constructors
McpDocumentFilter(McpDocumentOptions)
Initializes a new instance of the McpDocumentFilter class.
public McpDocumentFilter(McpDocumentOptions options)
Parameters
optionsMcpDocumentOptionsThe options that control which document and routes are documented.
Methods
Apply(OpenApiDocument, DocumentFilterContext)
Applies post-processing to the swaggerDoc.
public override void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
Parameters
swaggerDocOpenApiDocumentThe OpenApiDocument to modify.
contextDocumentFilterContextThe DocumentFilterContext that provides additional context.
Remarks
Injects the MCP Streamable HTTP transport endpoint (and optionally the legacy SSE endpoints) into the generated OpenAPI document.