atabey-mcp 0.0.4
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/dist/constants.js +64 -0
- package/dist/index.js +119 -0
- package/dist/tools/control_plane/locking.js +82 -0
- package/dist/tools/control_plane/registry.js +34 -0
- package/dist/tools/definitions.js +290 -0
- package/dist/tools/file_system/batch_surgical_edit.js +59 -0
- package/dist/tools/file_system/patch_file.js +29 -0
- package/dist/tools/file_system/read_file.js +51 -0
- package/dist/tools/file_system/replace_text.js +45 -0
- package/dist/tools/file_system/write_file.js +38 -0
- package/dist/tools/framework/audit_deps.js +41 -0
- package/dist/tools/framework/get_status.js +5 -0
- package/dist/tools/framework/orchestrate.js +5 -0
- package/dist/tools/framework/run_tests.js +27 -0
- package/dist/tools/framework/update_contract_hash.js +5 -0
- package/dist/tools/framework/update_memory.js +8 -0
- package/dist/tools/index.js +60 -0
- package/dist/tools/memory/get_insights.js +34 -0
- package/dist/tools/memory/read_memory.js +28 -0
- package/dist/tools/messaging/log_action.js +22 -0
- package/dist/tools/messaging/send_message.js +94 -0
- package/dist/tools/observability/check_ports.js +26 -0
- package/dist/tools/observability/get_health.js +19 -0
- package/dist/tools/quality/check_lint.js +30 -0
- package/dist/tools/search/get_gaps.js +48 -0
- package/dist/tools/search/get_map.js +43 -0
- package/dist/tools/search/grep_search.js +75 -0
- package/dist/tools/search/list_dir.js +28 -0
- package/dist/tools/shell/run_command.js +56 -0
- package/dist/tools/types.js +1 -0
- package/dist/utils/cli.js +59 -0
- package/dist/utils/compliance.js +78 -0
- package/dist/utils/fs.js +44 -0
- package/dist/utils/metrics.js +56 -0
- package/dist/utils/security.js +60 -0
- package/package.json +26 -0
- package/src/constants.ts +78 -0
- package/src/declarations.d.ts +17 -0
- package/src/index.ts +144 -0
- package/src/tools/control_plane/locking.ts +89 -0
- package/src/tools/control_plane/registry.ts +38 -0
- package/src/tools/definitions.ts +292 -0
- package/src/tools/file_system/batch_surgical_edit.ts +79 -0
- package/src/tools/file_system/patch_file.ts +39 -0
- package/src/tools/file_system/read_file.ts +58 -0
- package/src/tools/file_system/replace_text.ts +54 -0
- package/src/tools/file_system/write_file.ts +45 -0
- package/src/tools/framework/audit_deps.ts +49 -0
- package/src/tools/framework/get_status.ts +7 -0
- package/src/tools/framework/orchestrate.ts +7 -0
- package/src/tools/framework/run_tests.ts +30 -0
- package/src/tools/framework/update_contract_hash.ts +7 -0
- package/src/tools/framework/update_memory.ts +10 -0
- package/src/tools/index.ts +64 -0
- package/src/tools/memory/get_insights.ts +41 -0
- package/src/tools/memory/read_memory.ts +31 -0
- package/src/tools/messaging/log_action.ts +28 -0
- package/src/tools/messaging/send_message.ts +97 -0
- package/src/tools/observability/check_ports.ts +30 -0
- package/src/tools/observability/get_health.ts +24 -0
- package/src/tools/quality/check_lint.ts +36 -0
- package/src/tools/search/get_gaps.ts +54 -0
- package/src/tools/search/get_map.ts +48 -0
- package/src/tools/search/grep_search.ts +75 -0
- package/src/tools/search/list_dir.ts +34 -0
- package/src/tools/shell/run_command.ts +66 -0
- package/src/tools/types.ts +89 -0
- package/src/utils/cli.ts +53 -0
- package/src/utils/compliance.ts +95 -0
- package/src/utils/fs.ts +45 -0
- package/src/utils/metrics.ts +73 -0
- package/src/utils/security.ts +66 -0
- package/tsconfig.json +14 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { safePath } from "../../utils/security.js";
|
|
3
|
+
import { ReadFileArgs, ToolResult } from "../types.js";
|
|
4
|
+
import { Metrics } from "../../utils/metrics.js";
|
|
5
|
+
|
|
6
|
+
export function handleReadFile(projectRoot: string, args: ReadFileArgs): ToolResult {
|
|
7
|
+
if (!args.path) {
|
|
8
|
+
const err = "Missing 'path' argument.";
|
|
9
|
+
Metrics.logError(projectRoot, "@mcp", "read_file", err);
|
|
10
|
+
return { isError: true, content: [{ type: "text", text: `❌ ${err}` }] };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
const filePath = safePath(projectRoot, args.path);
|
|
15
|
+
if (!fs.existsSync(filePath)) {
|
|
16
|
+
const err = `File not found: ${args.path}`;
|
|
17
|
+
Metrics.logError(projectRoot, "@mcp", "read_file", err);
|
|
18
|
+
return { isError: true, content: [{ type: "text", text: `❌ ${err}` }] };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const startLine = args.startLine;
|
|
22
|
+
const endLine = args.endLine;
|
|
23
|
+
|
|
24
|
+
const content = fs.readFileSync(filePath, "utf8");
|
|
25
|
+
const lines = content.split(/\r?\n/);
|
|
26
|
+
|
|
27
|
+
if (startLine !== undefined || endLine !== undefined) {
|
|
28
|
+
const start = startLine !== undefined ? Math.max(1, startLine) - 1 : 0;
|
|
29
|
+
const end = endLine !== undefined ? Math.min(lines.length, endLine) : lines.length;
|
|
30
|
+
const sliced = lines.slice(start, end).join("\n");
|
|
31
|
+
const tokens = Metrics.estimateTokens(sliced);
|
|
32
|
+
Metrics.logUsage(projectRoot, "@mcp", `read_file: ${args.path}`, tokens);
|
|
33
|
+
return { content: [{ type: "text", text: sliced }] };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Default protection limit: 1000 lines
|
|
37
|
+
const DEFAULT_MAX_LINES = 1000;
|
|
38
|
+
if (lines.length > DEFAULT_MAX_LINES) {
|
|
39
|
+
const sliced = lines.slice(0, DEFAULT_MAX_LINES).join("\n");
|
|
40
|
+
const tokens = Metrics.estimateTokens(sliced);
|
|
41
|
+
Metrics.logUsage(projectRoot, "@mcp", `read_file: ${args.path} (truncated)`, tokens);
|
|
42
|
+
return {
|
|
43
|
+
content: [{
|
|
44
|
+
type: "text",
|
|
45
|
+
text: `${sliced}\n\n... [TRUNCATED - File is too long (${lines.length} lines). Only the first ${DEFAULT_MAX_LINES} lines are shown. Use startLine and endLine parameters to read other parts of the file.]`
|
|
46
|
+
}]
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const tokens = Metrics.estimateTokens(content);
|
|
51
|
+
Metrics.logUsage(projectRoot, "@mcp", `read_file: ${args.path}`, tokens);
|
|
52
|
+
return { content: [{ type: "text", text: content }] };
|
|
53
|
+
} catch (e) {
|
|
54
|
+
const err = `Failed to read file: ${String(e)}`;
|
|
55
|
+
Metrics.logError(projectRoot, "@mcp", `read_file:${args.path}`, err);
|
|
56
|
+
return { isError: true, content: [{ type: "text", text: `❌ ${err}` }] };
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { safePath } from "../../utils/security.js";
|
|
3
|
+
import { writeTextFileAtomic } from "../../utils/fs.js";
|
|
4
|
+
import { ReplaceTextArgs, ToolResult } from "../types.js";
|
|
5
|
+
import { Metrics } from "../../utils/metrics.js";
|
|
6
|
+
|
|
7
|
+
import { verifyCorporateCompliance } from "../../utils/compliance.js";
|
|
8
|
+
|
|
9
|
+
export function handleReplaceText(projectRoot: string, args: ReplaceTextArgs): ToolResult {
|
|
10
|
+
const filePath = safePath(projectRoot, args.path);
|
|
11
|
+
const content = fs.readFileSync(filePath, "utf8");
|
|
12
|
+
const oldText = args.oldText;
|
|
13
|
+
const newText = args.newText;
|
|
14
|
+
const allowMultiple = args.allowMultiple || false; // Default to false
|
|
15
|
+
|
|
16
|
+
if (!content.includes(oldText)) {
|
|
17
|
+
const err = `Text not found in file: ${oldText.slice(0, 100)}...`;
|
|
18
|
+
Metrics.logError(projectRoot, "@mcp", `replace_text:${args.path}`, err);
|
|
19
|
+
throw new Error(err);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Surgical precision guard: reject ambiguous replacements unless allowMultiple is true.
|
|
23
|
+
if (!allowMultiple) {
|
|
24
|
+
const firstIndex = content.indexOf(oldText);
|
|
25
|
+
const lastIndex = content.lastIndexOf(oldText);
|
|
26
|
+
if (firstIndex !== lastIndex) {
|
|
27
|
+
const count = (content.match(new RegExp(oldText.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g")) || []).length;
|
|
28
|
+
const err = `Ambiguous replacement: "${oldText.slice(0, 80)}..." found ${count} times in ${args.path}. ` +
|
|
29
|
+
"Provide a longer, unique context string or set 'allow_multiple' to true.";
|
|
30
|
+
Metrics.logError(projectRoot, "@mcp", `replace_text:${args.path}`, err);
|
|
31
|
+
throw new Error(err);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Perform replacement(s).
|
|
36
|
+
// Use a function replacer so special patterns ($&, $1, $$) inside newText
|
|
37
|
+
// are treated literally and never reinterpreted as regex backreferences.
|
|
38
|
+
let newContent: string;
|
|
39
|
+
if (allowMultiple) {
|
|
40
|
+
newContent = content.replace(new RegExp(oldText.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g"), () => newText);
|
|
41
|
+
} else {
|
|
42
|
+
newContent = content.replace(oldText, () => newText);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// ENFORCE CORPORATE COMPLIANCE
|
|
46
|
+
verifyCorporateCompliance(newContent, args.path as string);
|
|
47
|
+
|
|
48
|
+
writeTextFileAtomic(filePath, newContent);
|
|
49
|
+
|
|
50
|
+
const tokens = Metrics.estimateTokens(newText);
|
|
51
|
+
Metrics.logUsage(projectRoot, "@mcp", `replace_text: ${args.path}`, tokens);
|
|
52
|
+
|
|
53
|
+
return { content: [{ type: "text", text: `✅ Surgical edit successful in ${args.path}` }] };
|
|
54
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { safePath, resolveFrameworkDir } from "../../utils/security.js";
|
|
4
|
+
import { writeTextFileAtomic, appendFileSafe } from "../../utils/fs.js";
|
|
5
|
+
import { Metrics } from "../../utils/metrics.js";
|
|
6
|
+
import { WriteFileArgs, ToolResult } from "../types.js";
|
|
7
|
+
|
|
8
|
+
import { verifyCorporateCompliance } from "../../utils/compliance.js";
|
|
9
|
+
|
|
10
|
+
export function handleWriteFile(projectRoot: string, args: WriteFileArgs): ToolResult {
|
|
11
|
+
if (!args.path || args.content === undefined) {
|
|
12
|
+
const err = "Missing 'path' or 'content' argument.";
|
|
13
|
+
Metrics.logError(projectRoot, "@mcp", "write_file", err);
|
|
14
|
+
return { isError: true, content: [{ type: "text", text: `❌ ${err}` }] };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
const filePath = safePath(projectRoot, args.path);
|
|
19
|
+
const content = args.content;
|
|
20
|
+
|
|
21
|
+
// ENFORCE CORPORATE COMPLIANCE
|
|
22
|
+
verifyCorporateCompliance(content, args.path);
|
|
23
|
+
|
|
24
|
+
writeTextFileAtomic(filePath, content);
|
|
25
|
+
|
|
26
|
+
// AUTO-LOGGING & METRICS
|
|
27
|
+
const tokens = Metrics.estimateTokens(content);
|
|
28
|
+
Metrics.logUsage(projectRoot, "@mcp", `write_file: ${args.path}`, tokens);
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
const frameworkDir = resolveFrameworkDir(projectRoot);
|
|
32
|
+
const memoryPath = path.join(projectRoot, frameworkDir, "memory/PROJECT_MEMORY.md");
|
|
33
|
+
if (fs.existsSync(memoryPath)) {
|
|
34
|
+
const entry = `\n### ${new Date().toISOString().split("T")[0]} — Auto-Update\n- **Action:** wrote file \`${args.path}\` (${tokens} tokens estimated).\n`;
|
|
35
|
+
appendFileSafe(memoryPath, entry);
|
|
36
|
+
}
|
|
37
|
+
} catch { /* ignore memory logging errors */ }
|
|
38
|
+
|
|
39
|
+
return { content: [{ type: "text", text: `✅ File written: ${args.path}` }] };
|
|
40
|
+
} catch (e) {
|
|
41
|
+
const err = `Failed to write file: ${String(e)}`;
|
|
42
|
+
Metrics.logError(projectRoot, "@mcp", `write_file:${args.path}`, err);
|
|
43
|
+
return { isError: true, content: [{ type: "text", text: `❌ ${err}` }] };
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { ToolArgs, ToolResult } from "../types.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Audits package.json for unused or duplicate-like packages.
|
|
7
|
+
* Focuses on project health and cleanup.
|
|
8
|
+
*/
|
|
9
|
+
export function handleAuditDependencies(projectRoot: string, _args: ToolArgs): ToolResult {
|
|
10
|
+
const pkgPath = path.join(projectRoot, "package.json");
|
|
11
|
+
if (!fs.existsSync(pkgPath)) {
|
|
12
|
+
throw new Error("package.json not found.");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
|
|
16
|
+
const deps = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
17
|
+
const depNames = Object.keys(deps);
|
|
18
|
+
|
|
19
|
+
const results: string[] = [];
|
|
20
|
+
|
|
21
|
+
// 1. Look for similar packages (potential duplicates)
|
|
22
|
+
const similarityGroups: Record<string, string[]> = {
|
|
23
|
+
"CSS/Styling": ["tailwind", "panda", "styled-components", "emotion", "sass"],
|
|
24
|
+
"Testing": ["vitest", "jest", "mocha", "jasmine"],
|
|
25
|
+
"Fetching": ["axios", "ky", "fetch"]
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
for (const [group, patterns] of Object.entries(similarityGroups)) {
|
|
29
|
+
const found = depNames.filter(d => patterns.some(p => d.includes(p)));
|
|
30
|
+
if (found.length > 1) {
|
|
31
|
+
results.push(`⚠️ Potential redundancy in [${group}]: Found ${found.join(", ")}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 2. Scan for "any" usage in package names (bad practice markers)
|
|
36
|
+
const legacyDeps = depNames.filter(d => d.includes("legacy") || d.includes("compat"));
|
|
37
|
+
if (legacyDeps.length > 0) {
|
|
38
|
+
results.push(`ℹ️ Legacy compatibility packages detected: ${legacyDeps.join(", ")}`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
content: [{
|
|
43
|
+
type: "text",
|
|
44
|
+
text: results.length > 0
|
|
45
|
+
? `Dependency Audit Results:\n\n${results.join("\n")}`
|
|
46
|
+
: "✅ Dependencies look clean and consolidated."
|
|
47
|
+
}]
|
|
48
|
+
};
|
|
49
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { safeExec } from "../../utils/cli.js";
|
|
2
|
+
import { GetStatusArgs, ToolResult } from "../types.js";
|
|
3
|
+
|
|
4
|
+
export function handleGetFrameworkStatus(projectRoot: string, args: GetStatusArgs): ToolResult {
|
|
5
|
+
const output = safeExec("npx", ["atabey", "status"], projectRoot, args.timeout);
|
|
6
|
+
return { content: [{ type: "text", text: output }] };
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { safeExec } from "../../utils/cli.js";
|
|
2
|
+
import { OrchestrateArgs, ToolResult } from "../types.js";
|
|
3
|
+
|
|
4
|
+
export function handleOrchestrateLoop(projectRoot: string, args: OrchestrateArgs): ToolResult {
|
|
5
|
+
const output = safeExec("npx", ["atabey", "orchestrate"], projectRoot, args.timeout);
|
|
6
|
+
return { content: [{ type: "text", text: output }] };
|
|
7
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { execSync } from "child_process";
|
|
2
|
+
import { RunTestsArgs, ToolResult } from "../types.js";
|
|
3
|
+
import { getBackendLanguage, getDefaultTestCommand } from "../../utils/cli.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Executes project tests and returns results for agent analysis.
|
|
7
|
+
*/
|
|
8
|
+
export function handleRunTests(projectRoot: string, args: RunTestsArgs): ToolResult {
|
|
9
|
+
const language = getBackendLanguage(projectRoot);
|
|
10
|
+
const testCommand = args.command || getDefaultTestCommand(language);
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
const output = execSync(testCommand, { cwd: projectRoot, encoding: "utf8", stdio: "pipe" });
|
|
14
|
+
return {
|
|
15
|
+
content: [{ type: "text", text: `✅ Tests passed successfully for ${language}!\n\n${output}` }]
|
|
16
|
+
};
|
|
17
|
+
} catch (error: unknown) {
|
|
18
|
+
const err = error as { stderr?: Buffer; stdout?: Buffer };
|
|
19
|
+
const stderr = err.stderr?.toString() || "";
|
|
20
|
+
const stdout = err.stdout?.toString() || "";
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
isError: true,
|
|
24
|
+
content: [{
|
|
25
|
+
type: "text",
|
|
26
|
+
text: `❌ Tests FAILED for ${language}!\n\n--- STDOUT ---\n${stdout}\n\n--- STDERR ---\n${stderr}`
|
|
27
|
+
}]
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { safeExec } from "../../utils/cli.js";
|
|
2
|
+
import { UpdateContractHashArgs, ToolResult } from "../types.js";
|
|
3
|
+
|
|
4
|
+
export function handleUpdateContractHash(projectRoot: string, args: UpdateContractHashArgs): ToolResult {
|
|
5
|
+
const output = safeExec("npx", ["atabey", "update-contract"], projectRoot, args.timeout);
|
|
6
|
+
return { content: [{ type: "text", text: output }] };
|
|
7
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { safeExec } from "../../utils/cli.js";
|
|
2
|
+
import { UpdateProjectMemoryArgs, ToolResult } from "../types.js";
|
|
3
|
+
|
|
4
|
+
export function handleUpdateProjectMemory(projectRoot: string, args: UpdateProjectMemoryArgs): ToolResult {
|
|
5
|
+
const section = args.section;
|
|
6
|
+
const content = args.content;
|
|
7
|
+
// Using execFileSync with array args prevents command injection
|
|
8
|
+
safeExec("npx", ["atabey", "update_project_memory", section, content], projectRoot);
|
|
9
|
+
return { content: [{ type: "text", text: `✅ Section ${section} updated.` }] };
|
|
10
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { TOOLS } from "./definitions.js";
|
|
2
|
+
import { handleReadFile } from "./file_system/read_file.js";
|
|
3
|
+
import { handleWriteFile } from "./file_system/write_file.js";
|
|
4
|
+
import { handleReplaceText } from "./file_system/replace_text.js";
|
|
5
|
+
import { handleBatchSurgicalEdit } from "./file_system/batch_surgical_edit.js";
|
|
6
|
+
import { handlePatchFile } from "./file_system/patch_file.js";
|
|
7
|
+
import { handleGrepSearch } from "./search/grep_search.js";
|
|
8
|
+
import { handleListDir } from "./search/list_dir.js";
|
|
9
|
+
import { handleGetProjectGaps } from "./search/get_gaps.js";
|
|
10
|
+
import { handleGetProjectMap } from "./search/get_map.js";
|
|
11
|
+
import { handleGetFrameworkStatus } from "./framework/get_status.js";
|
|
12
|
+
import { handleUpdateProjectMemory } from "./framework/update_memory.js";
|
|
13
|
+
import { handleAuditDependencies } from "./framework/audit_deps.js";
|
|
14
|
+
import { handleRunTests } from "./framework/run_tests.js";
|
|
15
|
+
import { handleGetSystemHealth } from "./observability/get_health.js";
|
|
16
|
+
import { handleCheckPorts } from "./observability/check_ports.js";
|
|
17
|
+
import { handleOrchestrateLoop } from "./framework/orchestrate.js";
|
|
18
|
+
import { handleUpdateContractHash } from "./framework/update_contract_hash.js";
|
|
19
|
+
import { handleReadProjectMemory } from "./memory/read_memory.js";
|
|
20
|
+
import { handleGetMemoryInsights } from "./memory/get_insights.js";
|
|
21
|
+
import { handleSendAgentMessage } from "./messaging/send_message.js";
|
|
22
|
+
import { handleLogAgentAction } from "./messaging/log_action.js";
|
|
23
|
+
import { handleAcquireLock, handleReleaseLock } from "./control_plane/locking.js";
|
|
24
|
+
import { handleRegisterAgent } from "./control_plane/registry.js";
|
|
25
|
+
import { handleRunCommand } from "./shell/run_command.js";
|
|
26
|
+
import { handleCheckLint } from "./quality/check_lint.js";
|
|
27
|
+
import { ToolHandler, ToolResult } from "./types.js";
|
|
28
|
+
|
|
29
|
+
// Map of tool names to their handler functions
|
|
30
|
+
const bind = <T>(fn: (root: string, args: T) => ToolResult | Promise<ToolResult>): ToolHandler => {
|
|
31
|
+
return (root: string, args: unknown) => fn(root, args as T);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const toolHandlers: Record<string, ToolHandler> = {
|
|
35
|
+
read_file: bind(handleReadFile),
|
|
36
|
+
view_file: bind(handleReadFile), // Alias
|
|
37
|
+
list_dir: bind(handleListDir),
|
|
38
|
+
grep_search: bind(handleGrepSearch),
|
|
39
|
+
get_project_map: bind(handleGetProjectMap),
|
|
40
|
+
get_project_gaps: bind(handleGetProjectGaps),
|
|
41
|
+
write_file: bind(handleWriteFile),
|
|
42
|
+
replace_text: bind(handleReplaceText),
|
|
43
|
+
batch_surgical_edit: bind(handleBatchSurgicalEdit),
|
|
44
|
+
patch_file: bind(handlePatchFile),
|
|
45
|
+
get_framework_status: bind(handleGetFrameworkStatus),
|
|
46
|
+
read_project_memory: bind(handleReadProjectMemory),
|
|
47
|
+
get_memory_insights: bind(handleGetMemoryInsights),
|
|
48
|
+
update_project_memory: bind(handleUpdateProjectMemory),
|
|
49
|
+
audit_dependencies: bind(handleAuditDependencies),
|
|
50
|
+
run_tests: bind(handleRunTests),
|
|
51
|
+
get_system_health: bind(handleGetSystemHealth),
|
|
52
|
+
check_active_ports: bind(handleCheckPorts),
|
|
53
|
+
orchestrate_loop: bind(handleOrchestrateLoop),
|
|
54
|
+
send_agent_message: bind(handleSendAgentMessage),
|
|
55
|
+
log_agent_action: bind(handleLogAgentAction),
|
|
56
|
+
update_contract_hash: bind(handleUpdateContractHash),
|
|
57
|
+
acquire_lock: bind(handleAcquireLock),
|
|
58
|
+
release_lock: bind(handleReleaseLock),
|
|
59
|
+
register_agent: bind(handleRegisterAgent),
|
|
60
|
+
run_shell_command: bind(handleRunCommand),
|
|
61
|
+
check_lint: bind(handleCheckLint),
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export { TOOLS };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { resolveFrameworkDir } from "../../utils/security.js";
|
|
4
|
+
import { ToolArgs, ToolResult } from "../types.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Extracts key insights from the project memory to minimize token usage.
|
|
8
|
+
* Returns only the active phase, trace, and the last 5 decisions/tasks.
|
|
9
|
+
*/
|
|
10
|
+
export function handleGetMemoryInsights(projectRoot: string, _args: ToolArgs): ToolResult {
|
|
11
|
+
try {
|
|
12
|
+
const frameworkDir = resolveFrameworkDir(projectRoot);
|
|
13
|
+
const memoryPath = path.join(projectRoot, frameworkDir, "memory/PROJECT_MEMORY.md");
|
|
14
|
+
|
|
15
|
+
if (!fs.existsSync(memoryPath)) {
|
|
16
|
+
return { content: [{ type: "text", text: "New project: No history available." }] };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const content = fs.readFileSync(memoryPath, "utf8");
|
|
20
|
+
const lines = content.split("\n");
|
|
21
|
+
|
|
22
|
+
const activePhase = lines.find(l => l.includes("**Phase:**"))?.split("**Phase:**")[1]?.trim() || "Unknown";
|
|
23
|
+
const activeTrace = lines.find(l => l.includes("**Trace ID:**"))?.split("**Trace ID:**")[1]?.trim() || "None";
|
|
24
|
+
|
|
25
|
+
// Find the last 5 history items (heuristic)
|
|
26
|
+
const historyStartIndex = lines.findIndex(l => l.toUpperCase().includes("HISTORY"));
|
|
27
|
+
let recentHistory = "No history found.";
|
|
28
|
+
if (historyStartIndex !== -1) {
|
|
29
|
+
recentHistory = lines.slice(historyStartIndex + 1)
|
|
30
|
+
.filter(l => l.trim().startsWith("-"))
|
|
31
|
+
.slice(-5)
|
|
32
|
+
.join("\n");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const insights = `🧠 **Memory Insights**\n- **Phase:** ${activePhase}\n- **Trace:** ${activeTrace}\n\n**Recent Actions:**\n${recentHistory}`;
|
|
36
|
+
|
|
37
|
+
return { content: [{ type: "text", text: insights }] };
|
|
38
|
+
} catch (e) {
|
|
39
|
+
return { isError: true, content: [{ type: "text", text: `Failed to extract insights: ${String(e)}` }] };
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { resolveFrameworkDir } from "../../utils/security.js";
|
|
4
|
+
import { ToolArgs, ToolResult } from "../types.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Reads the project's central memory (PROJECT_MEMORY.md).
|
|
8
|
+
* This is the "brain" of the project.
|
|
9
|
+
*/
|
|
10
|
+
export function handleReadProjectMemory(projectRoot: string, _args: ToolArgs): ToolResult {
|
|
11
|
+
try {
|
|
12
|
+
const frameworkDir = resolveFrameworkDir(projectRoot);
|
|
13
|
+
const memoryPath = path.join(projectRoot, frameworkDir, "memory/PROJECT_MEMORY.md");
|
|
14
|
+
|
|
15
|
+
if (!fs.existsSync(memoryPath)) {
|
|
16
|
+
return {
|
|
17
|
+
content: [{ type: "text", text: "ℹ️ Project memory file not found. It might be a new project." }]
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const content = fs.readFileSync(memoryPath, "utf8");
|
|
22
|
+
return {
|
|
23
|
+
content: [{ type: "text", text: content }]
|
|
24
|
+
};
|
|
25
|
+
} catch (e) {
|
|
26
|
+
return {
|
|
27
|
+
isError: true,
|
|
28
|
+
content: [{ type: "text", text: `Failed to read project memory: ${String(e)}` }]
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { resolveFrameworkDir } from "../../utils/security.js";
|
|
4
|
+
import { LogAgentActionArgs, ToolResult } from "../types.js";
|
|
5
|
+
|
|
6
|
+
export function handleLogAgentAction(projectRoot: string, args: LogAgentActionArgs): ToolResult {
|
|
7
|
+
const { agent, action, traceId, status, summary } = args;
|
|
8
|
+
const findings = args.findings ? args.findings.split(",").map(f => f.trim()) : [];
|
|
9
|
+
|
|
10
|
+
const frameworkDir = resolveFrameworkDir(projectRoot);
|
|
11
|
+
const agentName = agent.replace("@", "");
|
|
12
|
+
const logPath = path.join(projectRoot, frameworkDir, "logs", `${agentName}.json`);
|
|
13
|
+
|
|
14
|
+
const logEntry = {
|
|
15
|
+
timestamp: new Date().toISOString(),
|
|
16
|
+
agent,
|
|
17
|
+
action,
|
|
18
|
+
requestId: traceId,
|
|
19
|
+
status,
|
|
20
|
+
summary,
|
|
21
|
+
findings
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
fs.mkdirSync(path.dirname(logPath), { recursive: true });
|
|
25
|
+
fs.appendFileSync(logPath, JSON.stringify(logEntry) + "\n");
|
|
26
|
+
|
|
27
|
+
return { content: [{ type: "text", text: `✅ Action logged for ${agent} to ${path.join(frameworkDir, "logs", `${agentName}.json`)}` }] };
|
|
28
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { resolveFrameworkDir } from "../../utils/security.js";
|
|
4
|
+
import { SendAgentMessageArgs, ToolResult } from "../types.js";
|
|
5
|
+
import { Metrics } from "../../utils/metrics.js";
|
|
6
|
+
|
|
7
|
+
export async function handleSendAgentMessage(projectRoot: string, args: SendAgentMessageArgs): Promise<ToolResult> {
|
|
8
|
+
const { to, category, content, traceId, parentId, requiresApproval } = args;
|
|
9
|
+
const from = args.from || "@mcp";
|
|
10
|
+
|
|
11
|
+
if (!to || !category || !content || !traceId) {
|
|
12
|
+
const err = "Missing required messaging arguments (to, category, content, or traceId).";
|
|
13
|
+
Metrics.logError(projectRoot, from, "send_agent_message", err);
|
|
14
|
+
return { isError: true, content: [{ type: "text", text: `❌ ${err}` }] };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const frameworkDir = resolveFrameworkDir(projectRoot);
|
|
18
|
+
const messagesDir = path.join(projectRoot, frameworkDir, "messages");
|
|
19
|
+
const agentName = to.replace("@", "");
|
|
20
|
+
const messagePath = path.join(messagesDir, `${agentName}.json`);
|
|
21
|
+
const lockPath = path.join(messagesDir, `${agentName}.lock`);
|
|
22
|
+
|
|
23
|
+
// Hermes Lock Protocol: Retry 20 times with 500ms delay
|
|
24
|
+
let retries = 20;
|
|
25
|
+
let acquired = false;
|
|
26
|
+
while (retries > 0) {
|
|
27
|
+
try {
|
|
28
|
+
if (fs.existsSync(lockPath)) {
|
|
29
|
+
try {
|
|
30
|
+
const stats = fs.statSync(lockPath);
|
|
31
|
+
if (Date.now() - stats.mtimeMs > 10000) {
|
|
32
|
+
const tempLockPath = `${lockPath}.${Math.random().toString(36).substring(2)}.old`;
|
|
33
|
+
fs.renameSync(lockPath, tempLockPath);
|
|
34
|
+
fs.unlinkSync(tempLockPath);
|
|
35
|
+
}
|
|
36
|
+
} catch {
|
|
37
|
+
// ignore if concurrently unlinked or renamed
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
fs.mkdirSync(messagesDir, { recursive: true });
|
|
41
|
+
fs.writeFileSync(lockPath, `Locked by ${from} at ${new Date().toISOString()}`, { flag: "wx" });
|
|
42
|
+
acquired = true;
|
|
43
|
+
break;
|
|
44
|
+
} catch (err: unknown) {
|
|
45
|
+
const error = err as { code?: string; message?: string };
|
|
46
|
+
if (error.code === "EEXIST") {
|
|
47
|
+
retries--;
|
|
48
|
+
if (retries > 0) {
|
|
49
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
50
|
+
}
|
|
51
|
+
} else {
|
|
52
|
+
return { content: [{ type: "text", text: `❌ Unexpected lock acquisition error: ${error.message || String(err)}` }], isError: true };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (!acquired) {
|
|
58
|
+
const err = `Could not send message to ${to}: Hermes lock is busy.`;
|
|
59
|
+
Metrics.logError(projectRoot, from, "send_agent_message", err);
|
|
60
|
+
return { content: [{ type: "text", text: `❌ ${err}` }], isError: true };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
const defaultPriority = (category === "ALERT" || category === "ACTION") ? "HIGH" : "NORMAL";
|
|
65
|
+
const finalRequiresApproval = requiresApproval !== undefined
|
|
66
|
+
? requiresApproval
|
|
67
|
+
: category === "ALERT";
|
|
68
|
+
|
|
69
|
+
const message = {
|
|
70
|
+
timestamp: new Date().toISOString(),
|
|
71
|
+
from,
|
|
72
|
+
to,
|
|
73
|
+
category,
|
|
74
|
+
traceId,
|
|
75
|
+
parentId,
|
|
76
|
+
content,
|
|
77
|
+
priority: args.priority || defaultPriority,
|
|
78
|
+
status: "PENDING",
|
|
79
|
+
requiresApproval: finalRequiresApproval
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
fs.appendFileSync(messagePath, JSON.stringify(message) + "\n");
|
|
83
|
+
return { content: [{ type: "text", text: `✅ Message sent to ${to} (from: ${from})` }] };
|
|
84
|
+
} catch (e) {
|
|
85
|
+
const err = `Failed to write message: ${String(e)}`;
|
|
86
|
+
Metrics.logError(projectRoot, from, "send_agent_message", err);
|
|
87
|
+
return { isError: true, content: [{ type: "text", text: `❌ ${err}` }] };
|
|
88
|
+
} finally {
|
|
89
|
+
if (acquired && fs.existsSync(lockPath)) {
|
|
90
|
+
try {
|
|
91
|
+
fs.unlinkSync(lockPath);
|
|
92
|
+
} catch {
|
|
93
|
+
// ignore
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { execSync } from "child_process";
|
|
2
|
+
import { CheckActivePortsArgs, ToolResult } from "../types.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Checks for active network ports and their status.
|
|
6
|
+
*/
|
|
7
|
+
export function handleCheckPorts(projectRoot: string, args: CheckActivePortsArgs): ToolResult {
|
|
8
|
+
const filter = args.filter || ""; // Optional filter (e.g., ":3000")
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
// Using 'lsof -i -P -n' to list open files and network connections
|
|
12
|
+
// Note: may require permissions or behave differently on non-Unix systems
|
|
13
|
+
const command = process.platform === "win32"
|
|
14
|
+
? `netstat -ano | findstr LISTENING ${filter ? `| findstr ${filter}` : ""}`
|
|
15
|
+
: `lsof -i -P -n | grep LISTEN ${filter ? `| grep ${filter}` : ""}`;
|
|
16
|
+
|
|
17
|
+
const output = execSync(command, { encoding: "utf8" });
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
content: [{
|
|
21
|
+
type: "text",
|
|
22
|
+
text: `📡 **Active Listening Ports:**\n\n${output || "No active listening ports found matching filter."}`
|
|
23
|
+
}]
|
|
24
|
+
};
|
|
25
|
+
} catch (_e) {
|
|
26
|
+
return {
|
|
27
|
+
content: [{ type: "text", text: "ℹ️ No active ports found or command failed (this is normal if nothing is listening)." }]
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import os from "os";
|
|
2
|
+
import { ToolArgs, ToolResult } from "../types.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Retrieves system health metrics including CPU load and memory usage.
|
|
6
|
+
*/
|
|
7
|
+
export function handleGetSystemHealth(_projectRoot: string, _args: ToolArgs): ToolResult {
|
|
8
|
+
const totalMem = os.totalmem();
|
|
9
|
+
const freeMem = os.freemem();
|
|
10
|
+
const usedMem = totalMem - freeMem;
|
|
11
|
+
const memUsagePercent = ((usedMem / totalMem) * 100).toFixed(2);
|
|
12
|
+
|
|
13
|
+
const loadAvg = os.loadavg(); // [1, 5, 15] minute averages
|
|
14
|
+
|
|
15
|
+
const healthReport = `🖥️ **System Health Report**
|
|
16
|
+
- **Memory:** ${memUsagePercent}% used (${(usedMem / 1024 / 1024 / 1024).toFixed(2)} GB / ${(totalMem / 1024 / 1024 / 1024).toFixed(2)} GB)
|
|
17
|
+
- **CPU Load (1m, 5m, 15m):** ${loadAvg.map(l => l.toFixed(2)).join(", ")}
|
|
18
|
+
- **Platform:** ${os.platform()} (${os.release()})
|
|
19
|
+
- **Uptime:** ${(os.uptime() / 3600).toFixed(2)} hours`;
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
content: [{ type: "text", text: healthReport }]
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { exec } from "child_process";
|
|
2
|
+
import { ToolArgs, ToolResult } from "../types.js";
|
|
3
|
+
import { Metrics } from "../../utils/metrics.js";
|
|
4
|
+
import { getBackendLanguage, getDefaultLintCommand } from "../../utils/cli.js";
|
|
5
|
+
|
|
6
|
+
const TIMEOUT = 60000; // 60 seconds
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Handles running the project's linter.
|
|
10
|
+
*/
|
|
11
|
+
export function handleCheckLint(projectRoot: string, _args: ToolArgs): Promise<ToolResult> {
|
|
12
|
+
const language = getBackendLanguage(projectRoot);
|
|
13
|
+
const lintCommand = getDefaultLintCommand(language);
|
|
14
|
+
|
|
15
|
+
return new Promise((resolve) => {
|
|
16
|
+
exec(lintCommand, { cwd: projectRoot, timeout: TIMEOUT }, (error, stdout, stderr) => {
|
|
17
|
+
const output = stdout + stderr;
|
|
18
|
+
const tokens = Metrics.estimateTokens(output);
|
|
19
|
+
|
|
20
|
+
if (error) {
|
|
21
|
+
const err = `Linting failed for ${language} with command: ${lintCommand}. ${error.message}`;
|
|
22
|
+
Metrics.logError(projectRoot, "@mcp", "check_lint", err);
|
|
23
|
+
resolve({
|
|
24
|
+
isError: true,
|
|
25
|
+
content: [{ type: "text", text: `❌ Lint Errors Found (${language}):\n\n${output}` }]
|
|
26
|
+
});
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
Metrics.logUsage(projectRoot, "@mcp", "check_lint", tokens);
|
|
31
|
+
resolve({
|
|
32
|
+
content: [{ type: "text", text: `✅ Lint check passed successfully for ${language}:\n\n${output}` }]
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
}
|