docusaurus-plugin-mcp-server 0.9.0 → 0.10.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/README.md +19 -8
- package/dist/adapters-entry.d.ts +12 -6
- package/dist/adapters-entry.js +30 -38
- package/dist/adapters-entry.js.map +1 -1
- package/dist/cli/verify.js +36 -44
- package/dist/cli/verify.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +50 -83
- package/dist/index.js.map +1 -1
- package/dist/theme/index.d.ts +4 -2
- package/dist/theme/index.js +69 -79
- package/dist/theme/index.js.map +1 -1
- package/package.json +7 -9
- package/dist/adapters-entry.d.mts +0 -225
- package/dist/adapters-entry.mjs +0 -1032
- package/dist/adapters-entry.mjs.map +0 -1
- package/dist/cli/verify.d.mts +0 -1
- package/dist/cli/verify.mjs +0 -705
- package/dist/cli/verify.mjs.map +0 -1
- package/dist/index-j-CdaS6k.d.mts +0 -216
- package/dist/index.d.mts +0 -479
- package/dist/index.mjs +0 -1056
- package/dist/index.mjs.map +0 -1
- package/dist/theme/index.d.mts +0 -88
- package/dist/theme/index.mjs +0 -440
- package/dist/theme/index.mjs.map +0 -1
package/dist/cli/verify.mjs
DELETED
|
@@ -1,705 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import fs2 from 'fs-extra';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
5
|
-
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
|
6
|
-
import { WebStandardStreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js';
|
|
7
|
-
import FlexSearch from 'flexsearch';
|
|
8
|
-
import { z } from 'zod';
|
|
9
|
-
|
|
10
|
-
var FIELD_WEIGHTS = {
|
|
11
|
-
title: 3,
|
|
12
|
-
headings: 2,
|
|
13
|
-
description: 1.5,
|
|
14
|
-
content: 1
|
|
15
|
-
};
|
|
16
|
-
function englishStemmer(word) {
|
|
17
|
-
if (word.length <= 3) return word;
|
|
18
|
-
return word.replace(/ing$/, "").replace(/tion$/, "t").replace(/sion$/, "s").replace(/([^aeiou])ed$/, "$1").replace(/([^aeiou])es$/, "$1").replace(/ly$/, "").replace(/ment$/, "").replace(/ness$/, "").replace(/ies$/, "y").replace(/([^s])s$/, "$1");
|
|
19
|
-
}
|
|
20
|
-
function createSearchIndex() {
|
|
21
|
-
return new FlexSearch.Document({
|
|
22
|
-
// Use 'full' tokenization for substring matching
|
|
23
|
-
// This allows "auth" to match "authentication"
|
|
24
|
-
tokenize: "full",
|
|
25
|
-
// Enable caching for faster repeated queries
|
|
26
|
-
cache: 100,
|
|
27
|
-
// Higher resolution = more granular ranking (1-9)
|
|
28
|
-
resolution: 9,
|
|
29
|
-
// Enable context for phrase/proximity matching
|
|
30
|
-
context: {
|
|
31
|
-
resolution: 2,
|
|
32
|
-
depth: 2,
|
|
33
|
-
bidirectional: true
|
|
34
|
-
},
|
|
35
|
-
// Apply stemming to normalize word forms
|
|
36
|
-
encode: (str) => {
|
|
37
|
-
const words = str.toLowerCase().split(/[\s\-_.,;:!?'"()[\]{}]+/);
|
|
38
|
-
return words.filter(Boolean).map(englishStemmer);
|
|
39
|
-
},
|
|
40
|
-
// Document schema
|
|
41
|
-
document: {
|
|
42
|
-
id: "id",
|
|
43
|
-
// Index these fields for searching
|
|
44
|
-
index: ["title", "content", "headings", "description"],
|
|
45
|
-
// Store these fields in results (for enriched queries)
|
|
46
|
-
store: ["title", "description"]
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
function searchIndex(index, docs, query, options = {}) {
|
|
51
|
-
const { limit = 5 } = options;
|
|
52
|
-
const rawResults = index.search(query, {
|
|
53
|
-
limit: limit * 3,
|
|
54
|
-
// Get extra results for better ranking after weighting
|
|
55
|
-
enrich: true
|
|
56
|
-
});
|
|
57
|
-
const docScores = /* @__PURE__ */ new Map();
|
|
58
|
-
for (const fieldResult of rawResults) {
|
|
59
|
-
const field = fieldResult.field;
|
|
60
|
-
const fieldWeight = FIELD_WEIGHTS[field] ?? 1;
|
|
61
|
-
const results2 = fieldResult.result;
|
|
62
|
-
for (let i = 0; i < results2.length; i++) {
|
|
63
|
-
const item = results2[i];
|
|
64
|
-
if (!item) continue;
|
|
65
|
-
const docId = typeof item === "string" ? item : item.id;
|
|
66
|
-
const positionScore = (results2.length - i) / results2.length;
|
|
67
|
-
const weightedScore = positionScore * fieldWeight;
|
|
68
|
-
const existingScore = docScores.get(docId) ?? 0;
|
|
69
|
-
docScores.set(docId, existingScore + weightedScore);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
const results = [];
|
|
73
|
-
for (const [docId, score] of docScores) {
|
|
74
|
-
const doc = docs[docId];
|
|
75
|
-
if (!doc) continue;
|
|
76
|
-
results.push({
|
|
77
|
-
url: docId,
|
|
78
|
-
// docId is the full URL when indexed with baseUrl
|
|
79
|
-
route: doc.route,
|
|
80
|
-
title: doc.title,
|
|
81
|
-
score,
|
|
82
|
-
snippet: generateSnippet(doc.markdown, query),
|
|
83
|
-
matchingHeadings: findMatchingHeadings(doc, query)
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
results.sort((a, b) => b.score - a.score);
|
|
87
|
-
return results.slice(0, limit);
|
|
88
|
-
}
|
|
89
|
-
function generateSnippet(markdown, query) {
|
|
90
|
-
const maxLength = 200;
|
|
91
|
-
const queryTerms = query.toLowerCase().split(/\s+/).filter(Boolean);
|
|
92
|
-
if (queryTerms.length === 0) {
|
|
93
|
-
return markdown.slice(0, maxLength) + (markdown.length > maxLength ? "..." : "");
|
|
94
|
-
}
|
|
95
|
-
const lowerMarkdown = markdown.toLowerCase();
|
|
96
|
-
let bestIndex = -1;
|
|
97
|
-
let bestTerm = "";
|
|
98
|
-
const allTerms = [...queryTerms, ...queryTerms.map(englishStemmer)];
|
|
99
|
-
for (const term of allTerms) {
|
|
100
|
-
const index = lowerMarkdown.indexOf(term);
|
|
101
|
-
if (index !== -1 && (bestIndex === -1 || index < bestIndex)) {
|
|
102
|
-
bestIndex = index;
|
|
103
|
-
bestTerm = term;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
if (bestIndex === -1) {
|
|
107
|
-
return markdown.slice(0, maxLength) + (markdown.length > maxLength ? "..." : "");
|
|
108
|
-
}
|
|
109
|
-
const snippetStart = Math.max(0, bestIndex - 50);
|
|
110
|
-
const snippetEnd = Math.min(markdown.length, bestIndex + bestTerm.length + 150);
|
|
111
|
-
let snippet = markdown.slice(snippetStart, snippetEnd);
|
|
112
|
-
snippet = snippet.replace(/^#{1,6}\s+/gm, "").replace(/\[([^\]]+)\]\([^)]+\)/g, "$1").replace(/!\[([^\]]*)\]\([^)]+\)/g, "").replace(/```[a-z]*\n?/g, "").replace(/`([^`]+)`/g, "$1").replace(/\s+/g, " ").trim();
|
|
113
|
-
const prefix = snippetStart > 0 ? "..." : "";
|
|
114
|
-
const suffix = snippetEnd < markdown.length ? "..." : "";
|
|
115
|
-
return prefix + snippet + suffix;
|
|
116
|
-
}
|
|
117
|
-
function findMatchingHeadings(doc, query) {
|
|
118
|
-
const queryTerms = query.toLowerCase().split(/\s+/).filter(Boolean);
|
|
119
|
-
const allTerms = [...queryTerms, ...queryTerms.map(englishStemmer)];
|
|
120
|
-
const matching = [];
|
|
121
|
-
for (const heading of doc.headings) {
|
|
122
|
-
const headingLower = heading.text.toLowerCase();
|
|
123
|
-
const headingStemmed = headingLower.split(/\s+/).map(englishStemmer).join(" ");
|
|
124
|
-
if (allTerms.some(
|
|
125
|
-
(term) => headingLower.includes(term) || headingStemmed.includes(englishStemmer(term))
|
|
126
|
-
)) {
|
|
127
|
-
matching.push(heading.text);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
return matching.slice(0, 3);
|
|
131
|
-
}
|
|
132
|
-
async function importSearchIndex(data) {
|
|
133
|
-
const index = createSearchIndex();
|
|
134
|
-
for (const [key, value] of Object.entries(data)) {
|
|
135
|
-
await index.import(
|
|
136
|
-
key,
|
|
137
|
-
value
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
return index;
|
|
141
|
-
}
|
|
142
|
-
var FlexSearchProvider = class {
|
|
143
|
-
name = "flexsearch";
|
|
144
|
-
docs = null;
|
|
145
|
-
searchIndex = null;
|
|
146
|
-
ready = false;
|
|
147
|
-
async initialize(_context, initData) {
|
|
148
|
-
if (!initData) {
|
|
149
|
-
throw new Error("[FlexSearch] SearchProviderInitData required for FlexSearch provider");
|
|
150
|
-
}
|
|
151
|
-
if (initData.docs && initData.indexData) {
|
|
152
|
-
this.docs = initData.docs;
|
|
153
|
-
this.searchIndex = await importSearchIndex(initData.indexData);
|
|
154
|
-
this.ready = true;
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
if (initData.docsPath && initData.indexPath) {
|
|
158
|
-
if (await fs2.pathExists(initData.docsPath)) {
|
|
159
|
-
this.docs = await fs2.readJson(initData.docsPath);
|
|
160
|
-
} else {
|
|
161
|
-
throw new Error(`[FlexSearch] Docs file not found: ${initData.docsPath}`);
|
|
162
|
-
}
|
|
163
|
-
if (await fs2.pathExists(initData.indexPath)) {
|
|
164
|
-
const indexData = await fs2.readJson(initData.indexPath);
|
|
165
|
-
this.searchIndex = await importSearchIndex(indexData);
|
|
166
|
-
} else {
|
|
167
|
-
throw new Error(`[FlexSearch] Search index not found: ${initData.indexPath}`);
|
|
168
|
-
}
|
|
169
|
-
this.ready = true;
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
throw new Error(
|
|
173
|
-
"[FlexSearch] Invalid init data: must provide either file paths (docsPath, indexPath) or pre-loaded data (docs, indexData)"
|
|
174
|
-
);
|
|
175
|
-
}
|
|
176
|
-
isReady() {
|
|
177
|
-
return this.ready && this.docs !== null && this.searchIndex !== null;
|
|
178
|
-
}
|
|
179
|
-
async search(query, options) {
|
|
180
|
-
if (!this.isReady() || !this.docs || !this.searchIndex) {
|
|
181
|
-
throw new Error("[FlexSearch] Provider not initialized");
|
|
182
|
-
}
|
|
183
|
-
const limit = options?.limit ?? 5;
|
|
184
|
-
return searchIndex(this.searchIndex, this.docs, query, { limit });
|
|
185
|
-
}
|
|
186
|
-
async getDocument(url) {
|
|
187
|
-
if (!this.docs) {
|
|
188
|
-
throw new Error("[FlexSearch] Provider not initialized");
|
|
189
|
-
}
|
|
190
|
-
return this.docs[url] ?? null;
|
|
191
|
-
}
|
|
192
|
-
async healthCheck() {
|
|
193
|
-
if (!this.isReady()) {
|
|
194
|
-
return { healthy: false, message: "FlexSearch provider not initialized" };
|
|
195
|
-
}
|
|
196
|
-
const docCount = this.docs ? Object.keys(this.docs).length : 0;
|
|
197
|
-
return {
|
|
198
|
-
healthy: true,
|
|
199
|
-
message: `FlexSearch provider ready with ${docCount} documents`
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* Get all loaded documents (for compatibility with existing server code)
|
|
204
|
-
*/
|
|
205
|
-
getDocs() {
|
|
206
|
-
return this.docs;
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Get the FlexSearch index (for compatibility with existing server code)
|
|
210
|
-
*/
|
|
211
|
-
getSearchIndex() {
|
|
212
|
-
return this.searchIndex;
|
|
213
|
-
}
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
// src/providers/loader.ts
|
|
217
|
-
async function loadSearchProvider(specifier) {
|
|
218
|
-
if (specifier === "flexsearch") {
|
|
219
|
-
return new FlexSearchProvider();
|
|
220
|
-
}
|
|
221
|
-
try {
|
|
222
|
-
const module = await import(specifier);
|
|
223
|
-
const ProviderClass = module.default;
|
|
224
|
-
if (typeof ProviderClass === "function") {
|
|
225
|
-
const instance = new ProviderClass();
|
|
226
|
-
if (!isSearchProvider(instance)) {
|
|
227
|
-
throw new Error(
|
|
228
|
-
`Invalid search provider module "${specifier}": does not implement SearchProvider interface`
|
|
229
|
-
);
|
|
230
|
-
}
|
|
231
|
-
return instance;
|
|
232
|
-
}
|
|
233
|
-
if (isSearchProvider(ProviderClass)) {
|
|
234
|
-
return ProviderClass;
|
|
235
|
-
}
|
|
236
|
-
throw new Error(
|
|
237
|
-
`Invalid search provider module "${specifier}": must export a default class or SearchProvider instance`
|
|
238
|
-
);
|
|
239
|
-
} catch (error) {
|
|
240
|
-
if (error instanceof Error && error.message.includes("Cannot find module")) {
|
|
241
|
-
throw new Error(
|
|
242
|
-
`Search provider module not found: "${specifier}". Check the path or package name.`
|
|
243
|
-
);
|
|
244
|
-
}
|
|
245
|
-
throw error;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
function isSearchProvider(obj) {
|
|
249
|
-
if (!obj || typeof obj !== "object") {
|
|
250
|
-
return false;
|
|
251
|
-
}
|
|
252
|
-
const provider = obj;
|
|
253
|
-
return typeof provider.name === "string" && typeof provider.initialize === "function" && typeof provider.isReady === "function" && typeof provider.search === "function";
|
|
254
|
-
}
|
|
255
|
-
var docsSearchInputSchema = {
|
|
256
|
-
query: z.string().min(1).describe("The search query string"),
|
|
257
|
-
limit: z.number().int().min(1).max(20).optional().default(5).describe("Maximum number of results to return (1-20, default: 5)")
|
|
258
|
-
};
|
|
259
|
-
var docsSearchTool = {
|
|
260
|
-
name: "docs_search",
|
|
261
|
-
description: "Search the documentation for relevant pages. Returns matching documents with URLs, snippets, and relevance scores. Use this to find information across all documentation.",
|
|
262
|
-
inputSchema: docsSearchInputSchema
|
|
263
|
-
};
|
|
264
|
-
function formatSearchResults(results) {
|
|
265
|
-
if (results.length === 0) {
|
|
266
|
-
return "No matching documents found.";
|
|
267
|
-
}
|
|
268
|
-
const lines = [`Found ${results.length} result(s):
|
|
269
|
-
`];
|
|
270
|
-
for (let i = 0; i < results.length; i++) {
|
|
271
|
-
const result = results[i];
|
|
272
|
-
if (!result) continue;
|
|
273
|
-
lines.push(`${i + 1}. **${result.title}**`);
|
|
274
|
-
lines.push(` URL: ${result.url}`);
|
|
275
|
-
if (result.matchingHeadings && result.matchingHeadings.length > 0) {
|
|
276
|
-
lines.push(` Matching sections: ${result.matchingHeadings.join(", ")}`);
|
|
277
|
-
}
|
|
278
|
-
lines.push(` ${result.snippet}`);
|
|
279
|
-
lines.push("");
|
|
280
|
-
}
|
|
281
|
-
lines.push("Use docs_fetch with the URL to retrieve the full page content.");
|
|
282
|
-
return lines.join("\n");
|
|
283
|
-
}
|
|
284
|
-
var docsFetchInputSchema = {
|
|
285
|
-
url: z.string().url().describe(
|
|
286
|
-
'The full URL of the page to fetch (e.g., "https://docs.example.com/docs/getting-started")'
|
|
287
|
-
)
|
|
288
|
-
};
|
|
289
|
-
var docsFetchTool = {
|
|
290
|
-
name: "docs_fetch",
|
|
291
|
-
description: "Fetch the complete content of a documentation page. Use this after searching to get the full markdown content of a specific page.",
|
|
292
|
-
inputSchema: docsFetchInputSchema
|
|
293
|
-
};
|
|
294
|
-
function formatPageContent(doc) {
|
|
295
|
-
if (!doc) {
|
|
296
|
-
return "Page not found. Please check the URL and try again.";
|
|
297
|
-
}
|
|
298
|
-
const lines = [];
|
|
299
|
-
lines.push(`# ${doc.title}`);
|
|
300
|
-
lines.push("");
|
|
301
|
-
if (doc.description) {
|
|
302
|
-
lines.push(`> ${doc.description}`);
|
|
303
|
-
lines.push("");
|
|
304
|
-
}
|
|
305
|
-
if (doc.headings.length > 0) {
|
|
306
|
-
lines.push("## Contents");
|
|
307
|
-
lines.push("");
|
|
308
|
-
for (const heading of doc.headings) {
|
|
309
|
-
if (heading.level <= 3) {
|
|
310
|
-
const indent = " ".repeat(heading.level - 1);
|
|
311
|
-
lines.push(`${indent}- [${heading.text}](#${heading.id})`);
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
lines.push("");
|
|
315
|
-
lines.push("---");
|
|
316
|
-
lines.push("");
|
|
317
|
-
}
|
|
318
|
-
lines.push(doc.markdown);
|
|
319
|
-
return lines.join("\n");
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
// src/mcp/server.ts
|
|
323
|
-
function isFileConfig(config) {
|
|
324
|
-
return "docsPath" in config && "indexPath" in config;
|
|
325
|
-
}
|
|
326
|
-
function isDataConfig(config) {
|
|
327
|
-
return "docs" in config && "searchIndexData" in config;
|
|
328
|
-
}
|
|
329
|
-
var McpDocsServer = class {
|
|
330
|
-
config;
|
|
331
|
-
searchProvider = null;
|
|
332
|
-
mcpServer;
|
|
333
|
-
initialized = false;
|
|
334
|
-
constructor(config) {
|
|
335
|
-
this.config = config;
|
|
336
|
-
this.mcpServer = new McpServer(
|
|
337
|
-
{
|
|
338
|
-
name: config.name,
|
|
339
|
-
version: config.version ?? "1.0.0"
|
|
340
|
-
},
|
|
341
|
-
{
|
|
342
|
-
capabilities: {
|
|
343
|
-
tools: {}
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
);
|
|
347
|
-
this.registerTools();
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Register all MCP tools using definitions from tool files
|
|
351
|
-
*/
|
|
352
|
-
registerTools() {
|
|
353
|
-
this.mcpServer.registerTool(
|
|
354
|
-
docsSearchTool.name,
|
|
355
|
-
{
|
|
356
|
-
description: docsSearchTool.description,
|
|
357
|
-
inputSchema: docsSearchTool.inputSchema
|
|
358
|
-
},
|
|
359
|
-
async ({ query, limit }) => {
|
|
360
|
-
await this.initialize();
|
|
361
|
-
if (!this.searchProvider || !this.searchProvider.isReady()) {
|
|
362
|
-
return {
|
|
363
|
-
content: [{ type: "text", text: "Server not initialized. Please try again." }],
|
|
364
|
-
isError: true
|
|
365
|
-
};
|
|
366
|
-
}
|
|
367
|
-
try {
|
|
368
|
-
const results = await this.searchProvider.search(query, { limit });
|
|
369
|
-
return {
|
|
370
|
-
content: [{ type: "text", text: formatSearchResults(results) }]
|
|
371
|
-
};
|
|
372
|
-
} catch (error) {
|
|
373
|
-
console.error("[MCP] Search error:", error);
|
|
374
|
-
return {
|
|
375
|
-
content: [{ type: "text", text: `Search error: ${String(error)}` }],
|
|
376
|
-
isError: true
|
|
377
|
-
};
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
);
|
|
381
|
-
this.mcpServer.registerTool(
|
|
382
|
-
docsFetchTool.name,
|
|
383
|
-
{
|
|
384
|
-
description: docsFetchTool.description,
|
|
385
|
-
inputSchema: docsFetchTool.inputSchema
|
|
386
|
-
},
|
|
387
|
-
async ({ url }) => {
|
|
388
|
-
await this.initialize();
|
|
389
|
-
if (!this.searchProvider || !this.searchProvider.isReady()) {
|
|
390
|
-
return {
|
|
391
|
-
content: [{ type: "text", text: "Server not initialized. Please try again." }],
|
|
392
|
-
isError: true
|
|
393
|
-
};
|
|
394
|
-
}
|
|
395
|
-
try {
|
|
396
|
-
const doc = await this.getDocument(url);
|
|
397
|
-
return {
|
|
398
|
-
content: [{ type: "text", text: formatPageContent(doc) }]
|
|
399
|
-
};
|
|
400
|
-
} catch (error) {
|
|
401
|
-
console.error("[MCP] Fetch error:", error);
|
|
402
|
-
return {
|
|
403
|
-
content: [{ type: "text", text: `Error fetching page: ${String(error)}` }],
|
|
404
|
-
isError: true
|
|
405
|
-
};
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
);
|
|
409
|
-
}
|
|
410
|
-
/**
|
|
411
|
-
* Get a document by URL using the search provider
|
|
412
|
-
*/
|
|
413
|
-
async getDocument(url) {
|
|
414
|
-
if (!this.searchProvider) {
|
|
415
|
-
return null;
|
|
416
|
-
}
|
|
417
|
-
if (this.searchProvider.getDocument) {
|
|
418
|
-
return this.searchProvider.getDocument(url);
|
|
419
|
-
}
|
|
420
|
-
return null;
|
|
421
|
-
}
|
|
422
|
-
/**
|
|
423
|
-
* Load docs and search index using the configured search provider
|
|
424
|
-
*
|
|
425
|
-
* For file-based config: reads from disk
|
|
426
|
-
* For data config: uses pre-loaded data directly
|
|
427
|
-
*/
|
|
428
|
-
async initialize() {
|
|
429
|
-
if (this.initialized) {
|
|
430
|
-
return;
|
|
431
|
-
}
|
|
432
|
-
try {
|
|
433
|
-
const searchSpecifier = this.config.search ?? "flexsearch";
|
|
434
|
-
this.searchProvider = await loadSearchProvider(searchSpecifier);
|
|
435
|
-
const providerContext = {
|
|
436
|
-
baseUrl: this.config.baseUrl ?? "",
|
|
437
|
-
serverName: this.config.name,
|
|
438
|
-
serverVersion: this.config.version ?? "1.0.0",
|
|
439
|
-
outputDir: ""
|
|
440
|
-
// Not relevant for runtime
|
|
441
|
-
};
|
|
442
|
-
const initData = {};
|
|
443
|
-
if (isDataConfig(this.config)) {
|
|
444
|
-
initData.docs = this.config.docs;
|
|
445
|
-
initData.indexData = this.config.searchIndexData;
|
|
446
|
-
} else if (isFileConfig(this.config)) {
|
|
447
|
-
initData.docsPath = this.config.docsPath;
|
|
448
|
-
initData.indexPath = this.config.indexPath;
|
|
449
|
-
} else {
|
|
450
|
-
throw new Error("Invalid server config: must provide either file paths or pre-loaded data");
|
|
451
|
-
}
|
|
452
|
-
await this.searchProvider.initialize(providerContext, initData);
|
|
453
|
-
this.initialized = true;
|
|
454
|
-
} catch (error) {
|
|
455
|
-
console.error("[MCP] Failed to initialize:", error);
|
|
456
|
-
throw error;
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
/**
|
|
460
|
-
* Handle an HTTP request using the MCP SDK's transport
|
|
461
|
-
*
|
|
462
|
-
* This method is designed for serverless environments (Vercel, Netlify).
|
|
463
|
-
* It creates a stateless transport instance and processes the request.
|
|
464
|
-
*
|
|
465
|
-
* @param req - Node.js IncomingMessage or compatible request object
|
|
466
|
-
* @param res - Node.js ServerResponse or compatible response object
|
|
467
|
-
* @param parsedBody - Optional pre-parsed request body
|
|
468
|
-
*/
|
|
469
|
-
async handleHttpRequest(req, res, parsedBody) {
|
|
470
|
-
await this.initialize();
|
|
471
|
-
const transport = new StreamableHTTPServerTransport({
|
|
472
|
-
sessionIdGenerator: void 0,
|
|
473
|
-
// Stateless mode - no session tracking
|
|
474
|
-
enableJsonResponse: true
|
|
475
|
-
// Return JSON instead of SSE streams
|
|
476
|
-
});
|
|
477
|
-
await this.mcpServer.connect(transport);
|
|
478
|
-
try {
|
|
479
|
-
await transport.handleRequest(req, res, parsedBody);
|
|
480
|
-
} finally {
|
|
481
|
-
await transport.close();
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
/**
|
|
485
|
-
* Handle a Web Standard Request (Cloudflare Workers, Deno, Bun)
|
|
486
|
-
*
|
|
487
|
-
* This method is designed for Web Standard environments that use
|
|
488
|
-
* the Fetch API Request/Response pattern.
|
|
489
|
-
*
|
|
490
|
-
* @param request - Web Standard Request object
|
|
491
|
-
* @returns Web Standard Response object
|
|
492
|
-
*/
|
|
493
|
-
async handleWebRequest(request) {
|
|
494
|
-
await this.initialize();
|
|
495
|
-
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
496
|
-
sessionIdGenerator: void 0,
|
|
497
|
-
// Stateless mode
|
|
498
|
-
enableJsonResponse: true
|
|
499
|
-
});
|
|
500
|
-
await this.mcpServer.connect(transport);
|
|
501
|
-
try {
|
|
502
|
-
return await transport.handleRequest(request);
|
|
503
|
-
} finally {
|
|
504
|
-
await transport.close();
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
/**
|
|
508
|
-
* Get server status information
|
|
509
|
-
*
|
|
510
|
-
* Useful for health checks and debugging
|
|
511
|
-
*/
|
|
512
|
-
async getStatus() {
|
|
513
|
-
let docCount = 0;
|
|
514
|
-
if (this.searchProvider instanceof FlexSearchProvider) {
|
|
515
|
-
const docs = this.searchProvider.getDocs();
|
|
516
|
-
docCount = docs ? Object.keys(docs).length : 0;
|
|
517
|
-
}
|
|
518
|
-
return {
|
|
519
|
-
name: this.config.name,
|
|
520
|
-
version: this.config.version ?? "1.0.0",
|
|
521
|
-
initialized: this.initialized,
|
|
522
|
-
docCount,
|
|
523
|
-
baseUrl: this.config.baseUrl,
|
|
524
|
-
searchProvider: this.searchProvider?.name
|
|
525
|
-
};
|
|
526
|
-
}
|
|
527
|
-
/**
|
|
528
|
-
* Get the underlying McpServer instance
|
|
529
|
-
*
|
|
530
|
-
* Useful for advanced use cases like custom transports
|
|
531
|
-
*/
|
|
532
|
-
getMcpServer() {
|
|
533
|
-
return this.mcpServer;
|
|
534
|
-
}
|
|
535
|
-
};
|
|
536
|
-
|
|
537
|
-
// src/cli/verify.ts
|
|
538
|
-
async function verifyBuild(buildDir) {
|
|
539
|
-
const result = {
|
|
540
|
-
success: true,
|
|
541
|
-
docsFound: 0,
|
|
542
|
-
errors: [],
|
|
543
|
-
warnings: []
|
|
544
|
-
};
|
|
545
|
-
const mcpDir = path.join(buildDir, "mcp");
|
|
546
|
-
if (!await fs2.pathExists(mcpDir)) {
|
|
547
|
-
result.errors.push(`MCP directory not found: ${mcpDir}`);
|
|
548
|
-
result.errors.push('Did you run "npm run build" with the MCP plugin configured?');
|
|
549
|
-
result.success = false;
|
|
550
|
-
return result;
|
|
551
|
-
}
|
|
552
|
-
const requiredFiles = ["docs.json", "search-index.json", "manifest.json"];
|
|
553
|
-
for (const file of requiredFiles) {
|
|
554
|
-
const filePath = path.join(mcpDir, file);
|
|
555
|
-
if (!await fs2.pathExists(filePath)) {
|
|
556
|
-
result.errors.push(`Required file missing: ${filePath}`);
|
|
557
|
-
result.success = false;
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
if (!result.success) {
|
|
561
|
-
return result;
|
|
562
|
-
}
|
|
563
|
-
try {
|
|
564
|
-
const docsPath = path.join(mcpDir, "docs.json");
|
|
565
|
-
const docs = await fs2.readJson(docsPath);
|
|
566
|
-
if (typeof docs !== "object" || docs === null) {
|
|
567
|
-
result.errors.push("docs.json is not a valid object");
|
|
568
|
-
result.success = false;
|
|
569
|
-
} else {
|
|
570
|
-
result.docsFound = Object.keys(docs).length;
|
|
571
|
-
if (result.docsFound === 0) {
|
|
572
|
-
result.warnings.push("docs.json contains no documents");
|
|
573
|
-
}
|
|
574
|
-
for (const [route, doc] of Object.entries(docs)) {
|
|
575
|
-
const d = doc;
|
|
576
|
-
if (!d.title || typeof d.title !== "string") {
|
|
577
|
-
result.warnings.push(`Document ${route} is missing a title`);
|
|
578
|
-
}
|
|
579
|
-
if (!d.markdown || typeof d.markdown !== "string") {
|
|
580
|
-
result.warnings.push(`Document ${route} is missing markdown content`);
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
} catch (error) {
|
|
585
|
-
result.errors.push(`Failed to parse docs.json: ${error.message}`);
|
|
586
|
-
result.success = false;
|
|
587
|
-
}
|
|
588
|
-
try {
|
|
589
|
-
const indexPath = path.join(mcpDir, "search-index.json");
|
|
590
|
-
const indexData = await fs2.readJson(indexPath);
|
|
591
|
-
if (typeof indexData !== "object" || indexData === null) {
|
|
592
|
-
result.errors.push("search-index.json is not a valid object");
|
|
593
|
-
result.success = false;
|
|
594
|
-
}
|
|
595
|
-
} catch (error) {
|
|
596
|
-
result.errors.push(`Failed to parse search-index.json: ${error.message}`);
|
|
597
|
-
result.success = false;
|
|
598
|
-
}
|
|
599
|
-
try {
|
|
600
|
-
const manifestPath = path.join(mcpDir, "manifest.json");
|
|
601
|
-
const manifest = await fs2.readJson(manifestPath);
|
|
602
|
-
if (!manifest.name || typeof manifest.name !== "string") {
|
|
603
|
-
result.warnings.push("manifest.json is missing server name");
|
|
604
|
-
}
|
|
605
|
-
if (!manifest.version || typeof manifest.version !== "string") {
|
|
606
|
-
result.warnings.push("manifest.json is missing server version");
|
|
607
|
-
}
|
|
608
|
-
} catch (error) {
|
|
609
|
-
result.errors.push(`Failed to parse manifest.json: ${error.message}`);
|
|
610
|
-
result.success = false;
|
|
611
|
-
}
|
|
612
|
-
return result;
|
|
613
|
-
}
|
|
614
|
-
async function testServer(buildDir) {
|
|
615
|
-
const mcpDir = path.join(buildDir, "mcp");
|
|
616
|
-
const docsPath = path.join(mcpDir, "docs.json");
|
|
617
|
-
const indexPath = path.join(mcpDir, "search-index.json");
|
|
618
|
-
const manifestPath = path.join(mcpDir, "manifest.json");
|
|
619
|
-
try {
|
|
620
|
-
const manifest = await fs2.readJson(manifestPath);
|
|
621
|
-
const docs = await fs2.readJson(docsPath);
|
|
622
|
-
const searchIndexData = await fs2.readJson(indexPath);
|
|
623
|
-
const server = new McpDocsServer({
|
|
624
|
-
name: manifest.name || "test-docs",
|
|
625
|
-
version: manifest.version || "1.0.0",
|
|
626
|
-
docs,
|
|
627
|
-
searchIndexData
|
|
628
|
-
});
|
|
629
|
-
await server.initialize();
|
|
630
|
-
const status = await server.getStatus();
|
|
631
|
-
if (!status.initialized) {
|
|
632
|
-
return { success: false, message: "Server failed to initialize" };
|
|
633
|
-
}
|
|
634
|
-
if (status.docCount === 0) {
|
|
635
|
-
return { success: false, message: "Server has no documents loaded" };
|
|
636
|
-
}
|
|
637
|
-
return {
|
|
638
|
-
success: true,
|
|
639
|
-
message: `Server initialized with ${status.docCount} documents`
|
|
640
|
-
};
|
|
641
|
-
} catch (error) {
|
|
642
|
-
return {
|
|
643
|
-
success: false,
|
|
644
|
-
message: `Server test failed: ${error.message}`
|
|
645
|
-
};
|
|
646
|
-
}
|
|
647
|
-
}
|
|
648
|
-
async function main() {
|
|
649
|
-
const args = process.argv.slice(2);
|
|
650
|
-
const buildDir = args[0] || "./build";
|
|
651
|
-
console.log("");
|
|
652
|
-
console.log("\u{1F50D} MCP Build Verification");
|
|
653
|
-
console.log("=".repeat(50));
|
|
654
|
-
console.log(`Build directory: ${path.resolve(buildDir)}`);
|
|
655
|
-
console.log("");
|
|
656
|
-
console.log("\u{1F4C1} Checking build output...");
|
|
657
|
-
const verifyResult = await verifyBuild(buildDir);
|
|
658
|
-
if (verifyResult.errors.length > 0) {
|
|
659
|
-
console.log("");
|
|
660
|
-
console.log("\u274C Errors:");
|
|
661
|
-
for (const error of verifyResult.errors) {
|
|
662
|
-
console.log(` \u2022 ${error}`);
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
if (verifyResult.warnings.length > 0) {
|
|
666
|
-
console.log("");
|
|
667
|
-
console.log("\u26A0\uFE0F Warnings:");
|
|
668
|
-
for (const warning of verifyResult.warnings) {
|
|
669
|
-
console.log(` \u2022 ${warning}`);
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
if (!verifyResult.success) {
|
|
673
|
-
console.log("");
|
|
674
|
-
console.log("\u274C Build verification failed");
|
|
675
|
-
process.exit(1);
|
|
676
|
-
}
|
|
677
|
-
console.log(` \u2713 Found ${verifyResult.docsFound} documents`);
|
|
678
|
-
console.log(" \u2713 All required files present");
|
|
679
|
-
console.log(" \u2713 File structure valid");
|
|
680
|
-
console.log("");
|
|
681
|
-
console.log("\u{1F680} Testing MCP server...");
|
|
682
|
-
const serverResult = await testServer(buildDir);
|
|
683
|
-
if (!serverResult.success) {
|
|
684
|
-
console.log(` \u274C ${serverResult.message}`);
|
|
685
|
-
console.log("");
|
|
686
|
-
console.log("\u274C Server test failed");
|
|
687
|
-
process.exit(1);
|
|
688
|
-
}
|
|
689
|
-
console.log(` \u2713 ${serverResult.message}`);
|
|
690
|
-
console.log("");
|
|
691
|
-
console.log("\u2705 All checks passed!");
|
|
692
|
-
console.log("");
|
|
693
|
-
console.log("Next steps:");
|
|
694
|
-
console.log(" 1. Deploy your site to a hosting provider");
|
|
695
|
-
console.log(" 2. Configure MCP endpoint (see README for platform guides)");
|
|
696
|
-
console.log(" 3. Connect your AI tools to the MCP server");
|
|
697
|
-
console.log("");
|
|
698
|
-
process.exit(0);
|
|
699
|
-
}
|
|
700
|
-
main().catch((error) => {
|
|
701
|
-
console.error("Unexpected error:", error);
|
|
702
|
-
process.exit(1);
|
|
703
|
-
});
|
|
704
|
-
//# sourceMappingURL=verify.mjs.map
|
|
705
|
-
//# sourceMappingURL=verify.mjs.map
|