SecurityCryptography

Token Storage

Cryptographic storage architecture for SHA-256 hashed MCP tokens, AES-256-GCM encrypted Discord tokens, physical taicket_mcp isolation, and scoped PAT lifecycles.

Dedicated taicket_mcp database isolation

All authentication material, OAuth clients, temporary authorization transactions, setup preview records, and audit logs live in a dedicated MongoDB database configured via MCP_MONGODB_URI and MCP_DB_NAME (default: taicket_mcp). None of these collections exist in the main Taicket bot database or the nine dedicated module databases.

Separating authentication state into taicket_mcp limits blast radius if another service cluster credential is rotated or audited, allows independent backup retention policies, and supports dedicated MongoDB role-based privileges on mcp_audit_logs.

CollectionCredential Type StoredAt-Rest ProtectionTTL Policy
McpAccessTokenOAuth access tokens and PATsSHA-256 hex digest (tokenHash); plaintext never stored1h (OAuth) or 7d / 30d / 90d (PAT)
McpRefreshTokenOAuth rotating refresh tokensSHA-256 hex digest (tokenHash); familyId linked30d sliding TTL
McpOAuthCodeSingle-use authorization codesSHA-256 hex digest (codeHash); consumedAt flag60s TTL index
McpOAuthTransactionIn-flight OAuth flow & Discord tokensSHA-256 cookie/CSRF hashes + AES-256-GCM Discord tokens10m TTL index
McpOAuthGrantActive OAuth/PAT grants & Discord tokensAES-256-GCM encrypted Discord access/refresh tokensRevoked on user action or expiry

One-way SHA-256 hashing for MCP bearer tokens

Both OAuth 2.1 access tokens and Personal Access Tokens (prefixed tmcp_) are generated with 256 bits of CSPRNG entropy from crypto.randomBytes(32). Before writing to MongoDB, the server computes SHA-256 over the raw bearer string and persists only tokenHash, tokenPrefix, and tokenLast4.

Because MCP bearer tokens carry 256 bits of cryptographic entropy, SHA-256 is resistant to offline preimage or dictionary attacks while enabling O(1) indexed lookups in requireMcpBearerAuth on every request to /mcp. Even a full read snapshot of McpAccessToken cannot reconstruct a usable bearer token.

Redacted McpAccessToken document shape in taicket_mcpjson
{
  "tokenHash": "9e8b4f1c2a7d60e5b3f8c1d4a9e2b7f0c5d8a1e4b7c0d3f6a9b2c5e8f1a4d7b0",
  "tokenPrefix": "tmcp_",
  "tokenLast4": "9f2a",
  "tokenKind": "pat",
  "label": "Cline Staging Profile",
  "userId": "555652788592443392",
  "grantId": "grant_01JXYZ8A4C9M2N5P",
  "clientId": "pat-portal",
  "clientName": "Scoped Personal Access Token",
  "resource": "https://mcp.taicket.xyz/mcp",
  "scopes": ["mcp:read", "mcp:write"],
  "allowedGuildIds": ["123456789012345678"],
  "revokedAt": null,
  "expiresAt": "2026-07-01T12:00:00.000Z"
}

AES-256-GCM encryption for upstream Discord tokens

Taicket MCP stores upstream Discord OAuth access and refresh tokens solely to refresh manageable guild lists when Discord OAuth sessions are active. These tokens are never returned in MCP responses, HTML views, or logs, and are encrypted at rest with AES-256-GCM using MCP_TOKEN_ENCRYPTION_KEY.

  • Unique per-record IV:: Every encryption call generates a fresh 96-bit (12-byte) initialization vector using crypto.randomBytes(12). IVs are never reused across records or token updates.
  • Authenticated ciphertext:: AES-256-GCM produces a 128-bit authentication tag stored alongside the IV and ciphertext. Any bit flip in the database payload causes decryption to throw immediately.
  • Key validation at startup:: mcp/src/config/env.ts requires MCP_TOKEN_ENCRYPTION_KEY to be at least 32 characters and fails server boot if absent or malformed.
AES-256-GCM envelope encryption helpertypescript
import crypto from 'node:crypto';

export function encryptToken(plaintext: string, keyMaterial: string): string {
  const key = crypto.createHash('sha256').update(keyMaterial, 'utf8').digest();
  const iv = crypto.randomBytes(12);
  const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
  const ciphertext = Buffer.concat([cipher.update(plaintext, 'utf8'), cipher.final()]);
  const authTag = cipher.getAuthTag();
  return [iv.toString('base64url'), authTag.toString('base64url'), ciphertext.toString('base64url')].join('.');
}

Scoped Personal Access Token lifecycle policy

For clients without native OAuth 2.1 support (DeepSeek, GLM, Cherry Studio, Cline, Open WebUI, or headless automation), server managers issue Scoped Personal Access Tokens at https://mcp.taicket.xyz/account after authenticating via Discord OAuth.

Token format
tmcp_ prefix + 256-bit random token body
Mandatory expiry options
7 days, 30 days, or 90 days (non-expiring tokens are disabled)
Active token quota
Maximum 10 active (non-revoked, non-expired) PATs per Discord user
Display policy
Plaintext displayed exactly once on creation; only tokenPrefix and tokenLast4 shown afterward
Revocation options
Instant per-token revoke, per-client OAuth revoke, and one-click Revoke All

Identical security pipeline

Scoped PATs do not bypass any security guard. On every /mcp call they pass through the exact same SHA-256 lookup, resource check, allowedGuildIds filter, BotBlacklist check, live Discord RBAC check, 18-surface tier gate, rate limiter, and HMAC audit logger as OAuth 2.1 tokens.

Related documentation