swarmauri_middleware_cors
Overview
Summary
Custom CORS middleware for the Swarmauri framework that integrates with Starlette applications.
Why This Package
swarmauri_middleware_cors isolates this responsibility: Custom CORS Middleware for Swarmauri Framework. Its package source exposes CustomCORSMiddleware, allowing consumers to select this package without treating adjacent packages as part of its public contract.
When to Use
Choose swarmauri_middleware_cors when your integration needs custom CORS Middleware for Swarmauri Framework. Match the imports below to your application boundary and install its declared dependencies before using the example.
Features
- Handles CORS preflight (
OPTIONS) requests directly and returns a 200 response with your configured headers.
- Adds CORS headers to downstream responses produced by your Starlette routes.
- Rejects requests with a missing
Origin header or an origin that is not explicitly allowed, returning a 403 response.
- Allows fine-grained control of the exposed headers, allowed methods, credentials support, and cache duration of the CORS policy.
Non-Goals
swarmauri_middleware_cors owns custom CORS Middleware for Swarmauri Framework. Its imports do not by themselves configure credentials, deploy a service, or certify the behavior of its consumers. Dependencies and neighboring packages retain their own documented responsibilities.
Getting Started
Prerequisites
Python >=3.10,<3.15 is required. The manifest declares runtime dependencies including starlette, swarmauri-core, swarmauri-base, swarmauri-standard. Read the example’s inputs and service requirements before execution.
Installation
uv
uv add swarmauri_middleware_cors
uv venv
source .venv/bin/activate # Use .\.venv\Scripts\activate on Windows
uv add swarmauri_middleware_cors
pip
python -m pip install swarmauri_middleware_cors
pip install swarmauri_middleware_cors
Poetry
poetry add swarmauri_middleware_cors
poetry add swarmauri_middleware_cors
Quick Start
Example
Source: docs/readme/quick_start.py.
from starlette.applications import Starlette
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import JSONResponse
from starlette.testclient import TestClient
from swarmauri_middleware_cors import CustomCORSMiddleware
app = Starlette()
cors = CustomCORSMiddleware(
allow_origins=["https://frontend.example"],
allow_methods=["GET", "OPTIONS"],
allow_headers=["Authorization"],
expose_headers=["X-App-Version"],
max_age=300,
)
async def apply_custom_cors(request, call_next):
return await cors.dispatch(request, call_next)
app.add_middleware(BaseHTTPMiddleware, dispatch=apply_custom_cors)
async def ping(_request):
return JSONResponse({"status": "ok"})
app.add_route("/ping", ping)
client = TestClient(app)
if __name__ == "__main__":
options_response = client.options(
"/ping", headers={"Origin": "https://frontend.example"}
)
print(
"Allowed origin header:",
options_response.headers["Access-Control-Allow-Origin"],
)
response = client.get(
"/ping", headers={"Origin": "https://frontend.example"}
)
print("Response JSON:", response.json())
Expected Result
The example demonstrates the documented swarmauri_middleware_cors source boundary or workflow. Complete its required configuration first. README CI checks the maintained source’s syntax and declarations; it does not claim that external services or the application workflow were exercised.
Usage
Common Workflows
Source: docs/readme/usage.py.
from starlette.applications import Starlette
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import JSONResponse
from starlette.testclient import TestClient
from swarmauri_middleware_cors import CustomCORSMiddleware
app = Starlette()
cors = CustomCORSMiddleware(
allow_origins=["https://frontend.example"],
allow_methods=["GET", "OPTIONS"],
allow_headers=["Authorization"],
expose_headers=["X-App-Version"],
max_age=300,
)
async def apply_custom_cors(request, call_next):
return await cors.dispatch(request, call_next)
app.add_middleware(BaseHTTPMiddleware, dispatch=apply_custom_cors)
async def ping(_request):
return JSONResponse({"status": "ok"})
app.add_route("/ping", ping)
client = TestClient(app)
if __name__ == "__main__":
options_response = client.options(
"/ping", headers={"Origin": "https://frontend.example"}
)
print(
"Allowed origin header:",
options_response.headers["Access-Control-Allow-Origin"],
)
response = client.get(
"/ping", headers={"Origin": "https://frontend.example"}
)
print("Response JSON:", response.json())
Configure the middleware with the exact origins, methods, and headers you want to allow. Origins are matched literally?if you use the default ['*'] setting, every request must send an Origin: * header. In practice you should list the concrete origins your frontend sends.
Python example
Running the example prints the allowed origin configured on the middleware and the JSON payload returned by the GET /ping route.
Configuration
Configure swarmauri_middleware_cors through the APIs in package source; its manifest describes installation requirements, not an implicit service configuration. No additional package-global configuration contract is inferred from dependencies.
Error Handling
The inspected swarmauri_middleware_cors source does not establish a package-wide exception hierarchy. Preserve errors from its declared dependencies and validate inputs using the specific public contract in package source.
Best Practices
Use the explicit swarmauri_middleware_cors imports documented below and keep collaborator configuration with the caller. Run the package’s relevant tests after changing CustomCORSMiddleware; an import or successful installation alone does not establish its runtime guarantees.
Package Boundary
What It Owns
swarmauri_middleware_cors is located in standards and owns custom CORS Middleware for Swarmauri Framework. Source-declared APIs include CustomCORSMiddleware. The manifest records its direct dependency boundary.
Public API and Import Surfaces
Import namespace: swarmauri_middleware_cors.
Source-declared exports or types: CustomCORSMiddleware.
Inspect package source for exact signatures; this inventory does not designate every internal module as stable.
Entry Points
swarmauri.middlewares → CustomCORSMiddleware → swarmauri_middleware_cors:CustomCORSMiddleware
Injected Dependencies
The direct installation dependencies of swarmauri_middleware_cors are starlette, swarmauri-core, swarmauri-base, swarmauri-standard. These declarations do not imply automatic provider selection. Pass collaborators through the explicit constructors or call signatures in package source.
Protocol Consumers
No direct package-manifest dependency on swarmauri_middleware_cors was found in the audited three-repository inventory. External consumers may still use its documented imports or commands.
Extensions
For swarmauri_middleware_cors, use the source-declared public interfaces (CustomCORSMiddleware) when composing or extending behavior. Its entry-point section below is authoritative for registered plugins; no additional extension loader is inferred from the package name.
Operations and Readiness
Operational Requirements
Operate swarmauri_middleware_cors according to its actual entry points and collaborators. The dependency and Python requirements in the manifest are prerequisites; deployment topology, credentials, and readiness remain the responsibility of the consuming application unless explicitly documented above.
Health and Observability
This README does not establish a standalone health endpoint for swarmauri_middleware_cors. Observe the concrete caller’s results, exceptions, and existing test reports. An application that embeds this package must define its own health and telemetry contract.
Security and Resource Limits
Apply the input, credential, and resource constraints implemented in package source. The swarmauri_middleware_cors README gate is a documentation check, not a security review or a substitute for the implementation’s limits and caller authorization.
Compatibility
This source version of swarmauri_middleware_cors declares Python >=3.10,<3.15. Consult its dependency constraints before upgrading. Source-version and registry-version badges have separate meanings; this migration makes no additional platform or interoperability certification claim.
Verification
Tests
Run python -m pytest tests/unit/test_readme_sources.py from the swarmauri_middleware_cors package directory for the maintained example source checks (Python compilation and non-Python file presence). Existing package tests include:
The repository README gate also checks the required heading tree, metadata, managed fragments, and referenced files.
Certification Status
No external certification is asserted for swarmauri_middleware_cors by this documentation migration. README conformance, example syntax, runtime tests, and protocol or security certification are distinct evidence scopes.
Evidence
The README standards CI records mechanical documentation results for its tested commit. The package source and example sources support the package-boundary description; external runtime behavior requires its own test evidence.
FAQ
Why
Why select this package? swarmauri_middleware_cors provides custom CORS Middleware for Swarmauri Framework; use its own API boundary rather than assuming neighboring packages are bundled or configured automatically.
How
How do I start? Install or synchronize the environment under Getting Started, follow the maintained example for swarmauri_middleware_cors, and consult package source for the exact types and signatures involved.
When
When should I validate an upgrade? Recheck swarmauri_middleware_cors whenever its source version, declared collaborators, configuration, or caller changes. Run relevant behavioral tests in addition to the README gate before promotion.
Documentation and Related Packages
More Documentation
The repository contribution guide, package source, and manifest provide the supporting context for swarmauri_middleware_cors. The README policy defines the common document structure.
Related Packages
- swarmauri_base: Reusable Swarmauri base classes, mixins, and dynamic Pydantic component models for composable intelligence infrastructure.
- swarmauri_core: Core interface contracts and shared types for Swarmauri composable intelligence infrastructure components.
- swarmauri_standard: First-party Swarmauri standard components for agents, tools, parsers, vectors, model adapters, and composable intelligence workflows.
Project Links
Contributing
How to Contribute
If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.
Development Setup
Work from the pkgs/40-standards/swarmauri_middleware_cors checkout. Synchronize its declared development environment with uv, then run the relevant package tests and python -m pytest tests/unit/test_readme_sources.py. The repository’s dedicated README job installs readme-standards==0.1.3 without resolving unrelated runtime packages.
Notes for Maintainers
Edit authored prose directly in this swarmauri_middleware_cors README. Update source examples in docs/readme/, then run readme-standards render --root . --write from the package directory and review the diff. Required headings and generated blocks come from package-guide-v1.
Governance
Ownership and Decision Making