Model Context Protocol (MCP) server for DhanHQ trading platform
Project description
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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dhan_mcp_server-0.4.0.tar.gz.
File metadata
- Download URL: dhan_mcp_server-0.4.0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4450582f84d73c813e2ecdadac9914bfd5e624207f8b38050208175fc7ffa442
|
|
| MD5 |
4446651b1a92c164b6d15e8d121fec64
|
|
| BLAKE2b-256 |
43c0742ff6d3981e6d2a0b6b189e77d39840c32f6f9469fde34f710fd584ee7f
|
Provenance
The following attestation bundles were made for dhan_mcp_server-0.4.0.tar.gz:
Publisher:
publish.yml on Vedhasagaran/dhan-mcp-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dhan_mcp_server-0.4.0.tar.gz -
Subject digest:
4450582f84d73c813e2ecdadac9914bfd5e624207f8b38050208175fc7ffa442 - Sigstore transparency entry: 712728640
- Sigstore integration time:
-
Permalink:
Vedhasagaran/dhan-mcp-py@c0c70c7b2284c62c91363406249db0c9f4830bd2 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/Vedhasagaran
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c0c70c7b2284c62c91363406249db0c9f4830bd2 -
Trigger Event:
push
-
Statement type:
File details
Details for the file dhan_mcp_server-0.4.0-py3-none-any.whl.
File metadata
- Download URL: dhan_mcp_server-0.4.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
778850372ba1a894d2d5fe12f189ddbf8097840c455c02c92742503aa73d9244
|
|
| MD5 |
42b02ba33737a42d697d1b301a084ce1
|
|
| BLAKE2b-256 |
26c1622a90f78f6a734e042688ec24ce5f0d213235b1b811790c4e2ba0b7ecd7
|
Provenance
The following attestation bundles were made for dhan_mcp_server-0.4.0-py3-none-any.whl:
Publisher:
publish.yml on Vedhasagaran/dhan-mcp-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dhan_mcp_server-0.4.0-py3-none-any.whl -
Subject digest:
778850372ba1a894d2d5fe12f189ddbf8097840c455c02c92742503aa73d9244 - Sigstore transparency entry: 712728641
- Sigstore integration time:
-
Permalink:
Vedhasagaran/dhan-mcp-py@c0c70c7b2284c62c91363406249db0c9f4830bd2 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/Vedhasagaran
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c0c70c7b2284c62c91363406249db0c9f4830bd2 -
Trigger Event:
push
-
Statement type: