Skip to main content
Glama

Zerodha Kite MCP Server

by 121yaseen
index.ts3.95 kB
import { KiteConnect } from "kiteconnect"; import fs from 'fs/promises'; import path from 'path'; const apiKey = "your_api_key"; const access_token = "your_access_token"; // const apiSecret = "your_api_secret"; const kc = new KiteConnect({ api_key: apiKey }); const counterFilePath = path.join(path.dirname(new URL(import.meta.url).pathname), 'counter.json'); let orderTagCounter = 1; // First run this function to get the login url // copy the request token from the url // async function getLoginUrl() { // return kc.getLoginURL(); // } // Second run this function to get the access token // copy the access token and paste it in the access_token variable // async function getAccessToken(requestToken: string) { // const response = await kc.generateSession(requestToken, apiSecret); // console.log(response.access_token); // } async function loadCounter() { try { const data = await fs.readFile(counterFilePath, 'utf8'); const json = JSON.parse(data); if (json && typeof json.count === 'number') { orderTagCounter = json.count; } else { // File exists but content is not as expected, initialize and save await saveCounter(orderTagCounter); } } catch (err: any) { if (err && typeof err === 'object' && 'code' in err && err.code === 'ENOENT') { // File doesn't exist, create it with the default counter await saveCounter(orderTagCounter); } else { console.error("Error loading counter:", err); // Keep the default counter value } } } async function saveCounter(count: number) { try { await fs.writeFile(counterFilePath, JSON.stringify({ count: count }, null, 2), 'utf8'); } catch (err) { console.error("Error saving counter:", err); } } export async function init() { try { await loadCounter(); // Load counter at the beginning kc.setAccessToken(access_token); } catch (err) { console.error(err); } } export async function getProfile() { try { const profile = await kc.getProfile(); return profile; } catch (err) { console.error("Error getting profile:", err); } } export async function getHoldings() { try { const holdings = await kc.getHoldings(); return holdings; } catch (err) { console.error("Error getting holdings:", err); } } export async function getPositions() { try { const positions = await kc.getPositions(); return positions; } catch (err) { console.error("Error getting positions:", err); } } export async function sellStock(tradingsymbol: string, quantity: number) { try { const currentTag = String(orderTagCounter); orderTagCounter++; await saveCounter(orderTagCounter); const order = await kc.placeOrder( kc.VARIETY_REGULAR, { exchange: kc.EXCHANGE_NSE, tradingsymbol: tradingsymbol, transaction_type: kc.TRANSACTION_TYPE_SELL, quantity: quantity, product: kc.PRODUCT_CNC, order_type: kc.ORDER_TYPE_MARKET, tag: currentTag, // Use the loaded/incremented counter } ); return order; } catch (err) { console.error("Error selling stock:", err); } } export async function buyStock(tradingsymbol: string, quantity: number) { try { const currentTag = String(orderTagCounter); orderTagCounter++; await saveCounter(orderTagCounter); const order = await kc.placeOrder(kc.VARIETY_REGULAR, { exchange: kc.EXCHANGE_NSE, tradingsymbol: tradingsymbol, transaction_type: kc.TRANSACTION_TYPE_BUY, quantity: quantity, product: kc.PRODUCT_CNC, order_type: kc.ORDER_TYPE_MARKET, tag: currentTag, // Use the loaded/incremented counter }); return order; } catch (err) { console.error("Error selling stock:", err); } } // Initialize the API calls await init();

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/121yaseen/Zerodha-MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server