mn-docs-mcp 0.1.7 → 0.2.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/mcp/cli.mjs +38 -2
- package/mcp/lib.mjs +23 -1
- package/mcp/server-http.mjs +40 -5
- package/mcp/server.mjs +38 -4
- package/package.json +1 -1
package/mcp/cli.mjs
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
|
|
3
7
|
const args = process.argv.slice(2);
|
|
4
8
|
|
|
5
9
|
let mode = 'stdio';
|
|
6
10
|
let port;
|
|
7
11
|
let prebuild = false;
|
|
8
|
-
let silent =
|
|
12
|
+
let silent = false;
|
|
13
|
+
let root;
|
|
9
14
|
|
|
10
15
|
for (let i = 0; i < args.length; i += 1) {
|
|
11
16
|
const arg = args[i];
|
|
@@ -25,15 +30,28 @@ for (let i = 0; i < args.length; i += 1) {
|
|
|
25
30
|
silent = false;
|
|
26
31
|
continue;
|
|
27
32
|
}
|
|
33
|
+
if (arg === '--silent') {
|
|
34
|
+
silent = true;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
28
37
|
if (arg === '--port' && args[i + 1]) {
|
|
29
38
|
port = args[i + 1];
|
|
30
39
|
i += 1;
|
|
31
40
|
continue;
|
|
32
41
|
}
|
|
42
|
+
if (arg === '--root' && args[i + 1]) {
|
|
43
|
+
root = args[i + 1];
|
|
44
|
+
i += 1;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
33
47
|
if (arg?.startsWith('--port=')) {
|
|
34
48
|
port = arg.split('=')[1];
|
|
35
49
|
continue;
|
|
36
50
|
}
|
|
51
|
+
if (arg?.startsWith('--root=')) {
|
|
52
|
+
root = arg.split('=')[1];
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
37
55
|
if (arg === '--help' || arg === '-h') {
|
|
38
56
|
process.stderr.write(
|
|
39
57
|
[
|
|
@@ -41,7 +59,9 @@ for (let i = 0; i < args.length; i += 1) {
|
|
|
41
59
|
' --http 启动HTTPStream模式(默认stdio)',
|
|
42
60
|
' --port <port> HTTP端口(默认8788)',
|
|
43
61
|
' --prebuild 启动后后台预构建索引',
|
|
44
|
-
' --verbose 输出日志(
|
|
62
|
+
' --verbose 输出日志(覆盖--silent)',
|
|
63
|
+
' --silent 关闭开屏与日志',
|
|
64
|
+
' --root <path> 指定文档仓库根目录',
|
|
45
65
|
' --stdio 显式使用stdio模式',
|
|
46
66
|
].join('\n') + '\n'
|
|
47
67
|
);
|
|
@@ -49,13 +69,29 @@ for (let i = 0; i < args.length; i += 1) {
|
|
|
49
69
|
}
|
|
50
70
|
}
|
|
51
71
|
|
|
72
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
73
|
+
const __dirname = path.dirname(__filename);
|
|
74
|
+
const packagePath = path.join(__dirname, '../package.json');
|
|
75
|
+
let version = '0.0.0';
|
|
76
|
+
try {
|
|
77
|
+
const raw = fs.readFileSync(packagePath, 'utf-8');
|
|
78
|
+
version = JSON.parse(raw).version || version;
|
|
79
|
+
} catch {}
|
|
80
|
+
|
|
52
81
|
if (mode === 'stdio') {
|
|
53
82
|
process.env.MCP_STDIO = '1';
|
|
54
83
|
process.env.MCP_SILENT = silent ? '1' : '0';
|
|
84
|
+
process.env.MN_DOCS_VERSION = version;
|
|
85
|
+
process.env.MN_DOCS_MODE = 'stdio';
|
|
86
|
+
process.env.MN_DOCS_ROOT = root || process.env.MN_DOCS_ROOT || process.cwd();
|
|
55
87
|
if (prebuild) process.env.MCP_PREBUILD = '1';
|
|
56
88
|
await import('./server.mjs');
|
|
57
89
|
} else {
|
|
58
90
|
if (port) process.env.MCP_HTTP_PORT = String(port);
|
|
91
|
+
process.env.MN_DOCS_VERSION = version;
|
|
92
|
+
process.env.MN_DOCS_MODE = 'http';
|
|
93
|
+
if (port) process.env.MN_DOCS_PORT = String(port);
|
|
94
|
+
process.env.MN_DOCS_ROOT = root || process.env.MN_DOCS_ROOT || process.cwd();
|
|
59
95
|
if (prebuild) process.env.MCP_PREBUILD = '1';
|
|
60
96
|
await import('./server-http.mjs');
|
|
61
97
|
}
|
package/mcp/lib.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import fs from 'node:fs/promises';
|
|
2
|
+
import fsSync from 'node:fs';
|
|
2
3
|
import path from 'node:path';
|
|
3
4
|
import { fileURLToPath } from 'node:url';
|
|
4
5
|
import matter from 'gray-matter';
|
|
@@ -9,7 +10,28 @@ import dotenv from 'dotenv';
|
|
|
9
10
|
const __filename = fileURLToPath(import.meta.url);
|
|
10
11
|
const __dirname = path.dirname(__filename);
|
|
11
12
|
|
|
12
|
-
const
|
|
13
|
+
const DEFAULT_ROOT = path.resolve(__dirname, '..');
|
|
14
|
+
|
|
15
|
+
function resolveRootDir() {
|
|
16
|
+
const envRoot = (process.env.MN_DOCS_ROOT || '').trim();
|
|
17
|
+
if (envRoot) return envRoot;
|
|
18
|
+
const cwd = process.cwd();
|
|
19
|
+
const docsFromCwd = path.join(cwd, 'src', 'content', 'docs');
|
|
20
|
+
try {
|
|
21
|
+
if (fsSyncExists(docsFromCwd)) return cwd;
|
|
22
|
+
} catch {}
|
|
23
|
+
return DEFAULT_ROOT;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function fsSyncExists(p) {
|
|
27
|
+
try {
|
|
28
|
+
return fsSync.statSync(p).isDirectory();
|
|
29
|
+
} catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const ROOT_DIR = resolveRootDir();
|
|
13
35
|
const DOCS_DIR = path.join(ROOT_DIR, 'src', 'content', 'docs');
|
|
14
36
|
const MCP_DIR = path.join(ROOT_DIR, '.mcp');
|
|
15
37
|
const INDEX_PATH = path.join(MCP_DIR, 'index.json');
|
package/mcp/server-http.mjs
CHANGED
|
@@ -4,6 +4,42 @@ import { buildIndex, getPaths, isIndexStale, loadIndex, searchDocs } from './lib
|
|
|
4
4
|
|
|
5
5
|
const TOOL_NAME = 'search_docs';
|
|
6
6
|
const PORT = Number(process.env.MCP_HTTP_PORT || 8788);
|
|
7
|
+
const IS_SILENT = process.env.MCP_SILENT === '1';
|
|
8
|
+
const NO_COLOR = process.env.MCP_NO_COLOR === '1';
|
|
9
|
+
|
|
10
|
+
function color(text, code) {
|
|
11
|
+
if (NO_COLOR) return text;
|
|
12
|
+
return `\x1b[${code}m${text}\x1b[0m`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function stripAnsi(text) {
|
|
16
|
+
return text.replace(/\x1b\[[0-9;]*m/g, '');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function padLine(line, width) {
|
|
20
|
+
const len = stripAnsi(line).length;
|
|
21
|
+
const padding = width - len;
|
|
22
|
+
return line + (padding > 0 ? ' '.repeat(padding) : '');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function renderSplash() {
|
|
26
|
+
if (IS_SILENT) return;
|
|
27
|
+
const version = process.env.MN_DOCS_VERSION || '0.0.0';
|
|
28
|
+
const mode = process.env.MN_DOCS_MODE || 'http';
|
|
29
|
+
const port = process.env.MN_DOCS_PORT || String(PORT);
|
|
30
|
+
const contentLines = [
|
|
31
|
+
color(`mn-docs-mcp v${version}`, '38;5;45'),
|
|
32
|
+
color(`模式: ${mode} 端口: ${port}`, '38;5;39'),
|
|
33
|
+
];
|
|
34
|
+
const maxWidth = Math.max(...contentLines.map((line) => stripAnsi(line).length)) + 4;
|
|
35
|
+
const top = color('╭' + '─'.repeat(maxWidth) + '╮', '38;5;45');
|
|
36
|
+
const bottom = color('╰' + '─'.repeat(maxWidth) + '╯', '38;5;45');
|
|
37
|
+
const body = contentLines.map((line) => {
|
|
38
|
+
const padded = padLine(line, maxWidth - 4);
|
|
39
|
+
return color('│', '38;5;45') + ' ' + padded + ' ' + color('│', '38;5;45');
|
|
40
|
+
});
|
|
41
|
+
process.stdout.write([top, ...body, bottom].join('\n') + '\n');
|
|
42
|
+
}
|
|
7
43
|
|
|
8
44
|
async function ensureIndex() {
|
|
9
45
|
const { INDEX_PATH } = getPaths();
|
|
@@ -67,8 +103,7 @@ await server.start({
|
|
|
67
103
|
},
|
|
68
104
|
});
|
|
69
105
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
106
|
+
renderSplash();
|
|
107
|
+
|
|
108
|
+
// 默认自动构建,异步启动避免阻塞握手
|
|
109
|
+
setTimeout(() => initIndexInBackground(), 0);
|
package/mcp/server.mjs
CHANGED
|
@@ -4,12 +4,46 @@ import { buildIndex, getPaths, isIndexStale, loadIndex, searchDocs } from './lib
|
|
|
4
4
|
|
|
5
5
|
const TOOL_NAME = 'search_docs';
|
|
6
6
|
const IS_SILENT = process.env.MCP_SILENT === '1';
|
|
7
|
+
const NO_COLOR = process.env.MCP_NO_COLOR === '1';
|
|
7
8
|
|
|
8
9
|
function logError(...args) {
|
|
9
10
|
if (IS_SILENT) return;
|
|
10
11
|
console.error(...args);
|
|
11
12
|
}
|
|
12
13
|
|
|
14
|
+
function color(text, code) {
|
|
15
|
+
if (NO_COLOR) return text;
|
|
16
|
+
return `\x1b[${code}m${text}\x1b[0m`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function stripAnsi(text) {
|
|
20
|
+
return text.replace(/\x1b\[[0-9;]*m/g, '');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function padLine(line, width) {
|
|
24
|
+
const len = stripAnsi(line).length;
|
|
25
|
+
const padding = width - len;
|
|
26
|
+
return line + (padding > 0 ? ' '.repeat(padding) : '');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function renderSplash() {
|
|
30
|
+
if (IS_SILENT) return;
|
|
31
|
+
const version = process.env.MN_DOCS_VERSION || '0.0.0';
|
|
32
|
+
const mode = process.env.MN_DOCS_MODE || 'stdio';
|
|
33
|
+
const contentLines = [
|
|
34
|
+
color(`mn-docs-mcp v${version}`, '38;5;45'),
|
|
35
|
+
color(`模式: ${mode}`, '38;5;39'),
|
|
36
|
+
];
|
|
37
|
+
const maxWidth = Math.max(...contentLines.map((line) => stripAnsi(line).length)) + 4;
|
|
38
|
+
const top = color('╭' + '─'.repeat(maxWidth) + '╮', '38;5;45');
|
|
39
|
+
const bottom = color('╰' + '─'.repeat(maxWidth) + '╯', '38;5;45');
|
|
40
|
+
const body = contentLines.map((line) => {
|
|
41
|
+
const padded = padLine(line, maxWidth - 4);
|
|
42
|
+
return color('│', '38;5;45') + ' ' + padded + ' ' + color('│', '38;5;45');
|
|
43
|
+
});
|
|
44
|
+
process.stderr.write([top, ...body, bottom].join('\n') + '\n');
|
|
45
|
+
}
|
|
46
|
+
|
|
13
47
|
async function ensureIndex() {
|
|
14
48
|
const { INDEX_PATH } = getPaths();
|
|
15
49
|
try {
|
|
@@ -85,7 +119,7 @@ await server.start({
|
|
|
85
119
|
transportType: 'stdio',
|
|
86
120
|
});
|
|
87
121
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
122
|
+
renderSplash();
|
|
123
|
+
|
|
124
|
+
// 默认自动构建,异步启动避免阻塞握手
|
|
125
|
+
setTimeout(() => initIndexInBackground(), 0);
|