Sell-Stock
Automate stock sales on Zerodha by executing sell orders at the current market price for specified stocks and quantities.
Instructions
This tool sells 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
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- index.js:82-95 (registration)Registers the 'Sell-Stock' MCP tool with input schema, description, and inline handler function that uses ZerodhaTrading to place a SELL order.server.tool( "Sell-Stock", "This tool sells 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, "SELL"); return { content: [{ type: "text", text: String(response) }] }; } catch (error) { return { content: [{ type: "text", text: `Error: ${error.message}` }] }; } } );
- index.js:86-94 (handler)Handler function for the Sell-Stock tool: initializes trading instance and invokes placeOrder with SELL transaction type.async ({ stock, qty }) => { try { const trading = await initializeTrading(); const response = await trading.placeOrder(stock, qty, "SELL"); return { content: [{ type: "text", text: String(response) }] }; } catch (error) { return { content: [{ type: "text", text: `Error: ${error.message}` }] }; } }
- index.js:85-85 (schema)Input schema for Sell-Stock tool: stock (string), qty (number).{ stock: z.string(), qty: z.number() },
- trading.js:15-39 (helper)Core helper method in ZerodhaTrading class that places market orders (BUY/SELL) via KiteConnect API, used by Sell-Stock handler.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; }
- index.js:16-24 (helper)Helper function to initialize ZerodhaTrading instance with valid access token, called by Sell-Stock handler.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; } }