Essay
·Stateless JWE Authentication for Serverless MCP Servers
How moving from stateful container sessions to JSON Web Encryption unlocks scale-to-zero Lambda for Model Context Protocol tools.
The Model Context Protocol (MCP) has transformed how large language models interact with external APIs, databases, and operational systems. However, most initial MCP server implementations assume long-running, stateful server processes or local stdio connections.
When bringing MCP tooling to enterprise production, running dedicated ECS containers 24/7 for dozens of internal tools quickly leads to wasted compute spend and unnecessary infrastructure complexity.
When architecting meta-mcp, our 24-tool TypeScript server connecting Claude to the Meta Marketing API, we transitioned from an always-on container architecture to scale-to-zero AWS Lambda functions behind HTTP API Gateway using stateless JSON Web Encryption (JWE).
The Limitation of Containerized OAuth
Our initial prototype ran on AWS ECS Fargate. When a user authenticated with Meta’s OAuth flow, the server stored access tokens in a backend session store and returned a stateful session cookie.
While standard for web apps, this model created significant operational hurdles for agent tooling:
- Compute Waste: Marketing teams use the MCP server heavily during campaign launches and reporting cycles, but it sits idle overnight and on weekends. Constant Fargate tasks incurred uninterrupted baseline costs.
- Session State Dependencies: Scaling container tasks required managing distributed Redis caches or database connections to validate incoming tool requests.
The Stateless JWE Architecture
To enable scale-to-zero execution on AWS Lambda, we removed the need for server-side session persistence by packing the user’s encrypted OAuth tokens directly into a stateless JSON Web Encryption (JWE) token.
Client (Claude) ──[ HTTP Request + JWE Bearer Token ]──> API Gateway ──> Lambda (meta-mcp)
│
Decrypt JWE with
Server Private Key
│
▼
Meta Marketing API Call
Key Engineering Decisions:
- Authenticated Encryption: The OAuth token payload is encrypted using compact JWE (AES-GCM encryption with server-managed keys rotated via AWS Secrets Manager). The client holds the encrypted blob, but cannot inspect or tamper with the contents.
- Cold-Start Optimization: By building a lightweight TypeScript bundle with minimal runtime dependencies, Lambda cold starts remain under 200 milliseconds, completely unnoticeable during conversational LLM tool execution.
- Keyless CI/CD Deployment: The entire stack is defined in AWS CDK and deployed via GitHub Actions using AWS OIDC federation, eliminating long-lived IAM keys from CI environments.
The Result
By eliminating stateful infrastructure, meta-mcp scales automatically to handle hundreds of concurrent tool calls during peak marketing workflows, drops to $0 in compute when idle, and requires zero database maintenance.