umpirix/scripts/mcp-stdio.mjs
caelum0x c6c9901c6a feat(oss): GOVERNANCE.md, Scribase-family branding, MCP serverInfo/tools fixes
- Add GOVERNANCE.md (BDFL-style, referenced from CONTRIBUTING.md).
- Scribase family branding: "Umpirix — an open-source project by Scribase"
  header line in README (linking scribase.com and scribase.com/decisions),
  a "Run it on Scribase" section next to the self-host instructions in
  README and SELF-HOSTING.md (self-hosting stays fully supported, nothing
  gated), the same line appended to every package's npm/PyPI description
  (root, sdk/js, sdk/js-lite, sdk/ai-sdk, sdk/scribase-decisions, skills,
  adapters/python, sdk/python, otsone), and the site's per-page footer
  brand line (10 routes) + roadmap prose, which still said the pre-rebrand
  "OpenJev · Apache-2.0" - now "Umpirix · Apache-2.0 · scribase.com".
- Fix two real gaps in the MCP server found in a fresh clone: serverInfo
  still said name "openjev" / title "OpenJev typed decisions"
  (src/lib/jev/mcp.ts) and the stdio bridge's log prefix was still
  "[openjev mcp]" (scripts/mcp-stdio.mjs). Both now say Umpirix. Verified
  live over stdio (plain node; Docker/colima unavailable in this
  environment) that `initialize` reports serverInfo.name "umpirix" /
  title "Umpirix typed decisions", and `tools/list` returns the real tool
  set: choice, score, noul, decide (no "cascade").
- npm run typecheck / npm test (138/138) / npm run gold (61/61) / npm run
  lint (0 errors, pre-existing warnings only) all green after this change.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-11 19:15:28 +03:00

98 lines
3.6 KiB
JavaScript

/**
* `openjev mcp` — Model Context Protocol over stdio.
*
* A thin bridge for MCP clients that launch a local command (Claude Code,
* Cursor, ...): each newline-delimited JSON-RPC message read from stdin is
* forwarded to a running OpenJev server's `POST /v1/mcp`, and the reply is
* written to stdout as one line. Nothing but JSON-RPC goes to stdout; logs go
* to stderr.
*
* Environment:
* OPENJEV_BASE_URL server to forward to (default http://localhost:3000)
* OPENJEV_API_KEY optional; sent as a Bearer token when set
*/
import { createInterface } from "node:readline";
const DEFAULT_BASE_URL = "http://localhost:3000";
/** `${base}/v1/mcp`, tolerating a trailing slash or a base that already ends in /v1. */
export function mcpEndpoint(baseUrl) {
const base = (baseUrl || DEFAULT_BASE_URL).trim().replace(/\/+$/, "").replace(/\/v1$/, "");
return `${base}/v1/mcp`;
}
function log(message) {
process.stderr.write(`[umpirix mcp] ${message}\n`);
}
/** The id of a JSON-RPC request, or undefined for a notification or unparseable input. */
function requestId(message) {
return message && typeof message === "object" && !Array.isArray(message) ? message.id : undefined;
}
function rpcError(id, code, message) {
return { jsonrpc: "2.0", id: id ?? null, error: { code, message } };
}
/** Forward one message; resolve to the reply object, or null when there is none. */
async function forward(endpoint, headers, line) {
let message;
try {
message = JSON.parse(line);
} catch {
return rpcError(null, -32700, "Parse error: input line is not JSON.");
}
const id = requestId(message);
let response;
try {
response = await fetch(endpoint, { method: "POST", headers, body: JSON.stringify(message) });
} catch (err) {
const reason = err instanceof Error ? err.message : String(err);
log(`cannot reach ${endpoint}: ${reason}`);
if (id === undefined && !Array.isArray(message)) return null;
return rpcError(
id,
-32000,
`OpenJev server not reachable at ${endpoint}. Start it (npm run dev, openjev serve or docker compose up) or set OPENJEV_BASE_URL.`,
);
}
if (response.status === 202 || response.status === 204) return null;
const text = await response.text();
try {
const reply = JSON.parse(text);
// A JSON-RPC reply (or batch) passes through; any other JSON is an HTTP-level error.
if (Array.isArray(reply) || (reply && reply.jsonrpc === "2.0")) return reply;
const detail = reply?.error?.message ?? text.slice(0, 200);
return id === undefined ? null : rpcError(id, -32000, `OpenJev server answered ${response.status}: ${detail}`);
} catch {
return id === undefined
? null
: rpcError(id, -32000, `OpenJev server answered ${response.status} with a non-JSON body.`);
}
}
export async function runMcpStdio(env = process.env) {
const endpoint = mcpEndpoint(env.OPENJEV_BASE_URL);
const key = env.OPENJEV_API_KEY?.trim();
const headers = {
"content-type": "application/json",
accept: "application/json",
...(key ? { authorization: `Bearer ${key}` } : {}),
};
log(`forwarding stdio to ${endpoint}`);
// Messages are handled one at a time so replies keep the order of the requests.
let queue = Promise.resolve();
const lines = createInterface({ input: process.stdin, crlfDelay: Infinity });
lines.on("line", (raw) => {
const line = raw.trim();
if (!line) return;
queue = queue.then(async () => {
const reply = await forward(endpoint, headers, line);
if (reply !== null) process.stdout.write(`${JSON.stringify(reply)}\n`);
});
});
await new Promise((resolve) => lines.once("close", resolve));
await queue;
}