stock-nse-india
Version:
This package will help us to get equity/index details and historical data from National Stock Exchange of India.
432 lines (330 loc) • 9.79 kB
Markdown
# MCP Client Demo - NSE India Stock Market
Complete demonstration of the MCP (Model Context Protocol) Client with **memory management** and **context summarization** features for NSE India stock market data.
## 🚀 Quick Start
### 1. Setup
```bash
# Set OpenAI API Key
export OPENAI_API_KEY=your-api-key
# Build the project
npm run build
```
### 2. Run Demos
```bash
# All demos
npm run demo:mcp-client
# Specific demo (1-7)
npm run demo:mcp-client 3
# Memory example
npm run demo:memory
```
## 📦 What's Included
### Demo Files
1. **`mcp-client-demo.js`** (566 lines)
- 7 comprehensive scenarios
- All features demonstrated
- Production-ready examples
2. **`memory-example.js`** (230 lines)
- Focused memory & summarization demo
- 8-query conversation flow
- Real-time context window tracking
- Visual progress indicators
## 🎯 Available Demos
> **Note:** All demos use memory and context summarization by default!
### Demo 1: Basic Query with Memory
Simple query demonstrating memory and summarization features.
```bash
npm run demo:mcp-client 1
```
**Features:**
- Session-based tracking
- Context summarization
- User preference tracking
- Response metadata
### Demo 2: Session Conversation
Multi-query conversation maintaining context.
```bash
npm run demo:mcp-client 2
```
**Features:**
- Follow-up questions work naturally
- Context awareness
- Preference learning
- Session persistence
### Demo 3: Technical Analysis
Fetch and analyze technical indicators.
```bash
npm run demo:mcp-client 3
```
**Features:**
- RSI, MACD, Bollinger Bands
- Multi-iteration processing
- Session tracking
- Analysis style detection
### Demo 4: Investment Recommendations
Get investment recommendations based on technical analysis.
```bash
npm run demo:mcp-client 4
```
**Features:**
- NIFTY 50 stock analysis
- Multi-step queries
- Learned preferences
- Investment insights
### Demo 5: Multi-user Sessions
Handle multiple users with separate sessions.
```bash
npm run demo:mcp-client 5
```
**Features:**
- Isolated user contexts
- User-specific preferences
- Concurrent sessions
- Session management
### Demo 6: Context Summarization
Long conversation with automatic summarization.
```bash
npm run demo:mcp-client 6
```
**Features:**
- 9-query conversation
- Automatic summarization
- Token optimization
- Summarization history
### Demo 7: Tools & Configuration
Display available tools and configuration.
```bash
npm run demo:mcp-client 7
```
**Features:**
- 28 available tools
- Configuration details
- Context window settings
- Tool capabilities
### Memory Example
Focused demonstration of memory features.
```bash
npm run demo:memory
```
**Features:**
- Natural conversation flow (8 queries)
- Real-time context window tracking
- Automatic preference learning
- Summarization statistics
- Visual progress indicators
**Example Output:**
```
Query 1: 📊 Context Window: 37.0% used (2,964 tokens) [GREEN]
Query 2: 📊 Context Window: 45.1% used (3,605 tokens) [GREEN]
Query 3: 📊 Context Window: 58.9% used (4,713 tokens) [GREEN]
Query 4: 📊 Context Window: 74.6% used (5,967 tokens) [YELLOW]
⚠️ Context Summarization Triggered!
Query 5: 📊 Context Window: 45.2% used (3,616 tokens) [GREEN]
```
## 🧠 Memory Features
### Session Management
- **Unique Session IDs**: Each conversation tracked separately
- **Multi-user Support**: Isolated contexts per user
- **Session Persistence**: Data maintained throughout conversation
- **Statistics**: Duration, message count, stocks accessed
### Context Awareness
- **Follow-up Questions**: Reference previous queries naturally
- **Cross-query Context**: Information synthesized across queries
- **Conversation History**: Full history maintained
- **Smart References**: AI understands context automatically
### User Preference Learning
Automatically learns and tracks:
- **Preferred Stocks**: Stocks you frequently ask about
- **Analysis Style**: Brief, detailed, or technical
- **Preferred Indices**: NIFTY, BANKNIFTY, etc.
- **Personalization**: Responses tailored to preferences
## 📊 Context Summarization
### Automatic Summarization
- **Token Monitoring**: Tracks usage in real-time
- **Smart Triggering**: Activates at 70% threshold (configurable)
- **Seamless Operation**: Happens transparently
- **Context Preservation**: Important info retained
### Benefits
- **Cost Savings**: 64% token reduction demonstrated
- **Performance**: Faster responses with less context
- **Scalability**: Enables unlimited conversation length
- **Optimization**: Automatic token management
### Real-World Results
```
Before Summarization: 5,752 tokens
After Summarization: 2,075 tokens
Savings: 3,677 tokens (64% reduction!)
```
## ⚙️ Configuration
### Client Configuration
```javascript
const client = new MCPClient({
enableMemory: true, // Enable session memory
enableContextSummarization: true, // Enable auto-summarization
enableDebugLogging: false, // Debug logs
memoryConfig: {
maxConversationHistory: 50, // Max messages in conversation history
contextWindowConfig: {
maxTokens: 8000, // Token limit
summarizationThreshold: 0.8 // Trigger at 80%
}
}
});
```
### Query Options
```javascript
await client.processQuery({
query: 'Your question',
sessionId: 'unique-session-id', // For memory
userId: 'user-identifier', // Optional
useMemory: true, // Use session memory
includeContext: true, // Include history
updatePreferences: true, // Learn preferences
maxIterations: 5, // Max AI iterations
model: 'gpt-4o-mini', // OpenAI model
temperature: 0.7, // Creativity (0-1)
max_tokens: 2000 // Max response length
});
```
## 💡 Usage Examples
### Basic Query
```javascript
const { MCPClient } = require('./build/mcp/client/mcp-client.js');
const client = new MCPClient();
const response = await client.processQuery({
query: 'What is the current market status?',
useMemory: false
});
console.log(response.response);
```
### Session-based Conversation
```javascript
const sessionId = 'user-123-' + Date.now();
// Query 1
await client.processQuery({
query: 'Tell me about RELIANCE stock',
sessionId,
useMemory: true
});
// Query 2 - Uses context from Query 1
await client.processQuery({
query: 'What is its current price?', // Knows you mean RELIANCE
sessionId,
useMemory: true,
includeContext: true
});
```
### With Preferences
```javascript
await client.processQuery({
query: 'I prefer detailed technical analysis',
sessionId,
useMemory: true,
updatePreferences: true
});
// Future queries will be more technical
```
## 🎨 Features Showcase
### Real-time Context Tracking
- See context window usage after each query
- Color-coded indicators (green/yellow)
- Token count display
- Visual progress
### Automatic Summarization
- Triggers when approaching limit
- Shows what was summarized
- Displays tokens saved
- Maintains conversation flow
### Session Analytics
- Message counts
- Token usage
- Summarization history
- Tool usage tracking
## 🔧 Troubleshooting
### Error: OPENAI_API_KEY not set
```bash
export OPENAI_API_KEY=your-api-key
```
### Error: Module not found
```bash
npm run build
```
### Slow responses
- Check internet connection
- NSE API may be slow during market hours
- Consider reducing `maxIterations`
### Rate limiting
- Wait between demos
- Use different API key
- Upgrade OpenAI plan
## 📈 Performance Metrics
### Demo Results
- **Session Duration**: ~170-210 seconds
- **Queries Processed**: 8
- **Stocks Analyzed**: 4 (RELIANCE, TCS, HDFC, etc.)
- **Summarizations**: 2 automatic triggers
- **Tokens Saved**: 3,677 (64% reduction)
- **Final Usage**: 48.5% (optimized)
### Benefits
- ✅ 64% lower API costs
- ✅ Faster response times
- ✅ Longer conversations possible
- ✅ Better context management
- ✅ Automatic optimization
## 🎓 Learning Outcomes
After running the demos, you'll understand:
- How to implement conversational memory
- How context summarization works
- How to manage multi-user sessions
- How to track user preferences
- How to optimize token usage
- How to build production AI apps
## 📚 Additional Resources
### Project Links
- **Main README**: [../README.md](../README.md)
- **MCP Server Docs**: [../MCP_README.md](../MCP_README.md)
- **API Documentation**: https://hi-imcodeman.github.io/stock-nse-india
- **GitHub**: https://github.com/hi-imcodeman/stock-nse-india
### Support
- **Issues**: https://github.com/hi-imcodeman/stock-nse-india/issues
- **Email**: asraf.cse@gmail.com
## ✨ Key Highlights
- ✅ **Production Ready**: Complete error handling, tested
- ✅ **Well Documented**: Comprehensive examples and guides
- ✅ **Feature Complete**: All memory & summarization features
- ✅ **User Friendly**: Beautiful console output, clear instructions
- ✅ **Cost Effective**: 64% token savings demonstrated
- ✅ **Scalable**: Handles unlimited conversation length
## 📊 Project Statistics
- **Code**: 796 lines (JavaScript demos)
- **Documentation**: This comprehensive guide
- **Demo Scenarios**: 7 + 1 focused example
- **Available Tools**: 28 NSE India API tools
- **Features**: Memory, Summarization, Analytics, Tracking
## 🏆 Status
**✅ Complete and Ready for Production Use**
All features tested and working:
- Memory management ✓
- Context summarization ✓
- User preferences ✓
- Session tracking ✓
- Multi-user support ✓
- Real-time analytics ✓
**License**: MIT
**Version**: 1.0.0
**Last Updated**: October 2025