@mcp-use/inspector 0.2.1 → 0.2.2

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,294 @@
1
+ import { exec } from 'node:child_process';
2
+ import { existsSync } from 'node:fs';
3
+ import { dirname, join } from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+ import { promisify } from 'node:util';
6
+ import { serve } from '@hono/node-server';
7
+ import { Hono } from 'hono';
8
+ import { cors } from 'hono/cors';
9
+ import { logger } from 'hono/logger';
10
+ import faviconProxy from './favicon-proxy.js';
11
+ import { MCPInspector } from './mcp-inspector.js';
12
+ const __filename = fileURLToPath(import.meta.url);
13
+ const __dirname = dirname(__filename);
14
+ const execAsync = promisify(exec);
15
+ // Check if a specific port is available
16
+ async function isPortAvailable(port) {
17
+ const net = await import('node:net');
18
+ return new Promise((resolve) => {
19
+ const server = net.createServer();
20
+ server.listen(port, () => {
21
+ server.close(() => resolve(true));
22
+ });
23
+ server.on('error', () => resolve(false));
24
+ });
25
+ }
26
+ const app = new Hono();
27
+ // Middleware
28
+ app.use('*', cors());
29
+ app.use('*', logger());
30
+ // Mount favicon proxy
31
+ app.route('/api/favicon', faviconProxy);
32
+ // Health check
33
+ app.get('/health', (c) => {
34
+ return c.json({ status: 'ok', timestamp: new Date().toISOString() });
35
+ });
36
+ // MCP Inspector routes
37
+ const mcpInspector = new MCPInspector();
38
+ // List available MCP servers
39
+ app.get('/api/servers', async (c) => {
40
+ try {
41
+ const servers = await mcpInspector.listServers();
42
+ return c.json({ servers });
43
+ }
44
+ catch {
45
+ return c.json({ error: 'Failed to list servers' }, 500);
46
+ }
47
+ });
48
+ // Connect to an MCP server
49
+ app.post('/api/servers/connect', async (c) => {
50
+ try {
51
+ const { url, command } = await c.req.json();
52
+ const server = await mcpInspector.connectToServer(url, command);
53
+ return c.json({ server });
54
+ }
55
+ catch {
56
+ return c.json({ error: 'Failed to connect to server' }, 500);
57
+ }
58
+ });
59
+ // Get server details
60
+ app.get('/api/servers/:id', async (c) => {
61
+ try {
62
+ const id = c.req.param('id');
63
+ const server = await mcpInspector.getServer(id);
64
+ if (!server) {
65
+ return c.json({ error: 'Server not found' }, 404);
66
+ }
67
+ return c.json({ server });
68
+ }
69
+ catch {
70
+ return c.json({ error: 'Failed to get server details' }, 500);
71
+ }
72
+ });
73
+ // Execute a tool on a server
74
+ app.post('/api/servers/:id/tools/:toolName/execute', async (c) => {
75
+ try {
76
+ const id = c.req.param('id');
77
+ const toolName = c.req.param('toolName');
78
+ const input = await c.req.json();
79
+ const result = await mcpInspector.executeTool(id, toolName, input);
80
+ return c.json({ result });
81
+ }
82
+ catch {
83
+ return c.json({ error: 'Failed to execute tool' }, 500);
84
+ }
85
+ });
86
+ // Get server tools
87
+ app.get('/api/servers/:id/tools', async (c) => {
88
+ try {
89
+ const id = c.req.param('id');
90
+ const tools = await mcpInspector.getServerTools(id);
91
+ return c.json({ tools });
92
+ }
93
+ catch {
94
+ return c.json({ error: 'Failed to get server tools' }, 500);
95
+ }
96
+ });
97
+ // Get server resources
98
+ app.get('/api/servers/:id/resources', async (c) => {
99
+ try {
100
+ const id = c.req.param('id');
101
+ const resources = await mcpInspector.getServerResources(id);
102
+ return c.json({ resources });
103
+ }
104
+ catch {
105
+ return c.json({ error: 'Failed to get server resources' }, 500);
106
+ }
107
+ });
108
+ // Disconnect from a server
109
+ app.delete('/api/servers/:id', async (c) => {
110
+ try {
111
+ const id = c.req.param('id');
112
+ await mcpInspector.disconnectServer(id);
113
+ return c.json({ success: true });
114
+ }
115
+ catch {
116
+ return c.json({ error: 'Failed to disconnect server' }, 500);
117
+ }
118
+ });
119
+ // Check if we're in development mode (Vite dev server running)
120
+ const isDev = process.env.NODE_ENV === 'development' || process.env.VITE_DEV === 'true';
121
+ // Serve static assets from the built client
122
+ const clientDistPath = join(__dirname, '../../dist/client');
123
+ if (isDev) {
124
+ // Development mode: proxy client requests to Vite dev server
125
+ console.warn('🔧 Development mode: Proxying client requests to Vite dev server');
126
+ // Proxy all non-API requests to Vite dev server
127
+ app.get('*', async (c) => {
128
+ const path = c.req.path;
129
+ // Skip API routes
130
+ if (path.startsWith('/api/')) {
131
+ return c.notFound();
132
+ }
133
+ try {
134
+ // Vite dev server should be running on port 3000
135
+ const viteUrl = `http://localhost:3000${path}`;
136
+ const response = await fetch(viteUrl, {
137
+ signal: AbortSignal.timeout(1000), // 1 second timeout
138
+ });
139
+ if (response.ok) {
140
+ const content = await response.text();
141
+ const contentType = response.headers.get('content-type') || 'text/html';
142
+ c.header('Content-Type', contentType);
143
+ return c.html(content);
144
+ }
145
+ }
146
+ catch (error) {
147
+ console.warn(`Failed to proxy to Vite dev server: ${error}`);
148
+ }
149
+ // Fallback HTML if Vite dev server is not running
150
+ return c.html(`
151
+ <!DOCTYPE html>
152
+ <html>
153
+ <head>
154
+ <title>MCP Inspector - Development</title>
155
+ </head>
156
+ <body>
157
+ <h1>MCP Inspector - Development Mode</h1>
158
+ <p>Vite dev server is not running. Please start it with:</p>
159
+ <pre>yarn dev:client</pre>
160
+ <p>API is available at <a href="/api/servers">/api/servers</a></p>
161
+ </body>
162
+ </html>
163
+ `);
164
+ });
165
+ }
166
+ else if (existsSync(clientDistPath)) {
167
+ // Production mode: serve static assets from built client
168
+ // Serve static assets from /inspector/assets/* (matching Vite's base path)
169
+ app.get('/inspector/assets/*', async (c) => {
170
+ const path = c.req.path.replace('/inspector/assets/', 'assets/');
171
+ const fullPath = join(clientDistPath, path);
172
+ if (existsSync(fullPath)) {
173
+ const content = await import('node:fs').then(fs => fs.readFileSync(fullPath));
174
+ // Set appropriate content type based on file extension
175
+ if (path.endsWith('.js')) {
176
+ c.header('Content-Type', 'application/javascript');
177
+ }
178
+ else if (path.endsWith('.css')) {
179
+ c.header('Content-Type', 'text/css');
180
+ }
181
+ else if (path.endsWith('.svg')) {
182
+ c.header('Content-Type', 'image/svg+xml');
183
+ }
184
+ return c.body(content);
185
+ }
186
+ return c.notFound();
187
+ });
188
+ // Redirect root path to /inspector
189
+ app.get('/', (c) => {
190
+ return c.redirect('/inspector');
191
+ });
192
+ // Serve the main HTML file for /inspector and all other routes (SPA routing)
193
+ app.get('*', (c) => {
194
+ const indexPath = join(clientDistPath, 'index.html');
195
+ if (existsSync(indexPath)) {
196
+ const content = import('node:fs').then(fs => fs.readFileSync(indexPath, 'utf-8'));
197
+ return c.html(content);
198
+ }
199
+ return c.html(`
200
+ <!DOCTYPE html>
201
+ <html>
202
+ <head>
203
+ <title>MCP Inspector</title>
204
+ </head>
205
+ <body>
206
+ <h1>MCP Inspector</h1>
207
+ <p>Client files not found. Please run 'yarn build' to build the UI.</p>
208
+ <p>API is available at <a href="/api/servers">/api/servers</a></p>
209
+ </body>
210
+ </html>
211
+ `);
212
+ });
213
+ }
214
+ else {
215
+ console.warn(`⚠️ MCP Inspector client files not found at ${clientDistPath}`);
216
+ console.warn(` Run 'yarn build' in the inspector package to build the UI`);
217
+ // Fallback for when client is not built
218
+ app.get('*', (c) => {
219
+ return c.html(`
220
+ <!DOCTYPE html>
221
+ <html>
222
+ <head>
223
+ <title>MCP Inspector</title>
224
+ </head>
225
+ <body>
226
+ <h1>MCP Inspector</h1>
227
+ <p>Client files not found. Please run 'yarn build' to build the UI.</p>
228
+ <p>API is available at <a href="/api/servers">/api/servers</a></p>
229
+ </body>
230
+ </html>
231
+ `);
232
+ });
233
+ }
234
+ // Start the server
235
+ async function startServer() {
236
+ try {
237
+ // In development mode, use port 3001 for API server
238
+ // In production/standalone mode, try 3001 first, then 3002 as fallback
239
+ const isDev = process.env.NODE_ENV === 'development' || process.env.VITE_DEV === 'true';
240
+ let port = 3001;
241
+ const available = await isPortAvailable(port);
242
+ if (!available) {
243
+ if (isDev) {
244
+ console.error(`❌ Port ${port} is not available. Please stop the process using this port and try again.`);
245
+ process.exit(1);
246
+ }
247
+ else {
248
+ // In standalone mode, try fallback port
249
+ const fallbackPort = 3002;
250
+ console.warn(`⚠️ Port ${port} is not available, trying ${fallbackPort}`);
251
+ const fallbackAvailable = await isPortAvailable(fallbackPort);
252
+ if (!fallbackAvailable) {
253
+ console.error(`❌ Neither port ${port} nor ${fallbackPort} is available. Please stop the processes using these ports and try again.`);
254
+ process.exit(1);
255
+ }
256
+ port = fallbackPort;
257
+ }
258
+ }
259
+ serve({
260
+ fetch: app.fetch,
261
+ port,
262
+ });
263
+ if (isDev) {
264
+ console.warn(`🚀 MCP Inspector API server running on http://localhost:${port}`);
265
+ console.warn(`🌐 Vite dev server should be running on http://localhost:3000`);
266
+ }
267
+ else {
268
+ console.warn(`🚀 MCP Inspector running on http://localhost:${port}`);
269
+ }
270
+ // Auto-open browser in development
271
+ if (process.env.NODE_ENV !== 'production') {
272
+ try {
273
+ const command = process.platform === 'win32' ? 'start' : process.platform === 'darwin' ? 'open' : 'xdg-open';
274
+ const url = isDev ? 'http://localhost:3000' : `http://localhost:${port}`;
275
+ await execAsync(`${command} ${url}`);
276
+ console.warn(`🌐 Browser opened automatically`);
277
+ }
278
+ catch {
279
+ const url = isDev ? 'http://localhost:3000' : `http://localhost:${port}`;
280
+ console.warn(`🌐 Please open ${url} in your browser`);
281
+ }
282
+ }
283
+ return { port, fetch: app.fetch };
284
+ }
285
+ catch (error) {
286
+ console.error('Failed to start server:', error);
287
+ process.exit(1);
288
+ }
289
+ }
290
+ // Start the server if this file is run directly
291
+ if (import.meta.url === `file://${process.argv[1]}`) {
292
+ startServer();
293
+ }
294
+ export default { startServer };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mcp-use/inspector",
3
3
  "type": "module",
4
- "version": "0.2.1",
4
+ "version": "0.2.2",
5
5
  "description": "MCP Inspector - A tool for inspecting and debugging MCP servers",
6
6
  "author": "",
7
7
  "license": "MIT",
@@ -58,7 +58,7 @@
58
58
  "sonner": "^2.0.7",
59
59
  "tailwind-merge": "^3.3.1",
60
60
  "vite-express": "^0.21.1",
61
- "mcp-use": "0.3.0"
61
+ "mcp-use": "1.0.0"
62
62
  },
63
63
  "publishConfig": {
64
64
  "access": "public"