Skip to main content
Glama
hareeshkar

CSE MCP Intelligence

by hareeshkar
README.md
# CSE MCP Intelligence

**Institutional-grade MCP server for the Colombo Stock Exchange.** Features real-time market data, order flow pressure analysis, and AI-powered forensic fundamental screening.

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Node.js](https://img.shields.io/badge/Node.js-18+-green)](https://nodejs.org/)
![MCP](https://img.shields.io/badge/MCP-0.7+-blue)

## πŸš€ Quick Start

```bash
# Clone repository
git clone https://github.com/hareeshkar/cse-mcp-intelligence.git
cd cse-mcp-intelligence

# Install dependencies
npm install

# Build
npm run build

# Run with Claude
npm start
```

## πŸ“‹ What is MCP?

[Model Context Protocol (MCP)](https://modelcontextprotocol.io) enables Claude and other AI models to access external data sources and APIs in real-time. Instead of relying on static training data, Claude can now fetch live market information from the CSE API.

## ✨ Features

- **Real-time Market Data** - Current stock prices, market status, and daily summaries
- **Sector Analysis** - Track performance across all market sectors with detailed metrics
- **Stock Intelligence** - Company profiles, financial reports, and compliance information
- **Order Flow Analysis** - Market depth, trading pressure indicators, and volume analysis
- **Historical Data** - Candlestick charts and detailed trade history for technical analysis
- **Smart Caching** - 30-second cache to optimize API usage and reduce latency

## πŸ”§ Installation

### Prerequisites

- **Node.js** 18+ ([download](https://nodejs.org/))
- **npm** 9+ (comes with Node.js)
- **Claude Desktop** (for integration)

### Setup Steps

1. **Clone the repository:**
   ```bash
   git clone https://github.com/hareeshkar/cse-mcp-intelligence.git
   cd cse-mcp-intelligence
   ```

2. **Install dependencies:**
   ```bash
   npm install
   ```

3. **Build the TypeScript source:**
   ```bash
   npm run build
   ```

## βš™οΈ Claude Desktop Configuration

## βš™οΈ Claude Desktop Configuration

### macOS/Linux

Edit `~/Library/Application Support/Claude/claude_desktop_config.json`:

```json
{
  "mcpServers": {
    "cse": {
      "command": "node",
      "args": ["/full/path/to/cse-mcp-intelligence/build/index.js"]
    }
  }
}
```

### Windows

Edit `%APPDATA%\Claude\claude_desktop_config.json`:

```json
{
  "mcpServers": {
    "cse": {
      "command": "node",
      "args": ["C:\\full\\path\\to\\cse-mcp-intelligence\\build\\index.js"]
    }
  }
}
```

**⚠️ Important:** Replace the path with your actual project location.

### Restart Claude Desktop

After updating the config, **completely close and reopen Claude Desktop** for the changes to take effect.

## πŸ›  Available Tools

### Market Overview

#### `scan_market`
Get all stocks trading on the Colombo Stock Exchange with filtering capabilities.

**Parameters:**
- `maxPrice` (number, optional) - Filter for penny stocks below this price in LKR
- `minVolume` (number, optional) - Only show stocks with volume above threshold (filters illiquid stocks)

**Returns:** Array of stocks with symbol, name, current price, change %, and volume

**Use Cases:**
- Penny stock hunting: `scan_market { maxPrice: 50 }`
- Active stocks only: `scan_market { minVolume: 100000 }`

#### `get_sectors`
Performance data for all CSE market sectors.

**Returns:** Sector names with change %, turnover, market capitalization

**Use Cases:**
- Sector rotation analysis
- Finding the strongest/weakest sectors
- Portfolio diversification checks

#### `get_market_status`
Real-time market status - check if trading is active.

**Returns:** Open/Closed status, current timestamp, trading hours

#### `get_market_summary`
Comprehensive daily market overview with macro indicators.

**Returns:** P/E ratio, P/B ratio, foreign investor flow, total turnover, market mood

### Stock Performance

#### `get_top_gainers`
Stocks with highest percentage gains today.

**Parameters:**
- `limit` (number, optional, default: 10) - How many top performers to show

**Returns:** Top performers with price action and volume data

#### `get_top_losers`
Stocks with highest percentage losses today.

**Parameters:**
- `limit` (number, optional, default: 10) - How many bottom performers to show

**Returns:** Oversold stocks useful for contrarian or risk assessment

### Stock Analysis

#### `get_stock_detail`
Complete detailed information about a specific stock.

**Parameters:**
- `symbol` (required) - Stock symbol (e.g., "SLTL", "ASPI", "LAUGF")

**Returns:** Company profile, current price, P/E ratio, dividends, 52-week high/low, trading stats

**Use Cases:**
- Fundamental analysis
- Company profile research
- Valuation metrics

#### `get_stock_trades`
Recent trade history for order flow analysis.

**Parameters:**
- `symbol` (required) - Stock symbol
- `limit` (optional, default: 20) - Number of recent trades

**Returns:** Timestamps, executed prices, volumes, trade direction (buy/sell)

**Use Cases:**
- Order flow forensics
- Volume profile analysis
- Institutional buying/selling detection

#### `get_order_book`
Live market depth with order flow pressure analysis.

**Parameters:**
- `symbol` (required) - Stock symbol

**Returns:** 
- Bid/ask levels with volumes
- Pressure index (-1 = extreme bearish, +1 = extreme bullish)
- Bid-ask spread percentage
- Market depth profile

**Use Cases:**
- Pre-trade liquidity analysis
- Support/resistance levels
- Institutional accumulation/distribution detection

### Technical Analysis

#### `get_candlesticks`
OHLC (Open, High, Low, Close) data for technical analysis.

**Parameters:**
- `symbol` (required) - Stock symbol
- `period` (optional, default: "1d") - Time period ("1d", "1w", "1m")

**Returns:** Candlestick data with open, high, low, close prices and volume

**Use Cases:**
- Technical pattern recognition
- Trend analysis
- Support/resistance identification
- Volume profile

## πŸ’‘ Real-World Usage Examples

### Penny Stock Screening
```
Query: "Find all penny stocks under Rs. 50 with high trading volume"
Tool: scan_market { maxPrice: 50, minVolume: 500000 }
β†’ Get list of actively traded penny stocks for swing trading
```

### Sector Rotation Strategy
```
Query: "Which sectors are outperforming today? Show me the leaders."
Tool: get_sectors
β†’ Identify hot sectors and potential rotation candidates
```

### Pre-Trade Liquidity Check
```
Query: "Can I buy 10,000 shares of SLTL without moving the price too much?"
Tool: get_order_book { symbol: "SLTL" }
β†’ Check bid-ask spread, depth, and available liquidity
```

### Order Flow Forensics
```
Query: "Show me the last 50 trades on ASPI - is it institutional buying or selling?"
Tool: get_stock_trades { symbol: "ASPI", limit: 50 }
β†’ Analyze trade timing and sizes to identify smart money activity
```

### Technical Setup Confirmation
```
Query: "Show SLTL candlesticks and confirm the resistance breakout"
Tool: get_candlesticks { symbol: "SLTL", period: "1d" }
β†’ Get OHLC data for pattern recognition and trend analysis
```

### Market Stress Test
```
Query: "What percentage of stocks are down today? Any panic selling?"
Tool: get_top_losers { limit: 20 }
β†’ Identify oversold conditions and panic-driven opportunities
```

## πŸ—οΈ Technical Architecture

### Core Components

#### **1. CSE API Client** ([src/cse-client.ts](src/cse-client.ts))
- **Purpose**: Direct wrapper around the Colombo Stock Exchange REST API
- **Features**:
  - Smart caching (30-second TTL) to reduce API load and improve responsiveness
  - Symbol mapping (Display Name ↔ API Symbol translation)
  - CDN URL fixing for company reports and documents
  - Error resilience with graceful fallbacks
  - Automatic retry logic with exponential backoff
  
- **Key Methods**:
  - `getAllStocks()` - Market scan
  - `getStockDetail(symbol)` - Detailed fundamental data
  - `getOrderBook(symbol)` - Real-time market depth
  - `getTradeHistory(symbol, limit)` - Order flow analysis
  - `getCandlesticks(symbol, period)` - Technical data

#### **2. Tool Definitions & Handlers** ([src/tools.ts](src/tools.ts))
- **Purpose**: Define all MCP tools available to Claude with proper schemas
- **Includes**:
  - Tool name, description, parameter schema
  - Input validation using JSON Schema
  - Type definitions for type safety
  
- **8+ Tools**:
  - `scan_market` - Market scanning with filters
  - `get_sectors` - Sector performance
  - `get_market_status` - Trading status
  - `get_market_summary` - Macro indicators
  - `get_top_gainers` / `get_top_losers` - Performance leaders/laggards
  - `get_stock_detail` - Fundamental analysis
  - `get_stock_trades` - Trade history/order flow
  - `get_order_book` - Market depth
  - `get_candlesticks` - Technical analysis

#### **3. MCP Server** ([src/index.ts](src/index.ts))
- **Purpose**: Entry point that bridges CSE API and Claude
- **Responsibilities**:
  - Listens to Claude's tool requests via stdio
  - Routes requests to appropriate CSE client methods
  - Handles request/response serialization
  - Error handling and user-friendly error messages
  - Request parameter validation

### Data Flow

```
Claude Desktop
    ↓
[MCP Server] ← Claude makes tool request (JSON-RPC)
    ↓
[Tool Router] ← Validates parameters
    ↓
[CSE Client] ← Fetches data from API
    ↓
Colombo Stock Exchange API
    ↓
[Cache Layer] ← 30-second cache for same requests
    ↓
[Response Formatter] ← Cleans and structures data
    ↓
Claude ← Gets structured market data for analysis
```

### Caching Strategy

The system implements **smart caching** to optimize performance:
- **TTL**: 30 seconds (market data doesn't change faster)
- **Cache Key**: Tool name + parameters hash
- **Benefits**: 
  - Reduces API calls during multi-tool analysis
  - Faster Claude response times
  - Respects CSE rate limits
  - Can handle 1,000+ requests/minute internally

### Error Handling & Fallbacks

- **API Timeouts** β†’ Cached data (if available) or graceful error
- **Invalid Symbol** β†’ Fuzzy matching suggestions
- **Market Closed** β†’ Return last known data with status
- **Network Errors** β†’ Retry up to 3 times with exponential backoff
- **Rate Limiting** β†’ Queue and delay requests intelligently

### CSE API Integration Points

The server connects to these CSE API endpoints:
- `/v1/stocks` - All stocks with live prices
- `/v1/stocks/{id}` - Stock detail & fundamentals
- `/v1/stocks/{id}/orderbook` - Market depth
- `/v1/stocks/{id}/trades` - Trade history  
- `/v1/sectors` - Sector performance
- `/v1/market` - Overall market status

## πŸ“ Scripts

```bash
npm run build    # Compile TypeScript to JavaScript
npm start        # Run the MCP server
npm run dev      # Run in development mode with hot reload
```

## πŸ› Troubleshooting

### Claude doesn't recognize the server
- Verify the path in `claude_desktop_config.json` is correct and absolute
- Make sure you've run `npm run build`
- Restart Claude Desktop completely (not just minimize)

### API Connection Issues
- Check that you have internet connectivity
- Verify the CSE API is accessible: `curl https://api.cse.lk/api/v1/market`
- Check rate limiting if getting 429 errors

### Build Errors
- Delete `build/` and `node_modules/` folders
- Run `npm install` and `npm run build` again
- Ensure Node.js 18+ is installed

## πŸ“– Documentation

- [API Reference](API_REFERENCE.md) - Detailed tool specifications
- [Implementation Guide](IMPLEMENTATION_SUMMARY.md) - Technical architecture
- [Changelog](CHANGELOG.md) - Version history
- [Graceful Fallbacks](GRACEFUL_FALLBACKS.md) - Error handling strategies

## 🀝 Contributing

This project is open to contributions! Areas for enhancement:
- Additional technical indicators
- Portfolio tracking features
- Alert systems
- WebSocket support for real-time updates

## πŸ“„ License

MIT License - See LICENSE file for details

## πŸ‘€ Author

**Hareesh Karravi** - [@hareeshkar](https://github.com/hareeshkar)

## πŸ”— Links

- [Colombo Stock Exchange](https://www.cse.lk)
- [Model Context Protocol](https://modelcontextprotocol.io)
- [Claude AI](https://claude.ai)

---

**Questions?** Open an issue on GitHub or check the documentation files.

4. **get_market_summary** - Daily market overview
   - Returns: P/E ratio, P/B ratio, foreign flow, total turnover

### Stock-Specific Data

5. **get_stock_snapshot** - Real-time price data

   - Input: Stock symbol
   - Returns: Open, high, low, last price, volume, change %

6. **get_top_gainers** - Top performing stocks

   - Optional: Limit (default 10)
   - Returns: Symbol, price, % change

7. **get_order_book** - Market depth for a stock

   - Input: Stock symbol
   - Returns: Bids/asks, volumes, market pressure

8. **get_chart_data** - Historical OHLCV data
   - Input: Symbol, period (1=Intraday, 2=Weekly, 3=Monthly, 5=Daily)
   - Returns: Open, high, low, close, volume with dates

### Deep Research

9. **get_detailed_trades** - Tick-by-tick trade data

   - Optional: Filter by symbol
   - Returns: Price, volume, time, buyer/seller

10. **get_company_profile** - Company information

    - Input: Stock symbol
    - Returns: Directors, secretaries, registrars, company details

11. **get_financial_reports** - Company reports

    - Input: Stock symbol
    - Returns: Links to quarterly and annual reports (PDFs)

12. **get_noncompliance_list** - At-risk companies
    - Returns: Companies on watch list or under enforcement action
    - ⚠️ Use for due diligence - avoid investing in these companies

## Usage Examples

### In Claude Desktop

Ask questions naturally:

```
"What are the top 5 gainers on CSE today?"
```

```
"Show me the current price and order book for JKH.N0000"
```

```
"Which stocks are trading under 10 LKR?"
```

```
"Get the financial reports for DIALOG.N0000"
```

```
"What's the market P/E ratio and which sectors are performing best?"
```

```
"List all companies currently on the non-compliance watch list"
```

## Development

### Local Testing

Run the development server:

```bash
npm run dev
```

### Build for Production

```bash
npm run build
npm start
```

## Project Structure

```
cse-mcp-server/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ index.ts          # MCP server entry point
β”‚   β”œβ”€β”€ cse-client.ts     # CSE API wrapper (12 atomic tools)
β”‚   └── tools.ts          # Tool definitions for Claude
β”œβ”€β”€ build/                # Compiled JavaScript (auto-generated)
β”œβ”€β”€ package.json          # Dependencies and scripts
β”œβ”€β”€ tsconfig.json         # TypeScript configuration
β”œβ”€β”€ .env.example          # Environment template
└── README.md             # This file
```

## API Details

### Base URL

`https://www.cse.lk/api`

### Features

- **Error Handling**: Graceful fallback when API unavailable
- **Caching**: 30-second TTL reduces API calls
- **Symbol Resolution**: Auto-builds symbol-to-ID mapping
- **Data Sanitization**: Removes commas from prices, handles null values

### Rate Limiting

The CSE API does not publicly advertise rate limits, but best practice suggests:

- Keep API calls under 10 requests per second
- Use caching to minimize redundant requests

## Troubleshooting

### Server won't start

```bash
# Check Node.js version
node --version  # Should be v18+

# Check if port 3000 is available
# Or rebuild everything
rm -rf build/ node_modules/
npm install
npm run build
```

### Claude can't find the tools

1. Check the file path in `claude_desktop_config.json` is correct
2. Ensure the build directory exists: `npm run build`
3. Restart Claude Desktop completely
4. Check Claude's error console for details

### API returns empty data

- CSE market may be closed (closed outside market hours)
- Symbol may be incorrect (check exact symbol format)
- Try `scan_market` to get list of valid symbols

### Performance issues

- Data is cached for 30 seconds - avoid rapid repeated calls
- The CSE API may have rate limits we're not aware of
- Try spreading requests over time

## Disclaimer

This tool provides educational access to public CSE data. Always conduct your own due diligence before making investment decisions. The authors are not responsible for any financial losses.

## License

MIT

## Author

Created for CSE market research and analysis

## Support

For issues, feature requests, or improvements:

1. Check that you're running the latest version
2. Review the troubleshooting section
3. Ensure Node.js and npm are up to date

## Roadmap (Future Phases)

- Phase 2: Historical data analysis, sector comparison
- Phase 3: Portfolio tracking, alerts on price changes
- Phase 4: Technical indicators, moving averages
- Phase 5: Machine learning predictions

TDQS

A3.9/5.0

Scored across 13 tools

Disambiguation5/5

Each tool targets a distinct data resource: market-wide scans, sector performance, status, summary, gainers/losers, depth, snapshots, historical data, trades, company profiles, and compliance lists. The descriptions clearly differentiate between similar concepts like order book vs. trades or real-time vs. historical data, leaving no ambiguity.

Naming Consistency4/5

All but one tool follow a consistent 'get_' prefix (e.g., get_sectors, get_order_book). The single outlier, 'scan_market', uses a different verb, breaking the pattern slightly. Overall the style is predictable and readable, with only a minor deviation.

Tool Count5/5

With 13 tools, the set is well-scoped for a market data server. Each tool covers a distinct aspectβ€”market overview, stock data, depth, trades, and fundamentalsβ€”without feeling bloated or sparse.

Completeness4/5

The tool surface is comprehensive for most market data needs: it includes scanning, by-security data, market-wide metrics, trading details, and company info. A notable gap is the absence of index performance data (e.g., ASPI, S&P SL20) and possibly market news, but core workflows are well covered.

Maintenance

ActivityInactive
ResponsivenessNo issues