docusaurus-plugin-mcp-server 0.6.0 → 0.7.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,5 @@
1
1
  import { IncomingMessage, ServerResponse } from 'node:http';
2
- import { M as McpServerConfig, P as ProcessedDoc } from './index-CzA4FjeE.mjs';
2
+ import { M as McpServerConfig, P as ProcessedDoc } from './index-4g0ZZK3z.mjs';
3
3
 
4
4
  /**
5
5
  * Vercel adapter for MCP server
@@ -1,5 +1,5 @@
1
1
  import { IncomingMessage, ServerResponse } from 'node:http';
2
- import { M as McpServerConfig, P as ProcessedDoc } from './index-CzA4FjeE.js';
2
+ import { M as McpServerConfig, P as ProcessedDoc } from './index-4g0ZZK3z.js';
3
3
 
4
4
  /**
5
5
  * Vercel adapter for MCP server
@@ -1,16 +1,17 @@
1
1
  'use strict';
2
2
 
3
- var fs = require('fs-extra');
4
3
  var mcp_js = require('@modelcontextprotocol/sdk/server/mcp.js');
5
4
  var streamableHttp_js = require('@modelcontextprotocol/sdk/server/streamableHttp.js');
6
5
  var webStandardStreamableHttp_js = require('@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js');
7
6
  var zod = require('zod');
8
7
  var FlexSearch = require('flexsearch');
8
+ var fs = require('fs-extra');
9
+ require('http');
9
10
 
10
11
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
11
12
 
12
- var fs__default = /*#__PURE__*/_interopDefault(fs);
13
13
  var FlexSearch__default = /*#__PURE__*/_interopDefault(FlexSearch);
14
+ var fs__default = /*#__PURE__*/_interopDefault(fs);
14
15
 
15
16
  // src/mcp/server.ts
16
17
  var FIELD_WEIGHTS = {
@@ -143,19 +144,126 @@ async function importSearchIndex(data) {
143
144
  }
144
145
  return index;
145
146
  }
147
+ var FlexSearchProvider = class {
148
+ name = "flexsearch";
149
+ docs = null;
150
+ searchIndex = null;
151
+ ready = false;
152
+ async initialize(_context, initData) {
153
+ if (!initData) {
154
+ throw new Error("[FlexSearch] SearchProviderInitData required for FlexSearch provider");
155
+ }
156
+ if (initData.docs && initData.indexData) {
157
+ this.docs = initData.docs;
158
+ this.searchIndex = await importSearchIndex(initData.indexData);
159
+ this.ready = true;
160
+ return;
161
+ }
162
+ if (initData.docsPath && initData.indexPath) {
163
+ if (await fs__default.default.pathExists(initData.docsPath)) {
164
+ this.docs = await fs__default.default.readJson(initData.docsPath);
165
+ } else {
166
+ throw new Error(`[FlexSearch] Docs file not found: ${initData.docsPath}`);
167
+ }
168
+ if (await fs__default.default.pathExists(initData.indexPath)) {
169
+ const indexData = await fs__default.default.readJson(initData.indexPath);
170
+ this.searchIndex = await importSearchIndex(indexData);
171
+ } else {
172
+ throw new Error(`[FlexSearch] Search index not found: ${initData.indexPath}`);
173
+ }
174
+ this.ready = true;
175
+ return;
176
+ }
177
+ throw new Error(
178
+ "[FlexSearch] Invalid init data: must provide either file paths (docsPath, indexPath) or pre-loaded data (docs, indexData)"
179
+ );
180
+ }
181
+ isReady() {
182
+ return this.ready && this.docs !== null && this.searchIndex !== null;
183
+ }
184
+ async search(query, options) {
185
+ if (!this.isReady() || !this.docs || !this.searchIndex) {
186
+ throw new Error("[FlexSearch] Provider not initialized");
187
+ }
188
+ const limit = options?.limit ?? 5;
189
+ return searchIndex(this.searchIndex, this.docs, query, { limit });
190
+ }
191
+ async getDocument(route) {
192
+ if (!this.docs) {
193
+ throw new Error("[FlexSearch] Provider not initialized");
194
+ }
195
+ if (this.docs[route]) {
196
+ return this.docs[route];
197
+ }
198
+ const normalizedRoute = route.startsWith("/") ? route : `/${route}`;
199
+ const withoutSlash = route.startsWith("/") ? route.slice(1) : route;
200
+ return this.docs[normalizedRoute] ?? this.docs[withoutSlash] ?? null;
201
+ }
202
+ async healthCheck() {
203
+ if (!this.isReady()) {
204
+ return { healthy: false, message: "FlexSearch provider not initialized" };
205
+ }
206
+ const docCount = this.docs ? Object.keys(this.docs).length : 0;
207
+ return {
208
+ healthy: true,
209
+ message: `FlexSearch provider ready with ${docCount} documents`
210
+ };
211
+ }
212
+ /**
213
+ * Get all loaded documents (for compatibility with existing server code)
214
+ */
215
+ getDocs() {
216
+ return this.docs;
217
+ }
218
+ /**
219
+ * Get the FlexSearch index (for compatibility with existing server code)
220
+ */
221
+ getSearchIndex() {
222
+ return this.searchIndex;
223
+ }
224
+ };
146
225
 
147
- // src/mcp/tools/docs-search.ts
148
- function executeDocsSearch(params, index, docs) {
149
- const { query, limit = 5 } = params;
150
- if (!query || typeof query !== "string" || query.trim().length === 0) {
151
- throw new Error("Query parameter is required and must be a non-empty string");
152
- }
153
- const effectiveLimit = Math.min(Math.max(1, limit), 20);
154
- const results = searchIndex(index, docs, query.trim(), {
155
- limit: effectiveLimit
156
- });
157
- return results;
226
+ // src/providers/loader.ts
227
+ async function loadSearchProvider(specifier) {
228
+ if (specifier === "flexsearch") {
229
+ return new FlexSearchProvider();
230
+ }
231
+ try {
232
+ const module = await import(specifier);
233
+ const ProviderClass = module.default;
234
+ if (typeof ProviderClass === "function") {
235
+ const instance = new ProviderClass();
236
+ if (!isSearchProvider(instance)) {
237
+ throw new Error(
238
+ `Invalid search provider module "${specifier}": does not implement SearchProvider interface`
239
+ );
240
+ }
241
+ return instance;
242
+ }
243
+ if (isSearchProvider(ProviderClass)) {
244
+ return ProviderClass;
245
+ }
246
+ throw new Error(
247
+ `Invalid search provider module "${specifier}": must export a default class or SearchProvider instance`
248
+ );
249
+ } catch (error) {
250
+ if (error instanceof Error && error.message.includes("Cannot find module")) {
251
+ throw new Error(
252
+ `Search provider module not found: "${specifier}". Check the path or package name.`
253
+ );
254
+ }
255
+ throw error;
256
+ }
158
257
  }
258
+ function isSearchProvider(obj) {
259
+ if (!obj || typeof obj !== "object") {
260
+ return false;
261
+ }
262
+ const provider = obj;
263
+ return typeof provider.name === "string" && typeof provider.initialize === "function" && typeof provider.isReady === "function" && typeof provider.search === "function";
264
+ }
265
+
266
+ // src/mcp/tools/docs-search.ts
159
267
  function formatSearchResults(results, baseUrl) {
160
268
  if (results.length === 0) {
161
269
  return "No matching documents found.";
@@ -181,28 +289,6 @@ function formatSearchResults(results, baseUrl) {
181
289
  }
182
290
 
183
291
  // src/mcp/tools/docs-get-page.ts
184
- function executeDocsGetPage(params, docs) {
185
- const { route } = params;
186
- if (!route || typeof route !== "string") {
187
- throw new Error("Route parameter is required and must be a string");
188
- }
189
- let normalizedRoute = route.trim();
190
- if (!normalizedRoute.startsWith("/")) {
191
- normalizedRoute = "/" + normalizedRoute;
192
- }
193
- if (normalizedRoute.length > 1 && normalizedRoute.endsWith("/")) {
194
- normalizedRoute = normalizedRoute.slice(0, -1);
195
- }
196
- const doc = docs[normalizedRoute];
197
- if (!doc) {
198
- const altRoute = normalizedRoute.slice(1);
199
- if (docs[altRoute]) {
200
- return docs[altRoute] ?? null;
201
- }
202
- return null;
203
- }
204
- return doc;
205
- }
206
292
  function formatPageContent(doc, baseUrl) {
207
293
  if (!doc) {
208
294
  return "Page not found. Please check the route path and try again.";
@@ -247,52 +333,6 @@ function extractSection(markdown, headingId, headings) {
247
333
  }
248
334
 
249
335
  // src/mcp/tools/docs-get-section.ts
250
- function executeDocsGetSection(params, docs) {
251
- const { route, headingId } = params;
252
- if (!route || typeof route !== "string") {
253
- throw new Error("Route parameter is required and must be a string");
254
- }
255
- if (!headingId || typeof headingId !== "string") {
256
- throw new Error("HeadingId parameter is required and must be a string");
257
- }
258
- let normalizedRoute = route.trim();
259
- if (!normalizedRoute.startsWith("/")) {
260
- normalizedRoute = "/" + normalizedRoute;
261
- }
262
- if (normalizedRoute.length > 1 && normalizedRoute.endsWith("/")) {
263
- normalizedRoute = normalizedRoute.slice(0, -1);
264
- }
265
- const doc = docs[normalizedRoute];
266
- if (!doc) {
267
- return {
268
- content: null,
269
- doc: null,
270
- headingText: null,
271
- availableHeadings: []
272
- };
273
- }
274
- const availableHeadings = doc.headings.map((h) => ({
275
- id: h.id,
276
- text: h.text,
277
- level: h.level
278
- }));
279
- const heading = doc.headings.find((h) => h.id === headingId.trim());
280
- if (!heading) {
281
- return {
282
- content: null,
283
- doc,
284
- headingText: null,
285
- availableHeadings
286
- };
287
- }
288
- const content = extractSection(doc.markdown, headingId.trim(), doc.headings);
289
- return {
290
- content,
291
- doc,
292
- headingText: heading.text,
293
- availableHeadings
294
- };
295
- }
296
336
  function formatSectionContent(result, headingId, baseUrl) {
297
337
  if (!result.doc) {
298
338
  return "Page not found. Please check the route path and try again.";
@@ -329,8 +369,7 @@ function isDataConfig(config) {
329
369
  }
330
370
  var McpDocsServer = class {
331
371
  config;
332
- docs = null;
333
- searchIndex = null;
372
+ searchProvider = null;
334
373
  mcpServer;
335
374
  initialized = false;
336
375
  constructor(config) {
@@ -363,18 +402,26 @@ var McpDocsServer = class {
363
402
  },
364
403
  async ({ query, limit }) => {
365
404
  await this.initialize();
366
- if (!this.docs || !this.searchIndex) {
405
+ if (!this.searchProvider || !this.searchProvider.isReady()) {
367
406
  return {
368
407
  content: [{ type: "text", text: "Server not initialized. Please try again." }],
369
408
  isError: true
370
409
  };
371
410
  }
372
- const results = executeDocsSearch({ query, limit }, this.searchIndex, this.docs);
373
- return {
374
- content: [
375
- { type: "text", text: formatSearchResults(results, this.config.baseUrl) }
376
- ]
377
- };
411
+ try {
412
+ const results = await this.searchProvider.search(query, { limit });
413
+ return {
414
+ content: [
415
+ { type: "text", text: formatSearchResults(results, this.config.baseUrl) }
416
+ ]
417
+ };
418
+ } catch (error) {
419
+ console.error("[MCP] Search error:", error);
420
+ return {
421
+ content: [{ type: "text", text: `Search error: ${String(error)}` }],
422
+ isError: true
423
+ };
424
+ }
378
425
  }
379
426
  );
380
427
  this.mcpServer.registerTool(
@@ -387,16 +434,24 @@ var McpDocsServer = class {
387
434
  },
388
435
  async ({ route }) => {
389
436
  await this.initialize();
390
- if (!this.docs) {
437
+ if (!this.searchProvider || !this.searchProvider.isReady()) {
391
438
  return {
392
439
  content: [{ type: "text", text: "Server not initialized. Please try again." }],
393
440
  isError: true
394
441
  };
395
442
  }
396
- const doc = executeDocsGetPage({ route }, this.docs);
397
- return {
398
- content: [{ type: "text", text: formatPageContent(doc, this.config.baseUrl) }]
399
- };
443
+ try {
444
+ const doc = await this.getDocument(route);
445
+ return {
446
+ content: [{ type: "text", text: formatPageContent(doc, this.config.baseUrl) }]
447
+ };
448
+ } catch (error) {
449
+ console.error("[MCP] Get page error:", error);
450
+ return {
451
+ content: [{ type: "text", text: `Error getting page: ${String(error)}` }],
452
+ isError: true
453
+ };
454
+ }
400
455
  }
401
456
  );
402
457
  this.mcpServer.registerTool(
@@ -412,26 +467,95 @@ var McpDocsServer = class {
412
467
  },
413
468
  async ({ route, headingId }) => {
414
469
  await this.initialize();
415
- if (!this.docs) {
470
+ if (!this.searchProvider || !this.searchProvider.isReady()) {
416
471
  return {
417
472
  content: [{ type: "text", text: "Server not initialized. Please try again." }],
418
473
  isError: true
419
474
  };
420
475
  }
421
- const result = executeDocsGetSection({ route, headingId }, this.docs);
422
- return {
423
- content: [
424
- {
425
- type: "text",
426
- text: formatSectionContent(result, headingId, this.config.baseUrl)
427
- }
428
- ]
429
- };
476
+ try {
477
+ const doc = await this.getDocument(route);
478
+ if (!doc) {
479
+ return {
480
+ content: [
481
+ {
482
+ type: "text",
483
+ text: formatSectionContent(
484
+ { content: null, doc: null, headingText: null, availableHeadings: [] },
485
+ headingId,
486
+ this.config.baseUrl
487
+ )
488
+ }
489
+ ]
490
+ };
491
+ }
492
+ const availableHeadings = doc.headings.map((h) => ({
493
+ id: h.id,
494
+ text: h.text,
495
+ level: h.level
496
+ }));
497
+ const heading = doc.headings.find((h) => h.id === headingId.trim());
498
+ if (!heading) {
499
+ return {
500
+ content: [
501
+ {
502
+ type: "text",
503
+ text: formatSectionContent(
504
+ { content: null, doc, headingText: null, availableHeadings },
505
+ headingId,
506
+ this.config.baseUrl
507
+ )
508
+ }
509
+ ]
510
+ };
511
+ }
512
+ const sectionContent = extractSection(doc.markdown, headingId.trim(), doc.headings);
513
+ return {
514
+ content: [
515
+ {
516
+ type: "text",
517
+ text: formatSectionContent(
518
+ { content: sectionContent, doc, headingText: heading.text, availableHeadings },
519
+ headingId,
520
+ this.config.baseUrl
521
+ )
522
+ }
523
+ ]
524
+ };
525
+ } catch (error) {
526
+ console.error("[MCP] Get section error:", error);
527
+ return {
528
+ content: [{ type: "text", text: `Error getting section: ${String(error)}` }],
529
+ isError: true
530
+ };
531
+ }
430
532
  }
431
533
  );
432
534
  }
433
535
  /**
434
- * Load docs and search index
536
+ * Get a document by route using the search provider
537
+ */
538
+ async getDocument(route) {
539
+ if (!this.searchProvider) {
540
+ return null;
541
+ }
542
+ if (this.searchProvider.getDocument) {
543
+ return this.searchProvider.getDocument(route);
544
+ }
545
+ if (this.searchProvider instanceof FlexSearchProvider) {
546
+ const docs = this.searchProvider.getDocs();
547
+ if (!docs) return null;
548
+ if (docs[route]) {
549
+ return docs[route];
550
+ }
551
+ const normalizedRoute = route.startsWith("/") ? route : `/${route}`;
552
+ const withoutSlash = route.startsWith("/") ? route.slice(1) : route;
553
+ return docs[normalizedRoute] ?? docs[withoutSlash] ?? null;
554
+ }
555
+ return null;
556
+ }
557
+ /**
558
+ * Load docs and search index using the configured search provider
435
559
  *
436
560
  * For file-based config: reads from disk
437
561
  * For data config: uses pre-loaded data directly
@@ -441,24 +565,26 @@ var McpDocsServer = class {
441
565
  return;
442
566
  }
443
567
  try {
568
+ const searchSpecifier = this.config.search ?? "flexsearch";
569
+ this.searchProvider = await loadSearchProvider(searchSpecifier);
570
+ const providerContext = {
571
+ baseUrl: this.config.baseUrl ?? "",
572
+ serverName: this.config.name,
573
+ serverVersion: this.config.version ?? "1.0.0",
574
+ outputDir: ""
575
+ // Not relevant for runtime
576
+ };
577
+ const initData = {};
444
578
  if (isDataConfig(this.config)) {
445
- this.docs = this.config.docs;
446
- this.searchIndex = await importSearchIndex(this.config.searchIndexData);
579
+ initData.docs = this.config.docs;
580
+ initData.indexData = this.config.searchIndexData;
447
581
  } else if (isFileConfig(this.config)) {
448
- if (await fs__default.default.pathExists(this.config.docsPath)) {
449
- this.docs = await fs__default.default.readJson(this.config.docsPath);
450
- } else {
451
- throw new Error(`Docs file not found: ${this.config.docsPath}`);
452
- }
453
- if (await fs__default.default.pathExists(this.config.indexPath)) {
454
- const indexData = await fs__default.default.readJson(this.config.indexPath);
455
- this.searchIndex = await importSearchIndex(indexData);
456
- } else {
457
- throw new Error(`Search index not found: ${this.config.indexPath}`);
458
- }
582
+ initData.docsPath = this.config.docsPath;
583
+ initData.indexPath = this.config.indexPath;
459
584
  } else {
460
585
  throw new Error("Invalid server config: must provide either file paths or pre-loaded data");
461
586
  }
587
+ await this.searchProvider.initialize(providerContext, initData);
462
588
  this.initialized = true;
463
589
  } catch (error) {
464
590
  console.error("[MCP] Failed to initialize:", error);
@@ -519,12 +645,18 @@ var McpDocsServer = class {
519
645
  * Useful for health checks and debugging
520
646
  */
521
647
  async getStatus() {
648
+ let docCount = 0;
649
+ if (this.searchProvider instanceof FlexSearchProvider) {
650
+ const docs = this.searchProvider.getDocs();
651
+ docCount = docs ? Object.keys(docs).length : 0;
652
+ }
522
653
  return {
523
654
  name: this.config.name,
524
655
  version: this.config.version ?? "1.0.0",
525
656
  initialized: this.initialized,
526
- docCount: this.docs ? Object.keys(this.docs).length : 0,
527
- baseUrl: this.config.baseUrl
657
+ docCount,
658
+ baseUrl: this.config.baseUrl,
659
+ searchProvider: this.searchProvider?.name
528
660
  };
529
661
  }
530
662
  /**