home / mcp / dhanhq trading assistant mcp server
Provides MCP-based access to DhanHQ trading actions, including orders, portfolio, and account insights via local tools.
Configuration
View docs{
"mcpServers": {
"mayankthole-dhan-mcp-trades": {
"command": "python",
"args": [
"-m",
"mcp.server.cli",
"dev",
"order_placement_tool.py"
],
"env": {
"DHAN_CLIENT_ID": "YOUR_CLIENT_ID",
"DHAN_ACCESS_TOKEN": "YOUR_ACCESS_TOKEN",
"DHAN_API_BASE_URL": "https://api.dhan.co/v2"
}
}
}
}The DhanHQ Trading Assistant uses Model Context Protocol (MCP) to let you interact with your DhanHQ trading account through natural language commands. By running small, focused MCP tools locally, you can place orders, view holdings, check balances, and analyze your portfolio without leaving your workflow. This setup enables rapid trading actions, contextual guidance, and automated workflows that align with your trading strategy.
You interact with the MCP server through a local client that issues commands to the available MCP tools. You can place regular orders, create super orders with targets and stop-loss, check your balance, view holdings, and run portfolio analyses. The system also supports after-market orders and access to order history and trading activity. For example, you can tell the client to buy a stock at market price, schedule an order for tomorrow, or analyze your monthly performance.
Prerequisites you need to prepare before running the MCP server and tools are listed here.
# Prerequisites
- Python 3.9 or higher
- A DhanHQ trading account
- DhanHQ API credentials (client ID and access token)
# Install dependencies and set up the project
git clone https://github.com/yourusername/dhan-broker-mcp-trades.git
cd dhan-broker-mcp-trades
# Install Python dependencies
pip install -r requirements.txt
# Configure credentials (example shown; replace with your actual values)
cat > config.py <<'PY'
DHAN_CLIENT_ID = "your_client_id_here"
DHAN_ACCESS_TOKEN = "your_access_token_here"
DHAN_API_BASE_URL = "https://api.dhan.co/v2"
PY
# Populate stocks (example structure)
cat > stocks.json <<'JSON'
{
"companies": [
{
"stock_code": "1333",
"company_name": "HDFC Bank Ltd.",
"stock_name": "HDFCBANK",
"description": "Description of the company..."
}
]
}
JSONMake sure your stocks.json is populated with the stocks you want to trade so the tools can map names to security IDs.
The MCP CLI is used to run each tool. You will typically start with the order placement workflow, portfolio checks, or balance analysis as you plan trades.
To run a specific tool, use the MCP CLI with the dev mode and the target tool file. The following commands start the order placement workflow and the portfolio server locally.
# To run the order placement tool
python -m mcp.server.cli dev order_placement_tool.py
# To run the portfolio server
python -m mcp.server.cli dev portfolio_server.py
# To run other tools
python -m mcp.server.cli dev <tool_filename>.pyThese examples show how you can perform common actions using MCP commands. You can adapt them to your trading routine.
Handles basic order placement (market and limit orders) for buying and selling stocks by name.
Manages super orders with target and stop-loss limits that can be absolute or percentage-based.
Places orders outside market hours to be executed on the next trading day.
Retrieves fund information and helps calculate margin requirements.
Retrieves holdings and positions and supports conversion between product types.
Calculates margin requirements for potential trades.
Provides access to order history and trade book, with cancellation options.
Main interface for portfolio management.