Skip to content

Commit 75a7ad8

Browse files
Merge pull request #8 from Kryptoxic/main
Fix: Startup banner corrupts stdio JSON-RPC stream
2 parents 1d66e3c + 70de035 commit 75a7ad8

5 files changed

Lines changed: 29 additions & 22 deletions

File tree

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"@modelcontextprotocol/sdk": "^1.9.0",
4141
"axios": "",
4242
"cors": "",
43-
"dotenv": "",
43+
"dotenv": "^17.2.0",
4444
"express": "",
4545
"zod": ""
4646
},

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import { resources, addResource as addResourceCore, Resource } from './resources
1818

1919
// Load environment variables with enhanced setup
2020
setupEnvironment();
21-
// Also load with standard dotenv for compatibility
22-
dotenv.config();
21+
// Also load with standard dotenv for compatibility.
22+
// quiet: true suppresses dotenv v17+'s startup banner, which is written to
23+
// stdout and would corrupt the stdio JSON-RPC stream (breaks the MCP client).
24+
dotenv.config({ quiet: true });
2325

2426
// Keep a reference to the server for notifications
2527
let serverInstance: Server | null = null;

src/utils/api-client.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import dotenv from 'dotenv';
33
import path from 'path';
44
import { transformParamsToHyphenated } from './param-transformer';
55

6-
// Try to load environment variables from .env file with absolute path
7-
dotenv.config();
6+
// Try to load environment variables from .env file with absolute path.
7+
// quiet: true suppresses dotenv v17+'s "injecting env" banner, which is
8+
// written to stdout and would corrupt the stdio JSON-RPC stream (this module
9+
// is imported by index.ts, so these calls run during server startup).
10+
dotenv.config({ quiet: true });
811
// Also try with explicit path as fallback
9-
dotenv.config({ path: path.resolve(process.cwd(), '.env') });
12+
dotenv.config({ path: path.resolve(process.cwd(), '.env'), quiet: true });
1013

1114
// NASA API Base URLs
1215
export const NASA_API_BASE_URL = 'https://api.nasa.gov';
@@ -28,7 +31,7 @@ export async function nasaApiRequest(
2831
if (!apiKey) {
2932
try {
3033
const envPath = path.resolve(process.cwd(), '.env');
31-
dotenv.config({ path: envPath });
34+
dotenv.config({ path: envPath, quiet: true });
3235
apiKey = process.env.NASA_API_KEY;
3336
} catch (error) {
3437
console.error('Error loading .env file:', error);
@@ -124,7 +127,7 @@ export async function jplApiRequest(
124127
// If other JPL endpoints require a key but it's missing, try loading .env
125128
try {
126129
const envPath = path.resolve(process.cwd(), '.env');
127-
dotenv.config({ path: envPath });
130+
dotenv.config({ path: envPath, quiet: true });
128131
apiKey = process.env.NASA_API_KEY;
129132
if (apiKey) {
130133
paramsToSend.api_key = apiKey;

src/utils/env-setup.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,26 @@ export function setupEnvironment() {
3939
const rootEnvPath = path.join(currentDir, '.env');
4040
const distEnvPath = path.join(currentDir, 'dist', '.env');
4141

42-
// First try standard .env loading
43-
dotenv.config();
44-
42+
// First try standard .env loading.
43+
// quiet: true suppresses dotenv v17+'s startup banner ("injecting env…"),
44+
// which is printed to stdout and would corrupt the stdio JSON-RPC stream.
45+
dotenv.config({ quiet: true });
46+
4547
// If running from dist, also try parent directory
4648
if (currentDir.includes('dist')) {
4749
const parentEnvPath = path.join(currentDir, '..', '.env');
4850
if (fs.existsSync(parentEnvPath)) {
49-
dotenv.config({ path: parentEnvPath });
51+
dotenv.config({ path: parentEnvPath, quiet: true });
5052
}
5153
}
52-
54+
5355
// Also try explicit paths
5456
if (fs.existsSync(rootEnvPath)) {
55-
dotenv.config({ path: rootEnvPath });
57+
dotenv.config({ path: rootEnvPath, quiet: true });
5658
}
57-
59+
5860
if (fs.existsSync(distEnvPath)) {
59-
dotenv.config({ path: distEnvPath });
61+
dotenv.config({ path: distEnvPath, quiet: true });
6062
}
6163

6264
// Ensure dist directory has a copy of .env

0 commit comments

Comments
 (0)