AI Assistant Hooks¶
Configure automatic API linting when working with AI coding assistants and git pre-commit hooks.
Overview¶
Hooks enable automatic API style checking in two contexts:
- AI Assistants - Auto-lint when you save OpenAPI files in Claude Code, Cursor, or Windsurf
- Git Pre-Commit - Block commits that contain API style violations
Supported Assistants¶
| Assistant | Config File | Events |
|---|---|---|
| Claude Code | .claude/settings.json |
16 events |
| Cursor | .cursor/hooks.json |
12 events |
| Windsurf | .windsurf/hooks.json |
9 events |
Quick Setup¶
# Generate hooks for Claude Code
api-style hooks --format claude
# Generate for all assistants
api-style hooks --format all
# See supported formats
api-style hooks list
# Install git pre-commit hook
api-style hooks init
Hook Types¶
Auto-Lint on Save¶
Automatically lint OpenAPI files when they're saved:
This creates a hook that:
- Triggers when files are written (Edit/Write tools)
- Checks if the file matches OpenAPI patterns
- Runs
mcp-api-style lintif it matches - Returns violations to the assistant
Matched Patterns:
openapi.yaml,openapi.yml,openapi.jsonswagger.yaml,swagger.yml,swagger.json**/openapi.yaml,**/api.yaml, etc.
Context Injection¶
Inject API style guidelines before prompts:
This adds context about the active style profile to help the AI assistant write better APIs.
Configuration Options¶
| Option | Default | Description |
|---|---|---|
--format |
claude |
Target assistant |
--profile |
default |
Style profile for linting |
--auto-lint |
true |
Enable auto-lint on save |
--inject-context |
false |
Inject style context |
--output |
auto | Output file path |
Generated Configuration¶
Claude Code¶
.claude/settings.json:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "#!/bin/bash\nFILE=\"$CLAUDE_FILE_PATH\"\n...",
"timeout": 30
}
]
}
]
}
}
Cursor¶
.cursor/hooks.json:
{
"hooks": {
"after_file_write": [
{
"pattern": "Write|Edit",
"actions": [
{
"type": "shell",
"command": "mcp-api-style lint ...",
"timeout": 30000
}
]
}
]
}
}
Windsurf¶
.windsurf/hooks.json:
Prerequisites¶
For hooks to work, the mcp-api-style binary must be in your PATH:
Manual Configuration¶
Claude Code¶
Add to .claude/settings.json:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "mcp-api-style lint -f \"$CLAUDE_FILE_PATH\" -p default 2>&1 | head -50",
"timeout": 30
}
]
}
]
}
}
Using with MCP Server¶
For full integration, combine hooks with the MCP server:
- Configure MCP server in settings
- Generate hooks for auto-linting
- AI assistant can use both automatic checks and on-demand tools
Workflow Example¶
-
Setup:
-
Working with Claude:
- Ask Claude to create an OpenAPI spec
- When Claude writes the file, hooks run automatically
- Violations appear in the conversation
-
Claude can fix issues immediately
-
On-Demand Tools:
- "Use the lint tool to check this against Azure guidelines"
- "Explain rule URI-001"
- "Run analyze for a GO/NO-GO decision"
Troubleshooting¶
Hooks Not Triggering¶
- Verify the config file exists in the correct location
- Check that
mcp-api-styleis in PATH - Ensure the file matches OpenAPI patterns
Timeout Issues¶
Increase the timeout in the generated config:
Profile Not Found¶
Ensure the profile name is valid:
Git Pre-Commit Hook¶
Install a git pre-commit hook that lints staged OpenAPI files before each commit:
How It Works¶
- When you run
git commit, the hook triggers - It finds staged files matching OpenAPI patterns (
openapi.yaml,swagger.json, etc.) - Each file is linted using
api-style lint - If errors are found, the commit is blocked
- Warnings are displayed but don't block commits
Options¶
# Use Azure profile
api-style hooks init --profile azure
# Enforce silver conformance level
api-style hooks init --level silver
# Overwrite existing hook
api-style hooks init --force
Bypassing the Hook¶
For emergency commits, bypass the hook with:
Uninstalling¶
Remove the hook manually:
Next Steps¶
- MCP Server - Full MCP integration
- CLI Reference - Command documentation