bookstack-mcp 5.2.3 → 5.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.
@@ -1,5 +1,6 @@
1
1
  import axios from 'axios';
2
2
  import https from 'https';
3
+ import { Semaphore } from './util/semaphore.js';
3
4
  const MAX_RETRIES_429 = 5;
4
5
  function parseRetryAfter(value) {
5
6
  if (typeof value !== 'string' || !value)
@@ -45,6 +46,19 @@ function getSlugCacheEntry(baseUrl) {
45
46
  }
46
47
  return entry;
47
48
  }
49
+ const DEFAULT_MAX_CONCURRENCY = 4;
50
+ // Process-wide concurrency limiters, one per BookStack base URL — shared across
51
+ // all sessions/clients so they cannot collectively exceed the per-token rate
52
+ // limit. Mirrors the slugCaches sharing model.
53
+ const concurrencyLimiters = new Map();
54
+ function getConcurrencyLimiter(baseUrl, max) {
55
+ let limiter = concurrencyLimiters.get(baseUrl);
56
+ if (!limiter) {
57
+ limiter = new Semaphore(max);
58
+ concurrencyLimiters.set(baseUrl, limiter);
59
+ }
60
+ return limiter;
61
+ }
48
62
  export class BookStackClient {
49
63
  client;
50
64
  enableWrite;
@@ -52,6 +66,8 @@ export class BookStackClient {
52
66
  constructor(config) {
53
67
  this.enableWrite = config.enableWrite || false;
54
68
  this.baseUrl = config.baseUrl;
69
+ const limiter = getConcurrencyLimiter(config.baseUrl, config.maxConcurrency ?? DEFAULT_MAX_CONCURRENCY);
70
+ const baseAdapter = axios.getAdapter(axios.defaults.adapter);
55
71
  this.client = axios.create({
56
72
  baseURL: `${config.baseUrl}/api`,
57
73
  timeout: config.timeoutMs ?? DEFAULT_TIMEOUT_MS,
@@ -61,7 +77,12 @@ export class BookStackClient {
61
77
  },
62
78
  httpsAgent: config.insecureSkipTlsVerify
63
79
  ? new https.Agent({ rejectUnauthorized: false })
64
- : undefined
80
+ : undefined,
81
+ // Every request — including the 429-retry re-issue below — flows through
82
+ // this adapter exactly once per HTTP attempt. The retry's backoff sleep
83
+ // happens in the response interceptor (outside the adapter), so the
84
+ // permit is released between attempts, not held during the wait.
85
+ adapter: (requestConfig) => limiter.run(() => baseAdapter(requestConfig)),
65
86
  });
66
87
  this.client.interceptors.response.use(undefined, async (error) => {
67
88
  const cfg = error.config;
package/dist/index.js CHANGED
@@ -947,6 +947,11 @@ async function main() {
947
947
  const timeoutRaw = process.env.BOOKSTACK_TIMEOUT_MS;
948
948
  const timeoutParsed = timeoutRaw ? parseInt(timeoutRaw, 10) : NaN;
949
949
  const timeoutMs = Number.isFinite(timeoutParsed) && timeoutParsed > 0 ? timeoutParsed : undefined;
950
+ const concurrencyRaw = process.env.BOOKSTACK_MAX_CONCURRENCY;
951
+ const concurrencyParsed = concurrencyRaw ? parseInt(concurrencyRaw, 10) : NaN;
952
+ const maxConcurrency = Number.isFinite(concurrencyParsed) && concurrencyParsed > 0
953
+ ? concurrencyParsed
954
+ : undefined;
950
955
  const oauth = loadOAuthConfig(process.env);
951
956
  // Initialize the OAuth broker-state store (Redis when REDIS_URL is set) before
952
957
  // the server handles any request, so DCR clients / pending flows / codes persist.
@@ -962,14 +967,15 @@ async function main() {
962
967
  tokenSecret: getRequiredEnvVar('BOOKSTACK_TOKEN_SECRET'),
963
968
  enableWrite: oauth ? false : envEnableWrite,
964
969
  insecureSkipTlsVerify,
965
- timeoutMs
970
+ timeoutMs,
971
+ maxConcurrency
966
972
  };
967
973
  // The write credential is optional and only used by the OAuth proxy for sessions whose
968
974
  // token carries the configured app role.
969
975
  const writeTokenId = process.env.BOOKSTACK_WRITE_TOKEN_ID;
970
976
  const writeTokenSecret = process.env.BOOKSTACK_WRITE_TOKEN_SECRET;
971
977
  const write = (writeTokenId && writeTokenSecret)
972
- ? { baseUrl, tokenId: writeTokenId, tokenSecret: writeTokenSecret, enableWrite: true, insecureSkipTlsVerify, timeoutMs }
978
+ ? { baseUrl, tokenId: writeTokenId, tokenSecret: writeTokenSecret, enableWrite: true, insecureSkipTlsVerify, timeoutMs, maxConcurrency }
973
979
  : null;
974
980
  const config = { read, write, oauth };
975
981
  console.error('Initializing BookStack MCP Server...');
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Minimal async counting semaphore. Bounds how many async operations run at
3
+ * once. `release()` hands a permit directly to the next waiter (FIFO) so a
4
+ * burst of `acquire()` calls drains in order rather than thundering.
5
+ */
6
+ export class Semaphore {
7
+ permits;
8
+ waiters = [];
9
+ constructor(max) {
10
+ if (!Number.isInteger(max) || max < 1) {
11
+ throw new Error(`Semaphore max must be an integer >= 1 (got ${max})`);
12
+ }
13
+ this.permits = max;
14
+ }
15
+ acquire() {
16
+ if (this.permits > 0) {
17
+ this.permits--;
18
+ return Promise.resolve();
19
+ }
20
+ return new Promise((resolve) => this.waiters.push(resolve));
21
+ }
22
+ release() {
23
+ const next = this.waiters.shift();
24
+ if (next) {
25
+ next(); // pass the permit straight to the waiter; permit count unchanged
26
+ }
27
+ else {
28
+ this.permits++;
29
+ }
30
+ }
31
+ async run(fn) {
32
+ await this.acquire();
33
+ try {
34
+ return await fn();
35
+ }
36
+ finally {
37
+ this.release();
38
+ }
39
+ }
40
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bookstack-mcp",
3
- "version": "5.2.3",
3
+ "version": "5.3.0",
4
4
  "description": "MCP server for BookStack wiki — search, read, create, and manage documentation via AI assistants",
5
5
  "type": "module",
6
6
  "bin": {
@@ -13,6 +13,7 @@
13
13
  "build": "tsc && shx chmod +x dist/*.js",
14
14
  "dev": "tsx src/index.ts",
15
15
  "start": "node dist/index.js",
16
+ "test": "node --import tsx --test 'src/**/*.test.ts'",
16
17
  "type-check": "tsc --noEmit",
17
18
  "prepare": "npm run build"
18
19
  },