Skip to main content
Glama
refreshTokenTool.jsβ€’2.72 kB
import { z } from 'zod'; import { zodToJsonSchema } from 'zod-to-json-schema'; import { BaseTool } from '../baseTool.js'; const RefreshTokenSchema = z.object({}); // No inputs β€” uses environment variables const ToolDefinition = { name: 'hubspot-refresh-token', description: ` πŸ”„ Refreshes the HubSpot OAuth access token using the refresh token from environment variables. 🎯 Purpose: - Use only when HubSpot API requests fail due to expired tokens. - Automatically refreshes and sets the active access token in process.env.PRIVATE_APP_ACCESS_TOKEN. - Not required for long-lived Private App tokens. πŸ›‘οΈ Guardrails: - Only use if using OAuth (i.e. REFRESH_TOKEN is present in environment). - Do not invoke more than once per session unless a 401 Unauthorized response is received from HubSpot API. `, inputSchema: zodToJsonSchema(RefreshTokenSchema), annotations: { title: 'Refresh HubSpot OAuth Token', readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false, }, }; export class RefreshTokenTool extends BaseTool { constructor() { super(RefreshTokenSchema, ToolDefinition); } async process() { const { CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN } = process.env; if (!CLIENT_ID || !CLIENT_SECRET || !REFRESH_TOKEN) { return { content: [ { type: 'text', text: '❌ Missing CLIENT_ID, CLIENT_SECRET, or REFRESH_TOKEN in environment.', }, ], isError: true, }; } try { const res = await fetch('https://api.hubapi.com/oauth/v1/token', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ grant_type: 'refresh_token', client_id: CLIENT_ID, client_secret: CLIENT_SECRET, refresh_token: REFRESH_TOKEN, }), }); const json = await res.json(); if (!res.ok) { throw new Error(json.message || 'Token refresh failed'); } process.env.PRIVATE_APP_ACCESS_TOKEN = json.access_token; return { content: [ { type: 'text', text: JSON.stringify({ access_token: json.access_token, expires_in: json.expires_in, refresh_token: json.refresh_token, }, null, 2), }, ], }; } catch (err) { return { content: [ { type: 'text', text: `❌ Token refresh failed: ${err instanceof Error ? err.message : String(err)}`, }, ], isError: true, }; } } }

Implementation Reference

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ajaystream/hubspot-mcp-custom'

If you have feedback or need assistance with the MCP directory API, please join our Discord server