Metadata-Version: 2.4
Name: toolpreflight
Version: 0.1.0
Summary: Zero-dependency pre-flight linter, token auditor, cross-provider schema checker, and live stdio smoke-tester for MCP servers and AI agent tools.
Author: Open Source Contributors
License-Expression: MIT
Project-URL: Homepage, https://github.com/Sarbojit10/toolpreflight
Project-URL: Repository, https://github.com/Sarbojit10/toolpreflight
Project-URL: Issues, https://github.com/Sarbojit10/toolpreflight/issues
Keywords: mcp,model-context-protocol,ai-agents,llm,json-schema,openai,gemini,anthropic,linter,devtools
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Quality Assurance
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: tiktoken
Requires-Dist: tiktoken>=0.7.0; extra == "tiktoken"
Provides-Extra: rich
Requires-Dist: rich>=13.0.0; extra == "rich"
Provides-Extra: pydantic
Requires-Dist: pydantic>=2.0.0; extra == "pydantic"
Provides-Extra: dev
Requires-Dist: pytest>=8.0.0; extra == "dev"
Requires-Dist: ruff>=0.6.0; extra == "dev"
Dynamic: license-file

# toolpreflight

**Zero-dependency pre-flight linter, context-token auditor, cross-provider schema checker, and live stdio probe for MCP servers & AI agent tools.**

AI agents fail silently when tool schemas are vague, redundant, oversized, missing safety annotations, or incompatible with a target LLM provider's structured-output / function-calling rules. `toolpreflight` catches those issues in milliseconds—offline or against a live MCP `stdio` server—before your users or CI hit `400 Bad Request` or tool-selection errors.

## Why `toolpreflight`?

- **Zero mandatory dependencies:** Pure Python 3.10+ stdlib core (with automatic `tiktoken` and `rich` enhancement if installed).
- **Format-agnostic:** Inspects MCP `tools/list` JSON, OpenAI `tools`, Anthropic `tools`, Gemini `functionDeclarations`, Python files (typed functions & Pydantic models), or live MCP `stdio` servers.
- **Context-window budget guard:** Measures per-tool and total token overhead (`--max-tokens`) so tool schemas don't quietly eat your prompt budget.
- **Cross-provider compatibility:** Validates schemas against **OpenAI Strict** (`strict: true`), **Gemini**, **Anthropic**, and **Local GBNF** (Ollama / llama.cpp) constraints.
- **Auto-normalization (`fix`):** Compiles schemas for a target provider (inlining `$defs`/`$ref`, enforcing `additionalProperties: false`, and preserving stripped constraint ranges in descriptions).
- **CI-native:** Supports `--format text | json | markdown` and `--fail-on error | warning`.

## Quickstart

Install locally in editable mode:

```bash
pip install -e .
```

### 1. Audit a tool manifest (MCP, OpenAI, Anthropic, Gemini, or Python module)

```bash
toolpreflight check examples/sample_mcp_tools.json --target openai_strict,gemini,anthropic --max-tokens 2000
```

### 2. Probe a live MCP `stdio` server

```bash
toolpreflight probe --stdio "python examples/demo_mcp_server.py"
```

Pass `--smoke-invalid` to safely send malformed/empty arguments **only** to tools marked `readOnlyHint: true` and verify the server returns a clean protocol error instead of crashing.

### 3. Normalize schemas for a target provider

```bash
toolpreflight fix examples/sample_mcp_tools.json --target openai_strict -o fixed_tools.json
```

### 4. Generate a GitHub PR Markdown report in CI

```bash
toolpreflight check tools.json --format markdown --fail-on error
```

## Example Output

Running `toolpreflight check examples/sample_mcp_tools.json --fail-on none`:

```text
toolpreflight report | source: examples/sample_mcp_tools.json
====================================================================
Grade: F | Tools: 2 | Context Tax: ~300 tokens
Findings: 8 error(s), 7 warning(s)

Provider Compatibility:
  [FAIL] openai_strict  errors=4 warnings=2
  [FAIL] gemini         errors=3 warnings=0
  [PASS] anthropic      errors=0 warnings=0
  [PASS] local_gbnf     errors=0 warnings=0

Token Breakdown (heaviest first):
  - search_issues                ~ 187 tok (desc=19, schema=156, heuristic)
  - delete_issue_comment         ~ 113 tok (desc=4, schema=95, heuristic)
```

Running `toolpreflight probe --stdio "python examples/demo_mcp_server.py" --target openai_strict,anthropic --smoke-invalid`:

```text
toolpreflight report | source: stdio:python examples/demo_mcp_server.py
====================================================================
Grade: A | Tools: 1 | Context Tax: ~95 tokens
Findings: 0 error(s), 0 warning(s)
Live Probe: demo-weather-mcp v0.1.0 (protocol 2024-11-05, init 81.55 ms, tools/list 0.51 ms)
  - Smoke invalid probe [PASS] get_weather (tool_result)
```

## Lint Rules

| Code | Severity | Description |
| --- | --- | --- |
| `TP001` | `error` | Tool is missing a description. |
| `TP002` | `warning` | Tool description is too short (`< 20` chars) or overly generic. |
| `TP003` | `error` | Missing `inputSchema` or root schema is not `"type": "object"`. |
| `TP004` | `warning` | Schema property is missing a `description`. |
| `TP005` | `error` | Array property is missing `items` definition. |
| `TP006` | `warning` | Schema nesting exceeds recommended depth (`> 4`). |
| `TP007` | `error` / `warning` | `required` references undefined properties, or object properties lack a `required` list. |
| `TP008` | `warning` | Mutating/destructive tool name lacks MCP safety annotations (`readOnlyHint` / `destructiveHint`). |
| `TP009` | `warning` | Single tool exceeds recommended per-tool token threshold (`> 500` tokens). |
| `TP010` | `warning` | High lexical overlap between two tools in the same server (risks LLM tool confusion). |
| `TP011` | `error` | Duplicate tool name detected in the manifest. |

## Running Tests

```bash
python -m unittest discover -s tests -v
# or with pytest:
pytest -v
```

## Contributing

Contributions are welcome! See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines on adding new lint rules, provider profiles, and tests.
