Dhan MCP Server
A production-ready MCP server for algorithmic trading with Dhan broker API, providing market data, order management, portfolio tracking, and real-time trading capabilities.
README
Dhan MCP Server - Python Trading API Integration with Model Context Protocol
Production-ready MCP server for algorithmic trading with Dhan broker API - Complete integration with Dhan trading platform providing market data, order management, portfolio tracking, and real-time trading capabilities through the Model Context Protocol.
🚀 Overview
The Dhan MCP Server is a comprehensive Python-based Model Context Protocol (MCP) server that enables seamless integration with the Dhan trading platform. Ideal for algorithmic trading, automated trading systems, trading bots, and quantitative trading strategies in Indian stock markets.
Key Features
- Complete Trading Suite: 19 production-ready tools for order execution, position management, and market analysis
- Real-time Market Data: Live LTP, OHLC, market depth, and tick-by-tick data streaming
- Order Management: Place, modify, cancel orders with support for all order types (Market, Limit, Stop Loss, Cover, Bracket)
- Risk Management: Pre-trade margin calculation and position monitoring
- Historical Data: Access to intraday and daily historical data for backtesting
- Portfolio Tracking: Real-time positions, holdings, and P&L monitoring
- MCP Protocol: Standards-compliant Model Context Protocol implementation using FastMCP
📋 Installation
Prerequisites
- Python 3.8 or higher
- Dhan trading account with API access
- Dhan API credentials (Client ID and Access Token)
Quick Start
# Clone the repository
git clone https://github.com/vikkysarswat/dhan-mcp-server.git
cd dhan-mcp-server
# Install dependencies
pip install -r requirements.txt
# Set up environment variables
export DHAN_CLIENT_ID="your_client_id"
export DHAN_ACCESS_TOKEN="your_access_token"
# Run the MCP server
python -m dhan_mcp_server
Installation via pip
pip install dhan-mcp-server
Configuration
Create a .env file in the project root:
DHAN_CLIENT_ID=your_dhan_client_id
DHAN_ACCESS_TOKEN=your_dhan_access_token
📊 Complete Feature Matrix
Account Management (3 Tools)
| Tool | Functionality | Status |
|---|---|---|
get_profile() |
User profile and account information | ✅ Complete |
validate_token() |
Access token validation and expiry | ✅ Complete |
get_fund_limits() |
Account balance and margin information | ✅ Complete |
Trading Operations (4 Tools)
| Tool | Functionality | Status |
|---|---|---|
place_order({...}) |
Place new orders (all types & products) | ✅ Complete |
modify_order({...}) |
Modify pending orders | ✅ Complete |
cancel_order({orderId}) |
Cancel pending orders | ✅ Complete |
slice_order({...}) |
Handle large quantity orders | ✅ Complete |
Order & Trade Tracking (5 Tools)
| Tool | Functionality | Status |
|---|---|---|
get_orders() |
Current day's order book | ✅ Complete |
get_order_by_id({orderId}) |
Specific order details | ✅ Complete |
get_order_by_correlation_id({correlationId}) |
Find order by custom ID | ✅ Complete |
get_trades() |
Current day's executed trades | ✅ Complete |
get_trades_by_order_id({orderId}) |
Trades for specific order | ✅ Complete |
Position & Holdings Management (2 Tools)
| Tool | Functionality | Status |
|---|---|---|
get_positions() |
Real-time open positions | ✅ Complete |
get_holdings() |
Long-term holdings portfolio | ✅ Complete |
Risk Management (1 Tool)
| Tool | Functionality | Status |
|---|---|---|
calculate_margin({...}) |
Pre-trade margin calculation | ✅ Complete |
Market Data (3 Tools)
| Tool | Functionality | Status |
|---|---|---|
get_market_ltp({...}) |
Live last traded prices | ✅ Complete |
get_market_ohlc({...}) |
OHLC data with volume | ✅ Complete |
get_market_depth({...}) |
Full market depth & order book | ✅ Complete |
Historical Data & Analysis (3 Tools)
| Tool | Functionality | Status |
|---|---|---|
get_historical_data({...}) |
Daily OHLC historical data | ✅ Complete |
get_intraday_data({...}) |
Minute-level intraday data | ✅ Complete |
get_tick_data({...}) |
Tick-by-tick historical data | ✅ Complete |
🔧 Usage Examples
Place a Market Order
from dhan_mcp_server import DhanMCPServer
server = DhanMCPServer()
order = server.place_order(
security_id="1333", # Reliance
exchange="NSE",
transaction_type="BUY",
quantity=1,
order_type="MARKET",
product_type="INTRADAY"
)
Get Real-time Market Data
# Get LTP for multiple securities
ltp_data = server.get_market_ltp(
exchange="NSE",
security_ids=["1333", "11536"] # Reliance, TCS
)
# Get market depth
depth = server.get_market_depth(
exchange="NSE",
security_id="1333"
)
Retrieve Historical Data
import datetime
historical = server.get_historical_data(
security_id="1333",
exchange="NSE",
instrument="EQUITY",
from_date=datetime.date(2024, 1, 1),
to_date=datetime.date(2024, 12, 31)
)
🏗️ Architecture
Built with:
- FastMCP: High-performance MCP server framework
- Dhan Python SDK: Official Dhan trading API client
- Python AsyncIO: Asynchronous operations for real-time data
🔐 Security
- Never commit API credentials to version control
- Use environment variables or secure vaults for sensitive data
- Implement IP whitelisting on Dhan platform
- Enable 2FA on your Dhan account
📝 API Documentation
For detailed Dhan API documentation, visit:
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🔗 Related Projects
- FastMCP - MCP server framework
- Model Context Protocol - Protocol specification
- Dhan Trading Platform - Trading broker
📧 Support
For issues, questions, or feature requests, please open an issue on GitHub.
Keywords
algorithmic trading, trading bot, stock market API, Dhan API, MCP server, Model Context Protocol, Python trading, automated trading, quantitative trading, Indian stock market, NSE, BSE, equity trading, derivatives trading, market data API, real-time trading, trading automation, backtesting, trading strategy
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。