@mirsella/context7-account-broker 0.3.0 → 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,15 +55,17 @@ 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 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.
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
- 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.
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
70
  context7-account-broker accounts list
41
71
  context7-account-broker accounts add NAME
@@ -56,7 +86,7 @@ The command prints each account's used and total requests, percentage used, bloc
56
86
 
57
87
  ## Architecture
58
88
 
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.
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.
60
90
 
61
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.
62
92
 
@@ -66,6 +96,15 @@ Default private state:
66
96
  - Broker state: `${XDG_CONFIG_HOME:-$HOME/.config}/context7-account-broker/<version>/broker.json`
67
97
  - Cache and log: `${XDG_CACHE_HOME:-$HOME/.cache}/context7-account-broker/<version>/`
68
98
 
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
+ ```
107
+
69
108
  ## License
70
109
 
71
110
  MIT
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mirsella/context7-account-broker",
3
- "version": "0.3.0",
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"
@@ -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"
@@ -4,50 +4,17 @@ import { fileURLToPath } from "node:url";
4
4
  const BINARY = fileURLToPath(new URL("../vendor/context7-account-broker", import.meta.url));
5
5
  const MCP_NAME = "context7-broker";
6
6
 
7
- type NativeLaunch = {
8
- url: string;
9
- token: string;
10
- };
11
-
12
- export function parseNativeLaunch(stdout: string): NativeLaunch {
13
- const value: unknown = JSON.parse(stdout);
14
- if (!value || typeof value !== "object" || Array.isArray(value)) {
15
- throw new Error("context7-account-broker start returned invalid output");
16
- }
17
- const launch = value as Record<string, unknown>;
18
- const port = typeof launch.url === "string" ? /^http:\/\/127\.0\.0\.1:([1-9]\d{0,4})\/mcp$/.exec(launch.url)?.[1] : undefined;
19
- if (
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)
25
- ) {
26
- throw new Error("context7-account-broker start returned invalid output");
27
- }
28
- return launch as NativeLaunch;
29
- }
30
-
31
- export const server: Plugin = async ({ $ }) => ({
7
+ export const server: Plugin = async () => ({
32
8
  config: async (config) => {
33
9
  if (config.mcp?.[MCP_NAME] !== undefined) {
34
10
  throw new Error(`@mirsella/context7-account-broker cannot replace the existing mcp.${MCP_NAME} configuration`);
35
11
  }
36
- const result = await $`${[BINARY, "start"]}`.quiet().nothrow();
37
- if (result.exitCode !== 0) {
38
- const stderr = String(result.stderr).trim();
39
- throw new Error(`context7-account-broker start failed with exit code ${result.exitCode}${stderr ? `: ${stderr}` : ""}`);
40
- }
41
- const launch = parseNativeLaunch(String(result.stdout));
42
12
  config.mcp ??= {};
43
13
  config.mcp[MCP_NAME] = {
44
- type: "remote",
45
- url: launch.url,
46
- oauth: false,
14
+ type: "local",
15
+ command: [BINARY, "mcp"],
16
+ enabled: true,
47
17
  timeout: 30_000,
48
- headers: {
49
- Authorization: `Bearer ${launch.token}`,
50
- },
51
18
  };
52
19
  },
53
20
  });
Binary file