Skip to main content

Test-gated AI agent workflow documentation for HTTP APIs.

Project description

agent-grammar

Test-gated AI agent workflow documentation for HTTP APIs.

agent-grammar lets API developers attach machine-readable workflow documentation to their existing pytest suite. When integration tests pass, a workflows.md blueprint is auto-compiled and served at a versioned route so that external developers' AI agents (Cursor, Claude, Copilot, Gemini) can fetch it and generate accurate integration code — no hallucinated endpoints, no missing parameter bindings.

Single source of truth: when business logic changes and tests are updated, the AI documentation re-compiles automatically.

Install

pip install agent-grammar          # core (pytest + serve via Starlette)
pip install "agent-grammar[fastapi]"  # adds FastAPI for serving

Quick Start

1. Annotate an integration test

from agent_grammar import AgentTestClient, step_boundary, workflow
from app.main import app

client = AgentTestClient(app)

@workflow(
    name="Material Onboarding Lifecycle",
    intent="Secure a token, query the local DB for a zone, and register an asset.",
    bindings=[
        {"source": "Step 1.response.access_token", "target": "Step 3.headers.Authorization"},
        {"source": "Step 2.mocked_db_result",      "target": "Step 3.body.assigned_zone"},
    ],
)
def test_compile_material_onboarding():
    auth = client.post("/v1/auth/token", json={"seed": "dev-token"})
    assert auth.status_code == 200
    token = auth.json()["access_token"]

    with step_boundary(domain="Database", name="Query PostgreSQL for Zone UUID"):
        zone_id = "mocked-uuid-from-db-query"

    resp = client.post(
        "/v1/materials",
        json={"sku": "MAT-9901", "assigned_zone": zone_id},
        headers={"Authorization": f"Bearer {token}"},
    )
    assert resp.status_code == 201

2. Run the test suite

pytest --agent-grammar-output=assets/workflows.md

When the test passes, assets/workflows.md is compiled. Failing tests are excluded — that's the "Test-Gated" guarantee.

3. Serve the compiled markdown

from fastapi import FastAPI
from agent_grammar.serve.fastapi import AgentTelemetryMiddleware, GrammarRouter

app = FastAPI(title="Core Engine API - v1")

def log_agent_metric(workflow_id: str) -> None:
    print(f"METRIC: agent-driven request for workflow {workflow_id}")

app.add_middleware(AgentTelemetryMiddleware, on_detect=log_agent_metric)
app.include_router(
    GrammarRouter(filepath="assets/workflows.md"),
    prefix="/v1/agent-workflows",
)

4. Publish system prompts for external developers

agent-grammar export-agent-docs \
    --base-url https://api.production.com \
    --api-version v1

Writes ./agent-docs/{cursor,claude,copilot,gemini}-rules.md ready for the API host's developer portal.

Configuration

Option Where Default
--agent-grammar-output pytest CLI assets/workflows.md
agent_grammar_output pyproject.toml [tool.pytest.ini_options] assets/workflows.md
--agent-grammar-disable pytest CLI off

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agent_grammar-0.1.2.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agent_grammar-0.1.2-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file agent_grammar-0.1.2.tar.gz.

File metadata

  • Download URL: agent_grammar-0.1.2.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for agent_grammar-0.1.2.tar.gz
Algorithm Hash digest
SHA256 3444b72a3e579fc97162ada115946b53c384059e96434e91873972bee0824122
MD5 3d2759f9a1618853a2d71a90754b624e
BLAKE2b-256 1e41d4e86df34dc0a7bd86349e00cecb358640e9aa5c6a5df84d2a863e569994

See more details on using hashes here.

Provenance

The following attestation bundles were made for agent_grammar-0.1.2.tar.gz:

Publisher: publish.yml on dlfelps/agent-grammar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file agent_grammar-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: agent_grammar-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for agent_grammar-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 91c97bdabbf71d7f9ceefc2b89e66631a225061b659bbb91fef8f9861730b126
MD5 6fec3ec3ed254a5b67b097c9781d927d
BLAKE2b-256 1bcdec19c729e3a8ce5d9e83f00889e3e8a1751b018c048fe9aaabfcd2ffc84f

See more details on using hashes here.

Provenance

The following attestation bundles were made for agent_grammar-0.1.2-py3-none-any.whl:

Publisher: publish.yml on dlfelps/agent-grammar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page