-
Notifications
You must be signed in to change notification settings - Fork 2
Architecture
Jai Rajput edited this page Jul 3, 2026
·
1 revision
AI Assistant (Claude/GPT)
β JSON-RPC 2.0 (stdio)
Groww MCP Server (Ruby)
β HTTPS (Bearer token)
Groww Trade API
β
Your Groww Account
- The AI assistant sends MCP requests over stdio (JSON-RPC 2.0)
- The MCP server processes the request, calls the Groww API
- Results are formatted and returned to the AI assistant
- The AI presents the data in natural language
groww-mcp/
βββ bin/
β βββ groww-mcp # Entry point β loads env, authenticates, starts server
βββ lib/
β βββ groww_mcp.rb # Main module β ALL_TOOLS registry (25 tools)
β βββ groww_mcp/
β βββ version.rb # Gem version (1.0.0)
β βββ auth.rb # Authentication β 3 methods, auto-refresh
β βββ client.rb # HTTP client β all Groww API endpoints
β βββ base_tool.rb # Base class β shared response/error helpers
β βββ tools/
β βββ auth_tools.rb # authenticate
β βββ portfolio_tools.rb # holdings, positions, margins, calculate_margin
β βββ order_tools.rb # place, modify, cancel, list, detail
β βββ smart_order_tools.rb # GTT/OCO β create, modify, cancel, list
β βββ market_tools.rb # quote, LTP, OHLC, historical
β βββ option_chain_tools.rb# option chain, order trades, backtest
β βββ instrument_tools.rb # search, detail, CSV download
β βββ user_tools.rb # profile
βββ .env.example # Config template
βββ Gemfile # Dependencies
βββ groww-mcp.gemspec # Gem spec
βββ CONTRIBUTING.md # Contributor guidelines
Manages 3 authentication methods with automatic token lifecycle:
- TOTP: Generates 6-digit code via ROTP gem β exchanges for access token
- Approval: SHA256(secret + timestamp) checksum β exchanges for access token
- Manual: Direct token passthrough
Token management:
- Tracks expiry timestamp from JWT payload
- Auto-refreshes 5 minutes before expiry via
ensure_token! - Called automatically before every API request
HTTP client using Ruby's net/http with SSL:
-
Base URL:
https://api.groww.in -
Headers:
Authorization: Bearer <token>,X-API-VERSION: 1.0 - Retry logic: 3 attempts with exponential backoff
-
Error classes:
ApiError,RateLimitError(429),ForbiddenError(403)
Every tool inherits from GrowwMcp::BaseTool which extends MCP::Tool:
-
format_response(data)β converts API response to MCP text response (JSON formatted) -
error_response(error)β standardized error formatting
All 25 tools are registered in GrowwMcp::ALL_TOOLS array. The entry point iterates this array to register each tool with the MCP server.
| Gem | Purpose |
|---|---|
mcp ~> 0.21 |
Official MCP Ruby SDK (by Anthropic + Shopify) |
rotp ~> 6.3 |
TOTP code generation for automated auth |
Why Ruby?
- Official MCP Ruby SDK is mature and well-maintained
- Ruby's expressiveness keeps tool definitions clean and readable
- Strong HTTP/JSON ecosystem built into stdlib
Why not a gem?
- MCP servers run as standalone processes, not imported as libraries
- Direct cloning keeps credentials management simple
- Easier to customize for individual setups
Why 3 auth methods?
- TOTP is best but requires one-time setup
- Approval flow is Groww's default and many users already have API keys
- Manual token is useful for quick testing and debugging
Why separate tool files?
- Each file maps to one API category
- Easy to add new tools β create file, add to registry
- Clear ownership and focused code review
Built by Jai Rajput | MIT License | Report an Issue
Setup
Reference
Community
Links