๐ API Gateway Generator Agent¶
๐ฏ Purpose and Architectural Role of the API Gateway in the Factory¶
๐ง Agent Identity¶
- Agent Name:
ApiGatewayGeneratorAgent - Cluster:
software-engineering.gateway - Domain: ConnectSoft AI Software Factory โ Infrastructure Surface
- Position: Executes after API Designer Agent defines contracts and just before service exposure
๐ฏ Core Purpose¶
The API Gateway Generator Agent is responsible for autonomously generating a custom .NET Core-based API Gateway solution for each bounded context or exposed interface defined in the platform.
It translates API surface designs, cross-cutting concerns, and multi-tenant boundary rules into an operational, production-grade gateway โ enforcing access, routing, policies, and observability in a composable, plugin-driven way.
๐ Architectural Role in the Factory¶
flowchart TD
subgraph ConnectSoft Factory
direction LR
API_Designer["๐งฉ API Designer Agent"]
Planner["๐งญ Planner Agent"]
Gateway["๐ก API Gateway Generator Agent"]
Auth["๐ Identity Server"]
Services["๐งฑ Microservices"]
QA["๐งช QA Agent"]
DevOps["๐ CI/CD Agent"]
Planner --> Gateway
API_Designer --> Gateway
Gateway --> QA
Gateway --> DevOps
Gateway --> Services
Gateway --> Auth
end
๐งญ Responsibilities by Stage¶
| Stage | Role |
|---|---|
| ๐ Design-Time | Transforms API contracts + routing plan into a gateway blueprint |
| ๐ Build-Time | Scaffolds a full .NET Core gateway solution with required policies, middlewares, configs, and interfaces |
| ๐งช Pre-Deploy | Validates routes, simulates tenant flows, integrates with OpenIddict |
| ๐ Deploy-Time | Produces deployable artifacts (Docker, Azure, Bicep, CI YAML) |
| ๐ Run-Time (Plugins) | Enables runtime behavior control via injected plugin filters or route modifiers |
๐ฏ Why Itโs Critical¶
| Area | Contribution |
|---|---|
| ๐ Security | Enforces JWT, scopes, API keys, rate limits |
| ๐งญ Routing | Dynamic reverse proxying with DDD-awareness |
| ๐งฉ Multi-Tenancy | Header/claim-based partitioning |
| ๐ Observability | Metrics, traces, structured logs |
| ๐งฌ Extensibility | Filters, middlewares, per-service plugin hooks |
| ๐ Documentation | Emits clear operator and route docs from intent |
๐ง Differentiation from Other Agents¶
| Agent | Difference |
|---|---|
| Microservice Generator | Produces internal services behind the gateway |
| API Designer Agent | Provides contract for routing and auth setup |
| Release Manager | Publishes gateway artifact after it's built |
| Identity Server Agent | Provides token issuance, integrated by this gateway |
| Observability Agent | Consumes gateway metrics and logs emitted from this agent's outputs |
๐ฆ Summary¶
The API Gateway Generator Agent acts as the frontline enforcer of every software product built in the ConnectSoft Factory โ translating platform-wide policies and feature blueprints into hardened, extensible, tenant-aware HTTP interfaces, all within a clean .NET Core codebase.
๐ฆ Responsibilities โ Security, Routing, Observability, Tiering¶
๐ฏ Overview¶
The API Gateway Generator Agent automates the creation of a hardened, policy-rich API gateway that acts as a gatekeeper, policy enforcer, traffic router, and telemetry emitter for all exposed services in the ConnectSoft ecosystem.
It translates functional contracts and platform constraints into a gateway that supports:
- Fine-grained authentication and authorization
- Multi-tenant-aware routing and service segmentation
- Layered observability (metrics, traces, logs)
- Quota enforcement and edition-based access rules
- Pluggable behavior through middleware and filters
๐ก Core Responsibility Areas¶
| Responsibility | Description |
|---|---|
| Routing & Path Resolution | Parses OpenAPI/DSL from API Designer to route to appropriate microservice endpoints using custom route tables or runtime config |
| Authentication | Integrates with Identity Server (OpenIddict) to validate JWT, access tokens, or API keys using built-in middleware |
| Authorization | Enforces route-level permissions via claims, scopes, and custom policies |
| Tenant Isolation | Extracts and routes based on tenant headers, claims, or tokens; supports edition/plan-specific gating |
| Rate Limiting & Quotas | Applies per-route or per-tenant rate limits, burst protection, and per-tier throttling |
| Observability | Emits structured logs (Serilog), metrics (Prometheus/OTel), and distributed traces for every request |
| Resilience | Adds retry, timeout, and circuit breaker policies per route using policy decorators |
| CORS Handling | Applies origin restrictions based on service configuration and environment |
| Plugin Injection | Supports filters (pre/post), feature toggles, and customization of request/response logic |
| Documentation Emission | Generates README, route map, operator documentation, and configuration summary automatically |
๐ Security Enforcements¶
| Area | Detail |
|---|---|
| Auth | OpenIddict token validation, HMAC-based API key checks |
| Zero Trust | Token presence enforcement, scoping, deny-by-default |
| Plan Gating | Access gates for Premium/Basic editions |
| Header Scrubbing | Automatic removal of unsafe/blacklisted headers |
๐ Routing Engine Output¶
The agent generates a routing definition similar to:
app.MapGatewayRoute("/v1/payments", "PaymentService")
.RequireAuthorization("Scope:payments.read")
.WithRateLimit("10r/s")
.WithTenantPartitioning("Header:X-Tenant-ID");
Or for YAML route DSL output:
- path: /v1/orders
backend: OrderService
auth:
type: jwt
scope: orders.read
rateLimit: 5r/s
tenantHeader: X-Tenant-ID
๐ Observability Hooks Embedded¶
| Type | Tech | Scope |
|---|---|---|
| Logs | Serilog + Enrichers | Full request lifecycle |
| Traces | OpenTelemetry SDK | Per request + span linking |
| Metrics | Prometheus exporter | Per route, status code, latency, retries |
๐ Plan & Tier Enforcement¶
Supports:
- Feature flags injected into routes based on
EditionDefinition - Selective exposure of routes/methods
- Rate tier enforcement (e.g., Free = 5r/s, Premium = 100r/s)
๐ Emitted Artifacts by Responsibility Area¶
| Area | Output |
|---|---|
| Routing | routes.json, RouteBuilder.cs, RouteRegistry.cs |
| Auth | JwtMiddleware.cs, ApiKeyValidator.cs |
| Observability | TelemetryMiddleware.cs, metrics.prom, otel-config.yml |
| Tenancy | TenantResolver.cs, TenantContext.cs |
| Policies | RateLimitMiddleware.cs, CircuitBreaker.cs |
| Docs | README.md, gateway-docs.json, gateway.routes.md |
๐งพ Inputs โ API Designer Outputs, Gateway Blueprint, Planner Intents¶
๐ฏ Purpose¶
This section details the structured inputs that the API Gateway Generator Agent consumes during its invocation. These inputs are received from:
- ๐ก Planner Agent: task orchestration and feature selection
- ๐ API Designer Agent: OpenAPI specs, endpoint contracts, route metadata
- ๐๏ธ Architect Agents: Gateway blueprints and policy constraints
- ๐ฆ Template Registry: Source scaffolds and reusable policy packages
๐ค Primary Input Channels¶
| Source | Format | Content |
|---|---|---|
| Planner Agent | JSON plan or task DSL | Feature flags, deployment context, tiering strategy |
| API Designer Agent | OpenAPI/Swagger or DSL | Route paths, methods, request/response shapes, auth model |
| Gateway Blueprint | gateway-blueprint.json |
Auth strategy, routing mode, rate limits, multi-tenant headers |
| Edition Definition | edition.json |
Tier-based access control (routes, quotas, limits) |
| Template Metadata | template.json |
Parameters, optional files, version pinning |
| Contextual Inputs | Semantic Kernel planner memory | Tenant ID strategy, service boundaries, previous failures |
๐ Example: API Designer Output¶
{
"apiName": "Vetspire.Appointments",
"version": "1.0.0",
"routes": [
{
"path": "/appointments",
"method": "GET",
"auth": "jwt",
"requiredScope": "appointments.read"
},
{
"path": "/appointments/{id}",
"method": "DELETE",
"auth": "jwt",
"requiredScope": "appointments.delete"
}
]
}
๐งฑ Gateway Blueprint Structure¶
{
"auth": {
"type": "jwt",
"provider": "OpenIddict"
},
"tenantResolution": {
"strategy": "header",
"headerName": "X-Tenant-ID"
},
"rateLimits": {
"enabled": true,
"strategy": "perRoutePerTenant"
},
"observability": {
"tracing": true,
"metrics": true,
"structuredLogging": true
}
}
๐ง Planner-Defined Flags¶
| Flag | Effect |
|---|---|
useRateLimit |
Injects RateLimitMiddleware.cs with policy map |
useEditionTiering |
Adds edition-aware access controls |
useTenantIsolation |
Enables TenantContext.cs and per-tenant routing |
generateReadme |
Triggers automatic README.md and gateway.routes.md output |
useChaosResilience |
Injects test route with retry + chaos monkey support |
๐ฆ Template Input Mapping¶
Agent maps the above inputs to dotnet new template symbols:
| CLI Flag | Mapped Input |
|---|---|
--authType jwt |
auth.type = "jwt" |
--tenantHeader X-Tenant-ID |
tenantResolution.headerName |
--useRateLimit true |
rateLimits.enabled = true |
--useChaos true |
triggers chaos route generation |
๐ Optional External Inputs¶
| Input | Use |
|---|---|
api-gateway.route-extensions.json |
Append extra headers, enrichments |
shared-config.json |
Token endpoint, JWKS URI, standard headers |
plan.json |
Planner-level DSL for microservice exposure logic |
๐ง Memory Recall Integration¶
If agent was previously invoked for this API or edition:
- Pulls last generated gateway config (for diffing)
- Injects version history for rollback/resilience
- Adjusts tenant headers, scopes, auth strategy based on known defaults
๐ค Outputs โ Gateway Project, Config, Plugins, Docs, Pipelines¶
๐ฏ Purpose¶
This section defines the artifacts produced by the API Gateway Generator Agent. These outputs are structured to deliver a fully functional .NET Core-based API Gateway solution with complete observability, extensibility, documentation, and deployment readiness.
๐๏ธ Core Generated Output Structure¶
๐ ApiGateway.Vetspire.Appointments/
โโโ ApiGateway.Vetspire.Appointments.csproj
โโโ Program.cs
โโโ Startup.cs
โโโ GatewayHost.cs
โโโ RouteRegistry.cs
โโโ Auth/
โ โโโ JwtValidationMiddleware.cs
โ โโโ ApiKeyHandler.cs
โโโ Tenancy/
โ โโโ TenantResolver.cs
โ โโโ TenantContext.cs
โโโ Policies/
โ โโโ RateLimiter.cs
โ โโโ RetryPolicy.cs
โ โโโ CircuitBreaker.cs
โโโ Observability/
โ โโโ SerilogConfig.cs
โ โโโ MetricsExporter.cs
โ โโโ TracingSetup.cs
โโโ Plugins/
โ โโโ IRouteFilter.cs
โโโ Configuration/
โ โโโ routes.json
โ โโโ gateway-settings.json
โโโ Docs/
โ โโโ gateway.routes.md
โ โโโ README.md
โโโ Tests/
โ โโโ RouteTests.cs
โ โโโ AuthTests.cs
โโโ CI/
โ โโโ azure-pipelines.yml
โโโ version.json
๐ฆ Main Output Categories¶
| Category | Outputs |
|---|---|
| Gateway Project | .csproj, Program.cs, Startup.cs, DI setup, routing middleware |
| Auth & Security | JWT validation, API Key handler, tenant resolution strategy |
| Routing | RouteRegistry.cs, routes.json, OpenAPI-consistent mapping |
| Policies | Resilience middleware, circuit breakers, retry, rate limiters |
| Plugins | Extensible IRouteFilter, IRequestTransformer, pluggable handlers |
| Observability | Logging config, metrics exports, OpenTelemetry setup |
| Configuration | Declarative gateway-settings.json, EditionPolicy.json |
| Docs | Auto-generated README, route docs, config summary |
| Pipelines | CI/CD YAML with validation, build, test, container push |
| Versioning | version.json for tracking build metadata and traceability |
๐ Example: version.json¶
{
"gateway": "ApiGateway.Vetspire.Appointments",
"version": "1.0.0",
"template": "ConnectSoft.GatewayTemplate@2.2.0",
"build": "2025-06-14T13:22:00Z",
"authType": "jwt",
"rateLimit": true,
"multiTenant": true
}
๐ Documentation Artifacts¶
| File | Purpose |
|---|---|
README.md |
Overview of gateway functionality, architecture, usage instructions |
gateway.routes.md |
List of all generated routes, methods, authorization rules |
Docs/edition-policies.md |
Lists tiered access by edition |
Docs/observability.md |
Explains metrics/traces/logging configuration |
๐งช Test and QA Outputs¶
| Test File | Description |
|---|---|
RouteTests.cs |
Validates each route maps to correct downstream |
AuthTests.cs |
Ensures proper JWT validation / rejection cases |
RateLimitTests.cs |
Simulates quota and burst conditions |
๐งต CI/CD Integration¶
| Output | Description |
|---|---|
azure-pipelines.yml |
Multi-stage CI/CD: restore โ build โ test โ pack โ publish |
build.ps1 |
Local development support script |
Dockerfile |
Optional generation for containerized deployment |
bicep/terraform |
Optional IaC generation for staging gateways |
๐ง Knowledge Base โ Archetypes, Shared Policies, Domain Boundaries¶
๐ฏ Purpose¶
This section defines what the API Gateway Generator Agent already knows at execution time โ its embedded knowledge base and predefined architectural archetypes, including:
- Domain-aligned gateway types
- Shared authentication and routing policies
- Multi-tenant design contracts
- Resilience and observability defaults
These form the cognitive backbone for accurate and standards-compliant gateway generation.
๐งฉ Gateway Archetypes (Types)¶
| Archetype | Description | Used When |
|---|---|---|
| Public Edge Gateway | Internet-facing, secured by OAuth2, API keys, and CORS | For external SaaS APIs |
| Internal Reverse Proxy | Authenticated by token relay or service identity | For internal service-to-service calls |
| Tenant BFF Gateway | Backend-for-frontend for a specific tenant or app | For UI-driven flows, tailored to editions |
| Multi-Host Gateway | Handles routing across tenant subdomains | For white-label or custom tenant domains |
These are aligned to product blueprints and selected by the Planner Agent or Architect Agent.
๐งฐ Built-in Shared Policies¶
The agent knows how to inject and scaffold the following shared platform-level policies:
| Policy | Location | Notes |
|---|---|---|
| JWT validation | JwtValidationMiddleware.cs |
Validates issuer, audience, scopes |
| API key validation | ApiKeyHandler.cs |
Can be per route or global |
| Rate limiting | RateLimiter.cs |
Configurable burst + window per tenant |
| Retry & circuit breaker | Resilience.cs |
Based on transient fault strategy |
| Edition gating | EditionGateMiddleware.cs |
Injected from edition.json |
| Chaos monkey | ChaosPolicy.cs |
Controlled via flags and testing env only |
๐ง Domain Boundary Knowledge¶
| Knowledge Source | Use |
|---|---|
api-designer-agent output |
Infers domain, bounded context |
| Gateway naming convention | ApiGateway.{BoundedContext} |
| Downstream service map | Maps routes to microservice names |
| Shared headers | Applies platform-wide headers like X-Tenant-ID, X-Edition-ID |
๐ Tenant Isolation Strategy Matrix¶
| Strategy | Implementation |
|---|---|
| Header | Uses X-Tenant-ID, extracted via middleware |
| Token Claims | Reads tenant_id from JWT claims |
| Subdomain | Optional: parses tenant from host header |
| Fallback | Defaults to โanonymousโ or denies access |
This matrix is pre-baked in the agentโs memory and dynamically adjusted by planner or input prompt flags.
๐ Semantic Patterns Known¶
| Pattern | Description |
|---|---|
Route โ Scope โ Policy โ Retry |
The core enforcement pipeline for each endpoint |
PathGroup โ EditionRules |
Tier gating at method or path level |
ServiceName โ RoutePrefix |
Auto-alignment of microservice to URL base path |
CustomPlugin โ HookPoint |
Allows for structured plugin registration per phase |
๐ Memory-Aware Components¶
The agent retains and reuses:
- Known route patterns (
/v1/*,/health, etc.) - Known gateway IDs and API namespace prefixes
- Previously used editions and policy combinations
- Retry logs and overrides from failed generations
๐ Process Flow โ Generation Stages, Validation, Retry Points¶
๐ฏ Purpose¶
This section breaks down the step-by-step internal flow the API Gateway Generator Agent follows from invocation to artifact completion โ including intermediate decisions, validation hooks, and retry handling logic.
This deterministic flow ensures predictable and safe gateway generation for any API interface or bounded context.
๐งญ High-Level Generation Phases¶
graph TD
A[Agent Invocation] --> B{Load Inputs}
B --> C[Validate API Contracts]
C --> D[Initialize Template Parameters]
D --> E[Scaffold Gateway Project]
E --> F[Inject Routing & Auth Middleware]
F --> G[Inject Policies, Plugins, Observability]
G --> H[Generate Configs, Docs, CI/CD]
H --> I[Validate Structure and Semantics]
I --> J{Retry Needed?}
J -- Yes --> D
J -- No --> K[Emit Final Artifacts]
K --> L[Return Output to Planner / Orchestrator]
โ๏ธ Detailed Steps¶
| Step | Description |
|---|---|
| 1๏ธโฃ Load Inputs | Loads API contract, planner intent, gateway blueprint, template metadata |
| 2๏ธโฃ Validate Contracts | Ensures all routes, methods, and schemas are consistent and mappable |
| 3๏ธโฃ Initialize Template Context | Sets up internal state: flags, defaults, known plugin list |
| 4๏ธโฃ Project Scaffold | Executes custom dotnet new template for ApiGateway.* solution |
| 5๏ธโฃ Route + Auth Injection | Builds routing tables and embeds JWT/API key validation flows |
| 6๏ธโฃ Policy Layering | Adds per-route rate limits, retries, circuit breakers, and tenant isolation |
| 7๏ธโฃ Observability and Plugins | Adds telemetry pipeline, metric exporters, plugin scaffolding |
| 8๏ธโฃ Docs and CI/CD | Generates README, route map, Azure DevOps YAML, and Dockerfile |
| 9๏ธโฃ Validation Phase | Runs static structure checks, simulation of routing plan, test generation |
| ๐ Retry Loop | If validation fails, reruns template generation with adjusted inputs or flags |
| โ Emit Outputs | Final gateway files, docs, version metadata are returned for registration and deployment |
๐งช Validation Types¶
| Type | Validator |
|---|---|
| Structural | Missing folders, duplicate routes, invalid symbols |
| Semantic | Route-to-service match, scope presence, conflicting headers |
| Observability | Missing metric hooks, untraced routes |
| Security | Auth mismatch (route says JWT but policy absent), missing edition gates |
| Build Check | Ensures project compiles locally (dotnet build) before emit |
| Test Scaffolding | Ensures at least 1 test per route class is emitted if test flag is true |
๐ง Retry Logic¶
| Failure Class | Retry Behavior |
|---|---|
| Missing scope mapping | Retry with defaultScope fallback |
| Invalid edition map | Retry with full plan exposure unless --strictEdition=true |
| Auth misalignment | Retry by removing route.auth: required if --failOnAuthMismatch=false |
| Plugin resolution error | Retry with pluginFallback = NoOp |
Retries are traceable, memoized in logs, and version-stamped per generation.
๐ Retry Decision Tree¶
flowchart TD
Validate --> InvalidAuth[Missing Auth Strategy]
InvalidAuth --> Retry1[Retry with default: JWT]
Retry1 --> Validate
Validate --> MissingEdition[Unmapped Edition]
MissingEdition --> Retry2[Retry with Basic Access]
Retry2 --> Validate
Validate --> Pass[All OK]
๐ Technologies and Stack Integration โ .NET 8, Plugins, Middleware, Telemetry¶
๐ฏ Purpose¶
This section defines the technical foundation and framework choices behind the gateway generatorโs emitted output. It ensures generated gateways are consistent with ConnectSoft standards for clean architecture, plugin extensibility, and full observability โ all within a .NET 8 custom gateway stack.
๐งฑ Core Technologies¶
| Technology | Purpose |
|---|---|
| .NET 8 SDK | Base framework for the API Gateway runtime |
| ASP.NET Core Web API | Hosts routing engine, middlewares, DI container |
| Serilog | Structured logging with enrichers and sinks |
| OpenTelemetry SDK | Tracing (Jaeger/Zipkin compatible), metrics (Prometheus ready) |
| MassTransit (Optional) | Event-driven messaging for domain events or gateway events |
| NHibernate / EF Core (Optional) | For plugins needing config persistence |
| FluentValidation | Used in gateway plugin extensions or policy rules |
| Azure SDK | KeyVault, AppConfig, etc., depending on planner instructions |
| Semantic Kernel | Agent-side skill orchestration (not inside gateway runtime) |
๐งฉ Plugin and Policy Framework¶
The custom gateway supports ConnectSoft plugin injection architecture via DI. Generated projects scaffold interfaces and registration patterns for:
| Plugin Type | Interface | Purpose |
|---|---|---|
IRouteFilter |
Pre-route and post-route request modification | |
IRequestValidator |
Apply request schema or token validation | |
ITenantPolicyProvider |
Inject edition- or tenant-based access rules | |
IMetricsObserver |
Observe and emit gateway-level metrics | |
IGatewayPlugin |
Marker interface for any injected runtime service |
Registered in Startup.cs via reflection scan or explicit DI:
services.Scan(scan => scan
.FromAssemblyOf<IGatewayPlugin>()
.AddClasses(classes => classes.AssignableTo<IGatewayPlugin>())
.AsImplementedInterfaces()
.WithScopedLifetime());
๐ Middleware Layers Injected¶
app.UseMiddleware<ExceptionMiddleware>();
app.UseMiddleware<ObservabilityMiddleware>();
app.UseMiddleware<JwtValidationMiddleware>();
app.UseMiddleware<TenantResolverMiddleware>();
app.UseMiddleware<RateLimitMiddleware>();
app.UseRouting();
app.UseEndpoints(...);
Each middleware implements:
ILoggerfor structured logsActivitySourcefor tracingIMetricsEmitteror custom Prometheus exporter for metrics
๐ฆ Generated Solution Patterns¶
| File | Stack Role |
|---|---|
Program.cs |
Minimal hosting model using .NET 8 |
Startup.cs |
Optional legacy mode for plug-and-play policies |
RateLimitMiddleware.cs |
Built on MemoryCache or IMeter |
TracingSetup.cs |
OpenTelemetry ActivitySource scoped per request |
MetricsExporter.cs |
Prometheus-compatible metric emitter |
AuthMiddleware.cs |
Token handler with fallback, claims enrichment |
ObservabilityMiddleware.cs |
Adds X-Trace-Id, logs latency, errors |
๐ Observability Tools¶
| Integration | Detail |
|---|---|
| Serilog | Console, Seq, or Application Insights |
| Prometheus | /metrics endpoint auto-exposed |
| OpenTelemetry | Exports traces to Jaeger, Zipkin, or Azure Monitor |
| Correlation Id | Injects and propagates X-Correlation-ID, X-Tenant-ID |
๐งช Test Stack (if --withTests enabled)¶
- MSTest + FluentAssertions
- Moq for service/plugin mocking
- Tracing/metrics mocking helpers for OTel
๐ Project Scaffolding and Folder Layout¶
๐ฏ Purpose¶
This section defines the standard structure of the emitted .NET 8-based API Gateway project โ enabling clean separation of concerns, extensibility, and predictable discovery of plugins, policies, and configuration.
The structure aligns with Clean Architecture principles and is optimized for modularity, testing, and observability.
๐งฑ Root Structure โ Example¶
๐ ApiGateway.Vetspire.Appointments/
โโโ ApiGateway.Vetspire.Appointments.csproj
โโโ Program.cs
โโโ Startup.cs
โโโ Properties/
โ โโโ launchSettings.json
โโโ Configuration/
โ โโโ routes.json
โ โโโ gateway-settings.json
โ โโโ edition-policy.json
โโโ Routing/
โ โโโ RouteRegistry.cs
โ โโโ RouteBuilder.cs
โ โโโ RoutingConstants.cs
โโโ Middleware/
โ โโโ JwtValidationMiddleware.cs
โ โโโ TenantResolverMiddleware.cs
โ โโโ RateLimiterMiddleware.cs
โ โโโ ObservabilityMiddleware.cs
โโโ Plugins/
โ โโโ IRouteFilter.cs
โ โโโ PluginRegistry.cs
โ โโโ ExamplePlugin.cs
โโโ Policies/
โ โโโ CircuitBreaker.cs
โ โโโ RetryPolicy.cs
โ โโโ EditionAccessPolicy.cs
โโโ Observability/
โ โโโ SerilogConfig.cs
โ โโโ TracingSetup.cs
โ โโโ MetricsExporter.cs
โโโ Docs/
โ โโโ gateway.routes.md
โ โโโ observability.md
โ โโโ README.md
โโโ CI/
โ โโโ azure-pipelines.yml
โ โโโ Dockerfile
โโโ Tests/
โ โโโ AuthTests.cs
โ โโโ RouteTests.cs
โ โโโ PolicyTests.cs
โโโ version.json
๐ Folder Responsibilities¶
| Folder | Responsibility |
|---|---|
Configuration/ |
All static and dynamic gateway configuration artifacts |
Routing/ |
Classes for building and mapping routes to downstream services |
Middleware/ |
All custom UseMiddleware<T> components |
Plugins/ |
Extensible plugin injection points with discovery support |
Policies/ |
Resilience, edition-based access, feature-gating |
Observability/ |
Setup and emitters for metrics, traces, logs |
Docs/ |
Auto-generated markdown for maintainers, developers, and ops |
CI/ |
Build, deploy, and containerization logic |
Tests/ |
Unit and integration tests scoped by concern |
Properties/ |
Default debug/run profiles |
๐งฉ Code Entry Points¶
| File | Role |
|---|---|
Program.cs |
Minimal host setup using WebApplicationBuilder |
Startup.cs |
Optional full DI + config registration (legacy support) |
RouteRegistry.cs |
Central place to register all mapped routes |
PluginRegistry.cs |
Discovers and registers all plugin-based extensions |
JwtValidationMiddleware.cs |
Handles token validation pipeline |
TracingSetup.cs |
Initializes OpenTelemetry spans and exporters |
๐ Naming Convention and Package Identity¶
- Namespace:
ConnectSoft.Gateways.{BoundedContext} - Project:
ApiGateway.{Context}(e.g.,ApiGateway.Vetspire.Appointments) - Internal class prefixes:
Gateway*(e.g.,GatewayRoutePolicy,GatewayOptions)
๐ง Dynamic Path Injection¶
During generation:
- Folder and class names adapt based on
contextName,apiId,editionSupport, etc. RouteRegistry.csauto-generates stub methods for each declared route.- Feature folders are included or excluded via template symbol switches (e.g.,
--withRateLimiter,--withTracing).
๐ฃ Route Mapping and Path Normalization¶
๐ฏ Purpose¶
This section details how the agent maps API Designer routes into the gateway's routing infrastructure โ transforming them into normalized paths, method-to-service bindings, and injecting required authorization and tenant awareness metadata.
The resulting routes are injected into RouteRegistry.cs and optionally exported to routes.json or OpenAPI-compatible files for downstream agents.
๐งฉ Route Definition Pipeline¶
The API Gateway Generator Agent:
- Receives route definitions from the API Designer Agent or DSL
- Normalizes the path structure to ensure consistent format and casing
- Applies route-wide metadata: method, scopes, rate limits, edition
- Generates route-to-service forwarding code or configuration
๐งฑ Normalized Route Shape¶
{
"path": "/v1/appointments/{id}",
"method": "GET",
"service": "AppointmentsService",
"auth": {
"type": "jwt",
"scope": "appointments.read"
},
"tenantHeader": "X-Tenant-ID",
"edition": "Pro",
"rateLimit": "10r/s"
}
๐ Generated C# Mapping¶
In RouteRegistry.cs:
app.MapGet("/v1/appointments/{id}", async context =>
{
var result = await _forwarder.ForwardAsync(context, "AppointmentsService");
return result;
})
.RequireAuthorization("appointments.read")
.WithMetadata(new TenantScope("X-Tenant-ID"))
.WithRateLimit("10r/s")
.WithEditionGate("Pro");
๐ Path Normalization Logic¶
| Rule | Purpose |
|---|---|
| Convert camelCase to kebab-case | /getAppointmentById โ /get-appointment-by-id |
| Add version prefix | /appointments โ /v1/appointments |
| Ensure trailing slash consistency | /path/ and /path behave the same |
| Auto-parameterize ids | /appointments/{id} if not already formatted |
๐ Authorization Metadata Injection¶
If the route defines:
Then the agent injects:
The scope name is normalized to match platform naming conventions.
๐งฌ Edition Gating¶
If route is flagged for edition enforcement:
The agent wraps the route in:
Backed by EditionGateMiddleware, which evaluates tenant claims.
๐งช Route-Level Validations¶
Before emitting each route:
- Ensures no duplicate path+method pair exists
- Confirms target service alias exists in service map
- Checks that required scope exists in OpenIddict catalog
- Verifies that edition name is valid (
Basic,Pro,Enterprise, etc.) - Confirms tenant routing strategy matches blueprint
๐ค Optional Export Formats¶
| Format | File | Purpose |
|---|---|---|
| JSON | routes.json |
For external pipeline consumption |
| Markdown | gateway.routes.md |
For human-readable summary |
| YAML | routes.yaml |
For third-party tooling compatibility |
| Code | RouteRegistry.cs |
Executable C# configuration |
๐ Authentication Layer โ JWT, API Keys, and Custom Claims¶
๐ฏ Purpose¶
This section defines how the API Gateway Generator Agent embeds authentication logic into the generated gateway โ ensuring that each route enforces the proper token validation strategy and scopes while supporting multi-strategy fallback (e.g., JWT or API Key).
๐ Supported Authentication Modes¶
| Mode | Description | Used When |
|---|---|---|
| JWT (OAuth2) | Validates bearer token issued by OpenIddict | Standard for SaaS APIs |
| API Key | Validates key in header/query against known hash/store | For automation, partners |
| Anonymous | No validation, optionally scoped by edition/plan | Public APIs only |
| Hybrid | Supports both JWT and API Key with precedence rules | Special use cases |
๐ง JWT Validation Middleware¶
Generated class: JwtValidationMiddleware.cs
Responsibilities:
- Validates access token signature using OpenIddict discovery
- Verifies
exp,aud,issclaims - Extracts
sub,scope,tenant_id, andeditionclaims - Optionally enforces scope based on route metadata
๐ง Agent-Known JWT Conventions¶
| Claim | Usage |
|---|---|
sub |
User or system identity |
scope |
Required for route access |
tenant_id |
Used by TenantResolverMiddleware |
edition |
Used by EditionGateMiddleware |
role |
Optional for role-based policy enforcement |
These are resolved from the factory-wide OpenIddict setup (via the Identity Server Agent).
๐ API Key Middleware¶
Generated class: ApiKeyValidationMiddleware.cs
Supports:
- API Key in
X-API-Keyheader orapikeyquery - Key-to-scope mapping via dictionary or configuration
- Key rotation support via
IApiKeyProvider
๐งฉ Fallback Flow: Hybrid Auth Mode¶
If both auth.type: jwt and auth.fallback: apikey are defined:
- Try JWT validation first
- If JWT missing or invalid, check API key
- If both fail, return 401
๐ Configured via:¶
{
"auth": {
"type": "jwt",
"fallback": "apikey",
"jwt": {
"authority": "https://identity.connectsoft.dev",
"audience": "vetspire"
},
"apikey": {
"header": "X-API-Key",
"validKeys": ["abc123", "xyz456"]
}
}
}
๐ Auth Mapping in Route Metadata¶
| Property | Description |
|---|---|
auth.type |
jwt, apikey, or none |
requiredScope |
e.g., appointments.read, orders.write |
roles (optional) |
admin, reader, etc. for claim matching |
Injected into route builder like:
๐งช Authentication Test Coverage¶
Tests emitted in AuthTests.cs:
- Valid token access with correct scope
- Rejection with invalid or expired token
- API key success/failure
- Hybrid mode token/key fallback
- Edition and tenant claim checks
๐ง Key Injection via DI¶
| Interface | Purpose |
|---|---|
ITokenValidator |
Abstract token validator for unit testing |
IApiKeyProvider |
Provides keys and associated metadata |
IClaimMapper |
Translates claim sets to policy decisions |
๐ Authorization and Edition Gating¶
๐ฏ Purpose¶
This section defines how the agent enforces access control within the API Gateway โ ensuring that each route is guarded not only by authentication, but also by authorization scopes, roles, and ConnectSoft edition-based restrictions (e.g., Free, Pro, Enterprise).
๐ Route-Level Authorization Enforcement¶
Every route can define:
| Property | Meaning |
|---|---|
requiredScope |
OAuth2 scope required to access the route |
roles |
Optional: claim-based role required |
edition |
Minimum edition required to access |
The generator translates these into attribute-based or middleware-enforced policies.
app.MapGet("/v1/appointments", ...)
.RequireAuthorization("appointments.read")
.WithEditionGate("Pro")
.WithRoles("admin", "staff");
๐ง Scope Policy Mapping¶
The agent uses convention-based or discovered mappings:
| Scope Name | Injected Behavior |
|---|---|
*.read |
HTTP GET |
*.write |
POST/PUT |
*.delete |
DELETE |
*.admin |
Admin-only panels or functions |
Supported via .AddPolicy(scope, ...) in Program.cs or Startup.cs.
๐งพ Role-Based Access Support¶
If route includes roles array:
Then middleware is injected to validate claims["role"] contains at least one match.
๐ฏ Edition-Based Gating¶
| Feature | Function |
|---|---|
WithEditionGate("Pro") |
Prevents route access unless edition claim โฅ Pro |
| Middleware Class | EditionGateMiddleware.cs |
| Edition Source | Claim edition, resolved via OpenIddict token or plugin |
| Fallback | Optional: route can return 403 with JSON explaining upgrade requirement |
๐ Edition Hierarchy (default)¶
Sorted and resolved using ordinal strategy or dictionary override from platform config.
๐ Middleware Chain¶
JwtValidationMiddlewareTenantResolverMiddlewareEditionGateMiddlewareScopeAuthorizationMiddlewareRoute Execution
๐ Configuration Support¶
In edition-policy.json:
๐งช Edition & Auth Tests (in PolicyTests.cs)¶
| Test | Description |
|---|---|
| Valid token with Pro edition โ โ | |
| Valid token with Free edition on Pro route โ โ | |
| Token missing edition claim โ fallback or fail | |
| Missing scope โ returns 403 | |
| Missing role (if required) โ returns 403 |
๐ง Agent Memory & Auto-Fix¶
If a route is defined with auth.type = jwt but no scope:
- Agent injects default:
scope = {resource}.read - If edition is undefined โ injects
Freeas base edition
๐ข Tenant Isolation and Context Resolution¶
๐ฏ Purpose¶
This section defines how the agent embeds multi-tenant awareness into the generated gateway. It ensures that every request is scoped by tenant โ isolating traffic, applying limits, enforcing edition gates, and supporting SaaS and B2B models by default.
๐ง Tenant Resolution Strategies¶
The gateway supports pluggable tenant identification, resolved at runtime through one of the following:
| Strategy | Source | Middleware |
|---|---|---|
| Header | X-Tenant-ID (or configurable) |
TenantResolverMiddleware.cs |
| JWT Claim | tenant_id in bearer token |
Extracted by JwtValidationMiddleware |
| Subdomain | {tenant}.api.connectsoft.dev |
HostTenantParser.cs (optional) |
| API Key Metadata | Mapped to tenant in key store | Injected via IApiKeyProvider |
These can be layered or combined depending on blueprint inputs.
๐งฑ Middleware Injection¶
Class: TenantResolverMiddleware.cs
Function:
- Resolves tenant ID from configured source
- Stores in
HttpContext.Items["TenantId"] - Logs, traces, and passes through for metrics
- Optionally validates tenant existence via
ITenantValidator
๐ Routing with Tenant Awareness¶
Generated routing entries include tenant context:
Or:
๐ Configuration Support¶
From gateway-settings.json:
Or via prompt flags:
๐ก Enforcement Behavior¶
| Feature | Description |
|---|---|
| โ Required | If tenant is missing โ 400/403 based on config |
| ๐งฉ Optional | If required = false, fallback to "anonymous" tenant |
| ๐ Isolation | Tenants cannot access othersโ resources or routes |
| ๐ Metrics | All observability tagged by tenant automatically |
๐ Observability Hooks¶
Tenant ID is injected into:
- Serilog
LogContext - OpenTelemetry
Activity(astenant_idattribute) - Prometheus metrics (via
labels["tenant"])
This enables tenant-specific dashboards, tracing, and quota analysis.
๐ง Plugin Extension Points¶
| Interface | Purpose |
|---|---|
ITenantResolver |
Custom resolution logic (e.g., encrypted headers) |
ITenantPolicyProvider |
Applies tenant-specific rate limits, plugin toggles |
ITenantValidator |
Confirms existence and state (active/inactive) |
๐งช Tenant Tests (in TenantTests.cs)¶
| Test | Description |
|---|---|
| Valid header โ tenant resolved โ | |
| Missing tenant header โ 403 โ | |
| Conflicting header + token claim โ fail or warn | |
| Subdomain resolution โ parses tenant name | |
| Invalid tenant โ returns 404 or customizable error |
๐ฆ Rate Limiting and Quotas¶
๐ฏ Purpose¶
This section explains how the API Gateway Generator Agent enforces per-tenant, per-route, or per-edition quotas and throttling policies in the generated gateway โ ensuring fairness, platform protection, and SaaS plan enforcement.
๐ก Supported Rate Limit Strategies¶
| Strategy | Description | Use Case |
|---|---|---|
| Per-Tenant | Each tenant has independent quota (e.g., 100 req/min) | Standard for multi-tenant SaaS |
| Per-Route | Different endpoints have custom thresholds | Expensive or admin APIs |
| Per-Edition | Pro tenants get higher limits than Free | Tier-based plans |
| Global | Fixed total cap across gateway | Traffic protection in emergencies |
๐งฑ Middleware and Configuration¶
Generated Middleware: RateLimiterMiddleware.cs
- Injected via
app.UseMiddleware<RateLimiterMiddleware>(); - Uses in-memory or distributed store (Redis, optionally)
- Per-request evaluation based on context
โ๏ธ Configuration Format¶
Defined in gateway-settings.json or DSL:
{
"rateLimits": {
"default": "100r/m",
"perRoute": {
"/v1/payments": "25r/m",
"/v1/reports": "10r/m"
},
"perEdition": {
"Free": "60r/m",
"Pro": "200r/m",
"Enterprise": "1000r/m"
}
}
}
๐ Runtime Token Bucket or Sliding Window¶
Implementation supports:
- Token bucket algorithm
- Sliding window counter
- Burst handling (
burst: 5)
๐งฉ Resolution Logic¶
For each request:
- Resolve
tenant_id - Determine route and edition
- Match against
perRoute,perTenant,perEditionrules - Apply strictest rule or fail if over quota
If match fails:
- Return
429 Too Many Requests - Include
Retry-Afterheader
๐ Observability Integration¶
Emits:
rate_limit.exceededlogs- Prometheus counter:
gateway_rate_limit_exceeded_total{tenant, route} - Trace event:
RateLimitDroppedRequest
๐งช Example Test Case¶
[TestMethod]
public async Task Tenant_Should_Be_Rate_Limited_If_Exceeding_Quota()
{
// Simulate 101 requests in 60 seconds
// Expect final response to be 429
}
๐ง Feature Flags and Prompt Symbols¶
| Prompt | Description |
|---|---|
--useRateLimit |
Enables rate limiting |
--rateStrategy edition |
Uses edition-specific quotas |
--rateBurst 5 |
Enables burst tokens |
--failOnRateExceed false |
Optional soft cap (logs instead of blocking) |
๐ก Plugin Extension¶
| Interface | Function |
|---|---|
IRateLimitProvider |
Can load limits from DB or API |
IRateLimitStrategy |
Switch algorithms (leaky bucket, token bucket) |
โป๏ธ Retry Policy, Circuit Breakers, and Resilience¶
๐ฏ Purpose¶
This section defines how the agent enables fault-tolerance and graceful degradation in the generated API Gateway. It scaffolds mechanisms to automatically retry, fail fast, or fallback based on downstream service behavior, preventing cascading failures and improving platform reliability.
๐ Retry Strategy¶
| Feature | Description |
|---|---|
| Transient fault retry | Retries failed downstream calls (e.g., HTTP 5xx, timeouts) |
| Configurable max attempts | Usually 2โ3 retries |
| Exponential backoff | Optional with jitter |
| Scoped per route | E.g., retry payments but not file uploads |
๐ Circuit Breaker Strategy¶
| Feature | Description |
|---|---|
| Per-route or per-service circuit | Detects repeated failures and opens the circuit |
| Open/half-open states | Based on rolling window of failures |
| Fail-fast logic | Short-circuits requests to avoid further pressure |
| Recovery timer | Periodically attempts to resume traffic |
โ๏ธ Middleware and Policy Injection¶
Generated Classes:
RetryPolicyMiddleware.csCircuitBreakerMiddleware.cs
Injected in the pipeline:
Policies are applied based on route annotations or config.
๐งพ Configuration Example¶
{
"resilience": {
"retry": {
"default": {
"maxAttempts": 3,
"backoff": "exponential",
"jitter": true
},
"perRoute": {
"/v1/payments": {
"maxAttempts": 2
}
}
},
"circuitBreaker": {
"threshold": 5,
"duration": "30s",
"reset": "60s"
}
}
}
๐ Route Example with Resilience Metadata¶
app.MapPost("/v1/orders", ...)
.WithRetryPolicy(attempts: 3)
.WithCircuitBreaker(threshold: 5, duration: TimeSpan.FromSeconds(30));
๐งฉ Metrics Emitted¶
| Metric | Description |
|---|---|
gateway_retry_attempts_total |
Number of retries per route/tenant |
gateway_circuit_open_total |
Circuit open events |
gateway_circuit_half_open_total |
Probing |
gateway_circuit_closed_total |
Recovery events |
Tagged by:
- route
- tenant
- service
๐ง Plugin Interfaces¶
| Interface | Purpose |
|---|---|
IRetryPolicyStrategy |
Override default retry logic (e.g., constant backoff) |
ICircuitBreakerProvider |
Define external thresholds or toggle breakers |
IFallbackHandler |
Provide degraded behavior or cache fallback |
๐งช Test Coverage¶
| Test | Description |
|---|---|
| Simulate 3 transient failures โ retry succeeds | |
| Force circuit open โ next call blocked | |
| Wait recovery โ circuit half-opens and resumes | |
| Jitter and delay respected in backoff |
๐ง Prompt Symbols¶
| Flag | Effect |
|---|---|
--withRetry |
Enables retry scaffolding |
--retryMax=3 |
Set max attempts |
--withCircuitBreaker |
Enables breaker |
--cbThreshold=5 |
Set failure threshold |
--cbDuration=30s |
Breaker open duration |
๐ Plugin Architecture and Extensibility¶
๐ฏ Purpose¶
This section explains how the generated API Gateway supports modular extensibility via a structured plugin architecture โ allowing the platform or downstream teams to plug in behavior at runtime without modifying core code.
This is crucial for scaling across domains, injecting observability, modifying behavior per tenant or edition, and supporting external integration.
๐งฑ Core Plugin Architecture Concepts¶
| Concept | Role |
|---|---|
| Plugins | Self-contained components injected at specific lifecycle points |
| Hook Points | Extension interfaces invoked during request pipeline or route resolution |
| Registry | Automatically discovers and registers plugins via DI |
| Isolation | Plugins have scoped access, no cross-plugin interference |
๐ Folder Structure¶
/Plugins/
โโโ IGatewayPlugin.cs
โโโ IRouteFilter.cs
โโโ IResponseEnricher.cs
โโโ PluginRegistry.cs
โโโ MetricsEnrichmentPlugin.cs
โโโ TenantAuditPlugin.cs
๐งฉ Supported Plugin Interfaces¶
| Interface | Hook Point | Description |
|---|---|---|
IGatewayPlugin |
Global | Marker for discovery |
IRouteFilter |
Pre-routing | Modify or validate route requests |
IResponseEnricher |
Post-routing | Inject headers, telemetry, mutation |
IMetricsObserver |
Tracing | Export custom metrics or spans |
IAuthorizationAugmentor |
Auth | Extend claims or access checks |
ITenantPolicyProvider |
Multi-tenancy | Inject tenant/edition-specific logic |
ICustomRateLimiter |
Rate limiting | Override quota logic per tenant |
๐ Plugin Lifecycle¶
sequenceDiagram
participant Client
participant Gateway
participant Plugin as CustomPlugin
Client->>Gateway: HTTP Request
Gateway->>Plugin: OnRouteMatch
Plugin-->>Gateway: Modified request (optional)
Gateway->>Plugin: OnAuthorize
Plugin-->>Gateway: Auth decision
Gateway->>Service: Forwarded call
Gateway->>Plugin: OnResponse
Plugin-->>Gateway: Add telemetry, headers
Gateway-->>Client: HTTP Response
โ๏ธ Plugin Registration¶
In PluginRegistry.cs:
services.Scan(scan => scan
.FromAssemblyOf<IGatewayPlugin>()
.AddClasses(classes => classes.AssignableTo<IGatewayPlugin>())
.AsImplementedInterfaces()
.WithScopedLifetime());
This ensures zero manual DI for new plugins.
๐งช Example Plugin¶
public class TenantAuditPlugin : IRouteFilter
{
public async Task InvokeAsync(HttpContext context)
{
var tenant = context.GetTenantId();
Log.Information("Routing request for tenant {Tenant}", tenant);
await Task.CompletedTask;
}
}
๐งฐ Plugin Use Cases¶
| Plugin | Use Case |
|---|---|
ResponseEnricher |
Inject latency headers |
IRateLimiter |
Dynamic quota by time of day |
AuthorizationAugmentor |
Validate feature flags or AB test groups |
ITenantPolicyProvider |
Adjust circuit breaker strategy per tenant |
๐ง Prompt Support¶
| Flag | Description |
|---|---|
--withPlugins |
Enables plugin support |
--pluginTypes=metrics,audit |
Includes prebuilt plugins |
--customPluginHook IMyPlugin |
Scaffold new hook |
๐ Observability of Plugins¶
Plugins can:
- Emit logs via Serilog
- Register custom OpenTelemetry spans
- Export metrics to Prometheus using injected IMeter
๐ก Observability and Tracing¶
๐ฏ Purpose¶
This section outlines how the generated API Gateway is equipped with full observability-first instrumentation โ including structured logging, distributed tracing, and metrics export โ to support platform SREs, DevOps, and AI agents in diagnostics, QoS analysis, and health monitoring.
Observability is embedded by default in every request lifecycle.
๐ Observability Pillars¶
| Pillar | Tooling | Output |
|---|---|---|
| Logs | Serilog | Structured logs to console, files, Seq, or Azure |
| Metrics | OpenTelemetry + Prometheus | Request count, duration, rate limits, failures |
| Traces | OpenTelemetry + Jaeger/Zipkin | Full span tree for each request path |
๐งฑ Middleware Stack¶
app.UseMiddleware<ObservabilityMiddleware>();
app.UseMiddleware<TracingMiddleware>();
app.UseMiddleware<RateLimiterMiddleware>();
Generated middleware includes:
- Activity start/stop hooks
- Correlation ID propagation
- Tenant and route tagging
- Automatic failure detection
๐ Log Format¶
{
"timestamp": "2025-06-14T10:30:12Z",
"level": "Information",
"message": "Handled request",
"tenant": "vetspire",
"route": "/v1/appointments",
"durationMs": 87,
"status": 200,
"traceId": "01F7YQ...",
"edition": "Pro"
}
Generated using Serilog with enrichers:
X-Tenant-IDX-Edition-IDX-Correlation-ID
๐ Metrics Exposed¶
Prometheus-compatible /metrics endpoint:
| Metric | Labels |
|---|---|
gateway_requests_total |
method, route, status, tenant, edition |
gateway_latency_seconds |
Histogram: le, route, status |
gateway_rate_limit_exceeded_total |
route, tenant |
gateway_plugin_errors_total |
plugin, route |
gateway_auth_failure_total |
method, auth_type, reason |
๐ Distributed Tracing¶
Each request generates a trace tree:
graph LR
A[StartSpan: Incoming Request] --> B[Middleware: JWT]
B --> C[RouteMatch Span]
C --> D[DownstreamCall: OrdersService]
D --> E[PluginHook: Audit]
E --> F[Return Span]
Using ActivitySource from OpenTelemetry.Trace.
Traces are:
- Exported to Jaeger, Zipkin, or Azure Monitor
- Correlated via
traceparentandbaggageheaders - Scoped per tenant, route, plugin
๐ง Configuration¶
In gateway-settings.json:
{
"observability": {
"enableTracing": true,
"traceExporter": "jaeger",
"metricsExporter": "prometheus",
"logLevel": "Information",
"enrich": ["tenant", "edition", "traceId"]
}
}
๐ File Outputs (Optional)¶
observability.md: Human-readable doc of all injected telemetrytracing.json: Sample trace structuremetrics.yaml: Describes exposed metrics with expected values
๐งช Tests¶
| Test | Description |
|---|---|
| Trace emitted with route & tenant tags | |
| Metrics endpoint exposes latency histogram | |
| Logs include correlation ID for every request |
๐ง Observability Prompt Flags¶
| Flag | Description |
|---|---|
--withObservability |
Enable all layers |
--traceExporter=jaeger |
Set tracing backend |
--logEnrich=tenant,traceId |
Customize log fields |
--prometheusPort=9191 |
Expose metrics on custom port |
๐ API Documentation and Route Manifest¶
๐ฏ Purpose¶
This section explains how the agent emits self-describing documentation for the generated gateway, including a structured Route Manifest, OpenAPI-compatible exports, and developer-oriented markdowns.
This ensures:
- Gateway consumers (internal/external) know what is exposed
- Edition-specific route restrictions are visible
- QA and Ops can validate route behaviors before deploy
๐ Output Artifacts¶
| File | Purpose |
|---|---|
gateway.routes.md |
Human-readable table of all routes |
routes.json |
Machine-readable manifest |
routes.openapi.json |
Optional OpenAPI export |
route-version.map |
Maps endpoints to supported API versions |
docs/observability.md |
Describes all observability hooks |
docs/plugins.md |
Lists registered plugins and their scope |
๐ Markdown Output (gateway.routes.md)¶
Generated content (per route):
| Path | Method | Auth | Scope | Edition | Rate Limit |
|---|---|---|---|---|---|
/v1/appointments |
GET | โ JWT | appointments.read |
Free | 100/min |
/v1/reports/export |
POST | โ JWT + APIKey | reports.write |
Pro | 10/min |
Includes annotations:
- โ = Requires Auth
- ๐ = Pro+ Only
- ๐ = Retry-enabled
- ๐งฉ = Plugin extended
๐งฌ routes.json Example¶
[
{
"path": "/v1/appointments",
"method": "GET",
"auth": "jwt",
"scope": "appointments.read",
"edition": "Free",
"rateLimit": "100r/m",
"version": "v1"
},
{
"path": "/v1/reports/export",
"method": "POST",
"auth": "jwt+apikey",
"scope": "reports.write",
"edition": "Pro",
"rateLimit": "10r/m",
"plugins": ["AuditPlugin", "MetricsPlugin"]
}
]
This is consumed by:
- QA agents for test generation
- Documentation tools (render as table)
- Deployment pipeline for policy syncing
๐ OpenAPI Export (Optional)¶
Flag: --emitOpenApi
- Generates
routes.openapi.jsonusing Swashbuckle-style schema - Supports tags, descriptions, response codes
- Edition and auth shown in extensions:
๐ง Prompt Symbols¶
| Flag | Description |
|---|---|
--withDocs |
Enables markdown and JSON manifest output |
--emitOpenApi |
Adds Swagger-style schema |
--docFormat=md,json,yaml |
Choose output formats |
--docOut=docs/routes/ |
Specify folder location |
๐ง Extension Points¶
| Plugin Hook | Purpose |
|---|---|
IRouteDocEnhancer |
Inject descriptions, deprecation flags |
IApiDocTransformer |
Generate alternative formats (PDF, Confluence) |
๐งช QA Use Cases¶
- Validate edition locks per route
- Detect missing auth for sensitive endpoints
- Match OpenIddict scopes to expected resources
- Diff
routes.jsonin CI/CD for changes
๐ Metrics¶
- Routes emitted count
- Authenticated vs anonymous routes
- Coverage by edition
๐ฉบ Health Checks, Status Pages, and Gateway Readiness¶
๐ฏ Purpose¶
This section defines how the generated gateway includes health endpoints, readiness probes, and liveness indicators. These endpoints allow platform orchestrators, human operators, and AI agents to assess whether the gateway is operational, degraded, or unhealthy.
They are structured for compatibility with:
- Kubernetes probes
- Azure App Service health check path
- ConnectSoft SRE tools
๐งช Health Endpoints Included¶
| Path | Purpose | Type |
|---|---|---|
/healthz |
Liveness check | HTTP 200 = healthy |
/readyz |
Readiness check | Validates internal DI, service maps, policies |
/status |
Human-readable HTML or JSON summary | Runtime diagnostics |
๐ง Implementation¶
Generated using .NET Health Checks with:
IHealthCheckimplementations- Custom middleware for status formatting
- Registration via DI pipeline
services.AddHealthChecks()
.AddCheck<GatewayPolicyHealthCheck>("GatewayPolicies")
.AddCheck<TenantRegistryHealthCheck>("TenantRegistry")
.AddCheck<PluginHealthCheck>("Plugins");
๐งฉ Example Output โ /healthz¶
{
"status": "Healthy",
"results": {
"GatewayPolicies": { "status": "Healthy" },
"PluginRegistry": { "status": "Healthy" },
"TenantMap": { "status": "Degraded", "description": "Some tenants missing configs" }
}
}
- Supports
Healthy,Degraded,Unhealthy - Returns
503onUnhealthy, customizable onDegraded
๐ Readiness Dependencies¶
- Plugin registry initialized
- Tenant + edition maps valid
- Downstream route targets parse successfully
- Tracing and metrics exporters are initialized
All must pass for /readyz to return 200.
๐งพ Human Status Page โ /status¶
If --withStatusPage flag is used, a simple HTML or JSON page is added:
{
"gateway": "ApiGateway.Vetspire.Appointments",
"environment": "Production",
"uptime": "4d 21h 32m",
"version": "1.0.0",
"routes": 42,
"observability": {
"tracing": "Jaeger",
"metrics": "Prometheus"
}
}
Useful for:
- Human operators
- Ops dashboards
- Snapshot comparisons in test environments
๐ง Prompt Flags¶
| Flag | Description |
|---|---|
--withHealth |
Enables /healthz and /readyz |
--healthProbes k8s |
Injects Kubernetes annotations or YAML |
--statusPage html |
Adds /status endpoint |
--failOnDegraded true |
Forces readiness probe failure on degraded services |
๐ Observability Integration¶
| Metric | Description |
|---|---|
gateway_healthcheck_failures_total |
Labelled by probe and failure type |
gateway_status_last_refresh |
Uptime and environment tracking |
gateway_degraded_services |
Count of degraded but available components |
๐งช Tests Included¶
| Test | Description |
|---|---|
| Healthcheck returns 200 | When all services OK |
| Readiness fails | When tenant config missing |
| Plugin health degrades status | When plugin registration throws |
/status |
Returns expected fields with runtime details |
โ๏ธ Configuration Model and Edition-Aware Settings¶
๐ฏ Purpose¶
This section defines how the generated gateway includes a centralized, validated, and edition-aware configuration model. The configuration is:
- Fully structured and validated
- Supports runtime overrides (env vars, secrets)
- Enables edition-specific behaviors at startup and runtime
๐ Files and Structure¶
| File | Description |
|---|---|
appsettings.json |
Default configuration |
gateway-settings.json |
ConnectSoft-specific extensions |
appsettings.{env}.json |
Environment-specific overrides |
edition-policy.json |
Defines edition rules and overrides |
secrets.{env}.json (optional) |
API keys, sensitive values (not committed) |
๐งฑ Configuration Domains¶
| Section | Purpose |
|---|---|
Routing |
Route-to-service mapping, versions |
Tenancy |
Resolution mode, headers, validation |
Auth |
JWT, API Key, fallback policies |
EditionPolicies |
Route edition access, rate quotas |
Observability |
Logging, tracing, metrics exporters |
Resilience |
Retry, circuit breakers |
RateLimits |
Global, per-tenant, per-edition limits |
Plugins |
Activation and DI of plugins |
๐งฉ Edition-Aware Configuration Support¶
Allows settings to dynamically resolve per edition.
{
"features": {
"enableAuditTrail": {
"Free": false,
"Pro": true,
"Enterprise": true
},
"rateLimit": {
"Free": "60r/m",
"Pro": "250r/m"
}
}
}
Generated helper:
๐ง Environment-Aware Overrides¶
Reads from:
Environment.GetEnvironmentVariable(...)appsettings.{ENVIRONMENT}.json- Azure App Configuration or Key Vault (if injected)
๐ฆ Strongly-Typed Configuration¶
The gateway uses IOptions<T> and IValidateOptions<T>:
services.Configure<GatewaySettings>(configuration.GetSection("Gateway"));
services.AddSingleton<IValidateOptions<GatewaySettings>, GatewaySettingsValidator>();
Auto-generated settings classes:
GatewaySettings.csTenantSettings.csAuthSettings.cs
Each includes data annotations for validation:
๐ง Prompt Flags¶
| Flag | Description |
|---|---|
--withConfig |
Injects base config + environment overrides |
--editionAware true |
Enables edition-based features |
--configOut=./config/ |
Emits config files to specified folder |
--withSecrets |
Adds example secrets.{env}.json file |
--azureAppConfig |
Enables Azure App Config integration |
--keyVault |
Enables secret resolution from Key Vault |
๐งช Configuration Tests¶
| Test | Description |
|---|---|
| Validate required sections loaded | |
| Edition fallback works as expected | |
| Invalid config file returns detailed startup error | |
| Environment overrides respected |
๐ง Self-Documenting Output¶
Optionally generates:
docs/configuration.md: Human-readable summarydocs/editions.md: Edition rules per route and setting
๐งฌ Edition Feature Enforcement and Feature Flags¶
๐ฏ Purpose¶
This section details how the API Gateway Generator Agent embeds edition-specific logic and feature flag control, ensuring that each tenant's edition (e.g., Free, Pro, Enterprise) dynamically governs access to routes, behavior toggles, and premium platform capabilities.
The system allows:
- SaaS edition control at runtime
- Feature enablement per edition or tenant
- Dynamic control without redeployment
๐งฉ Feature Enforcement Flow¶
graph TD
A[Incoming Request] --> B[Resolve Edition from Claims or Tenant Profile]
B --> C{Feature Required?}
C -- Yes --> D{Edition Allows Feature?}
D -- No --> E[Return 403 Upgrade Required]
D -- Yes --> F[Proceed with Execution]
C -- No --> F
๐ Feature Flag Definitions (Structured JSON)¶
Stored in edition-feature-flags.json or service-wide config:
{
"features": {
"auditTrail": {
"Free": false,
"Pro": true,
"Enterprise": true
},
"extendedExport": {
"Free": false,
"Pro": false,
"Enterprise": true
},
"realtimeTelemetry": {
"Pro": true,
"Enterprise": true
}
}
}
๐งฑ Code-Level Enforcement¶
Or inside middleware/plugin:
if (!FeatureEnforcer.Allows("realtimeTelemetry", edition))
return Results.Forbid("Upgrade to Pro or Enterprise required.");
๐ง Generated Helper Class¶
public interface IFeatureEnforcer
{
bool Allows(string feature, string edition);
bool AllowsForTenant(string feature, string tenantId);
string[] GetAvailableFeatures(string edition);
}
With in-memory, cached, and optional distributed versions.
๐ Per-Tenant Feature Override Support¶
Tenant profile can override feature matrix:
Resolved at runtime using ITenantFeatureStore.
๐ฆ Plugin & Middleware Integration¶
Plugins can declare features they depend on:
public class AuditPlugin : IGatewayPlugin
{
public string[] RequiredFeatures => new[] { "auditTrail" };
}
Disabled if feature not present for current tenant/edition.
๐งช Test Cases (FeatureTests.cs)¶
| Test | Description |
|---|---|
Pro tenant allowed to export audit logs |
|
Free tenant blocked from extendedExport |
|
| Tenant with override gets feature even if edition does not | |
| Plugin not registered if feature disabled |
๐ Observability Integration¶
- Feature decisions logged at debug level
-
Metrics:
-
gateway_feature_enabled_total{feature,tenant} gateway_feature_blocked_total{feature,tenant}
๐ง Prompt Flags¶
| Flag | Description |
|---|---|
--withFeatures |
Enables edition-aware feature system |
--featureMatrixFile |
Path to feature config |
--enableFeature auditTrail,realtimeTelemetry |
Predefine active features |
--tenantOverridesFromDb |
Enables dynamic per-tenant overrides |
๐ Plugin-Based Middleware Injection and Runtime Activation¶
๐ฏ Purpose¶
This section describes how the generated API Gateway supports dynamic middleware injection and runtime activation of plugins โ enabling flexible extension, on-demand enablement, and adaptive behavior per tenant, edition, or feature.
This pattern ensures that the gateway can be:
- Extended without code changes
- Optimized per route or plan
- Modular and reactive to SaaS rules
๐งฉ Middleware Activation Strategy¶
Plugins or middleware can be:
- Globally injected (affects all requests)
- Route-scoped (enabled only on specific paths)
- Feature-controlled (enabled if a feature is active)
- Edition-specific (enabled only for Pro/Enterprise)
๐งฑ Middleware Descriptor Model¶
In plugin-middleware.json or .cs registration:
{
"plugins": [
{
"type": "AuditPlugin",
"feature": "auditTrail",
"editions": ["Pro", "Enterprise"],
"routes": ["/v1/appointments", "/v1/export"]
},
{
"type": "MetricsPlugin",
"feature": "realtimeTelemetry",
"global": true
}
]
}
๐ง Generated Registry¶
public class MiddlewareActivator
{
public void Configure(IApplicationBuilder app, PluginManifest manifest)
{
foreach (var plugin in manifest.GetGlobalMiddlewares())
{
app.UseMiddleware(plugin.Type);
}
}
}
Also supports .UseWhen(...) for route or tenant/edition-based conditional logic.
๐ง Route-Level Middleware Injection¶
app.MapPost("/v1/appointments", ...)
.WithPlugin<AuditPlugin>()
.WithFeatureGate("auditTrail")
.WithEditionGate("Pro");
Or dynamically resolved via reflection from manifest.
๐ Edition and Feature Conditional Activation¶
Each plugin defines:
| Property | Purpose |
|---|---|
RequiredFeature |
Tied to edition feature model |
AllowedEditions |
Explicit edition constraints |
EnabledForTenants |
Targeted enablement for selected tenants only |
๐งฉ Extension Point Interfaces¶
| Interface | Role |
|---|---|
IPluginActivator |
Determines if plugin should activate |
IMiddlewareResolver |
Dynamically injects into pipeline |
IRouteMiddlewareBinder |
Binds per-route plugin logic |
IFeatureScopedPlugin |
Self-registering plugins gated by feature |
๐ Runtime Toggle Use Case¶
With --enableFeature=tracing:
TracingPluginis registered dynamically- Bound only to editions supporting tracing
- Auto-registered in observability metrics and health checks
๐ Plugin Output¶
| File | Description |
|---|---|
plugins.manifest.json |
Final registry of active plugins and hooks |
plugins.lifecycle.log |
Logs lifecycle events, plugin activation/deactivation |
docs/plugins.md |
Markdown summary of plugin capabilities and scopes |
๐ Observability and Diagnostics¶
- Metric:
gateway_plugins_active_total{plugin,tenant,route} - Logs:
- Plugin activation
- Skipped due to edition/feature constraint
- Plugin errors (captured via middleware wrapper)
๐งช Example Test Cases¶
| Test | Description |
|---|---|
| Plugin activates only if feature flag is on | |
| Plugin skipped on Free edition tenant | |
| Plugin injected only for defined routes | |
| Middleware wraps response if active |
๐ง Prompt Flags¶
| Flag | Description |
|---|---|
--withPlugins |
Enables plugin system |
--pluginManifestPath=./config/plugins.json |
Specifies manifest |
--pluginDynamicActivation |
Enables feature/edition-based injection |
--routeScopedPlugins |
Enables route-bound plugin binding |
๐ก๏ธ Security Hardening, Headers, and CORS Rules¶
๐ฏ Purpose¶
This section explains how the API Gateway Generator Agent embeds default security protections and policy-driven controls โ ensuring all external API exposure is hardened against common web threats, misconfigurations, and cross-origin issues.
This includes:
- Standard security headers
- Configurable CORS policies
- Edition- and tenant-aware API surface restrictions
๐งฑ Built-in Security Headers¶
Injected globally via middleware:
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
X-Download-Options: noopen
Referrer-Policy: no-referrer
Content-Security-Policy: default-src 'none';
Middleware injected via:
Fully customizable via config.
๐ CORS Policy Enforcement¶
Supports:
- Per-route CORS policy
- Edition-aware origin whitelisting
- Plugin-based CORS customizers
Example config in gateway-settings.json:
{
"cors": {
"default": {
"allowedOrigins": ["https://*.myapp.com"],
"allowedMethods": ["GET", "POST"],
"allowCredentials": true
},
"perRoute": {
"/v1/public/*": {
"allowedOrigins": ["*"],
"allowedMethods": ["GET"]
},
"/v1/secure/*": {
"allowedOrigins": ["https://portal.myapp.com"]
}
}
}
}
โ๏ธ .NET Integration¶
services.AddCors(options =>
{
options.AddPolicy("SecurePolicy", builder =>
{
builder.WithOrigins("https://portal.myapp.com")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
Applied via route:
๐ Additional Hardening Options¶
| Setting | Description |
|---|---|
enforceHttps |
Forces HTTPS redirects |
blockHttp |
Returns 403 on HTTP requests |
tokenInHeaderOnly |
Rejects bearer tokens in query string |
maxRequestSize |
Rejects payloads exceeding configured limit |
requireContentType |
Rejects requests without Content-Type header |
blockUnknownUserAgents |
Optional enforcement for known bots or scanners |
All configurable via security block in settings or CLI flags.
๐ง Prompt Flags¶
| Flag | Description |
|---|---|
--withSecurity |
Enables all security middlewares |
--withCors |
Enables CORS setup |
--corsOrigins="https://*.myapp.com" |
Sets default origin whitelist |
--corsPublicRoutes="/v1/public/*" |
Whitelist public route set |
--maxRequestSize=2MB |
Injects request size limitation |
--tokenHeaderOnly |
Rejects query token auth |
๐ Generated Output¶
| File | Purpose |
|---|---|
docs/security.md |
Lists all enforced security headers, auth checks |
cors-policies.json |
Runtime CORS policy manifest |
gateway.rules.audit.log |
Logs all rejected/blocked requests due to security violations |
๐งช Test Scenarios¶
| Test | Description |
|---|---|
| CORS preflight returns expected headers | |
| Requests blocked with token in query string | |
| Unknown origin โ 403 | |
| Large payload rejected with 413 | |
| HTTP call redirected to HTTPS |
๐ Observability¶
Metrics:
| Metric | Labels |
|---|---|
gateway_requests_blocked_total |
reason, route |
gateway_cors_violations_total |
origin, route |
gateway_headers_enforced_total |
policy |
๐ API Key Management and Header Enforcement¶
๐ฏ Purpose¶
This section describes how the generated API Gateway supports API Key validation, access control based on headers, and metadata injection, enabling secure machine-to-machine integrations, 3rd-party partner access, and fine-grained multi-tenant enforcement.
๐ API Key Modes¶
| Mode | Use Case |
|---|---|
| Static Keys | Manually defined for internal use or legacy systems |
| Per-Tenant Keys | Dynamically resolved for each tenant |
| Per-Edition Keys | Tiered access: Free users have limited key rights |
| Scoped Keys | Keys can be scoped to specific routes, scopes, or features |
๐ API Key Sources¶
| Source | Description |
|---|---|
apikey header |
Standard (preferred) |
X-Api-Key |
Fallback (legacy) |
?apikey= query param |
(Blocked unless explicitly allowed) |
Resolved using IApiKeyStore:
Backed by:
- In-memory dictionary (for static keys)
- Redis
- Database lookup
- Azure App Configuration or Key Vault
๐งช Key Validation Result Model¶
public class ApiKeyValidationResult
{
public bool IsValid { get; set; }
public string TenantId { get; set; }
public string[] AllowedRoutes { get; set; }
public string Edition { get; set; }
public string[] Scopes { get; set; }
}
๐ Header Enforcement Logic¶
Scaffolded policies:
- Require specific headers for certain routes
- Enforce header structure (e.g., correlation IDs, trace headers)
- Rejects unknown headers or formats
[RequireHeader("X-Request-Id", Regex = "^[a-zA-Z0-9-]{36}$")]
public async Task<IActionResult> DoSomething(...) { ... }
Route-level or global:
{
"headerPolicies": {
"required": ["X-Request-Id", "X-Tenant-Id"],
"rejectUnknown": false,
"validatePatterns": {
"X-Tenant-Id": "^[a-z0-9_-]+$"
}
}
}
๐ง Generated Middleware¶
๐ง Prompt Flags¶
| Flag | Description |
|---|---|
--withApiKey |
Enables API key enforcement |
--staticKeys=tenantA:key123;tenantB:key456 |
Inline static key mapping |
--apiKeyHeader=X-Api-Key |
Customize API key header |
--requireHeaders=X-Tenant-Id,X-Request-Id |
Global header validation |
--blockApiKeyQuery |
Block ?apikey= in query param |
๐ Output Files¶
| File | Purpose |
|---|---|
apikeys.json |
List of valid API keys and metadata |
docs/security/apikeys.md |
Documentation for consumers on how to use API keys |
headers-policy.json |
Enforced header expectations |
gateway-header-violations.log |
Audit log for missing/invalid headers |
๐ Metrics¶
| Metric | Description |
|---|---|
gateway_apikey_validation_total |
Total validations by tenant and result |
gateway_header_violations_total |
Count of header enforcement failures |
gateway_rejected_query_keys_total |
Count of query-based key attempts |
๐งช Test Coverage¶
| Scenario | Outcome |
|---|---|
| Valid key in header โ 200 OK | |
| Invalid key โ 403 Forbidden | |
| Missing required header โ 400 Bad Request | |
| Malformed trace ID โ 422 Unprocessable |
๐งญ Static vs Dynamic Route Discovery and Service Targeting¶
๐ฏ Purpose¶
This section details how the generated API Gateway handles route discovery, target resolution, and dynamic service mapping โ either at code-generation time (static) or runtime (dynamic). This design supports scalable gateway setups across distributed microservices, allowing routes to be declarative or auto-synced from service metadata.
๐งฑ Route Source Modes¶
| Mode | Description | Use Case |
|---|---|---|
| Static | Routes hardcoded at generation time using templates | Predictable services |
| Semi-Dynamic | Read from config or route manifest (routes.json) |
Simple runtime configurability |
| Fully Dynamic | Query service registry or mesh for updates | Highly elastic microservice ecosystems |
๐ Static Routing Example (Code-Generated)¶
app.MapGet("/v1/appointments", async context =>
{
await Proxy.To("http://appointments-svc/v1/appointments");
});
Routing and target are locked during scaffolding.
๐ Dynamic Routing via routes.json¶
[
{
"path": "/v1/users",
"method": "GET",
"target": "http://user-service.internal/api/users",
"auth": "jwt",
"edition": "Pro",
"feature": "userManagement"
}
]
Loaded at runtime via RouteRegistry service and injected into middleware.
Supports:
- Hot reloading via file watchers or APIs
- Remote registry fetch (HTTP, Redis, Service Bus)
- Change events to invalidate gateway caches
โ๏ธ Target Resolution Strategies¶
| Strategy | Example |
|---|---|
| Static URL | "target": "http://orders-svc" |
| Tenant-specific target | "targetByTenant": { "tenantA": "...", "tenantB": "..." } |
| Feature-based | "targetIfFeature": "enableLegacyRouting" |
| Azure Function or Logic App | "target": "https://func.api.com/orders" |
| MCP-Resolved Target | "targetFromMCP": true |
๐งฉ Generated Routing System¶
| Component | Role |
|---|---|
RouteRegistry |
Loads routes.json or remote |
RouteTargetResolver |
Computes final service URL |
RouteCache |
Caches resolved routes by edition, tenant, version |
RouteReloadService |
Refreshes routes if config or remote registry changes |
๐ Hot Reload CLI Example¶
- Reloads route manifest
- Logs diff
- Invalidates per-tenant caches
๐ง Prompt Flags¶
| Flag | Description |
|---|---|
--withStaticRoutes |
Emits route scaffolding as C# |
--withRouteManifest |
Includes routes.json runtime file |
--enableDynamicRouting |
Enables live updates from registry |
--mcpRouteResolution |
Enables dynamic route binding via MCP API |
๐งช Test Scenarios¶
| Test | Description |
|---|---|
| Static route โ maps to correct target | |
| Edition-specific route โ blocked for Free | |
| Dynamic route reload updates target URL | |
| Missing route returns 404 with log span |
๐ Metrics¶
| Metric | Description |
|---|---|
gateway_routes_loaded_total |
Count of active routes |
gateway_routes_dynamic_reload_total |
Number of reloads |
gateway_target_resolution_failures_total |
Resolution errors |
gateway_target_by_edition_total |
Per-edition routing decisions |
โณ Smart Rate Limiting and Adaptive Quotas¶
๐ฏ Purpose¶
This section defines how the API Gateway Generator Agent integrates smart rate-limiting middleware, supporting:
- Tenant- and edition-specific limits
- Route-level and global quotas
- Burst control, quota windows, and adaptive penalty strategies
- Integration with observability and response shaping (e.g., Retry-After headers)
โ๏ธ Rate Limiting Modes Supported¶
| Mode | Description |
|---|---|
| Fixed Window | Classic per-minute/hour count |
| Sliding Window | Smoother bucket-based enforcement |
| Token Bucket | Allows bursts with refill rate |
| Dynamic Quota | Based on usage history or plan limits |
๐ Example Configuration (JSON)¶
{
"rateLimits": {
"default": {
"limit": 100,
"window": "1m"
},
"perEdition": {
"Free": { "limit": 60, "window": "1m" },
"Pro": { "limit": 500, "window": "1m" }
},
"perRoute": {
"/v1/export": { "limit": 10, "window": "10m" }
}
}
}
Supports combinations and overrides:
- Global defaults
- Edition overrides
- Route-specific policies
- Tenant-specified hard caps
๐งฑ Middleware Injection¶
Backed by:
IRateLimiterPolicyProviderIRateLimiterStorage(memory, Redis, Cosmos DB, SQL)
๐ Adaptive Strategy¶
Supports:
- Penalty escalation (e.g., temp bans after repeated violations)
- Retry-After header injection
- Dynamic quota increase after subscription upgrade (hot reload)
- Soft warnings (log only) mode
๐ฆ Storage Backends¶
| Backend | Use Case |
|---|---|
| In-memory | Single-node |
| Redis | Distributed multi-node |
| Azure Cosmos DB | Persistent + multi-region |
| SQL | Admin-friendly analytics |
All storage backends support shared interfaces:
public interface IRateLimiterStorage
{
Task<bool> TryConsumeAsync(string key, int count, TimeSpan window);
}
๐ Headers Injected¶
| Header | Description |
|---|---|
X-RateLimit-Limit |
Max allowed requests |
X-RateLimit-Remaining |
Remaining tokens |
X-RateLimit-Reset |
Time when window resets |
Retry-After |
Delay before retry (if 429 issued) |
๐ Edition + Feature Control¶
- Feature flag:
rateLimitingEnabled - Edition enforcement via
EditionRateLimitPolicyResolver - Custom plugins can define custom rate calculation rules
๐ Output Artifacts¶
| File | Purpose |
|---|---|
rate-limits.json |
Merged policies |
docs/rate-limiting.md |
Human-readable overview |
rate-limit-violations.log |
Policy failures with tenant & trace ID |
๐ง Prompt Flags¶
| Flag | Description |
|---|---|
--withRateLimit |
Enables all rate limiting logic |
--rateLimitMode=tokenBucket |
Choose strategy |
--rateLimitStorage=redis |
Choose backend |
--rateLimitRetryAfterHeader=true |
Injects Retry-After |
--disableRateLimitSoftWarnings=false |
Enables log-only soft violations |
๐งช Test Scenarios¶
| Test | Expected |
|---|---|
| Exceed limit โ 429 | |
| Soft quota โ logged warning, no block | |
| Edition-specific limit enforced | |
| Plugin modifies quota dynamically |
๐ Observability Metrics¶
| Metric | Labels |
|---|---|
gateway_ratelimit_requests_total |
tenant, edition, route |
gateway_ratelimit_throttled_total |
429s |
gateway_ratelimit_retryafter_set_total |
count of Retry-After headers issued |
๐งพ Summary Manifest, CLI Metadata, and Generator Finalization¶
๐ฏ Purpose¶
This cycle finalizes the gateway generation process by producing:
- A complete manifest summarizing all generated components.
- Structured CLI output (human + machine readable).
- Reproducible generation metadata for traceability.
- Optional signing, registry indexing, and post-generation triggers.
This output is critical for:
- CI/CD verification
- MCP agent orchestration
- Developer documentation
- Audit and reproducibility
๐งฉ Gateway Manifest (JSON Format)¶
File: gateway-manifest.json
{
"gateway": {
"name": "ApiGateway.Vetspire.Appointments",
"version": "1.0.0",
"environment": "Production",
"editions": ["Free", "Pro", "Enterprise"],
"templateVersion": "3.4.1",
"generatorAgent": "ApiGatewayGeneratorAgent"
},
"features": [
"jwtAuth",
"apiKeyAuth",
"rateLimiting",
"editionPolicies",
"plugins",
"observability",
"healthProbes"
],
"routes": 42,
"plugins": ["AuditPlugin", "TracingPlugin", "RateLimiter"],
"auth": {
"modes": ["JWT", "ApiKey"],
"authority": "https://auth.vetspire.com"
},
"observability": {
"metrics": "Prometheus",
"tracing": "Jaeger",
"logging": "Serilog"
},
"security": {
"cors": true,
"headers": ["X-Tenant-Id", "X-Request-Id"],
"httpsRedirect": true
},
"generation": {
"timestamp": "2025-06-14T13:55:00Z",
"outputPath": "./output/api-gateway/",
"configProfile": "pro-edition",
"replayFile": "generator-replay.yaml"
}
}
๐ CLI Console Output¶
โ
Gateway generated successfully: ApiGateway.Vetspire.Appointments (v1.0.0)
๐ Output Path: ./output/api-gateway/
๐งพ Manifest: gateway-manifest.json
๐ Auth: JWT + API Key | Observability: Prometheus + Jaeger
๐ฆ Plugins: AuditPlugin, RateLimiter | Features: rateLimiting, multi-tenancy
๐ Docs: ./docs/index.md
๐ Replay: generator-replay.yaml
๐ง Machine-Readable CLI Output¶
File: cli-output.json
{
"success": true,
"outputPath": "./output/api-gateway/",
"manifest": "gateway-manifest.json",
"docs": "./docs/index.md"
}
๐งฌ Generator Replay Metadata (YAML)¶
File: generator-replay.yaml
template: api-gateway
version: 3.4.1
agent: ApiGatewayGeneratorAgent
edition: Pro
flags:
withPlugins: true
withObservability: true
withRateLimit: true
withCors: true
outputPath: ./output/api-gateway
timestamp: 2025-06-14T13:55:00Z
This enables:
- Re-generation without redefining options.
- MCP or CI replay builds.
- Audit trail of how the output was produced.
๐ Optional Signing & Validation¶
When --signManifest=true is passed:
gateway-manifest.sigis created.- SHA256 or RSA signature ensures manifest integrity.
- Used for trust validation during deployment.
๐ Generated Summary Files¶
| File | Description |
|---|---|
gateway-manifest.json |
Main metadata registry |
cli-output.json |
Machine-readable CLI feedback |
generator-replay.yaml |
Replayable config for regeneration |
docs/index.md |
Human-friendly overview |
metadata.txt |
Concise summary for ZIP/distribution |
manifest.sig (optional) |
Cryptographic signature of manifest file |
๐งฐ Output Packaging (Optional)¶
| CLI Flag | Description |
|---|---|
--archiveOutput |
Creates .zip of all output |
--uploadToMCP |
Sends manifest and artifacts to MCP registry |
--registerManifest |
Registers with ConnectSoft blueprint index |
๐ก Post-Generation Webhook¶
If enabled, invokes post-hook to notify orchestrators or DevOps flows:
postGenerate:
webhook: https://connectsoft.io/hooks/gateway-ready
payload:
tenant: vetspire
gateway: appointments
version: 1.0.0
๐ Metrics¶
| Metric | Description |
|---|---|
gateway_generation_completed_total |
Number of completed generations |
gateway_manifest_output_size_bytes |
Size of manifest output |
gateway_manifest_signature_verified |
Count of valid signed manifests |
๐ Integration with Identity & Authorization Servers¶
๐ฏ Purpose¶
This cycle defines how the generated API Gateway integrates with identity providers, authorization policies, and multi-tenant security scopes. It supports:
- JWT bearer token validation
- OpenIddict or external IdP (Auth0, Azure AD, etc.)
- Policy-based and claims-based access control
- Edition-aware and tenant-aware permission models
๐งฉ Identity Provider Support¶
| Provider Type | Description |
|---|---|
| OpenIddict | Default for internal, self-hosted identity |
| Azure AD | Enterprise integration with Microsoft tenants |
| Auth0 / Okta | SaaS-friendly cloud IdPs |
| Custom | Validated via OpenID Connect metadata |
๐ Example JWT Configuration¶
In gateway-settings.json:
{
"auth": {
"jwt": {
"authority": "https://identity.vetspire.com",
"audience": "appointments-api",
"requireHttpsMetadata": true,
"tokenSources": ["Authorization:Bearer", "cookie:access_token"]
}
}
}
Fallback API key is layered under separate mode.
โ๏ธ Code Integration¶
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = config["auth:jwt:authority"];
options.Audience = config["auth:jwt:audience"];
options.RequireHttpsMetadata = true;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateLifetime = true,
NameClaimType = "sub"
};
});
๐ Authorization Strategies¶
| Type | Description |
|---|---|
| Route-based | Routes protected via [Authorize] |
| Policy-based | Custom policies defined per edition, feature |
| Claims-based | Check for roles, permissions, scopes |
๐งฑ Generated Authorization Policies¶
services.AddAuthorization(options =>
{
options.AddPolicy("RequireTenantAdmin", policy =>
policy.RequireClaim("role", "admin")
.RequireClaim("tenant_id"));
options.AddPolicy("ProFeatureOnly", policy =>
policy.RequireClaim("edition", "Pro", "Enterprise"));
});
Mapped to route declarations via:
๐ง Edition-Aware Authorization Plugin¶
Supports middleware or plugin override:
public class EditionFeatureAuthorizationMiddleware : IMiddleware
{
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
var edition = context.User.FindFirst("edition")?.Value;
var route = context.Request.Path;
if (!_featureMatrix.Allows(route, edition))
return Results.Forbid("Upgrade your edition.");
await next(context);
}
}
๐ง Prompt Flags¶
| Flag | Description |
|---|---|
--withJwt |
Enables JWT bearer authentication |
--authAuthority=https://auth.mycompany.com |
Sets OpenID Connect issuer |
--requireHttpsMetadata=true |
Forces secure discovery |
--withAuthPolicies |
Adds edition/feature-specific access rules |
--withCustomClaims role,tenant_id,edition |
Define expected claims |
--supportMultipleIssuers |
Enables validation against multiple IdPs |
๐ Generated Files¶
| File | Description |
|---|---|
auth-settings.json |
Auth-related configuration |
AuthPolicies.cs |
Code-generated policies |
AuthDocumentation.md |
Markdown reference |
gateway-auth.log |
Auth request diagnostics (on failure) |
๐งช Test Scenarios¶
| Scenario | Outcome |
|---|---|
| JWT token valid โ access granted | |
| Invalid audience โ 403 | |
| Missing edition claim โ 403 (if feature-gated) | |
| Admin-only route โ non-admin blocked |
๐ Observability Metrics¶
| Metric | Labels |
|---|---|
gateway_auth_success_total |
tenant, issuer |
gateway_auth_failure_total |
reason (invalid, expired, no claim) |
gateway_authorization_policy_applied_total |
policy, route |
๐ค AI Copilot and MCP-Aware Enhancements¶
๐ฏ Purpose¶
This cycle introduces AI- and MCP-friendly metadata layers, allowing:
- AI agents (like ConnectSoft Copilot) to introspect, reason, and extend the gateway
- MCP Servers to orchestrate upgrades, diffs, and validations
- Developer copilots to explain, modify, and regenerate gateway components
This enables:
- ๐ก AI-assisted debugging and doc generation
- ๐ Autonomous regeneration from traced memory diffs
- ๐ MCP-driven software factory orchestration
๐ง MCP Server Annotations¶
Manifest fields enriched with MCP-standard tags:
{
"manifestVersion": "1.2",
"mcp": {
"type": "gateway",
"componentId": "connectsoft.gateway.vetspire.appointments",
"semanticTags": ["api", "reverse-proxy", "observability", "jwt"],
"lifecycle": "generated",
"relations": {
"routes": ["connectsoft.service.vetspire.appointments"],
"auth": ["connectsoft.auth.server"]
}
}
}
Can be pushed to MCP Server using:
๐ AI Copilot Summary File¶
File: copilot-hints.json
Includes:
- Human-readable explanations of key modules
- GPT-inlined summaries for each feature
- Route-to-feature mappings
- Annotated prompt inputs for regeneration
{
"overview": "API Gateway for Vetspire Appointments...",
"auth": "JWT-based access, scoped by tenant and edition...",
"rateLimiting": "Token bucket strategy with per-route quotas...",
"plugins": [
{ "name": "AuditPlugin", "purpose": "Logs every export for compliance" }
]
}
๐ Generated Explainability Output¶
| File | Description |
|---|---|
copilot-hints.json |
AI-introspectable explanations |
copilot-readme.md |
Developer-facing quick tour |
mcp-tags.json |
Tags to integrate with Model Context Protocol |
mcp-upload.yaml |
Snapshot trigger script |
๐งฉ Semantic Tags for MCP Integration¶
Injected into:
gateway-manifest.jsonroute-registry.jsondocs/index.mdfrontmatter
Example:
๐ง Prompt Flags¶
| Flag | Description |
|---|---|
--withCopilotHints |
Generate AI-friendly summaries |
--mcpTags=auth,edition-aware,plugins |
Append MCP tags to manifest |
--registerWithMCP |
Trigger MCP upload and indexing |
--copilotExplain=true |
Write explanations per route/feature |
๐งช Test Scenarios¶
| Scenario | Validation |
|---|---|
| MCP receives manifest with valid tags | |
| Copilot explains each route and plugin | |
Copilot recreates gateway from replay.yaml and copilot-hints.json |
|
| AI suggests valid modifications via CLI or UI |
๐ Metrics¶
| Metric | Description |
|---|---|
gateway_mcp_upload_success_total |
MCP publish count |
copilot_hint_generation_duration_seconds |
Time to summarize logic |
copilot_field_explained_total |
Fields summarized |
๐ Rebuild / Upgrade / Refactor Automation¶
๐ฏ Purpose¶
This cycle enables the API Gateway Generator Agent to automatically regenerate or refactor gateway output when:
- Edition rules, plugins, routes, or observability configurations change.
- Templates are upgraded to a newer version.
- CI/CD or MCP workflows require validation and refresh.
Supports:
- Smart diffing and change impact detection
- Auto-merge or regeneration logic
- Agent-triggered incremental gateway upgrades
๐ง Smart Diff Engine¶
The agent computes and stores a baseline snapshot of:
generator-replay.yamlgateway-manifest.json- All CLI options and feature flags
On re-run, compares current inputs with baseline:
changes:
- field: routes.added
value: /v1/export
- field: auth.authority
old: https://auth.vetspire.com
new: https://auth.vetspire.io
- field: features.removed
value: "auditTrail"
Triggers:
- Partial regeneration
- Plugin/module removal
- Conflict warnings if manual edits are detected
๐ Upgrade Workflow¶
Supports:
- Diff-only mode (dry run)
- Auto-resolve with preserved custom code
- Unsafe changes (breaking schema) flagged for human intervention
โ๏ธ Generated Artifacts¶
| File | Purpose |
|---|---|
gateway-diff-report.md |
Human-readable summary of changes |
gateway-changelog.json |
JSON diff between previous and new state |
upgrade-plan.yaml |
Structured upgrade instructions for automated application |
rebuild-script.sh |
Rebuild runner with preserved options |
๐งฉ Template Upgrade Handling¶
Checks:
- CLI template version differences
- Updated plugin APIs or observability providers
- Deprecated routes or manifest formats
When changes are safe:
- Auto-upgrades files in-place
- Logs all patch points
- Adds
upgradefield ingateway-manifest.json:
๐ง Prompt Flags¶
| Flag | Description | |
|---|---|---|
--rebuild |
Forces regeneration | |
--diffOnly |
Runs comparison without file writes | |
| `--resolveConflicts=manual | auto` | Conflict strategy |
--upgradeTo=version |
Upgrade template to newer version | |
--skipManualOverrides |
Ignores manually edited sections |
๐งช Scenarios¶
| Case | Expected Result |
|---|---|
| Route added โ diff marks as added | |
| Plugin removed from template โ flagged as breaking | |
| Edition upgraded โ changes reflected in manifest and auth policies | |
| Template version bump โ diffs reported with optional resolution |
๐ Metrics¶
| Metric | Description |
|---|---|
gateway_regeneration_total |
Count of regenerations |
gateway_diff_detected_total |
Diffs discovered |
gateway_upgrade_success_total |
Clean upgrade completions |
๐งช Developer Sandbox, Examples, and Quickstart Scripts¶
๐ฏ Purpose¶
This cycle enhances developer experience by generating a complete sandbox environment, including:
- A self-contained
devgateway configuration - Example microservice stubs or mocks
- Pre-configured HTTP clients and Postman collections
- Quickstart scripts for onboarding and local validation
This makes the generated gateway immediately testable, explainable, and extensible by humans and agents.
๐งช Sandbox Setup Structure¶
/sandbox/
โโโ docker-compose.override.yml
โโโ mock-services/
โ โโโ user-service.json
โ โโโ appointment-service.json
โโโ gateway.dev.settings.json
โโโ seed-data/
โ โโโ tenants.json
โ โโโ editions.json
โโโ run-sandbox.ps1 / .sh
โโโ quickstart.http
โโโ postman-collection.json
๐ณ Docker Sandbox Compose File¶
services:
gateway:
build: ../
ports:
- "5100:80"
environment:
- DOTNET_ENVIRONMENT=Development
volumes:
- ./gateway.dev.settings.json:/app/appsettings.Development.json
mock-appointments:
image: wiremock/wiremock
volumes:
- ./mock-services:/home/wiremock
ports:
- "9090:8080"
๐ Quickstart Scripts¶
run-sandbox.ps1 / run-sandbox.sh:
- Builds and starts the gateway and mocks
- Seeds example tenants
- Prints sample curl/Postman instructions
./run-sandbox.sh
# Output:
# ๐ Gateway running at http://localhost:5100
# ๐งช Try: curl http://localhost:5100/v1/appointments?apikey=test-key
๐งพ Quickstart HTTP File (for REST Clients)¶
File: quickstart.http
### GET Appointments (API Key)
GET http://localhost:5100/v1/appointments
X-Api-Key: test-key
### GET User (JWT)
GET http://localhost:5100/v1/users
Authorization: Bearer {{jwt}}
๐ฌ Postman Collection (Auto-generated)¶
-
Includes examples for:
-
Authenticated and anonymous routes
- Feature-gated editions
- CORS preflight test
- Comes with environment file for local vars (
localhost, tokens, etc.)
๐งฑ Seed Data¶
Used to simulate multi-tenant behavior:
// tenants.json
[
{ "id": "tenantA", "edition": "Free", "apikey": "test-key" },
{ "id": "tenantB", "edition": "Pro", "apikey": "pro-key" }
]
Loaded into in-memory store on startup or injected via plugin.
๐ง Prompt Flags¶
| Flag | Description |
|---|---|
--withSandbox |
Generates sandbox folder |
--includeMocks |
Adds WireMock or static mocks |
--generatePostman |
Outputs Postman collection |
--addQuickstartHttp |
Adds .http test file |
--seedTenants=test1:test-key,test2:pro-key |
Adds dev tenant mocks |
๐ Metrics¶
| Metric | Description |
|---|---|
sandbox_requests_total |
Requests during sandbox runs |
sandbox_mock_hit_total |
Wiremock or stub routes hit |
sandbox_script_run_total |
Times quickstart script executed |
โ Outcome¶
| Value | Description |
|---|---|
| ๐งโ๐ป Developer-Ready | One-liner start for local validation |
| ๐ค Agent-Introspectable | AI copilots can explore sandbox routes |
| ๐งช QA Test Harness | Used in integration and CI pipelines |
๐ Conclusion¶
The API Gateway Generator Agent plays a critical role in the ConnectSoft AI Software Factory, acting as the programmable ingress layer for all generated SaaS microservices. Its ability to synthesize a secure, scalable, multi-tenant API Gateway from edition-aware specifications and domain-driven contracts makes it foundational for governed exposure of capabilities across product boundaries.
Through its integration with MCP servers, authentication systems, observability stacks, and AI copilots, the agent ensures that each generated gateway is:
- Security-first by default,
- Edition-intelligent at runtime,
- Telemetry-compliant out of the box,
- And evolution-ready through replayable manifests and smart upgrade flows.
Its outputs not only serve as operational entry points but also become semantic assets, traceable and orchestratable across the lifecycle โ empowering AI agents, developers, and systems to coordinate seamlessly.
As ConnectSoft continues scaling toward thousands of intelligent microservices and APIs, the API Gateway Generator Agent ensures every route, plugin, and policy remains visible, controlled, and explainable โ by both humans and machines.