# The codebase

> How the repository is organised, how to build and test it, and where to start reading.

Applies to: the Sypnia engine (open source, self-hosted).  
Source: https://data.docs.sypnia.com/architecture/codebase/

For agents: Repository sypnia-data (engine AGPL-3.0, packages/data MIT): crates/{core,auth,mail,rest,sql,mcp,install,server,testkit} (Rust workspace), packages/data (@sypnia/data: src/client, src/server, src/cli, skills/), spec/{openapi.yaml,auth-schema.sql,introspect.sql} (the contract), Dockerfile and docker-compose.yml at the root, deploy/{aws.yaml,aws/sypnia-data-cloud.yaml}, examples/espiga, tests/mcp-sdk, docs/ARCHITECTURE.md. Build: cargo build --release (cargo lambda build --arm64 for AWS); pnpm -r build. Tests: cargo test with a throwaway Postgres (initdb/pg_ctl), pnpm -r test against a local engine. Start reading at crates/core/src/context.rs (who is calling) and crates/rest/src/parse.rs + build.rs (URL to SQL).

```text
sypnia-data/
├─ crates/                 the engine (a Rust workspace)
│  ├─ core/                configuration, registry, pools, auth context, keys, rate limits, audit, errors
│  ├─ auth/                /auth/v1 and /auth/v1/oauth
│  ├─ mail/                e-mail providers and templates
│  ├─ rest/                the Data API and the schema cache
│  ├─ sql/                 /sql/v1: query, transactions, migrations, schema
│  ├─ mcp/                 the MCP server: tools, resources, the prompt
│  ├─ install/             the SQL migrations of the engine's own schemas and roles
│  ├─ server/              the binary: router, Lambda adapter, the sypnia-data commands
│  └─ testkit/             throwaway Postgres and helpers for the tests
├─ packages/
│  └─ data/                @sypnia/data: src/client, src/server, src/cli, and skills/ (sypnia-data)
├─ spec/
│  ├─ openapi.yaml         every endpoint: the contract the code and these docs follow
│  ├─ auth-schema.sql      the engine's tables, as installed
│  └─ introspect.sql       the query behind the schema cache and GET /sql/v1/schema
├─ Dockerfile, docker-compose.yml
├─ deploy/                 aws.yaml (the AWS template), aws/sypnia-data-cloud.yaml
├─ examples/espiga/        the bakery example used across these docs
├─ tests/mcp-sdk/          the MCP server tested with the official MCP SDK
└─ docs/ARCHITECTURE.md
```

## Build and test

```bash
cargo build --release                         # the binary
cargo lambda build --release --arm64          # for AWS Lambda
cargo test                                    # starts a throwaway Postgres with initdb and pg_ctl
pnpm install && pnpm -r build && pnpm -r test # @sypnia/data (client, server), against a local engine
```

## Where to start reading

- `crates/core/src/context.rs`: how a request becomes _who is calling_ and _which role_.
- `crates/rest/src/parse.rs` and `build.rs`: how a URL becomes one SQL statement.
- `crates/auth/src/sessions.rs`: sessions, rotation and reuse detection.
- `crates/mcp/src/tools.rs`: every MCP tool, and the rights each one needs.

The contract is `spec/openapi.yaml`: a change to an endpoint changes the spec first, and a test checks the code against it.
