Zerodha MCP Server

Zerodha MCP Server

Enables AI assistants to interact with the Zerodha trading platform for placing stock orders, viewing holdings, and managing mutual fund investments through the MCP protocol.

Category
Visit Server

README

๐Ÿš€ Zerodha MCP Server

A Model Context Protocol (MCP) server that provides tools for interacting with the Zerodha trading platform. This server enables AI assistants to place stock orders, view holdings, and manage mutual fund investments through Zerodha's API.

TypeScript MCP SDK KiteConnect Bun

๐ŸŽฏ Features

Trading Tools

  • Place Stock Orders - Execute buy/sell orders on NSE and BSE
  • View Holdings - Get current stock portfolio holdings
  • Mutual Fund SIPs - View active Systematic Investment Plans
  • Mutual Fund Holdings - Check mutual fund portfolio (experimental)

Authentication

  • Zerodha Login - Secure OAuth-based authentication
  • Token Management - Automatic token storage and refresh
  • Session Handling - Persistent login sessions

Technical Features

  • TypeScript - Full type safety across the codebase
  • MCP Protocol - Standardized AI assistant integration
  • Error Handling - Comprehensive error management
  • Logging - Detailed operation logging

๐Ÿ—๏ธ Architecture

zerodha-mcp/
โ”œโ”€โ”€ config/
โ”‚   โ”œโ”€โ”€ KiteConfig.ts
โ”‚   โ””โ”€โ”€ tokens.json     
โ”œโ”€โ”€ functions/
โ”‚   โ””โ”€โ”€ OrderRelated.ts  
โ”œโ”€โ”€ utils/
โ”‚   โ””โ”€โ”€ readWrite.ts     
โ”œโ”€โ”€ .env                 
โ”œโ”€โ”€ bun.lock
โ”œโ”€โ”€ main.ts            
โ””โ”€โ”€ package.json 

๐Ÿ› ๏ธ Tech Stack

  • TypeScript - Type-safe JavaScript
  • MCP SDK - Model Context Protocol implementation
  • KiteConnect - Zerodha's official trading API
  • Axios - HTTP client for API requests
  • Bun - Required JavaScript runtime

๐Ÿ“ฆ Installation

Prerequisites

  • Bun - Required runtime
  • Zerodha trading account
  • Zerodha API credentials

1. Clone the Repository

git clone git@github.com:ManikLakhanpal/Zerodha-MCP.git
cd zerodha-mcp

2. Install Dependencies

bun install

3. Environment Setup

Create a .env file in the root directory:

API_KEY=your_zerodha_api_key
API_SECRET=your_zerodha_api_secret

4. Get Zerodha API Credentials

  1. Log in to your Zerodha account
  2. Go to Zerodha Developers
  3. Create a new application
  4. Copy your API Key and API Secret

๐Ÿš€ Usage

Starting the MCP Server

bun main.ts

Using with Claude

This MCP server is designed to work with Claude AI assistants. Here's how to set it up:

1. Install Claude Desktop

First, download and install Claude Desktop for your operating system.

2. Configure Claude to Use MCP โš™๏ธ

  1. ๐Ÿ“ฑ Open Claude Desktop
  2. ๐Ÿ”ง Go to Settings โ†’ Developer Settings
  3. ๐Ÿ“„ Click Open Config File to open claude_desktop_config.json in your default editor
  4. โœ๏ธ Add the following configuration to the mcpServers section:
{
  "mcpServers": {
    "kite": {
      "command": "/opt/homebrew/bin/bun", # Use `which bun` to get path
      "env": {
        "API_KEY": "your_zerodha_api_key",
        "API_SECRET": "your_zerodha_api_secret"
      },
      "args": ["/path/to/your/zerodha-mcp/main.ts"] # Paste the absolute path to main.ts
    }
  }
}

๐Ÿ”ง Important Notes:

  • ๐Ÿ› ๏ธ Replace /opt/homebrew/bin/bun with your Bun installation path (use which bun to find it)
  • ๐Ÿ“ Replace /path/to/your/zerodha-mcp/main.ts with the actual path to your project's main.ts file
  • ๐Ÿ”‘ Replace your_zerodha_api_key and your_zerodha_api_secret with your actual Zerodha API credentials
  • ๐Ÿท๏ธ The server name "kite" can be changed to any name you prefer

3. Authentication Flow ๐Ÿ”

  1. ๐Ÿ”‘ First Login: Ask Claude to "login to Zerodha"
  2. ๐ŸŒ Get Login URL: Claude will provide a login URL
  3. โœ… Complete Login: Open the URL in your browser and complete Zerodha login
  4. ๐Ÿ”ง Set Tokens: Copy the request token from the URL and ask Claude to "set Zerodha tokens" with the token

4. Available Commands in Claude ๐Ÿ’ฌ

Once connected, you can ask Claude to:

  • ๐Ÿ” "Login to Zerodha" - Get authentication URL
  • ๐Ÿ”ง "Set Zerodha tokens with [request_token]" - Complete authentication
  • ๐Ÿ“ˆ "Place a buy order for 10 shares of RELIANCE on NSE" - Execute trades
  • ๐Ÿ“Š "Show my current holdings" - View portfolio
  • ๐Ÿ’ฐ "Get my mutual fund SIPs" - View SIP investments
  • ๐Ÿฆ "Check my mutual fund holdings" - View MF portfolio

5. Example Conversations ๐Ÿ’ญ

You: ๐Ÿ” "Login to Zerodha"
Claude: ๐ŸŒ [Provides login URL]

You: ๐Ÿ”ง "Set Zerodha tokens with abc123xyz"
Claude: โœ… [Confirms token setup]

You: ๐Ÿ“ˆ "Buy 5 shares of TCS on NSE"
Claude: ๐Ÿ’ฐ [Executes order and shows result]

You: ๐Ÿ“Š "What stocks do I currently hold?"
Claude: ๐Ÿ“‹ [Shows your portfolio holdings]

Available Tools ๐Ÿ› ๏ธ

1. ๐Ÿ” Login to Zerodha

// Tool: login-to-zerodha
// Returns a login URL for user authentication

2. ๐Ÿ”ง Set Authentication Tokens

// Tool: set-zerodha-tokens
// Parameters: { request_token: string }
// Sets access tokens after successful login

3. ๐Ÿ“ˆ Place Stock Order

// Tool: place-stock-order
// Parameters: {
//   variety: "regular" | "amo" | "co" | "auction" | "iceberg",
//   stock: string,
//   qty: number,
//   exchange: "NSE" | "BSE",
//   orderType: "BUY" | "SELL"
// }

4. ๐Ÿ“Š Get Holdings

// Tool: get-zerodha-holdings
// Returns current stock portfolio holdings

5. ๐Ÿ’ฐ Get Mutual Fund SIPs

// Tool: get-zerodha-mutual-fund-sips
// Returns active Systematic Investment Plans

6. ๐Ÿฆ Get Mutual Fund Holdings

// Tool: zerodha-mf-holdings
// Returns mutual fund portfolio (experimental)

๐Ÿ”ง Configuration

๐Ÿ” Token Management

The server automatically manages authentication tokens:

  • ๐Ÿ“ Tokens are stored in config/tokens.json
  • ๐Ÿ”„ Automatic token refresh on session expiry
  • ๐Ÿ›ก๏ธ Secure token handling with error recovery

โšก API Limits

  • โฑ๏ธ Zerodha has rate limits on API calls
  • ๐Ÿ› ๏ธ The server includes built-in error handling
  • ๐Ÿ“ Failed requests are logged with detailed error information

๐Ÿ”’ Security

  • ๐Ÿ” Environment Variables - API credentials stored securely on your local machine
  • ๐Ÿ“ Error Logging - No sensitive data in error messages

๐Ÿ“Š API Reference

๐Ÿข Zerodha API Integration

The server uses Zerodha's official KiteConnect API:

  • ๐Ÿ“ˆ Trading - Place orders, view holdings
  • ๐Ÿ’ฐ Mutual Funds - SIP management and holdings
  • ๐Ÿ” Authentication - OAuth-based login flow

๐Ÿค– MCP Protocol

Implements the Model Context Protocol for AI assistant integration:

  • ๐Ÿ› ๏ธ Tool Definitions - Structured parameter schemas
  • ๐Ÿ›ก๏ธ Error Handling - Graceful failure responses
  • ๐Ÿ”’ Type Safety - Zod schema validation

๐Ÿงช Development

๐Ÿ“ Project Structure

src/
โ”œโ”€โ”€ main.ts              # Server entry point
โ”œโ”€โ”€ config/
โ”‚   โ””โ”€โ”€ KiteConfig.ts    # Zerodha configuration
โ”œโ”€โ”€ functions/
โ”‚   โ””โ”€โ”€ OrderRelated.ts  # Trading functions
โ””โ”€โ”€ utils/
    โ””โ”€โ”€ readWrite.ts     # File utilities

๐Ÿ› ๏ธ Adding New Tools

  1. ๐Ÿ“ Define the tool in main.ts
  2. โš™๏ธ Implement the function in functions/
  3. ๐Ÿ›ก๏ธ Add proper error handling
  4. ๐Ÿ“š Update this README

๐Ÿšจ Important Notes

โš ๏ธ Trading Risks

  • ๐Ÿ’ฐ Real Money Trading - This server executes real trades
  • โฐ Market Hours - Orders only work during market hours
  • ๐Ÿ›ก๏ธ Risk Management - Implement proper stop-loss mechanisms
  • ๐Ÿงช Testing - Use paper trading for testing

๐Ÿ”ง API Limitations

  • โฑ๏ธ Rate Limits - Respect Zerodha's API rate limits
  • ๐Ÿ• Market Hours - Some functions only work during trading hours
  • ๐Ÿฆ Account Type - Requires a Zerodha trading account

๐Ÿ”ง Troubleshooting

๐Ÿšจ Common Issues

๐Ÿ” Authentication Errors

# Check if tokens are valid
cat config/tokens.json

# Re-authenticate if needed
# Use login-to-zerodha tool

โš ๏ธ API Errors

  • ๐Ÿ”‘ Verify API credentials in .env
  • ๐Ÿฆ Check Zerodha account status
  • โฐ Ensure market hours for trading functions

๐ŸŒ Network Issues

  • ๐Ÿ“ก Check internet connection
  • ๐Ÿข Verify Zerodha API status
  • ๐Ÿ“ Review error logs for details

๐Ÿค– Claude MCP Connection Issues

  • ๐Ÿ” Server Not Found: Ensure the working directory path is correct
  • ๐Ÿ› ๏ธ Command Not Found: Make sure bun is installed and in PATH
  • ๐Ÿ” Authentication Failed: Check environment variables are set correctly
  • โฐ Token Expired: Re-authenticate using the login flow
  • ๐Ÿšซ Permission Denied: Ensure the project folder has proper read/write permissions

๐Ÿค Contributing

  1. ๐Ÿด Fork the repository
  2. ๐ŸŒฟ Create a feature branch (git checkout -b feature/new-tool)
  3. โš™๏ธ Implement your changes
  4. ๐Ÿ›ก๏ธ Add proper error handling
  5. ๐Ÿ“š Update documentation
  6. ๐Ÿ“ค Submit a pull request

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

โš ๏ธ Disclaimer

This software is for educational and personal use only. Trading involves substantial risk of loss and is not suitable for all investors. The developers are not responsible for any financial losses incurred through the use of this software. ๐Ÿ›ก๏ธ

๐Ÿ†˜ Support

  • ๐Ÿ› Issues - Report bugs via GitHub Issues
  • ๐Ÿ“š Documentation - Check this README and code comments
  • ๐Ÿข Zerodha Support - Contact Zerodha for API-related issues

๐Ÿ™ Acknowledgments

  • ๐Ÿข Zerodha - Trading platform and API
  • ๐Ÿค– MCP SDK - Model Context Protocol implementation
  • ๐Ÿ”’ TypeScript - Type safety and development experience

Made by Manik Lakhanpal ๐Ÿ”ฅ

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured