matex-cli 1.2.81 → 1.2.84
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/commands/chaos.js +3 -8
- package/dist/prompts/chaos-prompts.js +26 -21
- package/dist/session/agent-session.js +38 -9
- package/dist/utils/agent-orchestrator.js +7 -0
- package/dist/utils/mcp-server.js +6 -0
- package/dist/utils/tui.js +84 -7
- package/package.json +7 -1
- package/.agents/skills/mcp-server-dev/SKILL.md +0 -60
- package/.agents/skills/mcp-server-dev/examples/basic-ts-server/package.json +0 -20
- package/.agents/skills/mcp-server-dev/examples/basic-ts-server/src/index.ts +0 -66
- package/.agents/skills/mcp-server-dev/resources/best_practices.md +0 -20
- package/.agents/skills/pptx-presentation-builder/SKILL.md +0 -338
- package/.agents/workflows/deploy.md +0 -27
- package/dist/api/client.d.ts +0 -40
- package/dist/api/client.d.ts.map +0 -1
- package/dist/api/client.js.map +0 -1
- package/dist/commands/ask.d.ts +0 -3
- package/dist/commands/ask.d.ts.map +0 -1
- package/dist/commands/ask.js.map +0 -1
- package/dist/commands/augov.d.ts +0 -3
- package/dist/commands/augov.d.ts.map +0 -1
- package/dist/commands/augov.js.map +0 -1
- package/dist/commands/bro.d.ts +0 -4
- package/dist/commands/bro.d.ts.map +0 -1
- package/dist/commands/bro.js.map +0 -1
- package/dist/commands/chaos.d.ts +0 -3
- package/dist/commands/chaos.d.ts.map +0 -1
- package/dist/commands/chaos.js.map +0 -1
- package/dist/commands/chat.d.ts +0 -3
- package/dist/commands/chat.d.ts.map +0 -1
- package/dist/commands/chat.js.map +0 -1
- package/dist/commands/code.d.ts +0 -3
- package/dist/commands/code.d.ts.map +0 -1
- package/dist/commands/code.js.map +0 -1
- package/dist/commands/config.d.ts +0 -3
- package/dist/commands/config.d.ts.map +0 -1
- package/dist/commands/config.js.map +0 -1
- package/dist/commands/dev.d.ts +0 -3
- package/dist/commands/dev.d.ts.map +0 -1
- package/dist/commands/dev.js.map +0 -1
- package/dist/commands/help.d.ts +0 -3
- package/dist/commands/help.d.ts.map +0 -1
- package/dist/commands/help.js.map +0 -1
- package/dist/commands/login.d.ts +0 -3
- package/dist/commands/login.d.ts.map +0 -1
- package/dist/commands/login.js.map +0 -1
- package/dist/commands/models.d.ts +0 -3
- package/dist/commands/models.d.ts.map +0 -1
- package/dist/commands/models.js.map +0 -1
- package/dist/commands/student.d.ts +0 -3
- package/dist/commands/student.d.ts.map +0 -1
- package/dist/commands/student.js.map +0 -1
- package/dist/commands/study.d.ts +0 -3
- package/dist/commands/study.d.ts.map +0 -1
- package/dist/commands/study.js.map +0 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/prompts/banter-augov.d.ts +0 -2
- package/dist/prompts/banter-augov.d.ts.map +0 -1
- package/dist/prompts/banter-augov.js.map +0 -1
- package/dist/prompts/banter.d.ts +0 -6
- package/dist/prompts/banter.d.ts.map +0 -1
- package/dist/prompts/banter.js.map +0 -1
- package/dist/prompts/chaos-prompts.d.ts +0 -3
- package/dist/prompts/chaos-prompts.d.ts.map +0 -1
- package/dist/prompts/chaos-prompts.js.map +0 -1
- package/dist/prompts/system-prompts.d.ts +0 -4
- package/dist/prompts/system-prompts.d.ts.map +0 -1
- package/dist/prompts/system-prompts.js.map +0 -1
- package/dist/session/agent-session.d.ts +0 -41
- package/dist/session/agent-session.d.ts.map +0 -1
- package/dist/session/agent-session.js.map +0 -1
- package/dist/utils/agent-orchestrator.d.ts +0 -44
- package/dist/utils/agent-orchestrator.d.ts.map +0 -1
- package/dist/utils/agent-orchestrator.js.map +0 -1
- package/dist/utils/augov-logger.d.ts +0 -11
- package/dist/utils/augov-logger.d.ts.map +0 -1
- package/dist/utils/augov-logger.js.map +0 -1
- package/dist/utils/augov-scrubber.d.ts +0 -23
- package/dist/utils/augov-scrubber.d.ts.map +0 -1
- package/dist/utils/augov-scrubber.js.map +0 -1
- package/dist/utils/command-executor.d.ts +0 -56
- package/dist/utils/command-executor.d.ts.map +0 -1
- package/dist/utils/command-executor.js.map +0 -1
- package/dist/utils/config.d.ts +0 -56
- package/dist/utils/config.d.ts.map +0 -1
- package/dist/utils/config.js.map +0 -1
- package/dist/utils/mcp-server.d.ts +0 -77
- package/dist/utils/mcp-server.d.ts.map +0 -1
- package/dist/utils/mcp-server.js.map +0 -1
- package/dist/utils/patcher.d.ts +0 -45
- package/dist/utils/patcher.d.ts.map +0 -1
- package/dist/utils/patcher.js.map +0 -1
- package/dist/utils/repo-mapper.d.ts +0 -32
- package/dist/utils/repo-mapper.d.ts.map +0 -1
- package/dist/utils/repo-mapper.js.map +0 -1
- package/dist/utils/spinner.d.ts +0 -15
- package/dist/utils/spinner.d.ts.map +0 -1
- package/dist/utils/spinner.js.map +0 -1
- package/dist/utils/tui.d.ts +0 -134
- package/dist/utils/tui.d.ts.map +0 -1
- package/dist/utils/tui.js.map +0 -1
- package/fix-npm-permissions.sh +0 -23
- package/publish-local.sh +0 -16
- package/skills-lock.json +0 -10
- package/src/api/client.ts +0 -197
- package/src/commands/ask.ts +0 -62
- package/src/commands/augov.ts +0 -301
- package/src/commands/bro.ts +0 -336
- package/src/commands/chaos.ts +0 -67
- package/src/commands/chat.ts +0 -61
- package/src/commands/code.ts +0 -99
- package/src/commands/config.ts +0 -78
- package/src/commands/dev.ts +0 -68
- package/src/commands/help.ts +0 -67
- package/src/commands/login.ts +0 -47
- package/src/commands/models.ts +0 -81
- package/src/commands/student.ts +0 -23
- package/src/commands/study.ts +0 -75
- package/src/index.ts +0 -284
- package/src/prompts/banter-augov.ts +0 -17
- package/src/prompts/banter.ts +0 -101
- package/src/prompts/chaos-prompts.ts +0 -48
- package/src/prompts/system-prompts.ts +0 -145
- package/src/session/agent-session.ts +0 -428
- package/src/utils/agent-orchestrator.ts +0 -264
- package/src/utils/augov-logger.ts +0 -34
- package/src/utils/augov-scrubber.ts +0 -67
- package/src/utils/command-executor.ts +0 -529
- package/src/utils/config.ts +0 -124
- package/src/utils/mcp-server.ts +0 -388
- package/src/utils/patcher.ts +0 -229
- package/src/utils/repo-mapper.ts +0 -198
- package/src/utils/spinner.ts +0 -66
- package/src/utils/tui.ts +0 -749
- package/test-chaos-container.js +0 -16
- package/test-chaos-fix.js +0 -18
- package/test-config.ts +0 -2
- package/test-ui-output.js +0 -16
- package/tsconfig.json +0 -27
- package/vertex_ai_agent.py +0 -52
package/src/utils/config.ts
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import * as path from 'path';
|
|
3
|
-
import * as os from 'os';
|
|
4
|
-
|
|
5
|
-
export interface Config {
|
|
6
|
-
apiKey?: string;
|
|
7
|
-
defaultModel?: string;
|
|
8
|
-
baseURL?: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export class ConfigManager {
|
|
12
|
-
private configDir: string;
|
|
13
|
-
private configPath: string;
|
|
14
|
-
|
|
15
|
-
constructor() {
|
|
16
|
-
this.configDir = path.join(os.homedir(), '.matex');
|
|
17
|
-
this.configPath = path.join(this.configDir, 'config.json');
|
|
18
|
-
|
|
19
|
-
// Ensure config directory exists
|
|
20
|
-
if (!fs.existsSync(this.configDir)) {
|
|
21
|
-
fs.mkdirSync(this.configDir, { recursive: true });
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Load config from file
|
|
27
|
-
*/
|
|
28
|
-
private loadConfig(): Config {
|
|
29
|
-
if (!fs.existsSync(this.configPath)) {
|
|
30
|
-
return {};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
try {
|
|
34
|
-
const data = fs.readFileSync(this.configPath, 'utf-8');
|
|
35
|
-
return JSON.parse(data);
|
|
36
|
-
} catch (error) {
|
|
37
|
-
return {};
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Save config to file
|
|
43
|
-
*/
|
|
44
|
-
private saveConfig(config: Config): void {
|
|
45
|
-
fs.writeFileSync(this.configPath, JSON.stringify(config, null, 2), 'utf-8');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Set API key
|
|
50
|
-
*/
|
|
51
|
-
setAPIKey(apiKey: string): void {
|
|
52
|
-
const config = this.loadConfig();
|
|
53
|
-
config.apiKey = apiKey;
|
|
54
|
-
this.saveConfig(config);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Get API key
|
|
59
|
-
*/
|
|
60
|
-
getAPIKey(): string | undefined {
|
|
61
|
-
return this.loadConfig().apiKey;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Set default model
|
|
66
|
-
*/
|
|
67
|
-
setDefaultModel(model: string): void {
|
|
68
|
-
const config = this.loadConfig();
|
|
69
|
-
let normalizedModel = model.toLowerCase().trim().replace(/\s+/g, '');
|
|
70
|
-
if (normalizedModel === 'matexfree') {
|
|
71
|
-
normalizedModel = 'matex-free';
|
|
72
|
-
}
|
|
73
|
-
config.defaultModel = normalizedModel;
|
|
74
|
-
this.saveConfig(config);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Get default model
|
|
79
|
-
*/
|
|
80
|
-
getDefaultModel(): string {
|
|
81
|
-
return this.loadConfig().defaultModel || 'matexcodex';
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Set base URL
|
|
86
|
-
*/
|
|
87
|
-
setBaseURL(url: string): void {
|
|
88
|
-
const config = this.loadConfig();
|
|
89
|
-
config.baseURL = url;
|
|
90
|
-
this.saveConfig(config);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Get base URL
|
|
95
|
-
*/
|
|
96
|
-
getBaseURL(): string {
|
|
97
|
-
return this.loadConfig().baseURL || 'https://matexai-backend-550499663766.us-central1.run.app';
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Get all config
|
|
102
|
-
*/
|
|
103
|
-
getAll(): Config {
|
|
104
|
-
return this.loadConfig();
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Clear all config
|
|
109
|
-
*/
|
|
110
|
-
clear(): void {
|
|
111
|
-
if (fs.existsSync(this.configPath)) {
|
|
112
|
-
fs.unlinkSync(this.configPath);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Check if API key is set
|
|
118
|
-
*/
|
|
119
|
-
hasAPIKey(): boolean {
|
|
120
|
-
return !!this.getAPIKey();
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export const configManager = new ConfigManager();
|
package/src/utils/mcp-server.ts
DELETED
|
@@ -1,388 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* MCP (Model Context Protocol) Server Manager
|
|
3
|
-
* =============================================
|
|
4
|
-
* Production-level MCP servers that give CLI agents the ability to:
|
|
5
|
-
* - Read/write/search files on the local filesystem
|
|
6
|
-
* - Execute shell commands
|
|
7
|
-
* - Fetch web content (URL context)
|
|
8
|
-
* - Search Google (via SearxNG or Brave)
|
|
9
|
-
* - Access project context (repo map, git status)
|
|
10
|
-
*
|
|
11
|
-
* These tools are injected into agent system prompts so the AI
|
|
12
|
-
* knows what capabilities it has and can request them.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import * as fs from 'fs';
|
|
16
|
-
import * as path from 'path';
|
|
17
|
-
import { execSync } from 'child_process';
|
|
18
|
-
import chalk from 'chalk';
|
|
19
|
-
import https from 'https';
|
|
20
|
-
import http from 'http';
|
|
21
|
-
|
|
22
|
-
// ============================================================
|
|
23
|
-
// TOOL DEFINITIONS (MCP-style JSON Schema)
|
|
24
|
-
// ============================================================
|
|
25
|
-
|
|
26
|
-
export interface MCPToolDefinition {
|
|
27
|
-
name: string;
|
|
28
|
-
description: string;
|
|
29
|
-
parameters: Record<string, any>;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface MCPToolResult {
|
|
33
|
-
success: boolean;
|
|
34
|
-
output?: string;
|
|
35
|
-
error?: string;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* All available MCP tools for CLI agents
|
|
40
|
-
*/
|
|
41
|
-
export const MCP_TOOLS: MCPToolDefinition[] = [
|
|
42
|
-
{
|
|
43
|
-
name: 'read_file',
|
|
44
|
-
description: 'Read the full contents of a file at the given path.',
|
|
45
|
-
parameters: {
|
|
46
|
-
type: 'object',
|
|
47
|
-
properties: {
|
|
48
|
-
path: { type: 'string', description: 'Absolute or relative file path' },
|
|
49
|
-
start_line: { type: 'number', description: 'Optional start line (1-indexed)' },
|
|
50
|
-
end_line: { type: 'number', description: 'Optional end line (1-indexed)' },
|
|
51
|
-
},
|
|
52
|
-
required: ['path'],
|
|
53
|
-
},
|
|
54
|
-
},
|
|
55
|
-
{
|
|
56
|
-
name: 'write_file',
|
|
57
|
-
description: 'Create or overwrite a file with the given content.',
|
|
58
|
-
parameters: {
|
|
59
|
-
type: 'object',
|
|
60
|
-
properties: {
|
|
61
|
-
path: { type: 'string', description: 'File path to write' },
|
|
62
|
-
content: { type: 'string', description: 'File content to write' },
|
|
63
|
-
},
|
|
64
|
-
required: ['path', 'content'],
|
|
65
|
-
},
|
|
66
|
-
},
|
|
67
|
-
{
|
|
68
|
-
name: 'list_directory',
|
|
69
|
-
description: 'List files and subdirectories at a given path.',
|
|
70
|
-
parameters: {
|
|
71
|
-
type: 'object',
|
|
72
|
-
properties: {
|
|
73
|
-
path: { type: 'string', description: 'Directory path to list' },
|
|
74
|
-
recursive: { type: 'boolean', description: 'If true, list recursively (max depth 3)' },
|
|
75
|
-
},
|
|
76
|
-
required: ['path'],
|
|
77
|
-
},
|
|
78
|
-
},
|
|
79
|
-
{
|
|
80
|
-
name: 'search_files',
|
|
81
|
-
description: 'Search for a text pattern across files using grep.',
|
|
82
|
-
parameters: {
|
|
83
|
-
type: 'object',
|
|
84
|
-
properties: {
|
|
85
|
-
pattern: { type: 'string', description: 'Search pattern (regex supported)' },
|
|
86
|
-
path: { type: 'string', description: 'Directory or file to search in' },
|
|
87
|
-
include: { type: 'string', description: 'Glob pattern to filter files (e.g., "*.ts")' },
|
|
88
|
-
},
|
|
89
|
-
required: ['pattern', 'path'],
|
|
90
|
-
},
|
|
91
|
-
},
|
|
92
|
-
{
|
|
93
|
-
name: 'run_command',
|
|
94
|
-
description: 'Execute a shell command and return its output.',
|
|
95
|
-
parameters: {
|
|
96
|
-
type: 'object',
|
|
97
|
-
properties: {
|
|
98
|
-
command: { type: 'string', description: 'Shell command to execute' },
|
|
99
|
-
cwd: { type: 'string', description: 'Working directory for the command' },
|
|
100
|
-
},
|
|
101
|
-
required: ['command'],
|
|
102
|
-
},
|
|
103
|
-
},
|
|
104
|
-
{
|
|
105
|
-
name: 'fetch_url',
|
|
106
|
-
description: 'Fetch content from a URL and return it as text.',
|
|
107
|
-
parameters: {
|
|
108
|
-
type: 'object',
|
|
109
|
-
properties: {
|
|
110
|
-
url: { type: 'string', description: 'URL to fetch' },
|
|
111
|
-
},
|
|
112
|
-
required: ['url'],
|
|
113
|
-
},
|
|
114
|
-
},
|
|
115
|
-
{
|
|
116
|
-
name: 'git_status',
|
|
117
|
-
description: 'Get the current git status of the repository.',
|
|
118
|
-
parameters: {
|
|
119
|
-
type: 'object',
|
|
120
|
-
properties: {
|
|
121
|
-
path: { type: 'string', description: 'Repository path' },
|
|
122
|
-
},
|
|
123
|
-
required: ['path'],
|
|
124
|
-
},
|
|
125
|
-
},
|
|
126
|
-
{
|
|
127
|
-
name: 'web_search',
|
|
128
|
-
description: 'Search the web using SearxNG or Brave Search API.',
|
|
129
|
-
parameters: {
|
|
130
|
-
type: 'object',
|
|
131
|
-
properties: {
|
|
132
|
-
query: { type: 'string', description: 'Search query' },
|
|
133
|
-
num_results: { type: 'number', description: 'Number of results (default 5)' },
|
|
134
|
-
},
|
|
135
|
-
required: ['query'],
|
|
136
|
-
},
|
|
137
|
-
},
|
|
138
|
-
];
|
|
139
|
-
|
|
140
|
-
// ============================================================
|
|
141
|
-
// TOOL EXECUTORS
|
|
142
|
-
// ============================================================
|
|
143
|
-
|
|
144
|
-
export class MCPServer {
|
|
145
|
-
private cwd: string;
|
|
146
|
-
private braveApiKey?: string;
|
|
147
|
-
private searxngUrl?: string;
|
|
148
|
-
|
|
149
|
-
constructor(cwd: string, options?: { braveApiKey?: string; searxngUrl?: string }) {
|
|
150
|
-
this.cwd = cwd;
|
|
151
|
-
this.braveApiKey = options?.braveApiKey;
|
|
152
|
-
this.searxngUrl = options?.searxngUrl;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Execute an MCP tool by name
|
|
157
|
-
*/
|
|
158
|
-
async execute(toolName: string, args: Record<string, any>): Promise<MCPToolResult> {
|
|
159
|
-
try {
|
|
160
|
-
switch (toolName) {
|
|
161
|
-
case 'read_file': return this.readFile(args.path, args.start_line, args.end_line);
|
|
162
|
-
case 'write_file': return this.writeFile(args.path, args.content);
|
|
163
|
-
case 'list_directory': return this.listDirectory(args.path, args.recursive);
|
|
164
|
-
case 'search_files': return this.searchFiles(args.pattern, args.path, args.include);
|
|
165
|
-
case 'run_command': return this.runCommand(args.command, args.cwd);
|
|
166
|
-
case 'fetch_url': return await this.fetchUrl(args.url);
|
|
167
|
-
case 'git_status': return this.gitStatus(args.path);
|
|
168
|
-
case 'web_search': return await this.webSearch(args.query, args.num_results);
|
|
169
|
-
default: return { success: false, error: `Unknown tool: ${toolName}` };
|
|
170
|
-
}
|
|
171
|
-
} catch (err: any) {
|
|
172
|
-
return { success: false, error: err.message };
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Read a file
|
|
178
|
-
*/
|
|
179
|
-
private readFile(filePath: string, startLine?: number, endLine?: number): MCPToolResult {
|
|
180
|
-
const resolved = path.isAbsolute(filePath) ? filePath : path.resolve(this.cwd, filePath);
|
|
181
|
-
if (!fs.existsSync(resolved)) {
|
|
182
|
-
return { success: false, error: `File not found: ${resolved}` };
|
|
183
|
-
}
|
|
184
|
-
const content = fs.readFileSync(resolved, 'utf-8');
|
|
185
|
-
if (startLine || endLine) {
|
|
186
|
-
const lines = content.split('\n');
|
|
187
|
-
const start = Math.max(0, (startLine || 1) - 1);
|
|
188
|
-
const end = Math.min(lines.length, endLine || lines.length);
|
|
189
|
-
return { success: true, output: lines.slice(start, end).join('\n') };
|
|
190
|
-
}
|
|
191
|
-
// Cap output at 500 lines
|
|
192
|
-
const lines = content.split('\n');
|
|
193
|
-
if (lines.length > 500) {
|
|
194
|
-
return { success: true, output: lines.slice(0, 500).join('\n') + `\n\n... (${lines.length - 500} more lines truncated)` };
|
|
195
|
-
}
|
|
196
|
-
return { success: true, output: content };
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Write a file (creates directories if needed)
|
|
201
|
-
*/
|
|
202
|
-
private writeFile(filePath: string, content: string): MCPToolResult {
|
|
203
|
-
const resolved = path.isAbsolute(filePath) ? filePath : path.resolve(this.cwd, filePath);
|
|
204
|
-
const dir = path.dirname(resolved);
|
|
205
|
-
if (!fs.existsSync(dir)) {
|
|
206
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
207
|
-
}
|
|
208
|
-
fs.writeFileSync(resolved, content, 'utf-8');
|
|
209
|
-
return { success: true, output: `File written: ${resolved}` };
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* List directory contents
|
|
214
|
-
*/
|
|
215
|
-
private listDirectory(dirPath: string, recursive?: boolean): MCPToolResult {
|
|
216
|
-
const resolved = path.isAbsolute(dirPath) ? dirPath : path.resolve(this.cwd, dirPath);
|
|
217
|
-
if (!fs.existsSync(resolved)) {
|
|
218
|
-
return { success: false, error: `Directory not found: ${resolved}` };
|
|
219
|
-
}
|
|
220
|
-
try {
|
|
221
|
-
const maxDepth = recursive ? 3 : 1;
|
|
222
|
-
const result = execSync(
|
|
223
|
-
`find "${resolved}" -maxdepth ${maxDepth} -not -path '*/node_modules/*' -not -path '*/.git/*' -not -path '*/dist/*' | head -200`,
|
|
224
|
-
{ encoding: 'utf-8', timeout: 5000 }
|
|
225
|
-
).trim();
|
|
226
|
-
return { success: true, output: result };
|
|
227
|
-
} catch {
|
|
228
|
-
return { success: false, error: `Failed to list directory: ${resolved}` };
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Search files with grep
|
|
234
|
-
*/
|
|
235
|
-
private searchFiles(pattern: string, searchPath: string, include?: string): MCPToolResult {
|
|
236
|
-
const resolved = path.isAbsolute(searchPath) ? searchPath : path.resolve(this.cwd, searchPath);
|
|
237
|
-
try {
|
|
238
|
-
const includeFlag = include ? `--include='${include}'` : '';
|
|
239
|
-
const result = execSync(
|
|
240
|
-
`grep -rn ${includeFlag} --color=never "${pattern}" "${resolved}" 2>/dev/null | head -50`,
|
|
241
|
-
{ encoding: 'utf-8', timeout: 10000 }
|
|
242
|
-
).trim();
|
|
243
|
-
return { success: true, output: result || 'No matches found.' };
|
|
244
|
-
} catch {
|
|
245
|
-
return { success: true, output: 'No matches found.' };
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Run a shell command
|
|
251
|
-
*/
|
|
252
|
-
private runCommand(command: string, cwd?: string): MCPToolResult {
|
|
253
|
-
const workDir = cwd ? (path.isAbsolute(cwd) ? cwd : path.resolve(this.cwd, cwd)) : this.cwd;
|
|
254
|
-
try {
|
|
255
|
-
const result = execSync(command, {
|
|
256
|
-
encoding: 'utf-8',
|
|
257
|
-
cwd: workDir,
|
|
258
|
-
timeout: 30000,
|
|
259
|
-
env: { ...process.env, FORCE_COLOR: '0' },
|
|
260
|
-
}).trim();
|
|
261
|
-
return { success: true, output: result || '(No output)' };
|
|
262
|
-
} catch (err: any) {
|
|
263
|
-
return { success: false, error: err.stderr || err.message };
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Fetch URL content
|
|
269
|
-
*/
|
|
270
|
-
private async fetchUrl(url: string): Promise<MCPToolResult> {
|
|
271
|
-
return new Promise((resolve) => {
|
|
272
|
-
const client = url.startsWith('https') ? https : http;
|
|
273
|
-
const req = client.get(url, { timeout: 10000 }, (res) => {
|
|
274
|
-
let data = '';
|
|
275
|
-
res.on('data', (chunk: Buffer) => { data += chunk.toString(); });
|
|
276
|
-
res.on('end', () => {
|
|
277
|
-
// Strip HTML tags for readability, cap at 5000 chars
|
|
278
|
-
const text = data.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim();
|
|
279
|
-
resolve({
|
|
280
|
-
success: true,
|
|
281
|
-
output: text.length > 5000 ? text.substring(0, 5000) + '...' : text,
|
|
282
|
-
});
|
|
283
|
-
});
|
|
284
|
-
});
|
|
285
|
-
req.on('error', (err: Error) => resolve({ success: false, error: err.message }));
|
|
286
|
-
req.on('timeout', () => { req.destroy(); resolve({ success: false, error: 'Request timed out' }); });
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Git status
|
|
292
|
-
*/
|
|
293
|
-
private gitStatus(repoPath: string): MCPToolResult {
|
|
294
|
-
const resolved = path.isAbsolute(repoPath) ? repoPath : path.resolve(this.cwd, repoPath);
|
|
295
|
-
try {
|
|
296
|
-
const status = execSync('git status --short', { encoding: 'utf-8', cwd: resolved }).trim();
|
|
297
|
-
const branch = execSync('git branch --show-current', { encoding: 'utf-8', cwd: resolved }).trim();
|
|
298
|
-
const lastCommit = execSync('git log -1 --oneline', { encoding: 'utf-8', cwd: resolved }).trim();
|
|
299
|
-
return {
|
|
300
|
-
success: true,
|
|
301
|
-
output: `Branch: ${branch}\nLast commit: ${lastCommit}\n\nChanged files:\n${status || '(clean)'}`,
|
|
302
|
-
};
|
|
303
|
-
} catch (err: any) {
|
|
304
|
-
return { success: false, error: 'Not a git repository or git not available.' };
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Web search via SearxNG or Brave
|
|
310
|
-
*/
|
|
311
|
-
private async webSearch(query: string, numResults: number = 5): Promise<MCPToolResult> {
|
|
312
|
-
// Try SearxNG first
|
|
313
|
-
if (this.searxngUrl) {
|
|
314
|
-
try {
|
|
315
|
-
const url = `${this.searxngUrl}/search?q=${encodeURIComponent(query)}&format=json&engines=google,bing&results=${numResults}`;
|
|
316
|
-
const result = await this.fetchUrl(url);
|
|
317
|
-
if (result.success && result.output) {
|
|
318
|
-
try {
|
|
319
|
-
const data = JSON.parse(result.output);
|
|
320
|
-
const results = (data.results || []).slice(0, numResults).map((r: any, i: number) =>
|
|
321
|
-
`${i + 1}. ${r.title}\n ${r.url}\n ${(r.content || '').substring(0, 200)}`
|
|
322
|
-
).join('\n\n');
|
|
323
|
-
return { success: true, output: results || 'No results found.' };
|
|
324
|
-
} catch { /* fall through */ }
|
|
325
|
-
}
|
|
326
|
-
} catch { /* fall through to Brave */ }
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
// Fallback: Brave Search
|
|
330
|
-
if (this.braveApiKey) {
|
|
331
|
-
return new Promise((resolve) => {
|
|
332
|
-
const url = `https://api.search.brave.com/res/v1/web/search?q=${encodeURIComponent(query)}&count=${numResults}`;
|
|
333
|
-
const req = https.get(url, {
|
|
334
|
-
headers: {
|
|
335
|
-
'Accept': 'application/json',
|
|
336
|
-
'X-Subscription-Token': this.braveApiKey!,
|
|
337
|
-
},
|
|
338
|
-
timeout: 10000,
|
|
339
|
-
}, (res) => {
|
|
340
|
-
let data = '';
|
|
341
|
-
res.on('data', (chunk: Buffer) => { data += chunk.toString(); });
|
|
342
|
-
res.on('end', () => {
|
|
343
|
-
try {
|
|
344
|
-
const parsed = JSON.parse(data);
|
|
345
|
-
const results = (parsed.web?.results || []).slice(0, numResults).map((r: any, i: number) =>
|
|
346
|
-
`${i + 1}. ${r.title}\n ${r.url}\n ${(r.description || '').substring(0, 200)}`
|
|
347
|
-
).join('\n\n');
|
|
348
|
-
resolve({ success: true, output: results || 'No results found.' });
|
|
349
|
-
} catch {
|
|
350
|
-
resolve({ success: false, error: 'Failed to parse search results.' });
|
|
351
|
-
}
|
|
352
|
-
});
|
|
353
|
-
});
|
|
354
|
-
req.on('error', (err: Error) => resolve({ success: false, error: err.message }));
|
|
355
|
-
});
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
return { success: false, error: 'No search backend configured. Set BRAVE_SEARCH_API_KEY or SEARXNG_URL.' };
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* Generate the MCP tool capabilities section for system prompts
|
|
363
|
-
*/
|
|
364
|
-
static getToolsPromptSection(): string {
|
|
365
|
-
return `### 🔌 MCP TOOLS (Model Context Protocol):
|
|
366
|
-
You have access to the following production-level tools via MCP. Use them to navigate, read, write, and search:
|
|
367
|
-
|
|
368
|
-
| Tool | Description |
|
|
369
|
-
|------|-------------|
|
|
370
|
-
| \`read_file(path, start_line?, end_line?)\` | Read file contents (supports line ranges) |
|
|
371
|
-
| \`write_file(path, content)\` | Create or overwrite a file |
|
|
372
|
-
| \`list_directory(path, recursive?)\` | List files and subdirectories |
|
|
373
|
-
| \`search_files(pattern, path, include?)\` | Search for text patterns (grep) |
|
|
374
|
-
| \`run_command(command, cwd?)\` | Execute a shell command |
|
|
375
|
-
| \`fetch_url(url)\` | Fetch web content from a URL |
|
|
376
|
-
| \`git_status(path)\` | Get git status, branch, and last commit |
|
|
377
|
-
| \`web_search(query, num_results?)\` | Search the web via SearxNG/Brave |
|
|
378
|
-
|
|
379
|
-
**HOW TO USE:** When you need to use a tool, output it as a shell command:
|
|
380
|
-
- To read: \`head -50 path/to/file\` or \`cat path/to/file\`
|
|
381
|
-
- To search: \`grep -rn "pattern" path/\`
|
|
382
|
-
- To list: \`ls -la path/\` or \`find path/ -maxdepth 2\`
|
|
383
|
-
- To execute: wrap in \`\`\`bash code blocks
|
|
384
|
-
- To fetch URL: \`curl -s "url" | head -100\`
|
|
385
|
-
|
|
386
|
-
These tools allow you to **navigate any file/directory** on the system using your own brain to decide where to go.`;
|
|
387
|
-
}
|
|
388
|
-
}
|