Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { readProxyInstanceFile } from "./instance.js";
import { isPidAlive, isProxyInstanceFile, readProxyInstanceFile } from "./instance.js";

const VERSION = (() => {
try {
Expand All @@ -33,12 +33,27 @@ type McpToolDef = {
// BILI_CONVERSATION_ID (launcher-spawned hosts like codex) has no matching
// request id — binding is headless (next NEW session).
const DEFAULT_PROXY_ORIGIN = "http://127.0.0.1:8787";
let warnedStaleRecord = false;

export function resolveProxyOrigin(): string {
const fromEnv = process.env.BILI_MCP_PROXY?.trim();
if (fromEnv && fromEnv.length > 0) return fromEnv;
const discovered = readProxyInstanceFile();
if (discovered && /^https?:\/\/\S+$/.test(discovered.origin)) return discovered.origin;
if (discovered && /^https?:\/\/\S+$/.test(discovered.origin)) {
// #405: a JSON record naming a dead writer pid points at a port nothing
// listens on — skip it for the default origin instead of dialing it
// forever. The `pid > 0` guard is deliberate: pid 0 (foreign writer)
// and legacy plain-URL pointers make no liveness claim and keep the
// old trust semantics (unlike plugin-install, which refuses them).
if (isProxyInstanceFile(discovered) && discovered.pid > 0 && !isPidAlive(discovered.pid)) {
if (!warnedStaleRecord) {
warnedStaleRecord = true;
process.stderr.write(`[bili-mcp] recorded bili proxy (${discovered.origin}, pid ${discovered.pid}) is not running — falling back to ${DEFAULT_PROXY_ORIGIN}; set BILI_MCP_PROXY if your proxy listens elsewhere\n`);
}
return DEFAULT_PROXY_ORIGIN;
}
return discovered.origin;
}
return DEFAULT_PROXY_ORIGIN;
}

Expand Down
32 changes: 32 additions & 0 deletions tests/instance-governance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,38 @@ test("resolveProxyOrigin: env wins, JSON file parsed, default fallback", () => {
}
});

test("resolveProxyOrigin: dead-pid record falls back to default origin, live pid and pid 0 trusted (#405)", () => {
const st = tmpStateDir();
const prevEnv = process.env.BILI_MCP_PROXY;
const origStderrWrite = process.stderr.write;
const stderrOut: string[] = [];
process.stderr.write = ((chunk: string | Uint8Array) => {
stderrOut.push(String(chunk));
return true;
}) as typeof process.stderr.write;
try {
delete process.env.BILI_MCP_PROXY;

atomicWriteInstanceFile(sampleInstance({ origin: "http://127.0.0.1:4242", pid: deadPid() }));
assert.equal(resolveProxyOrigin(), "http://127.0.0.1:8787", "dead-pid record skipped");
assert.equal(resolveProxyOrigin(), "http://127.0.0.1:8787", "re-resolves on every call");
const notes = stderrOut.filter((l) => l.includes("is not running"));
assert.equal(notes.length, 1, "one-time stderr note");
assert.match(notes[0], /BILI_MCP_PROXY/);

atomicWriteInstanceFile(sampleInstance({ origin: "http://127.0.0.1:4242" }));
assert.equal(resolveProxyOrigin(), "http://127.0.0.1:4242", "live pid trusted");

atomicWriteInstanceFile(sampleInstance({ origin: "http://127.0.0.1:4242", pid: 0 }));
assert.equal(resolveProxyOrigin(), "http://127.0.0.1:4242", "pid 0 makes no liveness claim");
} finally {
process.stderr.write = origStderrWrite;
if (prevEnv === undefined) delete process.env.BILI_MCP_PROXY;
else process.env.BILI_MCP_PROXY = prevEnv;
st.restore();
}
});

test("isPidAlive: self alive, dead pid not", () => {
assert.equal(isPidAlive(process.pid), true);
assert.equal(isPidAlive(deadPid()), false);
Expand Down
Loading