# hoaxnerd/fastrest architecture

> Architecture diagram of the GitHub repository hoaxnerd/fastrest, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hoaxnerd/fastrest
- Repository: https://github.com/hoaxnerd/fastrest

## Overview

1. **What this repo is**
   FastREST is a Python library for building async REST APIs on top of **FastAPI + Pydantic**, with a DRF-style developer model. It is not an application; it provides reusable framework components that an app imports and wires into a FastAPI instance.

2. **Core request path**
   The main runtime flow is: **FastAPI request → router → viewset/generic view → serializer/model adapter → response**. Routing, validation, and response shaping are handled by FastREST rather than being left to ad hoc endpoint code.

3. **Primary API surface**
   The high-level entry points are:
   - `routers.py` for registering resources and generating routes
   - `viewsets.py` and `generics.py` for CRUD endpoints
   - `serializers.py` and `fields.py` for schema/validation
   - `decorators.py` for custom actions
   - `response.py`, `request.py`, `exceptions.py` for HTTP/runtime plumbing

4. **Model-driven CRUD generation**
   A major subsystem is `router.serve(Model)`, which auto-derives a serializer, viewset, and route set from a model. This is the “zero-config” path that turns ORM models into list/create/retrieve/update/delete endpoints plus optional search, ordering, pagination, and permissions.

5. **ORM abstraction boundary**
   The `compat/orm/` package is the main persistence boundary. It contains adapters for **SQLAlchemy, Tortoise, SQLModel, and Beanie**, all behind a common adapter interface. This lets the framework keep one CRUD/API layer while swapping persistence implementations underneath.

6. **Serializer and validation layer**
   `serializers.py` and `fields.py` provide a DRF-like schema system with Pydantic-backed validation. Serializers define the data contract, perform field/object validation, and produce request/response shapes used by both HTTP endpoints and generated OpenAPI schemas.

7. **Cross-cutting request policies**
   Authentication, permissions, throttling, filtering, pagination, and content negotiation are separate subsystems rather than endpoint-specific code:
   - `authentication.py` for token/basic/session auth
   - `permissions.py` for composable allow/deny rules
   - `throttling.py` for rate limits
   - `filters.py` and `pagination.py` for list query behavior
   - `negotiation.py` for renderer selection from `Accept`

8. **HTTP and API composition**
   `routers.py`, `views.py`, `viewsets.py`, `generics.py`, and `response.py` define how framework objects become FastAPI routes and HTTP responses. The library also generates method-specific OpenAPI metadata, typed path parameters, and status codes that match CRUD semantics.

9. **Agent-facing integration layer**
   FastREST goes beyond standard REST with two generated artifacts:
   - `mcp.py` mounts a **Model Context Protocol** server so AI agents can call viewset actions as tools
   - `skills.py` generates per-resource and global **SKILL.md** documents
   - `manifest.py` emits a machine-readable API manifest
   These are derived from the same router/viewset/serializer metadata as the HTTP API, so they stay in sync.

10. **Settings and feature toggles**
   `settings.py` centralizes app-level configuration, with precedence from viewset overrides to app settings to framework defaults. This controls default permissions, auth, pagination, MCP, skill generation, and other behavior without changing endpoint code.

11. **Support utilities and diagnostics**
   Smaller modules like `status.py`, `manifest.py`, `openapi.py`, `relations.py`, `mixins.py`, `authentication.py`, and `skills.py` fill in framework behavior around HTTP status constants, schema generation, relation handling, and reusable mixin logic.

12. **Testing strategy**
   The `tests/` directory is broad and feature-oriented: there are contract tests for each ORM adapter, plus coverage for auth, permissions, throttling, filtering, pagination, negotiation, MCP, manifest generation, router behavior, serializers, and end-to-end integration. This indicates the repo’s architecture is centered on consistent behavior across adapters and layers.

13. **Packaging and runtime constraints**
   The project is packaged as a typed Python library (`py.typed`) with optional extras for ORM backends and MCP support. Its architecture depends materially on **FastAPI**, **Pydantic v2**, and Python **3.10+**; everything else is layered on top of that core stack.

14. **How to read the architecture**
   If you are diagramming it, the key boundaries are:
   - **FastAPI app boundary** at the top
   - **Routing/viewset/serializer layer** in the middle
   - **ORM adapter boundary** below that
   - **Agent/documentation generators** as parallel outputs from the same metadata
   - **Cross-cutting policies** applied around the request lifecycle

Main entry points: [`src/fastrest/routers.py`](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/routers.py) (Routers), [`src/fastrest/serializers.py`](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/serializers.py) (Serializers), [`src/fastrest/settings.py`](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/settings.py) (Settings), [`src/fastrest/viewsets.py`](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/viewsets.py) (Viewsets)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core API"]
  node_routers["Routers<br/>route registry<br/>[routers.py]"]
  node_viewsets["Viewsets<br/>resource actions<br/>[viewsets.py]"]
  node_generics["Generics<br/>CRUD base<br/>[generics.py]"]
  node_views["Views<br/>http view layer<br/>[views.py]"]
  node_serializers["Serializers<br/>schema layer<br/>[serializers.py]"]
  node_fields["Fields<br/>field types<br/>[fields.py]"]
  node_decorators["Decorators<br/>action metadata<br/>[decorators.py]"]
  node_request["Request<br/>[request.py]"]
  node_response["Response<br/>[response.py]"]
  node_exceptions["Exceptions<br/>error mapping<br/>[exceptions.py]"]
  node_settings["Settings<br/>config<br/>[settings.py]"]
end

subgraph group_policies["Policies"]
  node_authentication["Auth<br/>[authentication.py]"]
  node_permissions["Permissions<br/>access control<br/>[permissions.py]"]
  node_throttling["Throttling<br/>rate limiting<br/>[throttling.py]"]
  node_filters["Filters<br/>query filtering<br/>[filters.py]"]
  node_pagination["Pagination<br/>list paging<br/>[pagination.py]"]
  node_negotiation["Negotiation<br/>[negotiation.py]"]
end

subgraph group_orm["ORM Adapters"]
  node_orm_base["ORM base<br/>adapter interface<br/>[base.py]"]
  node_orm_sqlalchemy["SQLAlchemy<br/>orm adapter<br/>[sqlalchemy.py]"]
  node_orm_tortoise["Tortoise<br/>orm adapter<br/>[tortoise.py]"]
  node_orm_sqlmodel["SQLModel<br/>orm adapter<br/>[sqlmodel.py]"]
  node_orm_beanie["Beanie<br/>orm adapter<br/>[beanie.py]"]
end

subgraph group_agent["Agent Outputs"]
  node_manifest["Manifest<br/>[manifest.py]"]
  node_mcp["MCP<br/>agent bridge<br/>[mcp.py]"]
  node_skills["Skills<br/>doc generator<br/>[skills.py]"]
  node_openapi["OpenAPI<br/>schema generation<br/>[openapi.py]"]
end

node_fastapi_app(("FastAPI app<br/>host app"))

node_fastapi_app -->|"mounts"| node_routers
node_routers -->|"builds"| node_viewsets
node_routers -->|"derives CRUD"| node_generics
node_viewsets -->|"extends"| node_generics
node_viewsets -->|"uses"| node_serializers
node_generics -->|"validates"| node_serializers
node_serializers -->|"composes"| node_fields
node_viewsets -->|"annotates"| node_decorators
node_views -->|"reads"| node_request
node_views -->|"returns"| node_response
node_routers -->|"applies"| node_authentication
node_routers -->|"applies"| node_permissions
node_routers -->|"applies"| node_throttling
node_routers -->|"applies"| node_filters
node_routers -->|"applies"| node_pagination
node_routers -->|"applies"| node_negotiation
node_serializers -->|"describes"| node_openapi
node_routers -->|"emits"| node_manifest
node_routers -->|"mounts"| node_mcp
node_routers -->|"generates"| node_skills
node_serializers -->|"feeds"| node_manifest
node_viewsets -->|"feeds"| node_manifest
node_serializers -->|"drives"| node_mcp
node_serializers -->|"drives"| node_skills
node_settings -->|"configures"| node_routers
node_settings -->|"defaults"| node_permissions
node_settings -->|"defaults"| node_authentication
node_settings -->|"defaults"| node_pagination
node_settings -->|"toggles"| node_mcp
node_settings -->|"toggles"| node_skills
node_routers -->|"adapts via"| node_orm_base
node_orm_base -->|"implements"| node_orm_sqlalchemy
node_orm_base -->|"implements"| node_orm_tortoise
node_orm_base -->|"implements"| node_orm_sqlmodel
node_orm_base -->|"implements"| node_orm_beanie
node_orm_sqlalchemy -->|"feeds models"| node_serializers
node_orm_tortoise -->|"feeds models"| node_serializers
node_orm_sqlmodel -->|"feeds models"| node_serializers
node_orm_beanie -->|"feeds models"| node_serializers

click node_routers "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/routers.py"
click node_viewsets "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/viewsets.py"
click node_generics "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/generics.py"
click node_views "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/views.py"
click node_serializers "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/serializers.py"
click node_fields "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/fields.py"
click node_decorators "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/decorators.py"
click node_request "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/request.py"
click node_response "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/response.py"
click node_exceptions "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/exceptions.py"
click node_settings "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/settings.py"
click node_authentication "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/authentication.py"
click node_permissions "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/permissions.py"
click node_throttling "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/throttling.py"
click node_filters "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/filters.py"
click node_pagination "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/pagination.py"
click node_negotiation "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/negotiation.py"
click node_orm_base "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/compat/orm/base.py"
click node_orm_sqlalchemy "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/compat/orm/sqlalchemy.py"
click node_orm_tortoise "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/compat/orm/tortoise.py"
click node_orm_sqlmodel "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/compat/orm/sqlmodel.py"
click node_orm_beanie "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/compat/orm/beanie.py"
click node_manifest "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/manifest.py"
click node_mcp "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/mcp.py"
click node_skills "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/skills.py"
click node_openapi "https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/openapi.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_routers,node_viewsets,node_generics,node_views,node_serializers,node_fields,node_decorators,node_request,node_response,node_exceptions,node_settings toneBlue
class node_authentication,node_permissions,node_throttling,node_filters,node_pagination,node_negotiation toneAmber
class node_orm_base,node_orm_sqlalchemy,node_orm_tortoise,node_orm_sqlmodel,node_orm_beanie toneMint
class node_manifest,node_mcp,node_skills,node_openapi toneRose
class node_fastapi_app toneNeutral
```

## Components

### Core API

Primary request-handling and composition layer for building REST endpoints.

- [Routers](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/routers.py): route registry · Registers resources and assembles route sets, including model-serving shortcuts. · `src/fastrest/routers.py`
- [Viewsets](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/viewsets.py): resource actions · DRF-style resource controllers that implement CRUD and custom actions. · `src/fastrest/viewsets.py`
- [Generics](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/generics.py): CRUD base · Reusable generic view behavior that powers standard list/create/retrieve/update flows. · `src/fastrest/generics.py`
- [Views](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/views.py): http view layer · FastAPI-facing view composition and dispatch helpers. · `src/fastrest/views.py`
- [Serializers](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/serializers.py): schema layer · Pydantic-backed data contracts, validation, and output shaping. · `src/fastrest/serializers.py`
- [Fields](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/fields.py): field types · Serializer field definitions and validation primitives. · `src/fastrest/fields.py`
- [Decorators](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/decorators.py): action metadata · Custom action annotations and endpoint metadata helpers. · `src/fastrest/decorators.py`
- [Request](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/request.py): request wrapper · Framework request helpers and parsed request context. · `src/fastrest/request.py`
- [Response](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/response.py): response shaping · Framework response construction and HTTP response normalization. · `src/fastrest/response.py`
- [Exceptions](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/exceptions.py): error mapping · Framework error types and HTTP exception translation. · `src/fastrest/exceptions.py`
- [Settings](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/settings.py): config · Central configuration and precedence rules for framework behavior. · `src/fastrest/settings.py`

### Policies

Cross-cutting request behavior applied across the API lifecycle.

- [Auth](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/authentication.py): authentication · Token, basic, and session authentication hooks. · `src/fastrest/authentication.py`
- [Permissions](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/permissions.py): access control · Composable allow/deny rules for endpoint access. · `src/fastrest/permissions.py`
- [Throttling](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/throttling.py): rate limiting · Request-rate limits and quota enforcement. · `src/fastrest/throttling.py`
- [Filters](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/filters.py): query filtering · List-query filtering behavior and predicate application. · `src/fastrest/filters.py`
- [Pagination](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/pagination.py): list paging · Page sizing and list navigation behavior. · `src/fastrest/pagination.py`
- [Negotiation](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/negotiation.py): content negotiation · Renderer selection and Accept header handling. · `src/fastrest/negotiation.py`

### ORM Adapters

Persistence abstraction layer shared by generated CRUD flows.

- [ORM base](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/compat/orm/base.py): adapter interface · Common persistence adapter contract used by all ORM backends. · `src/fastrest/compat/orm/base.py`
- [SQLAlchemy](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/compat/orm/sqlalchemy.py): orm adapter · SQLAlchemy persistence adapter implementation. · `src/fastrest/compat/orm/sqlalchemy.py`
- [Tortoise](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/compat/orm/tortoise.py): orm adapter · Tortoise ORM persistence adapter implementation. · `src/fastrest/compat/orm/tortoise.py`
- [SQLModel](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/compat/orm/sqlmodel.py): orm adapter · SQLModel persistence adapter implementation. · `src/fastrest/compat/orm/sqlmodel.py`
- [Beanie](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/compat/orm/beanie.py): orm adapter · Beanie persistence adapter implementation. · `src/fastrest/compat/orm/beanie.py`

### Agent Outputs

Machine-readable and agent-facing artifacts derived from API metadata.

- [Manifest](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/manifest.py): api manifest · Machine-readable API description generated from router metadata. · `src/fastrest/manifest.py`
- [MCP](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/mcp.py): agent bridge · Model Context Protocol server exposing resource actions as tools. · `src/fastrest/mcp.py`
- [Skills](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/skills.py): doc generator · Generates per-resource and global SKILL.md documentation. · `src/fastrest/skills.py`
- [OpenAPI](https://github.com/hoaxnerd/fastrest/blob/main/src/fastrest/openapi.py): schema generation · OpenAPI metadata generation from framework models and routes. · `src/fastrest/openapi.py`

### Other components

- **FastAPI app**: host app · External application boundary that mounts FastREST routes.

## Connections

- FastAPI app → Routers: mounts · An application registers FastREST routers to expose resources.
- Routers → Viewsets: builds · Routers instantiate viewsets for resources and actions.
- Routers → Generics: derives CRUD · Model-serving shortcuts create generic CRUD flows.
- Viewsets → Generics: extends · Viewsets reuse generic list/detail mutation behavior.
- Viewsets → Serializers: uses · Resource actions serialize input and output through schemas.
- Generics → Serializers: validates · Generic CRUD flows validate requests and shape responses.
- Serializers → Fields: composes · Serializers are built from field definitions and validators.
- Viewsets → Decorators: annotates · Decorators describe custom resource actions and methods.
- Views → Request: reads · View dispatch consumes framework request context.
- Views → Response: returns · Views emit normalized framework responses.
- Routers → Auth: applies · Routing configuration wires in authentication behavior.
- Routers → Permissions: applies · Routing configuration wires in access rules.
- Routers → Throttling: applies · Routing configuration wires in rate limits.
- Routers → Filters: applies · List endpoints use filtering behavior from router metadata.
- Routers → Pagination: applies · List endpoints use pagination behavior from router metadata.
- Routers → Negotiation: applies · Routers attach content negotiation for response rendering.
- Serializers → OpenAPI: describes · Schema definitions feed OpenAPI generation.
- Routers → Manifest: emits · Router metadata is exported as a machine-readable manifest.
- Routers → MCP: mounts · Router/viewset metadata is exposed through MCP tools.
- Routers → Skills: generates · Resource metadata is rendered into SKILL.md documents.
- Serializers → Manifest: feeds · Serializer contracts contribute to the manifest.
- Viewsets → Manifest: feeds · Viewset actions and routes contribute to the manifest.
- Serializers → MCP: drives · Tool schemas are derived from serializer and action metadata.
- Serializers → Skills: drives · Documentation content is derived from serializer and resource metadata.
- Settings → Routers: configures · Settings control router behavior and defaults.
- Settings → Permissions: defaults · Settings provide default access control policies.
- Settings → Auth: defaults · Settings provide default authentication configuration.
- Settings → Pagination: defaults · Settings provide default pagination configuration.
- Settings → MCP: toggles · Settings enable or disable MCP generation and mounting.
- Settings → Skills: toggles · Settings enable or disable skill generation.
- Routers → ORM base: adapts via · Model-serving flows use the persistence adapter contract.
- ORM base → SQLAlchemy: implements · SQLAlchemy backend implements the common adapter interface.
- ORM base → Tortoise: implements · Tortoise backend implements the common adapter interface.
- ORM base → SQLModel: implements · SQLModel backend implements the common adapter interface.
- ORM base → Beanie: implements · Beanie backend implements the common adapter interface.
- SQLAlchemy → Serializers: feeds models · Adapter metadata informs generated serializers and CRUD endpoints.
- Tortoise → Serializers: feeds models · Adapter metadata informs generated serializers and CRUD endpoints.
- SQLModel → Serializers: feeds models · Adapter metadata informs generated serializers and CRUD endpoints.
- Beanie → Serializers: feeds models · Adapter metadata informs generated serializers and CRUD endpoints.
