-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi_contract_guardian_mcp.py
More file actions
80 lines (64 loc) · 2.5 KB
/
Copy pathapi_contract_guardian_mcp.py
File metadata and controls
80 lines (64 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
"""
Example: Wrap api-contract-guardian as an MCP server.
This demonstrates how click-to-mcp can turn any Click/typer CLI into an MCP server
that AI coding agents (Claude Code, Cursor, Copilot) can use directly.
Run this example:
pip install click-to-mcp
python examples/api_contract_guardian_mcp.py
Then configure your MCP client (e.g. Claude Code):
# .claude/settings.json
{
"mcpServers": {
"api-contract-guardian": {
"command": "python",
"args": ["examples/api_contract_guardian_mcp.py"]
}
}
}
"""
import click
from click_to_mcp import run
# --- Mock api-contract-guardian CLI (replace with real import) ---
# In production, you would do:
# from api_contract_guardian.cli import app
# run(app, prefix="acg")
@click.group()
def acg():
"""API Contract Guardian — validate API contracts against live endpoints."""
pass
@acg.command()
@click.argument("spec_file", type=click.Path(exists=False))
@click.option("--base-url", default="http://localhost:8000", help="Base URL of the API")
@click.option("--strict/--no-strict", default=False, help="Fail on any contract violation")
@click.option(
"--format",
"-f",
"output_format",
type=click.Choice(["json", "table", "junit"]),
default="table",
help="Output format",
)
def validate(spec_file: str, base_url: str, strict: bool, output_format: str):
"""Validate an OpenAPI spec against a live API."""
click.echo(f"Validating {spec_file} against {base_url}...")
click.echo(f" Format: {output_format}")
click.echo(f" Strict: {strict}")
click.echo("✓ All contracts pass")
@acg.command()
@click.argument("spec_file", type=click.Path(exists=False))
@click.option("--output", "-o", default="contracts.json", help="Output file path")
def extract(spec_file: str, output: str):
"""Extract contracts from an OpenAPI spec file."""
click.echo(f"Extracting contracts from {spec_file}...")
click.echo(f" Output: {output}")
click.echo("✓ 12 contracts extracted")
@acg.command()
@click.option("--watch/--no-watch", default=False, help="Watch for spec changes")
@click.option("--interval", default=30, type=int, help="Watch interval in seconds")
def monitor(watch: bool, interval: int):
"""Monitor API endpoints for contract drift."""
click.echo(f"Monitoring contracts... (watch={watch}, interval={interval}s)")
click.echo("✓ No drift detected")
if __name__ == "__main__":
run(acg, prefix="acg", name="api-contract-guardian")