@bryan-thompson/inspector-assessment 1.0.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.
@@ -0,0 +1,277 @@
1
+ #!/usr/bin/env node
2
+ import { Command } from "commander";
3
+ import fs from "node:fs";
4
+ import path from "node:path";
5
+ import { dirname, resolve } from "path";
6
+ import { spawnPromise } from "spawn-rx";
7
+ import { fileURLToPath } from "url";
8
+ const __dirname = dirname(fileURLToPath(import.meta.url));
9
+ function handleError(error) {
10
+ let message;
11
+ if (error instanceof Error) {
12
+ message = error.message;
13
+ }
14
+ else if (typeof error === "string") {
15
+ message = error;
16
+ }
17
+ else {
18
+ message = "Unknown error";
19
+ }
20
+ console.error(message);
21
+ process.exit(1);
22
+ }
23
+ function delay(ms) {
24
+ return new Promise((resolve) => setTimeout(resolve, ms, true));
25
+ }
26
+ async function runWebClient(args) {
27
+ // Path to the client entry point
28
+ const inspectorClientPath = resolve(__dirname, "../../", "client", "bin", "start.js");
29
+ const abort = new AbortController();
30
+ let cancelled = false;
31
+ process.on("SIGINT", () => {
32
+ cancelled = true;
33
+ abort.abort();
34
+ });
35
+ // Build arguments to pass to start.js
36
+ const startArgs = [];
37
+ // Pass environment variables
38
+ for (const [key, value] of Object.entries(args.envArgs)) {
39
+ startArgs.push("-e", `${key}=${value}`);
40
+ }
41
+ // Pass transport type if specified
42
+ if (args.transport) {
43
+ startArgs.push("--transport", args.transport);
44
+ }
45
+ // Pass server URL if specified
46
+ if (args.serverUrl) {
47
+ startArgs.push("--server-url", args.serverUrl);
48
+ }
49
+ // Pass command and args (using -- to separate them)
50
+ if (args.command) {
51
+ startArgs.push("--", args.command, ...args.args);
52
+ }
53
+ try {
54
+ await spawnPromise("node", [inspectorClientPath, ...startArgs], {
55
+ signal: abort.signal,
56
+ echoOutput: true,
57
+ // pipe the stdout through here, prevents issues with buffering and
58
+ // dropping the end of console.out after 8192 chars due to node
59
+ // closing the stdout pipe before the output has finished flushing
60
+ stdio: "inherit",
61
+ });
62
+ }
63
+ catch (e) {
64
+ if (!cancelled || process.env.DEBUG)
65
+ throw e;
66
+ }
67
+ }
68
+ async function runCli(args) {
69
+ const projectRoot = resolve(__dirname, "..");
70
+ const cliPath = resolve(projectRoot, "build", "index.js");
71
+ const abort = new AbortController();
72
+ let cancelled = false;
73
+ process.on("SIGINT", () => {
74
+ cancelled = true;
75
+ abort.abort();
76
+ });
77
+ try {
78
+ // Build CLI arguments
79
+ const cliArgs = [cliPath];
80
+ // Add target URL/command first
81
+ cliArgs.push(args.command, ...args.args);
82
+ // Add transport flag if specified
83
+ if (args.transport && args.transport !== "stdio") {
84
+ // Convert streamable-http back to http for CLI mode
85
+ const cliTransport = args.transport === "streamable-http" ? "http" : args.transport;
86
+ cliArgs.push("--transport", cliTransport);
87
+ }
88
+ // Add headers if specified
89
+ if (args.headers) {
90
+ for (const [key, value] of Object.entries(args.headers)) {
91
+ cliArgs.push("--header", `${key}: ${value}`);
92
+ }
93
+ }
94
+ await spawnPromise("node", cliArgs, {
95
+ env: { ...process.env, ...args.envArgs },
96
+ signal: abort.signal,
97
+ echoOutput: true,
98
+ // pipe the stdout through here, prevents issues with buffering and
99
+ // dropping the end of console.out after 8192 chars due to node
100
+ // closing the stdout pipe before the output has finished flushing
101
+ stdio: "inherit",
102
+ });
103
+ }
104
+ catch (e) {
105
+ if (!cancelled || process.env.DEBUG) {
106
+ throw e;
107
+ }
108
+ }
109
+ }
110
+ function loadConfigFile(configPath, serverName) {
111
+ try {
112
+ const resolvedConfigPath = path.isAbsolute(configPath)
113
+ ? configPath
114
+ : path.resolve(process.cwd(), configPath);
115
+ if (!fs.existsSync(resolvedConfigPath)) {
116
+ throw new Error(`Config file not found: ${resolvedConfigPath}`);
117
+ }
118
+ const configContent = fs.readFileSync(resolvedConfigPath, "utf8");
119
+ const parsedConfig = JSON.parse(configContent);
120
+ if (!parsedConfig.mcpServers || !parsedConfig.mcpServers[serverName]) {
121
+ const availableServers = Object.keys(parsedConfig.mcpServers || {}).join(", ");
122
+ throw new Error(`Server '${serverName}' not found in config file. Available servers: ${availableServers}`);
123
+ }
124
+ const serverConfig = parsedConfig.mcpServers[serverName];
125
+ return serverConfig;
126
+ }
127
+ catch (err) {
128
+ if (err instanceof SyntaxError) {
129
+ throw new Error(`Invalid JSON in config file: ${err.message}`);
130
+ }
131
+ throw err;
132
+ }
133
+ }
134
+ function parseKeyValuePair(value, previous = {}) {
135
+ const parts = value.split("=");
136
+ const key = parts[0];
137
+ const val = parts.slice(1).join("=");
138
+ if (val === undefined || val === "") {
139
+ throw new Error(`Invalid parameter format: ${value}. Use key=value format.`);
140
+ }
141
+ return { ...previous, [key]: val };
142
+ }
143
+ function parseHeaderPair(value, previous = {}) {
144
+ const colonIndex = value.indexOf(":");
145
+ if (colonIndex === -1) {
146
+ throw new Error(`Invalid header format: ${value}. Use "HeaderName: Value" format.`);
147
+ }
148
+ const key = value.slice(0, colonIndex).trim();
149
+ const val = value.slice(colonIndex + 1).trim();
150
+ if (key === "" || val === "") {
151
+ throw new Error(`Invalid header format: ${value}. Use "HeaderName: Value" format.`);
152
+ }
153
+ return { ...previous, [key]: val };
154
+ }
155
+ function parseArgs() {
156
+ const program = new Command();
157
+ const argSeparatorIndex = process.argv.indexOf("--");
158
+ let preArgs = process.argv;
159
+ let postArgs = [];
160
+ if (argSeparatorIndex !== -1) {
161
+ preArgs = process.argv.slice(0, argSeparatorIndex);
162
+ postArgs = process.argv.slice(argSeparatorIndex + 1);
163
+ }
164
+ program
165
+ .name("inspector-bin")
166
+ .allowExcessArguments()
167
+ .allowUnknownOption()
168
+ .option("-e <env>", "environment variables in KEY=VALUE format", parseKeyValuePair, {})
169
+ .option("--config <path>", "config file path")
170
+ .option("--server <n>", "server name from config file")
171
+ .option("--cli", "enable CLI mode")
172
+ .option("--transport <type>", "transport type (stdio, sse, http)")
173
+ .option("--server-url <url>", "server URL for SSE/HTTP transport")
174
+ .option("--header <headers...>", 'HTTP headers as "HeaderName: Value" pairs (for HTTP/SSE transports)', parseHeaderPair, {});
175
+ // Parse only the arguments before --
176
+ program.parse(preArgs);
177
+ const options = program.opts();
178
+ const remainingArgs = program.args;
179
+ // Add back any arguments that came after --
180
+ const finalArgs = [...remainingArgs, ...postArgs];
181
+ // Validate config and server options
182
+ if (!options.config && options.server) {
183
+ throw new Error("--server requires --config to be specified");
184
+ }
185
+ // If config is provided without server, try to auto-select
186
+ if (options.config && !options.server) {
187
+ const configContent = fs.readFileSync(path.isAbsolute(options.config)
188
+ ? options.config
189
+ : path.resolve(process.cwd(), options.config), "utf8");
190
+ const parsedConfig = JSON.parse(configContent);
191
+ const servers = Object.keys(parsedConfig.mcpServers || {});
192
+ if (servers.length === 1) {
193
+ // Use the only server if there's just one
194
+ options.server = servers[0];
195
+ }
196
+ else if (servers.length === 0) {
197
+ throw new Error("No servers found in config file");
198
+ }
199
+ else {
200
+ // Multiple servers, require explicit selection
201
+ throw new Error(`Multiple servers found in config file. Please specify one with --server.\nAvailable servers: ${servers.join(", ")}`);
202
+ }
203
+ }
204
+ // If config file is specified, load and use the options from the file. We must merge the args
205
+ // from the command line and the file together, or we will miss the method options (--method,
206
+ // etc.)
207
+ if (options.config && options.server) {
208
+ const config = loadConfigFile(options.config, options.server);
209
+ if (config.type === "stdio") {
210
+ return {
211
+ command: config.command,
212
+ args: [...(config.args || []), ...finalArgs],
213
+ envArgs: { ...(config.env || {}), ...(options.e || {}) },
214
+ cli: options.cli || false,
215
+ transport: "stdio",
216
+ headers: options.header,
217
+ };
218
+ }
219
+ else if (config.type === "sse" || config.type === "streamable-http") {
220
+ return {
221
+ command: config.url,
222
+ args: finalArgs,
223
+ envArgs: options.e || {},
224
+ cli: options.cli || false,
225
+ transport: config.type,
226
+ serverUrl: config.url,
227
+ headers: options.header,
228
+ };
229
+ }
230
+ else {
231
+ // Backwards compatibility: if no type field, assume stdio
232
+ return {
233
+ command: config.command || "",
234
+ args: [...(config.args || []), ...finalArgs],
235
+ envArgs: { ...(config.env || {}), ...(options.e || {}) },
236
+ cli: options.cli || false,
237
+ transport: "stdio",
238
+ headers: options.header,
239
+ };
240
+ }
241
+ }
242
+ // Otherwise use command line arguments
243
+ const command = finalArgs[0] || "";
244
+ const args = finalArgs.slice(1);
245
+ // Map "http" shorthand to "streamable-http"
246
+ let transport = options.transport;
247
+ if (transport === "http") {
248
+ transport = "streamable-http";
249
+ }
250
+ return {
251
+ command,
252
+ args,
253
+ envArgs: options.e || {},
254
+ cli: options.cli || false,
255
+ transport: transport,
256
+ serverUrl: options.serverUrl,
257
+ headers: options.header,
258
+ };
259
+ }
260
+ async function main() {
261
+ process.on("uncaughtException", (error) => {
262
+ handleError(error);
263
+ });
264
+ try {
265
+ const args = parseArgs();
266
+ if (args.cli) {
267
+ await runCli(args);
268
+ }
269
+ else {
270
+ await runWebClient(args);
271
+ }
272
+ }
273
+ catch (error) {
274
+ handleError(error);
275
+ }
276
+ }
277
+ main();
@@ -0,0 +1,38 @@
1
+ export const validLogLevels = [
2
+ "trace",
3
+ "debug",
4
+ "info",
5
+ "warn",
6
+ "error",
7
+ ];
8
+ export async function connect(client, transport) {
9
+ try {
10
+ await client.connect(transport);
11
+ if (client.getServerCapabilities()?.logging) {
12
+ // default logging level is undefined in the spec, but the user of the
13
+ // inspector most likely wants debug.
14
+ await client.setLoggingLevel("debug");
15
+ }
16
+ }
17
+ catch (error) {
18
+ throw new Error(`Failed to connect to MCP server: ${error instanceof Error ? error.message : String(error)}`);
19
+ }
20
+ }
21
+ export async function disconnect(transport) {
22
+ try {
23
+ await transport.close();
24
+ }
25
+ catch (error) {
26
+ throw new Error(`Failed to disconnect from MCP server: ${error instanceof Error ? error.message : String(error)}`);
27
+ }
28
+ }
29
+ // Set logging level
30
+ export async function setLoggingLevel(client, level) {
31
+ try {
32
+ const response = await client.setLoggingLevel(level);
33
+ return response;
34
+ }
35
+ catch (error) {
36
+ throw new Error(`Failed to set logging level: ${error instanceof Error ? error.message : String(error)}`);
37
+ }
38
+ }
@@ -0,0 +1,6 @@
1
+ // Re-export everything from the client modules
2
+ export * from "./connection.js";
3
+ export * from "./prompts.js";
4
+ export * from "./resources.js";
5
+ export * from "./tools.js";
6
+ export * from "./types.js";
@@ -0,0 +1,38 @@
1
+ // List available prompts
2
+ export async function listPrompts(client) {
3
+ try {
4
+ const response = await client.listPrompts();
5
+ return response;
6
+ }
7
+ catch (error) {
8
+ throw new Error(`Failed to list prompts: ${error instanceof Error ? error.message : String(error)}`);
9
+ }
10
+ }
11
+ // Get a prompt
12
+ export async function getPrompt(client, name, args) {
13
+ try {
14
+ // Convert all arguments to strings for prompt arguments
15
+ const stringArgs = {};
16
+ if (args) {
17
+ for (const [key, value] of Object.entries(args)) {
18
+ if (typeof value === "string") {
19
+ stringArgs[key] = value;
20
+ }
21
+ else if (value === null || value === undefined) {
22
+ stringArgs[key] = String(value);
23
+ }
24
+ else {
25
+ stringArgs[key] = JSON.stringify(value);
26
+ }
27
+ }
28
+ }
29
+ const response = await client.getPrompt({
30
+ name,
31
+ arguments: stringArgs,
32
+ });
33
+ return response;
34
+ }
35
+ catch (error) {
36
+ throw new Error(`Failed to get prompt: ${error instanceof Error ? error.message : String(error)}`);
37
+ }
38
+ }
@@ -0,0 +1,30 @@
1
+ // List available resources
2
+ export async function listResources(client) {
3
+ try {
4
+ const response = await client.listResources();
5
+ return response;
6
+ }
7
+ catch (error) {
8
+ throw new Error(`Failed to list resources: ${error instanceof Error ? error.message : String(error)}`);
9
+ }
10
+ }
11
+ // Read a resource
12
+ export async function readResource(client, uri) {
13
+ try {
14
+ const response = await client.readResource({ uri });
15
+ return response;
16
+ }
17
+ catch (error) {
18
+ throw new Error(`Failed to read resource ${uri}: ${error instanceof Error ? error.message : String(error)}`);
19
+ }
20
+ }
21
+ // List resource templates
22
+ export async function listResourceTemplates(client) {
23
+ try {
24
+ const response = await client.listResourceTemplates();
25
+ return response;
26
+ }
27
+ catch (error) {
28
+ throw new Error(`Failed to list resource templates: ${error instanceof Error ? error.message : String(error)}`);
29
+ }
30
+ }
@@ -0,0 +1,74 @@
1
+ export async function listTools(client) {
2
+ try {
3
+ const response = await client.listTools();
4
+ return response;
5
+ }
6
+ catch (error) {
7
+ throw new Error(`Failed to list tools: ${error instanceof Error ? error.message : String(error)}`);
8
+ }
9
+ }
10
+ function convertParameterValue(value, schema) {
11
+ if (!value) {
12
+ return value;
13
+ }
14
+ if (schema.type === "number" || schema.type === "integer") {
15
+ return Number(value);
16
+ }
17
+ if (schema.type === "boolean") {
18
+ return value.toLowerCase() === "true";
19
+ }
20
+ if (schema.type === "object" || schema.type === "array") {
21
+ try {
22
+ return JSON.parse(value);
23
+ }
24
+ catch (error) {
25
+ return value;
26
+ }
27
+ }
28
+ return value;
29
+ }
30
+ function convertParameters(tool, params) {
31
+ const result = {};
32
+ const properties = tool.inputSchema.properties || {};
33
+ for (const [key, value] of Object.entries(params)) {
34
+ const paramSchema = properties[key];
35
+ if (paramSchema) {
36
+ result[key] = convertParameterValue(value, paramSchema);
37
+ }
38
+ else {
39
+ // If no schema is found for this parameter, keep it as string
40
+ result[key] = value;
41
+ }
42
+ }
43
+ return result;
44
+ }
45
+ export async function callTool(client, name, args) {
46
+ try {
47
+ const toolsResponse = await listTools(client);
48
+ const tools = toolsResponse.tools;
49
+ const tool = tools.find((t) => t.name === name);
50
+ let convertedArgs = args;
51
+ if (tool) {
52
+ // Convert parameters based on the tool's schema, but only for string values
53
+ // since we now accept pre-parsed values from the CLI
54
+ const stringArgs = {};
55
+ for (const [key, value] of Object.entries(args)) {
56
+ if (typeof value === "string") {
57
+ stringArgs[key] = value;
58
+ }
59
+ }
60
+ if (Object.keys(stringArgs).length > 0) {
61
+ const convertedStringArgs = convertParameters(tool, stringArgs);
62
+ convertedArgs = { ...args, ...convertedStringArgs };
63
+ }
64
+ }
65
+ const response = await client.callTool({
66
+ name: name,
67
+ arguments: convertedArgs,
68
+ });
69
+ return response;
70
+ }
71
+ catch (error) {
72
+ throw new Error(`Failed to call tool ${name}: ${error instanceof Error ? error.message : String(error)}`);
73
+ }
74
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,18 @@
1
+ function formatError(error) {
2
+ let message;
3
+ if (error instanceof Error) {
4
+ message = error.message;
5
+ }
6
+ else if (typeof error === "string") {
7
+ message = error;
8
+ }
9
+ else {
10
+ message = "Unknown error";
11
+ }
12
+ return message;
13
+ }
14
+ export function handleError(error) {
15
+ const errorMessage = formatError(error);
16
+ console.error(errorMessage);
17
+ process.exit(1);
18
+ }