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.

📖 Full documentation: https://dlfelps.github.io/agent-grammar/

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.

How it differs from fastapi-mcp

agent-grammar is aimed at making an agent a better coder, not a self-sufficient one. Tools like fastapi-mcp expose your endpoints as live MCP tools so an agent can call your API directly at runtime. agent-grammar does the opposite: it never puts the agent in the request path. It hands the agent a test-gated workflow contract to read so the integration code it writes — code a developer reviews and ships — is correct the first time. The two are complementary; see agent-grammar vs. fastapi-mcp for the full comparison.

Install

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

The pytest plugin is registered through the pytest11 entry point and is discovered automatically once the package is installed — no conftest.py change required.

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.",
)
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.

Documentation

The full documentation site at https://dlfelps.github.io/agent-grammar/ covers:

  • Installation — Python versions, install variants, and verification.
  • Walkthrough — Clone the dlfelps/roller-coaster FastAPI demo, decorate its three tests, and generate workflows.md end-to-end in under 10 minutes.
  • Configuration — Every pytest flag, GrammarRouter argument, and CLI option.
  • API Reference — Generated from the in-package docstrings.

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.3.tar.gz (27.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.3-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: agent_grammar-0.1.3.tar.gz
  • Upload date:
  • Size: 27.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.3.tar.gz
Algorithm Hash digest
SHA256 9659d3ba781030ace0dc596dc9b06a590ae8ea199712e1f9c864632c0a923bb5
MD5 eaae8830fafc4908bdbf2763821f51c0
BLAKE2b-256 a7402defdeee65ff75995e56c1c0862613264ef90534f1025d567c8a816e6c86

See more details on using hashes here.

Provenance

The following attestation bundles were made for agent_grammar-0.1.3.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.3-py3-none-any.whl.

File metadata

  • Download URL: agent_grammar-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 20.5 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 18bfecca887d770b746a64e26031b7fc1922f26c49f978d76a31b7d560ae38f3
MD5 c634a5af7f1f671bdbb1f46185b7210e
BLAKE2b-256 c14a3a2cff1db3f6d50d39c984d85dd73786e64bfe89cecfead0f75a01395346

See more details on using hashes here.

Provenance

The following attestation bundles were made for agent_grammar-0.1.3-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