toolpreflight 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- toolpreflight/__init__.py +36 -0
- toolpreflight/__main__.py +8 -0
- toolpreflight/analyzer.py +74 -0
- toolpreflight/cli.py +224 -0
- toolpreflight/compat.py +429 -0
- toolpreflight/loaders.py +262 -0
- toolpreflight/models.py +162 -0
- toolpreflight/render.py +144 -0
- toolpreflight/rules.py +333 -0
- toolpreflight/stdio_probe.py +217 -0
- toolpreflight/tokens.py +73 -0
- toolpreflight-0.1.0.dist-info/METADATA +142 -0
- toolpreflight-0.1.0.dist-info/RECORD +17 -0
- toolpreflight-0.1.0.dist-info/WHEEL +5 -0
- toolpreflight-0.1.0.dist-info/entry_points.txt +2 -0
- toolpreflight-0.1.0.dist-info/licenses/LICENSE +21 -0
- toolpreflight-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""toolpreflight: Pre-flight linter, token auditor, and compatibility checker for AI tools."""
|
|
2
|
+
|
|
3
|
+
from toolpreflight.analyzer import analyze_tools
|
|
4
|
+
from toolpreflight.compat import SUPPORTED_PROVIDERS, check_compatibility, normalize_schema
|
|
5
|
+
from toolpreflight.loaders import load_tools, parse_tool_payload
|
|
6
|
+
from toolpreflight.models import (
|
|
7
|
+
Finding,
|
|
8
|
+
PreflightReport,
|
|
9
|
+
ProbeMetadata,
|
|
10
|
+
ProviderSummary,
|
|
11
|
+
Severity,
|
|
12
|
+
TokenBreakdown,
|
|
13
|
+
ToolSpec,
|
|
14
|
+
)
|
|
15
|
+
from toolpreflight.render import render_report
|
|
16
|
+
from toolpreflight.stdio_probe import McpProbeError, probe_stdio_server
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"SUPPORTED_PROVIDERS",
|
|
20
|
+
"Finding",
|
|
21
|
+
"McpProbeError",
|
|
22
|
+
"PreflightReport",
|
|
23
|
+
"ProbeMetadata",
|
|
24
|
+
"ProviderSummary",
|
|
25
|
+
"Severity",
|
|
26
|
+
"TokenBreakdown",
|
|
27
|
+
"ToolSpec",
|
|
28
|
+
"analyze_tools",
|
|
29
|
+
"check_compatibility",
|
|
30
|
+
"load_tools",
|
|
31
|
+
"normalize_schema",
|
|
32
|
+
"parse_tool_payload",
|
|
33
|
+
"probe_stdio_server",
|
|
34
|
+
"render_report",
|
|
35
|
+
]
|
|
36
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""High-level analysis orchestration for toolpreflight."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from toolpreflight.compat import SUPPORTED_PROVIDERS, check_compatibility
|
|
6
|
+
from toolpreflight.models import (
|
|
7
|
+
PreflightReport,
|
|
8
|
+
ProbeMetadata,
|
|
9
|
+
Severity,
|
|
10
|
+
ToolSpec,
|
|
11
|
+
)
|
|
12
|
+
from toolpreflight.rules import lint_tools
|
|
13
|
+
from toolpreflight.tokens import measure_tool_tokens
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def analyze_tools(
|
|
17
|
+
tools: list[ToolSpec],
|
|
18
|
+
*,
|
|
19
|
+
source: str = "<memory>",
|
|
20
|
+
targets: list[str] | None = None,
|
|
21
|
+
max_tokens_budget: int | None = None,
|
|
22
|
+
per_tool_token_limit: int = 500,
|
|
23
|
+
probe: ProbeMetadata | None = None,
|
|
24
|
+
) -> PreflightReport:
|
|
25
|
+
"""Run token estimation, lint rules, and provider compatibility checks."""
|
|
26
|
+
active_targets = targets if targets is not None else list(SUPPORTED_PROVIDERS)
|
|
27
|
+
|
|
28
|
+
breakdowns = [measure_tool_tokens(tool) for tool in tools]
|
|
29
|
+
total_tokens = sum(item.total_tokens for item in breakdowns)
|
|
30
|
+
token_map = {item.tool_name: item.total_tokens for item in breakdowns}
|
|
31
|
+
|
|
32
|
+
lint_findings = lint_tools(
|
|
33
|
+
tools,
|
|
34
|
+
per_tool_token_limit=per_tool_token_limit,
|
|
35
|
+
token_totals=token_map,
|
|
36
|
+
)
|
|
37
|
+
compat_findings, provider_summaries = check_compatibility(tools, active_targets)
|
|
38
|
+
all_findings = lint_findings + compat_findings
|
|
39
|
+
|
|
40
|
+
budget_exceeded = (
|
|
41
|
+
max_tokens_budget is not None and total_tokens > max_tokens_budget
|
|
42
|
+
)
|
|
43
|
+
grade = _compute_grade(
|
|
44
|
+
error_count=sum(1 for f in all_findings if f.severity == Severity.ERROR),
|
|
45
|
+
warning_count=sum(1 for f in all_findings if f.severity == Severity.WARNING),
|
|
46
|
+
budget_exceeded=budget_exceeded,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
return PreflightReport(
|
|
50
|
+
source=source,
|
|
51
|
+
tool_count=len(tools),
|
|
52
|
+
total_tokens=total_tokens,
|
|
53
|
+
max_tokens_budget=max_tokens_budget,
|
|
54
|
+
grade=grade,
|
|
55
|
+
findings=all_findings,
|
|
56
|
+
token_breakdowns=breakdowns,
|
|
57
|
+
provider_summaries=provider_summaries,
|
|
58
|
+
probe=probe,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _compute_grade(
|
|
63
|
+
*, error_count: int, warning_count: int, budget_exceeded: bool
|
|
64
|
+
) -> str:
|
|
65
|
+
score = 100 - (error_count * 18) - (warning_count * 5) - (15 if budget_exceeded else 0)
|
|
66
|
+
if score >= 90 and error_count == 0:
|
|
67
|
+
return "A"
|
|
68
|
+
if score >= 75:
|
|
69
|
+
return "B"
|
|
70
|
+
if score >= 60:
|
|
71
|
+
return "C"
|
|
72
|
+
if score >= 40:
|
|
73
|
+
return "D"
|
|
74
|
+
return "F"
|
toolpreflight/cli.py
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
"""Command-line interface for toolpreflight."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import json
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from toolpreflight.analyzer import analyze_tools
|
|
11
|
+
from toolpreflight.compat import SUPPORTED_PROVIDERS, normalize_schema
|
|
12
|
+
from toolpreflight.loaders import load_tools
|
|
13
|
+
from toolpreflight.render import render_report
|
|
14
|
+
from toolpreflight.stdio_probe import McpProbeError, probe_stdio_server
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
18
|
+
parser = argparse.ArgumentParser(
|
|
19
|
+
prog="toolpreflight",
|
|
20
|
+
description=(
|
|
21
|
+
"Pre-flight linter, token auditor, cross-provider compatibility checker, "
|
|
22
|
+
"and live stdio probe for MCP servers & AI agent tools."
|
|
23
|
+
),
|
|
24
|
+
)
|
|
25
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
26
|
+
|
|
27
|
+
check_p = sub.add_parser(
|
|
28
|
+
"check",
|
|
29
|
+
help="Lint and audit a JSON tool manifest, Python module, or stdio MCP server.",
|
|
30
|
+
)
|
|
31
|
+
check_p.add_argument(
|
|
32
|
+
"source",
|
|
33
|
+
nargs="?",
|
|
34
|
+
help="Path to JSON manifest (MCP/OpenAI/Anthropic/Gemini), .py file, or '-' for stdin.",
|
|
35
|
+
)
|
|
36
|
+
check_p.add_argument(
|
|
37
|
+
"--stdio",
|
|
38
|
+
help="Command to launch a live stdio MCP server instead of reading a file.",
|
|
39
|
+
)
|
|
40
|
+
check_p.add_argument(
|
|
41
|
+
"--target",
|
|
42
|
+
default=",".join(SUPPORTED_PROVIDERS),
|
|
43
|
+
help=f"Comma-separated provider targets ({','.join(SUPPORTED_PROVIDERS)}).",
|
|
44
|
+
)
|
|
45
|
+
check_p.add_argument(
|
|
46
|
+
"--max-tokens",
|
|
47
|
+
type=int,
|
|
48
|
+
default=None,
|
|
49
|
+
help="Fail if total tool definitions exceed this token budget.",
|
|
50
|
+
)
|
|
51
|
+
check_p.add_argument(
|
|
52
|
+
"--per-tool-limit",
|
|
53
|
+
type=int,
|
|
54
|
+
default=500,
|
|
55
|
+
help="Warn when a single tool exceeds this token threshold (default: 500).",
|
|
56
|
+
)
|
|
57
|
+
check_p.add_argument(
|
|
58
|
+
"--format",
|
|
59
|
+
choices=("text", "json", "markdown"),
|
|
60
|
+
default="text",
|
|
61
|
+
help="Output format (default: text).",
|
|
62
|
+
)
|
|
63
|
+
check_p.add_argument(
|
|
64
|
+
"--fail-on",
|
|
65
|
+
choices=("error", "warning", "none"),
|
|
66
|
+
default="error",
|
|
67
|
+
help="Minimum severity that triggers a non-zero exit code (default: error).",
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
probe_p = sub.add_parser(
|
|
71
|
+
"probe",
|
|
72
|
+
help="Launch a live stdio MCP server, verify handshake, and audit its tools.",
|
|
73
|
+
)
|
|
74
|
+
probe_p.add_argument(
|
|
75
|
+
"--stdio",
|
|
76
|
+
required=True,
|
|
77
|
+
help="Command to launch the MCP server (e.g. 'python server.py').",
|
|
78
|
+
)
|
|
79
|
+
probe_p.add_argument(
|
|
80
|
+
"--timeout",
|
|
81
|
+
type=float,
|
|
82
|
+
default=10.0,
|
|
83
|
+
help="Seconds to wait for each JSON-RPC response (default: 10.0).",
|
|
84
|
+
)
|
|
85
|
+
probe_p.add_argument(
|
|
86
|
+
"--smoke-invalid",
|
|
87
|
+
action="store_true",
|
|
88
|
+
help="Safely test invalid arguments on tools annotated with readOnlyHint=true.",
|
|
89
|
+
)
|
|
90
|
+
probe_p.add_argument(
|
|
91
|
+
"--target",
|
|
92
|
+
default=",".join(SUPPORTED_PROVIDERS),
|
|
93
|
+
help=f"Comma-separated provider targets ({','.join(SUPPORTED_PROVIDERS)}).",
|
|
94
|
+
)
|
|
95
|
+
probe_p.add_argument(
|
|
96
|
+
"--max-tokens",
|
|
97
|
+
type=int,
|
|
98
|
+
default=None,
|
|
99
|
+
help="Fail if total tool definitions exceed this token budget.",
|
|
100
|
+
)
|
|
101
|
+
probe_p.add_argument(
|
|
102
|
+
"--format",
|
|
103
|
+
choices=("text", "json", "markdown"),
|
|
104
|
+
default="text",
|
|
105
|
+
help="Output format (default: text).",
|
|
106
|
+
)
|
|
107
|
+
probe_p.add_argument(
|
|
108
|
+
"--fail-on",
|
|
109
|
+
choices=("error", "warning", "none"),
|
|
110
|
+
default="error",
|
|
111
|
+
help="Minimum severity that triggers a non-zero exit code (default: error).",
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
fix_p = sub.add_parser(
|
|
115
|
+
"fix",
|
|
116
|
+
help="Normalize tool schemas for a target LLM provider.",
|
|
117
|
+
)
|
|
118
|
+
fix_p.add_argument("source", help="Path to JSON manifest or .py file.")
|
|
119
|
+
fix_p.add_argument(
|
|
120
|
+
"--target",
|
|
121
|
+
choices=SUPPORTED_PROVIDERS,
|
|
122
|
+
default="openai_strict",
|
|
123
|
+
help="Target provider schema profile (default: openai_strict).",
|
|
124
|
+
)
|
|
125
|
+
fix_p.add_argument(
|
|
126
|
+
"-o",
|
|
127
|
+
"--output",
|
|
128
|
+
default="-",
|
|
129
|
+
help="Output JSON file path (default: stdout).",
|
|
130
|
+
)
|
|
131
|
+
fix_p.add_argument(
|
|
132
|
+
"--no-preserve-constraints",
|
|
133
|
+
action="store_true",
|
|
134
|
+
help="Do not append stripped validation constraints into property descriptions.",
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
return parser
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _parse_targets(raw: str) -> list[str]:
|
|
141
|
+
targets = [t.strip() for t in raw.split(",") if t.strip()]
|
|
142
|
+
for t in targets:
|
|
143
|
+
if t not in SUPPORTED_PROVIDERS:
|
|
144
|
+
raise ValueError(
|
|
145
|
+
f"Unknown target '{t}'. Choose from: {', '.join(SUPPORTED_PROVIDERS)}"
|
|
146
|
+
)
|
|
147
|
+
return targets
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def main(argv: list[str] | None = None) -> int:
|
|
151
|
+
parser = build_parser()
|
|
152
|
+
args = parser.parse_args(argv)
|
|
153
|
+
|
|
154
|
+
try:
|
|
155
|
+
if args.command == "check":
|
|
156
|
+
targets = _parse_targets(args.target)
|
|
157
|
+
if args.stdio:
|
|
158
|
+
tools, probe_meta = probe_stdio_server(args.stdio)
|
|
159
|
+
source_label = f"stdio:{args.stdio}"
|
|
160
|
+
elif args.source:
|
|
161
|
+
tools = load_tools(args.source)
|
|
162
|
+
probe_meta = None
|
|
163
|
+
source_label = args.source
|
|
164
|
+
else:
|
|
165
|
+
parser.error("Either <source> or --stdio must be provided.")
|
|
166
|
+
return 2
|
|
167
|
+
|
|
168
|
+
report = analyze_tools(
|
|
169
|
+
tools,
|
|
170
|
+
source=source_label,
|
|
171
|
+
targets=targets,
|
|
172
|
+
max_tokens_budget=args.max_tokens,
|
|
173
|
+
per_tool_token_limit=args.per_tool_limit,
|
|
174
|
+
probe=probe_meta,
|
|
175
|
+
)
|
|
176
|
+
print(render_report(report, args.format))
|
|
177
|
+
return 1 if report.should_fail(args.fail_on) else 0
|
|
178
|
+
|
|
179
|
+
if args.command == "probe":
|
|
180
|
+
targets = _parse_targets(args.target)
|
|
181
|
+
tools, probe_meta = probe_stdio_server(
|
|
182
|
+
args.stdio,
|
|
183
|
+
timeout_sec=args.timeout,
|
|
184
|
+
smoke_invalid=args.smoke_invalid,
|
|
185
|
+
)
|
|
186
|
+
report = analyze_tools(
|
|
187
|
+
tools,
|
|
188
|
+
source=f"stdio:{args.stdio}",
|
|
189
|
+
targets=targets,
|
|
190
|
+
max_tokens_budget=args.max_tokens,
|
|
191
|
+
probe=probe_meta,
|
|
192
|
+
)
|
|
193
|
+
smoke_failed = any(
|
|
194
|
+
not item["handled_invalid_cleanly"] for item in probe_meta.smoke_checks
|
|
195
|
+
)
|
|
196
|
+
print(render_report(report, args.format))
|
|
197
|
+
if smoke_failed or report.should_fail(args.fail_on):
|
|
198
|
+
return 1
|
|
199
|
+
return 0
|
|
200
|
+
|
|
201
|
+
if args.command == "fix":
|
|
202
|
+
tools = load_tools(args.source)
|
|
203
|
+
fixed_tools = []
|
|
204
|
+
for tool in tools:
|
|
205
|
+
tool_dict = tool.to_mcp_dict()
|
|
206
|
+
tool_dict["inputSchema"] = normalize_schema(
|
|
207
|
+
tool.input_schema,
|
|
208
|
+
target=args.target,
|
|
209
|
+
preserve_constraints=not args.no_preserve_constraints,
|
|
210
|
+
)
|
|
211
|
+
fixed_tools.append(tool_dict)
|
|
212
|
+
|
|
213
|
+
rendered = json.dumps({"tools": fixed_tools}, indent=2)
|
|
214
|
+
if args.output == "-":
|
|
215
|
+
print(rendered)
|
|
216
|
+
else:
|
|
217
|
+
Path(args.output).write_text(rendered + "\n", encoding="utf-8")
|
|
218
|
+
return 0
|
|
219
|
+
|
|
220
|
+
except (FileNotFoundError, ValueError, json.JSONDecodeError, McpProbeError) as exc:
|
|
221
|
+
print(f"toolpreflight error: {exc}", file=sys.stderr)
|
|
222
|
+
return 2
|
|
223
|
+
|
|
224
|
+
return 0
|