Skip to content

Commit a9b0b03

Browse files
committed
feat(mcp): read server version from package.json at runtime
1 parent ca476dc commit a9b0b03

3 files changed

Lines changed: 62 additions & 60 deletions

File tree

‎dist/index.js‎

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21484,7 +21484,8 @@ class StdioServerTransport {
2148421484
}
2148521485

2148621486
// src/index.ts
21487-
import { resolve as resolve2 } from "path";
21487+
import { readFileSync } from "fs";
21488+
import { join as join2, resolve as resolve2 } from "path";
2148821489

2148921490
// src/config.ts
2149021491
import { existsSync } from "fs";
@@ -21971,65 +21972,10 @@ async function waitTask(task, waitSec) {
2197121972
await Promise.race([task.promise, new Promise((r) => timer = setTimeout(r, waitSec * 1000))]);
2197221973
clearTimeout(timer);
2197321974
}
21974-
// package.json
21975-
var package_default = {
21976-
name: "claude-opencode-delegate",
21977-
version: "0.1.0",
21978-
description: "Claude Code plugin \u2014 delegate tasks to any OpenCode model (Grok, Kimi, GLM, Qwen, DeepSeek\u2026) via agent activation, while Claude stays the orchestrator.",
21979-
type: "module",
21980-
license: "MIT",
21981-
author: "aashahin",
21982-
repository: {
21983-
type: "git",
21984-
url: "git+https://github.com/aashahin/claude-opencode-delegate.git"
21985-
},
21986-
keywords: [
21987-
"claude",
21988-
"claude-code",
21989-
"opencode",
21990-
"subagent",
21991-
"delegate"
21992-
],
21993-
files: [
21994-
"dist",
21995-
"src",
21996-
"skills",
21997-
"agents",
21998-
"commands",
21999-
".claude-plugin",
22000-
"README.md",
22001-
"LICENSE"
22002-
],
22003-
engines: {
22004-
bun: ">=1.1"
22005-
},
22006-
scripts: {
22007-
start: "bun src/index.ts",
22008-
build: "bun build src/index.ts --target=bun --outfile=dist/index.js",
22009-
typecheck: "tsc --noEmit",
22010-
test: "bun test",
22011-
lint: "eslint . --max-warnings 0",
22012-
fmt: 'prettier --check "**/*.{ts,js,json,md,yml,yaml}"',
22013-
"fmt:fix": 'prettier --write "**/*.{ts,js,json,md,yml,yaml}"',
22014-
verify: "bun run typecheck && bun test && bun run lint && bun run fmt"
22015-
},
22016-
dependencies: {
22017-
"@modelcontextprotocol/sdk": "^1.30.0",
22018-
zod: "^4.6.5"
22019-
},
22020-
devDependencies: {
22021-
"@eslint/js": "^10.0.1",
22022-
"@types/bun": "^1.4.2",
22023-
eslint: "^10.13.0",
22024-
"eslint-config-prettier": "^10.1.8",
22025-
prettier: "^3.9.10",
22026-
typescript: "~5.9.0",
22027-
"typescript-eslint": "^8.71.1"
22028-
}
22029-
};
2203021975

2203121976
// src/index.ts
22032-
var server = new McpServer({ name: "opencode-delegate", version: package_default.version });
21977+
var pkg = JSON.parse(readFileSync(join2(import.meta.dir, "..", "package.json"), "utf8"));
21978+
var server = new McpServer({ name: "opencode-delegate", version: pkg.version });
2203321979
var text = (t, isError = false) => ({
2203421980
content: [{ type: "text", text: t }],
2203521981
isError

‎src/index.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
#!/usr/bin/env bun
22
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
33
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4-
import { resolve } from "node:path";
4+
import { readFileSync } from "node:fs";
5+
import { join, resolve } from "node:path";
56
import { z } from "zod";
67
import { config } from "./config.ts";
78
import { delegate, formatOutcome } from "./delegate.ts";
89
import { listModels } from "./models.ts";
910
import { execText } from "./opencode.ts";
1011
import { getTask, listTasks, startTask, waitTask, type Task } from "./tasks.ts";
11-
import pkg from "../package.json" with { type: "json" };
12+
// Read at runtime (not bundled) so release-please version bumps never make dist/ stale.
13+
const pkg = JSON.parse(readFileSync(join(import.meta.dir, "..", "package.json"), "utf8")) as {
14+
version: string;
15+
};
1216

1317
const server = new McpServer({ name: "opencode-delegate", version: pkg.version });
1418

‎test/mcp-server.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { describe, expect, test } from "bun:test";
2+
import { copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+
import { tmpdir } from "node:os";
24
import { join } from "node:path";
35

46
const root = join(import.meta.dir, "..");
@@ -66,3 +68,53 @@ describe.each(["src/index.ts", "dist/index.js"])("MCP server (%s)", (entry) => {
6668
expect(d?.inputSchema.required).toContain("prompt");
6769
}, 15000);
6870
});
71+
72+
describe("MCP server version", () => {
73+
test("serverInfo.version is read from package.json at runtime", async () => {
74+
const tmp = mkdtempSync(join(tmpdir(), "ocd-version-"));
75+
try {
76+
mkdirSync(join(tmp, "dist"));
77+
copyFileSync(join(root, "dist/index.js"), join(tmp, "dist/index.js"));
78+
writeFileSync(join(tmp, "package.json"), JSON.stringify({ name: "x", version: "9.9.9" }));
79+
const proc = Bun.spawn(["bun", join(tmp, "dist/index.js")], {
80+
stdin: "pipe",
81+
stdout: "pipe",
82+
stderr: "pipe",
83+
env: { ...process.env, OPENCODE_BIN: "/bin/true" },
84+
});
85+
proc.stdin.write(
86+
JSON.stringify({
87+
jsonrpc: "2.0",
88+
id: 1,
89+
method: "initialize",
90+
params: {
91+
protocolVersion: "2024-11-05",
92+
capabilities: {},
93+
clientInfo: { name: "test", version: "0" },
94+
},
95+
}) + "\n",
96+
);
97+
proc.stdin.flush();
98+
const reader = proc.stdout.getReader();
99+
const dec = new TextDecoder();
100+
let buf = "";
101+
try {
102+
for (;;) {
103+
const { value, done } = await reader.read();
104+
if (done)
105+
throw new Error(`server exited early: ${await new Response(proc.stderr).text()}`);
106+
buf += dec.decode(value);
107+
const line = buf.split("\n").find((l) => l.includes('"id":1'));
108+
if (line) {
109+
expect(JSON.parse(line).result.serverInfo.version).toBe("9.9.9");
110+
return;
111+
}
112+
}
113+
} finally {
114+
proc.kill();
115+
}
116+
} finally {
117+
rmSync(tmp, { recursive: true, force: true });
118+
}
119+
}, 15000);
120+
});

0 commit comments

Comments
 (0)