trakt_complete_auth
Finalize Trakt.tv login by submitting the authorization code from the OAuth callback to authenticate access.
Instructions
Complete Trakt.tv authentication with authorization code
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | Authorization code from Trakt OAuth callback |
Implementation Reference
- src/trakt/mcp-functions.ts:104-144 (handler)The main handler for the trakt_complete_auth tool. Executes the OAuth token exchange by calling traktClient.exchangeCodeForToken(code), then retrieves the current user info and returns tokens and instructions.
async traktCompleteAuth(code: string): Promise<Record<string, unknown>> { if (!this.isInitialized) { return { success: false, error: 'Trakt client not initialized. Call trakt_authenticate first.' }; } try { const tokens = await this.traktClient.exchangeCodeForToken(code); const user = await this.traktClient.getCurrentUser(); return { success: true, user: { username: user.username, name: user.name, vip: user.vip }, tokens: { access_token: tokens.access_token, refresh_token: tokens.refresh_token, expires_in: tokens.expires_in, scope: tokens.scope, created_at: tokens.created_at }, message: 'Authentication successful! Add these to your environment config to persist across restarts:', env_config: `TRAKT_ACCESS_TOKEN=${tokens.access_token}\nTRAKT_REFRESH_TOKEN=${tokens.refresh_token}`, nextSteps: [ 'Add the above TRAKT_ACCESS_TOKEN and TRAKT_REFRESH_TOKEN to your MCP client env config or .env file', 'Use trakt_get_auth_status to verify authentication', 'Start syncing with trakt_sync_to_trakt' ] }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Token exchange failed' }; } } - src/trakt/tool-schemas.ts:17-27 (schema)Schema definition for trakt_complete_auth. Defines input schema requiring a 'code' string parameter (the authorization code from Trakt OAuth callback).
{ name: "trakt_complete_auth", description: "Complete Trakt.tv authentication with authorization code", inputSchema: { type: "object" as const, properties: { code: { type: "string", description: "Authorization code from Trakt OAuth callback" }, }, required: ["code"], }, }, - src/trakt/tool-registry.ts:16-18 (registration)Registration of the trakt_complete_auth tool in the Trakt tool registry. Maps the tool name to a call to traktFunctions.traktCompleteAuth(args.code).
registry.register("trakt_complete_auth", (args) => traktFunctions.traktCompleteAuth(args.code as string).then(wrapResponse) ); - src/trakt/client.ts:141-162 (helper)The TraktClient.exchangeCodeForToken() helper method that performs the actual HTTP POST to /oauth/token to exchange the authorization code for access and refresh tokens.
async exchangeCodeForToken(code: string): Promise<TraktTokens> { try { const response = await this.http.post<TraktTokens>('/oauth/token', { code, client_id: this.config.clientId, client_secret: this.config.clientSecret, redirect_uri: this.config.redirectUri, grant_type: 'authorization_code' }); const tokens = response.data; tokens.created_at = Math.floor(Date.now() / 1000); // Store tokens in config this.config.accessToken = tokens.access_token; this.config.refreshToken = tokens.refresh_token; return tokens; } catch (error) { throw new Error(`Token exchange failed: ${error}`); } } - src/trakt/types.ts:16-23 (helper)Type definition for TraktTokens, including access_token, refresh_token, expires_in, scope, and created_at fields.
export interface TraktTokens { access_token: string; refresh_token: string; expires_in: number; token_type: string; scope: string; created_at: number; }