How the system works under the hood.
┌─────────────────────────────────────────────────────────────────┐
│ AGENT (agent-v1.mjs) │
├─────────────────────────────────────────────────────────────────┤
│ ┌─────────────────┐ ┌─────────────────────────────────┐ │
│ │ StockTwits │ │ LLM Analysis (OpenAI) │ │
│ │ Sentiment │────▶│ • Signal research │ │
│ │ │ │ • Position management │ │
│ └─────────────────┘ │ • Buy/sell decisions │ │
│ └──────────────┬──────────────────┘ │
└─────────────────────────────────────────┼───────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ MCP SERVER (Cloudflare Workers) │
├─────────────────────────────────────────────────────────────────┤
│ Policy Engine → Approval Tokens → Order Execution │
└──────────────────────────┬──────────────────────────────────────┘
│
▼
┌─────────────┐
│ Alpaca │
│ Broker │
└─────────────┘
The agent runs continuously with two main phases:
For each trending stock, the LLM receives:
It returns:
{
"verdict": "BUY" | "SKIP" | "WAIT",
"confidence": 0.0-1.0,
"reasoning": "explanation",
"red_flags": ["concerns"],
"catalysts": ["positive factors"]
}
For each held position, the LLM receives:
It returns:
{
"action": "HOLD" | "SELL",
"confidence": 0.0-1.0,
"reasoning": "explanation"
}
All orders go through a two-step process for safety:
Call orders-preview with order details. The policy engine validates:
Returns an approval token (valid 5 minutes).
Call orders-submit with the approval token. The order is executed through Alpaca.
The MCP server exposes these tools:
| Tool | Description |
|---|---|
accounts-get |
Get account balance and status |
positions-list |
List current positions |
positions-close |
Close a position |
orders-preview |
Preview order, get approval token |
orders-submit |
Submit approved order |
orders-list |
List recent orders |
market-clock |
Check if market is open |
market-quote |
Get stock quote |
technicals-get |
Get RSI, MACD, etc. |
mahoraga/
├── agent-v1.mjs # Trading agent
├── .dev.vars # API keys (gitignored)
│
├── src/ # MCP Server
│ ├── index.ts # Entry point
│ ├── durable-objects/
│ │ └── trading-agent.ts # DO version (optional)
│ ├── mcp/
│ │ └── agent.ts # Tool definitions
│ ├── policy/
│ │ ├── engine.ts # Trade validation
│ │ └── approval.ts # Token handling
│ └── providers/
│ ├── alpaca/ # Broker client
│ └── llm/ # OpenAI client
│
├── dashboard/ # React dashboard
│ └── src/App.tsx
│
└── migrations/ # D1 database