Skip to content

Commit ce4344b

Browse files
committed
fix stdin issue with API key
1 parent 0e5e225 commit ce4344b

File tree

4 files changed

+59
-28
lines changed

4 files changed

+59
-28
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules/
2-
*.log
2+
*.log
3+
.vscode/
4+
build/index.js.map

build/index.js

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

src/index.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ const logger = pino({
4040

4141
const SOCKET_API_URL = "https://api.socket.dev/v0/purl?alerts=false&compact=false&fixable=false&licenseattrib=false&licensedetails=false";
4242

43-
let SOCKET_API_KEY = process.env.SOCKET_API_KEY || "";
44-
if (!SOCKET_API_KEY) {
45-
logger.error("SOCKET_API_KEY environment variable is not set");
43+
// Function to get API key interactively (only for HTTP mode)
44+
async function getApiKeyInteractively(): Promise<string> {
4645
const rl = readline.createInterface({
4746
input: process.stdin,
4847
output: process.stderr
@@ -60,15 +59,11 @@ if (!SOCKET_API_KEY) {
6059
process.exit(1);
6160
}
6261

63-
SOCKET_API_KEY = apiKey;
62+
return apiKey;
6463
}
6564

66-
const SOCKET_HEADERS = {
67-
"user-agent": `socket-mcp/${VERSION}`,
68-
"accept": "application/x-ndjson",
69-
"content-type": "application/json",
70-
"authorization": `Bearer ${SOCKET_API_KEY}`
71-
};
65+
// Initialize API key
66+
let SOCKET_API_KEY = process.env.SOCKET_API_KEY || "";
7267

7368
// Transport management
7469
const transports: Record<string, StreamableHTTPServerTransport> = {};
@@ -164,10 +159,8 @@ server.tool(
164159
.map(([key, value]) => `${key}: ${value}`)
165160
.join(', ');
166161

167-
const packageName = jsonData.name || 'unknown';
168162
results.push(`${purl}: ${scoreEntries}`);
169163
} else {
170-
const packageName = jsonData.name || 'unknown';
171164
results.push(`${purl}: No score found`);
172165
}
173166
}
@@ -180,7 +173,6 @@ server.tool(
180173
.map(([key, value]) => `${key}: ${value}`)
181174
.join(', ');
182175

183-
const packageName = jsonData.package?.name || 'unknown';
184176
results.push(`${purl}: ${scoreEntries}`);
185177
}
186178
}
@@ -221,6 +213,28 @@ server.tool(
221213
const useHttp = process.env.MCP_HTTP_MODE === 'true' || process.argv.includes('--http');
222214
const port = parseInt(process.env.MCP_PORT || '3000', 10);
223215

216+
// Validate API key - in stdio mode, we can't prompt interactively
217+
if (!SOCKET_API_KEY) {
218+
if (useHttp) {
219+
// In HTTP mode, we can prompt for the API key
220+
logger.error("SOCKET_API_KEY environment variable is not set");
221+
SOCKET_API_KEY = await getApiKeyInteractively();
222+
} else {
223+
// In stdio mode, we must have the API key as an environment variable
224+
logger.error("SOCKET_API_KEY environment variable is required in stdio mode");
225+
logger.error("Please set the SOCKET_API_KEY environment variable and try again");
226+
process.exit(1);
227+
}
228+
}
229+
230+
// Now that we have the API key, set up the headers
231+
const SOCKET_HEADERS = {
232+
"user-agent": `socket-mcp/${VERSION}`,
233+
"accept": "application/x-ndjson",
234+
"content-type": "application/json",
235+
"authorization": `Bearer ${SOCKET_API_KEY}`
236+
};
237+
224238
if (useHttp) {
225239
// HTTP mode with Server-Sent Events
226240
logger.info(`Starting HTTP server on port ${port}`);

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"strict": true,
99
"esModuleInterop": true,
1010
"skipLibCheck": true,
11+
"sourceMap": true,
1112
"forceConsistentCasingInFileNames": true
1213
},
1314
"include": ["src/**/*"],

0 commit comments

Comments
 (0)