Simplify runtime checks and MCP smokes
This commit is contained in:
188
scripts/mcp-smoke-client.mjs
Normal file
188
scripts/mcp-smoke-client.mjs
Normal file
@@ -0,0 +1,188 @@
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
export function textFrom(result) {
|
||||
return (result.content || [])
|
||||
.filter(part => part.type === "text")
|
||||
.map(part => part.text)
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
export function requireToolSuccess(name, result) {
|
||||
if (result?.isError) {
|
||||
const payload = textFrom(result) || JSON.stringify(result);
|
||||
throw new Error(`${name} returned an error: ${payload.slice(0, 500)}`);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function runSmoke({ usage, tmpPrefix, timeoutMs, clientInfo, scenario }) {
|
||||
const command = process.argv[2];
|
||||
const args = process.argv.slice(3);
|
||||
|
||||
if (!command) {
|
||||
throw new Error(usage);
|
||||
}
|
||||
|
||||
const client = new McpSmokeClient({ command, args, tmpPrefix });
|
||||
const timeout = setTimeout(async () => {
|
||||
await client.stop();
|
||||
console.error(`MCP smoke timed out. stderr: ${client.stderrTail(2000)}`);
|
||||
process.exit(1);
|
||||
}, timeoutMs);
|
||||
|
||||
try {
|
||||
await client.initialize(clientInfo);
|
||||
const result = await scenario(client);
|
||||
clearTimeout(timeout);
|
||||
await client.stop();
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
} catch (error) {
|
||||
clearTimeout(timeout);
|
||||
await client.stop();
|
||||
console.error(error.message);
|
||||
const stderr = client.stderrTail(4000);
|
||||
if (stderr) console.error(stderr);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
class McpSmokeClient {
|
||||
constructor({ command, args, tmpPrefix }) {
|
||||
this.tmpDir = mkdtempSync(join(tmpdir(), tmpPrefix));
|
||||
this.cidFile = join(this.tmpDir, "container.cid");
|
||||
this.nextId = 1;
|
||||
this.pending = new Map();
|
||||
this.stdoutBuffer = "";
|
||||
this.stderrBuffer = "";
|
||||
this.exited = false;
|
||||
|
||||
this.child = spawn(command, args, {
|
||||
cwd: new URL("..", import.meta.url).pathname,
|
||||
env: { ...process.env, CONTEXT_KIT_DOCKER_CIDFILE: this.cidFile },
|
||||
stdio: ["pipe", "pipe", "pipe"]
|
||||
});
|
||||
|
||||
this.child.once("exit", (code, signal) => {
|
||||
this.exited = true;
|
||||
if (this.pending.size > 0) {
|
||||
const error = new Error(`MCP child exited before responding (code=${code}, signal=${signal}). stderr: ${this.stderrTail(2000)}`);
|
||||
for (const { reject } of this.pending.values()) reject(error);
|
||||
this.pending.clear();
|
||||
}
|
||||
});
|
||||
|
||||
this.child.stderr.on("data", chunk => {
|
||||
this.stderrBuffer += chunk.toString();
|
||||
});
|
||||
|
||||
this.child.stdout.on("data", chunk => this.handleStdout(chunk));
|
||||
}
|
||||
|
||||
stderrTail(length) {
|
||||
return this.stderrBuffer.slice(-length);
|
||||
}
|
||||
|
||||
handleStdout(chunk) {
|
||||
this.stdoutBuffer += chunk.toString();
|
||||
let newline;
|
||||
while ((newline = this.stdoutBuffer.indexOf("\n")) >= 0) {
|
||||
const line = this.stdoutBuffer.slice(0, newline).trim();
|
||||
this.stdoutBuffer = this.stdoutBuffer.slice(newline + 1);
|
||||
if (!line) continue;
|
||||
let message;
|
||||
try {
|
||||
message = JSON.parse(line);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
if (message.id && this.pending.has(message.id)) {
|
||||
const { resolve, reject } = this.pending.get(message.id);
|
||||
this.pending.delete(message.id);
|
||||
if (message.error) reject(new Error(JSON.stringify(message.error)));
|
||||
else resolve(message.result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
request(method, params = {}) {
|
||||
if (this.exited) {
|
||||
return Promise.reject(new Error(`MCP child already exited. stderr: ${this.stderrTail(2000)}`));
|
||||
}
|
||||
const id = this.nextId++;
|
||||
this.child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id, method, params })}\n`);
|
||||
return new Promise((resolve, reject) => this.pending.set(id, { resolve, reject }));
|
||||
}
|
||||
|
||||
notify(method, params = {}) {
|
||||
this.child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", method, params })}\n`);
|
||||
}
|
||||
|
||||
async initialize(clientInfo) {
|
||||
await this.request("initialize", {
|
||||
protocolVersion: "2024-11-05",
|
||||
capabilities: {},
|
||||
clientInfo
|
||||
});
|
||||
this.notify("notifications/initialized");
|
||||
}
|
||||
|
||||
async tools() {
|
||||
const listed = await this.request("tools/list");
|
||||
return new Set((listed.tools || []).map(tool => tool.name));
|
||||
}
|
||||
|
||||
async requireTools(names) {
|
||||
const toolNames = await this.tools();
|
||||
for (const name of names) {
|
||||
if (!toolNames.has(name)) throw new Error(`missing tool: ${name}`);
|
||||
}
|
||||
return toolNames;
|
||||
}
|
||||
|
||||
callTool(name, args = {}) {
|
||||
return this.request("tools/call", { name, arguments: args });
|
||||
}
|
||||
|
||||
stopContainer() {
|
||||
if (!existsSync(this.cidFile)) return;
|
||||
const containerId = readFileSync(this.cidFile, "utf8").trim();
|
||||
if (!containerId) return;
|
||||
spawnSync("docker", ["stop", containerId], { stdio: "ignore" });
|
||||
}
|
||||
|
||||
stop() {
|
||||
return new Promise(resolve => {
|
||||
if (this.exited) {
|
||||
this.stopContainer();
|
||||
rmSync(this.tmpDir, { recursive: true, force: true });
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
let stopTimer;
|
||||
let termTimer;
|
||||
let killTimer;
|
||||
this.child.once("exit", () => {
|
||||
this.stopContainer();
|
||||
clearTimeout(stopTimer);
|
||||
clearTimeout(termTimer);
|
||||
clearTimeout(killTimer);
|
||||
rmSync(this.tmpDir, { recursive: true, force: true });
|
||||
resolve();
|
||||
});
|
||||
|
||||
stopTimer = setTimeout(() => this.stopContainer(), 1000);
|
||||
termTimer = setTimeout(() => {
|
||||
if (!this.exited) this.child.kill("SIGTERM");
|
||||
}, 3000);
|
||||
killTimer = setTimeout(() => {
|
||||
if (!this.exited) this.child.kill("SIGKILL");
|
||||
}, 6000);
|
||||
|
||||
this.child.stdin.end();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,29 @@ ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
cd "${ROOT}"
|
||||
|
||||
tmp_dir="$(mktemp -d)"
|
||||
pick_port() {
|
||||
python - <<'PY'
|
||||
import socket
|
||||
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
print(sock.getsockname()[1])
|
||||
PY
|
||||
}
|
||||
|
||||
release_id="release-$$"
|
||||
export CONTEXT_KIT_COMPOSE_PROJECT="context-kit-${release_id}"
|
||||
export CONTEXT_KIT_DATA_DIR="${tmp_dir}/data"
|
||||
export CONTEXT_KIT_SEARXNG_PORT="$(pick_port)"
|
||||
export CONTEXT_KIT_DOCS_PORT="$(pick_port)"
|
||||
export CONTEXT_KIT_DOCS_SOURCES="config/sources.default.txt"
|
||||
export CONTEXT_KIT_DOCS_LOCAL_SOURCES_DIR="${tmp_dir}/local-sources"
|
||||
export CONTEXT_KIT_WEB_SEARCH_IMAGE="context-kit/web-search-mcp:${release_id}"
|
||||
export CONTEXT_KIT_DOCS_IMAGE="context-kit/docs-mcp:${release_id}"
|
||||
|
||||
cleanup() {
|
||||
docker compose -p "${CONTEXT_KIT_COMPOSE_PROJECT}" -f compose.yml down -v --remove-orphans >/dev/null 2>&1 || true
|
||||
docker image rm "${CONTEXT_KIT_WEB_SEARCH_IMAGE}" "${CONTEXT_KIT_DOCS_IMAGE}" >/dev/null 2>&1 || true
|
||||
rm -rf "${tmp_dir}"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
@@ -32,19 +54,47 @@ assert_redaction_check_does_not_disclose_matches() {
|
||||
fi
|
||||
}
|
||||
|
||||
assert_web_search_image() {
|
||||
docker run --rm --entrypoint node \
|
||||
-e EXPECTED_MAX_BYTES="${CONTEXT_KIT_WEB_SEARCH_MAX_BYTES:-52428800}" \
|
||||
"${CONTEXT_KIT_WEB_SEARCH_IMAGE}" \
|
||||
-e '
|
||||
const fs = require("node:fs");
|
||||
const expected = Number(process.env.EXPECTED_MAX_BYTES) || 0;
|
||||
const actual = Number(process.env.MAX_BYTES) || 0;
|
||||
if (process.getuid && process.getuid() === 0) process.exit(1);
|
||||
if (actual !== expected) process.exit(1);
|
||||
|
||||
const serverPath = "/usr/local/lib/node_modules/@zhafron/mcp-web-search/dist/src/server.js";
|
||||
const server = fs.readFileSync(serverPath, "utf8");
|
||||
if (!server.includes("max_download_bytes: z.number().int().min(1).max(MAX_BYTES).optional()")) process.exit(1);
|
||||
|
||||
const bingPath = "/usr/local/lib/node_modules/@zhafron/mcp-web-search/dist/src/providers/bing.js";
|
||||
const bing = fs.readFileSync(bingPath, "utf8");
|
||||
if (!bing.includes("Context Kit override for @zhafron/mcp-web-search 1.3.0")) process.exit(1);
|
||||
if (!bing.includes("waitForSelector")) process.exit(1);
|
||||
if (!bing.includes("decodeBingRedirect")) process.exit(1);
|
||||
' >/dev/null
|
||||
|
||||
docker run --rm --entrypoint /usr/bin/test \
|
||||
"${CONTEXT_KIT_WEB_SEARCH_IMAGE}" \
|
||||
-x "${CONTEXT_KIT_WEB_SEARCH_CHROME_PATH:-/usr/bin/chromium}"
|
||||
}
|
||||
|
||||
git diff --check HEAD
|
||||
git show --check --format= HEAD >/dev/null
|
||||
git ls-files --cached --error-unmatch \
|
||||
docker/web-search/patch-mcp-web-search.mjs \
|
||||
docker/web-search/overrides/bing.js \
|
||||
docker/docs/constraints.txt \
|
||||
scripts/mcp-smoke-client.mjs \
|
||||
scripts/smoke-web-search.mjs \
|
||||
scripts/smoke-docs.mjs \
|
||||
scripts/release-check >/dev/null
|
||||
bash -n bin/context-kit
|
||||
bash -n scripts/release-check
|
||||
sh -n docker/docs/entrypoint.sh
|
||||
check_node docker/web-search/patch-mcp-web-search.mjs docker/web-search/overrides/bing.js scripts/smoke-web-search.mjs scripts/smoke-docs.mjs
|
||||
check_node docker/web-search/patch-mcp-web-search.mjs docker/web-search/overrides/bing.js scripts/mcp-smoke-client.mjs scripts/smoke-web-search.mjs scripts/smoke-docs.mjs
|
||||
|
||||
node -e 'const fs=require("node:fs"); JSON.parse(fs.readFileSync("snippets/opencode.json", "utf8")); JSON.parse(fs.readFileSync("snippets/claude.mcp.json", "utf8"));'
|
||||
bin/context-kit install opencode > "${tmp_dir}/opencode.json"
|
||||
@@ -60,13 +110,14 @@ bin/context-kit redaction-check "${tmp_dir}/opencode.json" "${tmp_dir}/claude.js
|
||||
assert_redaction_check_does_not_disclose_matches
|
||||
|
||||
bin/context-kit redaction-check
|
||||
docker compose -p context-kit -f compose.yml config >/dev/null
|
||||
if env -u HOME docker compose --env-file /dev/null -p context-kit-release-home-check -f compose.yml config >"${tmp_dir}/compose-no-home.out" 2>"${tmp_dir}/compose-no-home.err"; then
|
||||
docker compose -p "${CONTEXT_KIT_COMPOSE_PROJECT}" -f compose.yml config >/dev/null
|
||||
if env -u HOME -u CONTEXT_KIT_DATA_DIR -u CONTEXT_KIT_DOCS_LOCAL_SOURCES_DIR docker compose --env-file /dev/null -p context-kit-release-home-check -f compose.yml config >"${tmp_dir}/compose-no-home.out" 2>"${tmp_dir}/compose-no-home.err"; then
|
||||
printf 'compose config unexpectedly succeeded without HOME or CONTEXT_KIT_DATA_DIR\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
CONTEXT_KIT_DATA_DIR="${tmp_dir}/compose-data" env -u HOME docker compose --env-file /dev/null -p context-kit-release-home-check -f compose.yml config >/dev/null
|
||||
bin/context-kit build
|
||||
assert_web_search_image
|
||||
bin/context-kit restart
|
||||
bin/context-kit doctor
|
||||
node scripts/smoke-web-search.mjs bin/context-kit web-search
|
||||
|
||||
@@ -1,170 +1,42 @@
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { requireToolSuccess, runSmoke } from "./mcp-smoke-client.mjs";
|
||||
|
||||
const command = process.argv[2];
|
||||
const args = process.argv.slice(3);
|
||||
const live = process.env.CONTEXT_KIT_LIVE_CHECKS === "1";
|
||||
|
||||
if (!command) {
|
||||
throw new Error("usage: node scripts/smoke-docs.mjs <command> [args...]");
|
||||
}
|
||||
runSmoke({
|
||||
usage: "usage: node scripts/smoke-docs.mjs <command> [args...]",
|
||||
tmpPrefix: "context-kit-docs-smoke-",
|
||||
timeoutMs: 300000,
|
||||
clientInfo: { name: "context-kit-docs-smoke", version: "0.0.0" },
|
||||
scenario: async client => {
|
||||
const toolNames = await client.requireTools(["docs_query", "docs_sources"]);
|
||||
|
||||
const tmpDir = mkdtempSync(join(tmpdir(), "context-kit-docs-smoke-"));
|
||||
const cidFile = join(tmpDir, "container.cid");
|
||||
|
||||
const child = spawn(command, args, {
|
||||
cwd: new URL("..", import.meta.url).pathname,
|
||||
env: { ...process.env, CONTEXT_KIT_DOCKER_CIDFILE: cidFile },
|
||||
stdio: ["pipe", "pipe", "pipe"]
|
||||
});
|
||||
|
||||
let nextId = 1;
|
||||
const pending = new Map();
|
||||
let stdoutBuffer = "";
|
||||
let stderrBuffer = "";
|
||||
let childExited = false;
|
||||
|
||||
child.once("exit", (code, signal) => {
|
||||
childExited = true;
|
||||
if (pending.size > 0) {
|
||||
const error = new Error(`MCP child exited before responding (code=${code}, signal=${signal}). stderr: ${stderrBuffer.slice(-2000)}`);
|
||||
for (const { reject } of pending.values()) reject(error);
|
||||
pending.clear();
|
||||
}
|
||||
});
|
||||
|
||||
function stopChild() {
|
||||
return new Promise(resolve => {
|
||||
if (childExited) {
|
||||
stopContainer();
|
||||
rmSync(tmpDir, { recursive: true, force: true });
|
||||
resolve();
|
||||
return;
|
||||
const sources = requireToolSuccess("docs_sources", await client.callTool("docs_sources"));
|
||||
if (!Array.isArray(sources?.structuredContent?.result)) {
|
||||
const sourcesText = JSON.stringify(sources);
|
||||
throw new Error(`docs_sources returned unexpected payload: ${sourcesText.slice(0, 500)}`);
|
||||
}
|
||||
|
||||
child.stdin.end();
|
||||
const stopTimer = setTimeout(() => {
|
||||
stopContainer();
|
||||
}, 1000);
|
||||
const termTimer = setTimeout(() => {
|
||||
if (!childExited) child.kill("SIGTERM");
|
||||
}, 3000);
|
||||
const killTimer = setTimeout(() => {
|
||||
if (!childExited) child.kill("SIGKILL");
|
||||
}, 6000);
|
||||
const result = {
|
||||
tools: Array.from(toolNames).sort(),
|
||||
docs_sources: "pass"
|
||||
};
|
||||
|
||||
child.once("exit", () => {
|
||||
stopContainer();
|
||||
clearTimeout(stopTimer);
|
||||
clearTimeout(termTimer);
|
||||
clearTimeout(killTimer);
|
||||
rmSync(tmpDir, { recursive: true, force: true });
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function stopContainer() {
|
||||
if (!existsSync(cidFile)) return;
|
||||
const containerId = readFileSync(cidFile, "utf8").trim();
|
||||
if (!containerId) return;
|
||||
spawnSync("docker", ["stop", containerId], { stdio: "ignore" });
|
||||
}
|
||||
|
||||
const timeout = setTimeout(async () => {
|
||||
await stopChild();
|
||||
console.error(`Docs MCP smoke timed out. stderr: ${stderrBuffer.slice(-2000)}`);
|
||||
process.exit(1);
|
||||
}, 300000);
|
||||
|
||||
child.stderr.on("data", chunk => {
|
||||
stderrBuffer += chunk.toString();
|
||||
});
|
||||
|
||||
child.stdout.on("data", chunk => {
|
||||
stdoutBuffer += chunk.toString();
|
||||
let newline;
|
||||
while ((newline = stdoutBuffer.indexOf("\n")) >= 0) {
|
||||
const line = stdoutBuffer.slice(0, newline).trim();
|
||||
stdoutBuffer = stdoutBuffer.slice(newline + 1);
|
||||
if (!line) continue;
|
||||
let message;
|
||||
try {
|
||||
message = JSON.parse(line);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
if (message.id && pending.has(message.id)) {
|
||||
const { resolve, reject } = pending.get(message.id);
|
||||
pending.delete(message.id);
|
||||
if (message.error) reject(new Error(JSON.stringify(message.error)));
|
||||
else resolve(message.result);
|
||||
if (live) {
|
||||
const query = requireToolSuccess("docs_query", await client.callTool("docs_query", {
|
||||
query: "Model Context Protocol documentation",
|
||||
limit: 3,
|
||||
auto_retrieve: true,
|
||||
auto_retrieve_threshold: 0.1,
|
||||
auto_retrieve_limit: 1,
|
||||
max_bytes: 12000
|
||||
}));
|
||||
const queryText = JSON.stringify(query);
|
||||
if (!queryText.includes("search_results") && !queryText.includes("Model Context Protocol")) {
|
||||
throw new Error(`docs_query returned unexpected payload: ${queryText.slice(0, 500)}`);
|
||||
}
|
||||
result.docs_query = "pass";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
});
|
||||
|
||||
function request(method, params = {}) {
|
||||
if (childExited) {
|
||||
return Promise.reject(new Error(`MCP child already exited. stderr: ${stderrBuffer.slice(-2000)}`));
|
||||
}
|
||||
const id = nextId++;
|
||||
child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id, method, params })}\n`);
|
||||
return new Promise((resolve, reject) => pending.set(id, { resolve, reject }));
|
||||
}
|
||||
|
||||
function notify(method, params = {}) {
|
||||
child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", method, params })}\n`);
|
||||
}
|
||||
|
||||
async function callTool(name, args = {}) {
|
||||
return request("tools/call", { name, arguments: args });
|
||||
}
|
||||
|
||||
try {
|
||||
await request("initialize", {
|
||||
protocolVersion: "2024-11-05",
|
||||
capabilities: {},
|
||||
clientInfo: { name: "context-kit-docs-smoke", version: "0.0.0" }
|
||||
});
|
||||
notify("notifications/initialized");
|
||||
|
||||
const listed = await request("tools/list");
|
||||
const toolNames = new Set((listed.tools || []).map(tool => tool.name));
|
||||
for (const name of ["docs_query", "docs_sources"]) {
|
||||
if (!toolNames.has(name)) throw new Error(`missing tool: ${name}`);
|
||||
}
|
||||
|
||||
const sources = await callTool("docs_sources");
|
||||
const sourcesText = JSON.stringify(sources);
|
||||
if (sources.isError) {
|
||||
throw new Error(`docs_sources returned an error: ${sourcesText.slice(0, 500)}`);
|
||||
}
|
||||
|
||||
const query = await callTool("docs_query", {
|
||||
query: "Model Context Protocol documentation",
|
||||
limit: 3,
|
||||
auto_retrieve: true,
|
||||
auto_retrieve_threshold: 0.1,
|
||||
auto_retrieve_limit: 1,
|
||||
max_bytes: 12000
|
||||
});
|
||||
const queryText = JSON.stringify(query);
|
||||
if (!queryText.includes("search_results") && !queryText.includes("Model Context Protocol")) {
|
||||
throw new Error(`docs_query returned unexpected payload: ${queryText.slice(0, 500)}`);
|
||||
}
|
||||
|
||||
clearTimeout(timeout);
|
||||
await stopChild();
|
||||
console.log(JSON.stringify({
|
||||
tools: Array.from(toolNames).sort(),
|
||||
docs_sources: "pass",
|
||||
docs_query: "pass"
|
||||
}, null, 2));
|
||||
} catch (error) {
|
||||
clearTimeout(timeout);
|
||||
await stopChild();
|
||||
console.error(error.message);
|
||||
if (stderrBuffer) console.error(stderrBuffer.slice(-4000));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
@@ -1,197 +1,63 @@
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { requireToolSuccess, runSmoke, textFrom } from "./mcp-smoke-client.mjs";
|
||||
|
||||
const command = process.argv[2];
|
||||
const args = process.argv.slice(3);
|
||||
const live = process.env.CONTEXT_KIT_LIVE_CHECKS === "1";
|
||||
|
||||
if (!command) {
|
||||
throw new Error("usage: node scripts/smoke-web-search.mjs <command> [args...]");
|
||||
}
|
||||
runSmoke({
|
||||
usage: "usage: node scripts/smoke-web-search.mjs <command> [args...]",
|
||||
tmpPrefix: "context-kit-web-search-smoke-",
|
||||
timeoutMs: 120000,
|
||||
clientInfo: { name: "context-kit-web-search-smoke", version: "0.0.0" },
|
||||
scenario: async client => {
|
||||
const toolNames = await client.requireTools(["search_web", "fetch_url"]);
|
||||
|
||||
const tmpDir = mkdtempSync(join(tmpdir(), "context-kit-web-search-smoke-"));
|
||||
const cidFile = join(tmpDir, "container.cid");
|
||||
|
||||
const child = spawn(command, args, {
|
||||
cwd: new URL("..", import.meta.url).pathname,
|
||||
env: { ...process.env, CONTEXT_KIT_DOCKER_CIDFILE: cidFile },
|
||||
stdio: ["pipe", "pipe", "pipe"]
|
||||
});
|
||||
|
||||
let nextId = 1;
|
||||
const pending = new Map();
|
||||
let stdoutBuffer = "";
|
||||
let stderrBuffer = "";
|
||||
let childExited = false;
|
||||
|
||||
child.once("exit", (code, signal) => {
|
||||
childExited = true;
|
||||
if (pending.size > 0) {
|
||||
const error = new Error(`MCP child exited before responding (code=${code}, signal=${signal}). stderr: ${stderrBuffer.slice(-2000)}`);
|
||||
for (const { reject } of pending.values()) reject(error);
|
||||
pending.clear();
|
||||
}
|
||||
});
|
||||
|
||||
function stopChild() {
|
||||
return new Promise(resolve => {
|
||||
if (childExited) {
|
||||
stopContainer();
|
||||
rmSync(tmpDir, { recursive: true, force: true });
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
child.stdin.end();
|
||||
const stopTimer = setTimeout(() => {
|
||||
stopContainer();
|
||||
}, 1000);
|
||||
const termTimer = setTimeout(() => {
|
||||
if (!childExited) child.kill("SIGTERM");
|
||||
}, 3000);
|
||||
const killTimer = setTimeout(() => {
|
||||
if (!childExited) child.kill("SIGKILL");
|
||||
}, 6000);
|
||||
|
||||
child.once("exit", () => {
|
||||
stopContainer();
|
||||
clearTimeout(stopTimer);
|
||||
clearTimeout(termTimer);
|
||||
clearTimeout(killTimer);
|
||||
rmSync(tmpDir, { recursive: true, force: true });
|
||||
resolve();
|
||||
const localResult = await client.callTool("fetch_url", {
|
||||
url: "http://127.0.0.1:1/",
|
||||
max_download_bytes: 52428800
|
||||
});
|
||||
});
|
||||
}
|
||||
const localBlocked = Boolean(localResult.isError) && textFrom(localResult).includes("Blocked localhost/private URL");
|
||||
if (!localBlocked) throw new Error("localhost/private URL was not blocked as expected");
|
||||
|
||||
function stopContainer() {
|
||||
if (!existsSync(cidFile)) return;
|
||||
const containerId = readFileSync(cidFile, "utf8").trim();
|
||||
if (!containerId) return;
|
||||
spawnSync("docker", ["stop", containerId], { stdio: "ignore" });
|
||||
}
|
||||
const result = {
|
||||
tools: Array.from(toolNames).sort(),
|
||||
localhost_guard: "pass"
|
||||
};
|
||||
|
||||
const timeout = setTimeout(async () => {
|
||||
await stopChild();
|
||||
console.error(`MCP smoke timed out. stderr: ${stderrBuffer.slice(-2000)}`);
|
||||
process.exit(1);
|
||||
}, 120000);
|
||||
if (live) {
|
||||
const searxng = textFrom(requireToolSuccess("search_web/searxng", await client.callTool("search_web", {
|
||||
q: "Model Context Protocol",
|
||||
limit: 2,
|
||||
provider: "searxng"
|
||||
})));
|
||||
if (!searxng.includes("Model")) throw new Error(`SearXNG smoke returned unexpected text: ${searxng.slice(0, 500)}`);
|
||||
|
||||
child.stderr.on("data", chunk => {
|
||||
stderrBuffer += chunk.toString();
|
||||
});
|
||||
const bing = textFrom(requireToolSuccess("search_web/bing", await client.callTool("search_web", {
|
||||
q: "Model Context Protocol",
|
||||
limit: 2,
|
||||
provider: "bing"
|
||||
})));
|
||||
if (!bing.includes("Model")) throw new Error(`Bing smoke returned unexpected text: ${bing.slice(0, 500)}`);
|
||||
|
||||
child.stdout.on("data", chunk => {
|
||||
stdoutBuffer += chunk.toString();
|
||||
let newline;
|
||||
while ((newline = stdoutBuffer.indexOf("\n")) >= 0) {
|
||||
const line = stdoutBuffer.slice(0, newline).trim();
|
||||
stdoutBuffer = stdoutBuffer.slice(newline + 1);
|
||||
if (!line) continue;
|
||||
let message;
|
||||
try {
|
||||
message = JSON.parse(line);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
if (message.id && pending.has(message.id)) {
|
||||
const { resolve, reject } = pending.get(message.id);
|
||||
pending.delete(message.id);
|
||||
if (message.error) reject(new Error(JSON.stringify(message.error)));
|
||||
else resolve(message.result);
|
||||
const fetch = textFrom(requireToolSuccess("fetch_url/http", await client.callTool("fetch_url", {
|
||||
url: "https://example.com/",
|
||||
format: "markdown",
|
||||
max_download_bytes: 52428800
|
||||
})));
|
||||
if (!fetch.includes("Example Domain")) throw new Error(`fetch smoke returned unexpected text: ${fetch.slice(0, 500)}`);
|
||||
|
||||
const browserFetch = textFrom(requireToolSuccess("fetch_url/browser", await client.callTool("fetch_url", {
|
||||
url: "https://example.com/",
|
||||
format: "markdown",
|
||||
engine: "browser",
|
||||
max_download_bytes: 52428800
|
||||
})));
|
||||
if (!browserFetch.includes("Example Domain")) throw new Error(`browser fetch smoke returned unexpected text: ${browserFetch.slice(0, 500)}`);
|
||||
|
||||
result.searxng = "pass";
|
||||
result.bing = "pass";
|
||||
result.fetch_url = "pass";
|
||||
result.fetch_url_browser_engine_currently_http = "pass";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
});
|
||||
|
||||
function request(method, params = {}) {
|
||||
if (childExited) {
|
||||
return Promise.reject(new Error(`MCP child already exited. stderr: ${stderrBuffer.slice(-2000)}`));
|
||||
}
|
||||
const id = nextId++;
|
||||
child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id, method, params })}\n`);
|
||||
return new Promise((resolve, reject) => pending.set(id, { resolve, reject }));
|
||||
}
|
||||
|
||||
function notify(method, params = {}) {
|
||||
child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", method, params })}\n`);
|
||||
}
|
||||
|
||||
function textFrom(result) {
|
||||
return (result.content || [])
|
||||
.filter(part => part.type === "text")
|
||||
.map(part => part.text)
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
async function callTool(name, args = {}) {
|
||||
return request("tools/call", { name, arguments: args });
|
||||
}
|
||||
|
||||
try {
|
||||
await request("initialize", {
|
||||
protocolVersion: "2024-11-05",
|
||||
capabilities: {},
|
||||
clientInfo: { name: "context-kit-smoke", version: "0.0.0" }
|
||||
});
|
||||
notify("notifications/initialized");
|
||||
|
||||
const listed = await request("tools/list");
|
||||
const toolNames = new Set((listed.tools || []).map(tool => tool.name));
|
||||
for (const name of ["search_web", "fetch_url"]) {
|
||||
if (!toolNames.has(name)) throw new Error(`missing tool: ${name}`);
|
||||
}
|
||||
|
||||
const searxng = textFrom(await callTool("search_web", {
|
||||
q: "Model Context Protocol",
|
||||
limit: 2,
|
||||
provider: "searxng"
|
||||
}));
|
||||
if (!searxng.includes("Model")) throw new Error(`SearXNG smoke returned unexpected text: ${searxng.slice(0, 500)}`);
|
||||
|
||||
const bing = textFrom(await callTool("search_web", {
|
||||
q: "Model Context Protocol",
|
||||
limit: 2,
|
||||
provider: "bing"
|
||||
}));
|
||||
if (!bing.includes("Model")) throw new Error(`Bing smoke returned unexpected text: ${bing.slice(0, 500)}`);
|
||||
|
||||
const fetch = textFrom(await callTool("fetch_url", {
|
||||
url: "https://example.com/",
|
||||
format: "markdown",
|
||||
max_download_bytes: 52428800
|
||||
}));
|
||||
if (!fetch.includes("Example Domain")) throw new Error(`fetch smoke returned unexpected text: ${fetch.slice(0, 500)}`);
|
||||
|
||||
const browserFetch = textFrom(await callTool("fetch_url", {
|
||||
url: "https://example.com/",
|
||||
format: "markdown",
|
||||
engine: "browser",
|
||||
max_download_bytes: 52428800
|
||||
}));
|
||||
if (!browserFetch.includes("Example Domain")) throw new Error(`browser fetch smoke returned unexpected text: ${browserFetch.slice(0, 500)}`);
|
||||
|
||||
const localResult = await callTool("fetch_url", {
|
||||
url: "http://127.0.0.1:1/",
|
||||
max_download_bytes: 52428800
|
||||
});
|
||||
const localBlocked = Boolean(localResult.isError) && textFrom(localResult).includes("Blocked localhost/private URL");
|
||||
if (!localBlocked) throw new Error("localhost/private URL was not blocked as expected");
|
||||
|
||||
clearTimeout(timeout);
|
||||
await stopChild();
|
||||
console.log(JSON.stringify({
|
||||
tools: Array.from(toolNames).sort(),
|
||||
searxng: "pass",
|
||||
bing: "pass",
|
||||
fetch_url: "pass",
|
||||
fetch_url_browser_engine_currently_http: "pass",
|
||||
localhost_guard: "pass"
|
||||
}, null, 2));
|
||||
} catch (error) {
|
||||
clearTimeout(timeout);
|
||||
await stopChild();
|
||||
console.error(error.message);
|
||||
if (stderrBuffer) console.error(stderrBuffer.slice(-4000));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user