Programmatic Authentication Guide

This guide explains how to authenticate with mcp-trino programmatically using tokens obtained from mcp-remote's OAuth flow. This is useful for building automated agents, scripts, or applications that need to interact with the MCP server without interactive authentication.

Overview

┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐
│  1. OAuth Flow  │ ──► │ 2. Extract Token│ ──► │ 3. Use in Code  │
│  (mcp-remote)   │     │ (~/.mcp-auth/)  │     │ (Agent SDK)     │
└─────────────────┘     └─────────────────┘     └─────────────────┘

Prerequisites

  • Access to an mcp-trino server with OAuth enabled

  • Node.js 18+ (for Claude Agent SDK)

  • jq command-line tool (optional, for token extraction)

Step 1: Authenticate with mcp-remote

Using Claude Desktop or Cursor

Add the MCP server to your configuration:

Claude Desktop (~/Library/Application Support/Claude/claude_desktop_config.json):

When you first use the MCP server, a browser window will open for OAuth authentication.

Direct Authentication

This opens a browser for OAuth login and stores tokens locally.

Step 2: Extract the Token

Token Storage Location

mcp-remote stores credentials in:

Token File Format

Extract via Command Line

Helper Script

Step 3: Use the Token

Verify with curl

Claude Agent SDK (TypeScript)

Run with:

Reading Token from File (TypeScript)

Python Example

Token Expiration

OAuth tokens typically expire in 1 hour (expires_in: 3599 seconds).

Options for handling expiration:

  1. Re-authenticate manually: Run npx mcp-remote <server-url> again

  2. Client Credentials flow: For fully automated systems without user interaction, use OAuth Client Credentials (see OAuth Documentation)

  3. Check expiration in code: Decode the JWT and check the exp claim

Note: mcp-remote tokens may not include a refresh_token depending on the OAuth provider configuration. If no refresh token is available, re-authentication is required when the access token expires.

Troubleshooting

Error
Cause
Solution

No auth directory found

Never authenticated

Run npx mcp-remote <url>

401 Unauthorized

Token expired

Re-authenticate with mcp-remote

Invalid session ID

Missing session init

SDK handles this; for raw HTTP, call initialize first

Security Notes

  • Tokens are stored in plaintext in ~/.mcp-auth/

  • Set appropriate file permissions: chmod 700 ~/.mcp-auth

  • Never commit tokens to git

  • Tokens expire in ~1 hour; implement refresh for long-running apps

Last updated

Was this helpful?