home / mcp / zerodha mcp server

Zerodha MCP Server

๐Ÿ’น This is the code for Zerodha MCP โ€” you can use it to trade live on the Indian Stock Market ๐Ÿ‡ฎ๐Ÿ‡ณ๐Ÿ“ˆ๐Ÿ’ฐ

Installation
Add the following to your MCP client configuration file.

Configuration

View docs
{
  "mcpServers": {
    "kshitij-21-mcp-server": {
      "command": "/opt/homebrew/bin/bun",
      "args": [
        "/path/to/your/zerodha-mcp/main.ts"
      ],
      "env": {
        "API_KEY": "your_zerodha_api_key",
        "API_SECRET": "your_zerodha_api_secret"
      }
    }
  }
}

You deploy this MCP server to interact with Zerodha trading through an AI assistant. It provides tools to place stock orders, view holdings, and manage mutual fund investments via Zerodhaโ€™s API, all with OAuth-based authentication, token management, and robust error handling.

How to use

Start the MCP server locally and connect it to your MCP client. Your client can request actions such as placing stock orders, checking your current holdings, viewing active mutual fund SIPs, and checking mutual fund holdings. The server handles authentication flows, token storage, and session persistence so you can focus on trading tasks without re-authenticating each time.

How to install

Prerequisites you need before installing:

# Prerequisites
- Bun runtime installed
- Zerodha trading account
- Zerodha API credentials (API_KEY and API_SECRET)

Configuration and running

Configure environment variables and start the MCP server locally. Use the environment variables shown in the setup to authorize requests to Zerodha.

API_KEY=your_zerodha_api_key
API_SECRET=your_zerodha_api_secret

Starting the MCP server

Run the MCP server using Bun as the runtime and point it to the main entry file.

bun main.ts

Claude integration configuration (example)

If you are configuring an MCP client like Claude to talk to this server, use the following example configuration. It runs the MCP server locally and passes the necessary authentication tokens via environment variables.

{
  "mcpServers": {
    "kite": {
      "command": "/opt/homebrew/bin/bun",
      "env": {
        "API_KEY": "your_zerodha_api_key",
        "API_SECRET": "your_zerodha_api_secret"
      },
      "args": ["/path/to/your/zerodha-mcp/main.ts"]
    }
  }
}

Add a server connection (overview)

This MCP server supports a local stdio connection. The complete command to run is provided in the example above. You can customize the server name and path to your project as needed.

Additional usage notes

- The server stores tokens in a local file and automatically refreshes them as needed. - Authentication is OAuth-based, with a login flow initiated by your MCP client. - The server provides structured error handling and detailed operation logging to help diagnose issues.

Security and reliability

- Environment variables contain credentials and are kept on your local machine. - Sensitive data is not included in error messages. - Token management auto-refreshes, reducing the risk of session expiry during trades.

Troubleshooting

Common issues you might encounter include authentication failures, API errors, network issues, or MCP connection problems. Check that the environment variables are set, tokens are valid, and the server is running with the expected path to main.ts. Review error logs for detailed information.

# Example checks
cat config/tokens.json
# If tokens are invalid, re-authenticate via the login flow in your MCP client

Available tools

login-to-zerodha

Tool to initiate the OAuth login flow and obtain user authorization for Zerodha access.

set-zerodha-tokens

Tool to complete authentication by providing the request token obtained after login.

place-stock-order

Tool to place buy or sell orders for specified stocks on NSE or BSE with given quantity and order type.

get-zerodha-holdings

Tool to fetch current stock portfolio holdings.

get-zerodha-mutual-fund-sips

Tool to retrieve active mutual fund SIPs.

zerodha-mf-holdings

Tool to view mutual fund holdings (experimental).