|
| 1 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 2 | +import { env } from "std-env"; |
| 3 | +import { CliApiClient } from "../apiClient.js"; |
| 4 | +import { CLOUD_API_URL } from "../consts.js"; |
| 5 | +import { readAuthConfigProfile, writeAuthConfigProfile } from "../utilities/configFiles.js"; |
| 6 | +import { |
| 7 | + isPersonalAccessToken, |
| 8 | + NotPersonalAccessTokenError, |
| 9 | +} from "../utilities/isPersonalAccessToken.js"; |
| 10 | +import { LoginResult } from "../utilities/session.js"; |
| 11 | +import { getPersonalAccessToken } from "../commands/login.js"; |
| 12 | +import open from "open"; |
| 13 | +import pRetry from "p-retry"; |
| 14 | +import { McpContext } from "./context.js"; |
| 15 | + |
| 16 | +export type McpAuthOptions = { |
| 17 | + server: McpServer; |
| 18 | + context: McpContext; |
| 19 | + defaultApiUrl?: string; |
| 20 | + profile?: string; |
| 21 | +}; |
| 22 | + |
| 23 | +export async function mcpAuth(options: McpAuthOptions): Promise<LoginResult> { |
| 24 | + const opts = { |
| 25 | + defaultApiUrl: CLOUD_API_URL, |
| 26 | + ...options, |
| 27 | + }; |
| 28 | + |
| 29 | + const accessTokenFromEnv = env.TRIGGER_ACCESS_TOKEN; |
| 30 | + |
| 31 | + if (accessTokenFromEnv) { |
| 32 | + if (!isPersonalAccessToken(accessTokenFromEnv)) { |
| 33 | + throw new NotPersonalAccessTokenError( |
| 34 | + "Your TRIGGER_ACCESS_TOKEN is not a Personal Access Token, they start with 'tr_pat_'. You can generate one here: https://cloud.trigger.dev/account/tokens" |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + const auth = { |
| 39 | + accessToken: accessTokenFromEnv, |
| 40 | + apiUrl: env.TRIGGER_API_URL ?? opts.defaultApiUrl ?? CLOUD_API_URL, |
| 41 | + }; |
| 42 | + |
| 43 | + const apiClient = new CliApiClient(auth.apiUrl, auth.accessToken); |
| 44 | + const userData = await apiClient.whoAmI(); |
| 45 | + |
| 46 | + if (!userData.success) { |
| 47 | + throw new Error(userData.error); |
| 48 | + } |
| 49 | + |
| 50 | + return { |
| 51 | + ok: true as const, |
| 52 | + profile: options?.profile ?? "default", |
| 53 | + userId: userData.data.userId, |
| 54 | + email: userData.data.email, |
| 55 | + dashboardUrl: userData.data.dashboardUrl, |
| 56 | + auth: { |
| 57 | + accessToken: auth.accessToken, |
| 58 | + apiUrl: auth.apiUrl, |
| 59 | + }, |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + const authConfig = readAuthConfigProfile(options?.profile); |
| 64 | + |
| 65 | + if (authConfig && authConfig.accessToken) { |
| 66 | + const apiClient = new CliApiClient( |
| 67 | + authConfig.apiUrl ?? opts.defaultApiUrl, |
| 68 | + authConfig.accessToken |
| 69 | + ); |
| 70 | + const userData = await apiClient.whoAmI(); |
| 71 | + |
| 72 | + if (!userData.success) { |
| 73 | + throw new Error(userData.error); |
| 74 | + } |
| 75 | + |
| 76 | + return { |
| 77 | + ok: true as const, |
| 78 | + profile: options?.profile ?? "default", |
| 79 | + userId: userData.data.userId, |
| 80 | + email: userData.data.email, |
| 81 | + dashboardUrl: userData.data.dashboardUrl, |
| 82 | + auth: { |
| 83 | + accessToken: authConfig.accessToken, |
| 84 | + apiUrl: authConfig.apiUrl ?? opts.defaultApiUrl, |
| 85 | + }, |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + const apiClient = new CliApiClient(authConfig?.apiUrl ?? opts.defaultApiUrl); |
| 90 | + |
| 91 | + //generate authorization code |
| 92 | + const authorizationCodeResult = await createAuthorizationCode(apiClient); |
| 93 | + |
| 94 | + // Only elicitInput if the client has the elicitation capability |
| 95 | + |
| 96 | + // Elicit the user to visit the authorization code URL |
| 97 | + const allowLogin = await askForLoginPermission(opts.server, authorizationCodeResult.url); |
| 98 | + |
| 99 | + if (!allowLogin) { |
| 100 | + return { |
| 101 | + ok: false as const, |
| 102 | + error: "User did not allow login", |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + // Open the authorization code URL in the browser |
| 107 | + await open(authorizationCodeResult.url); |
| 108 | + |
| 109 | + // Poll for the personal access token |
| 110 | + const indexResult = await pRetry( |
| 111 | + () => getPersonalAccessToken(apiClient, authorizationCodeResult.authorizationCode), |
| 112 | + { |
| 113 | + //this means we're polling, same distance between each attempt |
| 114 | + factor: 1, |
| 115 | + retries: 60, |
| 116 | + minTimeout: 1000, |
| 117 | + } |
| 118 | + ); |
| 119 | + |
| 120 | + writeAuthConfigProfile( |
| 121 | + { accessToken: indexResult.token, apiUrl: opts.defaultApiUrl }, |
| 122 | + options?.profile |
| 123 | + ); |
| 124 | + |
| 125 | + const client = new CliApiClient(opts.defaultApiUrl, indexResult.token); |
| 126 | + const userData = await client.whoAmI(); |
| 127 | + |
| 128 | + if (!userData.success) { |
| 129 | + throw new Error(userData.error); |
| 130 | + } |
| 131 | + |
| 132 | + return { |
| 133 | + ok: true as const, |
| 134 | + profile: options?.profile ?? "default", |
| 135 | + userId: userData.data.userId, |
| 136 | + email: userData.data.email, |
| 137 | + dashboardUrl: userData.data.dashboardUrl, |
| 138 | + auth: { |
| 139 | + accessToken: indexResult.token, |
| 140 | + apiUrl: opts.defaultApiUrl, |
| 141 | + }, |
| 142 | + }; |
| 143 | +} |
| 144 | + |
| 145 | +async function createAuthorizationCode(apiClient: CliApiClient) { |
| 146 | + const authorizationCodeResult = await apiClient.createAuthorizationCode(); |
| 147 | + |
| 148 | + if (!authorizationCodeResult.success) { |
| 149 | + throw new Error(`Failed to create authorization code\n${authorizationCodeResult.error}`); |
| 150 | + } |
| 151 | + |
| 152 | + return authorizationCodeResult.data; |
| 153 | +} |
| 154 | + |
| 155 | +async function askForLoginPermission(server: McpServer, authorizationCodeUrl: string) { |
| 156 | + const capabilities = server.server.getClientCapabilities(); |
| 157 | + |
| 158 | + if (typeof capabilities?.elicitation !== "object") { |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + const result = await server.server.elicitInput({ |
| 163 | + message: `You are not currently logged in. Would you like to login now? We'll automatically open the authorization code URL (${authorizationCodeUrl}) in your browser.`, |
| 164 | + requestedSchema: { |
| 165 | + type: "object", |
| 166 | + properties: { |
| 167 | + allowLogin: { |
| 168 | + type: "boolean", |
| 169 | + default: false, |
| 170 | + title: "Allow Login", |
| 171 | + description: "Whether to allow the user to login", |
| 172 | + }, |
| 173 | + }, |
| 174 | + required: ["allowLogin"], |
| 175 | + }, |
| 176 | + }); |
| 177 | + |
| 178 | + return result.action === "accept" && result.content?.allowLogin; |
| 179 | +} |
0 commit comments