2026 · Model Context Protocol server
Dhan MCP Server
A Model Context Protocol server that lets Claude and Copilot query my live Dhan broker account and Indian market data in plain English.
Why I built this
I wanted my AI assistants to talk to my actual broker account, not a toy sandbox. So I wrapped Dhan's official SDK in a Model Context Protocol server, which means Claude or Copilot can pull my holdings, scan an option chain, or fetch historical data just by being asked in plain English. I deliberately left order placement commented out: the tools are written and tested, but a model that can read my portfolio is useful, while a model that can place orders on its own is a liability I did not want by default. Turning trading on is a conscious choice I make, not something that ships enabled.
Architecture
Request lifecycle
-
01
MCP Client
Claude, Copilot, or any MCP host calls a tool over stdio or HTTP.
-
02
FastMCP
Tool dispatched through FastMCP with a typed signature and a shared app context.
-
03
Config
Credentials load from env or INI, secrets masked in logs, sandbox stub if DHAN_FAKE is set.
-
04
DhanHQ SDK v2
Official SDK call routed to the right Dhan REST endpoint for the requested data.
-
05
Dhan REST API
Live NSE and BSE data, holdings, positions, or a full option chain comes back.
-
06
Normalize
Failure payloads parsed, DH-1111 treated as empty, clean JSON returned to the model.
Tool surface
- Portfolio · holdings, positions, orders, order by id
- Market data · OHLC, intraday minute, historical daily
- Options · chain with Greeks, expiry list, contract lookup
- Discovery · security id search across NSE and BSE
Order placement, off by default
place_order and cancel_order are fully implemented but left commented out in the server. The running service is read-only by design, so an LLM cannot move real money by accident. Enabling live trading is a deliberate one-line change, not the default.
Config and safety
- Credentials · env vars or INI, never hardcoded
- Secret masking · tokens shown as abc***xyz in logs
- Sandbox stub · DHAN_FAKE swaps in a mock client
- Graceful empty · DH-1111 returns empty data, not an error
- Rate aware · respects the 1 request / 3s options limit
Two transports
stdio for local hosts like Claude Desktop and Copilot, and streamable-HTTP for running the server remotely. Same tool surface either way, selected without touching tool code.
Tech stack
Technologies used
core
infra
tools
Key highlights
Proof points
- 01
Eleven live MCP tools cover portfolio (holdings, positions, orders), market data (OHLC, intraday, historical), options (chain with Greeks, expiries, contract lookup), and instrument discovery across NSE and BSE.
- 02
Order placement and cancellation are fully implemented but left commented out, so an LLM connected to the server is read-only by default and cannot move real money by accident.
- 03
The option chain tool returns full Greeks (Delta, Theta, Gamma, Vega) and respects Dhan's one request per three seconds options limit.
- 04
Two transports from the same code: stdio for local hosts like Claude Desktop and Copilot, and streamable-HTTP for running the server remotely.
- 05
Credentials load from environment or an INI file with secret masking in logs, and a DHAN_FAKE sandbox stub lets me develop without live access.
- 06
Feeds the same DhanHQ market data that the Rust hot path in ATIS consumes, so the broker integration is shared across my trading work.
Focus areas
Explore the work