← All projects

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.

Released 2026

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.

11 Live MCP tools
Δ Θ Γ V Option Greeks per contract
2 Transports (stdio + HTTP)

Architecture

Request lifecycle

  1. 01

    MCP Client

    Claude, Copilot, or any MCP host calls a tool over stdio or HTTP.

    ClaudeCopilot
  2. 02

    FastMCP

    Tool dispatched through FastMCP with a typed signature and a shared app context.

    typed toolsapp context
  3. 03

    Config

    Credentials load from env or INI, secrets masked in logs, sandbox stub if DHAN_FAKE is set.

    env / INIsecret masking
  4. 04

    DhanHQ SDK v2

    Official SDK call routed to the right Dhan REST endpoint for the requested data.

    dhanhq 2.0REST
  5. 05

    Dhan REST API

    Live NSE and BSE data, holdings, positions, or a full option chain comes back.

    NSE / BSEoption chain
  6. 06

    Normalize

    Failure payloads parsed, DH-1111 treated as empty, clean JSON returned to the model.

    DH-1111 emptyclean JSON

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

11 live tools · 1 config resource

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

Python 3.11FastMCP (mcp[cli] 1.14)DhanHQ SDK v2Pydantic v2Model Context Protocol

infra

stdio transportStreamable-HTTP transportINI / env config

tools

DhanHQ REST APINSE / BSE instrumentsOption chain + GreeksDHAN_FAKE sandbox stub

Key highlights

Proof points

  1. 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.

  2. 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.

  3. 03

    The option chain tool returns full Greeks (Delta, Theta, Gamma, Vega) and respects Dhan's one request per three seconds options limit.

  4. 04

    Two transports from the same code: stdio for local hosts like Claude Desktop and Copilot, and streamable-HTTP for running the server remotely.

  5. 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.

  6. 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

MCPFastMCPDhanHQ APIPythonOptions dataAPI integration