Dhan MCP Server
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@Dhan MCP Serverplace a market order to buy 50 shares of HDFC Bank"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
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_serverInstallation via pip
pip install dhan-mcp-serverConfiguration
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 |
| User profile and account information | ✅ Complete |
| Access token validation and expiry | ✅ Complete |
| Account balance and margin information | ✅ Complete |
Trading Operations (4 Tools)
Tool | Functionality | Status |
| Place new orders (all types & products) | ✅ Complete |
| Modify pending orders | ✅ Complete |
| Cancel pending orders | ✅ Complete |
| Handle large quantity orders | ✅ Complete |
Order & Trade Tracking (5 Tools)
Tool | Functionality | Status |
| Current day's order book | ✅ Complete |
| Specific order details | ✅ Complete |
| Find order by custom ID | ✅ Complete |
| Current day's executed trades | ✅ Complete |
| Trades for specific order | ✅ Complete |
Position & Holdings Management (2 Tools)
Tool | Functionality | Status |
| Real-time open positions | ✅ Complete |
| Long-term holdings portfolio | ✅ Complete |
Risk Management (1 Tool)
Tool | Functionality | Status |
| Pre-trade margin calculation | ✅ Complete |
Market Data (3 Tools)
Tool | Functionality | Status |
| Live last traded prices | ✅ Complete |
| OHLC data with volume | ✅ Complete |
| Full market depth & order book | ✅ Complete |
Historical Data & Analysis (3 Tools)
Tool | Functionality | Status |
| Daily OHLC historical data | ✅ Complete |
| Minute-level intraday data | ✅ Complete |
| 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
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Latest Blog Posts
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/vikkysarswat/dhan-mcp-server'
If you have feedback or need assistance with the MCP directory API, please join our Discord server