codebase-snapshot 0.20.4 → 0.20.5

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.
@@ -1,4 +1,4 @@
1
- import { buildTools, createServer, GENERATOR_VERSION } from './chunk-AQZNT3QL.js';
1
+ import { buildTools, createServer, GENERATOR_VERSION } from './chunk-C3WVC4FV.js';
2
2
  import * as http from 'http';
3
3
  import * as crypto from 'crypto';
4
4
 
@@ -6,6 +6,9 @@ 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_RATE_WINDOWS = 1024;
9
12
  function generateToken() {
10
13
  return crypto.randomBytes(24).toString("base64url");
11
14
  }
@@ -41,6 +44,13 @@ async function startHttpTransport(options) {
41
44
  const toolCount = buildTools().length;
42
45
  const mcpSessions = /* @__PURE__ */ new Map();
43
46
  const socketServers = /* @__PURE__ */ new WeakMap();
47
+ const rememberSession = (id, server) => {
48
+ if (!mcpSessions.has(id) && mcpSessions.size >= MAX_SESSION_ENTRIES) {
49
+ const oldest = mcpSessions.keys().next().value;
50
+ if (oldest !== void 0) mcpSessions.delete(oldest);
51
+ }
52
+ mcpSessions.set(id, server);
53
+ };
44
54
  const anonymousServers = /* @__PURE__ */ new Map();
45
55
  const serverFor = (request) => {
46
56
  const header = request.headers["mcp-session-id"];
@@ -49,11 +59,7 @@ async function startHttpTransport(options) {
49
59
  let existing = mcpSessions.get(supplied);
50
60
  if (existing === void 0) {
51
61
  existing = createMcp();
52
- if (mcpSessions.size >= 64) {
53
- const oldest = mcpSessions.keys().next().value;
54
- if (oldest !== void 0) mcpSessions.delete(oldest);
55
- }
56
- mcpSessions.set(supplied, existing);
62
+ rememberSession(supplied, existing);
57
63
  }
58
64
  return existing;
59
65
  }
@@ -84,6 +90,35 @@ async function startHttpTransport(options) {
84
90
  return fromQuery !== null && timingSafeEqual2(fromQuery, token);
85
91
  };
86
92
  const sessions = /* @__PURE__ */ new Map();
93
+ let inFlight = 0;
94
+ const acquireToolSlot = () => {
95
+ if (inFlight >= MAX_IN_FLIGHT) return false;
96
+ inFlight += 1;
97
+ return true;
98
+ };
99
+ const releaseToolSlot = () => {
100
+ if (inFlight > 0) inFlight -= 1;
101
+ };
102
+ const busyResponse = (id) => ({
103
+ jsonrpc: "2.0",
104
+ id: typeof id === "string" || typeof id === "number" || id === null ? id : null,
105
+ error: {
106
+ code: -32001,
107
+ message: "server busy: too many tool calls are already in flight"
108
+ }
109
+ });
110
+ const isToolCall = (message) => message !== null && typeof message === "object" && message.method === "tools/call";
111
+ const rememberRateWindow = (remote, now) => {
112
+ if (!sessions.has(remote) && sessions.size >= MAX_RATE_WINDOWS) {
113
+ for (const [key, value] of sessions) if (value.resetAt <= now) sessions.delete(key);
114
+ while (sessions.size >= MAX_RATE_WINDOWS) {
115
+ const oldest = sessions.keys().next().value;
116
+ if (oldest === void 0) break;
117
+ sessions.delete(oldest);
118
+ }
119
+ }
120
+ sessions.set(remote, { count: 1, resetAt: now + RATE_WINDOW_MS });
121
+ };
87
122
  const metrics = {
88
123
  requests: 0,
89
124
  byMethod: /* @__PURE__ */ new Map(),
@@ -99,7 +134,7 @@ async function startHttpTransport(options) {
99
134
  const now = Date.now();
100
135
  const window = sessions.get(remote);
101
136
  if (window === void 0 || window.resetAt <= now) {
102
- sessions.set(remote, { count: 1, resetAt: now + RATE_WINDOW_MS });
137
+ rememberRateWindow(remote, now);
103
138
  return false;
104
139
  }
105
140
  if (window.count >= RATE_LIMIT) return true;
@@ -259,8 +294,19 @@ async function startHttpTransport(options) {
259
294
  send(400, { jsonrpc: "2.0", id: null, error: { code: -32700, message: String(error) } });
260
295
  return;
261
296
  }
262
- const answer = await stream.server.handle(message);
263
- if (answer !== null) stream.write("event: message\ndata: " + JSON.stringify(answer) + "\n\n");
297
+ const gated = isToolCall(message);
298
+ if (gated && !acquireToolSlot()) {
299
+ const answer = busyResponse(message.id);
300
+ stream.write("event: message\ndata: " + JSON.stringify(answer) + "\n\n");
301
+ send(202, "", cors);
302
+ return;
303
+ }
304
+ try {
305
+ const answer = await stream.server.handle(message);
306
+ if (answer !== null) stream.write("event: message\ndata: " + JSON.stringify(answer) + "\n\n");
307
+ } finally {
308
+ if (gated) releaseToolSlot();
309
+ }
264
310
  send(202, "", cors);
265
311
  })();
266
312
  });
@@ -325,13 +371,22 @@ async function startHttpTransport(options) {
325
371
  }
326
372
  if (sessionId === "" && !Array.isArray(message) && message.method === "initialize") {
327
373
  sessionId = newSessionId();
328
- mcpSessions.set(sessionId, activeServer);
374
+ rememberSession(sessionId, activeServer);
329
375
  }
330
376
  const sessionHeaders = sessionId === "" ? cors : { ...cors, "mcp-session-id": sessionId };
331
377
  const answers = [];
332
378
  for (const item of batch) {
333
- const answer = await activeServer.handle(item);
334
- if (answer !== null) answers.push(answer);
379
+ const gated = isToolCall(item);
380
+ if (gated && !acquireToolSlot()) {
381
+ answers.push(busyResponse(item.id));
382
+ continue;
383
+ }
384
+ try {
385
+ const answer = await activeServer.handle(item);
386
+ if (answer !== null) answers.push(answer);
387
+ } finally {
388
+ if (gated) releaseToolSlot();
389
+ }
335
390
  }
336
391
  if (answers.length === 0) {
337
392
  send(202, "", sessionHeaders);