Skip to content

About

Python API and MCP Server security test project.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

secure-mcp-lab — secure MCP server build

A learning track (formerly named rlapi; the rlapi package is now just one part of it), built break-then-fix: a hardened CRUD API (rlapi), an OAuth 2.1 authorization server (rlas), and an MCP server in front of them (rlmcp) that is a proper OAuth 2.1 resource server. Each security step writes the vulnerability first, proves it with an exploit, fixes it, then keeps the exploit as a regression test.

This file is the front door: how to run things. The thinking lives in four other documents, listed at the bottom — PLAN.md is the source of truth for status and what to do next. Don't trust a status line here over that one.

Current status: steps 0–3 complete, step 4 (rlapi as a resource server) is next. See PLAN.md §0 for the authoritative version.


Setup

Requires uv and Python 3.13 (pinned in .python-version).

uv sync                    # create .venv and install the workspace
uv run pre-commit install  # enable the secret-scanning hooks

uv run <cmd> always works without activating the venv, and syncs against uv.lock first. The three packages in packages/* are installed editable, so source edits take effect with no reinstall.

The dev database is gitignored and already exists locally. If rlapi-dev.db is missing:

cd packages/rlapi
RLAPI_DATABASE_URL="sqlite:///$PWD/../../rlapi-dev.db" uv run alembic upgrade head

That gives an empty schema — there is still no reusable seed script (tracked as an open item in PLAN.md). The existing dev database holds 5 products and 4 orders and has been hand-edited, so treat it as scratch, not a fixture. Nothing depends on its contents; the tests build their own temp databases.

Run the API

From the repository root:

RLAPI_DATABASE_URL="sqlite:///$PWD/rlapi-dev.db" \
  uv run uvicorn --factory rlapi.app:asgi_app_from_env --reload

Serves on http://127.0.0.1:8000. Three things about that command are load-bearing:

  • RLAPI_DATABASE_URL is not optional in practice. The default in settings.py is the relative sqlite:///rlapi.db, so omitting the variable silently creates a new empty database in the current directory instead of failing. The $PWD makes the path absolute, which is why this must be run from the repository root.
  • --factory is required, because the target is a callable that returns the app.
  • Use asgi_app_from_env, not app_from_env. The first holds its engine for the process lifetime (what a server wants); the second is a context manager that disposes the engine, for scripts, and cannot be a uvicorn factory.

create_app runs assert_hardened() at startup, so an unhardened engine fails to boot rather than serving writes that corrupt data. If the server refuses to start, suspect that check before suspecting your config.

Endpoints

Path What
/healthz liveness — returns {"status":"ok"}
/docs Swagger UI, interactive — served from vendored assets, works offline
/static/swagger-ui/* those assets (swagger-ui-dist 5.33.2)
/openapi.json the raw OpenAPI 3.1.0 document
/products, /products/{id}, /products/search product CRUD
/orders, /orders/{id}, /orders/{id}/status order CRUD and status transitions

There is no authentication yet — that is steps 4–6, and until then any caller can act as any customer. The gap is deliberate and tracked, not overlooked.

To get the spec without starting a server (useful for diffing the contract in a commit):

RLAPI_DATABASE_URL="sqlite:///$PWD/rlapi-dev.db" uv run python -c "
import json
from rlapi.app import app_from_env
with app_from_env() as app:
    print(json.dumps(app.openapi(), indent=2))
" > openapi.json

This never opens the database — SQLAlchemy's create_engine is lazy.

Run the authorization server

rlas refuses to start without a signing key and a users file, both under the gitignored .secrets/ by default. One-time setup:

uv run python -m rlas keygen         # writes .secrets/rlas-signing-key.pem, mode 0600
uv run python -m rlas hash-password  # prompts twice (no echo), prints an scrypt hash

Then create .secrets/rlas-users.json — sub must be the user's id in rlapi's users table:

{"users": [{"username": "alice", "sub": "1", "roles": ["customer"], "password_hash": "<hash>"}]}

and, optionally, .secrets/rlas-clients.json to pre-register clients:

{"clients": [{"client_id": "https://client.test/cimd.json", "redirect_uris": ["http://127.0.0.1:8765/cb"]}]}
RLAS_CLIENTS_FILE=.secrets/rlas-clients.json \
  uv run uvicorn --factory rlas.app:asgi_app_from_env --port 9000

Serves on http://127.0.0.1:9000; metadata at /.well-known/oauth-authorization-server, keys at /.well-known/jwks.json. Things that will stop it or surprise you:

  • The key file must be mode 0600. Group- or world-readable keys are refused at boot, as ssh does. keygen refuses to overwrite an existing key — delete it deliberately.
  • Without RLAS_CLIENTS_FILE, every /authorize fetches the client_id URL. That is an SSRF primitive until step 17 (THREAT_MODEL.md F-21). Keep rlas on loopback.
  • PKCE is S256 and the method must be stated. code_challenge_method omitted is an error, not a default (F-16). redirect_uri must match a registered one exactly.
  • Nothing persists. Codes and refresh tokens are in memory; a restart forgets them.

Check everything

Run all five before committing. The first four are the resume sanity check in PLAN.md §0; ruff format --check is in the list because ruff check passes on code the formatter would rewrite, and pre-commit runs the formatter — so a commit fails after the other checks are green.

uv run pytest -q              # 164 passed
uv run pytest -m attack -q    # 57 passed, 107 deselected
uv run mypy                   # Success: no issues found in 37 source files
uv run ruff check .           # All checks passed
uv run ruff format --check .  # 43 files (37 .py + 6 .md)

Those counts are current as of 2026-10-12 (end of step 3). The attack suite is the interesting one: each test proves a specific vulnerability and must fail to exploit against fixed code.

Security tooling has its own runbook — SECURITYTEST.md — with the ruff --select S passes, how to audit # noqa suppressions, and the known blind spots. Start there, not here, for anything security-flavoured.

Layout

packages/rlapi/     CRUD API, owns the database (steps 1–6)
packages/rlas/      OAuth 2.1 authorization server (step 3+)
packages/rlmcp/     MCP server, protocol revision 2026-07-28 (steps 7–16)
tests/unit/         models, migrations, scaffold
tests/integration/  CRUD over a real app
tests/attacks/      exploit scripts kept as regression tests

Each package is a uv workspace member with its own pyproject.toml; the root one holds the shared ruff / mypy / pytest configuration.

Gotchas worth re-reading

Hard-won, in rough order of how likely each is to bite you again:

  • Don't browse the database with DBeaver carelessly. It opens SQLite with foreign_keys = 0, so writes made there bypass step 1's integrity control and can create exactly the orphan rows the hardening defends against. Browse read-only, or issue PRAGMA foreign_keys = ON first. journal_mode = WAL is persisted in the file; foreign_keys is per-connection and is not. Full detail in PLAN.md.
  • --reload falls back to polling. watchfiles is not installed, so uvicorn uses StatReload (mtime polling). It works, just slower and heavier; uv add --dev watchfiles fixes it. Drop --reload for anything production-like.
  • /docs serves a vendored Swagger UI (5.33.2, in rlapi/static/swagger-ui/), not the CDN copy FastAPI defaults to, and /redoc is gone (THREAT_MODEL.md F-14). pip-audit cannot see that directory, so refreshing it is manual: uv run python scripts/vendor_swagger_ui.py <version>, then the provenance check in SECURITYTEST.md §6. The pre-commit fixers are excluded from that directory deliberately, because they rewrite the files and break their hashes.
  • A fake credential in a test needs a per-line suppression, sometimes two: # noqa: S105 for ruff and # pragma: allowlist secret for detect-secrets. There is no blanket exemption for tests/ any more (SECURITYTEST.md gotcha 2).
  • The pre-commit hooks are bypassable with --no-verify, and they are local-only. They also scan Markdown, so writing about a secret in these docs can trip detect-secrets — reword rather than widening the exclude pattern.

The documents

File What it is Updated
PLAN.md the build plan, step by step, plus "where we are" and open items every step — authoritative for status
THREAT_MODEL.md findings F-01…F-21: what was attacked, what actually stopped it, what was theater every step
SECURITYTEST.md how to run the security checks; real commands with real output shapes every step
ARCHITECTURE.md components, trust boundaries, token flows per phase
README.md this file — how to run things when a command or workflow changes

Keep this file to how to run it. Reasoning, findings and status belong in the four above, and duplicating them here just creates two answers that drift apart.

About

Python API and MCP Server security test project.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

⌂ Proxy