Skip to content
Closed
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
27 changes: 25 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, type ProxyInstanceFile } from "./instance.js";

const VERSION = (() => {
try {
Expand All @@ -34,11 +34,34 @@ type McpToolDef = {
// request id — binding is headless (next NEW session).
const DEFAULT_PROXY_ORIGIN = "http://127.0.0.1:8787";

let staleOriginNoted = false;

function noteStaleOrigin(rec: ProxyInstanceFile): void {
if (staleOriginNoted) return;
staleOriginNoted = true;
try {
process.stderr.write(
`[bili-mcp] proxy-origin record is stale (instance ${rec.instanceId.slice(0, 8)} pid ${rec.pid} not running) — falling back to ${DEFAULT_PROXY_ORIGIN}; if your proxy runs elsewhere, set BILI_MCP_PROXY (#405)\n`,
);
} catch {}
}

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 left behind by a crashed/killed proxy names a
// port nothing listens on. pid liveness is the cheap check (no HTTP
// probe on the hot path); a dead record falls back to the default
// origin — where a replacement proxy binds. pid 0 means the record
// makes no liveness claim (foreign writer) — keep the legacy trust.
if (isProxyInstanceFile(discovered) && discovered.pid > 0 && !isPidAlive(discovered.pid)) {
noteStaleOrigin(discovered);
return DEFAULT_PROXY_ORIGIN;
}
return discovered.origin;
}
return DEFAULT_PROXY_ORIGIN;
}

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

test("resolveProxyOrigin: dead-pid JSON record falls back to default, live pid honored", () => {
const st = tmpStateDir();
const prevEnv = process.env.BILI_MCP_PROXY;
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");
atomicWriteInstanceFile(sampleInstance({ origin: "http://127.0.0.1:4242", pid: process.pid }));
assert.equal(resolveProxyOrigin(), "http://127.0.0.1:4242");
atomicWriteInstanceFile(sampleInstance({ origin: "http://127.0.0.1:4242", pid: 0 }));
assert.equal(resolveProxyOrigin(), "http://127.0.0.1:4242");
} finally {
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