|
| 1 | +/** |
| 2 | + * Dev Inspector Plugin |
| 3 | + * |
| 4 | + * Automatically spawns the MCP Inspector and connects it to this server. |
| 5 | + * Used internally by createApp when autoInspector config is enabled. |
| 6 | + * |
| 7 | + * @internal - Not exported publicly, used by createApp when autoInspector is enabled |
| 8 | + */ |
| 9 | + |
| 10 | +import { spawn, type ChildProcess } from "child_process"; |
| 11 | +import type { Plugin } from "../types"; |
| 12 | +import type { AutoInspectorConfig } from "../../types/config"; |
| 13 | + |
| 14 | +/** |
| 15 | + * Resolve auto-inspector config to full config object with defaults |
| 16 | + */ |
| 17 | +function resolveConfig(config: AutoInspectorConfig | true): Required<AutoInspectorConfig> { |
| 18 | + if (config === true) { |
| 19 | + return { |
| 20 | + port: 6274, |
| 21 | + debug: false, |
| 22 | + devOnly: true, |
| 23 | + }; |
| 24 | + } |
| 25 | + return { |
| 26 | + port: config.port ?? 6274, |
| 27 | + debug: config.debug ?? false, |
| 28 | + devOnly: config.devOnly ?? true, |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Create dev inspector plugin with given config |
| 34 | + * |
| 35 | + * @param config - Auto-inspector configuration |
| 36 | + * @param serverRoute - The MCP server route path (e.g., "/mcp") |
| 37 | + * @returns Plugin that spawns inspector on start |
| 38 | + * |
| 39 | + * @internal - Not exported publicly, used by createApp when autoInspector is enabled |
| 40 | + */ |
| 41 | +export function createDevInspectorPlugin( |
| 42 | + config: AutoInspectorConfig | true, |
| 43 | + serverRoute: string |
| 44 | +): Plugin { |
| 45 | + const resolvedConfig = resolveConfig(config); |
| 46 | + |
| 47 | + // Keep inspector process state scoped to this plugin instance |
| 48 | + let inspectorProcess: ChildProcess | null = null; |
| 49 | + |
| 50 | + return { |
| 51 | + name: "dev-inspector", |
| 52 | + version: "1.0.0", |
| 53 | + |
| 54 | + onStart: async (context) => { |
| 55 | + // Skip in production if devOnly is true |
| 56 | + if (resolvedConfig.devOnly && process.env.NODE_ENV === "production") { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // Only works with HTTP transport |
| 61 | + if (context.transport !== "http" || !context.port) { |
| 62 | + // eslint-disable-next-line no-console |
| 63 | + console.warn("[autoInspector] Skipped: only works with HTTP transport"); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + const serverUrl = `http://localhost:${context.port}${serverRoute}`; |
| 68 | + const args = ["--url", serverUrl, "--port", String(resolvedConfig.port)]; |
| 69 | + |
| 70 | + if (resolvedConfig.debug) { |
| 71 | + args.push("--debug"); |
| 72 | + } |
| 73 | + |
| 74 | + // eslint-disable-next-line no-console |
| 75 | + console.log(`[autoInspector] Starting inspector on port ${resolvedConfig.port}`); |
| 76 | + // eslint-disable-next-line no-console |
| 77 | + console.log(`[autoInspector] Connecting to: ${serverUrl}`); |
| 78 | + |
| 79 | + // Spawn mcp-inspector using npx to find the CLI |
| 80 | + // We use npx to ensure we find the installed mcp-inspector binary |
| 81 | + inspectorProcess = spawn("npx", ["mcp-inspector", ...args], { |
| 82 | + stdio: "inherit", |
| 83 | + detached: false, |
| 84 | + shell: true, |
| 85 | + }); |
| 86 | + |
| 87 | + inspectorProcess.on("error", (err) => { |
| 88 | + // eslint-disable-next-line no-console |
| 89 | + console.error(`[autoInspector] Failed to spawn inspector: ${err.message}`); |
| 90 | + }); |
| 91 | + |
| 92 | + inspectorProcess.on("exit", (code) => { |
| 93 | + if (code !== null && code !== 0) { |
| 94 | + // eslint-disable-next-line no-console |
| 95 | + console.warn(`[autoInspector] Inspector exited with code ${code}`); |
| 96 | + } |
| 97 | + inspectorProcess = null; |
| 98 | + }); |
| 99 | + }, |
| 100 | + |
| 101 | + onShutdown: async () => { |
| 102 | + if (inspectorProcess) { |
| 103 | + // eslint-disable-next-line no-console |
| 104 | + console.log("[autoInspector] Stopping inspector..."); |
| 105 | + inspectorProcess.kill(); |
| 106 | + inspectorProcess = null; |
| 107 | + } |
| 108 | + }, |
| 109 | + }; |
| 110 | +} |
0 commit comments