Skip to main content
Glama

Buy-Stock

Execute stock purchases on Zerodha platform by specifying stock symbol and quantity for automated trading operations.

Instructions

This tool buys a stock in the zerodha trading platform for the given quantity and stock name at the current market price for the given user

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
stockYes
qtyYes

Implementation Reference

  • index.js:67-80 (registration)
    Registration of the 'Buy-Stock' MCP tool, including inline Zod input schema and the main handler function that initializes the trading client and delegates to placeOrder.
    server.tool( "Buy-Stock", "This tool buys a stock in the zerodha trading platform for the given quantity and stock name at the current market price for the given user", { stock: z.string(), qty: z.number() }, async ({ stock, qty }) => { try { const trading = await initializeTrading(); const response = await trading.placeOrder(stock, qty, "BUY"); return { content: [{ type: "text", text: String(response) }] }; } catch (error) { return { content: [{ type: "text", text: `Error: ${error.message}` }] }; } } );
  • index.js:71-79 (handler)
    The inline handler function for the Buy-Stock tool, which handles input parameters, initializes trading, places the BUY order, and returns response or error.
    async ({ stock, qty }) => { try { const trading = await initializeTrading(); const response = await trading.placeOrder(stock, qty, "BUY"); return { content: [{ type: "text", text: String(response) }] }; } catch (error) { return { content: [{ type: "text", text: `Error: ${error.message}` }] }; } }
  • Zod schema for Buy-Stock tool input: stock (string), qty (number).
    { stock: z.string(), qty: z.number() },
  • Supporting helper: placeOrder method in ZerodhaTrading class that performs the actual market order placement via KiteConnect API.
    async placeOrder(tradingsymbol, quantity, type) { if (!tradingsymbol || !quantity || !type) { throw new Error( "Please provide all required parameters: tradingsymbol, quantity, and type" ); } try { const response = await this.kc.placeOrder("regular", { exchange: "NSE", tradingsymbol: tradingsymbol, transaction_type: type, quantity: quantity, product: "CNC", order_type: "MARKET", }); return response; } catch (err) { if (err.error_type === "InputException") { throw new Error( `Invalid trading symbol or order parameters: ${err.message}` ); } throw err; }
  • Supporting helper: initializeTrading function that obtains a valid access token and instantiates the ZerodhaTrading client.
    async function initializeTrading() { try { const accessToken = await tokenManager.getValidToken(); return new ZerodhaTrading(apiKey, accessToken); } catch (error) { console.error("Error initializing trading:", error.message); throw error; } }

Latest Blog Posts

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/yogendhra9/ZerodhaMCP'

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