-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathindex.ts
More file actions
executable file
·475 lines (412 loc) · 13.4 KB
/
index.ts
File metadata and controls
executable file
·475 lines (412 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#!/usr/bin/env bun
/**
* MCP-CLI - A lightweight CLI for interacting with MCP servers
*
* Commands:
* mcp-cli List all servers and tools
* mcp-cli info <server> Show server details
* mcp-cli info <server> <tool> Show tool schema
* mcp-cli grep <pattern> Search tools by glob pattern
* mcp-cli call <server> <tool> Call tool (reads JSON from stdin if no args)
* mcp-cli call <server> <tool> {} Call tool with JSON args
*/
import { callCommand } from './commands/call.js';
import { grepCommand } from './commands/grep.js';
import { infoCommand } from './commands/info.js';
import { listCommand } from './commands/list.js';
import {
DEFAULT_CONCURRENCY,
DEFAULT_MAX_RETRIES,
DEFAULT_RETRY_DELAY_MS,
DEFAULT_TIMEOUT_SECONDS,
listServerNames,
loadConfig,
} from './config.js';
import {
ErrorCode,
ambiguousCommandError,
formatCliError,
missingArgumentError,
tooManyArgumentsError,
unknownOptionError,
unknownSubcommandError,
} from './errors.js';
import { VERSION } from './version.js';
interface ParsedArgs {
command: 'list' | 'info' | 'grep' | 'call' | 'help' | 'version';
server?: string;
tool?: string;
pattern?: string;
args?: string;
withDescriptions: boolean;
configPath?: string;
}
/**
* Known subcommands
*/
const SUBCOMMANDS = ['info', 'grep', 'call'] as const;
/**
* Check if a string looks like a subcommand (not a server name)
*/
function isKnownSubcommand(arg: string): boolean {
return SUBCOMMANDS.includes(arg as (typeof SUBCOMMANDS)[number]);
}
/**
* Check if a string looks like it could be an unknown subcommand
* (common aliases that users might try)
*/
function isPossibleSubcommand(arg: string): boolean {
const aliases = [
'run',
'execute',
'exec',
'invoke',
'list',
'ls',
'get',
'show',
'describe',
'search',
'find',
'query',
];
return aliases.includes(arg.toLowerCase());
}
/**
* Parse server/tool from either "server/tool" or "server tool" format
*/
function parseServerTool(args: string[]): { server: string; tool?: string } {
if (args.length === 0) {
return { server: '' };
}
const first = args[0];
// Check for slash format: server/tool
if (first.includes('/')) {
const slashIndex = first.indexOf('/');
return {
server: first.substring(0, slashIndex),
tool: first.substring(slashIndex + 1) || undefined,
};
}
// Space format: server tool
return {
server: first,
tool: args[1],
};
}
/**
* Parse command line arguments
*/
function parseArgs(args: string[]): ParsedArgs {
const result: ParsedArgs = {
command: 'info',
withDescriptions: false,
};
const positional: string[] = [];
for (let i = 0; i < args.length; i++) {
const arg = args[i];
switch (arg) {
case '-h':
case '--help':
result.command = 'help';
return result;
case '-v':
case '--version':
result.command = 'version';
return result;
case '-d':
case '--with-descriptions':
result.withDescriptions = true;
break;
case '-c':
case '--config':
result.configPath = args[++i];
if (!result.configPath) {
console.error(
formatCliError(missingArgumentError('-c/--config', 'path')),
);
process.exit(ErrorCode.CLIENT_ERROR);
}
break;
default:
// Single '-' is allowed (stdin indicator), but other dash-prefixed args are options
if (arg.startsWith('-') && arg !== '-') {
console.error(formatCliError(unknownOptionError(arg)));
process.exit(ErrorCode.CLIENT_ERROR);
}
positional.push(arg);
}
}
// No positional args = list all servers
if (positional.length === 0) {
result.command = 'list';
return result;
}
const firstArg = positional[0];
// =========================================================================
// Explicit subcommand routing
// =========================================================================
if (firstArg === 'info') {
result.command = 'info';
const remaining = positional.slice(1);
const { server, tool } = parseServerTool(remaining);
// info requires a server argument - show available servers in error
if (!server) {
// Try to load config synchronously to show available servers
let availableServers: string[] = [];
const configPaths = [
result.configPath,
process.env.MCP_CONFIG_PATH,
'./mcp_servers.json',
`${process.env.HOME}/.mcp_servers.json`,
`${process.env.HOME}/.config/mcp/mcp_servers.json`,
].filter(Boolean) as string[];
const fs = require('node:fs');
for (const cfgPath of configPaths) {
try {
const content = fs.readFileSync(cfgPath, 'utf-8');
const config = JSON.parse(content);
if (config.mcpServers) {
availableServers = Object.keys(config.mcpServers);
break;
}
} catch {
// Try next path
}
}
const serverList =
availableServers.length > 0
? availableServers.join(', ')
: '(none found)';
console.error(
'Error [MISSING_ARGUMENT]: Missing required argument for info: server',
);
console.error(` Available servers: ${serverList}`);
console.error(
` Suggestion: Use 'mcp-cli info <server>' to see server details, or just 'mcp-cli' to list all`,
);
process.exit(ErrorCode.CLIENT_ERROR);
}
result.server = server;
result.tool = tool;
return result;
}
if (firstArg === 'grep') {
result.command = 'grep';
result.pattern = positional[1];
if (!result.pattern) {
console.error(formatCliError(missingArgumentError('grep', 'pattern')));
process.exit(ErrorCode.CLIENT_ERROR);
}
if (positional.length > 2) {
console.error(
formatCliError(tooManyArgumentsError('grep', positional.length - 1, 1)),
);
process.exit(ErrorCode.CLIENT_ERROR);
}
return result;
}
if (firstArg === 'call') {
result.command = 'call';
const remaining = positional.slice(1);
if (remaining.length === 0) {
console.error(
formatCliError(missingArgumentError('call', 'server and tool')),
);
process.exit(ErrorCode.CLIENT_ERROR);
}
// Parse server/tool from remaining args
const { server, tool } = parseServerTool(remaining);
result.server = server;
if (!tool) {
// Check if it was slash format without tool
if (remaining[0].includes('/') && !remaining[0].split('/')[1]) {
console.error(formatCliError(missingArgumentError('call', 'tool')));
process.exit(ErrorCode.CLIENT_ERROR);
}
// Space format with only server
if (remaining.length < 2) {
console.error(formatCliError(missingArgumentError('call', 'tool')));
process.exit(ErrorCode.CLIENT_ERROR);
}
}
result.tool = tool;
// Determine where args start
let argsStartIndex: number;
if (remaining[0].includes('/')) {
// slash format: call server/tool '{}' → args at index 1
argsStartIndex = 1;
} else {
// space format: call server tool '{}' → args at index 2
argsStartIndex = 2;
}
// Collect remaining args as JSON (support '-' for stdin)
const jsonArgs = remaining.slice(argsStartIndex);
if (jsonArgs.length > 0) {
const argsValue = jsonArgs.join(' ');
result.args = argsValue === '-' ? undefined : argsValue;
}
return result;
}
// =========================================================================
// Check for unknown subcommand (common aliases)
// =========================================================================
if (isPossibleSubcommand(firstArg)) {
console.error(formatCliError(unknownSubcommandError(firstArg)));
process.exit(ErrorCode.CLIENT_ERROR);
}
// =========================================================================
// Slash format without subcommand → error (require explicit subcommand)
// =========================================================================
if (firstArg.includes('/')) {
const parts = firstArg.split('/');
const serverName = parts[0];
const toolName = parts[1] || '';
const hasArgs = positional.length > 1;
console.error(
formatCliError(ambiguousCommandError(serverName, toolName, hasArgs)),
);
process.exit(ErrorCode.CLIENT_ERROR);
}
// =========================================================================
// Ambiguous command detection: server tool without subcommand
// =========================================================================
if (positional.length >= 2) {
const serverName = positional[0];
const possibleTool = positional[1];
// Check if second arg looks like a tool name (not JSON)
const looksLikeJson =
possibleTool.startsWith('{') || possibleTool.startsWith('[');
const looksLikeToolName = /^[a-zA-Z_][a-zA-Z0-9_-]*$/.test(possibleTool);
if (!looksLikeJson && looksLikeToolName) {
const hasArgs = positional.length > 2;
console.error(
formatCliError(
ambiguousCommandError(serverName, possibleTool, hasArgs),
),
);
process.exit(ErrorCode.CLIENT_ERROR);
}
}
// =========================================================================
// Default: single server name → info
// =========================================================================
result.command = 'info';
result.server = firstArg;
return result;
}
/**
* Print help message
*/
function printHelp(): void {
console.log(`
mcp-cli v${VERSION} - A lightweight CLI for MCP servers
Usage:
mcp-cli [options] List all servers and tools
mcp-cli [options] info <server> Show server details
mcp-cli [options] info <server> <tool> Show tool schema
mcp-cli [options] grep <pattern> Search tools by glob pattern
mcp-cli [options] call <server> <tool> Call tool (reads JSON from stdin if no args)
mcp-cli [options] call <server> <tool> <json> Call tool with JSON arguments
Formats (both work):
mcp-cli info server tool Space-separated
mcp-cli info server/tool Slash-separated
mcp-cli call server tool '{}' Space-separated
mcp-cli call server/tool '{}' Slash-separated
Options:
-h, --help Show this help message
-v, --version Show version number
-d, --with-descriptions Include tool descriptions
-c, --config <path> Path to mcp_servers.json config file
Output:
mcp-cli/info/grep Human-readable text to stdout
call Raw JSON to stdout (for piping)
Errors Always to stderr
Examples:
mcp-cli # List all servers
mcp-cli -d # List with descriptions
mcp-cli grep "*file*" # Search for file tools
mcp-cli info filesystem # Show server tools
mcp-cli info filesystem read_file # Show tool schema
mcp-cli call filesystem read_file '{}' # Call tool
cat input.json | mcp-cli call server tool # Read from stdin (no '-' needed)
Environment Variables:
MCP_NO_DAEMON=1 Disable connection caching (force fresh connections)
MCP_DAEMON_TIMEOUT=N Set daemon idle timeout in seconds (default: 60)
Config File:
The CLI looks for mcp_servers.json in:
1. Path specified by -c/--config
2. Path specified by MCP_CONFIG_PATH
3. ./mcp_servers.json (current directory)
4. ~/.mcp_servers.json
5. ~/.config/mcp/mcp_servers.json
`);
}
/**
* Build target string from server and tool
*/
function buildTarget(server?: string, tool?: string): string {
if (!server) return '';
if (!tool) return server;
return `${server}/${tool}`;
}
/**
* Main entry point
*/
async function main(): Promise<void> {
const args = parseArgs(process.argv.slice(2));
switch (args.command) {
case 'help':
printHelp();
break;
case 'version':
console.log(`mcp-cli v${VERSION}`);
break;
case 'list':
await listCommand({
withDescriptions: args.withDescriptions,
configPath: args.configPath,
});
break;
case 'info':
// info always has a server (validated in parseArgs)
await infoCommand({
target: buildTarget(args.server, args.tool),
withDescriptions: args.withDescriptions,
configPath: args.configPath,
});
break;
case 'grep':
await grepCommand({
pattern: args.pattern ?? '',
withDescriptions: args.withDescriptions,
configPath: args.configPath,
});
break;
case 'call':
await callCommand({
target: buildTarget(args.server, args.tool),
args: args.args,
configPath: args.configPath,
});
break;
}
}
// Handle graceful shutdown on SIGINT/SIGTERM
process.on('SIGINT', () => {
process.exit(130); // 128 + SIGINT(2)
});
process.on('SIGTERM', () => {
process.exit(143); // 128 + SIGTERM(15)
});
// Run
main()
.then(() => {
// Use setImmediate to let stdout flush before exiting
setImmediate(() => process.exit(0));
})
.catch((error) => {
// Error message already formatted by command handlers
console.error(error.message);
setImmediate(() => process.exit(ErrorCode.CLIENT_ERROR));
});