micro-models-agent 0.57.0 → 0.57.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/CHANGELOG.md +481 -476
- package/README.md +358 -358
- package/dist/certification/certifications.json +493 -493
- package/dist/cli/commands.js +447 -0
- package/dist/cli/completer.js +167 -0
- package/dist/cli/index.js +2 -0
- package/dist/cli/main.js +153 -0
- package/dist/cli/plugin-commands.js +36 -0
- package/dist/cli/repl-commands.js +761 -0
- package/dist/cli/repl.js +702 -0
- package/dist/cli/run-result.js +33 -0
- package/dist/cli/security-commands.js +164 -0
- package/dist/cli/setup.js +237 -0
- package/dist/config/config.js +276 -0
- package/dist/config/defaults.js +141 -0
- package/dist/config/domains.js +179 -0
- package/dist/config/experts.js +15 -0
- package/dist/config/index.js +4 -0
- package/dist/config/security.js +213 -0
- package/dist/config/types.js +1 -0
- package/dist/core/agent-moe.js +102 -0
- package/dist/core/agent.js +1018 -0
- package/dist/core/bootstrap.js +481 -0
- package/dist/core/crash-handler.js +51 -0
- package/dist/core/environment.js +199 -0
- package/dist/core/index.js +2 -0
- package/dist/core/prompt-builder.js +76 -0
- package/dist/core/session-logger.js +251 -0
- package/dist/core/types.js +1 -0
- package/dist/core/version.js +26 -0
- package/dist/core/workspace.js +76 -0
- package/dist/i18n/en.json +679 -0
- package/dist/i18n/index.js +46 -0
- package/dist/i18n/ru.json +679 -0
- package/dist/index.js +22 -0
- package/dist/llm/image-utils.js +143 -0
- package/dist/llm/index.js +4 -0
- package/dist/llm/model-loader.js +78 -0
- package/dist/llm/openai-compat.js +497 -0
- package/dist/llm/orchestrator.js +200 -0
- package/dist/llm/provider.js +10 -0
- package/dist/llm/response.js +39 -0
- package/dist/llm/token-counter.js +39 -0
- package/dist/llm/types.js +1 -0
- package/dist/logger/app-logger.js +189 -0
- package/dist/logger/file-log.js +151 -0
- package/dist/logger/index.js +1 -0
- package/dist/migration/backup.js +45 -0
- package/dist/migration/detect.js +50 -0
- package/dist/migration/index.js +2 -0
- package/dist/modules/artifacts/store.js +61 -0
- package/dist/modules/browser/actions.js +76 -0
- package/dist/modules/browser/bridge-client.js +199 -0
- package/dist/modules/browser/bridge-path.js +10 -0
- package/dist/modules/browser/bridge-server.mjs +219 -219
- package/dist/modules/browser/cookie-store.js +24 -0
- package/dist/modules/browser/driver.js +136 -0
- package/dist/modules/browser/index.js +7 -0
- package/dist/modules/browser/module.js +29 -0
- package/dist/modules/browser/session.js +342 -0
- package/dist/modules/browser/snapshot.js +148 -0
- package/dist/modules/browser/types.js +12 -0
- package/dist/modules/certification/cli.js +213 -0
- package/dist/modules/certification/fact-checker.js +82 -0
- package/dist/modules/certification/loader.js +106 -0
- package/dist/modules/certification/manifest.js +58 -0
- package/dist/modules/certification/runner.js +245 -0
- package/dist/modules/certification/scenarios.js +407 -0
- package/dist/modules/certification/types.js +1 -0
- package/dist/modules/context/chunk-query.js +100 -0
- package/dist/modules/context/fact-extractor.js +168 -0
- package/dist/modules/context/history.js +15 -0
- package/dist/modules/context/index.js +1 -0
- package/dist/modules/context/manager.js +440 -0
- package/dist/modules/execution/audit-runners.js +206 -0
- package/dist/modules/execution/auditor.js +218 -0
- package/dist/modules/execution/execution-plugin.js +431 -0
- package/dist/modules/execution/index.js +8 -0
- package/dist/modules/execution/module.js +625 -0
- package/dist/modules/execution/moe-executor.js +304 -0
- package/dist/modules/execution/plan-coverage.js +68 -0
- package/dist/modules/execution/plan-persister.js +46 -0
- package/dist/modules/execution/plan-store.js +196 -0
- package/dist/modules/execution/plan-tool.js +677 -0
- package/dist/modules/execution/plan-validator.js +153 -0
- package/dist/modules/execution/planner.js +94 -0
- package/dist/modules/execution/stuck-detector.js +746 -0
- package/dist/modules/execution/tracker.js +69 -0
- package/dist/modules/execution/types.js +1 -0
- package/dist/modules/execution/verifier.js +235 -0
- package/dist/modules/execution/windows-commands.js +41 -0
- package/dist/modules/hallucination/confidence.js +66 -0
- package/dist/modules/hallucination/consistency.js +26 -0
- package/dist/modules/hallucination/detector.js +47 -0
- package/dist/modules/hallucination/factual.js +169 -0
- package/dist/modules/hallucination/index.js +5 -0
- package/dist/modules/hallucination/js-identifiers.js +262 -0
- package/dist/modules/hallucination/llm-judge.js +101 -0
- package/dist/modules/index.js +5 -0
- package/dist/modules/indexer/cache.js +40 -0
- package/dist/modules/indexer/index.js +3 -0
- package/dist/modules/indexer/module.js +246 -0
- package/dist/modules/indexer/project-profile.js +183 -0
- package/dist/modules/indexer/walker.js +101 -0
- package/dist/modules/lsp/check-tool.js +58 -0
- package/dist/modules/lsp/client.js +389 -0
- package/dist/modules/lsp/command.js +60 -0
- package/dist/modules/lsp/config.js +135 -0
- package/dist/modules/lsp/index.js +3 -0
- package/dist/modules/lsp/module.js +260 -0
- package/dist/modules/lsp/probe.js +86 -0
- package/dist/modules/lsp/project-root.js +32 -0
- package/dist/modules/lsp/startup-check.js +144 -0
- package/dist/modules/lsp/types.js +1 -0
- package/dist/modules/mcp/client.js +399 -0
- package/dist/modules/mcp/index.js +3 -0
- package/dist/modules/mcp/module.js +142 -0
- package/dist/modules/mcp/registry.js +15 -0
- package/dist/modules/memory/index.js +1 -0
- package/dist/modules/memory/module.js +96 -0
- package/dist/modules/memory/search.js +42 -0
- package/dist/modules/memory/store.js +69 -0
- package/dist/modules/pipelines/engine.js +60 -0
- package/dist/modules/pipelines/index.js +3 -0
- package/dist/modules/pipelines/parser.js +56 -0
- package/dist/modules/pipelines/template.js +14 -0
- package/dist/modules/plugins/builtin/lint-on-write.js +334 -0
- package/dist/modules/plugins/builtin/notify.js +9 -0
- package/dist/modules/plugins/index.js +1 -0
- package/dist/modules/plugins/loader.js +70 -0
- package/dist/modules/plugins/manager.js +261 -0
- package/dist/modules/plugins/types.js +1 -0
- package/dist/modules/pricing/index.js +61 -0
- package/dist/modules/pricing/prices.js +129 -0
- package/dist/modules/processes/detect.js +34 -0
- package/dist/modules/processes/index.js +2 -0
- package/dist/modules/processes/registry.js +327 -0
- package/dist/modules/processes/runner.js +23 -0
- package/dist/modules/providers/create.js +22 -0
- package/dist/modules/providers/fallback.js +79 -0
- package/dist/modules/providers/health.js +46 -0
- package/dist/modules/providers/index.js +5 -0
- package/dist/modules/providers/manager.js +161 -0
- package/dist/modules/providers/presets.js +128 -0
- package/dist/modules/providers/registry.js +22 -0
- package/dist/modules/providers/types.js +1 -0
- package/dist/modules/registry.js +48 -0
- package/dist/modules/security/audit-log.js +136 -0
- package/dist/modules/security/audit-notifier.js +292 -0
- package/dist/modules/security/command-validator.js +219 -0
- package/dist/modules/security/content-scanner.js +53 -0
- package/dist/modules/security/data-sanitizer.js +89 -0
- package/dist/modules/security/encryption.js +242 -0
- package/dist/modules/security/index.js +14 -0
- package/dist/modules/security/network-validator.js +88 -0
- package/dist/modules/security/path-validator.js +203 -0
- package/dist/modules/security/rate-limiter.js +119 -0
- package/dist/modules/security/security-policies.js +531 -0
- package/dist/modules/security/session-encryption.js +210 -0
- package/dist/modules/security/session-isolation.js +95 -0
- package/dist/modules/session/index.js +3 -0
- package/dist/modules/session/manager.js +172 -0
- package/dist/modules/session/module.js +24 -0
- package/dist/modules/session/store.js +222 -0
- package/dist/modules/session/types.js +1 -0
- package/dist/modules/skills/index.js +2 -0
- package/dist/modules/skills/loader.js +72 -0
- package/dist/modules/skills/matcher.js +27 -0
- package/dist/modules/skills/module.js +129 -0
- package/dist/modules/types.js +1 -0
- package/dist/modules/updater/checker.js +96 -0
- package/dist/modules/updater/index.js +2 -0
- package/dist/modules/updater/module.js +116 -0
- package/dist/modules/user-profile/compressor.js +16 -0
- package/dist/modules/user-profile/index.js +1 -0
- package/dist/modules/user-profile/profile.js +68 -0
- package/dist/skills/builtin/git.md +36 -36
- package/dist/skills/builtin/typescript.md +35 -35
- package/dist/tools/approve.js +33 -0
- package/dist/tools/attach-image.js +101 -0
- package/dist/tools/bash.js +519 -0
- package/dist/tools/browser.js +115 -0
- package/dist/tools/chunk-query.js +100 -0
- package/dist/tools/create-dir.js +56 -0
- package/dist/tools/delete-file.js +63 -0
- package/dist/tools/download-file.js +117 -0
- package/dist/tools/edit-file.js +80 -0
- package/dist/tools/enable-tools.js +59 -0
- package/dist/tools/executor.js +154 -0
- package/dist/tools/file-info.js +47 -0
- package/dist/tools/filter-tools.js +17 -0
- package/dist/tools/glob-tool.js +27 -0
- package/dist/tools/grep-tool.js +125 -0
- package/dist/tools/hidden-tools-block.js +37 -0
- package/dist/tools/index.js +78 -0
- package/dist/tools/list-dir.js +49 -0
- package/dist/tools/load-skill.js +43 -0
- package/dist/tools/mcp-call.js +69 -0
- package/dist/tools/move-file.js +86 -0
- package/dist/tools/path-utils.js +101 -0
- package/dist/tools/pipeline-run.js +145 -0
- package/dist/tools/preview.js +2 -0
- package/dist/tools/process-kill.js +40 -0
- package/dist/tools/process-list.js +37 -0
- package/dist/tools/process-log.js +54 -0
- package/dist/tools/question.js +141 -0
- package/dist/tools/read-file.js +179 -0
- package/dist/tools/recall.js +118 -0
- package/dist/tools/registry.js +47 -0
- package/dist/tools/remember.js +68 -0
- package/dist/tools/scope-check.js +32 -0
- package/dist/tools/search-history.js +85 -0
- package/dist/tools/subagent.js +196 -0
- package/dist/tools/types.js +1 -0
- package/dist/tools/user-input.js +123 -0
- package/dist/tools/web-browse.js +87 -0
- package/dist/tools/web-fetch.js +119 -0
- package/dist/tools/web-search.js +105 -0
- package/dist/tools/write-file.js +82 -0
- package/dist/ui/box.js +77 -0
- package/dist/ui/colors.js +4 -0
- package/dist/ui/diff.js +178 -0
- package/dist/ui/index.js +6 -0
- package/dist/ui/line-editor.js +822 -0
- package/dist/ui/line-math.js +73 -0
- package/dist/ui/md-formatter.js +212 -0
- package/dist/ui/output.js +13 -0
- package/dist/ui/plan-view.js +103 -0
- package/dist/ui/renderer.js +259 -0
- package/dist/ui/spinner.js +70 -0
- package/dist/ui/table.js +144 -0
- package/package.json +51 -51
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { spawn } from "child_process";
|
|
2
|
+
export class MCPClient {
|
|
3
|
+
serverName;
|
|
4
|
+
config;
|
|
5
|
+
process = null;
|
|
6
|
+
_connected = false;
|
|
7
|
+
abortController = null;
|
|
8
|
+
pendingResolve = null;
|
|
9
|
+
pendingReject = null;
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.config = config;
|
|
12
|
+
this.serverName = config.name;
|
|
13
|
+
}
|
|
14
|
+
isConnected() {
|
|
15
|
+
return this._connected;
|
|
16
|
+
}
|
|
17
|
+
async connect() {
|
|
18
|
+
if (this._connected)
|
|
19
|
+
return;
|
|
20
|
+
if (this.config.transport === "http") {
|
|
21
|
+
this._connected = true;
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (this.config.transport === "sse" || this.config.url) {
|
|
25
|
+
return this.connectSSE();
|
|
26
|
+
}
|
|
27
|
+
return this.connectStdio();
|
|
28
|
+
}
|
|
29
|
+
async connectSSE() {
|
|
30
|
+
const url = this.config.url;
|
|
31
|
+
if (!url)
|
|
32
|
+
throw new Error("SSE transport requires a url");
|
|
33
|
+
this.abortController = new AbortController();
|
|
34
|
+
const response = await fetch(url, {
|
|
35
|
+
method: "GET",
|
|
36
|
+
headers: {
|
|
37
|
+
Accept: "text/event-stream",
|
|
38
|
+
...this.config.headers,
|
|
39
|
+
},
|
|
40
|
+
signal: this.abortController.signal,
|
|
41
|
+
});
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
throw new Error(`SSE connection failed: ${response.status} ${response.statusText}`);
|
|
44
|
+
}
|
|
45
|
+
this._connected = true;
|
|
46
|
+
const reader = response.body.getReader();
|
|
47
|
+
const decoder = new TextDecoder();
|
|
48
|
+
let buffer = "";
|
|
49
|
+
this.readSSE(reader, decoder, buffer);
|
|
50
|
+
}
|
|
51
|
+
async readSSE(reader, decoder, buffer) {
|
|
52
|
+
try {
|
|
53
|
+
while (this._connected) {
|
|
54
|
+
const { done, value } = await reader.read();
|
|
55
|
+
if (done)
|
|
56
|
+
break;
|
|
57
|
+
buffer += decoder.decode(value, { stream: true });
|
|
58
|
+
const lines = buffer.split("\n");
|
|
59
|
+
buffer = lines.pop() || "";
|
|
60
|
+
let eventType = "";
|
|
61
|
+
let data = "";
|
|
62
|
+
for (const line of lines) {
|
|
63
|
+
if (line.startsWith("event: ")) {
|
|
64
|
+
eventType = line.slice(7).trim();
|
|
65
|
+
}
|
|
66
|
+
else if (line.startsWith("data: ")) {
|
|
67
|
+
data = line.slice(6);
|
|
68
|
+
}
|
|
69
|
+
else if (line === "") {
|
|
70
|
+
if (eventType === "message" && data) {
|
|
71
|
+
this.handleSSEMessage(data);
|
|
72
|
+
}
|
|
73
|
+
eventType = "";
|
|
74
|
+
data = "";
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
if (err.name !== "AbortError") {
|
|
81
|
+
this.handleSSEError(err);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
this._connected = false;
|
|
85
|
+
}
|
|
86
|
+
handleSSEMessage(data) {
|
|
87
|
+
try {
|
|
88
|
+
const msg = JSON.parse(data);
|
|
89
|
+
if (this.pendingResolve && msg.id) {
|
|
90
|
+
if (msg.error) {
|
|
91
|
+
this.pendingReject?.(new Error(msg.error.message));
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
this.pendingResolve(msg.result);
|
|
95
|
+
}
|
|
96
|
+
this.pendingResolve = null;
|
|
97
|
+
this.pendingReject = null;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
catch { }
|
|
101
|
+
}
|
|
102
|
+
handleSSEError(err) {
|
|
103
|
+
if (this.pendingReject) {
|
|
104
|
+
this.pendingReject(err);
|
|
105
|
+
this.pendingResolve = null;
|
|
106
|
+
this.pendingReject = null;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
connectStdio() {
|
|
110
|
+
return new Promise((resolve, reject) => {
|
|
111
|
+
const child = spawn(this.config.command, this.config.args || [], {
|
|
112
|
+
env: { ...process.env, ...this.config.env },
|
|
113
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
114
|
+
});
|
|
115
|
+
let _resolved = false;
|
|
116
|
+
child.on("error", (err) => {
|
|
117
|
+
this._connected = false;
|
|
118
|
+
if (!_resolved) {
|
|
119
|
+
_resolved = true;
|
|
120
|
+
reject(err);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
child.on("exit", (code) => {
|
|
124
|
+
this._connected = false;
|
|
125
|
+
if (code !== 0 && !_resolved) {
|
|
126
|
+
_resolved = true;
|
|
127
|
+
reject(new Error(`MCP server exited with code ${code}`));
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
child.on("spawn", () => {
|
|
131
|
+
this._connected = true;
|
|
132
|
+
this.process = child;
|
|
133
|
+
_resolved = true;
|
|
134
|
+
resolve();
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
async listTools() {
|
|
139
|
+
if (!this._connected) {
|
|
140
|
+
await this.connect();
|
|
141
|
+
}
|
|
142
|
+
if (this.config.transport === "http") {
|
|
143
|
+
return this.listToolsHTTP();
|
|
144
|
+
}
|
|
145
|
+
if (this.config.transport === "sse" || this.config.url) {
|
|
146
|
+
return this.listToolsSSE();
|
|
147
|
+
}
|
|
148
|
+
return this.listToolsStdio();
|
|
149
|
+
}
|
|
150
|
+
async listToolsHTTP() {
|
|
151
|
+
const request = {
|
|
152
|
+
jsonrpc: "2.0",
|
|
153
|
+
id: Date.now(),
|
|
154
|
+
method: "tools/list",
|
|
155
|
+
};
|
|
156
|
+
const response = await fetch(this.config.url, {
|
|
157
|
+
method: "POST",
|
|
158
|
+
headers: {
|
|
159
|
+
"Content-Type": "application/json",
|
|
160
|
+
...this.config.headers,
|
|
161
|
+
},
|
|
162
|
+
body: JSON.stringify(request),
|
|
163
|
+
});
|
|
164
|
+
if (!response.ok) {
|
|
165
|
+
const text = await response.text().catch(() => "");
|
|
166
|
+
throw new Error(`MCP listTools failed: ${response.status} ${text}`);
|
|
167
|
+
}
|
|
168
|
+
const result = await response.json();
|
|
169
|
+
if (result.error) {
|
|
170
|
+
throw new Error(result.error.message);
|
|
171
|
+
}
|
|
172
|
+
return result.result?.tools || [];
|
|
173
|
+
}
|
|
174
|
+
async listToolsSSE() {
|
|
175
|
+
const request = {
|
|
176
|
+
jsonrpc: "2.0",
|
|
177
|
+
id: Date.now(),
|
|
178
|
+
method: "tools/list",
|
|
179
|
+
};
|
|
180
|
+
const response = await fetch(this.config.url, {
|
|
181
|
+
method: "POST",
|
|
182
|
+
headers: {
|
|
183
|
+
"Content-Type": "application/json",
|
|
184
|
+
...this.config.headers,
|
|
185
|
+
},
|
|
186
|
+
body: JSON.stringify(request),
|
|
187
|
+
signal: this.abortController?.signal,
|
|
188
|
+
});
|
|
189
|
+
if (!response.ok) {
|
|
190
|
+
const text = await response.text().catch(() => "");
|
|
191
|
+
throw new Error(`MCP listTools failed: ${response.status} ${text}`);
|
|
192
|
+
}
|
|
193
|
+
const result = await response.json();
|
|
194
|
+
if (result.error) {
|
|
195
|
+
throw new Error(result.error.message);
|
|
196
|
+
}
|
|
197
|
+
return result.result?.tools || [];
|
|
198
|
+
}
|
|
199
|
+
async listToolsStdio() {
|
|
200
|
+
if (!this.process) {
|
|
201
|
+
throw new Error("Not connected to MCP server");
|
|
202
|
+
}
|
|
203
|
+
return new Promise((resolve, reject) => {
|
|
204
|
+
const request = {
|
|
205
|
+
jsonrpc: "2.0",
|
|
206
|
+
id: Date.now(),
|
|
207
|
+
method: "tools/list",
|
|
208
|
+
};
|
|
209
|
+
const requestStr = JSON.stringify(request) + "\n";
|
|
210
|
+
this.process.stdin.write(requestStr);
|
|
211
|
+
let responseData = "";
|
|
212
|
+
const onData = (data) => {
|
|
213
|
+
responseData += data.toString();
|
|
214
|
+
if (responseData.includes("\n")) {
|
|
215
|
+
this.process.stdout.off("data", onData);
|
|
216
|
+
try {
|
|
217
|
+
const lines = responseData.split("\n").filter((l) => l.trim());
|
|
218
|
+
for (const line of lines) {
|
|
219
|
+
const response = JSON.parse(line);
|
|
220
|
+
if (response.id === request.id) {
|
|
221
|
+
if (response.error) {
|
|
222
|
+
reject(new Error(response.error.message));
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
resolve(response.result?.tools || []);
|
|
226
|
+
}
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
reject(new Error("No matching response found"));
|
|
231
|
+
}
|
|
232
|
+
catch (e) {
|
|
233
|
+
reject(new Error(`Parse error: ${e.message}`));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
this.process.stdout.on("data", onData);
|
|
238
|
+
setTimeout(() => {
|
|
239
|
+
this.process.stdout.off("data", onData);
|
|
240
|
+
reject(new Error("MCP listTools timeout"));
|
|
241
|
+
}, 30000);
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
async disconnect() {
|
|
245
|
+
if (this.config.transport === "http") {
|
|
246
|
+
this._connected = false;
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
if (this.config.transport === "sse" || this.config.url) {
|
|
250
|
+
this.abortController?.abort();
|
|
251
|
+
this.abortController = null;
|
|
252
|
+
}
|
|
253
|
+
else if (this.process) {
|
|
254
|
+
this.process.kill();
|
|
255
|
+
this.process = null;
|
|
256
|
+
}
|
|
257
|
+
this._connected = false;
|
|
258
|
+
}
|
|
259
|
+
async callTool(toolName, args) {
|
|
260
|
+
if (!this._connected) {
|
|
261
|
+
throw new Error("Not connected to MCP server");
|
|
262
|
+
}
|
|
263
|
+
if (this.config.transport === "http") {
|
|
264
|
+
return this.callToolHTTP(toolName, args);
|
|
265
|
+
}
|
|
266
|
+
if (this.config.transport === "sse" || this.config.url) {
|
|
267
|
+
return this.callToolSSE(toolName, args);
|
|
268
|
+
}
|
|
269
|
+
return this.callToolStdio(toolName, args);
|
|
270
|
+
}
|
|
271
|
+
async callToolHTTP(toolName, args) {
|
|
272
|
+
const request = {
|
|
273
|
+
jsonrpc: "2.0",
|
|
274
|
+
id: Date.now(),
|
|
275
|
+
method: "tools/call",
|
|
276
|
+
params: {
|
|
277
|
+
name: toolName,
|
|
278
|
+
arguments: args,
|
|
279
|
+
},
|
|
280
|
+
};
|
|
281
|
+
const response = await fetch(this.config.url, {
|
|
282
|
+
method: "POST",
|
|
283
|
+
headers: {
|
|
284
|
+
"Content-Type": "application/json",
|
|
285
|
+
...this.config.headers,
|
|
286
|
+
},
|
|
287
|
+
body: JSON.stringify(request),
|
|
288
|
+
});
|
|
289
|
+
if (!response.ok) {
|
|
290
|
+
const text = await response.text().catch(() => "");
|
|
291
|
+
throw new Error(`MCP call failed: ${response.status} ${text}`);
|
|
292
|
+
}
|
|
293
|
+
const result = await response.json();
|
|
294
|
+
if (result.error) {
|
|
295
|
+
throw new Error(result.error.message);
|
|
296
|
+
}
|
|
297
|
+
return result.result || result;
|
|
298
|
+
}
|
|
299
|
+
async callToolSSE(toolName, args) {
|
|
300
|
+
const request = {
|
|
301
|
+
jsonrpc: "2.0",
|
|
302
|
+
id: Date.now(),
|
|
303
|
+
method: "tools/call",
|
|
304
|
+
params: {
|
|
305
|
+
name: toolName,
|
|
306
|
+
arguments: args,
|
|
307
|
+
},
|
|
308
|
+
};
|
|
309
|
+
return new Promise((resolve, reject) => {
|
|
310
|
+
this.pendingResolve = resolve;
|
|
311
|
+
this.pendingReject = reject;
|
|
312
|
+
const timeout = this.config.timeout || 30000;
|
|
313
|
+
const timer = setTimeout(() => {
|
|
314
|
+
this.pendingResolve = null;
|
|
315
|
+
this.pendingReject = null;
|
|
316
|
+
reject(new Error("MCP call timeout"));
|
|
317
|
+
}, timeout);
|
|
318
|
+
fetch(this.config.url, {
|
|
319
|
+
method: "POST",
|
|
320
|
+
headers: {
|
|
321
|
+
"Content-Type": "application/json",
|
|
322
|
+
...this.config.headers,
|
|
323
|
+
},
|
|
324
|
+
body: JSON.stringify(request),
|
|
325
|
+
signal: this.abortController?.signal,
|
|
326
|
+
})
|
|
327
|
+
.then(async (response) => {
|
|
328
|
+
clearTimeout(timer);
|
|
329
|
+
if (!response.ok) {
|
|
330
|
+
const text = await response.text().catch(() => "");
|
|
331
|
+
reject(new Error(`MCP POST failed: ${response.status} ${text}`));
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
const result = await response.json();
|
|
335
|
+
if (result.error) {
|
|
336
|
+
reject(new Error(result.error.message));
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
resolve(result.result || result);
|
|
340
|
+
}
|
|
341
|
+
})
|
|
342
|
+
.catch((err) => {
|
|
343
|
+
clearTimeout(timer);
|
|
344
|
+
if (err.name !== "AbortError") {
|
|
345
|
+
reject(err);
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
callToolStdio(toolName, args) {
|
|
351
|
+
if (!this.process) {
|
|
352
|
+
throw new Error("Not connected to MCP server");
|
|
353
|
+
}
|
|
354
|
+
return new Promise((resolve, reject) => {
|
|
355
|
+
const request = {
|
|
356
|
+
jsonrpc: "2.0",
|
|
357
|
+
id: Date.now(),
|
|
358
|
+
method: "tools/call",
|
|
359
|
+
params: {
|
|
360
|
+
name: toolName,
|
|
361
|
+
arguments: args,
|
|
362
|
+
},
|
|
363
|
+
};
|
|
364
|
+
const requestStr = JSON.stringify(request) + "\n";
|
|
365
|
+
this.process.stdin.write(requestStr);
|
|
366
|
+
let responseData = "";
|
|
367
|
+
const onData = (data) => {
|
|
368
|
+
responseData += data.toString();
|
|
369
|
+
if (responseData.includes("\n")) {
|
|
370
|
+
this.process.stdout.off("data", onData);
|
|
371
|
+
try {
|
|
372
|
+
const lines = responseData.split("\n").filter((l) => l.trim());
|
|
373
|
+
for (const line of lines) {
|
|
374
|
+
const response = JSON.parse(line);
|
|
375
|
+
if (response.id === request.id) {
|
|
376
|
+
if (response.error) {
|
|
377
|
+
reject(new Error(response.error.message));
|
|
378
|
+
}
|
|
379
|
+
else {
|
|
380
|
+
resolve(response.result);
|
|
381
|
+
}
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
reject(new Error("No matching response found"));
|
|
386
|
+
}
|
|
387
|
+
catch (e) {
|
|
388
|
+
reject(new Error(`Parse error: ${e.message}`));
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
this.process.stdout.on("data", onData);
|
|
393
|
+
setTimeout(() => {
|
|
394
|
+
this.process.stdout.off("data", onData);
|
|
395
|
+
reject(new Error("MCP call timeout"));
|
|
396
|
+
}, 30000);
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { MCPClient } from "./client";
|
|
2
|
+
import { MCPRegistry } from "./registry";
|
|
3
|
+
function sanitizeToolName(server, tool) {
|
|
4
|
+
const safeServer = server.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
5
|
+
const safeTool = tool.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
6
|
+
return `mcp__${safeServer}__${safeTool}`;
|
|
7
|
+
}
|
|
8
|
+
function convertInputSchema(schema) {
|
|
9
|
+
if (schema && typeof schema === "object" && "properties" in schema) {
|
|
10
|
+
const required = Array.isArray(schema.required) ? schema.required : [];
|
|
11
|
+
return {
|
|
12
|
+
type: "object",
|
|
13
|
+
properties: schema.properties,
|
|
14
|
+
required: required.length > 0 ? required : undefined,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return { type: "object", properties: {} };
|
|
18
|
+
}
|
|
19
|
+
export class MCPModule {
|
|
20
|
+
config;
|
|
21
|
+
name = "mcp";
|
|
22
|
+
connections = new Map();
|
|
23
|
+
discovered = [];
|
|
24
|
+
initialized = false;
|
|
25
|
+
initError = null;
|
|
26
|
+
constructor(config) {
|
|
27
|
+
this.config = config;
|
|
28
|
+
}
|
|
29
|
+
async initialize() {
|
|
30
|
+
if (this.initialized)
|
|
31
|
+
return;
|
|
32
|
+
this.initialized = true;
|
|
33
|
+
const servers = this.config.mcpServers || {};
|
|
34
|
+
const serverEntries = Object.entries(servers).filter(([, cfg]) => cfg.enabled !== false);
|
|
35
|
+
if (serverEntries.length === 0)
|
|
36
|
+
return;
|
|
37
|
+
const registry = new MCPRegistry();
|
|
38
|
+
for (const [key, cfg] of serverEntries) {
|
|
39
|
+
const serverConfig = {
|
|
40
|
+
name: cfg.name || key,
|
|
41
|
+
command: cfg.command,
|
|
42
|
+
args: cfg.args,
|
|
43
|
+
env: cfg.env,
|
|
44
|
+
transport: cfg.transport,
|
|
45
|
+
url: cfg.url,
|
|
46
|
+
headers: cfg.headers,
|
|
47
|
+
timeout: cfg.timeout,
|
|
48
|
+
};
|
|
49
|
+
registry.register(serverConfig);
|
|
50
|
+
}
|
|
51
|
+
for (const serverName of registry.list()) {
|
|
52
|
+
const serverConfig = registry.get(serverName);
|
|
53
|
+
const client = new MCPClient(serverConfig);
|
|
54
|
+
try {
|
|
55
|
+
await client.connect();
|
|
56
|
+
const tools = await client.listTools();
|
|
57
|
+
this.connections.set(serverName, client);
|
|
58
|
+
for (const tool of tools) {
|
|
59
|
+
this.discovered.push({
|
|
60
|
+
serverName,
|
|
61
|
+
toolName: tool.name,
|
|
62
|
+
description: tool.description || `Tool on MCP server "${serverName}"`,
|
|
63
|
+
inputSchema: tool.inputSchema || {},
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch (e) {
|
|
68
|
+
this.initError = `MCP server "${serverName}" init failed: ${e.message}`;
|
|
69
|
+
await client.disconnect().catch(() => { });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
getSystemPromptBlock() {
|
|
74
|
+
if (this.discovered.length === 0)
|
|
75
|
+
return null;
|
|
76
|
+
const lines = ["---", "MCP servers available:"];
|
|
77
|
+
const byServer = new Map();
|
|
78
|
+
for (const d of this.discovered) {
|
|
79
|
+
const list = byServer.get(d.serverName) || [];
|
|
80
|
+
list.push(d);
|
|
81
|
+
byServer.set(d.serverName, list);
|
|
82
|
+
}
|
|
83
|
+
for (const [server, tools] of byServer) {
|
|
84
|
+
lines.push(` [${server}]`);
|
|
85
|
+
for (const t of tools) {
|
|
86
|
+
const toolRef = sanitizeToolName(server, t.toolName);
|
|
87
|
+
lines.push(` - ${toolRef}: ${t.description}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
lines.push("---");
|
|
91
|
+
return {
|
|
92
|
+
content: lines.join("\n"),
|
|
93
|
+
priority: "high",
|
|
94
|
+
essential: false,
|
|
95
|
+
estimatedTokens: this.discovered.length * 20 + 20,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
getToolDefinitions() {
|
|
99
|
+
return this.discovered.map((d) => ({
|
|
100
|
+
name: sanitizeToolName(d.serverName, d.toolName),
|
|
101
|
+
description: `${d.description} [MCP server: ${d.serverName}]`,
|
|
102
|
+
parameters: convertInputSchema(d.inputSchema),
|
|
103
|
+
tags: ["code", "research"],
|
|
104
|
+
handler: async (_ctx, args) => {
|
|
105
|
+
const client = this.connections.get(d.serverName);
|
|
106
|
+
if (!client) {
|
|
107
|
+
return {
|
|
108
|
+
success: false,
|
|
109
|
+
output: `MCP server "${d.serverName}" not connected`,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
if (!client.isConnected()) {
|
|
114
|
+
await client.connect();
|
|
115
|
+
}
|
|
116
|
+
const result = await client.callTool(d.toolName, args);
|
|
117
|
+
return {
|
|
118
|
+
success: true,
|
|
119
|
+
output: typeof result === "string" ? result : JSON.stringify(result, null, 2),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
catch (e) {
|
|
123
|
+
return { success: false, output: `MCP call failed: ${e.message}` };
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
}));
|
|
127
|
+
}
|
|
128
|
+
getPlugin() {
|
|
129
|
+
return {
|
|
130
|
+
name: "mcp_module",
|
|
131
|
+
priority: 0,
|
|
132
|
+
onSessionEnd: async () => {
|
|
133
|
+
for (const client of this.connections.values()) {
|
|
134
|
+
try {
|
|
135
|
+
await client.disconnect();
|
|
136
|
+
}
|
|
137
|
+
catch { }
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export class MCPRegistry {
|
|
2
|
+
servers = new Map();
|
|
3
|
+
register(config) {
|
|
4
|
+
this.servers.set(config.name, config);
|
|
5
|
+
}
|
|
6
|
+
get(name) {
|
|
7
|
+
return this.servers.get(name);
|
|
8
|
+
}
|
|
9
|
+
list() {
|
|
10
|
+
return Array.from(this.servers.keys()).sort();
|
|
11
|
+
}
|
|
12
|
+
remove(name) {
|
|
13
|
+
this.servers.delete(name);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { MemoryStore } from "./store";
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { homedir } from "os";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import { MemoryStore } from "./store";
|
|
4
|
+
import { rememberTool } from "../../tools/remember";
|
|
5
|
+
import { recallTool } from "../../tools/recall";
|
|
6
|
+
const MEMORY_MD_FILES = ["errors", "conventions", "decisions", "facts"];
|
|
7
|
+
export class MemoryModule {
|
|
8
|
+
name = "memory";
|
|
9
|
+
store;
|
|
10
|
+
constructor(storeOrDir) {
|
|
11
|
+
if (storeOrDir instanceof MemoryStore) {
|
|
12
|
+
this.store = storeOrDir;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
const dir = storeOrDir || join(homedir(), ".mma", "memory");
|
|
16
|
+
this.store = new MemoryStore(dir);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
getSystemPromptBlock() {
|
|
20
|
+
const parts = [];
|
|
21
|
+
const prefs = this.store.getPreferences();
|
|
22
|
+
if (Object.keys(prefs).length > 0) {
|
|
23
|
+
const prefStr = Object.entries(prefs)
|
|
24
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
25
|
+
.join(", ");
|
|
26
|
+
parts.push(`User preferences: ${prefStr}`);
|
|
27
|
+
}
|
|
28
|
+
const memoryTail = this.getMemoryTail();
|
|
29
|
+
if (memoryTail)
|
|
30
|
+
parts.push(memoryTail);
|
|
31
|
+
if (parts.length === 0)
|
|
32
|
+
return null;
|
|
33
|
+
const content = parts.join("\n\n");
|
|
34
|
+
return {
|
|
35
|
+
content,
|
|
36
|
+
priority: "normal",
|
|
37
|
+
essential: false,
|
|
38
|
+
estimatedTokens: Math.ceil(content.length / 4) + 10,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Bounded tail of the append-only .md memory files (errors, conventions,
|
|
43
|
+
* decisions, facts). This is what makes past-session lessons visible in the
|
|
44
|
+
* system prompt — without it the loop's error rules are write-only.
|
|
45
|
+
*/
|
|
46
|
+
getMemoryTail() {
|
|
47
|
+
const sections = [];
|
|
48
|
+
const tailPerFile = 3;
|
|
49
|
+
for (const name of MEMORY_MD_FILES) {
|
|
50
|
+
const content = this.store.read(name).trim();
|
|
51
|
+
if (!content)
|
|
52
|
+
continue;
|
|
53
|
+
const entries = content
|
|
54
|
+
.split(/\r?\n/)
|
|
55
|
+
.map((l) => l.trim())
|
|
56
|
+
.filter((l) => l.startsWith("- "))
|
|
57
|
+
.slice(-tailPerFile);
|
|
58
|
+
if (entries.length === 0)
|
|
59
|
+
continue;
|
|
60
|
+
sections.push(`[Memory ${name}]:\n${entries.join("\n")}`);
|
|
61
|
+
}
|
|
62
|
+
if (sections.length === 0)
|
|
63
|
+
return null;
|
|
64
|
+
let joined = sections.join("\n\n");
|
|
65
|
+
// Hard cap so a huge errors.md can never blow the system prompt budget.
|
|
66
|
+
if (joined.length > 1200)
|
|
67
|
+
joined = joined.slice(0, 1200) + "\n…";
|
|
68
|
+
return joined;
|
|
69
|
+
}
|
|
70
|
+
getToolDefinitions() {
|
|
71
|
+
return [rememberTool, recallTool];
|
|
72
|
+
}
|
|
73
|
+
getPlugin() {
|
|
74
|
+
return {
|
|
75
|
+
name: "memory",
|
|
76
|
+
isBuiltin: true,
|
|
77
|
+
onBuildPrompt: () => {
|
|
78
|
+
const parts = [];
|
|
79
|
+
const prefs = this.store.getPreferences();
|
|
80
|
+
if (Object.keys(prefs).length > 0) {
|
|
81
|
+
const prefStr = Object.entries(prefs)
|
|
82
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
83
|
+
.join(", ");
|
|
84
|
+
parts.push(`User preferences: ${prefStr}`);
|
|
85
|
+
}
|
|
86
|
+
const memoryTail = this.getMemoryTail();
|
|
87
|
+
if (memoryTail)
|
|
88
|
+
parts.push(memoryTail);
|
|
89
|
+
return parts.length > 0 ? parts.join("\n\n") : null;
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
getStore() {
|
|
94
|
+
return this.store;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { readFileSync, existsSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
const MEMORY_FILES = ["conventions", "decisions", "errors", "facts"];
|
|
4
|
+
export class MemorySearch {
|
|
5
|
+
memoryDir;
|
|
6
|
+
constructor(memoryDir) {
|
|
7
|
+
this.memoryDir = memoryDir;
|
|
8
|
+
}
|
|
9
|
+
query(query) {
|
|
10
|
+
const results = [];
|
|
11
|
+
const lowerQuery = query.toLowerCase();
|
|
12
|
+
for (const name of MEMORY_FILES) {
|
|
13
|
+
const path = join(this.memoryDir, `${name}.md`);
|
|
14
|
+
if (!existsSync(path))
|
|
15
|
+
continue;
|
|
16
|
+
const content = readFileSync(path, "utf-8");
|
|
17
|
+
const lines = content.split("\n");
|
|
18
|
+
for (const line of lines) {
|
|
19
|
+
if (line.toLowerCase().includes(lowerQuery)) {
|
|
20
|
+
results.push({ file: name, match: line.trim() });
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
// Search preferences.json
|
|
25
|
+
const prefsPath = join(this.memoryDir, "preferences.json");
|
|
26
|
+
if (existsSync(prefsPath)) {
|
|
27
|
+
try {
|
|
28
|
+
const prefs = JSON.parse(readFileSync(prefsPath, "utf-8"));
|
|
29
|
+
for (const [key, value] of Object.entries(prefs)) {
|
|
30
|
+
const searchStr = `${key}=${value}`;
|
|
31
|
+
if (searchStr.toLowerCase().includes(lowerQuery)) {
|
|
32
|
+
results.push({ file: "preferences", match: `${key} = ${value}` });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
/* skip */
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return results;
|
|
41
|
+
}
|
|
42
|
+
}
|