agentleFS
Abenor-LabsAbenor-Labs/Open-MHS/.claude/skills/fastapi-scaffold/SKILL.md

fastapi-scaffold

Skill2 starsChanged 2 months agoApache-2.0View on GitHub

Use when adding or modifying async FastAPI routers, Pydantic models, or dependency wiring in the Open-MHS middleware server. Triggers on "add an endpoint", "scaffold a router", "wire up /discover", "expose the execute primitive", "add a JSON-RPC method", or any change under server/.

2 other projects use a version of this.

Adopted by

Most recently taken up by

  • Other versions2 projects use a version of this.

    Word for word · 0

    None found.

    Edited · 0

    None found.

    Wrote their own version · 2

    1. tae0y/python-project-template★ 14same name
    2. erdcpatel/agent-skills★ 0same name

    How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.

  • In this repository3 other files it gives its agents.

The file

We split this skill into the rules it gives an agent

6 rules. About 0 are common, 3 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Always read these first so the new code matches what exists:

    line 19
  2. Distinctive:

    If a router for this concern already exists, extend it.

    line 26
  3. Very specific:

    Every handler is async def: No blocking I/O in a handler — driver calls go through an awaitable transport, or anyio.to_thread.run_sync …

    line 45
  4. Very specific:

    Pydantic v2 models for every request and response: Never return a bare dict. Declare response_model= on the decorator so the OpenAPI doc is …

    line 51
  5. Very specific:

    Never add an endpoint that mutates hardware outside the JSON-RPC dispatcher.

    line 112
  6. Distinctive:

    Add a test alongside the endpoint in the same change.

    line 123

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Open-MHS FastAPI Router Scaffold

Generate async FastAPI routers for the Open-MHS Discovery and Execution layers that match the repo's existing conventions exactly.

When to use

  • Adding a new endpoint to the middleware server.
  • Adding a JSON-RPC method to /execute.
  • Splitting a growing open_mhs/server/main.py into routers.

Read before writing

Always read these first so the new code matches what exists:

sed -n '1,80p' server/main.py
ls server/

If a router for this concern already exists, extend it. Do not create a parallel one.

Layout

server/
  main.py            # app factory + router registration only, no business logic
  models.py          # Pydantic request/response models, shared
  registry.py        # in-memory device registry (single source of truth)
  safety.py          # limit evaluation - the only place bounds are interpreted
  errors.py          # JSON-RPC error codes + typed exceptions
  routers/
    discovery.py     # GET  /discover, POST /register, DELETE /devices/{id}
    rpc.py           # POST /rpc, POST /execute  (JSON-RPC 2.0 dispatcher)
  deps.py            # FastAPI dependencies (get_registry, etc.)

Conventions

  1. Every handler is async def. No blocking I/O in a handler — driver calls go through an awaitable transport, or anyio.to_thread.run_sync for genuinely blocking hardware SDKs.
  2. APIRouter per concern, with prefix and tags set at construction:
    router = APIRouter(tags=["discovery"])
  3. Pydantic v2 models for every request and response. Never return a bare dict. Declare response_model= on the decorator so the OpenAPI doc is accurate — the OpenAPI doc is how an agent discovers the server.
  4. Shared state via Depends, never a module-level global reached into directly:
    async def discover(registry: Registry = Depends(get_registry)) -> DiscoverResponse: ...
  5. Errors are typed. Raise HTTPException for transport-level failures; return a JSON-RPC error object for method-level failures. Never leak a driver traceback.

JSON-RPC contract

POST /rpc is the canonical JSON-RPC 2.0 surface. POST /execute is a compatibility alias onto the same dispatcher. Four methods, and no others:

MethodMutatingPurpose
mhs.discovernoList registered devices and their capability tags.
mhs.readnoRead one sensor or actuator state.
mhs.writeyesCommand one actuator, inside its safety limits.
mhs.emergency_stopyesDrive a device to its declared safe state.

Bare read / write are accepted as legacy aliases for mhs.read / mhs.write.

{"jsonrpc": "2.0", "id": 1, "method": "mhs.read",
 "params": {"device_id": "mock-temp-01", "target": "ambient_temp"}}
{"jsonrpc": "2.0", "id": 2, "method": "mhs.write",
 "params": {"device_id": "arm-01", "target": "joint_1", "value": 45.0}}

Reserved error codes — use these, do not invent new ones in the JSON-RPC reserved range:

CodeMeaning
-32700Parse error
-32600Invalid request
-32601Method not found (anything outside the four mhs.* methods and their aliases)
-32602Invalid params
-32000Device not found
-32001Safety limit violation
-32002Hardware execution error (driver/transport raised)
-32003State desync (feedback sensor disagrees with commanded value after settle)

A write aimed at a sensor id is -32602 (invalid params), not a custom code: the target exists but is not a legal write target, which is a params problem, not a hardware problem.

-32001 must include the attempted value and the violated bound in error.data, so the agent can correct itself without a second round trip. -32003 must include both the commanded value and the observed feedback value: a desync means the world and the agent's model of it have diverged, and the agent cannot recover without seeing both.

Safety rule

The safety check runs in the server, before the driver is called, and again in the driver. Two independent enforcement points. Never scaffold a write path that reaches a driver without passing the value through the device's safety_limits.

Never add an endpoint that mutates hardware outside the JSON-RPC dispatcher. One mutating surface means one place to audit. emergency_stop is the sole exception to limit checking: it drives to the tag-declared safe state, which is trusted by definition.

After scaffolding

python -m pytest tests/ -q
uvicorn open_mhs.server.main:app --reload   # verify /docs renders

Add a test alongside the endpoint in the same change. An endpoint with no test does not ship.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.