mcp-gauntlet 0.0.0-stage → 0.2.0
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.
- package/LICENSE +21 -0
- package/README.md +202 -3
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +223 -0
- package/dist/config.d.ts +42 -0
- package/dist/config.js +20 -0
- package/dist/connect.d.ts +24 -0
- package/dist/connect.js +126 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +9 -0
- package/dist/lock.d.ts +24 -0
- package/dist/lock.js +103 -0
- package/dist/report.d.ts +20 -0
- package/dist/report.js +111 -0
- package/dist/rules/docs.d.ts +6 -0
- package/dist/rules/docs.js +92 -0
- package/dist/rules/index.d.ts +3 -0
- package/dist/rules/index.js +14 -0
- package/dist/rules/names.d.ts +6 -0
- package/dist/rules/names.js +135 -0
- package/dist/rules/schema.d.ts +2 -0
- package/dist/rules/schema.js +251 -0
- package/dist/schema-walk.d.ts +12 -0
- package/dist/schema-walk.js +56 -0
- package/dist/smoke.d.ts +13 -0
- package/dist/smoke.js +67 -0
- package/dist/surface.d.ts +7 -0
- package/dist/surface.js +32 -0
- package/dist/tokens.d.ts +7 -0
- package/dist/tokens.js +9 -0
- package/dist/types.d.ts +67 -0
- package/dist/types.js +1 -0
- package/package.json +65 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 velutinous
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,202 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# mcp-gauntlet
|
|
4
|
+
|
|
5
|
+
**Run your MCP server through every client's rules before your users hit them.**
|
|
6
|
+
|
|
7
|
+
Claude, Claude Code, Cursor, OpenAI and Gemini each reject different tool names and JSON Schema features.<br>
|
|
8
|
+
mcp-gauntlet connects to your server like a real client does, and tells you what will break, where, and how to fix it.
|
|
9
|
+
|
|
10
|
+
[](https://github.com/velutinous/mcp-gauntlet/actions/workflows/ci.yml)
|
|
11
|
+
[](LICENSE)
|
|
12
|
+

|
|
13
|
+
|
|
14
|
+
<img src="assets/demo.png" alt="mcp-gauntlet output: per-client pass/fail and fixes for each tool" width="900">
|
|
15
|
+
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
## Why
|
|
19
|
+
|
|
20
|
+
An MCP server that works in one client can still fail in another:
|
|
21
|
+
|
|
22
|
+
- `issues.search` works in Cursor and Gemini. **OpenAI and Anthropic reject the dot.**
|
|
23
|
+
- `"type": "array"` with no `items` is valid JSON Schema. **OpenAI rejects the whole request.**
|
|
24
|
+
- `"enum": [1, 2, 3]` is fine everywhere except **Gemini: `enum: only allowed for STRING type`**.
|
|
25
|
+
- A 49-character tool name is fine until it is installed as `acme-tracker`. Then **Claude Code builds `mcp__acme-tracker__…` and goes over its 64-character limit**, and Cursor's 60-character limit too.
|
|
26
|
+
- One tool whose `inputSchema` lacks `"type": "object"` makes the **official TypeScript SDK client reject the entire tool list**. That client sees zero tools, not one broken tool.
|
|
27
|
+
|
|
28
|
+
You only learn this from bug reports, one client at a time. mcp-gauntlet checks all of them in a single run, in CI, on every pull request.
|
|
29
|
+
|
|
30
|
+
## Quick start
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
# stdio server
|
|
34
|
+
npx github:velutinous/mcp-gauntlet -- node dist/index.js
|
|
35
|
+
|
|
36
|
+
# Python server
|
|
37
|
+
npx github:velutinous/mcp-gauntlet -- uv run my-server
|
|
38
|
+
|
|
39
|
+
# remote server (Streamable HTTP; add --sse for legacy SSE)
|
|
40
|
+
npx github:velutinous/mcp-gauntlet --url https://example.com/mcp --header "Authorization: Bearer $TOKEN"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
It exits `0` when no client is broken, `1` when something will fail, and `2` when it cannot connect. The connect error includes the server's stderr, so a missing API key is easy to spot.
|
|
44
|
+
|
|
45
|
+
> Pass `--name <alias>` with the name users put in their client config (for example `github`). Claude Code and Cursor build the full tool name from it, so the length checks depend on it.
|
|
46
|
+
|
|
47
|
+
## What it checks
|
|
48
|
+
|
|
49
|
+
| | Check | Clients |
|
|
50
|
+
|-|-|-|
|
|
51
|
+
| **Protocol** | The official SDK client accepts your `tools/list` response at all | all |
|
|
52
|
+
| **Names** | MCP spec charset and length; uniqueness and case-only collisions | all |
|
|
53
|
+
| | `^[A-Za-z0-9_-]{1,64}$`: no dots, max 64 | Claude, OpenAI |
|
|
54
|
+
| | Starts with a letter or `_`, max 64 | Gemini |
|
|
55
|
+
| | `mcp__<alias>__<tool>` ≤ 64 | Claude Code |
|
|
56
|
+
| | `<alias>` + `<tool>` ≤ 60 | Cursor |
|
|
57
|
+
| **Schemas** | Root is `type: object`; schemas compile (2020-12, or draft-07 when declared) | all |
|
|
58
|
+
| | `required` lists only properties that exist; every property has a type | all |
|
|
59
|
+
| | Arrays declare `items`; no `anyOf`/`oneOf`/`allOf`/`enum`/`not` at the root | OpenAI, Gemini |
|
|
60
|
+
| | Strict-mode readiness: closed objects, every field required | OpenAI (note) |
|
|
61
|
+
| | `enum` only on strings, no empty values; no `$ref`/`oneOf`/`allOf`/`const`/type arrays; no free-form objects | Gemini |
|
|
62
|
+
| | `outputSchema` is an object, and smoke-call `structuredContent` matches it | all |
|
|
63
|
+
| **Docs and cost** | Tools have real descriptions; descriptions stay within budget; params are described | all |
|
|
64
|
+
| | No more than 128 tools; warn above `budget.tools` (default 40), since every enabled server shares the per-request cap | OpenAI (error), all |
|
|
65
|
+
| | `readOnlyHint` / `destructiveHint` annotations, so clients know when to ask the user | all |
|
|
66
|
+
| | Token cost of the whole tool list, with the heaviest tools named | all |
|
|
67
|
+
|
|
68
|
+
Run `mcp-gauntlet rules` for the full list. [docs/rules.md](docs/rules.md) explains each rule, why it exists, and where the limit comes from.
|
|
69
|
+
|
|
70
|
+
## Catch breaking changes with a lockfile
|
|
71
|
+
|
|
72
|
+
Agents, saved prompts and other people's workflows depend on your tool signatures. Snapshot them:
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
npx github:velutinous/mcp-gauntlet lock -- node dist/index.js # writes mcp-gauntlet.lock.json, commit it
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
From then on, every check compares against the lockfile:
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
BREAKING tool:echo Tool removed. Agents and saved workflows that call it will fail.
|
|
82
|
+
BREAKING tool:search_issues#repo New required parameter. Existing callers do not send it.
|
|
83
|
+
BREAKING tool:search_issues#state Enum values removed: "closed".
|
|
84
|
+
change tool:search_issues#page New optional parameter.
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Breaking changes fail the run unless you pass `--allow-breaking`. When the change is intended, re-run `lock`. The lockfile stores description hashes, not full text, so it stays small and diffs cleanly.
|
|
88
|
+
|
|
89
|
+
## Smoke calls
|
|
90
|
+
|
|
91
|
+
Listing tools proves little. Calling them proves more. Add an `mcp-gauntlet.json`:
|
|
92
|
+
|
|
93
|
+
```json
|
|
94
|
+
{
|
|
95
|
+
"server": { "command": "node", "args": ["dist/index.js"] },
|
|
96
|
+
"name": "issues",
|
|
97
|
+
"calls": [
|
|
98
|
+
{ "tool": "search_issues", "args": { "query": "crash" }, "expect": { "contains": "/\\d+ match/", "maxMs": 2000 } },
|
|
99
|
+
{ "tool": "search_issues", "args": { "query": "" }, "expect": { "error": true } }
|
|
100
|
+
]
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Each call must:
|
|
105
|
+
|
|
106
|
+
- succeed (or return `isError: true` when you expect it to);
|
|
107
|
+
- contain the expected text (a plain substring, or `/regex/flags`);
|
|
108
|
+
- finish within `maxMs`;
|
|
109
|
+
- return `structuredContent` that matches the tool's `outputSchema`.
|
|
110
|
+
|
|
111
|
+
mcp-gauntlet also checks your `args` against the tool's own `inputSchema`, so the example cannot drift from the schema either. A tool that throws a JSON-RPC error instead of returning `isError` is reported too, because clients show the model nothing useful when that happens.
|
|
112
|
+
|
|
113
|
+
## GitHub Action
|
|
114
|
+
|
|
115
|
+
```yaml
|
|
116
|
+
# .github/workflows/mcp.yml
|
|
117
|
+
name: MCP
|
|
118
|
+
on: [push, pull_request]
|
|
119
|
+
jobs:
|
|
120
|
+
gauntlet:
|
|
121
|
+
runs-on: ubuntu-latest
|
|
122
|
+
steps:
|
|
123
|
+
- uses: actions/checkout@v4
|
|
124
|
+
- uses: actions/setup-node@v4
|
|
125
|
+
with: { node-version: 22 }
|
|
126
|
+
- run: npm ci && npm run build
|
|
127
|
+
- uses: velutinous/mcp-gauntlet@v0
|
|
128
|
+
with:
|
|
129
|
+
command: node dist/index.js
|
|
130
|
+
name: issues
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Findings appear as annotations on the run, and a per-client pass/fail table is written to the job summary. To post the table as a PR comment, run the CLI with `--format markdown`.
|
|
134
|
+
|
|
135
|
+
| Input | Default | |
|
|
136
|
+
|-|-|-|
|
|
137
|
+
| `command` | | Command that starts a stdio server |
|
|
138
|
+
| `url` | | Streamable HTTP URL (start the server in an earlier step) |
|
|
139
|
+
| `name` | server name | Alias users register the server under |
|
|
140
|
+
| `clients` | all | `mcp,claude,claude-code,cursor,openai,gemini` |
|
|
141
|
+
| `fail-on` | `error` | `error`, `warning` or `info` |
|
|
142
|
+
| `allow-breaking` | `false` | Report lockfile breaks without failing |
|
|
143
|
+
| `args` | | Extra CLI flags |
|
|
144
|
+
| `working-directory` | `.` | Where `mcp-gauntlet.json` and the lockfile live |
|
|
145
|
+
|
|
146
|
+
## CLI reference
|
|
147
|
+
|
|
148
|
+
```
|
|
149
|
+
mcp-gauntlet [check] [options] -- <command> [args...]
|
|
150
|
+
mcp-gauntlet [check] [options] --url <url>
|
|
151
|
+
mcp-gauntlet lock [options] -- <command> [args...]
|
|
152
|
+
mcp-gauntlet inspect [options] -- <command> [args...] tools sorted by token cost (--json to dump)
|
|
153
|
+
mcp-gauntlet rules
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
| Option | |
|
|
157
|
+
|-|-|
|
|
158
|
+
| `--name <alias>` | Name users register the server under |
|
|
159
|
+
| `--clients <list>` | Only check these clients |
|
|
160
|
+
| `--format <fmt>` | `text`, `json`, `markdown`, `github` |
|
|
161
|
+
| `--fail-on <sev>` | Lowest severity that fails the run (default `error`) |
|
|
162
|
+
| `--lock <file>` / `--allow-breaking` | Lockfile path / do not fail on breaks |
|
|
163
|
+
| `--disable <rules>` | Comma list of rule ids to skip |
|
|
164
|
+
| `--surface <file>` | Check saved JSON with no server running: an `inspect --json` dump, a `tools/list` result (`{"tools": [...]}`, bare or in its JSON-RPC response) or a plain array of tools |
|
|
165
|
+
| `--header "K: V"` / `--env K=V` | Repeatable, for HTTP and stdio servers |
|
|
166
|
+
| `-V, --verbose` | Show info-level notes too |
|
|
167
|
+
|
|
168
|
+
Every option can also go in `mcp-gauntlet.json`. See [examples/mcp-gauntlet.json](examples/mcp-gauntlet.json).
|
|
169
|
+
|
|
170
|
+
## Use it as a library
|
|
171
|
+
|
|
172
|
+
```ts
|
|
173
|
+
import { connect, runRules, toLockfile, diffLock } from "mcp-gauntlet";
|
|
174
|
+
|
|
175
|
+
const session = await connect({ kind: "stdio", command: "node", args: ["dist/index.js"] });
|
|
176
|
+
const findings = runRules({
|
|
177
|
+
surface: session.surface,
|
|
178
|
+
serverAlias: "issues",
|
|
179
|
+
clients: new Set(["claude-code", "openai"]),
|
|
180
|
+
budget: { totalTokens: 8000, descriptionChars: 1024 },
|
|
181
|
+
});
|
|
182
|
+
await session.close();
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Try it on the demo server
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
git clone https://github.com/velutinous/mcp-gauntlet && cd mcp-gauntlet && npm install
|
|
189
|
+
node dist/cli.js --name acme-tracker -- node examples/demo-server.mjs
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## How accurate are the client rules?
|
|
193
|
+
|
|
194
|
+
Each rule cites its source in [docs/rules.md](docs/rules.md): an API error message, official documentation, or a client maintainer's statement. Client limits change. If one has moved, please [open an issue](https://github.com/velutinous/mcp-gauntlet/issues/new?template=client-rule.yml) with the error you saw, and the rule will be updated in a patch release. Rules that only matter for opt-in features, such as OpenAI strict mode, are notes, not errors.
|
|
195
|
+
|
|
196
|
+
## Contributing
|
|
197
|
+
|
|
198
|
+
Bug reports with a failing server are the most valuable thing you can send. See [CONTRIBUTING.md](CONTRIBUTING.md). `npm test` runs every rule against real stdio servers in `test/fixtures`.
|
|
199
|
+
|
|
200
|
+
## License
|
|
201
|
+
|
|
202
|
+
MIT
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
import { readFileSync, writeFileSync, existsSync, appendFileSync } from "node:fs";
|
|
4
|
+
import { resolve } from "node:path";
|
|
5
|
+
import { createRequire } from "node:module";
|
|
6
|
+
import { ALL_CLIENTS } from "./types.js";
|
|
7
|
+
import { RULES, runRules } from "./rules/index.js";
|
|
8
|
+
import { connect } from "./connect.js";
|
|
9
|
+
import { diffLock, toLockfile } from "./lock.js";
|
|
10
|
+
import { runSmoke } from "./smoke.js";
|
|
11
|
+
import { loadConfig } from "./config.js";
|
|
12
|
+
import { buildReport, formatGithub, formatMarkdown, formatText } from "./report.js";
|
|
13
|
+
import { estimateTokens } from "./tokens.js";
|
|
14
|
+
import { parseSurface } from "./surface.js";
|
|
15
|
+
import { DEFAULT_TOOL_BUDGET } from "./rules/docs.js";
|
|
16
|
+
const VERSION = createRequire(import.meta.url)("../package.json").version;
|
|
17
|
+
const DEFAULT_LOCK = "mcp-gauntlet.lock.json";
|
|
18
|
+
const HELP = `mcp-gauntlet ${VERSION}: put an MCP server through every client's rules before your users do.
|
|
19
|
+
|
|
20
|
+
Usage
|
|
21
|
+
mcp-gauntlet [check] [options] -- <command> [args...] test a stdio server
|
|
22
|
+
mcp-gauntlet [check] [options] --url <url> test a Streamable HTTP / SSE server
|
|
23
|
+
mcp-gauntlet lock [options] -- <command> [args...] write ${DEFAULT_LOCK}
|
|
24
|
+
mcp-gauntlet inspect [options] -- <command> [args...] list tools with token costs (--json to dump)
|
|
25
|
+
mcp-gauntlet rules list every rule
|
|
26
|
+
|
|
27
|
+
Options
|
|
28
|
+
--name <alias> name users register the server under (default: from server info)
|
|
29
|
+
--clients <list> comma list of ${ALL_CLIENTS.join(", ")} (default: all)
|
|
30
|
+
--format <fmt> text | json | markdown | github (default: text; adds github annotations in Actions)
|
|
31
|
+
--fail-on <sev> error | warning | info (default: error)
|
|
32
|
+
--lock <file> lockfile to compare against (default: ${DEFAULT_LOCK} if present)
|
|
33
|
+
--allow-breaking do not fail on breaking changes against the lockfile
|
|
34
|
+
--disable <rules> comma list of rule ids to skip
|
|
35
|
+
--surface <file> check saved JSON instead of a live server: an inspect --json
|
|
36
|
+
dump, a tools/list result, or an array of tools
|
|
37
|
+
--header <k: v> HTTP header, repeatable (e.g. "Authorization: Bearer $TOKEN")
|
|
38
|
+
--env <K=V> environment variable for the stdio server, repeatable
|
|
39
|
+
--sse use the legacy SSE transport for --url
|
|
40
|
+
--config <file> config file (default: mcp-gauntlet.json)
|
|
41
|
+
--timeout <ms> per-request timeout (default: 20000)
|
|
42
|
+
-V, --verbose also show info-level notes in text output
|
|
43
|
+
-v, --version print version
|
|
44
|
+
-h, --help print this help
|
|
45
|
+
`;
|
|
46
|
+
function fail(msg) {
|
|
47
|
+
process.stderr.write(`mcp-gauntlet: ${msg}\n`);
|
|
48
|
+
process.exit(2);
|
|
49
|
+
}
|
|
50
|
+
function slug(s) {
|
|
51
|
+
return s.toLowerCase().replace(/^@[^/]+\//, "").replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "") || "server";
|
|
52
|
+
}
|
|
53
|
+
async function main(argv) {
|
|
54
|
+
const dash = argv.indexOf("--");
|
|
55
|
+
const own = dash === -1 ? argv : argv.slice(0, dash);
|
|
56
|
+
const command = dash === -1 ? [] : argv.slice(dash + 1);
|
|
57
|
+
const { values, positionals } = parseArgs({
|
|
58
|
+
args: own,
|
|
59
|
+
allowPositionals: true,
|
|
60
|
+
options: {
|
|
61
|
+
name: { type: "string" },
|
|
62
|
+
clients: { type: "string" },
|
|
63
|
+
format: { type: "string" },
|
|
64
|
+
"fail-on": { type: "string" },
|
|
65
|
+
lock: { type: "string" },
|
|
66
|
+
"allow-breaking": { type: "boolean" },
|
|
67
|
+
disable: { type: "string" },
|
|
68
|
+
surface: { type: "string" },
|
|
69
|
+
url: { type: "string" },
|
|
70
|
+
header: { type: "string", multiple: true },
|
|
71
|
+
env: { type: "string", multiple: true },
|
|
72
|
+
sse: { type: "boolean" },
|
|
73
|
+
config: { type: "string" },
|
|
74
|
+
timeout: { type: "string" },
|
|
75
|
+
json: { type: "boolean" },
|
|
76
|
+
verbose: { type: "boolean", short: "V" },
|
|
77
|
+
version: { type: "boolean", short: "v" },
|
|
78
|
+
help: { type: "boolean", short: "h" },
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
if (values.version)
|
|
82
|
+
return void process.stdout.write(VERSION + "\n"), 0;
|
|
83
|
+
const sub = positionals[0] ?? "check";
|
|
84
|
+
if (values.help || !["check", "lock", "inspect", "rules"].includes(sub))
|
|
85
|
+
return void process.stdout.write(HELP), values.help ? 0 : 2;
|
|
86
|
+
if (sub === "rules") {
|
|
87
|
+
for (const r of RULES)
|
|
88
|
+
process.stdout.write(`${r.id.padEnd(26)} ${r.summary}\n`);
|
|
89
|
+
process.stdout.write(`${"protocol/tools-list".padEnd(26)} The official SDK client accepts the tools/list response\n`);
|
|
90
|
+
process.stdout.write(`${"smoke/call".padEnd(26)} Configured smoke calls succeed and match outputSchema\n`);
|
|
91
|
+
return 0;
|
|
92
|
+
}
|
|
93
|
+
const { config } = loadConfig(values.config);
|
|
94
|
+
const timeout = Number(values.timeout ?? 20_000);
|
|
95
|
+
const clients = (values.clients ?? config.clients?.join(",") ?? ALL_CLIENTS.join(",")).split(",").map((s) => s.trim());
|
|
96
|
+
for (const c of clients)
|
|
97
|
+
if (!ALL_CLIENTS.includes(c))
|
|
98
|
+
fail(`unknown client "${c}" (expected ${ALL_CLIENTS.join(", ")})`);
|
|
99
|
+
const failOn = (values["fail-on"] ?? config.failOn ?? "error");
|
|
100
|
+
if (!["error", "warning", "info"].includes(failOn))
|
|
101
|
+
fail(`--fail-on must be error, warning or info`);
|
|
102
|
+
// Resolve what to talk to.
|
|
103
|
+
let session = null;
|
|
104
|
+
let surface;
|
|
105
|
+
if (values.surface) {
|
|
106
|
+
const file = resolve(values.surface);
|
|
107
|
+
let data;
|
|
108
|
+
try {
|
|
109
|
+
data = JSON.parse(readFileSync(file, "utf8"));
|
|
110
|
+
}
|
|
111
|
+
catch (e) {
|
|
112
|
+
fail(`cannot read --surface ${values.surface}: ${e.message}`);
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
surface = parseSurface(data, file);
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
fail(`--surface ${values.surface}: ${e.message}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
const target = resolveTarget(values, command, config);
|
|
123
|
+
try {
|
|
124
|
+
session = await connect(target, timeout);
|
|
125
|
+
}
|
|
126
|
+
catch (e) {
|
|
127
|
+
fail(e.message);
|
|
128
|
+
}
|
|
129
|
+
surface = session.surface;
|
|
130
|
+
}
|
|
131
|
+
try {
|
|
132
|
+
if (sub === "inspect") {
|
|
133
|
+
if (values.json)
|
|
134
|
+
process.stdout.write(JSON.stringify(surface, null, 2) + "\n");
|
|
135
|
+
else {
|
|
136
|
+
const rows = surface.tools.map((t) => ({ t, n: estimateTokens(t) })).sort((a, b) => b.n - a.n);
|
|
137
|
+
const w = Math.max(4, ...rows.map((r) => r.t.name.length));
|
|
138
|
+
process.stdout.write(`${surface.server.name} ${surface.server.version}\n\n`);
|
|
139
|
+
for (const { t, n } of rows)
|
|
140
|
+
process.stdout.write(`${t.name.padEnd(w)} ~${String(n).padStart(5)} tok ${(t.description ?? "").split("\n")[0].slice(0, 70)}\n`);
|
|
141
|
+
process.stdout.write(`\n${rows.length} tools, ~${rows.reduce((s, r) => s + r.n, 0)} tokens\n`);
|
|
142
|
+
}
|
|
143
|
+
return 0;
|
|
144
|
+
}
|
|
145
|
+
const lockPath = resolve(values.lock ?? config.lockfile ?? DEFAULT_LOCK);
|
|
146
|
+
if (sub === "lock") {
|
|
147
|
+
writeFileSync(lockPath, JSON.stringify(toLockfile(surface), null, 2) + "\n");
|
|
148
|
+
process.stdout.write(`Wrote ${lockPath} (${surface.tools.length} tools)\n`);
|
|
149
|
+
return 0;
|
|
150
|
+
}
|
|
151
|
+
const serverAlias = values.name ?? config.name ?? slug(surface.server.name);
|
|
152
|
+
const disabled = new Set([...(config.disable ?? []), ...(values.disable?.split(",").map((s) => s.trim()) ?? [])]);
|
|
153
|
+
const findings = runRules({
|
|
154
|
+
surface,
|
|
155
|
+
serverAlias,
|
|
156
|
+
clients: new Set(clients),
|
|
157
|
+
budget: { totalTokens: config.budget?.totalTokens ?? 8000, descriptionChars: config.budget?.descriptionChars ?? 1024, tools: config.budget?.tools ?? DEFAULT_TOOL_BUDGET },
|
|
158
|
+
}, disabled);
|
|
159
|
+
if (session)
|
|
160
|
+
findings.unshift(...session.findings.filter((f) => !disabled.has(f.rule)));
|
|
161
|
+
let changes = null;
|
|
162
|
+
if (existsSync(lockPath))
|
|
163
|
+
changes = diffLock(JSON.parse(readFileSync(lockPath, "utf8")), toLockfile(surface));
|
|
164
|
+
else if (values.lock)
|
|
165
|
+
fail(`lockfile not found: ${lockPath}`);
|
|
166
|
+
let smoke = [];
|
|
167
|
+
if (session && config.calls?.length) {
|
|
168
|
+
const res = await runSmoke(session.client, surface, config.calls, timeout);
|
|
169
|
+
smoke = res.results;
|
|
170
|
+
findings.push(...res.findings);
|
|
171
|
+
}
|
|
172
|
+
const report = buildReport(surface, clients, findings, changes, smoke);
|
|
173
|
+
const inActions = process.env.GITHUB_ACTIONS === "true";
|
|
174
|
+
const format = values.format ?? "text";
|
|
175
|
+
if (format === "json")
|
|
176
|
+
process.stdout.write(JSON.stringify(report, null, 2) + "\n");
|
|
177
|
+
else if (format === "markdown")
|
|
178
|
+
process.stdout.write(formatMarkdown(report));
|
|
179
|
+
else if (format === "github")
|
|
180
|
+
process.stdout.write(formatGithub(report) + "\n");
|
|
181
|
+
else if (format === "text") {
|
|
182
|
+
process.stdout.write(formatText(report, (process.stdout.isTTY === true || !!process.env.FORCE_COLOR) && !process.env.NO_COLOR, values.verbose) + "\n");
|
|
183
|
+
if (inActions)
|
|
184
|
+
process.stdout.write(formatGithub(report) + "\n");
|
|
185
|
+
}
|
|
186
|
+
else
|
|
187
|
+
fail(`unknown format "${format}"`);
|
|
188
|
+
if (inActions && process.env.GITHUB_STEP_SUMMARY)
|
|
189
|
+
appendFileSync(process.env.GITHUB_STEP_SUMMARY, formatMarkdown(report));
|
|
190
|
+
const rank = { error: 0, warning: 1, info: 2 };
|
|
191
|
+
const failing = findings.some((f) => rank[f.severity] <= rank[failOn]);
|
|
192
|
+
const breaking = !values["allow-breaking"] && (changes ?? []).some((c) => c.kind === "breaking");
|
|
193
|
+
return failing || breaking ? 1 : 0;
|
|
194
|
+
}
|
|
195
|
+
finally {
|
|
196
|
+
await session?.close().catch(() => { });
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
function resolveTarget(values, command, config) {
|
|
200
|
+
const headers = Object.fromEntries((values.header ?? []).map((h) => {
|
|
201
|
+
const i = h.indexOf(":");
|
|
202
|
+
if (i < 1)
|
|
203
|
+
fail(`bad --header "${h}" (expected "Name: value")`);
|
|
204
|
+
return [h.slice(0, i).trim(), h.slice(i + 1).trim()];
|
|
205
|
+
}));
|
|
206
|
+
const env = Object.fromEntries((values.env ?? []).map((e) => {
|
|
207
|
+
const i = e.indexOf("=");
|
|
208
|
+
if (i < 1)
|
|
209
|
+
fail(`bad --env "${e}" (expected KEY=value)`);
|
|
210
|
+
return [e.slice(0, i), e.slice(i + 1)];
|
|
211
|
+
}));
|
|
212
|
+
if (values.url)
|
|
213
|
+
return { kind: "http", url: values.url, headers, transport: values.sse ? "sse" : "streamable" };
|
|
214
|
+
if (command.length)
|
|
215
|
+
return { kind: "stdio", command: command[0], args: command.slice(1), env };
|
|
216
|
+
const s = config.server;
|
|
217
|
+
if (s && "url" in s)
|
|
218
|
+
return { kind: "http", url: s.url, headers: { ...(s.headers ?? {}), ...headers }, transport: values.sse ? "sse" : s.transport };
|
|
219
|
+
if (s && "command" in s)
|
|
220
|
+
return { kind: "stdio", command: s.command, args: s.args ?? [], env: { ...(s.env ?? {}), ...env }, cwd: s.cwd };
|
|
221
|
+
fail("no server given. Pass -- <command>, --url <url>, --surface <file>, or set \"server\" in mcp-gauntlet.json. See --help.");
|
|
222
|
+
}
|
|
223
|
+
main(process.argv.slice(2)).then((code) => process.exit(code), (e) => fail(e.stack ?? String(e)));
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ClientId, Severity } from "./types.js";
|
|
2
|
+
export interface SmokeCall {
|
|
3
|
+
tool: string;
|
|
4
|
+
args?: Record<string, unknown>;
|
|
5
|
+
expect?: {
|
|
6
|
+
/** Expect the tool to report isError (default false). */
|
|
7
|
+
error?: boolean;
|
|
8
|
+
/** Substring (or /regex/) that the text content must contain. */
|
|
9
|
+
contains?: string;
|
|
10
|
+
/** Fail if the call takes longer than this. */
|
|
11
|
+
maxMs?: number;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export interface Config {
|
|
15
|
+
server?: {
|
|
16
|
+
command: string;
|
|
17
|
+
args?: string[];
|
|
18
|
+
env?: Record<string, string>;
|
|
19
|
+
cwd?: string;
|
|
20
|
+
} | {
|
|
21
|
+
url: string;
|
|
22
|
+
headers?: Record<string, string>;
|
|
23
|
+
transport?: "streamable" | "sse";
|
|
24
|
+
};
|
|
25
|
+
/** Name users register the server under (e.g. "github"). Drives prefixed-name limits. */
|
|
26
|
+
name?: string;
|
|
27
|
+
clients?: ClientId[];
|
|
28
|
+
disable?: string[];
|
|
29
|
+
failOn?: Severity;
|
|
30
|
+
budget?: {
|
|
31
|
+
totalTokens?: number;
|
|
32
|
+
descriptionChars?: number;
|
|
33
|
+
tools?: number;
|
|
34
|
+
};
|
|
35
|
+
lockfile?: string;
|
|
36
|
+
calls?: SmokeCall[];
|
|
37
|
+
}
|
|
38
|
+
export declare const CONFIG_FILES: string[];
|
|
39
|
+
export declare function loadConfig(explicit?: string, cwd?: string): {
|
|
40
|
+
config: Config;
|
|
41
|
+
path?: string;
|
|
42
|
+
};
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { readFileSync, existsSync } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
export const CONFIG_FILES = ["mcp-gauntlet.json", ".mcp-gauntlet.json"];
|
|
4
|
+
export function loadConfig(explicit, cwd = process.cwd()) {
|
|
5
|
+
const candidates = explicit ? [explicit] : CONFIG_FILES;
|
|
6
|
+
for (const c of candidates) {
|
|
7
|
+
const p = resolve(cwd, c);
|
|
8
|
+
if (existsSync(p)) {
|
|
9
|
+
try {
|
|
10
|
+
return { config: JSON.parse(readFileSync(p, "utf8")), path: p };
|
|
11
|
+
}
|
|
12
|
+
catch (e) {
|
|
13
|
+
throw new Error(`Could not parse ${p}: ${e.message}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
if (explicit)
|
|
17
|
+
throw new Error(`Config file not found: ${p}`);
|
|
18
|
+
}
|
|
19
|
+
return { config: {} };
|
|
20
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
|
+
import type { Finding, Surface } from "./types.js";
|
|
3
|
+
export type Target = {
|
|
4
|
+
kind: "stdio";
|
|
5
|
+
command: string;
|
|
6
|
+
args: string[];
|
|
7
|
+
env?: Record<string, string>;
|
|
8
|
+
cwd?: string;
|
|
9
|
+
} | {
|
|
10
|
+
kind: "http";
|
|
11
|
+
url: string;
|
|
12
|
+
headers?: Record<string, string>;
|
|
13
|
+
transport?: "streamable" | "sse";
|
|
14
|
+
};
|
|
15
|
+
export interface Session {
|
|
16
|
+
client: Client;
|
|
17
|
+
surface: Surface;
|
|
18
|
+
/** Protocol-level problems seen while capturing the surface. */
|
|
19
|
+
findings: Finding[];
|
|
20
|
+
stderr: string[];
|
|
21
|
+
close(): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
/** Connect, capture every tool, prompt and resource (following pagination), and keep the session open for smoke calls. */
|
|
24
|
+
export declare function connect(target: Target, timeoutMs?: number): Promise<Session>;
|
package/dist/connect.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
|
+
import { StdioClientTransport, getDefaultEnvironment } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
3
|
+
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
4
|
+
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
const LenientPage = z.looseObject({ nextCursor: z.string().optional() });
|
|
7
|
+
function makeTransport(target, stderr) {
|
|
8
|
+
if (target.kind === "stdio") {
|
|
9
|
+
const t = new StdioClientTransport({
|
|
10
|
+
command: target.command,
|
|
11
|
+
args: target.args,
|
|
12
|
+
env: { ...getDefaultEnvironment(), ...(target.env ?? {}) },
|
|
13
|
+
cwd: target.cwd,
|
|
14
|
+
stderr: "pipe",
|
|
15
|
+
});
|
|
16
|
+
t.stderr?.on("data", (chunk) => {
|
|
17
|
+
stderr.push(chunk.toString());
|
|
18
|
+
if (stderr.length > 200)
|
|
19
|
+
stderr.shift();
|
|
20
|
+
});
|
|
21
|
+
return t;
|
|
22
|
+
}
|
|
23
|
+
const url = new URL(target.url);
|
|
24
|
+
const requestInit = { headers: target.headers ?? {} };
|
|
25
|
+
return target.transport === "sse" ? new SSEClientTransport(url, { requestInit }) : new StreamableHTTPClientTransport(url, { requestInit });
|
|
26
|
+
}
|
|
27
|
+
async function withTimeout(p, ms, what) {
|
|
28
|
+
let timer;
|
|
29
|
+
const timeout = new Promise((_, rej) => {
|
|
30
|
+
timer = setTimeout(() => rej(new Error(`${what} timed out after ${ms} ms`)), ms);
|
|
31
|
+
});
|
|
32
|
+
try {
|
|
33
|
+
return await Promise.race([p, timeout]);
|
|
34
|
+
}
|
|
35
|
+
finally {
|
|
36
|
+
clearTimeout(timer);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** Connect, capture every tool, prompt and resource (following pagination), and keep the session open for smoke calls. */
|
|
40
|
+
export async function connect(target, timeoutMs = 20_000) {
|
|
41
|
+
const stderr = [];
|
|
42
|
+
const client = new Client({ name: "mcp-gauntlet", version: "0.1.0" }, { capabilities: {} });
|
|
43
|
+
const transport = makeTransport(target, stderr);
|
|
44
|
+
try {
|
|
45
|
+
await withTimeout(client.connect(transport), timeoutMs, "initialize");
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
const tail = stderr.join("").trim().split("\n").slice(-8).join("\n");
|
|
49
|
+
throw new Error(`Could not connect: ${e.message}${tail ? `\n--- server stderr ---\n${tail}` : ""}`);
|
|
50
|
+
}
|
|
51
|
+
const caps = client.getServerCapabilities() ?? {};
|
|
52
|
+
const info = client.getServerVersion() ?? { name: "unknown", version: "0.0.0" };
|
|
53
|
+
const tools = [];
|
|
54
|
+
const prompts = [];
|
|
55
|
+
const resources = [];
|
|
56
|
+
const opts = { timeout: timeoutMs };
|
|
57
|
+
const findings = [];
|
|
58
|
+
if (caps.tools) {
|
|
59
|
+
// First as the official SDK client would; if it refuses, say so and re-read leniently so every other rule still runs.
|
|
60
|
+
try {
|
|
61
|
+
let cursor;
|
|
62
|
+
do {
|
|
63
|
+
const page = await client.listTools(cursor ? { cursor } : {}, opts);
|
|
64
|
+
tools.push(...page.tools);
|
|
65
|
+
cursor = page.nextCursor;
|
|
66
|
+
} while (cursor);
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
findings.push({
|
|
70
|
+
rule: "protocol/tools-list",
|
|
71
|
+
severity: "error",
|
|
72
|
+
clients: ["mcp"],
|
|
73
|
+
target: "server",
|
|
74
|
+
message: `The official MCP TypeScript SDK client rejects this server's tools/list response, so clients built on it see no tools at all: ${summarize(e)}`,
|
|
75
|
+
});
|
|
76
|
+
tools.length = 0;
|
|
77
|
+
let cursor;
|
|
78
|
+
do {
|
|
79
|
+
const page = (await client.request({ method: "tools/list", params: cursor ? { cursor } : {} }, LenientPage, opts));
|
|
80
|
+
tools.push(...(page.tools ?? []));
|
|
81
|
+
cursor = page.nextCursor;
|
|
82
|
+
} while (cursor);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (caps.prompts) {
|
|
86
|
+
let cursor;
|
|
87
|
+
do {
|
|
88
|
+
const page = await client.listPrompts(cursor ? { cursor } : {}, opts);
|
|
89
|
+
prompts.push(...page.prompts);
|
|
90
|
+
cursor = page.nextCursor;
|
|
91
|
+
} while (cursor);
|
|
92
|
+
}
|
|
93
|
+
if (caps.resources) {
|
|
94
|
+
let cursor;
|
|
95
|
+
do {
|
|
96
|
+
const page = await client.listResources(cursor ? { cursor } : {}, opts);
|
|
97
|
+
resources.push(...page.resources);
|
|
98
|
+
cursor = page.nextCursor;
|
|
99
|
+
} while (cursor);
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
client,
|
|
103
|
+
stderr,
|
|
104
|
+
findings,
|
|
105
|
+
surface: {
|
|
106
|
+
server: { name: info.name, version: info.version },
|
|
107
|
+
protocolVersion: transport.protocolVersion,
|
|
108
|
+
tools,
|
|
109
|
+
prompts,
|
|
110
|
+
resources,
|
|
111
|
+
},
|
|
112
|
+
close: () => client.close(),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function summarize(e) {
|
|
116
|
+
const msg = e.message ?? String(e);
|
|
117
|
+
try {
|
|
118
|
+
const issues = JSON.parse(msg);
|
|
119
|
+
if (Array.isArray(issues))
|
|
120
|
+
return issues.slice(0, 3).map((i) => `${(i.path ?? []).join(".")}: ${i.message}`).join("; ");
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
/* not a zod issue list */
|
|
124
|
+
}
|
|
125
|
+
return msg.split("\n")[0];
|
|
126
|
+
}
|