ai-cli-mcp 2.11.0 → 2.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/README.ja.md +102 -3
- package/README.md +102 -3
- package/dist/__tests__/app-cli.test.js +285 -0
- package/dist/__tests__/cli-bin-smoke.test.js +54 -0
- package/dist/__tests__/cli-process-service.test.js +233 -0
- package/dist/__tests__/cli-utils.test.js +109 -0
- package/dist/__tests__/error-cases.test.js +2 -1
- package/dist/__tests__/mcp-contract.test.js +195 -0
- package/dist/__tests__/process-management.test.js +15 -8
- package/dist/__tests__/server.test.js +29 -3
- package/dist/__tests__/wait.test.js +31 -0
- package/dist/app/cli.js +304 -0
- package/dist/app/mcp.js +362 -0
- package/dist/bin/ai-cli-mcp.js +6 -0
- package/dist/bin/ai-cli.js +10 -0
- package/dist/cli-builder.js +1 -6
- package/dist/cli-process-service.js +328 -0
- package/dist/cli-utils.js +142 -88
- package/dist/model-catalog.js +50 -0
- package/dist/process-service.js +198 -0
- package/dist/server.js +3 -577
- package/docs/cli-architecture.md +275 -0
- package/package.json +3 -2
- package/src/__tests__/app-cli.test.ts +362 -0
- package/src/__tests__/cli-bin-smoke.test.ts +71 -0
- package/src/__tests__/cli-process-service.test.ts +278 -0
- package/src/__tests__/cli-utils.test.ts +132 -0
- package/src/__tests__/error-cases.test.ts +3 -4
- package/src/__tests__/mcp-contract.test.ts +250 -0
- package/src/__tests__/process-management.test.ts +15 -9
- package/src/__tests__/server.test.ts +27 -6
- package/src/__tests__/wait.test.ts +38 -0
- package/src/app/cli.ts +373 -0
- package/src/app/mcp.ts +398 -0
- package/src/bin/ai-cli-mcp.ts +7 -0
- package/src/bin/ai-cli.ts +11 -0
- package/src/cli-builder.ts +1 -7
- package/src/cli-process-service.ts +415 -0
- package/src/cli-utils.ts +185 -99
- package/src/model-catalog.ts +60 -0
- package/src/process-service.ts +261 -0
- package/src/server.ts +3 -667
package/dist/app/mcp.js
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
2
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
3
|
+
import { CallToolRequestSchema, ErrorCode, ListToolsRequestSchema, McpError, } from '@modelcontextprotocol/sdk/types.js';
|
|
4
|
+
import { spawn } from 'node:child_process';
|
|
5
|
+
import { debugLog, findClaudeCli, findCodexCli, findGeminiCli } from '../cli-utils.js';
|
|
6
|
+
import { getModelParameterDescription, getSupportedModelsDescription } from '../model-catalog.js';
|
|
7
|
+
import { ProcessService } from '../process-service.js';
|
|
8
|
+
// Server version - update this when releasing new versions
|
|
9
|
+
const SERVER_VERSION = "2.2.0";
|
|
10
|
+
// Track if this is the first tool use for version printing
|
|
11
|
+
let isFirstToolUse = true;
|
|
12
|
+
// Capture server startup time when the module loads
|
|
13
|
+
const serverStartupTime = new Date().toISOString();
|
|
14
|
+
// Ensure spawnAsync is defined correctly *before* the class
|
|
15
|
+
export async function spawnAsync(command, args, options) {
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
debugLog(`[Spawn] Running command: ${command} ${args.join(' ')}`);
|
|
18
|
+
const process = spawn(command, args, {
|
|
19
|
+
shell: false,
|
|
20
|
+
timeout: options?.timeout,
|
|
21
|
+
cwd: options?.cwd,
|
|
22
|
+
stdio: ['ignore', 'pipe', 'pipe']
|
|
23
|
+
});
|
|
24
|
+
let stdout = '';
|
|
25
|
+
let stderr = '';
|
|
26
|
+
process.stdout.on('data', (data) => { stdout += data.toString(); });
|
|
27
|
+
process.stderr.on('data', (data) => {
|
|
28
|
+
stderr += data.toString();
|
|
29
|
+
debugLog(`[Spawn Stderr Chunk] ${data.toString()}`);
|
|
30
|
+
});
|
|
31
|
+
process.on('error', (error) => {
|
|
32
|
+
debugLog(`[Spawn Error Event] Full error object:`, error);
|
|
33
|
+
let errorMessage = `Spawn error: ${error.message}`;
|
|
34
|
+
if (error.path) {
|
|
35
|
+
errorMessage += ` | Path: ${error.path}`;
|
|
36
|
+
}
|
|
37
|
+
if (error.syscall) {
|
|
38
|
+
errorMessage += ` | Syscall: ${error.syscall}`;
|
|
39
|
+
}
|
|
40
|
+
errorMessage += `\nStderr: ${stderr.trim()}`;
|
|
41
|
+
reject(new Error(errorMessage));
|
|
42
|
+
});
|
|
43
|
+
process.on('close', (code) => {
|
|
44
|
+
debugLog(`[Spawn Close] Exit code: ${code}`);
|
|
45
|
+
debugLog(`[Spawn Stderr Full] ${stderr.trim()}`);
|
|
46
|
+
debugLog(`[Spawn Stdout Full] ${stdout.trim()}`);
|
|
47
|
+
if (code === 0) {
|
|
48
|
+
resolve({ stdout, stderr });
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
reject(new Error(`Command failed with exit code ${code}\nStderr: ${stderr.trim()}\nStdout: ${stdout.trim()}`));
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
export class ClaudeCodeServer {
|
|
57
|
+
server;
|
|
58
|
+
claudeCliPath;
|
|
59
|
+
codexCliPath;
|
|
60
|
+
geminiCliPath;
|
|
61
|
+
processService;
|
|
62
|
+
sigintHandler;
|
|
63
|
+
packageVersion;
|
|
64
|
+
constructor() {
|
|
65
|
+
this.claudeCliPath = findClaudeCli();
|
|
66
|
+
this.codexCliPath = findCodexCli();
|
|
67
|
+
this.geminiCliPath = findGeminiCli();
|
|
68
|
+
console.error(`[Setup] Using Claude CLI command/path: ${this.claudeCliPath}`);
|
|
69
|
+
console.error(`[Setup] Using Codex CLI command/path: ${this.codexCliPath}`);
|
|
70
|
+
console.error(`[Setup] Using Gemini CLI command/path: ${this.geminiCliPath}`);
|
|
71
|
+
this.packageVersion = SERVER_VERSION;
|
|
72
|
+
this.processService = new ProcessService({
|
|
73
|
+
cliPaths: {
|
|
74
|
+
claude: this.claudeCliPath,
|
|
75
|
+
codex: this.codexCliPath,
|
|
76
|
+
gemini: this.geminiCliPath,
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
this.server = new Server({
|
|
80
|
+
name: 'ai_cli_mcp',
|
|
81
|
+
version: SERVER_VERSION,
|
|
82
|
+
}, {
|
|
83
|
+
capabilities: {
|
|
84
|
+
tools: {},
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
this.setupToolHandlers();
|
|
88
|
+
this.server.onerror = (error) => console.error('[Error]', error);
|
|
89
|
+
this.sigintHandler = async () => {
|
|
90
|
+
await this.server.close();
|
|
91
|
+
process.exit(0);
|
|
92
|
+
};
|
|
93
|
+
process.on('SIGINT', this.sigintHandler);
|
|
94
|
+
}
|
|
95
|
+
setupToolHandlers() {
|
|
96
|
+
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
97
|
+
tools: [
|
|
98
|
+
{
|
|
99
|
+
name: 'run',
|
|
100
|
+
description: `AI Agent Runner: Starts a Claude, Codex, or Gemini CLI process in the background and returns a PID immediately. Use list_processes and get_result to monitor progress.
|
|
101
|
+
|
|
102
|
+
• File ops: Create, read, (fuzzy) edit, move, copy, delete, list files, analyze/ocr images, file content analysis
|
|
103
|
+
• Code: Generate / analyse / refactor / fix
|
|
104
|
+
• Git: Stage ▸ commit ▸ push ▸ tag (any workflow)
|
|
105
|
+
• Terminal: Run any CLI cmd or open URLs
|
|
106
|
+
• Web search + summarise content on-the-fly
|
|
107
|
+
• Multi-step workflows & GitHub integration
|
|
108
|
+
|
|
109
|
+
**IMPORTANT**: This tool now returns immediately with a PID. Use other tools to check status and get results.
|
|
110
|
+
|
|
111
|
+
**Supported models**:
|
|
112
|
+
${getSupportedModelsDescription()}
|
|
113
|
+
|
|
114
|
+
**Prompt input**: You must provide EITHER prompt (string) OR prompt_file (file path), but not both.
|
|
115
|
+
|
|
116
|
+
**Prompt tips**
|
|
117
|
+
1. Be concise, explicit & step-by-step for complex tasks.
|
|
118
|
+
2. Check process status with list_processes
|
|
119
|
+
3. Get results with get_result using the returned PID
|
|
120
|
+
4. Kill long-running processes with kill_process if needed
|
|
121
|
+
|
|
122
|
+
`,
|
|
123
|
+
inputSchema: {
|
|
124
|
+
type: 'object',
|
|
125
|
+
properties: {
|
|
126
|
+
prompt: {
|
|
127
|
+
type: 'string',
|
|
128
|
+
description: 'The detailed natural language prompt for the agent to execute. Either this or prompt_file is required.',
|
|
129
|
+
},
|
|
130
|
+
prompt_file: {
|
|
131
|
+
type: 'string',
|
|
132
|
+
description: 'Path to a file containing the prompt. Either this or prompt is required. Must be an absolute path or relative to workFolder.',
|
|
133
|
+
},
|
|
134
|
+
workFolder: {
|
|
135
|
+
type: 'string',
|
|
136
|
+
description: 'The working directory for the agent execution. Must be an absolute path.',
|
|
137
|
+
},
|
|
138
|
+
model: {
|
|
139
|
+
type: 'string',
|
|
140
|
+
description: getModelParameterDescription(),
|
|
141
|
+
},
|
|
142
|
+
reasoning_effort: {
|
|
143
|
+
type: 'string',
|
|
144
|
+
description: 'Reasoning control for Claude and Codex. Claude uses --effort with "low", "medium", "high". Codex uses model_reasoning_effort with "low", "medium", "high", "xhigh".',
|
|
145
|
+
},
|
|
146
|
+
session_id: {
|
|
147
|
+
type: 'string',
|
|
148
|
+
description: 'Optional session ID to resume a previous session. Supported for: haiku, sonnet, opus, gemini-2.5-pro, gemini-2.5-flash, gemini-3.1-pro-preview, gemini-3-pro-preview, gemini-3-flash-preview.',
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
required: ['workFolder'],
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
name: 'list_processes',
|
|
156
|
+
description: 'List all running and completed AI agent processes. Returns a simple list with PID, agent type, and status for each process.',
|
|
157
|
+
inputSchema: {
|
|
158
|
+
type: 'object',
|
|
159
|
+
properties: {},
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
name: 'get_result',
|
|
164
|
+
description: 'Get the current output and status of an AI agent process by PID. Returns the output from the agent including session_id (if applicable), along with process metadata.',
|
|
165
|
+
inputSchema: {
|
|
166
|
+
type: 'object',
|
|
167
|
+
properties: {
|
|
168
|
+
pid: {
|
|
169
|
+
type: 'number',
|
|
170
|
+
description: 'The process ID returned by run tool.',
|
|
171
|
+
},
|
|
172
|
+
verbose: {
|
|
173
|
+
type: 'boolean',
|
|
174
|
+
description: 'Optional: If true, returns detailed execution information including tool usage history. Defaults to false.',
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
required: ['pid'],
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
name: 'wait',
|
|
182
|
+
description: 'Wait for multiple AI agent processes to complete and return their results. Blocks until all specified PIDs finish or timeout occurs.',
|
|
183
|
+
inputSchema: {
|
|
184
|
+
type: 'object',
|
|
185
|
+
properties: {
|
|
186
|
+
pids: {
|
|
187
|
+
type: 'array',
|
|
188
|
+
items: { type: 'number' },
|
|
189
|
+
description: 'List of process IDs to wait for (returned by the run tool).',
|
|
190
|
+
},
|
|
191
|
+
timeout: {
|
|
192
|
+
type: 'number',
|
|
193
|
+
description: 'Optional: Maximum time to wait in seconds. Defaults to 180 (3 minutes).',
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
required: ['pids'],
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
name: 'kill_process',
|
|
201
|
+
description: 'Terminate a running AI agent process by PID.',
|
|
202
|
+
inputSchema: {
|
|
203
|
+
type: 'object',
|
|
204
|
+
properties: {
|
|
205
|
+
pid: {
|
|
206
|
+
type: 'number',
|
|
207
|
+
description: 'The process ID to terminate.',
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
required: ['pid'],
|
|
211
|
+
},
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
name: 'cleanup_processes',
|
|
215
|
+
description: 'Remove all completed and failed processes from the process list to free up memory.',
|
|
216
|
+
inputSchema: {
|
|
217
|
+
type: 'object',
|
|
218
|
+
properties: {},
|
|
219
|
+
},
|
|
220
|
+
}
|
|
221
|
+
],
|
|
222
|
+
}));
|
|
223
|
+
this.server.setRequestHandler(CallToolRequestSchema, async (args) => {
|
|
224
|
+
debugLog('[Debug] Handling CallToolRequest:', args);
|
|
225
|
+
const toolName = args.params.name;
|
|
226
|
+
const toolArguments = args.params.arguments || {};
|
|
227
|
+
switch (toolName) {
|
|
228
|
+
case 'run':
|
|
229
|
+
return this.handleRun(toolArguments);
|
|
230
|
+
case 'list_processes':
|
|
231
|
+
return this.handleListProcesses();
|
|
232
|
+
case 'get_result':
|
|
233
|
+
return this.handleGetResult(toolArguments);
|
|
234
|
+
case 'wait':
|
|
235
|
+
return this.handleWait(toolArguments);
|
|
236
|
+
case 'kill_process':
|
|
237
|
+
return this.handleKillProcess(toolArguments);
|
|
238
|
+
case 'cleanup_processes':
|
|
239
|
+
return this.handleCleanupProcesses();
|
|
240
|
+
default:
|
|
241
|
+
throw new McpError(ErrorCode.MethodNotFound, `Tool ${toolName} not found`);
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
async handleRun(toolArguments) {
|
|
246
|
+
if (isFirstToolUse) {
|
|
247
|
+
console.error(`ai_cli_mcp v${SERVER_VERSION} started at ${serverStartupTime}`);
|
|
248
|
+
isFirstToolUse = false;
|
|
249
|
+
}
|
|
250
|
+
try {
|
|
251
|
+
const result = this.processService.startProcess({
|
|
252
|
+
prompt: toolArguments.prompt,
|
|
253
|
+
prompt_file: toolArguments.prompt_file,
|
|
254
|
+
workFolder: toolArguments.workFolder,
|
|
255
|
+
model: toolArguments.model,
|
|
256
|
+
session_id: toolArguments.session_id,
|
|
257
|
+
reasoning_effort: toolArguments.reasoning_effort,
|
|
258
|
+
});
|
|
259
|
+
return {
|
|
260
|
+
content: [{
|
|
261
|
+
type: 'text',
|
|
262
|
+
text: JSON.stringify(result, null, 2)
|
|
263
|
+
}]
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
catch (error) {
|
|
267
|
+
const code = /Failed to start/.test(error.message) ? ErrorCode.InternalError : ErrorCode.InvalidParams;
|
|
268
|
+
throw new McpError(code, error.message);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
async handleListProcesses() {
|
|
272
|
+
return {
|
|
273
|
+
content: [{
|
|
274
|
+
type: 'text',
|
|
275
|
+
text: JSON.stringify(this.processService.listProcesses(), null, 2)
|
|
276
|
+
}]
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
async handleGetResult(toolArguments) {
|
|
280
|
+
if (!toolArguments.pid || typeof toolArguments.pid !== 'number') {
|
|
281
|
+
throw new McpError(ErrorCode.InvalidParams, 'Missing or invalid required parameter: pid');
|
|
282
|
+
}
|
|
283
|
+
const pid = toolArguments.pid;
|
|
284
|
+
const verbose = !!toolArguments.verbose;
|
|
285
|
+
try {
|
|
286
|
+
const response = this.processService.getProcessResult(pid, verbose);
|
|
287
|
+
return {
|
|
288
|
+
content: [{
|
|
289
|
+
type: 'text',
|
|
290
|
+
text: JSON.stringify(response, null, 2)
|
|
291
|
+
}]
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
catch (error) {
|
|
295
|
+
const code = /not found/.test(error.message) ? ErrorCode.InvalidParams : ErrorCode.InternalError;
|
|
296
|
+
throw new McpError(code, error.message);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
async handleWait(toolArguments) {
|
|
300
|
+
if (!toolArguments.pids || !Array.isArray(toolArguments.pids) || toolArguments.pids.length === 0) {
|
|
301
|
+
throw new McpError(ErrorCode.InvalidParams, 'Missing or invalid required parameter: pids (must be a non-empty array of numbers)');
|
|
302
|
+
}
|
|
303
|
+
try {
|
|
304
|
+
const results = await this.processService.waitForProcesses(toolArguments.pids, typeof toolArguments.timeout === 'number' ? toolArguments.timeout : 180);
|
|
305
|
+
return {
|
|
306
|
+
content: [{
|
|
307
|
+
type: 'text',
|
|
308
|
+
text: JSON.stringify(results, null, 2)
|
|
309
|
+
}]
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
const code = /not found/.test(error.message) ? ErrorCode.InvalidParams : ErrorCode.InternalError;
|
|
314
|
+
throw new McpError(code, error.message);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
async handleKillProcess(toolArguments) {
|
|
318
|
+
if (!toolArguments.pid || typeof toolArguments.pid !== 'number') {
|
|
319
|
+
throw new McpError(ErrorCode.InvalidParams, 'Missing or invalid required parameter: pid');
|
|
320
|
+
}
|
|
321
|
+
const pid = toolArguments.pid;
|
|
322
|
+
try {
|
|
323
|
+
const response = this.processService.killProcess(pid);
|
|
324
|
+
return {
|
|
325
|
+
content: [{
|
|
326
|
+
type: 'text',
|
|
327
|
+
text: JSON.stringify(response, null, 2)
|
|
328
|
+
}]
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
catch (error) {
|
|
332
|
+
const code = /not found/.test(error.message) ? ErrorCode.InvalidParams : ErrorCode.InternalError;
|
|
333
|
+
const message = code === ErrorCode.InternalError
|
|
334
|
+
? `Failed to terminate process: ${error.message}`
|
|
335
|
+
: error.message;
|
|
336
|
+
throw new McpError(code, message);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
async handleCleanupProcesses() {
|
|
340
|
+
return {
|
|
341
|
+
content: [{
|
|
342
|
+
type: 'text',
|
|
343
|
+
text: JSON.stringify(this.processService.cleanupProcesses(), null, 2)
|
|
344
|
+
}]
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
async run() {
|
|
348
|
+
const transport = new StdioServerTransport();
|
|
349
|
+
await this.server.connect(transport);
|
|
350
|
+
console.error('AI CLI MCP server running on stdio');
|
|
351
|
+
}
|
|
352
|
+
async cleanup() {
|
|
353
|
+
if (this.sigintHandler) {
|
|
354
|
+
process.removeListener('SIGINT', this.sigintHandler);
|
|
355
|
+
}
|
|
356
|
+
await this.server.close();
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
export async function runMcpServer() {
|
|
360
|
+
const server = new ClaudeCodeServer();
|
|
361
|
+
await server.run();
|
|
362
|
+
}
|
package/dist/cli-builder.js
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from 'node:fs';
|
|
2
2
|
import { resolve as pathResolve, isAbsolute } from 'node:path';
|
|
3
|
-
|
|
4
|
-
export const MODEL_ALIASES = {
|
|
5
|
-
'claude-ultra': 'opus',
|
|
6
|
-
'codex-ultra': 'gpt-5.4',
|
|
7
|
-
'gemini-ultra': 'gemini-3.1-pro-preview'
|
|
8
|
-
};
|
|
3
|
+
import { MODEL_ALIASES } from './model-catalog.js';
|
|
9
4
|
export const ALLOWED_REASONING_EFFORTS = new Set(['low', 'medium', 'high', 'xhigh']);
|
|
10
5
|
const CLAUDE_REASONING_EFFORTS = new Set(['low', 'medium', 'high']);
|
|
11
6
|
function getAgentForModel(model) {
|