-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathinfo.ts
More file actions
113 lines (100 loc) · 2.73 KB
/
info.ts
File metadata and controls
113 lines (100 loc) · 2.73 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
/**
* Info command - Show server or tool details
*/
import { type McpConnection, getConnection, safeClose } from '../client.js';
import {
type McpServersConfig,
type ServerConfig,
getServerConfig,
loadConfig,
} from '../config.js';
import {
ErrorCode,
formatCliError,
serverConnectionError,
toolNotFoundError,
} from '../errors.js';
import { formatServerDetails, formatToolSchema } from '../output.js';
export interface InfoOptions {
target: string; // "server" or "server/tool"
withDescriptions: boolean;
configPath?: string;
}
/**
* Parse target into server and optional tool name
*/
function parseTarget(target: string): { server: string; tool?: string } {
const parts = target.split('/');
if (parts.length === 1) {
return { server: parts[0] };
}
return { server: parts[0], tool: parts.slice(1).join('/') };
}
/**
* Execute the info command
*/
export async function infoCommand(options: InfoOptions): Promise<void> {
let config: McpServersConfig;
const { server: serverName, tool: toolName } = parseTarget(options.target);
try {
config = await loadConfig({
explicitPath: options.configPath,
serverNames: [serverName],
});
} catch (error) {
console.error((error as Error).message);
process.exit(ErrorCode.CLIENT_ERROR);
}
let serverConfig: ServerConfig;
try {
serverConfig = getServerConfig(config, serverName);
} catch (error) {
console.error((error as Error).message);
process.exit(ErrorCode.CLIENT_ERROR);
}
let connection: McpConnection;
try {
connection = await getConnection(serverName, serverConfig);
} catch (error) {
console.error(
formatCliError(
serverConnectionError(serverName, (error as Error).message),
),
);
process.exit(ErrorCode.NETWORK_ERROR);
}
try {
if (toolName) {
// Show specific tool schema
const tools = await connection.listTools();
const tool = tools.find((t) => t.name === toolName);
if (!tool) {
const availableTools = tools.map((t) => t.name);
console.error(
formatCliError(
toolNotFoundError(toolName, serverName, availableTools),
),
);
process.exit(ErrorCode.CLIENT_ERROR);
}
// Human-readable output
console.log(formatToolSchema(serverName, tool));
} else {
// Show server details
const tools = await connection.listTools();
const instructions = await connection.getInstructions();
// Human-readable output
console.log(
formatServerDetails(
serverName,
serverConfig,
tools,
options.withDescriptions,
instructions,
),
);
}
} finally {
await safeClose(connection.close);
}
}