Dhan Trading MCP server

Control your Dhan trading account through Claude using simple conversation. Check your account balance, view current holdings and positions, get live market prices, and place various orders (market, limit, stop-loss, bracket) - all by simply asking your LLM
Back to servers
Provider
Mayank Thole
Release date
Apr 29, 2025
Language
Python
Stats
1 star

The DhanHQ Trading Assistant is an AI-powered solution that enables natural language interaction with the DhanHQ trading platform. This tool allows you to place orders, check your portfolio, and manage trading activities through simple conversational commands, making trading more accessible and efficient.

Features Overview

Order Management

  • Regular market/limit orders
  • Super orders with target and stop-loss functionality
  • After-market order placement
  • Order book and trade history access

Portfolio Management

  • View holdings and positions information
  • Convert positions between types (e.g., intraday to delivery)

Account Information

  • Check fund balance and available margin
  • Calculate margin requirements for potential trades

Installation

Prerequisites

  • Python 3.9 or higher
  • DhanHQ trading account
  • DhanHQ API credentials (client ID and access token)

Setup Steps

  1. Clone the repository:

    git clone https://github.com/yourusername/dhan-broker-mcp-trades.git
    cd dhan-broker-mcp-trades
    
  2. Install dependencies:

    pip install -r requirements.txt
    
  3. Configure your DhanHQ credentials:

    Edit the config.py file:

    DHAN_CLIENT_ID = "your_client_id_here"
    DHAN_ACCESS_TOKEN = "your_access_token_here"
    DHAN_API_BASE_URL = "https://api.dhan.co/v2"
    
  4. Ensure your stocks.json file contains the stocks you want to trade:

    {
      "companies": [
        {
          "stock_code": "1333",
          "company_name": "HDFC Bank Ltd.",
          "stock_name": "HDFCBANK",
          "description": "Description of the company..."
        }
      ]
    }
    

Usage

Running Trading Tools

Each tool can be operated independently using the MCP CLI:

# For order placement
python -m mcp.server.cli dev order_placement_tool.py

# For portfolio management
python -m mcp.server.cli dev portfolio_server.py

# For other specific tools
python -m mcp.server.cli dev <tool_filename>.py

Trading Commands

Basic Order Placement

  • "Buy 10 shares of Infosys at market price"
  • "Sell 5 shares of TCS at limit price of ₹3500"
  • "Place a GTT order to buy 20 shares of HDFC Bank at ₹1450"

Orders with Stop-Loss and Targets

  • "Buy Reliance with 2% target and 1% stop-loss"
  • "Place a trailing stop-loss buy order for Tata Motors"
  • "Short sell Axis Bank with 5% target and 2% stop-loss"

After-Market & Scheduled Orders

  • "Create an after-market order to buy 100 shares of ONGC at ₹180"
  • "Schedule a buy order for Tech Mahindra tomorrow at 9:15 AM"

Account Management

  • "What are my current holdings?"
  • "Check my available balance and margin"
  • "Show me my open positions and unrealized profits"

Portfolio Analysis

  • "Analyze my portfolio performance this month"
  • "Give me a P&L report on all banking sector trades"
  • "What was my best-performing stock in the last 30 days?"

Smart Trading Commands

  • "Buy all PSU bank stocks"
  • "Short all private sector banks today"
  • "Go long on top 5 Nifty IT companies"

Technical Analysis Trading

  • "Buy breakout stocks above 200-day moving average"
  • "Short stocks that broke below lower Bollinger Band"
  • "Buy stocks where RSI crossed above 70"

Tool Descriptions

order_placement_tool.py

Handles regular market and limit orders for buying and selling stocks.

super-order.py

Creates advanced orders with target and stop-loss limits that can be specified in absolute values or percentages.

after_market_order_tool.py

Enables order placement outside trading hours for execution on the next trading day.

fund_balance_tool.py

Retrieves account fund information and available margin.

holdings_positions_tool.py

Provides portfolio information and allows position conversion between product types.

margin_calculator_tool.py

Calculates margin requirements before placing trades.

order_book_tool.py

Accesses order history, trade book, and facilitates order cancellation.

portfolio_server.py

Serves as the main interface for portfolio management functions.

How to add this MCP server to Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > MCP and click "Add new global MCP server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "cursor-rules-mcp": {
            "command": "npx",
            "args": [
                "-y",
                "cursor-rules-mcp"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explictly ask the agent to use the tool by mentioning the tool name and describing what the function does.

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later