Skip to main content

API Contract Doctor

Validate OpenAPI API contracts (YAML or JSON) straight from your terminal, or via the bundled MCP (Model Context Protocol) server for AI clients such as VS Code Copilot and Claude Desktop.

  • Detects the contract version (openapi: 3.0.x / 3.1.x / swagger: 2.0).
  • Validates against the official OpenAPI schema using openapi-spec-validator.
  • Never crashes on bad input: missing files, YAML syntax errors, and non-object documents all come back as readable errors.

Installation

pip install api-contract-doctor

Requires Python 3.9+.

Command line interface

Check whether a contract file is valid:

api-contract-doctor validate contracts/sample.yaml

Valid contract:

contracts/sample.yaml: VALID (OpenAPI 3.0.3, "Pet Store API")

Invalid contract (exit code 1):

contracts/invalid_sample.yaml: INVALID
  1. 'version' is a required property (at $.info)

Examples in this README use files from the contracts/ folder of this repository.

MCP server

The package also exposes a local MCP (Model Context Protocol) server that lets AI clients validate contracts through the validate_contract tool.

Run it with:

python -m server

or, from a source checkout:

python server.py

The server starts and prints nothing and just sits there — that is normal. It communicates over stdio (stdin/stdout) using the MCP protocol, so it is designed to be launched by an MCP client, not typed at by a human. Press Ctrl+C to stop it.

To verify the whole MCP round-trip (handshake + tool call) without a client:

python e2e_check.py

Connect it to VS Code (Copilot agent mode)

VS Code (with GitHub Copilot in agent mode) reads MCP servers from .vscode/mcp.json. Add this file to your workspace and reload VS Code:

{
  "servers": {
    "api-contract-doctor": {
      "command": "${workspaceFolder}/.venv/Scripts/python.exe",
      "args": ["${workspaceFolder}/server.py"]
    }
  }
}

On macOS/Linux the interpreter path is ${workspaceFolder}/.venv/bin/python instead. After reloading, open Copilot Chat in agent mode, and the validate_contract tool becomes available to the model.

A separate VS Code extension is bundled in this repository (extension.js + package.json), which registers the command "API Contract Doctor: Validate Contract". It reuses the exact same tools.validator logic through validate_contract_cli.py and can be packaged with npx @vscode/vsce package.

Development

Setup

python -m venv .venv
# Windows PowerShell:
.venv\Scripts\Activate.ps1
# macOS / Linux:
source .venv/bin/activate

pip install -r requirements.txt

Build the Python package

pip install build
python -m build
python -m twine check dist/*

Run the tests

python -m pytest tests -v

Project structure

api-contract-doctor/
├── cli.py                     # Command line entry point (validate command)
├── server.py                  # MCP server (stdio transport)
├── tools/
│   ├── __init__.py
│   └── validator.py           # OpenAPI validation logic (shared)
├── contracts/
│   ├── sample.yaml            # a VALID OpenAPI 3.0 contract
│   └── invalid_sample.yaml    # an intentionally BROKEN contract (for demos)
├── tests/
│   ├── test_validator.py      # pytest tests for the validator
│   └── test_cli.py            # pytest tests for the CLI
├── validate_contract_cli.py   # bridge used by the VS Code extension
├── extension.js               # VS Code extension (main)
├── package.json               # VS Code extension manifest
├── pyproject.toml             # Python package configuration
├── requirements.txt
├── LICENSE.md                 # MIT License
└── README.md

How validate_contract works

  1. Reads the file and parses it (JSON is tried first, then YAML, so both formats work).
  2. Detects the contract version (openapi / swagger).
  3. Validates the document against the official OpenAPI JSON Schema for that version.
  4. Returns a structured result, e.g.:
{
  "file": "contracts/invalid_sample.yaml",
  "valid": false,
  "openapi_version": "3.0.0",
  "title": "Broken API",
  "errors": [
    { "message": "'version' is a required property", "json_path": "$.info" }
  ]
}

Bad input never crashes: every failure mode is reported as valid: false plus a human-readable entry in errors.

License

MIT

Metadata

Release files for api-contract-doctor 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for api-contract-doctor 0.0.1
File Size Uploaded Provenance Actions
api_contract_doctor-0.0.1.tar.gz 7.5 kB Without provenance Details for api_contract_doctor-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for api-contract-doctor 0.0.1
File Interpreter ABI Platform Actions
api_contract_doctor-0.0.1-py3-none-any.whl Python 3 none any Details for api_contract_doctor-0.0.1-py3-none-any.whl

Total release size: 14.4 kB

Release files / api_contract_doctor-0.0.1.tar.gz

Download URL api_contract_doctor-0.0.1.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a50b49c16c5d763bc36078d8ff5fbb423aeb1ff1bb18f957f70cd51ab72944b4
BLAKE2b-256 checksum
How to use checksums
f5e7b83700b17271610237acdff836b347ca8f9edd844d83d2b4ce48e9e42e0d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / api_contract_doctor-0.0.1-py3-none-any.whl

Download URL api_contract_doctor-0.0.1-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
76f301a2201a7097eed5bbf18ae5ef5398ba254b4771c43a6d83821ca129acc2
BLAKE2b-256 checksum
How to use checksums
dd25eaf4f367b7be98eb6839fa2f9bf3eda50cf0efe3920f37a597c198cb0037
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Security for api-contract-doctor 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page