OpenAPI 3.1 and MCP Dual Generator from Pure Python AST
Maintaining hand-written OpenAPI specs for REST consumers alongside separate MCP JSON-RPC servers for AI coding agents inevitably drifts out of sync. Monetize-Anything parses Python type hints and docstrings statically via Python 3's built-in ast module and generates both GET /openapi.json and POST /mcp from the exact same MonetizeTargetSpec IR, wired directly into Creem prepaid billing. Verified in our live adversarial Gauntlet (16/16 real non-mocked heterogeneous cases passing in 13546.33 ms total, median 305.8 ms, plus 34/34 unit tests under python3 -W default with 0 warnings).
HTTP 422, and empty or fake-200 errors bill strictly $0.00.
Deterministic Architecture & Billing Invariants
A single AST walk maps str/int/float/bool/list/dict annotations and defaults into JSON Schema properties, producing synchronized OpenAPI 3.1 paths, MCP tools/list schemas, and DualMcpFacade flat discovery tools on pure Python 3 stdlib.
1-Source → 3-Channel Publishing: Every compiled target emits (1) a self-hosted Creem prepaid storefront + SQLite WAL gateway (3.9% + $0.40 MoR, $0/mo base), (2) a remote Streamable JSON-RPC 2.0 /mcp endpoint + Causal Bazaar public card (b01_monetize_anything_compiler_v1), and (3) a 4-file treg_hub/ merchant pack at 0% commission (treg call monetize-anything).
Data-Backed Comparison & Live Benchmarks
| Verified Python Target / Feature | DIY FastAPI + MCP SDK | Monetize-Anything Dual AST Compiler | Engineering Benefit |
|---|---|---|---|
| Python PhoneNumbers E.164 International Parser & Validator (Apache-2.0, 3,780 GitHub stars) — https://github.com/daviddrysdale/python-phonenumbers | Numverify Professional · $59.99/mo · $0.003/call | $19 Starter (38,000 calls, $0.0005/call) | 83.3% cheaper; 92% cold / 100% cached margin |
| JSONSchema Draft 2020-12 Strict Validator & Diff Function (MIT, 5,004 GitHub stars) — https://github.com/python-jsonschema/jsonschema | RapidAPI Hosted JSON Validation APIs · $29/mo · $0.0029/call | $19 Starter (38,000 calls, $0.0005/call) | 82.8% cheaper; 94% cold / 100% cached margin |
| Croniter Deterministic Cron Schedule Expander & Collision Auditor (MIT, 568 GitHub stars) — https://github.com/kiorky/croniter | RapidAPI / Cron Monitoring Utility APIs · $25/mo · $0.0025/call | $19 Starter (47,500 calls, $0.0004/call) | 84% cheaper; 93.75% cold / 100% cached margin |
| Runtime Footprint | FastAPI + Pydantic + Uvicorn + MCP SDK | 0 external packages (Python 3 stdlib only) | Zero pip dependency churn |
Copy-Paste Integration Example
curl -fsSL -X POST "https://monetize.fastmesh.dev/v1/inspect" \
-H "Content-Type: application/json" \
-d '{"target_kind":"python_ast","source_code":"def expand_cron_schedule(expr: str, count: int = 10) -> dict:\n \"\"\"Expand cron expression into upcoming UTC timestamps.\"\"\"\n return {\"expr\": expr, \"count\": count}"}'
Deterministic Output
{
"ok": true,
"spec": {
"target_kind": "python_ast",
"operations": [{"name": "expand_cron_schedule", "input_schema": {"required": ["expr"]}}]
}
}
Implementation Steps
- Add standard Python type annotations & docstrings: No custom decorators or web-framework imports required.
- Compile OpenAPI 3.1 + /mcp from one IR: Call POST /v1/compile to emit openapi.json and server.py serving both protocols cleanly.
- Expose /llms.txt and /skill.md for agent discovery: Give human engineers REST docs and autonomous agents flat MCP tooling simultaneously.
Frequently Asked Questions
Does inspection import or execute my Python file?
Never. Pure ast.parse() inspects syntax trees statically without running top-level module code.
Are optional parameters with defaults handled accurately?
Yes. Parameters with default values are omitted from JSON Schema required[] in both OpenAPI 3.1 and MCP inputSchema.
What Python versions are supported?
Any standard Python 3.10+ interpreter with zero external packages.
support@fastmesh.dev.