Skip to content

Commit c4467f9

Browse files
committed
Merge branch 'main' of github.com:MCPJam/inspector
2 parents 5671338 + 7842390 commit c4467f9

File tree

3 files changed

+77
-23
lines changed

3 files changed

+77
-23
lines changed

server/app.ts

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,55 @@ import { cors } from "hono/cors";
44
import { logger } from "hono/logger";
55
import { serveStatic } from "@hono/node-server/serve-static";
66
import dotenv from "dotenv";
7+
import { existsSync } from "fs";
8+
import { join, dirname, resolve } from "path";
9+
import { fileURLToPath } from "url";
710

811
// Import routes
912
import mcpRoutes from "./routes/mcp/index.js";
1013
import { MCPJamClientManager } from "./services/mcpjam-client-manager.js";
1114
import path from "path";
1215

16+
const __filename = fileURLToPath(import.meta.url);
17+
const __dirname = dirname(__filename);
18+
1319
export function createHonoApp() {
1420
// Load environment variables early so route handlers can read CONVEX_HTTP_URL
15-
try {
16-
const envFile =
17-
process.env.NODE_ENV === "production"
18-
? ".env.production"
19-
: ".env.development";
20-
dotenv.config({ path: envFile });
21-
if (!process.env.CONVEX_HTTP_URL) {
22-
dotenv.config();
21+
const envFile =
22+
process.env.NODE_ENV === "production"
23+
? ".env.production"
24+
: ".env.development";
25+
26+
// Determine where to look for .env file:
27+
// 1. Electron packaged: use process.resourcesPath directly
28+
// 2. npm package: package root (two levels up from dist/server)
29+
// 3. Local dev: current working directory
30+
let envPath = envFile;
31+
32+
if (process.env.IS_PACKAGED === "true" && process.resourcesPath) {
33+
// Electron packaged app - use process.resourcesPath directly
34+
envPath = join(process.resourcesPath, envFile);
35+
} else if (process.env.ELECTRON_APP === "true") {
36+
// Electron dev mode - already handled by src/main.ts setting env vars
37+
envPath = join(process.env.ELECTRON_RESOURCES_PATH || ".", envFile);
38+
} else {
39+
// npm package or local dev
40+
const packageRoot = resolve(__dirname, "..", "..");
41+
const packageEnvPath = join(packageRoot, envFile);
42+
if (existsSync(packageEnvPath)) {
43+
envPath = packageEnvPath;
2344
}
24-
} catch (error) {
25-
console.warn("[startup] Failed loading env files", error);
45+
}
46+
47+
dotenv.config({ path: envPath });
48+
49+
// Validate required env vars
50+
if (!process.env.CONVEX_HTTP_URL) {
51+
throw new Error(
52+
`CONVEX_HTTP_URL is required but not set. Tried loading from: ${envPath}\n` +
53+
`IS_PACKAGED=${process.env.IS_PACKAGED}, resourcesPath=${process.resourcesPath}\n` +
54+
`File exists: ${existsSync(envPath)}`,
55+
);
2656
}
2757

2858
// Ensure PATH includes user shell paths so child processes (e.g., npx) can be found

server/index.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import { Hono } from "hono";
55
import { cors } from "hono/cors";
66
import { logger } from "hono/logger";
77
import { serveStatic } from "@hono/node-server/serve-static";
8-
import { readFileSync } from "fs";
9-
import { join } from "path";
8+
import { readFileSync, existsSync } from "fs";
9+
import { join, dirname, resolve } from "path";
10+
import { fileURLToPath } from "url";
11+
12+
const __filename = fileURLToPath(import.meta.url);
13+
const __dirname = dirname(__filename);
1014

1115
// ANSI color codes for console output
1216
const colors = {
@@ -123,17 +127,36 @@ try {
123127
const app = new Hono();
124128

125129
// Load environment variables early so route handlers can read CONVEX_HTTP_URL
126-
try {
127-
const envFile =
128-
process.env.NODE_ENV === "production"
129-
? ".env.production"
130-
: ".env.development";
131-
dotenv.config({ path: envFile });
132-
if (!process.env.CONVEX_HTTP_URL) {
133-
dotenv.config();
130+
const envFile =
131+
process.env.NODE_ENV === "production"
132+
? ".env.production"
133+
: ".env.development";
134+
135+
// Determine where to look for .env file:
136+
// 1. Electron: Resources folder
137+
// 2. npm package: package root (two levels up from dist/server)
138+
// 3. Local dev: current working directory
139+
let envPath = envFile;
140+
if (
141+
process.env.ELECTRON_APP === "true" &&
142+
process.env.ELECTRON_RESOURCES_PATH
143+
) {
144+
envPath = join(process.env.ELECTRON_RESOURCES_PATH, envFile);
145+
} else {
146+
const packageRoot = resolve(__dirname, "..", "..");
147+
const packageEnvPath = join(packageRoot, envFile);
148+
if (existsSync(packageEnvPath)) {
149+
envPath = packageEnvPath;
134150
}
135-
} catch (error) {
136-
console.warn("[startup] Failed loading env files", error);
151+
}
152+
153+
dotenv.config({ path: envPath });
154+
155+
// Validate required env vars
156+
if (!process.env.CONVEX_HTTP_URL) {
157+
throw new Error(
158+
"CONVEX_HTTP_URL is required but not set. Please set it via environment variable or .env file.",
159+
);
137160
}
138161

139162
// Initialize centralized MCPJam Client Manager

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ async function startHonoServer(): Promise<number> {
5555
try {
5656
const port = app.isPackaged ? 3000 : await findAvailablePort(3000);
5757

58-
// Set environment variable to tell the server it's running in Electron
58+
// Set environment variables to tell the server it's running in Electron
5959
process.env.ELECTRON_APP = "true";
6060
process.env.IS_PACKAGED = app.isPackaged ? "true" : "false";
6161
process.env.ELECTRON_RESOURCES_PATH = process.resourcesPath;
62+
process.env.NODE_ENV = app.isPackaged ? "production" : "development";
6263

6364
const honoApp = createHonoApp();
6465

0 commit comments

Comments
 (0)