rivet-mcp 1.0.0 → 1.0.1

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/dist/cli.js CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env node
2
+ import { pathToFileURL } from "node:url";
2
3
  import { Server } from "@modelcontextprotocol/sdk/server/index.js";
3
4
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4
5
  import { CallToolRequestSchema, ListResourcesRequestSchema, ListToolsRequestSchema, ReadResourceRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
@@ -14,7 +15,7 @@ function requiredEnv(name) {
14
15
  return value;
15
16
  }
16
17
  /** HTTPS обязателен; http допускается только для локального dev-стенда. */
17
- function assertSecureUrl(name, raw) {
18
+ export function assertSecureUrl(name, raw) {
18
19
  const url = new URL(raw);
19
20
  const isLocal = LOCAL_HOSTS.has(url.hostname);
20
21
  if (url.protocol === "http:" && !isLocal) {
@@ -55,8 +56,11 @@ async function main() {
55
56
  process.on("SIGTERM", () => void shutdown());
56
57
  await server.connect(new StdioServerTransport());
57
58
  }
58
- main().catch((error) => {
59
- const message = error instanceof Error ? error.message : String(error);
60
- console.error(`rivet-mcp-bridge: ${message}`);
61
- process.exitCode = 1;
62
- });
59
+ // main() — только при запуске файла как CLI, не при import из тестов
60
+ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
61
+ main().catch((error) => {
62
+ const message = error instanceof Error ? error.message : String(error);
63
+ console.error(`rivet-mcp-bridge: ${message}`);
64
+ process.exitCode = 1;
65
+ });
66
+ }
package/dist/remote.js CHANGED
@@ -7,6 +7,7 @@ export class RemoteRivet {
7
7
  mcpUrl;
8
8
  tokens;
9
9
  client;
10
+ reconnectPromise;
10
11
  constructor(mcpUrl, tokens) {
11
12
  this.mcpUrl = mcpUrl;
12
13
  this.tokens = tokens;
@@ -21,28 +22,37 @@ export class RemoteRivet {
21
22
  this.client = client;
22
23
  }
23
24
  /**
24
- * Выполнить вызов к rivet; при 401 (истёк токен) — обменять PAT заново,
25
- * переподключиться и повторить ровно один раз.
25
+ * Выполнить вызов к rivet; при 401 (истёк токен) — один общий reconnect
26
+ * (single-flight: параллельные вызовы ждут тот же reconnect) и повтор ровно один раз.
26
27
  */
27
28
  async call(action) {
28
29
  if (!this.client) {
29
- await this.connect();
30
+ await this.reconnectOnce();
30
31
  }
31
- let client = this.client;
32
32
  try {
33
- return await action(client);
33
+ return await action(this.client);
34
34
  }
35
35
  catch (error) {
36
36
  if (!(error instanceof UnauthorizedError)) {
37
37
  throw error;
38
38
  }
39
- await this.tokens.refresh();
40
- await this.close();
41
- await this.connect();
42
- client = this.client;
43
- return await action(client);
39
+ await this.reconnectOnce();
40
+ return await action(this.client);
44
41
  }
45
42
  }
43
+ /** Один reconnect на все параллельные вызовы; завершившиеся ошибкой — получают её все. */
44
+ reconnectOnce() {
45
+ if (!this.reconnectPromise) {
46
+ this.reconnectPromise = (async () => {
47
+ await this.tokens.refresh();
48
+ await this.close();
49
+ await this.connect();
50
+ })().finally(() => {
51
+ this.reconnectPromise = undefined;
52
+ });
53
+ }
54
+ return this.reconnectPromise;
55
+ }
46
56
  async close() {
47
57
  if (!this.client) {
48
58
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rivet-mcp",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "stdio MCP bridge for the Rivet code-retrieval platform: exchanges a GitLab PAT for a short-lived MCP token and forwards to the Rivet Streamable HTTP endpoint",
5
5
  "type": "module",
6
6
  "bin": {
@@ -11,9 +11,10 @@
11
11
  "README.md"
12
12
  ],
13
13
  "scripts": {
14
- "build": "tsc -p tsconfig.json",
14
+ "build": "rm -rf dist && tsc -p tsconfig.json",
15
+ "build:test": "tsc -p tsconfig.test.json",
15
16
  "prepack": "npm run build",
16
- "test": "npm run build && node --test dist/test/*.test.js"
17
+ "test": "npm run build && npm run build:test && node --test dist/*.test.js"
17
18
  },
18
19
  "engines": {
19
20
  "node": ">=20.11.0"
package/dist/auth.test.js DELETED
@@ -1,79 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { createServer } from "node:http";
3
- import { describe, it } from "node:test";
4
- import { RivetTokenProvider } from "./auth.js";
5
- async function withServer(handler, run) {
6
- const server = createServer(handler);
7
- await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
8
- const port = server.address().port;
9
- try {
10
- await run(port);
11
- }
12
- finally {
13
- await new Promise((resolve) => server.close(() => resolve()));
14
- }
15
- }
16
- function provider(port, pat = "glpat-test", gitlabUrl = "https://gitlab.example") {
17
- return new RivetTokenProvider(`http://127.0.0.1:${port}/exchange`, pat, gitlabUrl);
18
- }
19
- describe("RivetTokenProvider", () => {
20
- it("exchanges PAT and caches the token", async () => {
21
- let exchanges = 0;
22
- await withServer((req, res) => {
23
- exchanges += 1;
24
- let body = "";
25
- req.on("data", (chunk) => (body += chunk));
26
- req.on("end", () => {
27
- assert.equal(req.headers["content-type"], "application/json");
28
- assert.equal(JSON.parse(body).gitlab_pat, "glpat-test");
29
- res.setHeader("content-type", "application/json");
30
- res.end(JSON.stringify({
31
- access_token: `tok-${exchanges}`,
32
- expires_at: "2030-01-01T00:00:00Z",
33
- gitlab_url: "https://gitlab.example/",
34
- }));
35
- });
36
- }, async (port) => {
37
- const p = provider(port);
38
- assert.equal(await p.getToken(), "tok-1");
39
- assert.equal(await p.getToken(), "tok-1");
40
- assert.equal(await p.refresh(), "tok-2");
41
- assert.equal(exchanges, 2);
42
- });
43
- });
44
- it("fails with HTTP status when PAT is invalid", async () => {
45
- await withServer((_req, res) => {
46
- res.statusCode = 401;
47
- res.end('{"detail": "Invalid GitLab PAT"}');
48
- }, async (port) => {
49
- await assert.rejects(provider(port, "bad").getToken(), /HTTP 401/);
50
- });
51
- });
52
- it("rejects exchange from a foreign GitLab instance", async () => {
53
- await withServer((_req, res) => {
54
- res.setHeader("content-type", "application/json");
55
- res.end(JSON.stringify({ access_token: "tok", gitlab_url: "https://evil.example" }));
56
- }, async (port) => {
57
- await assert.rejects(provider(port).getToken(), /instance mismatch/);
58
- });
59
- });
60
- it("refreshes after expiry instead of returning a stale token", async () => {
61
- let exchanges = 0;
62
- await withServer((_req, res) => {
63
- exchanges += 1;
64
- res.setHeader("content-type", "application/json");
65
- // TTL 20 секунд < REFRESH_MARGIN (30s): второй вызов обязан пойти в exchange
66
- res.end(JSON.stringify({
67
- access_token: `tok-${exchanges}`,
68
- expires_at: new Date(Date.now() + 20_000).toISOString(),
69
- gitlab_url: "https://gitlab.example",
70
- }));
71
- }, async (port) => {
72
- const p = provider(port);
73
- const first = await p.getToken();
74
- const second = await p.getToken();
75
- assert.notEqual(first, second);
76
- assert.equal(exchanges, 2);
77
- });
78
- });
79
- });