### Complete Setup with Tool Definitions Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/McpSwaggerGenOptionsExtensions.md Provides a complete setup example including defining MCP tools using attributes and configuring both RESTful Swagger and MCP Server. ```APIDOC ## POST /mcp ### Description Streamable HTTP endpoint with comprehensive tool definitions and integration with RESTful Swagger. ### Method POST ### Endpoint /mcp ### Parameters None ### Request Body None ### Response #### Success Response (200) - None (Streamable endpoint) #### Response Example None ## POST /mcp/searchProducts ### Description Searches products by query and optional category. ### Method POST ### Endpoint /mcp/searchProducts ### Parameters #### Request Body - **query** (string) - Required - The search query. - **category** (string) - Optional - The product category. ### Response #### Success Response (200) - **List** - A list of products matching the search criteria. #### Response Example ```json [ { "productId": "123", "name": "Example Product", "price": 19.99 } ] ``` ## POST /mcp/get_product_details ### Description Gets detailed information about a specific product. ### Method POST ### Endpoint /mcp/get_product_details ### Parameters #### Request Body - **productId** (string) - Required - The ID of the product. ### Response #### Success Response (200) - **Product** - Detailed information about the product. #### Response Example ```json { "productId": "123", "name": "Example Product", "price": 19.99, "description": "This is a detailed description." } ``` ## POST /mcp/getCategories ### Description Lists all available product categories. ### Method POST ### Endpoint /mcp/getCategories ### Parameters None ### Request Body None ### Response #### Success Response (200) - **List** - A list of available product categories. #### Response Example ```json [ "Electronics", "Books", "Clothing" ] ``` ``` -------------------------------- ### Complete Setup with MCP Tools and Endpoint Mapping Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/McpSwaggerGenOptionsExtensions.md A complete setup example including defining MCP tools with attributes, configuring services with versioning, security, RestfulSwagger, and the MCP server, and mapping the MCP endpoint in the application pipeline. ```csharp // Define MCP tools [McpServerToolType] public class ProductTools { [McpServerTool] [Description("Search products by query and optional category")] public static async Task> SearchProducts( string query, string category = null) { // Implementation } [McpServerTool(Name = "get_product_details")] [Description("Get detailed information about a specific product")] public static async Task GetProductDetails(string productId) { // Implementation } [McpServerTool] [Description("List all available product categories")] public static async Task> GetCategories() { // Implementation } } public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddEndpointsApiExplorer(); services.AddRestfulApiVersioning(o => { o.Conventions.Controller() .HasApiVersion(new ApiVersion(1, 0)); }); services.AddSwaggerGen(o => { o.AddXApiKeySecurity(); o.AddJwtBearerSecurity(); }); services.AddRestfulSwagger(o => { o.OpenApiInfo.Title = "Product API with AI Integration"; o.OpenApiInfo.Description = "RESTful product service with MCP tools for AI agents"; o.XmlDocumentations.AddFromBaseDirectory(typeof(Program)); }); // Add MCP endpoint documentation services.Configure(swaggerOpts => { swaggerOpts.AddMcpServer(mcp => { mcp.Pattern = "/mcp"; mcp.TagName = "AI Integration"; mcp.IncludeTools = true; }); }); } public void Configure(IApplicationBuilder app) { app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); // MapMcp must be called to register the actual endpoint endpoints.MapMcp(); }); app.UseSwagger(); app.UseSwaggerUI(); app.UseRestfulApiVersioning(); } ``` -------------------------------- ### Complete McpDocumentFilter Setup with Tools Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/McpDocumentFilter.md Demonstrates a full setup including defining MCP server tools, configuring services for API versioning and Swagger, and setting up the application pipeline. ```csharp [McpServerToolType] public class ProductTools { [McpServerTool] [Description("Search for products by name or category")] public static async Task> SearchProducts( string query, string category = null) { // Implementation } [McpServerTool] [Description("Get detailed product information")] public static async Task GetProductDetails(string productId) { // Implementation } } public void ConfigureServices(IServiceCollection services) { services.AddRestfulApiVersioning(o => { o.Conventions.Controller() .HasApiVersion(new ApiVersion(1, 0)); }); services.AddRestfulSwagger(o => { o.OpenApiInfo.Title = "Product API with MCP"; }); services.Configure(o => { o.AddMcpServer(mcp => { mcp.Pattern = "/mcp"; mcp.TagName = "MCP"; mcp.IncludeTools = true; }); }); } public void Configure(IApplicationBuilder app) { app.UseRouting(); app.UseEndpoints(endpoints => endpoints.MapControllers()); app.UseSwagger(); app.UseSwaggerUI(); } ``` -------------------------------- ### Basic McpDocumentFilter Setup Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/McpDocumentFilter.md Use this snippet for a straightforward integration of McpDocumentFilter with default settings. ```csharp services.AddSwaggerGen(o => { o.AddMcpServer(); }); ``` -------------------------------- ### Set User-Agent Example Value Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/UserAgentDocumentOptions.md Gets or sets the example value to associate with the User-Agent field. This provides a concrete example shown in API documentation and Swagger/OpenAPI tools. ```csharp public string Example { get; set; } ``` -------------------------------- ### Internal Configuration Classes Example Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/types.md Examples of internal configuration classes that implement IConfigureOptions. These are automatically registered and should not be directly instantiated. ```csharp public class ConfigureSwaggerGenOptions : Configurable, IConfigureOptions public class ConfigureSwaggerUIOptions : IConfigureOptions ``` -------------------------------- ### Versioned Endpoint Example Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/endpoints.md Demonstrates a standard RESTful GET endpoint for retrieving a product by its ID, including versioning and various security and response scenarios. ```APIDOC ## GET /api/products/{id} (v1.0) ### Description Standard RESTful GET endpoint to retrieve a product by its identifier. This documentation is automatically generated when using `AddRestfulSwagger()` and includes XML documentation, schemas, and security requirements. ### Method GET ### Endpoint `/api/products/{id}` ### Parameters #### Path Parameters - **id** (integer) - Required - Product identifier #### Query Parameters None #### Request Headers - **User-Agent** (string) - Optional - Client identifier - **Authorization** (string) - Optional - Bearer token (if JWT security enabled) - **X-Api-Key** (string) - Optional - API key (if API key security enabled) ### Request Body None ### Response #### Success Response (200 OK) - **id** (integer) - Product identifier - **name** (string) - Product name - **description** (string) - Product description - **price** (number) - Product price - **inStock** (boolean) - Availability status #### Response Example ```json { "id": 1, "name": "Product Name", "description": "Product description", "price": 99.99, "inStock": true } ``` #### Error Response (404 Not Found) - **type** (string) - URI identifying the error type - **title** (string) - A short, human-readable summary of the error - **status** (integer) - The HTTP status code - **detail** (string) - A specific description of the error #### Response Example ```json { "type": "https://tools.ietf.org/html/rfc7231#section-6.5.4", "title": "Not Found", "status": 404, "detail": "Product with ID 999 was not found." } ``` #### Error Response (401 Unauthorized) Authentication required. #### Error Response (403 Forbidden) Insufficient permissions. ### Authentication Optional (Bearer token or X-Api-Key) ``` -------------------------------- ### Example Usage of RestfulSwaggerOptions Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/RestfulSwaggerOptions.md Demonstrates how to instantiate and configure RestfulSwaggerOptions, including setting OpenApiInfo, IncludeControllerXmlComments, and JsonSerializerOptionsFactory, and then using it with AddRestfulSwagger. ```csharp var options = new RestfulSwaggerOptions { OpenApiInfo = new OpenApiInfoOptions { Title = "Product API", Description = "API for managing products", Contact = new OpenApiContact { Email = "support@example.com" }, License = new OpenApiLicense { Name = "MIT" } }, IncludeControllerXmlComments = true, JsonSerializerOptionsFactory = sp => sp.GetRequiredService>().Value }; options.XmlDocumentations.AddFromBaseDirectory(typeof(Program)); options.XmlDocumentations.AddFromReferencePacks(typeof(object)); // Validate before use options.ValidateOptions(); // Use with AddRestfulSwagger services.AddRestfulSwagger(o => { o.OpenApiInfo = options.OpenApiInfo; o.XmlDocumentations = options.XmlDocumentations; o.IncludeControllerXmlComments = options.IncludeControllerXmlComments; o.Settings = options.Settings; o.JsonSerializerOptionsFactory = options.JsonSerializerOptionsFactory; }); ``` -------------------------------- ### RestfulSwaggerOptions Example Usage Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/RestfulSwaggerOptions.md Demonstrates how to instantiate and configure RestfulSwaggerOptions, and then use it with AddRestfulSwagger. ```APIDOC ## Example Usage ```csharp var options = new RestfulSwaggerOptions { OpenApiInfo = new OpenApiInfoOptions { Title = "Product API", Description = "API for managing products", Contact = new OpenApiContact { Email = "support@example.com" }, License = new OpenApiLicense { Name = "MIT" } }, IncludeControllerXmlComments = true, JsonSerializerOptionsFactory = sp => sp.GetRequiredService>().Value }; options.XmlDocumentations.AddFromBaseDirectory(typeof(Program)); options.XmlDocumentations.AddFromReferencePacks(typeof(object)); // Validate before use options.ValidateOptions(); // Use with AddRestfulSwagger services.AddRestfulSwagger(o => { o.OpenApiInfo = options.OpenApiInfo; o.XmlDocumentations = options.XmlDocumentations; o.IncludeControllerXmlComments = options.IncludeControllerXmlComments; o.Settings = options.Settings; o.JsonSerializerOptionsFactory = options.JsonSerializerOptionsFactory; }); ``` ``` -------------------------------- ### AddByFilename Example Usage Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/XPathDocumentExtensions.md Illustrates the usage of the AddByFilename method to add XML documentation from specified file paths. It includes examples of valid paths and a non-existent path which is silently ignored. ```csharp var docs = new List(); docs.AddByFilename("/usr/share/xml/MyApp.xml"); docs.AddByFilename("./Documentation/MyLibrary.xml"); // File doesn't exist - silently skipped docs.AddByFilename("/nonexistent/path.xml"); ``` -------------------------------- ### AddRestfulSwagger Configuration Example Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/ConfigureSwaggerGenOptions.md Demonstrates how to configure API versioning and Swagger options using AddRestfulApiVersioning and AddRestfulSwagger. This setup automatically triggers ConfigureSwaggerGenOptions. ```csharp services.AddRestfulApiVersioning(o => { o.Conventions.Controller().HasApiVersion(new ApiVersion(1, 0)); o.Conventions.Controller().HasApiVersion(new ApiVersion(2, 0)); }); services.AddRestfulSwagger(o => { o.OpenApiInfo.Title = "Product API"; o.OpenApiInfo.Description = "Product management service"; o.OpenApiInfo.Contact = new OpenApiContact { Email = "support@example.com" }; o.XmlDocumentations.AddFromBaseDirectory(typeof(Program)); }); // ConfigureSwaggerGenOptions is now automatically registered and will: // 1. Discover versions 1.0 and 2.0 // 2. Create two Swagger docs (v1 and v2) // 3. Apply title and description to both // 4. Register XML documentation for both versions ``` -------------------------------- ### Basic Setup for API Versioning and Swagger Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/README.md Configure services and the application pipeline for API versioning and Swagger documentation. Ensure controllers are registered and routing is set up. ```csharp public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddEndpointsApiExplorer(); // Configure API versioning services.AddRestfulApiVersioning(o => { o.Conventions.Controller() .HasApiVersion(new ApiVersion(1, 0)) .HasApiVersion(new ApiVersion(2, 0)); }); // Add Swagger with RESTful support services.AddSwaggerGen(); services.AddRestfulSwagger(o => { o.OpenApiInfo.Title = "Product API"; o.OpenApiInfo.Description = "Product management service"; o.XmlDocumentations.AddFromBaseDirectory(typeof(Program)); o.XmlDocumentations.AddFromReferencePacks(typeof(ProblemDetails)); }); } public void Configure(IApplicationBuilder app) { app.UseRouting(); app.UseEndpoints(endpoints => endpoints.MapControllers()); app.UseSwagger(); app.UseSwaggerUI(); app.UseRestfulApiVersioning(); } ``` -------------------------------- ### Example Configuration Flow for API Versioning Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/ConfigureSwaggerGenOptions.md Illustrates the process of configuring Swagger documents for multiple API versions, including setting titles and versions. ```text API Versions: 1.0, 2.0, 3.0 ↓ For each version: - Create SwaggerDoc("v1", new OpenApiInfo { Title = "API 1.0", Version = "1.0" }) - Create SwaggerDoc("v2", new OpenApiInfo { Title = "API 2.0", Version = "2.0" }) - Create SwaggerDoc("v3", new OpenApiInfo { Title = "API 3.0", Version = "3.0" }) - Add XML documentation to all three ``` -------------------------------- ### Reference Pack File Pattern Example Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/XPathDocumentExtensions.md Illustrates the expected file path pattern for XML documentation files within .NET reference packs. ```plaintext ~/.dotnet/packs/System.Net.Http.Ref/9.0.0/ref/net9.0/System.Net.Http.xml ``` -------------------------------- ### C# Tool Definition Example Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/McpDocumentFilter.md Example of defining a tool using C# attributes for McpServerToolType and McpServerTool. This demonstrates how methods are discovered and documented. ```csharp [McpServerToolType] public class WeatherTools { [McpServerTool] [Description("Get current weather for a location")] public static async Task GetWeather(string location, string unit = "celsius") { // Implementation } } // Generates tool name: "get_weather" // Parameters: location (string), unit (string) ``` -------------------------------- ### Custom User-Agent Configuration Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/UserAgentDocumentFilter.md Configure custom description, example, and required status for the User-Agent header. ```csharp services.AddSwaggerGen(o => { o.AddUserAgent(ua => { ua.Description = "Client application identifier (e.g., MyApp/1.2.3)"; ua.Example = "ProductApp/2.1.0"; ua.Required = true; }); }); ``` -------------------------------- ### Example: Configurable Document Filter Implementation Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/DocumentFilter.md An example of a generic `DocumentFilter` that uses `CustomDocumentOptions`. It applies the version and base URL from the options to the OpenAPI document. ```csharp public class CustomConfigurableDocumentFilter : DocumentFilter { public CustomConfigurableDocumentFilter(CustomDocumentOptions options) : base(options) { } public override void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) { swaggerDoc.Info.Version = Options.Version; swaggerDoc.Servers ??= new List(); swaggerDoc.Servers.Add(new OpenApiServer { Url = Options.BaseUrl }); } } ``` -------------------------------- ### Example Usage of AddFromReferencePacks Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/XPathDocumentExtensions.md Demonstrates how to use the `AddFromReferencePacks` method to add XML documentation for `ProblemDetails` and `object` types to a list of `XPathDocument` objects. ```csharp var docs = new List() .AddFromReferencePacks() .AddFromReferencePacks(); ``` -------------------------------- ### UserAgentDocumentOptions Configuration Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/UserAgentDocumentOptions.md Configure options for documenting the User-Agent header, including its description, an example value, and its required status. ```APIDOC ## UserAgentDocumentOptions ### Description Configuration options for the `UserAgentDocumentFilter` that controls how the User-Agent header parameter is documented. ### Constructor ```csharp public UserAgentDocumentOptions() ``` Initializes a new instance of the `UserAgentDocumentOptions` class with default values. ### Properties #### Description ```csharp public string Description { get; set; } ``` Gets or sets the description of the User-Agent field. This documentation text explains the purpose of the User-Agent header and appears in the OpenAPI specification and API documentation tools. **Type:** `string` **Default:** `"The identifier of the calling client."` #### Example ```csharp public string Example { get; set; } ``` Gets or sets the example value to associate with the User-Agent field. This is a concrete example value shown in API documentation and Swagger/OpenAPI tools, helping developers understand the expected format. **Type:** `string` **Default:** `"Your-Awesome-Client/1.0.0"` #### Required ```csharp public bool Required { get; set; } ``` Gets or sets whether the User-Agent field is mandatory. Controls whether clients are required to include the User-Agent header in their requests. When true, API tools will mark this parameter as required in documentation. **Type:** `bool` **Default:** `false` **Values:** - `true` — User-Agent header must be present on all requests - `false` — User-Agent header is optional ``` -------------------------------- ### Configure User-Agent Header Documentation Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/configuration.md Use this snippet to configure the documentation for the User-Agent header parameter. Specify its description, an example value, and whether it is required. ```csharp services.AddSwaggerGen(o => { o.AddUserAgent(ua => { ua.Description = "Client application identifier"; ua.Example = "MyApp/2.1.0"; ua.Required = true; }); }); ``` -------------------------------- ### Example Usage of AddFromReferencePacks Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/XPathDocumentExtensions.md Demonstrates how to use the AddFromReferencePacks method to load documentation for various types, including framework types like ControllerBase and ProblemDetails, and how to integrate it with RestfulSwagger services. ```csharp var docs = new List(); // Load Microsoft.AspNetCore docs from reference packs docs.AddFromReferencePacks(typeof(ControllerBase)); // Load System documentation docs.AddFromReferencePacks(typeof(object)); docs.AddFromReferencePacks(typeof(ProblemDetails)); // Use in RestfulSwagger services.AddRestfulSwagger(o => { o.XmlDocumentations.AddFromReferencePacks(typeof(ProblemDetails)); o.XmlDocumentations.AddFromReferencePacks(typeof(object)); }); ``` -------------------------------- ### Configure Application Pipeline for Swagger UI Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/ConfigureSwaggerUIOptions.md This example shows the necessary middleware configuration in the `Configure` method to enable routing, controllers, Swagger, and Swagger UI. The `UseSwaggerUI` call automatically applies the `ConfigureSwaggerUIOptions`. ```csharp public void Configure(IApplicationBuilder app) { app.UseRouting(); app.UseEndpoints(endpoints => endpoints.MapControllers()); app.UseSwagger(); // ConfigureSwaggerUIOptions is automatically applied here app.UseSwaggerUI(); } ``` -------------------------------- ### Recommended User-Agent Format Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/UserAgentDocumentOptions.md This section provides a recommended format for the User-Agent string, which includes the application name, version, and an optional platform. Examples illustrate various valid formats. ```text AppName/Version[/Platform] ``` ```text MyMobileApp/2.1.3 ``` ```text WebClient/1.0.0/WebBrowser ``` ```text DataSyncService/3.2.1/Windows ``` ```text IoTDevice/1.0.0/RaspberryPi ``` -------------------------------- ### AddFromBaseDirectory Example Usage Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/XPathDocumentExtensions.md Shows the usage of the generic AddFromBaseDirectory method to load XML documentation files for multiple types in a type-safe manner. ```csharp var docs = new List() .AddFromBaseDirectory() .AddFromBaseDirectory(); ``` -------------------------------- ### Configure Services for API Versioning and Swagger Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/ConfigureSwaggerUIOptions.md This example demonstrates how to configure API versioning and Swagger documentation within the `ConfigureServices` method of an ASP.NET Core application. It sets up multiple API versions for a controller and configures basic Swagger information. ```csharp public void ConfigureServices(IServiceCollection services) { services.AddRestfulApiVersioning(o => { o.Conventions.Controller() .HasApiVersion(new ApiVersion(1, 0)); o.Conventions.Controller() .HasApiVersion(new ApiVersion(2, 0)); o.Conventions.Controller() .HasApiVersion(new ApiVersion(3, 0)) .IsDeprecated(new ApiVersion(4, 0)); }); services.AddRestfulSwagger(o => { o.OpenApiInfo.Title = "Product API"; o.OpenApiInfo.Description = "Product management service"; }); } ``` -------------------------------- ### Example: Custom Document Filter Implementation Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/DocumentFilter.md An example of a custom document filter that modifies the OpenAPI document's version and adds a server URL. This demonstrates overriding the `Apply` method. ```csharp public class CustomDocumentFilter : DocumentFilter { public override void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) { // Modify the document as needed swaggerDoc.Info.Version = "2.0.0"; swaggerDoc.Servers.Add(new OpenApiServer { Url = "https://api.example.com" }); } } ``` -------------------------------- ### RestfulSwaggerOptions Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/types.md Comprehensive configuration for RESTful Swagger setup, including API metadata, documentation sources, and Swagger generation options. ```APIDOC ## Class: RestfulSwaggerOptions ### Description Configuration object for RESTful Swagger setup with API versioning. ### Namespace `Codebelt.Extensions.Swashbuckle.AspNetCore` ### Implements `IValidatableParameterObject` ### Properties | Property | Type | Description | |---|---|---| | OpenApiInfo | OpenApiInfoOptions | API metadata configuration | | XmlDocumentations | IList<XPathDocument> | XML documentation files to include | | Settings | SwaggerGenOptions | Underlying Swashbuckle options | | IncludeControllerXmlComments | bool | Use controller comments as tag descriptions | | JsonSerializerOptionsFactory | Func<IServiceProvider, JsonSerializerOptions> | Custom JSON options resolver | ### Methods | Method | Return Type | Description | |---|---|---| | ValidateOptions() | void | Validates all properties are not null | ### Used By - `ServiceCollectionExtensions.AddRestfulSwagger()` - `ConfigureSwaggerGenOptions` - `ConfigureSwaggerUIOptions` ``` -------------------------------- ### AddFromBaseDirectory Example Usage Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/XPathDocumentExtensions.md Demonstrates how to use the AddFromBaseDirectory method to load XML documentation for specific types and integrate it into RestfulSwagger configuration. ```csharp var docs = new List(); // Load docs for a specific type and its base types docs.AddFromBaseDirectory(typeof(ProductModel)); docs.AddFromBaseDirectory(typeof(Program)); // Use in RestfulSwagger configuration services.AddRestfulSwagger(o => { o.XmlDocumentations.AddFromBaseDirectory(typeof(Program)); }); ``` -------------------------------- ### Add Custom UserAgent Configuration Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/UserAgentDocumentOptions.md Adds UserAgent documentation options with custom description, example, and required status. Use this to define specific requirements for the User-Agent header. ```csharp services.AddSwaggerGen(o => { o.AddUserAgent(ua => { ua.Description = "Client application identifier"; ua.Example = "ProductApp/2.1.0"; ua.Required = true; }); }); ``` -------------------------------- ### Example: Basic Custom Filter Implementation Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/DocumentFilter.md A concrete implementation of `DocumentFilter` that adds multiple server entries to the OpenAPI document. It ensures the `Servers` list is initialized before adding entries. ```csharp public class ServerListDocumentFilter : DocumentFilter { public override void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) { swaggerDoc.Servers ??= new List(); swaggerDoc.Servers.Add(new OpenApiServer { Url = "https://api.example.com", Description = "Production" }); swaggerDoc.Servers.Add(new OpenApiServer { Url = "https://staging-api.example.com", Description = "Staging" }); } } ``` -------------------------------- ### UserAgentDocumentOptions Property Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/UserAgentDocumentFilter.md Provides access to the configured options for the User-Agent parameter, including description, example, and required flag. ```csharp public UserAgentDocumentOptions Options { get; } ``` -------------------------------- ### Complete Service Configuration Example Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/configuration.md This C# code configures services for an ASP.NET Core application, including controllers, API versioning, Swagger/OpenAPI generation, JWT Bearer and API Key security, MCP for AI integration, and RESTful Swagger settings. It also configures the application pipeline for routing, endpoints, Swagger UI, and API versioning. ```csharp public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddEndpointsApiExplorer(); // Configure versioning services.AddRestfulApiVersioning(o => { o.Conventions.Controller() .HasApiVersion(new ApiVersion(1, 0)); o.Conventions.Controller() .HasApiVersion(new ApiVersion(2, 0)); }); // Configure Swagger/OpenAPI services.AddSwaggerGen(); // Add security services.Configure(o => { o.AddJwtBearerSecurity(); o.AddXApiKeySecurity(); o.AddUserAgent(ua => { ua.Required = true; ua.Example = "MyApp/1.0.0"; }); }); // Configure RESTful Swagger services.AddRestfulSwagger(o => { o.OpenApiInfo.Title = "Product API"; o.OpenApiInfo.Description = "RESTful product management service"; o.OpenApiInfo.Version = "1.0.0"; o.OpenApiInfo.Contact = new OpenApiContact { Name = "API Support", Email = "api@example.com" }; o.IncludeControllerXmlComments = true; o.XmlDocumentations .AddFromBaseDirectory(typeof(Program)) .AddFromReferencePacks(typeof(ProblemDetails)); o.Settings.UseAllOfToExtendReferenceSchemas(); }); // Add MCP for AI integration services.Configure(o => { o.AddMcpServer(mcp => { mcp.Pattern = "/mcp"; mcp.IncludeTools = true; }); }); } public void Configure(IApplicationBuilder app) { app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapMcp(); }); app.UseSwagger(); app.UseSwaggerUI(); app.UseRestfulApiVersioning(); } ``` -------------------------------- ### AddFromReferencePacks Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/XPathDocumentExtensions.md Adds XML documentation files from installed .NET reference packs that match a type's assembly hierarchy. This method is chainable. ```APIDOC ## AddFromReferencePacks ### Description Adds XML documentation files from installed .NET reference packs that match a type's assembly hierarchy. ### Method Signature `public static IList AddFromReferencePacks(this IList documents, Type type)` ### Parameters #### Path Parameters - **documents** (IList) - Required - The collection of documents to extend - **type** (Type) - Required - The type whose assembly hierarchy is scanned ### Return Type `IList` — Returns the documents collection to enable method chaining. ### Throws - `ArgumentNullException` if either parameter is null. ### Remarks Locates .NET reference packs directory via `DOTNET_ROOT` environment variable or by scanning the file system. Searches for XML documentation files matching the target framework moniker (e.g., `net9.0`). **Reference Pack Location Logic:** 1. Checks `DOTNET_ROOT` environment variable first 2. If not set, locates from `System.Object` assembly location 3. Scans up the directory tree for a `packs` directory 4. Matches against current runtime's target framework moniker **File Pattern:** ``` {dotnet-root}/packs/{assembly-name}.Ref/{version}/ref/{tfm}/{assembly-name}.xml ``` For example: ``` ~/.dotnet/packs/System.Net.Http.Ref/9.0.0/ref/net9.0/System.Net.Http.xml ``` ### Example Usage ```csharp var docs = new List(); // Load Microsoft.AspNetCore docs from reference packs docs.AddFromReferencePacks(typeof(ControllerBase)); // Load System documentation docs.AddFromReferencePacks(typeof(object)); docs.AddFromReferencePacks(typeof(ProblemDetails)); // Use in RestfulSwagger services.AddRestfulSwagger(o => { o.XmlDocumentations.AddFromReferencePacks(typeof(ProblemDetails)); o.XmlDocumentations.AddFromReferencePacks(typeof(object)); }); ``` ``` -------------------------------- ### Define UserAgentDocumentOptions in C# Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/types.md Use this class to configure how the User-Agent header is documented in the OpenAPI specification. Default values are provided for Description, Example, and Required properties. ```csharp public class UserAgentDocumentOptions : IParameterObject { public string Description { get; set; } public string Example { get; set; } public bool Required { get; set; } } ``` -------------------------------- ### AddByAssembly Example Usage Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/XPathDocumentExtensions.md Demonstrates how to use the AddByAssembly method to add XML documentation from assemblies to a list of XPathDocument objects. It shows adding documentation for a known type's assembly and a dynamically loaded assembly. ```csharp var docs = new List(); docs.AddByAssembly(typeof(ProductModel).Assembly); docs.AddByAssembly(Assembly.Load("CustomAssembly")); ``` -------------------------------- ### Example Usage of AddRestfulSwagger Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/ServiceCollectionExtensions.md Demonstrates how to configure RESTful API versioning and Swashbuckle.AspNetCore using the AddRestfulApiVersioning and AddRestfulSwagger extension methods. This includes setting API version information, configuring Swagger document details, and enabling XML documentation. ```csharp services.AddRestfulApiVersioning(o => { o.Conventions.Controller().HasApiVersion(new ApiVersion(1, 0)); }); services.AddRestfulSwagger(o => { o.OpenApiInfo.Title = "My API"; o.OpenApiInfo.Description = "A RESTful API"; o.XmlDocumentations.AddFromBaseDirectory(typeof(Program)); o.XmlDocumentations.AddFromReferencePacks(typeof(ProblemDetails)); o.IncludeControllerXmlComments = true; o.Settings.UseAllOfToExtendReferenceSchemas(); }); ``` -------------------------------- ### Configure RestfulSwaggerOptions for API Versioning Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/types.md Comprehensive configuration for RESTful Swagger setup. This includes API metadata, XML documentation sources, and underlying Swashbuckle generation options. It also allows for custom JSON serializer options. ```csharp public class RestfulSwaggerOptions : IValidatableParameterObject { public OpenApiInfoOptions OpenApiInfo { get; set; } public IList XmlDocumentations { get; set; } public SwaggerGenOptions Settings { get; set; } public bool IncludeControllerXmlComments { get; set; } public Func JsonSerializerOptionsFactory { get; set; } public void ValidateOptions(); } ``` -------------------------------- ### Configure Services for Multi-Version API and Swagger Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/ConfigureSwaggerUIOptions.md Configure API versioning and Swagger support in your application's services. This setup automatically registers all defined API versions for Swagger. ```csharp public void ConfigureServices(IServiceCollection services) { // Configure API versioning services.AddRestfulApiVersioning(o => { // Version 1.0 - Original API o.Conventions.Controller() .HasApiVersion(new ApiVersion(1, 0)); // Version 2.0 - Enhanced with new fields o.Conventions.Controller() .HasApiVersion(new ApiVersion(2, 0)); // Version 3.0 - Restructured response o.Conventions.Controller() .HasApiVersion(new ApiVersion(3, 0)); }); // Configure Swagger with RESTful support // ConfigureSwaggerUIOptions will automatically register all versions services.AddRestfulSwagger(o => { o.OpenApiInfo.Title = "Product API"; o.OpenApiInfo.Description = "RESTful product service"; o.XmlDocumentations.AddFromBaseDirectory(typeof(Program)); }); } ``` -------------------------------- ### Example: Custom Options Class for Configurable Filter Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/DocumentFilter.md Defines a simple options class `CustomDocumentOptions` that implements `IParameterObject`. This class holds configuration values like `Version` and `BaseUrl` for a configurable document filter. ```csharp public class CustomDocumentOptions : IParameterObject { public string Version { get; set; } = "1.0.0"; public string BaseUrl { get; set; } = "https://api.example.com"; } ``` -------------------------------- ### MCP Tool Discovery with Partial Failures Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/errors.md The `McpDocumentFilter` handles assembly or type loading failures gracefully. Even if some tools fail to discover, the endpoint will still be documented without examples, preventing fatal errors. ```csharp // Even if tool discovery partially fails, // the endpoint is still documented without examples services.Configure(o => { o.AddMcpServer(mcp => mcp.IncludeTools = true); }); // If some types fail to load, others are still processed ``` -------------------------------- ### Registering Document Filters in Startup Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/DocumentFilter.md Demonstrates how to register both a simple `DocumentFilter` and a configurable `DocumentFilter` with dependency injection in an ASP.NET Core application's startup configuration. ```csharp services.AddSwaggerGen(o => { // Simple filter registration o.DocumentFilter(); // Configurable filter registration o.DocumentFilter( new CustomDocumentOptions { Version = "2.0.0", BaseUrl = "https://api.mycompany.com" } ); }); ``` -------------------------------- ### Example: DeprecationOperationFilter Implementation Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/OperationFilter.md An example implementation of OperationFilter that marks operations as deprecated if their corresponding controller action method is marked with the ObsoleteAttribute. ```csharp public class DeprecationOperationFilter : OperationFilter { public override void Apply(OpenApiOperation operation, OperationFilterContext context) { var apiDesc = context.ApiDescription; if (apiDesc.ActionDescriptor is ControllerActionDescriptor cad) { if (cad.MethodInfo.GetCustomAttribute() != null) { operation.Deprecated = true; operation.Summary = "[DEPRECATED] " + operation.Summary; } } } } ``` -------------------------------- ### Add McpServer to SwaggerGenOptions Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/McpSwaggerGenOptionsExtensions.md Use this extension method to register a `McpDocumentFilter` with `SwaggerGenOptions`. This filter documents MCP Streamable HTTP transport endpoints and optionally legacy SSE endpoints in the OpenAPI specification. It discovers MCP tools, creates an "MCP" tag, registers the endpoint, and generates example request bodies. Call without a setup action to use default settings for pattern, tag name, tool inclusion, and legacy SSE enablement. ```csharp public static void AddMcpServer( this SwaggerGenOptions options, Action setup = null) ``` -------------------------------- ### Comprehensive Service Configuration with XML Documentation Loading Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/XPathDocumentExtensions.md Illustrates configuring services for an ASP.NET Core application, including loading XML documentation from various sources such as the application's base directory, specific types, assemblies, and .NET reference packs for framework types like `ProblemDetails`, `object`, and `DateTime`. ```csharp public void ConfigureServices(IServiceCollection services) { services.AddRestfulApiVersioning(o => { o.Conventions.Controller().HasApiVersion(new ApiVersion(1, 0)); }); services.AddRestfulSwagger(o => { o.OpenApiInfo.Title = "Product API"; o.OpenApiInfo.Description = "Complete product management system"; // Load documentation from multiple sources o.XmlDocumentations // Application's own documentation .AddFromBaseDirectory(typeof(Program)) // Documentation for custom types .AddByType() .AddByType() .AddByAssembly(typeof(SharedModels).Assembly) // Microsoft framework documentation .AddFromReferencePacks() .AddFromReferencePacks() .AddFromReferencePacks(); o.IncludeControllerXmlComments = true; }); } ``` -------------------------------- ### User-Agent Configuration in Startup Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/UserAgentDocumentFilter.md Integrate User-Agent header configuration within the startup process, alongside other API versioning and Swagger settings. ```csharp public void ConfigureServices(IServiceCollection services) { services.AddRestfulApiVersioning(o => { o.Conventions.Controller().HasApiVersion(new ApiVersion(1, 0)); }); services.AddRestfulSwagger(o => { o.OpenApiInfo.Title = "Product API"; o.OpenApiInfo.Description = "Product management system"; o.Settings.AddUserAgent(ua => { ua.Required = true; ua.Description = "Identifies the client making the request"; ua.Example = "MobileApp/3.0.0"; }); }); } ``` -------------------------------- ### JSON-RPC Request Example Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/McpDocumentFilter.md Example of a JSON-RPC 2.0 request body for calling a tool. This format is used for requests to the streamable HTTP endpoint. ```json { "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": { "name": "get_weather", "arguments": { "location": "San Francisco", "unit": "celsius" } } } ``` -------------------------------- ### Build, Test, and Run DocFX Scripts Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/AGENTS.md Standard commands for building the project, running tests, and executing DocFX scripts for documentation generation and validation. Ensure the script path is correctly resolved. ```bash dotnet build dotnet test dotnet run --file /scripts/docfx.cs -- --repo-root . --build-api-model --validate-samples --verify-docfx-build ``` -------------------------------- ### MCP Streamable HTTP Example Request Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/endpoints.md Example cURL command for sending a POST request to the MCP streamable HTTP endpoint. Includes necessary headers and a JSON-RPC 2.0 request body for tool invocation. ```bash curl -X POST http://localhost:5000/mcp \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_TOKEN" \ -d '{ "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": { "name": "get_weather", "arguments": { "location": "San Francisco", "unit": "celsius" } } }' ``` -------------------------------- ### Initialize McpDocumentOptions with Default Values Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/McpDocumentOptions.md Use the default constructor to create an instance of McpDocumentOptions with predefined settings. ```csharp public McpDocumentOptions() ``` -------------------------------- ### Document MCP Tools as Request Body Examples Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/McpDocumentOptions.md When 'IncludeTools' is true, the filter discovers MCP tools and generates examples for the POST {Pattern} request body. This allows developers to select tools from a dropdown in Swagger UI. ```yaml requestBody: content: application/json: examples: get_weather: summary: get_weather value: { "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": { "name": "get_weather", "arguments": { "location": "" } } } search_products: summary: search_products value: { "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": { "name": "search_products", "arguments": { "query": "" } } } ``` -------------------------------- ### Initialize OpenApiInfoOptions Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/OpenApiInfoOptions.md Demonstrates how to initialize and configure an OpenApiInfoOptions object with various properties like title, description, terms of service, contact, and license. This configuration is then used when adding RestfulSwagger services. ```csharp var apiInfo = new OpenApiInfoOptions { Title = "Product Service API", Description = "Manages product catalog and inventory", TermsOfService = new Uri("https://api.example.com/terms"), Contact = new OpenApiContact { Name = "API Support", Email = "api-support@example.com", Url = new Uri("https://example.com/support") }, License = new OpenApiLicense { Name = "Apache 2.0", Url = new Uri("https://www.apache.org/licenses/LICENSE-2.0") } }; // Add custom extensions apiInfo.Extensions.Add("x-logo", new OpenApiString("https://example.com/logo.png")); services.AddRestfulSwagger(o => o.OpenApiInfo = apiInfo); ``` -------------------------------- ### Options Property Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/DocumentFilter.md Gets the configured options of type `T` for this filter instance. These options are used to customize the filter's behavior. ```csharp public T Options { get; } ``` -------------------------------- ### AddUserAgent to SwaggerGenOptions Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/SwaggerGenOptionsExtensions.md Adds a User-Agent header parameter to all operations in Swagger/OpenAPI documentation. Configure User-Agent options using the setup action. ```csharp public static SwaggerGenOptions AddUserAgent( this SwaggerGenOptions options, Action setup = null) ``` ```csharp services.AddSwaggerGen(o => { o.AddUserAgent(ua => { ua.Description = "Client identifier"; ua.Example = "MyApp/2.0.0"; ua.Required = true; }); }); ``` -------------------------------- ### Add Basic McpServer Configuration Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/McpDocumentOptions.md Use this to add MCP server documentation with default settings. Ensure AddMcpServer is called within AddSwaggerGen. ```csharp services.AddSwaggerGen(o => { o.AddMcpServer(); // Uses all defaults }); ``` -------------------------------- ### Full MCP Server Configuration Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/McpSwaggerGenOptionsExtensions.md Demonstrates a comprehensive configuration of the MCP Server with custom pattern, tag name, tool inclusion, and SSE support settings. ```APIDOC ## POST /agent/mcp ### Description Streamable HTTP endpoint with full configuration, including custom pattern, tag name, tool inclusion, and SSE support settings. ### Method POST ### Endpoint /agent/mcp ### Parameters None ### Request Body None ### Response #### Success Response (200) - None (Streamable endpoint) #### Response Example None ``` -------------------------------- ### Configure Application Pipeline for Swagger UI Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/ConfigureSwaggerUIOptions.md Set up the application pipeline to use routing, endpoints, Swagger, and API versioning. The Swagger UI will automatically display version options after configuration. ```csharp public void Configure(IApplicationBuilder app, IApiVersionDescriptionProvider provider) { app.UseRouting(); app.UseEndpoints(endpoints => endpoints.MapControllers()); app.UseSwagger(); app.UseSwaggerUI(c => { // ConfigureSwaggerUIOptions has already registered all versions // You can add further customization here if needed c.DisplayOperationId(); c.DefaultModelsExpandDepth(1); }); app.UseRestfulApiVersioning(); } ``` -------------------------------- ### Environment-Specific Swashbuckle Configuration Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/configuration.md Configure Swagger generation options based on the ASP.NET Core environment. This example enables XML documentation comments only in development. ```csharp public void ConfigureServices(IServiceCollection services) { var isDevelopment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development"; services.AddSwaggerGen(); services.AddRestfulSwagger(o => { o.OpenApiInfo.Title = "Product API"; if (isDevelopment) { o.XmlDocumentations.AddFromBaseDirectory(typeof(Program)); o.IncludeControllerXmlComments = true; } }); if (isDevelopment) { services.Configure(opts => { opts.AddMcpServer(mcp => mcp.EnableLegacySse = true); }); } } ``` -------------------------------- ### Add Default UserAgent Configuration Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/UserAgentDocumentOptions.md Adds UserAgent documentation options with all default values. Use this when no specific customization is needed. ```csharp services.AddSwaggerGen(o => { o.AddUserAgent(); // Uses all default values }); ``` -------------------------------- ### AddRestfulSwagger Method Signature Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/ServiceCollectionExtensions.md Defines the signature for the AddRestfulSwagger extension method, which configures Swashbuckle.AspNetCore for RESTful APIs. It accepts an optional setup action for RestfulSwaggerOptions. ```csharp public static IServiceCollection AddRestfulSwagger( this IServiceCollection services, Action setup = null) ``` -------------------------------- ### Set User-Agent Required Status Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/UserAgentDocumentOptions.md Gets or sets whether the User-Agent field is mandatory. When true, API tools will mark this parameter as required in documentation. ```csharp public bool Required { get; set; } ``` -------------------------------- ### Set User-Agent Description Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/api-reference/UserAgentDocumentOptions.md Gets or sets the description of the User-Agent field. This documentation text explains the purpose of the User-Agent header and appears in the OpenAPI specification. ```csharp public string Description { get; set; } ``` -------------------------------- ### JWT Bearer Authentication Header Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/endpoints.md Use this header format for JWT Bearer token authentication. The value must start with 'Bearer ' followed by the token. ```http Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9... ``` -------------------------------- ### Handle Missing Files Gracefully in C# Source: https://github.com/codebeltnet/swashbuckle-aspnetcore/blob/main/_autodocs/errors.md Configure XML documentation loading to silently handle missing files. This approach prevents application failures due to non-existent documentation files. ```csharp public void ConfigureXmlDocumentation( IServiceCollection services) { services.AddRestfulSwagger(o => { // These silently handle missing files o.XmlDocumentations.AddByFilename("/path/to/docs.xml"); o.XmlDocumentations.AddFromBaseDirectory(typeof(Program)); o.XmlDocumentations.AddFromReferencePacks(typeof(object)); // All succeed even if some files don't exist }); } ```