Skip to main content

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
  ]
}

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:

  1. Add a new function in server.py decorated with @mcp.tool()
  2. 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 .gitignore file excludes .env files
  • 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

dhan_mcp_server-0.2.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dhan_mcp_server-0.2.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file dhan_mcp_server-0.2.0.tar.gz.

File metadata

  • Download URL: dhan_mcp_server-0.2.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for dhan_mcp_server-0.2.0.tar.gz
Algorithm Hash digest
SHA256 087aab92eae87bfdbeb505e79f8461272a819505fc29e309403c81800d514fa5
MD5 22410f91b2db12606cde8596259060a7
BLAKE2b-256 33e9d93a29770692f3a5519ac5337bad4caa08c3fd4656fc72cd6af720960775

See more details on using hashes here.

File details

Details for the file dhan_mcp_server-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dhan_mcp_server-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 feb3619529da4cf6c39ce962380892c84c01325abece34a287b9d313706c5ab5
MD5 6f117f16f09de9d51d35549193cff770
BLAKE2b-256 dc4e42624d28d0a4bc41ad0e1d647cade590bc6a7cf1c650560e3d953f9e4bb9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page