@mirsella/context7-account-broker 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -27,9 +27,9 @@ That is the complete OpenCode configuration. Do not add an `mcp.context7-broker`
27
27
 
28
28
  ## Lifecycle
29
29
 
30
- When OpenCode loads the plugin, the packaged Linux x64 binary runs `start`. It creates a private server token if needed, reuses an authenticated healthy broker on `127.0.0.1:14197`, or launches the shared broker and waits briefly for readiness. The plugin reads the token into OpenCode's in-memory MCP configuration; it never writes the token to `opencode.json` or passes it in process arguments.
30
+ When OpenCode loads the plugin, the packaged Linux x64 binary runs `start`. It creates private runtime state for that package version, reuses its matching healthy broker, or launches one on an available loopback port. Each broker process gets a fresh token, which `start` returns directly to the plugin for OpenCode's in-memory MCP configuration. The token is never written to `opencode.json` or passed in process arguments.
31
31
 
32
- The broker stays available across project and session lifetimes. Its containing process manager may stop it with OpenCode; a later plugin launch reuses it when healthy or starts a replacement when it is absent. There is no supervisor, heartbeat, separate systemd unit, PATH installation, manual token, or last-client shutdown.
32
+ OpenCode processes using the same plugin version share one broker. Separate OpenCode processes can use different plugin versions at the same time because each version has its own port, token, cache, and broker process. A broker stays available across project and session lifetimes; its containing process manager may stop it with OpenCode. There is no supervisor, heartbeat, separate systemd unit, PATH installation, manual token, or last-client shutdown.
33
33
 
34
34
  Startup makes no Context7 requests. Context7 is contacted only for tool calls and the explicit `status` command.
35
35
 
@@ -37,7 +37,6 @@ Startup makes no Context7 requests. Context7 is contacted only for tool calls an
37
37
 
38
38
  ```text
39
39
  context7-account-broker start
40
- context7-account-broker serve
41
40
  context7-account-broker accounts list
42
41
  context7-account-broker accounts add NAME
43
42
  context7-account-broker accounts remove NAME
@@ -47,19 +46,25 @@ context7-account-broker config
47
46
 
48
47
  Use `bunx @mirsella/context7-account-broker ...` when the package is not otherwise installed.
49
48
 
49
+ ### Check account quotas
50
+
51
+ ```sh
52
+ bunx @mirsella/context7-account-broker status
53
+ ```
54
+
55
+ The command prints each account's used and total requests, percentage used, blocked state, and UTC reset time. It makes one Context7 request per configured account to read the current quota headers; the broker does not probe quotas in the background.
56
+
50
57
  ## Architecture
51
58
 
52
- The plugin contains a static `x86_64-unknown-linux-musl` binary. One current-thread Tokio process exposes authenticated Streamable HTTP MCP on loopback. It forwards requests directly to the official Context7 REST API with bounded concurrency four.
59
+ The plugin contains a static `x86_64-unknown-linux-musl` binary. Each active package version runs one current-thread Tokio process exposing authenticated Streamable HTTP MCP on an OS-assigned loopback port. It forwards requests directly to the official Context7 REST API with bounded concurrency four.
53
60
 
54
- The broker provides the canonical `resolve-library-id` and `query-docs` tools. It routes toward account affinity and lower quota use, rotates unknown quota fairly, fails over account-specific `401`, `403`, and `429` responses, and caps shared endpoint failures at two attempts. Successful results use an account-partitioned disk cache. Identical typed requests share one cancellation-safe in-flight producer.
61
+ The broker provides the canonical `resolve-library-id` and `query-docs` tools. It routes toward account affinity and lower quota use, rotates unknown quota fairly, fails over account-specific `401`, `402`, `403`, and `429` responses, and caps shared endpoint failures at two attempts. Successful results use an account-partitioned disk cache. Identical typed requests share one cancellation-safe in-flight producer.
55
62
 
56
63
  Default private state:
57
64
 
58
- - Accounts: `$XDG_CONFIG_HOME/context7-account-broker/accounts.json`
59
- - Server token: `$XDG_CONFIG_HOME/context7-account-broker/server-token`
60
- - Cache and launch log: `$XDG_CACHE_HOME/context7-account-broker/`
61
-
62
- Optional environment settings are `CONTEXT7_BROKER_CONFIG`, `CONTEXT7_API_KEY`, `CONTEXT7_API_KEYS`, `CONTEXT7_BROKER_INCLUDE_ENV`, `CONTEXT7_BROKER_TOKEN_FILE`, `CONTEXT7_CACHE_DIR`, `CONTEXT7_CACHE_TTL_DAYS`, `CONTEXT7_ACCOUNT_COOLDOWN_MS`, and `CONTEXT7_BROKER_PORT`.
65
+ - Accounts: `${XDG_CONFIG_HOME:-$HOME/.config}/context7-account-broker/accounts.json`
66
+ - Broker state: `${XDG_CONFIG_HOME:-$HOME/.config}/context7-account-broker/<version>/broker.json`
67
+ - Cache and log: `${XDG_CACHE_HOME:-$HOME/.cache}/context7-account-broker/<version>/`
63
68
 
64
69
  ## License
65
70
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mirsella/context7-account-broker",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Linux x64 multi-account Context7 plugin for OpenCode",
5
5
  "type": "module",
6
6
  "exports": {
@@ -17,7 +17,7 @@
17
17
  ],
18
18
  "scripts": {
19
19
  "build:native": "cargo build --locked --release --target x86_64-unknown-linux-musl",
20
- "stage:native": "node scripts/stage-native.mjs",
20
+ "stage:native": "install -Dm755 target/x86_64-unknown-linux-musl/release/context7-account-broker vendor/context7-account-broker",
21
21
  "test": "cargo test --locked && bun test tests/plugin.test.ts",
22
22
  "prepack": "pnpm build:native && pnpm stage:native"
23
23
  },
@@ -1,5 +1,4 @@
1
1
  import type { Plugin, PluginModule } from "@opencode-ai/plugin";
2
- import { readFile } from "node:fs/promises";
3
2
  import { fileURLToPath } from "node:url";
4
3
 
5
4
  const BINARY = fileURLToPath(new URL("../vendor/context7-account-broker", import.meta.url));
@@ -7,70 +6,47 @@ const MCP_NAME = "context7-broker";
7
6
 
8
7
  type NativeLaunch = {
9
8
  url: string;
10
- tokenFile: string;
9
+ token: string;
11
10
  };
12
11
 
13
12
  export function parseNativeLaunch(stdout: string): NativeLaunch {
14
- let value: unknown;
15
- try {
16
- value = JSON.parse(stdout);
17
- } catch {
18
- throw new Error("context7-account-broker start returned invalid JSON");
19
- }
13
+ const value: unknown = JSON.parse(stdout);
20
14
  if (!value || typeof value !== "object" || Array.isArray(value)) {
21
- throw new Error("context7-account-broker start returned an invalid launch object");
15
+ throw new Error("context7-account-broker start returned invalid output");
22
16
  }
23
17
  const launch = value as Record<string, unknown>;
24
- if (Object.keys(launch).sort().join(",") !== "tokenFile,url") {
25
- throw new Error("context7-account-broker start returned unexpected fields");
26
- }
27
- if (typeof launch.url !== "string" || typeof launch.tokenFile !== "string") {
28
- throw new Error("context7-account-broker start returned invalid launch fields");
29
- }
30
- const url = new URL(launch.url);
18
+ const port = typeof launch.url === "string" ? /^http:\/\/127\.0\.0\.1:([1-9]\d{0,4})\/mcp$/.exec(launch.url)?.[1] : undefined;
31
19
  if (
32
- url.protocol !== "http:" ||
33
- url.hostname !== "127.0.0.1" ||
34
- !url.port ||
35
- url.pathname !== "/mcp" ||
36
- url.username ||
37
- url.password ||
38
- url.search ||
39
- url.hash ||
40
- !launch.tokenFile.startsWith("/")
20
+ Object.keys(launch).sort().join(",") !== "token,url" ||
21
+ !port ||
22
+ Number(port) > 65_535 ||
23
+ typeof launch.token !== "string" ||
24
+ !/^[0-9a-f]{64}$/.test(launch.token)
41
25
  ) {
42
- throw new Error("context7-account-broker start returned an unsafe launch target");
26
+ throw new Error("context7-account-broker start returned invalid output");
43
27
  }
44
- return { url: launch.url, tokenFile: launch.tokenFile };
28
+ return launch as NativeLaunch;
45
29
  }
46
30
 
47
31
  export const server: Plugin = async ({ $ }) => ({
48
32
  config: async (config) => {
49
33
  if (config.mcp?.[MCP_NAME] !== undefined) {
50
- throw new Error(
51
- `@mirsella/context7-account-broker cannot replace the existing mcp.${MCP_NAME} configuration`,
52
- );
34
+ throw new Error(`@mirsella/context7-account-broker cannot replace the existing mcp.${MCP_NAME} configuration`);
53
35
  }
54
36
  const result = await $`${[BINARY, "start"]}`.quiet().nothrow();
55
37
  if (result.exitCode !== 0) {
56
38
  const stderr = String(result.stderr).trim();
57
- throw new Error(
58
- `context7-account-broker start failed with exit code ${result.exitCode}${stderr ? `: ${stderr}` : ""}`,
59
- );
39
+ throw new Error(`context7-account-broker start failed with exit code ${result.exitCode}${stderr ? `: ${stderr}` : ""}`);
60
40
  }
61
41
  const launch = parseNativeLaunch(String(result.stdout));
62
- const token = (await readFile(launch.tokenFile, "utf8")).trim();
63
- if (!/^[0-9a-f]{64}$/.test(token)) {
64
- throw new Error("context7-account-broker server token is invalid");
65
- }
66
42
  config.mcp ??= {};
67
43
  config.mcp[MCP_NAME] = {
68
44
  type: "remote",
69
45
  url: launch.url,
70
- enabled: true,
46
+ oauth: false,
71
47
  timeout: 30_000,
72
48
  headers: {
73
- Authorization: `Bearer ${token}`,
49
+ Authorization: `Bearer ${launch.token}`,
74
50
  },
75
51
  };
76
52
  },
Binary file