@daogee/mdgraph 1.0.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.
- package/CHANGELOG.md +25 -0
- package/LICENSE +21 -0
- package/README-ZH.md +203 -0
- package/README.md +203 -0
- package/agent-pack/README.md +26 -0
- package/agent-pack/host-examples.md +71 -0
- package/agent-pack/mcp-config.example.json +14 -0
- package/agent-pack/mdgraph-agent-instructions.md +21 -0
- package/agent-pack/prompts/relationship-trace.md +12 -0
- package/agent-pack/prompts/status-doctor.md +13 -0
- package/agent-pack/prompts/task-start-context.md +15 -0
- package/dist/analysis/doctor-scope.d.ts +8 -0
- package/dist/analysis/doctor-scope.js +109 -0
- package/dist/analysis/doctor.d.ts +148 -0
- package/dist/analysis/doctor.js +919 -0
- package/dist/analysis/status-freshness.d.ts +13 -0
- package/dist/analysis/status-freshness.js +55 -0
- package/dist/benchmark/benchmark.d.ts +118 -0
- package/dist/benchmark/benchmark.js +357 -0
- package/dist/bin/mdgraph.d.ts +2 -0
- package/dist/bin/mdgraph.js +877 -0
- package/dist/bundle/bundle.d.ts +66 -0
- package/dist/bundle/bundle.js +320 -0
- package/dist/config/limits.d.ts +24 -0
- package/dist/config/limits.js +24 -0
- package/dist/config/load-config.d.ts +14 -0
- package/dist/config/load-config.js +177 -0
- package/dist/db/connection.d.ts +15 -0
- package/dist/db/connection.js +145 -0
- package/dist/db/repositories.d.ts +159 -0
- package/dist/db/repositories.js +929 -0
- package/dist/db/schema.sql +110 -0
- package/dist/db/sqlite-adapter.d.ts +18 -0
- package/dist/db/sqlite-adapter.js +60 -0
- package/dist/diff/graph-diff.d.ts +41 -0
- package/dist/diff/graph-diff.js +334 -0
- package/dist/evaluation/retrieval-eval.d.ts +115 -0
- package/dist/evaluation/retrieval-eval.js +399 -0
- package/dist/export/diagram.d.ts +11 -0
- package/dist/export/diagram.js +41 -0
- package/dist/export/graphjson.d.ts +89 -0
- package/dist/export/graphjson.js +299 -0
- package/dist/export/markdown-index.d.ts +21 -0
- package/dist/export/markdown-index.js +89 -0
- package/dist/export/source-bridge.d.ts +22 -0
- package/dist/export/source-bridge.js +83 -0
- package/dist/extraction/entity-extractor.d.ts +12 -0
- package/dist/extraction/entity-extractor.js +153 -0
- package/dist/extraction/graph-builder.d.ts +2 -0
- package/dist/extraction/graph-builder.js +168 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +28 -0
- package/dist/indexer.d.ts +19 -0
- package/dist/indexer.js +100 -0
- package/dist/mcp/debug.d.ts +7 -0
- package/dist/mcp/debug.js +41 -0
- package/dist/mcp/server-instructions.d.ts +2 -0
- package/dist/mcp/server-instructions.js +40 -0
- package/dist/mcp/server.d.ts +28 -0
- package/dist/mcp/server.js +232 -0
- package/dist/mcp/tools.d.ts +31 -0
- package/dist/mcp/tools.js +506 -0
- package/dist/mcp/transport.d.ts +50 -0
- package/dist/mcp/transport.js +88 -0
- package/dist/parser/frontmatter.d.ts +10 -0
- package/dist/parser/frontmatter.js +191 -0
- package/dist/parser/links.d.ts +6 -0
- package/dist/parser/links.js +9 -0
- package/dist/parser/markdown-parser.d.ts +2 -0
- package/dist/parser/markdown-parser.js +267 -0
- package/dist/query/context-builder.d.ts +85 -0
- package/dist/query/context-builder.js +413 -0
- package/dist/query/search.d.ts +32 -0
- package/dist/query/search.js +237 -0
- package/dist/query/trace.d.ts +10 -0
- package/dist/query/trace.js +55 -0
- package/dist/reporting/report.d.ts +49 -0
- package/dist/reporting/report.js +143 -0
- package/dist/resolution/link-resolver.d.ts +17 -0
- package/dist/resolution/link-resolver.js +113 -0
- package/dist/scanner/file-scanner.d.ts +4 -0
- package/dist/scanner/file-scanner.js +144 -0
- package/dist/semantic/local-embedding.d.ts +4 -0
- package/dist/semantic/local-embedding.js +30 -0
- package/dist/semantic/status.d.ts +19 -0
- package/dist/semantic/status.js +70 -0
- package/dist/semantic/vector-codec.d.ts +3 -0
- package/dist/semantic/vector-codec.js +35 -0
- package/dist/types.d.ts +198 -0
- package/dist/types.js +15 -0
- package/dist/utils/bounded-json.d.ts +9 -0
- package/dist/utils/bounded-json.js +57 -0
- package/dist/utils/content-risk.d.ts +5 -0
- package/dist/utils/content-risk.js +20 -0
- package/dist/utils/fts.d.ts +3 -0
- package/dist/utils/fts.js +52 -0
- package/dist/utils/id.d.ts +2 -0
- package/dist/utils/id.js +8 -0
- package/dist/utils/path-safety.d.ts +4 -0
- package/dist/utils/path-safety.js +25 -0
- package/dist/utils/text.d.ts +7 -0
- package/dist/utils/text.js +36 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +11 -0
- package/dist/watcher/file-watcher.d.ts +11 -0
- package/dist/watcher/file-watcher.js +127 -0
- package/docs/EN/Agent_Integration.md +129 -0
- package/docs/EN/Architecture.md +135 -0
- package/docs/EN/Evaluation_Questions.md +68 -0
- package/docs/EN/Output_Contracts.md +274 -0
- package/docs/EN/Public_Contracts.md +88 -0
- package/docs/EN/Public_Contracts_1.0.md +234 -0
- package/docs/EN/Release_Checklist.md +82 -0
- package/docs/ZH/Agent_Integration.md +105 -0
- package/docs/ZH/Architecture.md +124 -0
- package/docs/ZH/Evaluation_Questions.md +66 -0
- package/docs/ZH/Output_Contracts.md +269 -0
- package/docs/ZH/Public_Contracts.md +81 -0
- package/docs/ZH/Public_Contracts_1.0.md +202 -0
- package/docs/ZH/Release_Checklist.md +83 -0
- package/package.json +94 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { ErrorCodes, StdioTransport } from "./transport.js";
|
|
5
|
+
import { errorMessage, initializeErrorMessage, internalToolErrorMessage, mcpDebug, mcpErrorData } from "./debug.js";
|
|
6
|
+
import { SERVER_INSTRUCTIONS, SERVER_INSTRUCTIONS_UNINDEXED } from "./server-instructions.js";
|
|
7
|
+
import { McpInputError, ToolHandler, hasIndex, tools } from "./tools.js";
|
|
8
|
+
import { packageVersion } from "../version.js";
|
|
9
|
+
import { isPathInsideOrEqual } from "../utils/path-safety.js";
|
|
10
|
+
import { watchProject } from "../watcher/file-watcher.js";
|
|
11
|
+
export const PROTOCOL_VERSION = "2024-11-05";
|
|
12
|
+
export class MCPServer {
|
|
13
|
+
transport;
|
|
14
|
+
boundProjectRoot;
|
|
15
|
+
projectRoot;
|
|
16
|
+
toolHandler;
|
|
17
|
+
watchEnabled;
|
|
18
|
+
watchSemantic;
|
|
19
|
+
watchDebounceMs;
|
|
20
|
+
watchHandle;
|
|
21
|
+
watchRoot;
|
|
22
|
+
constructor(transport, options = {}) {
|
|
23
|
+
this.transport = transport;
|
|
24
|
+
this.boundProjectRoot = validatedProjectRoot(path.resolve(options.projectRoot ?? process.cwd()));
|
|
25
|
+
this.projectRoot = this.boundProjectRoot;
|
|
26
|
+
this.toolHandler = new ToolHandler(this.projectRoot, this.boundProjectRoot);
|
|
27
|
+
this.watchEnabled = options.watch ?? true;
|
|
28
|
+
this.watchSemantic = options.semantic;
|
|
29
|
+
this.watchDebounceMs = options.debounceMs;
|
|
30
|
+
}
|
|
31
|
+
start() {
|
|
32
|
+
mcpDebug("server start", {
|
|
33
|
+
boundProjectRoot: this.boundProjectRoot,
|
|
34
|
+
watch: this.watchEnabled,
|
|
35
|
+
semantic: this.watchSemantic ?? false,
|
|
36
|
+
debounceMs: this.watchDebounceMs ?? null
|
|
37
|
+
});
|
|
38
|
+
this.transport.start(this.handleMessage.bind(this));
|
|
39
|
+
}
|
|
40
|
+
async stop() {
|
|
41
|
+
mcpDebug("server stop", { projectRoot: this.projectRoot, watchRoot: this.watchRoot ?? null });
|
|
42
|
+
await this.stopWatch();
|
|
43
|
+
this.transport.stop();
|
|
44
|
+
}
|
|
45
|
+
async handleMessage(message) {
|
|
46
|
+
const isRequest = "id" in message;
|
|
47
|
+
switch (message.method) {
|
|
48
|
+
case "initialize":
|
|
49
|
+
if (isRequest) {
|
|
50
|
+
mcpDebug("initialize request", { id: message.id });
|
|
51
|
+
await this.handleInitialize(message);
|
|
52
|
+
}
|
|
53
|
+
return;
|
|
54
|
+
case "initialized":
|
|
55
|
+
return;
|
|
56
|
+
case "ping":
|
|
57
|
+
if (isRequest) {
|
|
58
|
+
this.transport.sendResult(message.id, {});
|
|
59
|
+
}
|
|
60
|
+
return;
|
|
61
|
+
case "tools/list":
|
|
62
|
+
if (isRequest) {
|
|
63
|
+
mcpDebug("tools/list request", { id: message.id });
|
|
64
|
+
this.transport.sendResult(message.id, { tools });
|
|
65
|
+
}
|
|
66
|
+
return;
|
|
67
|
+
case "tools/call":
|
|
68
|
+
if (isRequest) {
|
|
69
|
+
mcpDebug("tools/call request", { id: message.id });
|
|
70
|
+
this.handleToolsCall(message);
|
|
71
|
+
}
|
|
72
|
+
return;
|
|
73
|
+
case "resources/list":
|
|
74
|
+
if (isRequest) {
|
|
75
|
+
this.transport.sendResult(message.id, { resources: [] });
|
|
76
|
+
}
|
|
77
|
+
return;
|
|
78
|
+
case "resources/templates/list":
|
|
79
|
+
if (isRequest) {
|
|
80
|
+
this.transport.sendResult(message.id, { resourceTemplates: [] });
|
|
81
|
+
}
|
|
82
|
+
return;
|
|
83
|
+
case "prompts/list":
|
|
84
|
+
if (isRequest) {
|
|
85
|
+
this.transport.sendResult(message.id, { prompts: [] });
|
|
86
|
+
}
|
|
87
|
+
return;
|
|
88
|
+
default:
|
|
89
|
+
if (isRequest) {
|
|
90
|
+
this.transport.sendError(message.id, ErrorCodes.MethodNotFound, `Method not found: ${message.method}`, mcpErrorData("protocol.dispatch", { method: message.method }));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async handleInitialize(request) {
|
|
95
|
+
let projectRoot;
|
|
96
|
+
try {
|
|
97
|
+
projectRoot = validatedProjectRoot(projectRootFromInitialize(request.params) ?? this.projectRoot);
|
|
98
|
+
if (!isPathInsideOrEqual(this.boundProjectRoot, projectRoot)) {
|
|
99
|
+
throw new Error(`Initialize root must stay inside served project root: ${this.boundProjectRoot}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
mcpDebug("initialize rejected", { id: request.id, error: errorMessage(error) });
|
|
104
|
+
this.transport.sendError(request.id, ErrorCodes.InvalidParams, error instanceof Error ? error.message : String(error), mcpErrorData("initialize.params", { projectRoot: this.projectRoot }));
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
await this.ensureWatch(projectRoot);
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
mcpDebug("initialize watch failure", { id: request.id, projectRoot, error: errorMessage(error) });
|
|
112
|
+
this.transport.sendError(request.id, ErrorCodes.InternalError, initializeErrorMessage(error), mcpErrorData("initialize.watch", { projectRoot }));
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
this.projectRoot = projectRoot;
|
|
116
|
+
this.toolHandler = new ToolHandler(projectRoot, this.boundProjectRoot);
|
|
117
|
+
mcpDebug("initialize resolved", { id: request.id, projectRoot, indexed: hasIndex(projectRoot) });
|
|
118
|
+
this.transport.sendResult(request.id, {
|
|
119
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
120
|
+
capabilities: { tools: {} },
|
|
121
|
+
serverInfo: { name: "mdgraph", version: packageVersion() },
|
|
122
|
+
instructions: hasIndex(projectRoot) ? SERVER_INSTRUCTIONS : SERVER_INSTRUCTIONS_UNINDEXED
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
handleToolsCall(request) {
|
|
126
|
+
const params = request.params;
|
|
127
|
+
if (!params || typeof params.name !== "string") {
|
|
128
|
+
this.transport.sendError(request.id, ErrorCodes.InvalidParams, "Missing tool name", mcpErrorData("tools.call.params"));
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const tool = tools.find((candidate) => candidate.name === params.name);
|
|
132
|
+
if (!tool) {
|
|
133
|
+
this.transport.sendError(request.id, ErrorCodes.InvalidParams, `Unknown tool: ${params.name}`, mcpErrorData("tools.call.params", { tool: params.name }));
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
const args = params.arguments && typeof params.arguments === "object" ? params.arguments : {};
|
|
138
|
+
const result = this.toolHandler.execute(params.name, args);
|
|
139
|
+
mcpDebug("tools/call result", {
|
|
140
|
+
id: request.id,
|
|
141
|
+
tool: params.name,
|
|
142
|
+
isError: result.isError ?? false,
|
|
143
|
+
hasStructuredContent: result.structuredContent !== undefined
|
|
144
|
+
});
|
|
145
|
+
this.transport.sendResult(request.id, result);
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
const code = error instanceof McpInputError ? ErrorCodes.InvalidParams : ErrorCodes.InternalError;
|
|
149
|
+
mcpDebug("tools/call failure", { id: request.id, tool: params.name, code, error: errorMessage(error) });
|
|
150
|
+
this.transport.sendError(request.id, code, code === ErrorCodes.InvalidParams ? errorMessage(error) : internalToolErrorMessage(error), mcpErrorData(code === ErrorCodes.InvalidParams ? "tools.call.input" : "tools.call.execute", { tool: params.name }));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
async ensureWatch(projectRoot) {
|
|
154
|
+
if (!this.watchEnabled) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (this.watchRoot === projectRoot && this.watchHandle) {
|
|
158
|
+
mcpDebug("watch reuse", { projectRoot });
|
|
159
|
+
await this.watchHandle;
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
await this.stopWatch();
|
|
163
|
+
this.watchRoot = projectRoot;
|
|
164
|
+
mcpDebug("watch start", { projectRoot, semantic: this.watchSemantic ?? false, debounceMs: this.watchDebounceMs ?? null });
|
|
165
|
+
const watchHandle = watchProject(projectRoot, {
|
|
166
|
+
semantic: this.watchSemantic,
|
|
167
|
+
debounceMs: this.watchDebounceMs,
|
|
168
|
+
onIndexed: (result) => {
|
|
169
|
+
if (result.changed > 0 || result.deleted > 0 || result.skipped > 0) {
|
|
170
|
+
console.error(`MDGraph MCP watch indexed ${result.changed} changed, ${result.deleted} deleted, ${result.unchanged} unchanged, ${result.skipped} skipped document(s).`);
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
onError: (error) => {
|
|
174
|
+
console.error(`MDGraph MCP watch failed: ${error.message}`);
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
this.watchHandle = watchHandle;
|
|
178
|
+
try {
|
|
179
|
+
await watchHandle;
|
|
180
|
+
mcpDebug("watch ready", { projectRoot });
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
if (this.watchHandle === watchHandle) {
|
|
184
|
+
this.watchHandle = undefined;
|
|
185
|
+
this.watchRoot = undefined;
|
|
186
|
+
}
|
|
187
|
+
mcpDebug("watch startup failure", { projectRoot, error: errorMessage(error) });
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
async stopWatch() {
|
|
192
|
+
if (!this.watchHandle) {
|
|
193
|
+
this.watchRoot = undefined;
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
const watchHandle = this.watchHandle;
|
|
197
|
+
this.watchHandle = undefined;
|
|
198
|
+
this.watchRoot = undefined;
|
|
199
|
+
const handle = await watchHandle;
|
|
200
|
+
mcpDebug("watch stop", { projectRoot: this.projectRoot });
|
|
201
|
+
await handle.close();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
export function startStdioMcpServer(options = {}) {
|
|
205
|
+
const server = new MCPServer(new StdioTransport(), options);
|
|
206
|
+
server.start();
|
|
207
|
+
return server;
|
|
208
|
+
}
|
|
209
|
+
function projectRootFromInitialize(params) {
|
|
210
|
+
if (!params || typeof params !== "object") {
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
const record = params;
|
|
214
|
+
if (typeof record.rootUri === "string") {
|
|
215
|
+
return fileUriToPath(record.rootUri);
|
|
216
|
+
}
|
|
217
|
+
const firstFolder = record.workspaceFolders?.[0]?.uri;
|
|
218
|
+
return typeof firstFolder === "string" ? fileUriToPath(firstFolder) : undefined;
|
|
219
|
+
}
|
|
220
|
+
function fileUriToPath(uri) {
|
|
221
|
+
return path.resolve(fileURLToPath(uri));
|
|
222
|
+
}
|
|
223
|
+
function validatedProjectRoot(projectRoot) {
|
|
224
|
+
const resolved = path.resolve(projectRoot);
|
|
225
|
+
if (!fs.existsSync(resolved)) {
|
|
226
|
+
throw new Error(`Project root does not exist: ${resolved}`);
|
|
227
|
+
}
|
|
228
|
+
if (!fs.statSync(resolved).isDirectory()) {
|
|
229
|
+
throw new Error(`Project root is not a directory: ${resolved}`);
|
|
230
|
+
}
|
|
231
|
+
return resolved;
|
|
232
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface McpToolDefinition {
|
|
2
|
+
name: string;
|
|
3
|
+
description: string;
|
|
4
|
+
inputSchema: {
|
|
5
|
+
type: "object";
|
|
6
|
+
properties: Record<string, unknown>;
|
|
7
|
+
required?: string[];
|
|
8
|
+
additionalProperties: boolean;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export interface McpToolResult {
|
|
12
|
+
content: Array<{
|
|
13
|
+
type: "text";
|
|
14
|
+
text: string;
|
|
15
|
+
}>;
|
|
16
|
+
structuredContent?: unknown;
|
|
17
|
+
isError?: boolean;
|
|
18
|
+
}
|
|
19
|
+
export declare class McpInputError extends Error {
|
|
20
|
+
name: string;
|
|
21
|
+
}
|
|
22
|
+
export declare const tools: McpToolDefinition[];
|
|
23
|
+
export declare class ToolHandler {
|
|
24
|
+
private readonly defaultProjectRoot;
|
|
25
|
+
private readonly boundProjectRoot;
|
|
26
|
+
constructor(defaultProjectRoot?: string, boundProjectRoot?: string);
|
|
27
|
+
getTools(): McpToolDefinition[];
|
|
28
|
+
execute(name: string, args?: Record<string, unknown>): McpToolResult;
|
|
29
|
+
private withRepository;
|
|
30
|
+
}
|
|
31
|
+
export declare function hasIndex(projectRoot: string): boolean;
|