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
访问服务器

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 🔥

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选