Skip to content
Thakorn S.

A production-ready MCP server with OAuth 2.1

Thai stock-market data for AI agents, behind proper authorization.

TSThakorn Swaengkit·Dec 6, 2025·1 min read
A Claude conversation asking for KBANK stock info, with get stock info and get stock price tool steps and a company summary table.
Fig. 1 · Claude.ai calling the server through a Custom Connector.
3tools for AI agents
2OAuth-protected transports
100 /minrate limit per IP
60 sTTL cache
ROLESolo builder
TIMELINEDec 2025

I built an MCP server for Thai stock data and connected it to Claude.ai as a Custom Connector. The tools were the small part. The authorization was the real work.

Why authorization is the hard part

A demo MCP server is easy to write. A server that a remote client can safely connect to needs real authorization, and that means implementing the OAuth 2.1 flow an MCP client expects rather than handing out a static key.

What the server does

It's Python 3.10 with FastMCP, reading SET data through yfinance with the .BK suffix. Prices are delayed 15–20 minutes. There are three tools:

  • get_stock_price
  • get_stock_info
  • search_stocks

Two transports are exposed, /mcp for Streamable HTTP and /sse for SSE. Both are OAuth-protected.

The OAuth 2.1 surface

These are the pieces the server implements:

PieceStandard
PKCERFC 7636
Dynamic client registrationRFC 7591
Audience validationRFC 8707
Access tokensJWT RS256
Refresh tokens30-day, rotating

The endpoints are /oauth/register, /oauth/authorize, /oauth/token and /oauth/revoke, plus the .well-known metadata and JWKS.

What makes it production-ready

  • A 60-second TTL cache, with asyncio
  • A rate limit of 100 requests per minute per IP
  • CORS, /health and /metrics
  • Structured JSON logs, security headers and a non-root container

Traffic goes through Cloudflare for SSL, then nginx on port 80, then FastMCP and uvicorn on port 8000.SIDENOTEThere are separate Docker Compose files for dev, test and prod.

SSLCloudflare
:80nginx
:8000FastMCP + uvicorn
DATAyfinance
Fig. 2 · The request path from the client to market data.
BUILT WITH
Python 3.10FastMCPOAuth 2.1 (PKCE, JWT RS256)yfinancenginxCloudflareDocker
TSThakorn SwaengkitRobotics & AI engineer in Bangkok. I lead software engineering at AltoTech and write about building for the physical world.
Next project · 03Smart IoT room on VolttronAll projects