codebase-snapshot 0.20.4 → 0.20.6
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/CHANGELOG.md +96 -0
- package/README.md +1 -1
- package/dist/bin.js +1840 -791
- package/dist/{chunk-QB5TNXMZ.js → chunk-E4VHKJJU.js} +94 -15
- package/dist/{chunk-AQZNT3QL.js → chunk-KPDO7W5D.js} +1356 -469
- package/dist/cli.cjs +1740 -691
- package/dist/cli.d.cts +4 -2
- package/dist/cli.d.ts +4 -2
- package/dist/cli.js +100 -20
- package/dist/{generate-worker-CNRsO_z7.d.cts → generate-worker-CkWFKtf0.d.ts} +6 -0
- package/dist/{generate-worker-CNRsO_z7.d.ts → generate-worker-Wne8FzN5.d.cts} +6 -0
- package/dist/index.cjs +1340 -465
- package/dist/index.d.cts +6 -4
- package/dist/index.d.ts +6 -4
- package/dist/index.js +1 -1
- package/dist/mcp.cjs +1018 -431
- package/dist/mcp.d.cts +2 -1
- package/dist/mcp.d.ts +2 -1
- package/dist/mcp.js +2 -2
- package/dist/{types-Drw1J0e0.d.cts → types-CykWdR6V.d.cts} +6 -0
- package/dist/{types-Drw1J0e0.d.ts → types-CykWdR6V.d.ts} +6 -0
- package/docs/cli.md +5 -5
- package/docs/deploy.md +2 -2
- package/docs/mcp.md +4 -4
- package/docs/security-review.md +26 -1
- package/docs/workspace.md +3 -0
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { buildTools, createServer, GENERATOR_VERSION } from './chunk-
|
|
1
|
+
import { buildTools, createServer, GENERATOR_VERSION } from './chunk-KPDO7W5D.js';
|
|
2
2
|
import * as http from 'http';
|
|
3
3
|
import * as crypto from 'crypto';
|
|
4
4
|
|
|
@@ -6,6 +6,10 @@ var DEFAULT_PORT = 4789;
|
|
|
6
6
|
var DEFAULT_MAX_BODY = 2 * 1024 * 1024;
|
|
7
7
|
var RATE_LIMIT = 60;
|
|
8
8
|
var RATE_WINDOW_MS = 1e4;
|
|
9
|
+
var MAX_IN_FLIGHT = 4;
|
|
10
|
+
var MAX_SESSION_ENTRIES = 64;
|
|
11
|
+
var MAX_SSE_SESSIONS = 64;
|
|
12
|
+
var MAX_RATE_WINDOWS = 1024;
|
|
9
13
|
function generateToken() {
|
|
10
14
|
return crypto.randomBytes(24).toString("base64url");
|
|
11
15
|
}
|
|
@@ -41,6 +45,10 @@ async function startHttpTransport(options) {
|
|
|
41
45
|
const toolCount = buildTools().length;
|
|
42
46
|
const mcpSessions = /* @__PURE__ */ new Map();
|
|
43
47
|
const socketServers = /* @__PURE__ */ new WeakMap();
|
|
48
|
+
const rememberSession = (id, server) => {
|
|
49
|
+
if (!mcpSessions.has(id) && mcpSessions.size >= MAX_SESSION_ENTRIES) return;
|
|
50
|
+
mcpSessions.set(id, server);
|
|
51
|
+
};
|
|
44
52
|
const anonymousServers = /* @__PURE__ */ new Map();
|
|
45
53
|
const serverFor = (request) => {
|
|
46
54
|
const header = request.headers["mcp-session-id"];
|
|
@@ -49,11 +57,7 @@ async function startHttpTransport(options) {
|
|
|
49
57
|
let existing = mcpSessions.get(supplied);
|
|
50
58
|
if (existing === void 0) {
|
|
51
59
|
existing = createMcp();
|
|
52
|
-
|
|
53
|
-
const oldest = mcpSessions.keys().next().value;
|
|
54
|
-
if (oldest !== void 0) mcpSessions.delete(oldest);
|
|
55
|
-
}
|
|
56
|
-
mcpSessions.set(supplied, existing);
|
|
60
|
+
rememberSession(supplied, existing);
|
|
57
61
|
}
|
|
58
62
|
return existing;
|
|
59
63
|
}
|
|
@@ -84,6 +88,35 @@ async function startHttpTransport(options) {
|
|
|
84
88
|
return fromQuery !== null && timingSafeEqual2(fromQuery, token);
|
|
85
89
|
};
|
|
86
90
|
const sessions = /* @__PURE__ */ new Map();
|
|
91
|
+
let inFlight = 0;
|
|
92
|
+
const acquireToolSlot = () => {
|
|
93
|
+
if (inFlight >= MAX_IN_FLIGHT) return false;
|
|
94
|
+
inFlight += 1;
|
|
95
|
+
return true;
|
|
96
|
+
};
|
|
97
|
+
const releaseToolSlot = () => {
|
|
98
|
+
if (inFlight > 0) inFlight -= 1;
|
|
99
|
+
};
|
|
100
|
+
const busyResponse = (id) => ({
|
|
101
|
+
jsonrpc: "2.0",
|
|
102
|
+
id: typeof id === "string" || typeof id === "number" || id === null ? id : null,
|
|
103
|
+
error: {
|
|
104
|
+
code: -32001,
|
|
105
|
+
message: "server busy: too many tool calls are already in flight"
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
const isToolCall = (message) => message !== null && typeof message === "object" && message.method === "tools/call";
|
|
109
|
+
const rememberRateWindow = (remote, now) => {
|
|
110
|
+
if (!sessions.has(remote) && sessions.size >= MAX_RATE_WINDOWS) {
|
|
111
|
+
for (const [key, value] of sessions) if (value.resetAt <= now) sessions.delete(key);
|
|
112
|
+
while (sessions.size >= MAX_RATE_WINDOWS) {
|
|
113
|
+
const oldest = sessions.keys().next().value;
|
|
114
|
+
if (oldest === void 0) break;
|
|
115
|
+
sessions.delete(oldest);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
sessions.set(remote, { count: 1, resetAt: now + RATE_WINDOW_MS });
|
|
119
|
+
};
|
|
87
120
|
const metrics = {
|
|
88
121
|
requests: 0,
|
|
89
122
|
byMethod: /* @__PURE__ */ new Map(),
|
|
@@ -99,7 +132,7 @@ async function startHttpTransport(options) {
|
|
|
99
132
|
const now = Date.now();
|
|
100
133
|
const window = sessions.get(remote);
|
|
101
134
|
if (window === void 0 || window.resetAt <= now) {
|
|
102
|
-
|
|
135
|
+
rememberRateWindow(remote, now);
|
|
103
136
|
return false;
|
|
104
137
|
}
|
|
105
138
|
if (window.count >= RATE_LIMIT) return true;
|
|
@@ -160,6 +193,10 @@ async function startHttpTransport(options) {
|
|
|
160
193
|
return;
|
|
161
194
|
}
|
|
162
195
|
if (request.method === "GET" && request.url === "/metrics") {
|
|
196
|
+
if (token !== null && host !== "127.0.0.1" && host !== "localhost" && host !== "::1" && !tokenAccepted(request, new URL(request.url, "http://localhost"))) {
|
|
197
|
+
send(401, { error: "unauthorized" }, { "www-authenticate": 'Bearer realm="codebase-snapshot"' });
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
163
200
|
const lines = [
|
|
164
201
|
"# HELP snapshot_uptime_seconds Seconds since the process started.",
|
|
165
202
|
"# TYPE snapshot_uptime_seconds gauge",
|
|
@@ -197,6 +234,10 @@ async function startHttpTransport(options) {
|
|
|
197
234
|
send(429, { error: "too many requests" }, { ...cors, "retry-after": "10" });
|
|
198
235
|
return;
|
|
199
236
|
}
|
|
237
|
+
if (sseSessions.size >= MAX_SSE_SESSIONS) {
|
|
238
|
+
send(503, { error: "too many open SSE sessions" }, { ...cors, "retry-after": "10" });
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
200
241
|
const id = crypto.randomBytes(12).toString("hex");
|
|
201
242
|
response.writeHead(200, {
|
|
202
243
|
...cors,
|
|
@@ -206,9 +247,21 @@ async function startHttpTransport(options) {
|
|
|
206
247
|
"x-accel-buffering": "no"
|
|
207
248
|
});
|
|
208
249
|
const write = (payload) => {
|
|
209
|
-
|
|
250
|
+
try {
|
|
251
|
+
response.write(payload);
|
|
252
|
+
} catch {
|
|
253
|
+
}
|
|
210
254
|
};
|
|
211
|
-
sseSessions.set(id, {
|
|
255
|
+
sseSessions.set(id, {
|
|
256
|
+
write,
|
|
257
|
+
end: () => {
|
|
258
|
+
try {
|
|
259
|
+
response.end();
|
|
260
|
+
} catch {
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
server: createMcp()
|
|
264
|
+
});
|
|
212
265
|
const heartbeat = setInterval(() => write(": ping\n\n"), 15e3);
|
|
213
266
|
const cleanup = () => {
|
|
214
267
|
clearInterval(heartbeat);
|
|
@@ -259,10 +312,27 @@ async function startHttpTransport(options) {
|
|
|
259
312
|
send(400, { jsonrpc: "2.0", id: null, error: { code: -32700, message: String(error) } });
|
|
260
313
|
return;
|
|
261
314
|
}
|
|
262
|
-
const
|
|
263
|
-
if (
|
|
315
|
+
const gated = isToolCall(message);
|
|
316
|
+
if (gated && !acquireToolSlot()) {
|
|
317
|
+
const answer = busyResponse(message.id);
|
|
318
|
+
stream.write("event: message\ndata: " + JSON.stringify(answer) + "\n\n");
|
|
319
|
+
send(202, "", cors);
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
try {
|
|
323
|
+
const answer = await stream.server.handle(message);
|
|
324
|
+
if (answer !== null) stream.write("event: message\ndata: " + JSON.stringify(answer) + "\n\n");
|
|
325
|
+
} finally {
|
|
326
|
+
if (gated) releaseToolSlot();
|
|
327
|
+
}
|
|
264
328
|
send(202, "", cors);
|
|
265
|
-
})()
|
|
329
|
+
})().catch((error) => {
|
|
330
|
+
log("mcp/http: " + error.message);
|
|
331
|
+
try {
|
|
332
|
+
send(500, { jsonrpc: "2.0", id: null, error: { code: -32603, message: "Internal error" } }, cors);
|
|
333
|
+
} catch {
|
|
334
|
+
}
|
|
335
|
+
});
|
|
266
336
|
});
|
|
267
337
|
return;
|
|
268
338
|
}
|
|
@@ -325,13 +395,22 @@ async function startHttpTransport(options) {
|
|
|
325
395
|
}
|
|
326
396
|
if (sessionId === "" && !Array.isArray(message) && message.method === "initialize") {
|
|
327
397
|
sessionId = newSessionId();
|
|
328
|
-
|
|
398
|
+
rememberSession(sessionId, activeServer);
|
|
329
399
|
}
|
|
330
400
|
const sessionHeaders = sessionId === "" ? cors : { ...cors, "mcp-session-id": sessionId };
|
|
331
401
|
const answers = [];
|
|
332
402
|
for (const item of batch) {
|
|
333
|
-
const
|
|
334
|
-
if (
|
|
403
|
+
const gated = isToolCall(item);
|
|
404
|
+
if (gated && !acquireToolSlot()) {
|
|
405
|
+
answers.push(busyResponse(item.id));
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
try {
|
|
409
|
+
const answer = await activeServer.handle(item);
|
|
410
|
+
if (answer !== null) answers.push(answer);
|
|
411
|
+
} finally {
|
|
412
|
+
if (gated) releaseToolSlot();
|
|
413
|
+
}
|
|
335
414
|
}
|
|
336
415
|
if (answers.length === 0) {
|
|
337
416
|
send(202, "", sessionHeaders);
|