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.
| Collection | Credential Type Stored | At-Rest Protection | TTL Policy |
|---|---|---|---|
| McpAccessToken | OAuth access tokens and PATs | SHA-256 hex digest (tokenHash); plaintext never stored | 1h (OAuth) or 7d / 30d / 90d (PAT) |
| McpRefreshToken | OAuth rotating refresh tokens | SHA-256 hex digest (tokenHash); familyId linked | 30d sliding TTL |
| McpOAuthCode | Single-use authorization codes | SHA-256 hex digest (codeHash); consumedAt flag | 60s TTL index |
| McpOAuthTransaction | In-flight OAuth flow & Discord tokens | SHA-256 cookie/CSRF hashes + AES-256-GCM Discord tokens | 10m TTL index |
| McpOAuthGrant | Active OAuth/PAT grants & Discord tokens | AES-256-GCM encrypted Discord access/refresh tokens | Revoked 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.
{
"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.
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.