@mirsella/context7-account-broker 0.2.2 → 0.4.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
@@ -1,6 +1,6 @@
1
1
  # context7-account-broker
2
2
 
3
- An OpenCode plugin that runs one local Context7 MCP broker across projects and shares quota, caching, affinity, and in-flight requests across configured API keys.
3
+ A Context7 MCP server that shares accounts, quota routing, caching, affinity, and in-flight requests across projects and coding agents. Use its native stdio command with any MCP client, or its OpenCode plugin for automatic registration.
4
4
 
5
5
  > [!IMPORTANT]
6
6
  > This package supports Linux x86-64 only. macOS, Windows, Linux ARM64, and other platforms are not supported.
@@ -10,12 +10,40 @@ An OpenCode plugin that runs one local Context7 MCP broker across projects and s
10
10
  Add at least one Context7 account:
11
11
 
12
12
  ```sh
13
- bunx @mirsella/context7-account-broker accounts add personal
13
+ context7-account-broker accounts add personal
14
14
  ```
15
15
 
16
16
  The command reads the API key from a hidden terminal prompt and stores it in a private `accounts.json` file.
17
17
 
18
- Then add the plugin to `opencode.json`:
18
+ ### Any MCP client
19
+
20
+ Configure a local stdio server with command `context7-account-broker` and arguments `["mcp"]`. For clients that use `mcpServers`:
21
+
22
+ ```json
23
+ {
24
+ "mcpServers": {
25
+ "context7-broker": {
26
+ "command": "context7-account-broker",
27
+ "args": ["mcp"]
28
+ }
29
+ }
30
+ }
31
+ ```
32
+
33
+ Codex uses the same command:
34
+
35
+ ```toml
36
+ [mcp_servers.context7-broker]
37
+ command = "context7-account-broker"
38
+ args = ["mcp"]
39
+ tool_timeout_sec = 30
40
+ ```
41
+
42
+ The command starts or reuses the shared broker. It speaks MCP on stdin/stdout; diagnostics go to stderr. Accounts and server tokens stay outside the client's configuration.
43
+
44
+ ### OpenCode
45
+
46
+ Add the plugin to `opencode.json`:
19
47
 
20
48
  ```json
21
49
  {
@@ -27,17 +55,18 @@ That is the complete OpenCode configuration. Do not add an `mcp.context7-broker`
27
55
 
28
56
  ## Lifecycle
29
57
 
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.
58
+ Each `mcp` process starts or reuses a healthy broker for its package version, then connects to its authenticated loopback HTTP endpoint. The native command handles authentication internally. The OpenCode plugin registers this same stdio command.
31
59
 
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.
60
+ Clients using the same version and XDG configuration/cache directories share the broker and its state. Different versions use separate ports, tokens, and caches. Disconnecting a client closes its stdio transport; the shared broker remains available. A containing process manager may stop it with the harness. No separate service setup is needed.
33
61
 
62
+ The `start` command remains available for direct HTTP clients and prints connection JSON. Keep that output private: it includes the broker token.
34
63
  Startup makes no Context7 requests. Context7 is contacted only for tool calls and the explicit `status` command.
35
64
 
36
65
  ## Commands
37
66
 
38
67
  ```text
68
+ context7-account-broker mcp
39
69
  context7-account-broker start
40
- context7-account-broker serve
41
70
  context7-account-broker accounts list
42
71
  context7-account-broker accounts add NAME
43
72
  context7-account-broker accounts remove NAME
@@ -57,17 +86,24 @@ The command prints each account's used and total requests, percentage used, bloc
57
86
 
58
87
  ## Architecture
59
88
 
60
- 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.
89
+ The npm package contains a static `x86_64-unknown-linux-musl` binary. Each active version runs one current-thread Tokio broker exposing authenticated Streamable HTTP MCP on an OS-assigned loopback port. Each client runs a stdio process backed by the Rust MCP SDK. Requests can overlap; the shared broker limits upstream Context7 REST API calls to four at a time.
61
90
 
62
- 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.
91
+ 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.
63
92
 
64
93
  Default private state:
65
94
 
66
- - Accounts: `$XDG_CONFIG_HOME/context7-account-broker/accounts.json`
67
- - Server token: `$XDG_CONFIG_HOME/context7-account-broker/server-token`
68
- - Cache and launch log: `$XDG_CACHE_HOME/context7-account-broker/`
95
+ - Accounts: `${XDG_CONFIG_HOME:-$HOME/.config}/context7-account-broker/accounts.json`
96
+ - Broker state: `${XDG_CONFIG_HOME:-$HOME/.config}/context7-account-broker/<version>/broker.json`
97
+ - Cache and log: `${XDG_CACHE_HOME:-$HOME/.cache}/context7-account-broker/<version>/`
69
98
 
70
- 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`.
99
+ ## Development and Nix
100
+
101
+ The Rust source owns both transports, startup, account-file handling, and tests. `nix/package.nix` builds the native command from this repository's `Cargo.lock` and wraps its CA bundle. Consumers can call that expression with their own Nixpkgs; no external source patches or transport wrappers are needed.
102
+
103
+ ```sh
104
+ cargo test --locked
105
+ bun test tests/plugin.test.ts
106
+ ```
71
107
 
72
108
  ## License
73
109
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mirsella/context7-account-broker",
3
- "version": "0.2.2",
4
- "description": "Linux x64 multi-account Context7 plugin for OpenCode",
3
+ "version": "0.4.0",
4
+ "description": "Shared multi-account Context7 MCP for Linux x64",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  "./server": "./plugin/context7.ts"
@@ -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
  },
@@ -40,9 +40,6 @@
40
40
  "cpu": [
41
41
  "x64"
42
42
  ],
43
- "engines": {
44
- "opencode": ">=1.18.0"
45
- },
46
43
  "packageManager": "pnpm@10.14.0",
47
44
  "devDependencies": {
48
45
  "@opencode-ai/plugin": "1.18.11"
@@ -1,77 +1,20 @@
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));
6
5
  const MCP_NAME = "context7-broker";
7
6
 
8
- type NativeLaunch = {
9
- url: string;
10
- tokenFile: string;
11
- };
12
-
13
- 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
- }
20
- if (!value || typeof value !== "object" || Array.isArray(value)) {
21
- throw new Error("context7-account-broker start returned an invalid launch object");
22
- }
23
- 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);
31
- 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("/")
41
- ) {
42
- throw new Error("context7-account-broker start returned an unsafe launch target");
43
- }
44
- return { url: launch.url, tokenFile: launch.tokenFile };
45
- }
46
-
47
- export const server: Plugin = async ({ $ }) => ({
7
+ export const server: Plugin = async () => ({
48
8
  config: async (config) => {
49
9
  if (config.mcp?.[MCP_NAME] !== undefined) {
50
- throw new Error(
51
- `@mirsella/context7-account-broker cannot replace the existing mcp.${MCP_NAME} configuration`,
52
- );
53
- }
54
- const result = await $`${[BINARY, "start"]}`.quiet().nothrow();
55
- if (result.exitCode !== 0) {
56
- const stderr = String(result.stderr).trim();
57
- throw new Error(
58
- `context7-account-broker start failed with exit code ${result.exitCode}${stderr ? `: ${stderr}` : ""}`,
59
- );
60
- }
61
- 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");
10
+ throw new Error(`@mirsella/context7-account-broker cannot replace the existing mcp.${MCP_NAME} configuration`);
65
11
  }
66
12
  config.mcp ??= {};
67
13
  config.mcp[MCP_NAME] = {
68
- type: "remote",
69
- url: launch.url,
14
+ type: "local",
15
+ command: [BINARY, "mcp"],
70
16
  enabled: true,
71
17
  timeout: 30_000,
72
- headers: {
73
- Authorization: `Bearer ${token}`,
74
- },
75
18
  };
76
19
  },
77
20
  });
Binary file