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.
Files changed (99) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +243 -0
  3. package/dist/auth/oauthFlow.d.ts +23 -0
  4. package/dist/auth/oauthFlow.d.ts.map +1 -0
  5. package/dist/auth/oauthFlow.js +174 -0
  6. package/dist/auth/pkce.d.ts +11 -0
  7. package/dist/auth/pkce.d.ts.map +1 -0
  8. package/dist/auth/pkce.js +17 -0
  9. package/dist/auth/tokenValidator.d.ts +40 -0
  10. package/dist/auth/tokenValidator.d.ts.map +1 -0
  11. package/dist/auth/tokenValidator.js +117 -0
  12. package/dist/cli.d.ts +3 -0
  13. package/dist/cli.d.ts.map +1 -0
  14. package/dist/cli.js +387 -0
  15. package/dist/client/apiClient.d.ts +27 -0
  16. package/dist/client/apiClient.d.ts.map +1 -0
  17. package/dist/client/apiClient.js +81 -0
  18. package/dist/client/authManager.d.ts +42 -0
  19. package/dist/client/authManager.d.ts.map +1 -0
  20. package/dist/client/authManager.js +136 -0
  21. package/dist/client/errors.d.ts +10 -0
  22. package/dist/client/errors.d.ts.map +1 -0
  23. package/dist/client/errors.js +29 -0
  24. package/dist/client/types.d.ts +54 -0
  25. package/dist/client/types.d.ts.map +1 -0
  26. package/dist/client/types.js +1 -0
  27. package/dist/config.d.ts +23 -0
  28. package/dist/config.d.ts.map +1 -0
  29. package/dist/config.js +50 -0
  30. package/dist/env.d.ts +2 -0
  31. package/dist/env.d.ts.map +1 -0
  32. package/dist/env.js +8 -0
  33. package/dist/log-guard.d.ts +2 -0
  34. package/dist/log-guard.d.ts.map +1 -0
  35. package/dist/log-guard.js +17 -0
  36. package/dist/server.d.ts +4 -0
  37. package/dist/server.d.ts.map +1 -0
  38. package/dist/server.js +370 -0
  39. package/dist/tools/index.d.ts +352 -0
  40. package/dist/tools/index.d.ts.map +1 -0
  41. package/dist/tools/index.js +39 -0
  42. package/dist/tools/protected/autoFix.d.ts +22 -0
  43. package/dist/tools/protected/autoFix.d.ts.map +1 -0
  44. package/dist/tools/protected/autoFix.js +64 -0
  45. package/dist/tools/protected/autoFixAll.d.ts +26 -0
  46. package/dist/tools/protected/autoFixAll.d.ts.map +1 -0
  47. package/dist/tools/protected/autoFixAll.js +79 -0
  48. package/dist/tools/protected/checkAppStatus.d.ts +21 -0
  49. package/dist/tools/protected/checkAppStatus.d.ts.map +1 -0
  50. package/dist/tools/protected/checkAppStatus.js +57 -0
  51. package/dist/tools/protected/checkTaskStatus.d.ts +21 -0
  52. package/dist/tools/protected/checkTaskStatus.d.ts.map +1 -0
  53. package/dist/tools/protected/checkTaskStatus.js +53 -0
  54. package/dist/tools/protected/getFixSummary.d.ts +17 -0
  55. package/dist/tools/protected/getFixSummary.d.ts.map +1 -0
  56. package/dist/tools/protected/getFixSummary.js +90 -0
  57. package/dist/tools/protected/getVulnerabilities.d.ts +29 -0
  58. package/dist/tools/protected/getVulnerabilities.d.ts.map +1 -0
  59. package/dist/tools/protected/getVulnerabilities.js +41 -0
  60. package/dist/tools/protected/listApps.d.ts +25 -0
  61. package/dist/tools/protected/listApps.d.ts.map +1 -0
  62. package/dist/tools/protected/listApps.js +32 -0
  63. package/dist/tools/protected/logout.d.ts +12 -0
  64. package/dist/tools/protected/logout.d.ts.map +1 -0
  65. package/dist/tools/protected/logout.js +9 -0
  66. package/dist/tools/protected/registerApp.d.ts +33 -0
  67. package/dist/tools/protected/registerApp.d.ts.map +1 -0
  68. package/dist/tools/protected/registerApp.js +96 -0
  69. package/dist/tools/protected/runSecurityTest.d.ts +17 -0
  70. package/dist/tools/protected/runSecurityTest.d.ts.map +1 -0
  71. package/dist/tools/protected/runSecurityTest.js +48 -0
  72. package/dist/tools/protected/selectApp.d.ts +18 -0
  73. package/dist/tools/protected/selectApp.d.ts.map +1 -0
  74. package/dist/tools/protected/selectApp.js +29 -0
  75. package/dist/tools/protected/submitAppCredentials.d.ts +22 -0
  76. package/dist/tools/protected/submitAppCredentials.d.ts.map +1 -0
  77. package/dist/tools/protected/submitAppCredentials.js +43 -0
  78. package/dist/tools/protected/suggestFix.d.ts +22 -0
  79. package/dist/tools/protected/suggestFix.d.ts.map +1 -0
  80. package/dist/tools/protected/suggestFix.js +37 -0
  81. package/dist/tools/protected/vulnerabilityActions.d.ts +62 -0
  82. package/dist/tools/protected/vulnerabilityActions.d.ts.map +1 -0
  83. package/dist/tools/protected/vulnerabilityActions.js +69 -0
  84. package/dist/tools/public/authStatus.d.ts +12 -0
  85. package/dist/tools/public/authStatus.d.ts.map +1 -0
  86. package/dist/tools/public/authStatus.js +13 -0
  87. package/dist/tools/public/login.d.ts +21 -0
  88. package/dist/tools/public/login.d.ts.map +1 -0
  89. package/dist/tools/public/login.js +305 -0
  90. package/dist/utils/formatters.d.ts +7 -0
  91. package/dist/utils/formatters.d.ts.map +1 -0
  92. package/dist/utils/formatters.js +47 -0
  93. package/dist/utils/poll.d.ts +19 -0
  94. package/dist/utils/poll.d.ts.map +1 -0
  95. package/dist/utils/poll.js +32 -0
  96. package/dist/utils/promptBuilder.d.ts +20 -0
  97. package/dist/utils/promptBuilder.d.ts.map +1 -0
  98. package/dist/utils/promptBuilder.js +131 -0
  99. package/package.json +55 -0
@@ -0,0 +1,29 @@
1
+ import { getApp } from "../../client/apiClient.js";
2
+ import { formatApp } from "../../utils/formatters.js";
3
+ export const selectAppTool = {
4
+ name: "select_app",
5
+ description: "🎯 Set the 'current app' for subsequent tools (run_security_test, get_vulnerabilities, etc.) by appId.",
6
+ inputSchema: {
7
+ type: "object",
8
+ properties: {
9
+ appId: { type: "string", description: "App ID, e.g. from list_apps" },
10
+ },
11
+ required: ["appId"],
12
+ additionalProperties: false,
13
+ },
14
+ };
15
+ export async function handleSelectApp(auth, args) {
16
+ if (!args?.appId) {
17
+ return { content: [{ type: "text", text: "❌ appId is required." }] };
18
+ }
19
+ try {
20
+ const app = await getApp(auth, args.appId);
21
+ auth.setCurrentApp(app.appId, app.appUrl);
22
+ return { content: [{ type: "text", text: `🎯 Current app set.\n\n${formatApp(app)}` }] };
23
+ }
24
+ catch (error) {
25
+ return {
26
+ content: [{ type: "text", text: `❌ Failed to select app: ${error instanceof Error ? error.message : String(error)}` }],
27
+ };
28
+ }
29
+ }
@@ -0,0 +1,22 @@
1
+ import { AuthManager } from "../../client/authManager.js";
2
+ export declare const submitAppCredentialsTool: {
3
+ readonly name: "submit_app_credentials";
4
+ readonly description: string;
5
+ readonly inputSchema: {
6
+ readonly type: "object";
7
+ readonly properties: {
8
+ readonly appId: {
9
+ readonly type: "string";
10
+ readonly description: "App ID (defaults to the current app)";
11
+ };
12
+ readonly credentials: {
13
+ readonly type: "object";
14
+ readonly description: "Credential payload — shape depends on the app's login type (e.g. {\"username\":..., \"password\":...})";
15
+ };
16
+ };
17
+ readonly required: readonly ["credentials"];
18
+ readonly additionalProperties: false;
19
+ };
20
+ };
21
+ export declare function handleSubmitAppCredentials(auth: AuthManager, args: any): Promise<any>;
22
+ //# sourceMappingURL=submitAppCredentials.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"submitAppCredentials.d.ts","sourceRoot":"","sources":["../../../src/tools/protected/submitAppCredentials.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAG1D,eAAO,MAAM,wBAAwB;;;;;;;;;;;;;;;;;;CAiB3B,CAAC;AAEX,wBAAsB,0BAA0B,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAwB3F"}
@@ -0,0 +1,43 @@
1
+ import { submitAccountSetup } from "../../client/apiClient.js";
2
+ export const submitAppCredentialsTool = {
3
+ name: "submit_app_credentials",
4
+ description: "🔑 Supply login credentials when an app's onboarding is stuck in AWAITING_INPUT " +
5
+ "(POST /v1/apps/{appId}/account-setup). Only needed if check_app_status reports AWAITING_INPUT.",
6
+ inputSchema: {
7
+ type: "object",
8
+ properties: {
9
+ appId: { type: "string", description: "App ID (defaults to the current app)" },
10
+ credentials: {
11
+ type: "object",
12
+ description: "Credential payload — shape depends on the app's login type (e.g. {\"username\":..., \"password\":...})",
13
+ },
14
+ },
15
+ required: ["credentials"],
16
+ additionalProperties: false,
17
+ },
18
+ };
19
+ export async function handleSubmitAppCredentials(auth, args) {
20
+ const appId = args?.appId ?? auth.currentAppId;
21
+ if (!appId) {
22
+ return { content: [{ type: "text", text: "❌ No app selected. Run register_app or select_app first." }] };
23
+ }
24
+ if (!args?.credentials) {
25
+ return { content: [{ type: "text", text: "❌ credentials is required." }] };
26
+ }
27
+ try {
28
+ const task = await submitAccountSetup(auth, appId, args.credentials);
29
+ return {
30
+ content: [
31
+ {
32
+ type: "text",
33
+ text: `✅ Credentials submitted, onboarding resumed (task ${task.taskId ?? "n/a"}).\n\nRun check_app_status to continue polling.`,
34
+ },
35
+ ],
36
+ };
37
+ }
38
+ catch (error) {
39
+ return {
40
+ content: [{ type: "text", text: `❌ Failed to submit credentials: ${error instanceof Error ? error.message : String(error)}` }],
41
+ };
42
+ }
43
+ }
@@ -0,0 +1,22 @@
1
+ import { AuthManager } from "../../client/authManager.js";
2
+ export declare const suggestFixTool: {
3
+ readonly name: "suggest_fix";
4
+ readonly description: string;
5
+ readonly inputSchema: {
6
+ readonly type: "object";
7
+ readonly properties: {
8
+ readonly vulnerability_id: {
9
+ readonly type: "string";
10
+ readonly description: "Sequence number from get_vulnerabilities (e.g. '3') or raw vulnerability id";
11
+ };
12
+ readonly appId: {
13
+ readonly type: "string";
14
+ readonly description: "App ID (defaults to the current app)";
15
+ };
16
+ };
17
+ readonly required: readonly ["vulnerability_id"];
18
+ readonly additionalProperties: false;
19
+ };
20
+ };
21
+ export declare function handleSuggestFix(auth: AuthManager, args: any): Promise<any>;
22
+ //# sourceMappingURL=suggestFix.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"suggestFix.d.ts","sourceRoot":"","sources":["../../../src/tools/protected/suggestFix.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAG1D,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;CAejB,CAAC;AAEX,wBAAsB,gBAAgB,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAsBjF"}
@@ -0,0 +1,37 @@
1
+ import { buildFixPrompt } from "../../utils/promptBuilder.js";
2
+ export const suggestFixTool = {
3
+ name: "suggest_fix",
4
+ description: "🤖 Generate a fix prompt for a finding from the last get_vulnerabilities listing. Pass the " +
5
+ "sequence number shown there (e.g. \"3\") or the raw vulnerability id. Returns a self-contained " +
6
+ "prompt you (the calling assistant) can act on directly to write the fix.",
7
+ inputSchema: {
8
+ type: "object",
9
+ properties: {
10
+ vulnerability_id: { type: "string", description: "Sequence number from get_vulnerabilities (e.g. '3') or raw vulnerability id" },
11
+ appId: { type: "string", description: "App ID (defaults to the current app)" },
12
+ },
13
+ required: ["vulnerability_id"],
14
+ additionalProperties: false,
15
+ },
16
+ };
17
+ export async function handleSuggestFix(auth, args) {
18
+ const appId = args?.appId ?? auth.currentAppId;
19
+ if (!appId) {
20
+ return { content: [{ type: "text", text: "❌ No app selected. Run register_app or select_app first." }] };
21
+ }
22
+ if (!args?.vulnerability_id) {
23
+ return { content: [{ type: "text", text: "❌ vulnerability_id is required." }] };
24
+ }
25
+ const vuln = auth.getCachedVulnerability(appId, args.vulnerability_id);
26
+ if (!vuln) {
27
+ return {
28
+ content: [
29
+ {
30
+ type: "text",
31
+ text: `❌ Could not find "${args.vulnerability_id}" in the last get_vulnerabilities listing for this app. Run get_vulnerabilities first, then reference the # it shows.`,
32
+ },
33
+ ],
34
+ };
35
+ }
36
+ return { content: [{ type: "text", text: buildFixPrompt(auth.currentAppUrl ?? appId, vuln) }] };
37
+ }
@@ -0,0 +1,62 @@
1
+ import { AuthManager } from "../../client/authManager.js";
2
+ export declare const dismissVulnerabilityTool: {
3
+ readonly name: "dismiss_vulnerability";
4
+ readonly description: "🙈 Dismiss a finding as a false positive / won't-fix (POST /v1/vulnerabilities/{id}/dismiss).";
5
+ readonly inputSchema: {
6
+ readonly type: "object";
7
+ readonly properties: {
8
+ readonly vulnerability_id: {
9
+ readonly type: "string";
10
+ readonly description: "Sequence number from get_vulnerabilities (e.g. '3') or raw vulnerability id";
11
+ };
12
+ readonly appId: {
13
+ readonly type: "string";
14
+ readonly description: "App ID (defaults to the current app)";
15
+ };
16
+ };
17
+ readonly required: readonly ["vulnerability_id"];
18
+ readonly additionalProperties: false;
19
+ };
20
+ };
21
+ export declare function handleDismissVulnerability(auth: AuthManager, args: any): Promise<any>;
22
+ export declare const reopenVulnerabilityTool: {
23
+ readonly name: "reopen_vulnerability";
24
+ readonly description: "🔁 Reopen a dismissed/fixed finding (POST /v1/vulnerabilities/{id}/reopen).";
25
+ readonly inputSchema: {
26
+ readonly type: "object";
27
+ readonly properties: {
28
+ readonly vulnerability_id: {
29
+ readonly type: "string";
30
+ readonly description: "Sequence number from get_vulnerabilities (e.g. '3') or raw vulnerability id";
31
+ };
32
+ readonly appId: {
33
+ readonly type: "string";
34
+ readonly description: "App ID (defaults to the current app)";
35
+ };
36
+ };
37
+ readonly required: readonly ["vulnerability_id"];
38
+ readonly additionalProperties: false;
39
+ };
40
+ };
41
+ export declare function handleReopenVulnerability(auth: AuthManager, args: any): Promise<any>;
42
+ export declare const markVulnerabilityFixedTool: {
43
+ readonly name: "mark_vulnerability_fixed";
44
+ readonly description: "✅ Mark a finding as fixed after applying the suggested fix (POST /v1/vulnerabilities/{id}/fix).";
45
+ readonly inputSchema: {
46
+ readonly type: "object";
47
+ readonly properties: {
48
+ readonly vulnerability_id: {
49
+ readonly type: "string";
50
+ readonly description: "Sequence number from get_vulnerabilities (e.g. '3') or raw vulnerability id";
51
+ };
52
+ readonly appId: {
53
+ readonly type: "string";
54
+ readonly description: "App ID (defaults to the current app)";
55
+ };
56
+ };
57
+ readonly required: readonly ["vulnerability_id"];
58
+ readonly additionalProperties: false;
59
+ };
60
+ };
61
+ export declare function handleMarkVulnerabilityFixed(auth: AuthManager, args: any): Promise<any>;
62
+ //# sourceMappingURL=vulnerabilityActions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vulnerabilityActions.d.ts","sourceRoot":"","sources":["../../../src/tools/protected/vulnerabilityActions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAoB1D,eAAO,MAAM,wBAAwB;;;;;;;;;;;;;;;;;;CAI3B,CAAC;AAEX,wBAAsB,0BAA0B,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAS3F;AAED,eAAO,MAAM,uBAAuB;;;;;;;;;;;;;;;;;;CAI1B,CAAC;AAEX,wBAAsB,yBAAyB,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAS1F;AAED,eAAO,MAAM,0BAA0B;;;;;;;;;;;;;;;;;;CAI7B,CAAC;AAEX,wBAAsB,4BAA4B,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAS7F"}
@@ -0,0 +1,69 @@
1
+ import { dismissVulnerability, fixVulnerability, reopenVulnerability } from "../../client/apiClient.js";
2
+ function resolveArgs(auth, args) {
3
+ const appId = args?.appId ?? auth.currentAppId;
4
+ if (!appId)
5
+ return { error: "No app selected. Run register_app or select_app first." };
6
+ if (!args?.vulnerability_id)
7
+ return { error: "vulnerability_id is required." };
8
+ return { appId, id: auth.resolveVulnerabilityId(appId, args.vulnerability_id) };
9
+ }
10
+ const idSchema = {
11
+ type: "object",
12
+ properties: {
13
+ vulnerability_id: { type: "string", description: "Sequence number from get_vulnerabilities (e.g. '3') or raw vulnerability id" },
14
+ appId: { type: "string", description: "App ID (defaults to the current app)" },
15
+ },
16
+ required: ["vulnerability_id"],
17
+ additionalProperties: false,
18
+ };
19
+ export const dismissVulnerabilityTool = {
20
+ name: "dismiss_vulnerability",
21
+ description: "🙈 Dismiss a finding as a false positive / won't-fix (POST /v1/vulnerabilities/{id}/dismiss).",
22
+ inputSchema: idSchema,
23
+ };
24
+ export async function handleDismissVulnerability(auth, args) {
25
+ const resolved = resolveArgs(auth, args);
26
+ if ("error" in resolved)
27
+ return { content: [{ type: "text", text: `❌ ${resolved.error}` }] };
28
+ try {
29
+ await dismissVulnerability(auth, resolved.id);
30
+ return { content: [{ type: "text", text: `🙈 Dismissed vulnerability ${resolved.id}.` }] };
31
+ }
32
+ catch (error) {
33
+ return { content: [{ type: "text", text: `❌ Failed to dismiss: ${error instanceof Error ? error.message : String(error)}` }] };
34
+ }
35
+ }
36
+ export const reopenVulnerabilityTool = {
37
+ name: "reopen_vulnerability",
38
+ description: "🔁 Reopen a dismissed/fixed finding (POST /v1/vulnerabilities/{id}/reopen).",
39
+ inputSchema: idSchema,
40
+ };
41
+ export async function handleReopenVulnerability(auth, args) {
42
+ const resolved = resolveArgs(auth, args);
43
+ if ("error" in resolved)
44
+ return { content: [{ type: "text", text: `❌ ${resolved.error}` }] };
45
+ try {
46
+ await reopenVulnerability(auth, resolved.id);
47
+ return { content: [{ type: "text", text: `🔁 Reopened vulnerability ${resolved.id}.` }] };
48
+ }
49
+ catch (error) {
50
+ return { content: [{ type: "text", text: `❌ Failed to reopen: ${error instanceof Error ? error.message : String(error)}` }] };
51
+ }
52
+ }
53
+ export const markVulnerabilityFixedTool = {
54
+ name: "mark_vulnerability_fixed",
55
+ description: "✅ Mark a finding as fixed after applying the suggested fix (POST /v1/vulnerabilities/{id}/fix).",
56
+ inputSchema: idSchema,
57
+ };
58
+ export async function handleMarkVulnerabilityFixed(auth, args) {
59
+ const resolved = resolveArgs(auth, args);
60
+ if ("error" in resolved)
61
+ return { content: [{ type: "text", text: `❌ ${resolved.error}` }] };
62
+ try {
63
+ await fixVulnerability(auth, resolved.id);
64
+ return { content: [{ type: "text", text: `✅ Marked vulnerability ${resolved.id} as fixed.` }] };
65
+ }
66
+ catch (error) {
67
+ return { content: [{ type: "text", text: `❌ Failed to mark fixed: ${error instanceof Error ? error.message : String(error)}` }] };
68
+ }
69
+ }
@@ -0,0 +1,12 @@
1
+ import { AuthManager } from "../../client/authManager.js";
2
+ export declare const authStatusTool: {
3
+ readonly name: "auth_status";
4
+ readonly description: "🔎 Check whether the server currently holds a valid PerfAI session.";
5
+ readonly inputSchema: {
6
+ readonly type: "object";
7
+ readonly properties: {};
8
+ readonly additionalProperties: false;
9
+ };
10
+ };
11
+ export declare function handleAuthStatus(auth: AuthManager): Promise<any>;
12
+ //# sourceMappingURL=authStatus.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authStatus.d.ts","sourceRoot":"","sources":["../../../src/tools/public/authStatus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAE1D,eAAO,MAAM,cAAc;;;;;;;;CAIjB,CAAC;AAEX,wBAAsB,gBAAgB,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,CAQtE"}
@@ -0,0 +1,13 @@
1
+ export const authStatusTool = {
2
+ name: "auth_status",
3
+ description: "🔎 Check whether the server currently holds a valid PerfAI session.",
4
+ inputSchema: { type: "object", properties: {}, additionalProperties: false },
5
+ };
6
+ export async function handleAuthStatus(auth) {
7
+ const text = auth.isAuthenticated()
8
+ ? `✅ Authenticated as ${auth.getAuthenticatedEmail() ?? `unknown (${auth.getAuthMethod()}-based session)`}` +
9
+ (auth.currentAppId ? `\n📱 Current app: ${auth.currentAppId}` : "") +
10
+ (auth.currentTaskId ? `\n🧪 Current task: ${auth.currentTaskId}` : "")
11
+ : `❌ Not authenticated. Run \`login\` (credentials from args, or PERFAI_EMAIL/PERFAI_PASSWORD env).`;
12
+ return { content: [{ type: "text", text }] };
13
+ }
@@ -0,0 +1,21 @@
1
+ import { AuthManager } from "../../client/authManager.js";
2
+ export declare const loginTool: {
3
+ readonly name: "login";
4
+ readonly description: string;
5
+ readonly inputSchema: {
6
+ readonly type: "object";
7
+ readonly properties: {
8
+ readonly email: {
9
+ readonly type: "string";
10
+ readonly description: "PerfAI account email (optional)";
11
+ };
12
+ readonly password: {
13
+ readonly type: "string";
14
+ readonly description: "PerfAI account password (optional)";
15
+ };
16
+ };
17
+ readonly additionalProperties: false;
18
+ };
19
+ };
20
+ export declare function handleLogin(auth: AuthManager, args: any): Promise<any>;
21
+ //# sourceMappingURL=login.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"login.d.ts","sourceRoot":"","sources":["../../../src/tools/public/login.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAG1D,eAAO,MAAM,SAAS;;;;;;;;;;;;;;;;;CAgBZ,CAAC;AAmNX,wBAAsB,WAAW,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAgG5E"}
@@ -0,0 +1,305 @@
1
+ import { createServer } from "node:http";
2
+ import { exec } from "node:child_process";
3
+ import { platform } from "node:os";
4
+ import { AUTH0_CLIENT_ID } from "../../config.js";
5
+ export const loginTool = {
6
+ name: "login",
7
+ description: "Authenticate with PerfAI. " +
8
+ "With no arguments: opens a browser for OAuth login (if configured) " +
9
+ "or falls back to a simple login form. " +
10
+ "You can also pass email/password directly, or rely on " +
11
+ "PERFAI_TOKEN / PERFAI_EMAIL+PERFAI_PASSWORD environment variables.",
12
+ inputSchema: {
13
+ type: "object",
14
+ properties: {
15
+ email: { type: "string", description: "PerfAI account email (optional)" },
16
+ password: { type: "string", description: "PerfAI account password (optional)" },
17
+ },
18
+ additionalProperties: false,
19
+ },
20
+ };
21
+ function openBrowser(url) {
22
+ const cmd = platform() === "win32" ? `start "" "${url}"`
23
+ : platform() === "darwin" ? `open "${url}"`
24
+ : `xdg-open "${url}"`;
25
+ exec(cmd, (err) => {
26
+ if (err)
27
+ console.error("Could not open browser:", err.message);
28
+ });
29
+ }
30
+ /* ------------------------------------------------------------------ */
31
+ /* Fallback: simple browser login form (when OAuth is not configured) */
32
+ /* ------------------------------------------------------------------ */
33
+ const LOGIN_PAGE_HTML = `<!DOCTYPE html>
34
+ <html lang="en">
35
+ <head>
36
+ <meta charset="UTF-8">
37
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
38
+ <title>PerfAI — Login</title>
39
+ <style>
40
+ * { margin: 0; padding: 0; box-sizing: border-box; }
41
+ body {
42
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
43
+ background: #f5f5f5;
44
+ display: flex; align-items: center; justify-content: center;
45
+ min-height: 100vh;
46
+ }
47
+ .card {
48
+ background: #fff; border-radius: 12px; padding: 40px;
49
+ box-shadow: 0 4px 24px rgba(0,0,0,0.1); width: 100%; max-width: 400px;
50
+ }
51
+ .logo { text-align: center; margin-bottom: 24px; }
52
+ .logo h1 { font-size: 24px; color: #111; }
53
+ .logo p { color: #666; font-size: 14px; margin-top: 4px; }
54
+ label { display: block; font-size: 14px; font-weight: 500; color: #333; margin-bottom: 6px; }
55
+ input {
56
+ width: 100%; padding: 10px 12px; border: 1px solid #ddd; border-radius: 8px;
57
+ font-size: 14px; margin-bottom: 16px; outline: none; transition: border-color 0.2s;
58
+ }
59
+ input:focus { border-color: #111; }
60
+ button {
61
+ width: 100%; padding: 12px; background: #111; color: #fff; border: none;
62
+ border-radius: 8px; font-size: 15px; font-weight: 600; cursor: pointer;
63
+ transition: background 0.2s;
64
+ }
65
+ button:hover { background: #333; }
66
+ button:disabled { background: #999; cursor: not-allowed; }
67
+ .error { color: #e53e3e; font-size: 13px; margin-bottom: 12px; display: none; }
68
+ .success {
69
+ text-align: center; color: #38a169; font-size: 16px; font-weight: 600;
70
+ display: none; margin-top: 16px;
71
+ }
72
+ </style>
73
+ </head>
74
+ <body>
75
+ <div class="card">
76
+ <div class="logo">
77
+ <h1>PerfAI</h1>
78
+ <p>Sign in with your cloud.perfai.ai account</p>
79
+ </div>
80
+ <form id="loginForm">
81
+ <label for="email">Email</label>
82
+ <input type="email" id="email" name="email" placeholder="you@example.com" required autofocus>
83
+ <label for="password">Password</label>
84
+ <input type="password" id="password" name="password" placeholder="Your password" required>
85
+ <div class="error" id="error"></div>
86
+ <button type="submit" id="btn">Sign in</button>
87
+ </form>
88
+ <div class="success" id="success">Authenticated! You can close this tab and return to your AI assistant.</div>
89
+ </div>
90
+ <script>
91
+ document.getElementById('loginForm').addEventListener('submit', async (e) => {
92
+ e.preventDefault();
93
+ const btn = document.getElementById('btn');
94
+ const err = document.getElementById('error');
95
+ btn.disabled = true; btn.textContent = 'Signing in...'; err.style.display = 'none';
96
+ try {
97
+ const res = await fetch('/auth', {
98
+ method: 'POST',
99
+ headers: { 'Content-Type': 'application/json' },
100
+ body: JSON.stringify({
101
+ email: document.getElementById('email').value,
102
+ password: document.getElementById('password').value,
103
+ }),
104
+ });
105
+ const data = await res.json();
106
+ if (data.ok) {
107
+ document.getElementById('loginForm').style.display = 'none';
108
+ document.getElementById('success').style.display = 'block';
109
+ } else {
110
+ err.textContent = data.message || 'Login failed'; err.style.display = 'block';
111
+ btn.disabled = false; btn.textContent = 'Sign in';
112
+ }
113
+ } catch (ex) {
114
+ err.textContent = 'Connection error'; err.style.display = 'block';
115
+ btn.disabled = false; btn.textContent = 'Sign in';
116
+ }
117
+ });
118
+ </script>
119
+ </body>
120
+ </html>`;
121
+ let activeBrowserServer = null;
122
+ let browserLoginPort = null;
123
+ function startBrowserLoginServer(auth) {
124
+ if (activeBrowserServer) {
125
+ return browserLoginPort;
126
+ }
127
+ const PORT_RANGE_START = 18200;
128
+ const PORT_RANGE_END = 18300;
129
+ const httpServer = createServer(async (req, res) => {
130
+ if (req.method === "GET" && (req.url === "/" || req.url === "/login")) {
131
+ res.writeHead(200, { "Content-Type": "text/html" });
132
+ res.end(LOGIN_PAGE_HTML);
133
+ return;
134
+ }
135
+ if (req.method === "POST" && req.url === "/auth") {
136
+ let body = "";
137
+ req.on("data", (chunk) => { body += chunk; });
138
+ req.on("end", async () => {
139
+ try {
140
+ const { email, password } = JSON.parse(body);
141
+ await auth.login(email, password);
142
+ res.writeHead(200, { "Content-Type": "application/json" });
143
+ res.end(JSON.stringify({ ok: true }));
144
+ console.error(`Browser login successful: ${email}`);
145
+ setTimeout(() => {
146
+ activeBrowserServer?.close();
147
+ activeBrowserServer = null;
148
+ browserLoginPort = null;
149
+ }, 2000);
150
+ }
151
+ catch (err) {
152
+ res.writeHead(401, { "Content-Type": "application/json" });
153
+ res.end(JSON.stringify({
154
+ ok: false,
155
+ message: err instanceof Error ? err.message : "Login failed",
156
+ }));
157
+ }
158
+ });
159
+ return;
160
+ }
161
+ res.writeHead(404);
162
+ res.end("Not found");
163
+ });
164
+ setTimeout(() => {
165
+ if (activeBrowserServer === httpServer) {
166
+ httpServer.close();
167
+ activeBrowserServer = null;
168
+ browserLoginPort = null;
169
+ console.error("Browser login server timed out (5 min).");
170
+ }
171
+ }, 5 * 60 * 1000);
172
+ function tryListen(port) {
173
+ httpServer.listen(port, "127.0.0.1", () => {
174
+ activeBrowserServer = httpServer;
175
+ browserLoginPort = port;
176
+ const url = `http://127.0.0.1:${port}/login`;
177
+ console.error(`Browser login server started at ${url}`);
178
+ openBrowser(url);
179
+ });
180
+ httpServer.on("error", (err) => {
181
+ if (err.code === "EADDRINUSE" && port < PORT_RANGE_END) {
182
+ tryListen(port + 1);
183
+ }
184
+ else {
185
+ console.error("Could not start browser login server:", err.message);
186
+ }
187
+ });
188
+ }
189
+ tryListen(PORT_RANGE_START);
190
+ return null;
191
+ }
192
+ /* ------------------------------------------------------------------ */
193
+ /* OAuth PKCE browser flow (primary when AUTH0_CLIENT_ID is set) */
194
+ /* ------------------------------------------------------------------ */
195
+ let oauthFlowInProgress = false;
196
+ async function startOAuthLogin(auth) {
197
+ if (oauthFlowInProgress)
198
+ return false;
199
+ oauthFlowInProgress = true;
200
+ try {
201
+ const { startOAuthBrowserFlow } = await import("../../auth/oauthFlow.js");
202
+ const tokens = await startOAuthBrowserFlow();
203
+ auth.setToken(tokens.idToken, tokens.email, "oauth");
204
+ console.error(`OAuth login successful: ${tokens.email ?? "unknown"}`);
205
+ return true;
206
+ }
207
+ catch (err) {
208
+ console.error("OAuth flow failed:", err instanceof Error ? err.message : err);
209
+ return false;
210
+ }
211
+ finally {
212
+ oauthFlowInProgress = false;
213
+ }
214
+ }
215
+ /* ------------------------------------------------------------------ */
216
+ /* Tool handler */
217
+ /* ------------------------------------------------------------------ */
218
+ export async function handleLogin(auth, args) {
219
+ // Already authenticated
220
+ if (auth.isAuthenticated() && !args?.email) {
221
+ return {
222
+ content: [
223
+ {
224
+ type: "text",
225
+ text: `Already authenticated as ${auth.getAuthenticatedEmail()} ` +
226
+ `(via ${auth.getAuthMethod()}). Run logout first to switch accounts.`,
227
+ },
228
+ ],
229
+ };
230
+ }
231
+ // Explicit email/password provided
232
+ if (args?.email && args?.password) {
233
+ try {
234
+ await auth.login(args.email, args.password);
235
+ return {
236
+ content: [
237
+ {
238
+ type: "text",
239
+ text: `Authenticated as ${auth.getAuthenticatedEmail()}.\n\n` +
240
+ `Next: register_app {"appUrl": "https://your-app.example.com"} to onboard an app.`,
241
+ },
242
+ ],
243
+ };
244
+ }
245
+ catch (error) {
246
+ return {
247
+ content: [{ type: "text", text: `Login failed: ${error instanceof Error ? error.message : String(error)}` }],
248
+ };
249
+ }
250
+ }
251
+ // Env credentials (PERFAI_EMAIL + PERFAI_PASSWORD)
252
+ if (auth.hasCredentials()) {
253
+ try {
254
+ await auth.login();
255
+ return {
256
+ content: [
257
+ {
258
+ type: "text",
259
+ text: `Authenticated as ${auth.getAuthenticatedEmail()}.\n\n` +
260
+ `Next: register_app {"appUrl": "https://your-app.example.com"} to onboard an app.`,
261
+ },
262
+ ],
263
+ };
264
+ }
265
+ catch {
266
+ // Fall through to browser login
267
+ }
268
+ }
269
+ // OAuth PKCE browser flow (primary — when AUTH0_CLIENT_ID is configured)
270
+ if (AUTH0_CLIENT_ID) {
271
+ // Non-blocking: start the OAuth flow in the background
272
+ startOAuthLogin(auth).then((success) => {
273
+ if (!success) {
274
+ console.error("OAuth login did not complete. User can retry with 'login'.");
275
+ }
276
+ });
277
+ return {
278
+ content: [
279
+ {
280
+ type: "text",
281
+ text: "A browser window has been opened for PerfAI OAuth login.\n\n" +
282
+ "**Sign in with your cloud.perfai.ai account in the browser.**\n\n" +
283
+ "Once you see \"Authenticated!\" in the browser, come back here and call " +
284
+ "the `login` tool again to confirm.\n\n" +
285
+ "Alternatively, set `PERFAI_TOKEN` in your MCP server config with a token " +
286
+ "copied from the PerfAI console.",
287
+ },
288
+ ],
289
+ };
290
+ }
291
+ // Fallback: simple browser login form (no OAuth configured)
292
+ startBrowserLoginServer(auth);
293
+ return {
294
+ content: [
295
+ {
296
+ type: "text",
297
+ text: "A browser window has been opened for PerfAI login.\n\n" +
298
+ "**Please sign in with your cloud.perfai.ai credentials in the browser.**\n\n" +
299
+ "Once you see \"Authenticated!\" in the browser, come back here and call the `login` tool again to confirm.\n\n" +
300
+ "Tip: For a seamless experience, set `AUTH0_CLIENT_ID` in your server config to enable OAuth PKCE login, " +
301
+ "or set `PERFAI_TOKEN` with a token from the PerfAI console.",
302
+ },
303
+ ],
304
+ };
305
+ }