MCP Servers

A collection of Model Context Protocol servers, templates, tools and more.

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

Created 7/18/2025
Updated 2 days ago
Repository documentation and setup instructions

๐Ÿš€ 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 ๐Ÿ”ฅ

Quick Setup
Installation guide for this server

Install Package (if required)

npx @modelcontextprotocol/server-zerodha-mcp

Cursor configuration (mcp.json)

{ "mcpServers": { "maniklakhanpal-zerodha-mcp": { "command": "npx", "args": [ "maniklakhanpal-zerodha-mcp" ] } } }