perfai-cloud-mcp 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +243 -0
- package/dist/auth/oauthFlow.d.ts +23 -0
- package/dist/auth/oauthFlow.d.ts.map +1 -0
- package/dist/auth/oauthFlow.js +174 -0
- package/dist/auth/pkce.d.ts +11 -0
- package/dist/auth/pkce.d.ts.map +1 -0
- package/dist/auth/pkce.js +17 -0
- package/dist/auth/tokenValidator.d.ts +40 -0
- package/dist/auth/tokenValidator.d.ts.map +1 -0
- package/dist/auth/tokenValidator.js +117 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +387 -0
- package/dist/client/apiClient.d.ts +27 -0
- package/dist/client/apiClient.d.ts.map +1 -0
- package/dist/client/apiClient.js +81 -0
- package/dist/client/authManager.d.ts +42 -0
- package/dist/client/authManager.d.ts.map +1 -0
- package/dist/client/authManager.js +136 -0
- package/dist/client/errors.d.ts +10 -0
- package/dist/client/errors.d.ts.map +1 -0
- package/dist/client/errors.js +29 -0
- package/dist/client/types.d.ts +54 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +1 -0
- package/dist/config.d.ts +23 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +50 -0
- package/dist/env.d.ts +2 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/env.js +8 -0
- package/dist/log-guard.d.ts +2 -0
- package/dist/log-guard.d.ts.map +1 -0
- package/dist/log-guard.js +17 -0
- package/dist/server.d.ts +4 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +370 -0
- package/dist/tools/index.d.ts +352 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +39 -0
- package/dist/tools/protected/autoFix.d.ts +22 -0
- package/dist/tools/protected/autoFix.d.ts.map +1 -0
- package/dist/tools/protected/autoFix.js +64 -0
- package/dist/tools/protected/autoFixAll.d.ts +26 -0
- package/dist/tools/protected/autoFixAll.d.ts.map +1 -0
- package/dist/tools/protected/autoFixAll.js +79 -0
- package/dist/tools/protected/checkAppStatus.d.ts +21 -0
- package/dist/tools/protected/checkAppStatus.d.ts.map +1 -0
- package/dist/tools/protected/checkAppStatus.js +57 -0
- package/dist/tools/protected/checkTaskStatus.d.ts +21 -0
- package/dist/tools/protected/checkTaskStatus.d.ts.map +1 -0
- package/dist/tools/protected/checkTaskStatus.js +53 -0
- package/dist/tools/protected/getFixSummary.d.ts +17 -0
- package/dist/tools/protected/getFixSummary.d.ts.map +1 -0
- package/dist/tools/protected/getFixSummary.js +90 -0
- package/dist/tools/protected/getVulnerabilities.d.ts +29 -0
- package/dist/tools/protected/getVulnerabilities.d.ts.map +1 -0
- package/dist/tools/protected/getVulnerabilities.js +41 -0
- package/dist/tools/protected/listApps.d.ts +25 -0
- package/dist/tools/protected/listApps.d.ts.map +1 -0
- package/dist/tools/protected/listApps.js +32 -0
- package/dist/tools/protected/logout.d.ts +12 -0
- package/dist/tools/protected/logout.d.ts.map +1 -0
- package/dist/tools/protected/logout.js +9 -0
- package/dist/tools/protected/registerApp.d.ts +33 -0
- package/dist/tools/protected/registerApp.d.ts.map +1 -0
- package/dist/tools/protected/registerApp.js +96 -0
- package/dist/tools/protected/runSecurityTest.d.ts +17 -0
- package/dist/tools/protected/runSecurityTest.d.ts.map +1 -0
- package/dist/tools/protected/runSecurityTest.js +48 -0
- package/dist/tools/protected/selectApp.d.ts +18 -0
- package/dist/tools/protected/selectApp.d.ts.map +1 -0
- package/dist/tools/protected/selectApp.js +29 -0
- package/dist/tools/protected/submitAppCredentials.d.ts +22 -0
- package/dist/tools/protected/submitAppCredentials.d.ts.map +1 -0
- package/dist/tools/protected/submitAppCredentials.js +43 -0
- package/dist/tools/protected/suggestFix.d.ts +22 -0
- package/dist/tools/protected/suggestFix.d.ts.map +1 -0
- package/dist/tools/protected/suggestFix.js +37 -0
- package/dist/tools/protected/vulnerabilityActions.d.ts +62 -0
- package/dist/tools/protected/vulnerabilityActions.d.ts.map +1 -0
- package/dist/tools/protected/vulnerabilityActions.js +69 -0
- package/dist/tools/public/authStatus.d.ts +12 -0
- package/dist/tools/public/authStatus.d.ts.map +1 -0
- package/dist/tools/public/authStatus.js +13 -0
- package/dist/tools/public/login.d.ts +21 -0
- package/dist/tools/public/login.d.ts.map +1 -0
- package/dist/tools/public/login.js +305 -0
- package/dist/utils/formatters.d.ts +7 -0
- package/dist/utils/formatters.d.ts.map +1 -0
- package/dist/utils/formatters.js +47 -0
- package/dist/utils/poll.d.ts +19 -0
- package/dist/utils/poll.d.ts.map +1 -0
- package/dist/utils/poll.js +32 -0
- package/dist/utils/promptBuilder.d.ts +20 -0
- package/dist/utils/promptBuilder.d.ts.map +1 -0
- package/dist/utils/promptBuilder.js +131 -0
- package/package.json +55 -0
package/dist/server.js
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Must be first: redirects console.* to stderr so stdout carries only JSON-RPC.
|
|
3
|
+
import "./log-guard.js";
|
|
4
|
+
// Must be second: loads .env before config.js reads process.env at import time.
|
|
5
|
+
import "./env.js";
|
|
6
|
+
import { randomUUID } from "node:crypto";
|
|
7
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
8
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
9
|
+
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
10
|
+
import { CallToolRequestSchema, ErrorCode, ListToolsRequestSchema, McpError, } from "@modelcontextprotocol/sdk/types.js";
|
|
11
|
+
import { isInitializeRequest } from "@modelcontextprotocol/sdk/types.js";
|
|
12
|
+
import { SERVER_INFO } from "./config.js";
|
|
13
|
+
import { AuthManager } from "./client/authManager.js";
|
|
14
|
+
import { PUBLIC_TOOLS, PROTECTED_TOOLS, handleLogin, handleAuthStatus, handleLogout, handleRegisterApp, handleCheckAppStatus, handleListApps, handleSelectApp, handleSubmitAppCredentials, handleRunSecurityTest, handleCheckTaskStatus, handleGetVulnerabilities, handleSuggestFix, handleAutoFix, handleAutoFixAll, handleGetFixSummary, handleDismissVulnerability, handleReopenVulnerability, handleMarkVulnerabilityFixed, } from "./tools/index.js";
|
|
15
|
+
function createMcpServer(auth) {
|
|
16
|
+
const server = new Server(SERVER_INFO, {
|
|
17
|
+
capabilities: { tools: {} },
|
|
18
|
+
});
|
|
19
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
20
|
+
return { tools: [...Object.values(PUBLIC_TOOLS), ...Object.values(PROTECTED_TOOLS)] };
|
|
21
|
+
});
|
|
22
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
23
|
+
const { name, arguments: args } = request.params;
|
|
24
|
+
console.error(`→ Tool call: ${name}`);
|
|
25
|
+
try {
|
|
26
|
+
switch (name) {
|
|
27
|
+
case "login":
|
|
28
|
+
return await handleLogin(auth, args);
|
|
29
|
+
case "auth_status":
|
|
30
|
+
return await handleAuthStatus(auth);
|
|
31
|
+
}
|
|
32
|
+
if (!auth.isAuthenticated() && !auth.hasCredentials()) {
|
|
33
|
+
throw new McpError(ErrorCode.InvalidRequest, `Authentication required. Run the 'login' tool first to access '${name}'.`);
|
|
34
|
+
}
|
|
35
|
+
switch (name) {
|
|
36
|
+
case "logout":
|
|
37
|
+
return await handleLogout(auth);
|
|
38
|
+
case "register_app":
|
|
39
|
+
return await handleRegisterApp(auth, args);
|
|
40
|
+
case "check_app_status":
|
|
41
|
+
return await handleCheckAppStatus(auth, args);
|
|
42
|
+
case "list_apps":
|
|
43
|
+
return await handleListApps(auth, args);
|
|
44
|
+
case "select_app":
|
|
45
|
+
return await handleSelectApp(auth, args);
|
|
46
|
+
case "submit_app_credentials":
|
|
47
|
+
return await handleSubmitAppCredentials(auth, args);
|
|
48
|
+
case "run_security_test":
|
|
49
|
+
return await handleRunSecurityTest(auth, args);
|
|
50
|
+
case "check_task_status":
|
|
51
|
+
return await handleCheckTaskStatus(auth, args);
|
|
52
|
+
case "get_vulnerabilities":
|
|
53
|
+
return await handleGetVulnerabilities(auth, args);
|
|
54
|
+
case "suggest_fix":
|
|
55
|
+
return await handleSuggestFix(auth, args);
|
|
56
|
+
case "auto_fix":
|
|
57
|
+
return await handleAutoFix(auth, args);
|
|
58
|
+
case "auto_fix_all":
|
|
59
|
+
return await handleAutoFixAll(auth, args);
|
|
60
|
+
case "get_fix_summary":
|
|
61
|
+
return await handleGetFixSummary(auth, args);
|
|
62
|
+
case "dismiss_vulnerability":
|
|
63
|
+
return await handleDismissVulnerability(auth, args);
|
|
64
|
+
case "reopen_vulnerability":
|
|
65
|
+
return await handleReopenVulnerability(auth, args);
|
|
66
|
+
case "mark_vulnerability_fixed":
|
|
67
|
+
return await handleMarkVulnerabilityFixed(auth, args);
|
|
68
|
+
default:
|
|
69
|
+
throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${name}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (error instanceof McpError)
|
|
74
|
+
throw error;
|
|
75
|
+
console.error(`Tool '${name}' failed:`, error);
|
|
76
|
+
throw new McpError(ErrorCode.InternalError, `Tool execution failed: ${error}`);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
server.onerror = (error) => {
|
|
80
|
+
console.error("[MCP Error]", error);
|
|
81
|
+
};
|
|
82
|
+
return server;
|
|
83
|
+
}
|
|
84
|
+
/* ------------------------------------------------------------------ */
|
|
85
|
+
/* Stdio transport (default — used by Cursor, Claude Code, etc.) */
|
|
86
|
+
/* ------------------------------------------------------------------ */
|
|
87
|
+
async function startStdio() {
|
|
88
|
+
const auth = new AuthManager();
|
|
89
|
+
const server = createMcpServer(auth);
|
|
90
|
+
const transport = new StdioServerTransport();
|
|
91
|
+
await server.connect(transport);
|
|
92
|
+
if (auth.isAuthenticated()) {
|
|
93
|
+
console.error(`Ready (stdio). Already authenticated as ${auth.getAuthenticatedEmail()} via ${auth.getAuthMethod()}.`);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
console.error("Ready (stdio). Run 'login' to authenticate (or set PERFAI_TOKEN / PERFAI_EMAIL+PERFAI_PASSWORD).");
|
|
97
|
+
}
|
|
98
|
+
process.on("SIGINT", async () => {
|
|
99
|
+
await server.close();
|
|
100
|
+
process.exit(0);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
/* ------------------------------------------------------------------ */
|
|
104
|
+
/* HTTP transport (for ChatGPT, remote MCP clients, etc.) */
|
|
105
|
+
/* ------------------------------------------------------------------ */
|
|
106
|
+
import { validateBearerToken, isJWT } from "./auth/tokenValidator.js";
|
|
107
|
+
function decodeCredentialToken(token) {
|
|
108
|
+
try {
|
|
109
|
+
const decoded = Buffer.from(token, "base64").toString("utf-8");
|
|
110
|
+
const colonIndex = decoded.indexOf(":");
|
|
111
|
+
if (colonIndex <= 0 || colonIndex === decoded.length - 1)
|
|
112
|
+
return null;
|
|
113
|
+
// Heuristic: if decoded looks like email:password (has @ before colon),
|
|
114
|
+
// it's likely the legacy format. JWTs won't decode to this pattern.
|
|
115
|
+
if (!decoded.slice(0, colonIndex).includes("@"))
|
|
116
|
+
return null;
|
|
117
|
+
return {
|
|
118
|
+
email: decoded.slice(0, colonIndex),
|
|
119
|
+
password: decoded.slice(colonIndex + 1),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
async function validatePerfAICredentials(email, password) {
|
|
127
|
+
const { default: axios } = await import("axios");
|
|
128
|
+
const { AUTH_TOKEN_URL } = await import("./config.js");
|
|
129
|
+
try {
|
|
130
|
+
const response = await axios.post(AUTH_TOKEN_URL, { username: email, password }, { headers: { "Content-Type": "application/json" }, timeout: 15_000 });
|
|
131
|
+
return Boolean(response.data?.id_token);
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
async function startHttp() {
|
|
138
|
+
const { default: express } = await import("express");
|
|
139
|
+
const app = express();
|
|
140
|
+
app.use(express.json());
|
|
141
|
+
const MCP_API_KEY = process.env.MCP_API_KEY;
|
|
142
|
+
console.error("Auth mode: OAuth tokens (primary)" +
|
|
143
|
+
(MCP_API_KEY ? " + static MCP_API_KEY (legacy)" : "") +
|
|
144
|
+
" + base64 credentials (deprecated fallback)");
|
|
145
|
+
// Bearer token authentication middleware
|
|
146
|
+
// Priority order:
|
|
147
|
+
// 1. Static MCP_API_KEY (legacy, if configured)
|
|
148
|
+
// 2. Real OAuth/id_token (JWT — validated via tokenValidator)
|
|
149
|
+
// 3. base64(email:password) — DEPRECATED, kept for backward compatibility
|
|
150
|
+
const bearerAuth = async (req, res, next) => {
|
|
151
|
+
const authHeader = req.headers.authorization;
|
|
152
|
+
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
153
|
+
res.status(401).json({
|
|
154
|
+
jsonrpc: "2.0",
|
|
155
|
+
error: {
|
|
156
|
+
code: -32001,
|
|
157
|
+
message: "Unauthorized: Bearer token required. " +
|
|
158
|
+
"Use your PerfAI id_token (from OAuth login or PERFAI_TOKEN) as the bearer value.",
|
|
159
|
+
},
|
|
160
|
+
id: null,
|
|
161
|
+
});
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const token = authHeader.slice(7);
|
|
165
|
+
// 1. Check static MCP_API_KEY first (backward compatible)
|
|
166
|
+
if (MCP_API_KEY && token === MCP_API_KEY) {
|
|
167
|
+
return next();
|
|
168
|
+
}
|
|
169
|
+
// 2. Real token (JWT) — primary path
|
|
170
|
+
if (isJWT(token)) {
|
|
171
|
+
const result = await validateBearerToken(token);
|
|
172
|
+
if (result.valid) {
|
|
173
|
+
req.perfaiToken = token;
|
|
174
|
+
req.perfaiEmail = result.email;
|
|
175
|
+
console.error(`Authenticated (token): ${result.email ?? "unknown"}`);
|
|
176
|
+
return next();
|
|
177
|
+
}
|
|
178
|
+
res.status(401).json({
|
|
179
|
+
jsonrpc: "2.0",
|
|
180
|
+
error: {
|
|
181
|
+
code: -32001,
|
|
182
|
+
message: `Token validation failed: ${result.error ?? "invalid token"}. ` +
|
|
183
|
+
"Get a fresh token via OAuth login at https://cloud.perfai.ai or set PERFAI_TOKEN.",
|
|
184
|
+
},
|
|
185
|
+
id: null,
|
|
186
|
+
});
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
// 3. Legacy: base64(email:password) — DEPRECATED fallback
|
|
190
|
+
const creds = decodeCredentialToken(token);
|
|
191
|
+
if (creds) {
|
|
192
|
+
console.error(`DEPRECATED: base64(email:password) auth from ${creds.email}. ` +
|
|
193
|
+
"Migrate to OAuth tokens (id_token) — see docs.");
|
|
194
|
+
const valid = await validatePerfAICredentials(creds.email, creds.password);
|
|
195
|
+
if (valid) {
|
|
196
|
+
req.perfaiCredentials = creds;
|
|
197
|
+
req.perfaiEmail = creds.email;
|
|
198
|
+
return next();
|
|
199
|
+
}
|
|
200
|
+
res.status(403).json({
|
|
201
|
+
jsonrpc: "2.0",
|
|
202
|
+
error: {
|
|
203
|
+
code: -32001,
|
|
204
|
+
message: `Authentication failed for ${creds.email}. ` +
|
|
205
|
+
"Verify credentials at https://cloud.perfai.ai, or migrate to OAuth token auth.",
|
|
206
|
+
},
|
|
207
|
+
id: null,
|
|
208
|
+
});
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
// 4. Unrecognized format
|
|
212
|
+
res.status(401).json({
|
|
213
|
+
jsonrpc: "2.0",
|
|
214
|
+
error: {
|
|
215
|
+
code: -32001,
|
|
216
|
+
message: "Unrecognized token format. Provide either: " +
|
|
217
|
+
"(1) a PerfAI id_token from OAuth login, or " +
|
|
218
|
+
"(2) PERFAI_TOKEN from your PerfAI console.",
|
|
219
|
+
},
|
|
220
|
+
id: null,
|
|
221
|
+
});
|
|
222
|
+
};
|
|
223
|
+
app.use("/mcp", bearerAuth);
|
|
224
|
+
// Per-session state
|
|
225
|
+
const transports = {};
|
|
226
|
+
const sessions = {};
|
|
227
|
+
// POST /mcp — main MCP request endpoint
|
|
228
|
+
app.post("/mcp", async (req, res) => {
|
|
229
|
+
const sessionId = req.headers["mcp-session-id"];
|
|
230
|
+
try {
|
|
231
|
+
if (sessionId && transports[sessionId]) {
|
|
232
|
+
await transports[sessionId].handleRequest(req, res, req.body);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
if (!sessionId && isInitializeRequest(req.body)) {
|
|
236
|
+
const auth = new AuthManager();
|
|
237
|
+
// Pre-load session from whichever auth method succeeded
|
|
238
|
+
if (req.perfaiToken) {
|
|
239
|
+
// Real token path — no password ever touches the server
|
|
240
|
+
auth.setToken(req.perfaiToken, req.perfaiEmail, "oauth");
|
|
241
|
+
}
|
|
242
|
+
else if (req.perfaiCredentials) {
|
|
243
|
+
// Deprecated legacy path
|
|
244
|
+
auth.setCredentials(req.perfaiCredentials.email, req.perfaiCredentials.password);
|
|
245
|
+
}
|
|
246
|
+
const transport = new StreamableHTTPServerTransport({
|
|
247
|
+
sessionIdGenerator: () => randomUUID(),
|
|
248
|
+
onsessioninitialized: (sid) => {
|
|
249
|
+
transports[sid] = transport;
|
|
250
|
+
sessions[sid] = { auth };
|
|
251
|
+
console.error(`Session initialized: ${sid} (user: ${req.perfaiEmail ?? "api-key"})`);
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
transport.onclose = () => {
|
|
255
|
+
const sid = transport.sessionId;
|
|
256
|
+
if (sid) {
|
|
257
|
+
delete transports[sid];
|
|
258
|
+
delete sessions[sid];
|
|
259
|
+
console.error(`Session closed: ${sid}`);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
const server = createMcpServer(auth);
|
|
263
|
+
await server.connect(transport);
|
|
264
|
+
await transport.handleRequest(req, res, req.body);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
res.status(400).json({
|
|
268
|
+
jsonrpc: "2.0",
|
|
269
|
+
error: { code: -32000, message: "Bad Request: No valid session ID provided" },
|
|
270
|
+
id: null,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
catch (error) {
|
|
274
|
+
console.error("Error handling MCP request:", error);
|
|
275
|
+
if (!res.headersSent) {
|
|
276
|
+
res.status(500).json({
|
|
277
|
+
jsonrpc: "2.0",
|
|
278
|
+
error: { code: -32603, message: "Internal server error" },
|
|
279
|
+
id: null,
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
// GET /mcp — SSE stream for server-initiated messages
|
|
285
|
+
app.get("/mcp", async (req, res) => {
|
|
286
|
+
const sessionId = req.headers["mcp-session-id"];
|
|
287
|
+
if (!sessionId || !transports[sessionId]) {
|
|
288
|
+
res.status(400).json({
|
|
289
|
+
jsonrpc: "2.0",
|
|
290
|
+
error: { code: -32000, message: "Invalid or missing session ID" },
|
|
291
|
+
id: null,
|
|
292
|
+
});
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
await transports[sessionId].handleRequest(req, res);
|
|
296
|
+
});
|
|
297
|
+
// DELETE /mcp — session termination
|
|
298
|
+
app.delete("/mcp", async (req, res) => {
|
|
299
|
+
const sessionId = req.headers["mcp-session-id"];
|
|
300
|
+
if (!sessionId || !transports[sessionId]) {
|
|
301
|
+
res.status(400).json({
|
|
302
|
+
jsonrpc: "2.0",
|
|
303
|
+
error: { code: -32000, message: "Invalid or missing session ID" },
|
|
304
|
+
id: null,
|
|
305
|
+
});
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
await transports[sessionId].handleRequest(req, res);
|
|
309
|
+
});
|
|
310
|
+
// Root — friendly landing page
|
|
311
|
+
app.get("/", (_req, res) => {
|
|
312
|
+
res.json({
|
|
313
|
+
name: SERVER_INFO.name,
|
|
314
|
+
version: SERVER_INFO.version,
|
|
315
|
+
status: "running",
|
|
316
|
+
mcp_endpoint: "/mcp",
|
|
317
|
+
auth: {
|
|
318
|
+
primary: "Bearer <id_token> — use your PerfAI OAuth token",
|
|
319
|
+
headless: "Set PERFAI_TOKEN env var with your token from https://cloud.perfai.ai",
|
|
320
|
+
deprecated: "base64(email:password) still accepted but will be removed",
|
|
321
|
+
},
|
|
322
|
+
docs: "This is a Model Context Protocol (MCP) server for PerfAI. Connect to /mcp from any MCP-compatible client (ChatGPT, Cursor, Claude, etc.).",
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
// Health-check endpoint
|
|
326
|
+
app.get("/health", (_req, res) => {
|
|
327
|
+
res.json({ status: "ok", server: SERVER_INFO.name, version: SERVER_INFO.version });
|
|
328
|
+
});
|
|
329
|
+
const PORT = parseInt(process.env.PORT || "3000", 10);
|
|
330
|
+
app.listen(PORT, "0.0.0.0", () => {
|
|
331
|
+
console.error(`MCP HTTP server listening on http://0.0.0.0:${PORT}/mcp`);
|
|
332
|
+
console.error(`Health check: http://0.0.0.0:${PORT}/health`);
|
|
333
|
+
});
|
|
334
|
+
process.on("SIGINT", async () => {
|
|
335
|
+
console.error("Shutting down...");
|
|
336
|
+
for (const sid of Object.keys(transports)) {
|
|
337
|
+
await transports[sid].close().catch(() => { });
|
|
338
|
+
delete transports[sid];
|
|
339
|
+
delete sessions[sid];
|
|
340
|
+
}
|
|
341
|
+
process.exit(0);
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
/* ------------------------------------------------------------------ */
|
|
345
|
+
/* Entry point */
|
|
346
|
+
/* ------------------------------------------------------------------ */
|
|
347
|
+
async function main() {
|
|
348
|
+
console.error(`Starting ${SERVER_INFO.name} v${SERVER_INFO.version}...`);
|
|
349
|
+
console.error("Public tools:", Object.keys(PUBLIC_TOOLS).join(", "));
|
|
350
|
+
console.error("Protected tools:", Object.keys(PROTECTED_TOOLS).join(", "));
|
|
351
|
+
const transport = process.env.MCP_TRANSPORT?.toLowerCase();
|
|
352
|
+
if (transport === "http") {
|
|
353
|
+
await startHttp();
|
|
354
|
+
}
|
|
355
|
+
else {
|
|
356
|
+
await startStdio();
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
process.on("uncaughtException", (error) => {
|
|
360
|
+
console.error("Uncaught Exception:", error);
|
|
361
|
+
process.exit(1);
|
|
362
|
+
});
|
|
363
|
+
process.on("unhandledRejection", (reason, promise) => {
|
|
364
|
+
console.error("Unhandled Rejection at:", promise, "reason:", reason);
|
|
365
|
+
process.exit(1);
|
|
366
|
+
});
|
|
367
|
+
main().catch((error) => {
|
|
368
|
+
console.error("Failed to start server:", error);
|
|
369
|
+
process.exit(1);
|
|
370
|
+
});
|