@mirsella/context7-account-broker 0.2.2 → 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 +7 -10
- package/package.json +2 -2
- package/plugin/context7.ts +15 -39
- package/vendor/context7-account-broker +0 -0
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
|
|
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
|
-
|
|
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
|
|
@@ -57,17 +56,15 @@ The command prints each account's used and total requests, percentage used, bloc
|
|
|
57
56
|
|
|
58
57
|
## Architecture
|
|
59
58
|
|
|
60
|
-
The plugin contains a static `x86_64-unknown-linux-musl` binary.
|
|
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.
|
|
61
60
|
|
|
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.
|
|
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.
|
|
63
62
|
|
|
64
63
|
Default private state:
|
|
65
64
|
|
|
66
|
-
- Accounts: `$XDG_CONFIG_HOME/context7-account-broker/accounts.json`
|
|
67
|
-
-
|
|
68
|
-
- Cache and
|
|
69
|
-
|
|
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`.
|
|
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>/`
|
|
71
68
|
|
|
72
69
|
## License
|
|
73
70
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mirsella/context7-account-broker",
|
|
3
|
-
"version": "0.
|
|
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": "
|
|
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
|
},
|
package/plugin/context7.ts
CHANGED
|
@@ -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
|
-
|
|
9
|
+
token: string;
|
|
11
10
|
};
|
|
12
11
|
|
|
13
12
|
export function parseNativeLaunch(stdout: string): NativeLaunch {
|
|
14
|
-
|
|
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
|
|
15
|
+
throw new Error("context7-account-broker start returned invalid output");
|
|
22
16
|
}
|
|
23
17
|
const launch = value as Record<string, unknown>;
|
|
24
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
|
26
|
+
throw new Error("context7-account-broker start returned invalid output");
|
|
43
27
|
}
|
|
44
|
-
return
|
|
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
|
-
|
|
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
|