@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,232 @@
1
+ #!/usr/bin/env node
2
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
3
+ import { Command } from "commander";
4
+ import { callTool, connect, disconnect, getPrompt, listPrompts, listResources, listResourceTemplates, listTools, readResource, setLoggingLevel, validLogLevels, } from "./client/index.js";
5
+ import { handleError } from "./error-handler.js";
6
+ import { createTransport } from "./transport.js";
7
+ import { awaitableLog } from "./utils/awaitable-log.js";
8
+ import packageJson from "../package.json" with { type: "json" };
9
+ function createTransportOptions(target, transport, headers) {
10
+ if (target.length === 0) {
11
+ throw new Error("Target is required. Specify a URL or a command to execute.");
12
+ }
13
+ const [command, ...commandArgs] = target;
14
+ if (!command) {
15
+ throw new Error("Command is required.");
16
+ }
17
+ const isUrl = command.startsWith("http://") || command.startsWith("https://");
18
+ if (isUrl && commandArgs.length > 0) {
19
+ throw new Error("Arguments cannot be passed to a URL-based MCP server.");
20
+ }
21
+ let transportType;
22
+ if (transport) {
23
+ if (!isUrl && transport !== "stdio") {
24
+ throw new Error("Only stdio transport can be used with local commands.");
25
+ }
26
+ if (isUrl && transport === "stdio") {
27
+ throw new Error("stdio transport cannot be used with URLs.");
28
+ }
29
+ transportType = transport;
30
+ }
31
+ else if (isUrl) {
32
+ const url = new URL(command);
33
+ if (url.pathname.endsWith("/mcp")) {
34
+ transportType = "http";
35
+ }
36
+ else if (url.pathname.endsWith("/sse")) {
37
+ transportType = "sse";
38
+ }
39
+ else {
40
+ transportType = "sse";
41
+ }
42
+ }
43
+ else {
44
+ transportType = "stdio";
45
+ }
46
+ return {
47
+ transportType,
48
+ command: isUrl ? undefined : command,
49
+ args: isUrl ? undefined : commandArgs,
50
+ url: isUrl ? command : undefined,
51
+ headers,
52
+ };
53
+ }
54
+ async function callMethod(args) {
55
+ const transportOptions = createTransportOptions(args.target, args.transport, args.headers);
56
+ const transport = createTransport(transportOptions);
57
+ const [, name = packageJson.name] = packageJson.name.split("/");
58
+ const version = packageJson.version;
59
+ const clientIdentity = { name, version };
60
+ const client = new Client(clientIdentity);
61
+ try {
62
+ await connect(client, transport);
63
+ let result;
64
+ // Tools methods
65
+ if (args.method === "tools/list") {
66
+ result = await listTools(client);
67
+ }
68
+ else if (args.method === "tools/call") {
69
+ if (!args.toolName) {
70
+ throw new Error("Tool name is required for tools/call method. Use --tool-name to specify the tool name.");
71
+ }
72
+ result = await callTool(client, args.toolName, args.toolArg || {});
73
+ }
74
+ // Resources methods
75
+ else if (args.method === "resources/list") {
76
+ result = await listResources(client);
77
+ }
78
+ else if (args.method === "resources/read") {
79
+ if (!args.uri) {
80
+ throw new Error("URI is required for resources/read method. Use --uri to specify the resource URI.");
81
+ }
82
+ result = await readResource(client, args.uri);
83
+ }
84
+ else if (args.method === "resources/templates/list") {
85
+ result = await listResourceTemplates(client);
86
+ }
87
+ // Prompts methods
88
+ else if (args.method === "prompts/list") {
89
+ result = await listPrompts(client);
90
+ }
91
+ else if (args.method === "prompts/get") {
92
+ if (!args.promptName) {
93
+ throw new Error("Prompt name is required for prompts/get method. Use --prompt-name to specify the prompt name.");
94
+ }
95
+ result = await getPrompt(client, args.promptName, args.promptArgs || {});
96
+ }
97
+ // Logging methods
98
+ else if (args.method === "logging/setLevel") {
99
+ if (!args.logLevel) {
100
+ throw new Error("Log level is required for logging/setLevel method. Use --log-level to specify the log level.");
101
+ }
102
+ result = await setLoggingLevel(client, args.logLevel);
103
+ }
104
+ else {
105
+ throw new Error(`Unsupported method: ${args.method}. Supported methods include: tools/list, tools/call, resources/list, resources/read, resources/templates/list, prompts/list, prompts/get, logging/setLevel`);
106
+ }
107
+ await awaitableLog(JSON.stringify(result, null, 2));
108
+ }
109
+ finally {
110
+ try {
111
+ await disconnect(transport);
112
+ }
113
+ catch (disconnectError) {
114
+ throw disconnectError;
115
+ }
116
+ }
117
+ }
118
+ function parseKeyValuePair(value, previous = {}) {
119
+ const parts = value.split("=");
120
+ const key = parts[0];
121
+ const val = parts.slice(1).join("=");
122
+ if (val === undefined || val === "") {
123
+ throw new Error(`Invalid parameter format: ${value}. Use key=value format.`);
124
+ }
125
+ // Try to parse as JSON first
126
+ let parsedValue;
127
+ try {
128
+ parsedValue = JSON.parse(val);
129
+ }
130
+ catch {
131
+ // If JSON parsing fails, keep as string
132
+ parsedValue = val;
133
+ }
134
+ return { ...previous, [key]: parsedValue };
135
+ }
136
+ function parseHeaderPair(value, previous = {}) {
137
+ const colonIndex = value.indexOf(":");
138
+ if (colonIndex === -1) {
139
+ throw new Error(`Invalid header format: ${value}. Use "HeaderName: Value" format.`);
140
+ }
141
+ const key = value.slice(0, colonIndex).trim();
142
+ const val = value.slice(colonIndex + 1).trim();
143
+ if (key === "" || val === "") {
144
+ throw new Error(`Invalid header format: ${value}. Use "HeaderName: Value" format.`);
145
+ }
146
+ return { ...previous, [key]: val };
147
+ }
148
+ function parseArgs() {
149
+ const program = new Command();
150
+ // Find if there's a -- in the arguments and split them
151
+ const argSeparatorIndex = process.argv.indexOf("--");
152
+ let preArgs = process.argv;
153
+ let postArgs = [];
154
+ if (argSeparatorIndex !== -1) {
155
+ preArgs = process.argv.slice(0, argSeparatorIndex);
156
+ postArgs = process.argv.slice(argSeparatorIndex + 1);
157
+ }
158
+ program
159
+ .name("inspector-cli")
160
+ .allowUnknownOption()
161
+ .argument("<target...>", "Command and arguments or URL of the MCP server")
162
+ //
163
+ // Method selection
164
+ //
165
+ .option("--method <method>", "Method to invoke")
166
+ //
167
+ // Tool-related options
168
+ //
169
+ .option("--tool-name <toolName>", "Tool name (for tools/call method)")
170
+ .option("--tool-arg <pairs...>", "Tool argument as key=value pair", parseKeyValuePair, {})
171
+ //
172
+ // Resource-related options
173
+ //
174
+ .option("--uri <uri>", "URI of the resource (for resources/read method)")
175
+ //
176
+ // Prompt-related options
177
+ //
178
+ .option("--prompt-name <promptName>", "Name of the prompt (for prompts/get method)")
179
+ .option("--prompt-args <pairs...>", "Prompt arguments as key=value pairs", parseKeyValuePair, {})
180
+ //
181
+ // Logging options
182
+ //
183
+ .option("--log-level <level>", "Logging level (for logging/setLevel method)", (value) => {
184
+ if (!validLogLevels.includes(value)) {
185
+ throw new Error(`Invalid log level: ${value}. Valid levels are: ${validLogLevels.join(", ")}`);
186
+ }
187
+ return value;
188
+ })
189
+ //
190
+ // Transport options
191
+ //
192
+ .option("--transport <type>", "Transport type (sse, http, or stdio). Auto-detected from URL: /mcp → http, /sse → sse, commands → stdio", (value) => {
193
+ const validTransports = ["sse", "http", "stdio"];
194
+ if (!validTransports.includes(value)) {
195
+ throw new Error(`Invalid transport type: ${value}. Valid types are: ${validTransports.join(", ")}`);
196
+ }
197
+ return value;
198
+ })
199
+ //
200
+ // HTTP headers
201
+ //
202
+ .option("--header <headers...>", 'HTTP headers as "HeaderName: Value" pairs (for HTTP/SSE transports)', parseHeaderPair, {});
203
+ // Parse only the arguments before --
204
+ program.parse(preArgs);
205
+ const options = program.opts();
206
+ let remainingArgs = program.args;
207
+ // Add back any arguments that came after --
208
+ const finalArgs = [...remainingArgs, ...postArgs];
209
+ if (!options.method) {
210
+ throw new Error("Method is required. Use --method to specify the method to invoke.");
211
+ }
212
+ return {
213
+ target: finalArgs,
214
+ ...options,
215
+ headers: options.header, // commander.js uses 'header' field, map to 'headers'
216
+ };
217
+ }
218
+ async function main() {
219
+ process.on("uncaughtException", (error) => {
220
+ handleError(error);
221
+ });
222
+ try {
223
+ const args = parseArgs();
224
+ await callMethod(args);
225
+ // Explicitly exit to ensure process terminates in CI
226
+ process.exit(0);
227
+ }
228
+ catch (error) {
229
+ handleError(error);
230
+ }
231
+ }
232
+ main();
@@ -0,0 +1,65 @@
1
+ import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
2
+ import { getDefaultEnvironment, StdioClientTransport, } from "@modelcontextprotocol/sdk/client/stdio.js";
3
+ import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
4
+ import { findActualExecutable } from "spawn-rx";
5
+ function createStdioTransport(options) {
6
+ let args = [];
7
+ if (options.args !== undefined) {
8
+ args = options.args;
9
+ }
10
+ const processEnv = {};
11
+ for (const [key, value] of Object.entries(process.env)) {
12
+ if (value !== undefined) {
13
+ processEnv[key] = value;
14
+ }
15
+ }
16
+ const defaultEnv = getDefaultEnvironment();
17
+ const env = {
18
+ ...defaultEnv,
19
+ ...processEnv,
20
+ };
21
+ const { cmd: actualCommand, args: actualArgs } = findActualExecutable(options.command ?? "", args);
22
+ return new StdioClientTransport({
23
+ command: actualCommand,
24
+ args: actualArgs,
25
+ env,
26
+ stderr: "pipe",
27
+ });
28
+ }
29
+ export function createTransport(options) {
30
+ const { transportType } = options;
31
+ try {
32
+ if (transportType === "stdio") {
33
+ return createStdioTransport(options);
34
+ }
35
+ // If not STDIO, then it must be either SSE or HTTP.
36
+ if (!options.url) {
37
+ throw new Error("URL must be provided for SSE or HTTP transport types.");
38
+ }
39
+ const url = new URL(options.url);
40
+ if (transportType === "sse") {
41
+ const transportOptions = options.headers
42
+ ? {
43
+ requestInit: {
44
+ headers: options.headers,
45
+ },
46
+ }
47
+ : undefined;
48
+ return new SSEClientTransport(url, transportOptions);
49
+ }
50
+ if (transportType === "http") {
51
+ const transportOptions = options.headers
52
+ ? {
53
+ requestInit: {
54
+ headers: options.headers,
55
+ },
56
+ }
57
+ : undefined;
58
+ return new StreamableHTTPClientTransport(url, transportOptions);
59
+ }
60
+ throw new Error(`Unsupported transport type: ${transportType}`);
61
+ }
62
+ catch (error) {
63
+ throw new Error(`Failed to create transport: ${error instanceof Error ? error.message : String(error)}`);
64
+ }
65
+ }
@@ -0,0 +1,7 @@
1
+ export function awaitableLog(logValue) {
2
+ return new Promise((resolve) => {
3
+ process.stdout.write(logValue, () => {
4
+ resolve();
5
+ });
6
+ });
7
+ }
@@ -0,0 +1,50 @@
1
+ # React + TypeScript + Vite
2
+
3
+ This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
4
+
5
+ Currently, two official plugins are available:
6
+
7
+ - [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
8
+ - [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
9
+
10
+ ## Expanding the ESLint configuration
11
+
12
+ If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:
13
+
14
+ - Configure the top-level `parserOptions` property like this:
15
+
16
+ ```js
17
+ export default tseslint.config({
18
+ languageOptions: {
19
+ // other options...
20
+ parserOptions: {
21
+ project: ["./tsconfig.node.json", "./tsconfig.app.json"],
22
+ tsconfigRootDir: import.meta.dirname,
23
+ },
24
+ },
25
+ });
26
+ ```
27
+
28
+ - Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked`
29
+ - Optionally add `...tseslint.configs.stylisticTypeChecked`
30
+ - Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and update the config:
31
+
32
+ ```js
33
+ // eslint.config.js
34
+ import react from "eslint-plugin-react";
35
+
36
+ export default tseslint.config({
37
+ // Set the react version
38
+ settings: { react: { version: "18.3" } },
39
+ plugins: {
40
+ // Add the react plugin
41
+ react,
42
+ },
43
+ rules: {
44
+ // other rules...
45
+ // Enable its recommended rules
46
+ ...react.configs.recommended.rules,
47
+ ...react.configs["jsx-runtime"].rules,
48
+ },
49
+ });
50
+ ```
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env node
2
+
3
+ import open from "open";
4
+ import { join, dirname } from "path";
5
+ import { fileURLToPath } from "url";
6
+ import handler from "serve-handler";
7
+ import http from "http";
8
+
9
+ const __dirname = dirname(fileURLToPath(import.meta.url));
10
+ const distPath = join(__dirname, "../dist");
11
+
12
+ const server = http.createServer((request, response) => {
13
+ const handlerOptions = {
14
+ public: distPath,
15
+ rewrites: [{ source: "/**", destination: "/index.html" }],
16
+ headers: [
17
+ {
18
+ // Ensure index.html is never cached
19
+ source: "index.html",
20
+ headers: [
21
+ {
22
+ key: "Cache-Control",
23
+ value: "no-cache, no-store, max-age=0",
24
+ },
25
+ ],
26
+ },
27
+ {
28
+ // Allow long-term caching for hashed assets
29
+ source: "assets/**",
30
+ headers: [
31
+ {
32
+ key: "Cache-Control",
33
+ value: "public, max-age=31536000, immutable",
34
+ },
35
+ ],
36
+ },
37
+ ],
38
+ };
39
+
40
+ return handler(request, response, handlerOptions);
41
+ });
42
+
43
+ const port = parseInt(process.env.CLIENT_PORT || "6274", 10);
44
+ const host = process.env.HOST || "localhost";
45
+ server.on("listening", () => {
46
+ const url = process.env.INSPECTOR_URL || `http://${host}:${port}`;
47
+ console.log(`\nšŸš€ MCP Inspector is up and running at:\n ${url}\n`);
48
+ if (process.env.MCP_AUTO_OPEN_ENABLED !== "false") {
49
+ console.log(`🌐 Opening browser...`);
50
+ open(url);
51
+ }
52
+ });
53
+ server.on("error", (err) => {
54
+ if (err.message.includes(`EADDRINUSE`)) {
55
+ console.error(
56
+ `āŒ MCP Inspector PORT IS IN USE at http://${host}:${port} āŒ `,
57
+ );
58
+ } else {
59
+ throw err;
60
+ }
61
+ });
62
+ server.listen(port, host);