stock-nse-india
repository·master·Indexed 43 days ago
https://github.com/hi-imcodeman/stock-nse-indiaA toolset for accessing National Stock Exchange (NSE) of India market data, featuring an NPM package (v1.4.0), a GraphQL/REST API server, a CLI tool, and an MCP server for AI assistants. It provides capabilities to retrieve equity details, index data, and historical OHLC data, including a specialized MCP client with session-based memory and context summarization using OpenAI functions.
What's inside stock-nse-india
- The NSE India MCP Server is a Model Context Protocol (MCP) implementation that exposes functions from the NSE India stock market API library. It allows AI assistants (like Cursor or other MCP-compatible clients) to access real-time stock market data from the National Stock Exchange of India through standardized tools.
How context summarization works
masterThe MCP Client includes an automatic context summarization feature to manage token usage and costs.
Mechanism:
- Token Monitoring: The client tracks the number of tokens used in the current conversation.
- Smart Triggering: When the token usage reaches a configurable threshold (default is often 70-80% of the
maxTokenslimit), the summarization process is triggered. - Summarization: The client summarizes the conversation history, retaining important information while significantly reducing the token count.
- Seamless Operation: This happens transparently during the query process, allowing for much longer conversations without hitting hard context limits.
Benefits:
- Cost Savings: Can reduce token usage by up to 64%.
- Performance: Faster responses due to smaller context windows.
- Scalability: Enables virtually unlimited conversation length.
How MCP context summarization works
masterThe summarization process follows a specific lifecycle to prevent token overflow while maintaining conversation continuity:
- Token Counting: The system calculates
totalTokens = systemPromptTokens + messageTokens + reservedTokens. - Threshold Check: Summarization triggers only if
totalTokens > (maxTokens * summarizationThreshold)ANDmessageCount >= minMessagesToSummarize. - Summarization Process:
- Recent messages (targeting ~40% of
maxTokens) are kept. - Older messages are summarized using AI.
- The old messages are replaced by the summary.
- Recent messages (targeting ~40% of
- Result: Token usage drops to approximately 40% of
maxTokens, preserving recent context and key information from the past.
- Token Counting: The system calculates
How the NSE India MCP Server architecture works
masterThe server uses a modular architecture to ensure consistency across different implementations (Stdio server vs. OpenAI Functions client):
src/mcp/mcp-tools.ts: The single source of truth for common tools configuration and handler functions.src/mcp/server/mcp-server.ts: A Stdio-based MCP server designed for local AI assistant integration (e.g., Cursor).src/mcp/client/mcp-client.ts: An OpenAI Functions-based MCP client for natural language queries.
By sharing tool definitions and business logic, the project ensures that all server implementations behave identically.
Install and build the NSE India MCP Server from source
masterTo set up the server locally for development:
- Install dependencies:
yarn install- Build the project:
yarn build- Start the stdio MCP server:
yarn start:mcpyarn install yarn build yarn start:mcpSet up local development environment
masterTo develop locally, ensure you have Node.js 18+ installed.
- Clone the repository.
- Install dependencies using
npm install. - Use
npm run start:devfor development mode with auto-reload. - Use
npm run buildto compile TypeScript to JavaScript. - Use
npm run docsto generate TypeDoc documentation.
# Clone repository git clone https://github.com/hi-imcodeman/stock-nse-india.git cd stock-nse-india # Install dependencies npm install # Development mode with auto-reload npm run start:devInstall stock-nse-india CLI tool
masterTo access NSE India data via the command line, install the package globally:
npm install -g stock-nse-indiaConfigure the MCP server using a local build
masterIf you have cloned the repository and built the project locally, use this configuration in your MCP settings:
{ "mcpServers": { "nse-india-stdio": { "command": "node", "args": ["build/mcp/server/mcp-server-stdio.js"], "env": { "NODE_ENV": "production" } } } }Prerequisites:
- Node.js 18+ installed
- Repository cloned and dependencies installed (
npm install) - Project built (
npm run build)
Run via Docker
masterYou can run the NSE India API server using Docker. The container exposes the main application, GraphQL endpoint, and API documentation.
# Pull and run from Docker Hub docker run --rm -d -p 3001:3001 imcodeman/nseindia # Or build locally docker build -t nseindia . && docker run --rm -d -p 3001:3001 nseindia:latestSetup and run the MCP Client Demos
masterTo run the demonstration suite for the NSE India Stock Market MCP Client, you must first set your OpenAI API key and build the project.
Prerequisites
- An
OPENAI_API_KEYenvironment variable.
Setup Steps
- Set the environment variable:
export OPENAI_API_KEY=your-api-key - Build the project:
npm run build
Running Demos
- Run all demos:
npm run demo:mcp-client - Run a specific demo (1-7):
npm run demo:mcp-client <number> - Run the memory-focused example:
npm run demo:memory
# Set OpenAI API Key export OPENAI_API_KEY=your-api-key # Build the project npm run build # Run all demos npm run demo:mcp-client # Run a specific demo (1-7) npm run demo:mcp-client 3 # Memory example npm run demo:memory- An
Install stock-nse-india via npm or yarn
masterTo use the library in your project, install it via npm or yarn:
npm install stock-nse-india # or yarn add stock-nse-indianpm install stock-nse-indiaConfigure the MCP server using npx (Recommended)
masterThe easiest way to use the MCP server without cloning the repository is via
npx.Prerequisites:
- Node.js 18+ installed (
node --versionshould be v18.0.0 or higher).
Steps:
- (Optional) Install the package globally for faster startup:
npm install -g stock-nse-india - Add the following configuration to your MCP settings (e.g., in Cursor or Claude Desktop):
{ "mcpServers": { "npx-stock-nse-india": { "command": "npx", "args": ["stock-nse-india", "mcp"], "env": { "NODE_ENV": "production" } } } }- Node.js 18+ installed (