authenticate
Generate a URL to authenticate with Google Tasks, enabling direct task management through the MCP server integration.
Instructions
Get URL to authenticate with Google Tasks
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.ts:56-135 (handler)The handler function for the 'authenticate' tool. It creates a temporary local HTTP server to receive the OAuth callback from Google, generates and returns the authentication URL to visit.async () => { // Make sure any previous server is closed if (authServer) { try { authServer.close(); } catch (error) { console.error('Error closing existing auth server:', error); } authServer = null; } // Create the temporary HTTP server for OAuth callback authServer = http.createServer(async (req, res) => { try { // Parse the URL to get the authorization code const queryParams = url.parse(req.url || '', true).query; const code = queryParams.code; if (code) { console.error('✅ Authorization code received'); // Send success response with the code res.writeHead(200, { 'Content-Type': 'text/html' }); res.end(` <h1>Authorization Code Received</h1> <p>Please copy this code and use it with the 'set-auth-code' tool in Claude:</p> <div style="padding: 10px; background-color: #f0f0f0; border: 1px solid #ccc; margin: 20px 0;"> <code>${code}</code> </div> <p>You can close this window after copying the code.</p> `); // Close the server after a short delay setTimeout(() => { if (authServer) { authServer.close(); authServer = null; } }, 60000); // Keep the server alive for 1 minute } else { res.writeHead(400, { 'Content-Type': 'text/html' }); res.end(` <h1>Authentication Failed</h1> <p>No authorization code received.</p> <p>Please try again.</p> `); } } catch (error) { console.error('Error during authentication:', error); res.writeHead(500, { 'Content-Type': 'text/html' }); res.end(` <h1>Authentication Error</h1> <p>${error instanceof Error ? error.message : String(error)}</p> `); } }); // Start the server authServer.listen(REDIRECT_PORT, () => { console.error(`Temporary authentication server running at http://localhost:${REDIRECT_PORT}/`); console.error('Waiting for authentication...'); }); // Generate the auth URL const authUrl = oauth2Client.generateAuthUrl({ access_type: 'offline', scope: SCOPES, // Force approval prompt to always get a refresh token prompt: 'consent' }); return { content: [ { type: "text", text: `Please visit this URL to authenticate with Google Tasks:\n\n${authUrl}\n\nAfter authenticating, you'll receive a code. Use the 'set-auth-code' tool with that code.`, }, ], }; }
- src/index.ts:52-136 (registration)Registers the 'authenticate' tool using server.tool with name, description, empty input schema, and the handler function.server.tool( "authenticate", "Get URL to authenticate with Google Tasks", {}, async () => { // Make sure any previous server is closed if (authServer) { try { authServer.close(); } catch (error) { console.error('Error closing existing auth server:', error); } authServer = null; } // Create the temporary HTTP server for OAuth callback authServer = http.createServer(async (req, res) => { try { // Parse the URL to get the authorization code const queryParams = url.parse(req.url || '', true).query; const code = queryParams.code; if (code) { console.error('✅ Authorization code received'); // Send success response with the code res.writeHead(200, { 'Content-Type': 'text/html' }); res.end(` <h1>Authorization Code Received</h1> <p>Please copy this code and use it with the 'set-auth-code' tool in Claude:</p> <div style="padding: 10px; background-color: #f0f0f0; border: 1px solid #ccc; margin: 20px 0;"> <code>${code}</code> </div> <p>You can close this window after copying the code.</p> `); // Close the server after a short delay setTimeout(() => { if (authServer) { authServer.close(); authServer = null; } }, 60000); // Keep the server alive for 1 minute } else { res.writeHead(400, { 'Content-Type': 'text/html' }); res.end(` <h1>Authentication Failed</h1> <p>No authorization code received.</p> <p>Please try again.</p> `); } } catch (error) { console.error('Error during authentication:', error); res.writeHead(500, { 'Content-Type': 'text/html' }); res.end(` <h1>Authentication Error</h1> <p>${error instanceof Error ? error.message : String(error)}</p> `); } }); // Start the server authServer.listen(REDIRECT_PORT, () => { console.error(`Temporary authentication server running at http://localhost:${REDIRECT_PORT}/`); console.error('Waiting for authentication...'); }); // Generate the auth URL const authUrl = oauth2Client.generateAuthUrl({ access_type: 'offline', scope: SCOPES, // Force approval prompt to always get a refresh token prompt: 'consent' }); return { content: [ { type: "text", text: `Please visit this URL to authenticate with Google Tasks:\n\n${authUrl}\n\nAfter authenticating, you'll receive a code. Use the 'set-auth-code' tool with that code.`, }, ], }; } );
- src/index.ts:55-55 (schema)Empty input schema for the 'authenticate' tool (no parameters required).{},
- src/index.ts:47-49 (helper)Helper function used by other tools to check authentication status before executing Google Tasks operations.function isAuthenticated() { return credentials !== null; }