DhanHQ MCP Server
An MCP server that enables AI assistants to interact with the DhanHQ trading platform, providing access to holdings, orders, trade history, and token renewal via DhanHQ APIs.
README
DhanHQ MCP Server
A Model Context Protocol (MCP) server that provides access to DhanHQ trading platform APIs. This server allows AI assistants and other MCP clients to interact with your DhanHQ trading account.
Features
- Holdings Summary: Fetch your current holdings from DhanHQ
- Extensible architecture for adding more DhanHQ API endpoints
Prerequisites
- Python 3.10 or higher
- DhanHQ account with API access
- DhanHQ Client ID and Access Token
Installation
Option 1: Install from PyPI (when published)
pip install dhan-mcp-server
Option 2: Install from source
git clone https://github.com/Vedhasagaran/dhan-mcp-py.git
cd dhan-mcp-py
pip install -e .
Option 3: Direct installation from GitHub
pip install git+https://github.com/Vedhasagaran/dhan-mcp-py.git
Configuration
1. Set Environment Variables
The server requires DhanHQ credentials to be set as environment variables:
Windows (PowerShell):
$env:DHAN_CLIENT_ID="your_client_id"
$env:DHAN_ACCESS_TOKEN="your_access_token"
Windows (Command Prompt):
set DHAN_CLIENT_ID=your_client_id
set DHAN_ACCESS_TOKEN=your_access_token
Linux/Mac:
export DHAN_CLIENT_ID="your_client_id"
export DHAN_ACCESS_TOKEN="your_access_token"
Using .env file (recommended):
Create a .env file in your project directory:
DHAN_CLIENT_ID=your_client_id
DHAN_ACCESS_TOKEN=your_access_token
2. Configure MCP Client
Add the server to your MCP client configuration. The configuration file location varies by application:
- Claude Desktop:
%APPDATA%\Claude\claude_desktop_config.json(Windows) or~/Library/Application Support/Claude/claude_desktop_config.json(Mac) - Other MCP Clients: Refer to your client's documentation
Example configuration:
{
"mcpServers": {
"dhan": {
"command": "dhan-mcp-server",
"env": {
"DHAN_CLIENT_ID": "your_client_id",
"DHAN_ACCESS_TOKEN": "your_access_token"
}
}
}
}
Alternative using Python directly:
{
"mcpServers": {
"dhan": {
"command": "python",
"args": ["-m", "server"],
"env": {
"DHAN_CLIENT_ID": "your_client_id",
"DHAN_ACCESS_TOKEN": "your_access_token"
}
}
}
}
Usage
Starting the Server Manually
If you want to run the server directly:
dhan-mcp-server
Or with Python:
python -m server
The server communicates via stdio (standard input/output) using the MCP protocol.
Available Tools
Once connected via an MCP client, the following tools are available:
get_holdings_summary
Fetches your current holdings from DhanHQ.
Returns:
{
"holdings": [
// Array of holding objects
]
}
get_all_orders
Fetches all orders from your DhanHQ account.
Returns:
{
"orders": [
// Array of order objects with details like order ID, status, quantity, price, etc.
]
}
get_trade_history
Fetch trade history within a specific date range.
Parameters:
from_date: Start date in formatYYYY-MM-DD(e.g.,2025-11-01)to_date: End date in formatYYYY-MM-DD(e.g.,2025-11-21)
Returns:
{
"status": "success",
"from_date": "2025-11-01",
"to_date": "2025-11-21",
"trades": [
// Array of trade objects with details like trade ID, order ID, quantity, price, time, settlement status
]
}
renew_access_token
Renews your DhanHQ access token for another 24 hours. This is useful for extending your session without manually copying tokens from the web interface.
Important: Only works with tokens generated from Dhan Web (web.dhan.co). Tokens obtained via API key/secret cannot be renewed using this method.
Returns:
{
"status": "success",
"message": "Token renewed successfully...",
"new_token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
"expires_in": "24 hours"
}
Usage: After renewal, update your DHAN_ACCESS_TOKEN environment variable with the new token, or update your MCP client configuration.
Development
Setting Up Development Environment
# Clone the repository
git clone https://github.com/Vedhasagaran/dhan-mcp-py.git
cd dhan-mcp-py
# Create virtual environment
python -m venv venv
# Activate virtual environment
# Windows:
venv\Scripts\activate
# Linux/Mac:
source venv/bin/activate
# Install in editable mode with dependencies
pip install -e .
Adding New Tools
To add new DhanHQ API endpoints:
- Add a new function in
server.pydecorated with@mcp.tool() - Follow this pattern:
@mcp.tool()
def your_new_tool() -> dict:
"""
Description of what this tool does.
"""
client = dhanhq(client_id, access_token)
result = client.your_api_method()
return {"data": result}
Security Notes
- Never commit credentials: The
.gitignorefile excludes.envfiles - Keep tokens secure: Access tokens provide full access to your DhanHQ account
- Use environment variables: Always load credentials from environment variables
- Rotate tokens regularly: Follow DhanHQ's security best practices
Troubleshooting
"Missing DHAN_CLIENT_ID or DHAN_ACCESS_TOKEN"
Ensure environment variables are set correctly. Check with:
# Windows PowerShell
echo $env:DHAN_CLIENT_ID
# Linux/Mac
echo $DHAN_CLIENT_ID
MCP Client Can't Find Server
Verify the installation:
pip show dhan-mcp-server
which dhan-mcp-server # Linux/Mac
where dhan-mcp-server # Windows
License
MIT License - see LICENSE file for details
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Resources
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。