@drak-marketing/mcp-test-harness 0.2.2
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 +101 -0
- package/dist/helpers/pack.d.ts +9 -0
- package/dist/helpers/pack.js +57 -0
- package/dist/helpers/source-lint.d.ts +17 -0
- package/dist/helpers/source-lint.js +61 -0
- package/dist/helpers/spawn.d.ts +21 -0
- package/dist/helpers/spawn.js +44 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +65 -0
- package/dist/suites/backward-compat.d.ts +3 -0
- package/dist/suites/backward-compat.js +28 -0
- package/dist/suites/build-integrity.d.ts +3 -0
- package/dist/suites/build-integrity.js +43 -0
- package/dist/suites/concurrent.d.ts +3 -0
- package/dist/suites/concurrent.js +25 -0
- package/dist/suites/config-loading.d.ts +3 -0
- package/dist/suites/config-loading.js +38 -0
- package/dist/suites/credential-security.d.ts +3 -0
- package/dist/suites/credential-security.js +36 -0
- package/dist/suites/cross-platform.d.ts +3 -0
- package/dist/suites/cross-platform.js +43 -0
- package/dist/suites/dependency-health.d.ts +3 -0
- package/dist/suites/dependency-health.js +45 -0
- package/dist/suites/ender-strategic.d.ts +3 -0
- package/dist/suites/ender-strategic.js +155 -0
- package/dist/suites/graceful-shutdown.d.ts +3 -0
- package/dist/suites/graceful-shutdown.js +31 -0
- package/dist/suites/input-sanitization.d.ts +3 -0
- package/dist/suites/input-sanitization.js +26 -0
- package/dist/suites/mayhem-adversarial.d.ts +3 -0
- package/dist/suites/mayhem-adversarial.js +175 -0
- package/dist/suites/mcp-protocol.d.ts +3 -0
- package/dist/suites/mcp-protocol.js +65 -0
- package/dist/suites/network-failures.d.ts +3 -0
- package/dist/suites/network-failures.js +49 -0
- package/dist/suites/observability.d.ts +3 -0
- package/dist/suites/observability.js +33 -0
- package/dist/suites/output-contract.d.ts +3 -0
- package/dist/suites/output-contract.js +34 -0
- package/dist/suites/ralph-chaos.d.ts +3 -0
- package/dist/suites/ralph-chaos.js +122 -0
- package/dist/suites/smoke.d.ts +3 -0
- package/dist/suites/smoke.js +50 -0
- package/dist/suites/stdout-purity.d.ts +3 -0
- package/dist/suites/stdout-purity.js +128 -0
- package/dist/suites/token-lifecycle.d.ts +3 -0
- package/dist/suites/token-lifecycle.js +64 -0
- package/dist/suites/version-publish.d.ts +3 -0
- package/dist/suites/version-publish.js +34 -0
- package/dist/types.d.ts +50 -0
- package/dist/types.js +15 -0
- package/package.json +50 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { readFileSync, existsSync } from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
export function observabilitySuite(config) {
|
|
5
|
+
describe("observability", () => {
|
|
6
|
+
it("server entry logs version at startup", () => {
|
|
7
|
+
const entryPath = path.join(config.repoRoot, config.serverEntry ?? "dist/index.js");
|
|
8
|
+
if (!existsSync(entryPath))
|
|
9
|
+
return;
|
|
10
|
+
const src = readFileSync(entryPath, "utf-8");
|
|
11
|
+
// Check for version reference in startup code
|
|
12
|
+
const hasVersion = src.includes("version") || src.includes("pkg.version") || src.includes("__cliPkg");
|
|
13
|
+
expect(hasVersion, "server should log version at startup").toBe(true);
|
|
14
|
+
});
|
|
15
|
+
it("no access tokens or secrets in log statements", () => {
|
|
16
|
+
const entryPath = path.join(config.repoRoot, config.serverEntry ?? "dist/index.js");
|
|
17
|
+
if (!existsSync(entryPath))
|
|
18
|
+
return;
|
|
19
|
+
const src = readFileSync(entryPath, "utf-8");
|
|
20
|
+
// Check that logger/console.error calls don't interpolate token vars
|
|
21
|
+
const lines = src.split("\n");
|
|
22
|
+
const violations = [];
|
|
23
|
+
for (let i = 0; i < lines.length; i++) {
|
|
24
|
+
const line = lines[i];
|
|
25
|
+
if ((line.includes("console.error") || line.includes("logger.")) &&
|
|
26
|
+
/\$\{.*(?:access_?token|refresh_?token|client_?secret|api_?key)\b/.test(line)) {
|
|
27
|
+
violations.push(`line ${i + 1}: ${line.trim()}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
expect(violations, "log statements may leak secrets").toEqual([]);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { readFileSync, existsSync } from "fs";
|
|
4
|
+
export function outputContractSuite(config) {
|
|
5
|
+
describe("output contract", () => {
|
|
6
|
+
it("error responses use isError: true", () => {
|
|
7
|
+
const entryPath = path.join(config.repoRoot, config.serverEntry ?? "dist/index.js");
|
|
8
|
+
if (!existsSync(entryPath))
|
|
9
|
+
return;
|
|
10
|
+
const src = readFileSync(entryPath, "utf-8");
|
|
11
|
+
// Check that catch blocks include isError in the response
|
|
12
|
+
if (src.includes("catch") && src.includes("content")) {
|
|
13
|
+
const hasIsError = src.includes("isError");
|
|
14
|
+
expect(hasIsError, "error responses should include isError: true").toBe(true);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
it("safeResponse or size limiting exists for large payloads", () => {
|
|
18
|
+
const entryPath = path.join(config.repoRoot, config.serverEntry ?? "dist/index.js");
|
|
19
|
+
if (!existsSync(entryPath))
|
|
20
|
+
return;
|
|
21
|
+
const src = readFileSync(entryPath, "utf-8");
|
|
22
|
+
// Check for any response size limiting
|
|
23
|
+
const hasSizeLimit = src.includes("safeResponse") ||
|
|
24
|
+
src.includes("truncat") ||
|
|
25
|
+
src.includes("MAX_RESPONSE") ||
|
|
26
|
+
src.includes("size") ||
|
|
27
|
+
src.includes("slice");
|
|
28
|
+
// This is advisory, not a hard requirement
|
|
29
|
+
if (!hasSizeLimit) {
|
|
30
|
+
process.stderr.write("[info] no response size limiting detected -- large responses may exceed transport limits\n");
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ralph Wiggum chaos tests.
|
|
3
|
+
* "I'm in danger!" -- tests for the dumbest possible user behavior.
|
|
4
|
+
*/
|
|
5
|
+
import { describe, it, expect } from "vitest";
|
|
6
|
+
import { spawnSync } from "child_process";
|
|
7
|
+
import path from "path";
|
|
8
|
+
import { existsSync, readFileSync } from "fs";
|
|
9
|
+
import { spawnCli } from "../helpers/spawn.js";
|
|
10
|
+
export function ralphChaosSuite(config) {
|
|
11
|
+
describe("ralph: chaos (dumbest possible user)", () => {
|
|
12
|
+
const entry = config.serverEntry ?? "dist/index.js";
|
|
13
|
+
// ----------------------------------------------------------------
|
|
14
|
+
// First-run experience: zero config, zero env vars, fresh machine
|
|
15
|
+
// ----------------------------------------------------------------
|
|
16
|
+
it("first-run with zero config gives helpful message, not stack trace", () => {
|
|
17
|
+
// Strip ALL env vars except PATH/HOME
|
|
18
|
+
const bareEnv = {
|
|
19
|
+
PATH: process.env.PATH ?? "/usr/bin:/bin",
|
|
20
|
+
HOME: "/tmp/fake-home-ralph",
|
|
21
|
+
USER: "ralph",
|
|
22
|
+
NODE_PATH: "",
|
|
23
|
+
};
|
|
24
|
+
const result = spawnCli(config, entry, [], bareEnv, 10_000);
|
|
25
|
+
const output = result.stdout + result.stderr;
|
|
26
|
+
if (result.exitCode !== 0) {
|
|
27
|
+
// Should NOT be a raw Node.js error
|
|
28
|
+
const isRawNodeError = output.includes("TypeError:") ||
|
|
29
|
+
output.includes("ReferenceError:") ||
|
|
30
|
+
output.includes("Cannot find module") ||
|
|
31
|
+
(output.includes("at Object.") && !output.includes("credential") && !output.includes("config"));
|
|
32
|
+
expect(isRawNodeError, `First-run crash should be user-friendly.\nGot: ${output.slice(0, 300)}`).toBe(false);
|
|
33
|
+
// Should contain SOME guidance
|
|
34
|
+
const hasGuidance = /npx|env|config|credential|token|auth|set|run|install|setup/i.test(output);
|
|
35
|
+
expect(hasGuidance, `First-run error should tell user what to do.\nGot: ${output.slice(0, 300)}`).toBe(true);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
// ----------------------------------------------------------------
|
|
39
|
+
// Environment variable pollution across MCPs
|
|
40
|
+
// ----------------------------------------------------------------
|
|
41
|
+
it("env vars from other MCPs don't cause unexpected behavior", () => {
|
|
42
|
+
// Set env vars that belong to OTHER MCPs -- should be ignored
|
|
43
|
+
const pollutedEnv = {
|
|
44
|
+
PATH: process.env.PATH ?? "",
|
|
45
|
+
HOME: process.env.HOME ?? "",
|
|
46
|
+
// Pollution from other MCPs
|
|
47
|
+
GOOGLE_ADS_DEVELOPER_TOKEN: "wrong-mcp-token",
|
|
48
|
+
BING_ADS_DEVELOPER_TOKEN: "wrong-mcp-token",
|
|
49
|
+
META_ACCESS_TOKEN: "wrong-mcp-token",
|
|
50
|
+
REDDIT_CLIENT_ID: "wrong-mcp-id",
|
|
51
|
+
LINKEDIN_ADS_REFRESH_TOKEN: "wrong-mcp-token",
|
|
52
|
+
GA4_PROPERTY_ID: "999999999",
|
|
53
|
+
GOOGLE_APPLICATION_CREDENTIALS: "/nonexistent/creds.json",
|
|
54
|
+
};
|
|
55
|
+
const result = spawnCli(config, entry, ["--help"], pollutedEnv, 5_000);
|
|
56
|
+
// --help should still work regardless of env pollution
|
|
57
|
+
expect(result.exitCode, `--help should work even with polluted env vars`).toBe(0);
|
|
58
|
+
});
|
|
59
|
+
// ----------------------------------------------------------------
|
|
60
|
+
// Double-invocation: bin entry run twice rapidly
|
|
61
|
+
// ----------------------------------------------------------------
|
|
62
|
+
it("rapid double --help invocation doesn't crash", () => {
|
|
63
|
+
const fullPath = path.resolve(config.repoRoot, entry);
|
|
64
|
+
const r1 = spawnSync("node", [fullPath, "--help"], { timeout: 5000, cwd: config.repoRoot });
|
|
65
|
+
const r2 = spawnSync("node", [fullPath, "--help"], { timeout: 5000, cwd: config.repoRoot });
|
|
66
|
+
expect(r1.status).toBe(0);
|
|
67
|
+
expect(r2.status).toBe(0);
|
|
68
|
+
});
|
|
69
|
+
// ----------------------------------------------------------------
|
|
70
|
+
// Stale npx cache detection
|
|
71
|
+
// ----------------------------------------------------------------
|
|
72
|
+
it("package.json version matches dist/build-info.json if it exists", () => {
|
|
73
|
+
const buildInfoPath = path.join(config.repoRoot, "dist", "build-info.json");
|
|
74
|
+
if (!existsSync(buildInfoPath))
|
|
75
|
+
return; // not all repos generate build-info
|
|
76
|
+
const pkg = JSON.parse(readFileSync(path.join(config.repoRoot, "package.json"), "utf-8"));
|
|
77
|
+
// build-info doesn't always include version, but if the sha is present,
|
|
78
|
+
// at least the build ran. Check it's valid JSON.
|
|
79
|
+
const buildInfo = JSON.parse(readFileSync(buildInfoPath, "utf-8"));
|
|
80
|
+
expect(buildInfo).toHaveProperty("sha");
|
|
81
|
+
expect(typeof buildInfo.sha).toBe("string");
|
|
82
|
+
});
|
|
83
|
+
// ----------------------------------------------------------------
|
|
84
|
+
// Missing dependency resilience
|
|
85
|
+
// ----------------------------------------------------------------
|
|
86
|
+
it("entry point imports don't reference nonexistent dist files", () => {
|
|
87
|
+
const entryPath = path.resolve(config.repoRoot, entry);
|
|
88
|
+
if (!existsSync(entryPath))
|
|
89
|
+
return;
|
|
90
|
+
const src = readFileSync(entryPath, "utf-8");
|
|
91
|
+
// Extract relative imports like: from "./foo.js" or from "../lib/bar.js"
|
|
92
|
+
const importRegex = /from\s+["'](\.[^"']+)["']/g;
|
|
93
|
+
let match;
|
|
94
|
+
const missing = [];
|
|
95
|
+
while ((match = importRegex.exec(src)) !== null) {
|
|
96
|
+
const importPath = match[1];
|
|
97
|
+
const resolvedImport = path.resolve(path.dirname(entryPath), importPath);
|
|
98
|
+
// Try with and without extensions
|
|
99
|
+
const candidates = [
|
|
100
|
+
resolvedImport,
|
|
101
|
+
resolvedImport.replace(/\.js$/, ".ts"),
|
|
102
|
+
];
|
|
103
|
+
if (!candidates.some((c) => existsSync(c))) {
|
|
104
|
+
missing.push(importPath);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
expect(missing, `Entry point imports files that don't exist in dist/: ${missing.join(", ")}`).toEqual([]);
|
|
108
|
+
});
|
|
109
|
+
// ----------------------------------------------------------------
|
|
110
|
+
// Unknown CLI flags don't crash
|
|
111
|
+
// ----------------------------------------------------------------
|
|
112
|
+
it("unknown flag --banana doesn't crash the process", () => {
|
|
113
|
+
const result = spawnCli(config, entry, ["--banana"], config.startupEnv, 5_000);
|
|
114
|
+
// Should either ignore the flag or exit gracefully -- not crash
|
|
115
|
+
const output = result.stdout + result.stderr;
|
|
116
|
+
const isCrash = output.includes("TypeError:") ||
|
|
117
|
+
output.includes("ReferenceError:") ||
|
|
118
|
+
output.includes("SyntaxError:");
|
|
119
|
+
expect(isCrash, `Unknown flag caused a crash:\n${output.slice(0, 200)}`).toBe(false);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { spawnCli } from "../helpers/spawn.js";
|
|
3
|
+
export function smokeSuite(config) {
|
|
4
|
+
describe("smoke tests", () => {
|
|
5
|
+
const entry = config.serverEntry ?? "dist/index.js";
|
|
6
|
+
it("server --help exits 0 and produces output", () => {
|
|
7
|
+
const result = spawnCli(config, entry, ["--help"]);
|
|
8
|
+
expect(result.exitCode, `--help exited with ${result.exitCode}\nstderr: ${result.stderr}`).toBe(0);
|
|
9
|
+
const output = result.stdout + result.stderr;
|
|
10
|
+
expect(output.length, "--help produced no output").toBeGreaterThan(0);
|
|
11
|
+
expect(output.toLowerCase()).toMatch(/help|usage|options|version/);
|
|
12
|
+
});
|
|
13
|
+
it("server --version exits 0 and prints semver", () => {
|
|
14
|
+
const result = spawnCli(config, entry, ["--version"]);
|
|
15
|
+
expect(result.exitCode, `--version exited with ${result.exitCode}\nstderr: ${result.stderr}`).toBe(0);
|
|
16
|
+
const output = result.stdout + result.stderr;
|
|
17
|
+
expect(output).toMatch(/\d+\.\d+\.\d+/);
|
|
18
|
+
});
|
|
19
|
+
if (config.hasAuthCli && config.authCliBin) {
|
|
20
|
+
it("auth-cli --help exits 0 and produces output", () => {
|
|
21
|
+
const result = spawnCli(config, config.authCliBin, ["--help"]);
|
|
22
|
+
expect(result.exitCode, `auth-cli --help exited with ${result.exitCode}\nstderr: ${result.stderr}`).toBe(0);
|
|
23
|
+
const output = result.stdout + result.stderr;
|
|
24
|
+
expect(output.length, "auth-cli --help produced no output").toBeGreaterThan(0);
|
|
25
|
+
expect(output.toLowerCase()).toMatch(/auth|credential|sign.in|usage/);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
it("server with no credentials exits non-zero with actionable message", () => {
|
|
29
|
+
// Strip all credential env vars to simulate a fresh install
|
|
30
|
+
const cleanEnv = {};
|
|
31
|
+
for (const [k, v] of Object.entries(process.env)) {
|
|
32
|
+
if (v === undefined)
|
|
33
|
+
continue;
|
|
34
|
+
// Keep PATH and basic system vars, strip everything else credential-related
|
|
35
|
+
if (k === "PATH" || k === "HOME" || k === "USER" || k === "SHELL" || k === "TERM" || k === "NODE_PATH") {
|
|
36
|
+
cleanEnv[k] = v;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const result = spawnCli(config, entry, [], cleanEnv, 10_000);
|
|
40
|
+
// Server should exit non-zero (missing creds) or print error guidance
|
|
41
|
+
const output = result.stdout + result.stderr;
|
|
42
|
+
if (result.exitCode !== 0) {
|
|
43
|
+
// Good: it failed fast. Check the error is helpful.
|
|
44
|
+
expect(output.toLowerCase()).toMatch(/credential|token|auth|config|env|missing|required|set|run/);
|
|
45
|
+
}
|
|
46
|
+
// If exitCode is 0, it may be running in stdio mode waiting for input.
|
|
47
|
+
// That's acceptable for servers that defer auth to first tool call.
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* stdout purity contract
|
|
3
|
+
*
|
|
4
|
+
* MCP's stdio transport uses stdout exclusively for JSON-RPC 2.0 frames
|
|
5
|
+
* between the server and the client (Claude Desktop, mcp-proxy, etc.).
|
|
6
|
+
* Anything else on stdout -- pino NDJSON log lines, console.log, writes
|
|
7
|
+
* from a misconfigured dependency -- corrupts the channel and causes the
|
|
8
|
+
* client's schema validator to reject frames, typically with an
|
|
9
|
+
* "unrecognized_keys: level, time, pid, hostname, msg" error.
|
|
10
|
+
*
|
|
11
|
+
* This suite spawns the real server as a subprocess with stdio piped
|
|
12
|
+
* (non-TTY, matching how Claude Desktop launches it), sends a minimal
|
|
13
|
+
* initialize + tools/list handshake, waits for activity, then inspects
|
|
14
|
+
* stdout line by line. Every non-empty line must parse as JSON and have
|
|
15
|
+
* `jsonrpc: "2.0"` set; no pino keys may appear at the top level.
|
|
16
|
+
*
|
|
17
|
+
* Why this suite exists: Mark's 2026-04-17 production incident, where
|
|
18
|
+
* mcp-google-ads and mcp-ga4 both shipped with a logger gated on
|
|
19
|
+
* `process.stderr.isTTY`. In Claude Desktop's non-TTY subprocess, pino
|
|
20
|
+
* silently fell back to stdout and every MCP request failed.
|
|
21
|
+
*/
|
|
22
|
+
import { spawn } from "child_process";
|
|
23
|
+
import path from "path";
|
|
24
|
+
import { describe, expect, it } from "vitest";
|
|
25
|
+
const PINO_KEYS = ["level", "time", "pid", "hostname", "msg"];
|
|
26
|
+
export function stdoutPuritySuite(config) {
|
|
27
|
+
describe("stdout purity (MCP JSON-RPC channel)", () => {
|
|
28
|
+
it("does not emit pino log fields on stdout under non-TTY stdio", async () => {
|
|
29
|
+
const entry = config.serverEntry ?? "dist/index.js";
|
|
30
|
+
const fullPath = path.resolve(config.repoRoot, entry);
|
|
31
|
+
// Spawn exactly as Claude Desktop would: all three fds piped, not a TTY.
|
|
32
|
+
// Pass startupEnv if defined so the server doesn't exit(1) on missing
|
|
33
|
+
// creds before we get to exercise the logger.
|
|
34
|
+
const proc = spawn("node", [fullPath], {
|
|
35
|
+
cwd: config.repoRoot,
|
|
36
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
37
|
+
env: { ...process.env, ...(config.startupEnv ?? {}), LOG_LEVEL: "debug" },
|
|
38
|
+
});
|
|
39
|
+
let stdout = "";
|
|
40
|
+
let stderr = "";
|
|
41
|
+
proc.stdout?.on("data", (chunk) => { stdout += chunk.toString("utf8"); });
|
|
42
|
+
proc.stderr?.on("data", (chunk) => { stderr += chunk.toString("utf8"); });
|
|
43
|
+
// Send a minimal MCP initialize + tools/list so the server exercises
|
|
44
|
+
// its request-handling code paths (which typically log something).
|
|
45
|
+
const send = (msg) => proc.stdin?.write(JSON.stringify(msg) + "\n");
|
|
46
|
+
send({
|
|
47
|
+
jsonrpc: "2.0",
|
|
48
|
+
id: 1,
|
|
49
|
+
method: "initialize",
|
|
50
|
+
params: {
|
|
51
|
+
protocolVersion: "2024-11-05",
|
|
52
|
+
capabilities: {},
|
|
53
|
+
clientInfo: { name: "mcp-test-harness", version: "1.0.0" },
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
send({ jsonrpc: "2.0", id: 2, method: "tools/list" });
|
|
57
|
+
// Give the server time to respond and any background init logs to flush.
|
|
58
|
+
// 2s is enough for local spawn + handshake; longer for slow credential
|
|
59
|
+
// auth paths but we don't need to wait for those — we just need any
|
|
60
|
+
// activity that would tickle the logger.
|
|
61
|
+
await new Promise((r) => setTimeout(r, 2000));
|
|
62
|
+
proc.kill("SIGTERM");
|
|
63
|
+
await new Promise((r) => {
|
|
64
|
+
const kill = setTimeout(() => { proc.kill("SIGKILL"); r(); }, 3000);
|
|
65
|
+
proc.on("exit", () => { clearTimeout(kill); r(); });
|
|
66
|
+
});
|
|
67
|
+
// Empty stdout is fine — means server crashed or didn't respond. That's
|
|
68
|
+
// a separate test's problem (smoke, mcp-protocol). We're only checking
|
|
69
|
+
// that whatever IS on stdout is valid JSON-RPC, not pino pollution.
|
|
70
|
+
const lines = stdout.split("\n").filter((l) => l.trim().length > 0);
|
|
71
|
+
const offenders = [];
|
|
72
|
+
for (const line of lines) {
|
|
73
|
+
let parsed;
|
|
74
|
+
try {
|
|
75
|
+
parsed = JSON.parse(line);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
offenders.push(`non-JSON on stdout: ${line.slice(0, 200)}`);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (parsed.jsonrpc !== "2.0") {
|
|
82
|
+
const keys = Object.keys(parsed).join(",");
|
|
83
|
+
offenders.push(`stdout line missing jsonrpc:"2.0" (keys: ${keys}): ${line.slice(0, 200)}`);
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
for (const k of PINO_KEYS) {
|
|
87
|
+
if (k in parsed) {
|
|
88
|
+
offenders.push(`stdout line has top-level pino key "${k}": ${line.slice(0, 200)}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (offenders.length > 0) {
|
|
93
|
+
const preview = offenders.slice(0, 5).join("\n - ");
|
|
94
|
+
const more = offenders.length > 5 ? `\n ... and ${offenders.length - 5} more` : "";
|
|
95
|
+
const hint = "\n\nFix: in your logger module, pass `pino.destination(2)` as the " +
|
|
96
|
+
"SECOND argument to `pino(...)` unconditionally. Do not gate it on " +
|
|
97
|
+
"`process.stderr.isTTY` — Claude Desktop spawns the server with pipes, " +
|
|
98
|
+
"not a TTY, and pino will silently default to stdout when no " +
|
|
99
|
+
"destination is specified.";
|
|
100
|
+
throw new Error(`stdout contains non-JSON-RPC output (${offenders.length} offending line${offenders.length === 1 ? "" : "s"}):\n - ${preview}${more}` +
|
|
101
|
+
`\n\nstderr tail (for context):\n${stderr.split("\n").slice(-10).join("\n")}` +
|
|
102
|
+
hint);
|
|
103
|
+
}
|
|
104
|
+
// Weak positive assertion: at least confirm the server actually handled
|
|
105
|
+
// the initialize (otherwise the test is vacuously passing). If stdout
|
|
106
|
+
// is empty the server is probably crashing at startup — note that but
|
|
107
|
+
// don't fail here; smoke suite owns startup correctness.
|
|
108
|
+
if (lines.length === 0) {
|
|
109
|
+
process.stderr.write("[stdout-purity] note: server produced no stdout output. Check smoke suite for startup failures.\n");
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
// If we got any output, at least one line should be an initialize response
|
|
113
|
+
expect(lines.some((l) => l.includes('"id":1') || l.includes('"id": 1'))).toBe(true);
|
|
114
|
+
}
|
|
115
|
+
// This test spends 5s waiting on purpose (2s flush sleep + up to 3s for
|
|
116
|
+
// the process to exit before SIGKILL). Everything left over is the budget
|
|
117
|
+
// for spawning node, loading the server's modules, and whatever the
|
|
118
|
+
// server does at startup — which for any server that validates
|
|
119
|
+
// credentials before connecting its transport includes a network
|
|
120
|
+
// round-trip. The previous 10s ceiling left only 5s for all of that, and
|
|
121
|
+
// mcp-bing-ads failed at 10.32s in isolation (not parallel-load
|
|
122
|
+
// contention) on exactly that margin.
|
|
123
|
+
//
|
|
124
|
+
// 30s leaves 25s of headroom. tests/stdout-purity-timeout.test.ts pins
|
|
125
|
+
// the ceiling against the hardcoded waits so the two cannot drift apart.
|
|
126
|
+
}, 30_000);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import os from "os";
|
|
4
|
+
import { existsSync, readFileSync, writeFileSync, unlinkSync } from "fs";
|
|
5
|
+
export function tokenLifecycleSuite(config) {
|
|
6
|
+
describe("token lifecycle", () => {
|
|
7
|
+
it("credentials.ts handles missing file gracefully", async () => {
|
|
8
|
+
try {
|
|
9
|
+
const mod = await import(path.join(config.repoRoot, "dist", "credentials.js"));
|
|
10
|
+
const readFn = mod.readStoredCredentials ?? mod.readCredentials;
|
|
11
|
+
if (!readFn)
|
|
12
|
+
return;
|
|
13
|
+
const result = readFn("/nonexistent/path/credentials.json");
|
|
14
|
+
expect(result).toBeNull();
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
// Module may not export this function -- skip
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
it("credentials.ts handles corrupt JSON gracefully", async () => {
|
|
21
|
+
const tmpFile = path.join(os.tmpdir(), `mcp-test-corrupt-${Date.now()}.json`);
|
|
22
|
+
writeFileSync(tmpFile, "{{{not json}}}");
|
|
23
|
+
try {
|
|
24
|
+
const mod = await import(path.join(config.repoRoot, "dist", "credentials.js"));
|
|
25
|
+
const readFn = mod.readStoredCredentials ?? mod.readCredentials;
|
|
26
|
+
if (!readFn)
|
|
27
|
+
return;
|
|
28
|
+
const result = readFn(tmpFile);
|
|
29
|
+
expect(result).toBeNull();
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
// If it throws, that's a bug -- but some implementations may not have this function
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
unlinkSync(tmpFile);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
it("credentials.ts handles wrong version field gracefully", async () => {
|
|
39
|
+
const tmpFile = path.join(os.tmpdir(), `mcp-test-version-${Date.now()}.json`);
|
|
40
|
+
writeFileSync(tmpFile, JSON.stringify({ version: 999, refresh_token: "fake" }));
|
|
41
|
+
try {
|
|
42
|
+
const mod = await import(path.join(config.repoRoot, "dist", "credentials.js"));
|
|
43
|
+
const readFn = mod.readStoredCredentials ?? mod.readCredentials;
|
|
44
|
+
if (!readFn)
|
|
45
|
+
return;
|
|
46
|
+
const result = readFn(tmpFile);
|
|
47
|
+
expect(result).toBeNull();
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
// skip
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
unlinkSync(tmpFile);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
it("credential write sets restrictive file permissions", async () => {
|
|
57
|
+
const credPath = path.join(config.repoRoot, config.srcDir ?? "src", "credentials.ts");
|
|
58
|
+
if (!existsSync(credPath))
|
|
59
|
+
return;
|
|
60
|
+
const src = readFileSync(credPath, "utf-8");
|
|
61
|
+
expect(src.includes("0o600") || src.includes("0600") || src.includes("chmod"), "credentials should be written with restrictive permissions").toBe(true);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { readFileSync, existsSync } from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { getPackFiles, findForbiddenPackFiles } from "../helpers/pack.js";
|
|
5
|
+
export function versionPublishSuite(config) {
|
|
6
|
+
describe("version & publish safety", () => {
|
|
7
|
+
it("package.json files field exists and includes dist/", () => {
|
|
8
|
+
const pkg = JSON.parse(readFileSync(path.join(config.repoRoot, "package.json"), "utf-8"));
|
|
9
|
+
if (!pkg.files)
|
|
10
|
+
return; // if no files field, npm includes everything (risky but valid)
|
|
11
|
+
expect(pkg.files.some((f) => f === "dist" || f.startsWith("dist/")), 'package.json "files" should include "dist"').toBe(true);
|
|
12
|
+
});
|
|
13
|
+
it("npm pack does not include test files", () => {
|
|
14
|
+
const packed = getPackFiles(config.repoRoot);
|
|
15
|
+
if (packed.length === 0)
|
|
16
|
+
return;
|
|
17
|
+
const testFiles = packed.filter((f) => f.endsWith(".test.ts") || f.endsWith(".test.js") || f.endsWith(".spec.ts"));
|
|
18
|
+
expect(testFiles, "test files would be included in npm package").toEqual([]);
|
|
19
|
+
});
|
|
20
|
+
it("npm pack does not include run-mcp.sh or .env", () => {
|
|
21
|
+
const packed = getPackFiles(config.repoRoot);
|
|
22
|
+
if (packed.length === 0)
|
|
23
|
+
return;
|
|
24
|
+
const forbidden = findForbiddenPackFiles(packed, [".env", "run-mcp.sh"]);
|
|
25
|
+
expect(forbidden, "sensitive files in npm package").toEqual([]);
|
|
26
|
+
});
|
|
27
|
+
it("README.md exists", () => {
|
|
28
|
+
expect(existsSync(path.join(config.repoRoot, "README.md")), "README.md not found").toBe(true);
|
|
29
|
+
});
|
|
30
|
+
it("LICENSE exists", () => {
|
|
31
|
+
expect(existsSync(path.join(config.repoRoot, "LICENSE")), "LICENSE not found").toBe(true);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration for a single MCP server repo.
|
|
3
|
+
* The harness uses this to decide which test suites apply and how to parameterize them.
|
|
4
|
+
*/
|
|
5
|
+
export interface McpServerConfig {
|
|
6
|
+
/** Human-readable name, e.g. "mcp-google-ads" */
|
|
7
|
+
name: string;
|
|
8
|
+
/** Absolute path to the repo root */
|
|
9
|
+
repoRoot: string;
|
|
10
|
+
/** Tool name prefix, e.g. "google_ads_" */
|
|
11
|
+
toolPrefix: string;
|
|
12
|
+
/** Minimum expected tool count */
|
|
13
|
+
minTools: number;
|
|
14
|
+
/** Tool names that MUST exist */
|
|
15
|
+
requiredTools: string[];
|
|
16
|
+
/** Binary entries from package.json "bin" -- validated against dist/ */
|
|
17
|
+
binEntries: Record<string, string>;
|
|
18
|
+
/** Does this server have an auth-cli binary? */
|
|
19
|
+
hasAuthCli: boolean;
|
|
20
|
+
/** Auth CLI binary path relative to repo root (if hasAuthCli) */
|
|
21
|
+
authCliBin?: string;
|
|
22
|
+
/** Does this server have credentials.ts with file-based credential storage? */
|
|
23
|
+
hasCredentials: boolean;
|
|
24
|
+
/** Does this server have resilience.ts (retry/circuit-breaker)? */
|
|
25
|
+
hasResilience: boolean;
|
|
26
|
+
/** Does this server have platform.ts (cross-platform helpers)? */
|
|
27
|
+
hasPlatform: boolean;
|
|
28
|
+
/** Environment variable names the server checks for at startup */
|
|
29
|
+
requiredEnvVars: string[];
|
|
30
|
+
/** Env var prefix, e.g. "GOOGLE_ADS_" -- used for credential leak detection */
|
|
31
|
+
envPrefix: string;
|
|
32
|
+
/** Files/patterns that must NOT appear in npm pack output */
|
|
33
|
+
packExcludes?: string[];
|
|
34
|
+
/** Source directory relative to repo root (default: "src") */
|
|
35
|
+
srcDir?: string;
|
|
36
|
+
/** Entry point for the MCP server relative to repo root (default: "dist/index.js") */
|
|
37
|
+
serverEntry?: string;
|
|
38
|
+
/** Env vars to set when spawning the server for smoke/shutdown tests.
|
|
39
|
+
* Set dummy values so the server doesn't exit(1) on missing creds. */
|
|
40
|
+
startupEnv?: Record<string, string>;
|
|
41
|
+
/** Patterns to skip in source-lint (e.g. files that legitimately use platform APIs) */
|
|
42
|
+
sourceLintIgnore?: string[];
|
|
43
|
+
}
|
|
44
|
+
/** Default values applied when config fields are omitted */
|
|
45
|
+
export declare const CONFIG_DEFAULTS: {
|
|
46
|
+
readonly srcDir: "src";
|
|
47
|
+
readonly serverEntry: "dist/index.js";
|
|
48
|
+
readonly packExcludes: readonly [".env", "config.json", "credentials.json", "*.test.ts", "*.test.js", "*.spec.ts", "run-mcp.sh", "__pycache__"];
|
|
49
|
+
};
|
|
50
|
+
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Default values applied when config fields are omitted */
|
|
2
|
+
export const CONFIG_DEFAULTS = {
|
|
3
|
+
srcDir: "src",
|
|
4
|
+
serverEntry: "dist/index.js",
|
|
5
|
+
packExcludes: [
|
|
6
|
+
".env",
|
|
7
|
+
"config.json",
|
|
8
|
+
"credentials.json",
|
|
9
|
+
"*.test.ts",
|
|
10
|
+
"*.test.js",
|
|
11
|
+
"*.spec.ts",
|
|
12
|
+
"run-mcp.sh",
|
|
13
|
+
"__pycache__",
|
|
14
|
+
],
|
|
15
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@drak-marketing/mcp-test-harness",
|
|
3
|
+
"version": "0.2.2",
|
|
4
|
+
"description": "Shared test harness for MCP server repos. Parameterized test suites for build integrity, smoke tests, protocol conformance, security, and more.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist/**/*.js",
|
|
16
|
+
"dist/**/*.d.ts",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"test": "vitest run",
|
|
23
|
+
"typecheck": "tsc --noEmit",
|
|
24
|
+
"prepublishOnly": "npm run build"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"mcp",
|
|
28
|
+
"model-context-protocol",
|
|
29
|
+
"test-harness",
|
|
30
|
+
"vitest",
|
|
31
|
+
"testing"
|
|
32
|
+
],
|
|
33
|
+
"author": "drak-marketing",
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/mharnett/mcp-test-harness.git"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"vitest": ">=3.0.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/node": "^20.10.0",
|
|
47
|
+
"typescript": "^5.3.0",
|
|
48
|
+
"vitest": "^4.0.18"
|
|
49
|
+
}
|
|
50
|
+
}
|