cmc-market-desk
README.md
# Market Desk
A CoinMarketCap market-data agent that puts live CMC prices, a listings screener, and market-wide context in front of an LLM.
This repo is an entry for **Build with CMC: API Hackathon, Round 2**, track **AI Agents and Automation**.
Official participant: **Lawrence Macquarie**. AI coding assistants helped write the code and do not need to be declared.
It is designed for a person who wants a straight answer — “what is SOL doing, and what is the tape around it?” — without opening five CMC pages. The same tools are exposed as:
1. An **MCP server** you can attach to an MCP client (Cursor, Claude Desktop, and similar).
2. A **local demo chat UI** so judges can see the tools work without any MCP setup.
Live prices come from the CoinMarketCap Pro API once `CMC_API_KEY` is set. Without a key, the app uses a **clearly labeled fixture snapshot** captured from CMC’s Keyless Public API on 29 Nov 2026. Authenticated Pro evidence and a live-Pro demo recording are in this repo; fixture artifacts are labeled separately and are not live prices.
This is not financial advice.
## What’s useful
- **Quotes** for named assets, with ticker collision handling. Asking for `BTC` does not silently merge Bitcoin with every meme token that reused the ticker.
- **Screener-style listings**: sort by market cap, 24h change, volume, and filter by tag or ranges. 24h gainers use the listings endpoint’s own `sort=percent_change_24h`, not a local re-sort of the top market-cap names. The chat preset “Show today’s top 5 gainers” adds `volume_24h_min=100000` so illiquid +billions-% names don’t dominate. CMC applies that filter to the requested page, not the whole market, so the agent fetches a 200-row sorted window and returns the first 5 matches. Say “unfiltered” or call the tool without that floor to see the raw sort.
- **Market backdrop**: global market cap and volume, BTC/ETH dominance, Fear & Greed, and the Altcoin Season Index, composed into one briefing.
- **Asset profiles** (description, links, tags) and **amount conversion**.
- A **market brief** that stitches backdrop + top of the tape + optional featured quotes.
## How to run
Python 3.11+ (developed on 3.13). From this directory:
```bash
python3 -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
```
### Demo chat (local)
```bash
# No key: starts in fixture mode. The UI banner says so.
python -m cmc_market_agent serve --host 127.0.0.1 --port 8787
```
Open [http://127.0.0.1:8787](http://127.0.0.1:8787). Try “Give me a market brief with BTC and ETH” or “Show today’s top 5 gainers”. Add `?demo=1` to auto-play those prompts plus a BTC quote. Recordings: fixture-mode [`demo/market-desk-fixture-demo.mp4`](demo/market-desk-fixture-demo.mp4) (not live), and live Pro [`demo/market-desk-live-pro-demo.mp4`](demo/market-desk-live-pro-demo.mp4) once a key is set.
The chat uses a built-in intent router, so the demo works without an LLM key. To have an OpenAI-compatible model choose tools, set `OPENAI_API_KEY` (optional `OPENAI_BASE_URL`, `OPENAI_MODEL`).
### Live Pro API
1. Create a CoinMarketCap API account and copy the key into a local `.env` (see `.env.example`).
2. Export it in your shell. The app reads **only** `CMC_API_KEY`.
```bash
set -a && source .env && set +a
python -m cmc_market_agent serve
```
Do not put the key in the repo, in screenshots, or in chat logs.
### MCP server
```bash
python -m cmc_market_agent mcp
```
Example MCP config:
```json
{
"mcpServers": {
"cmc-market-desk": {
"command": "python",
"args": ["-m", "cmc_market_agent", "mcp"],
"cwd": "/workspace/cmc-market-agent",
"env": { "CMC_API_KEY": "set-this-in-your-own-config-not-in-git" }
}
}
}
```
### CLI
```bash
python -m cmc_market_agent status
python -m cmc_market_agent call get_quotes --arg symbols=BTC,ETH --pretty
python -m cmc_market_agent call screen_listings --arg sort=percent_change_24h --arg limit=5 --pretty
python -m cmc_market_agent chat "What's the Fear and Greed backdrop?"
```
### Tests
```bash
pytest -q
```
## Data modes
| Mode | When | What happens |
| --- | --- | --- |
| `pro` | `CMC_API_KEY` is set (default) | `GET https://pro-api.coinmarketcap.com/...` with header `X-CMC_PRO_API_KEY` |
| `fixtures` | no key, or `CMC_DATA_MODE=fixtures`, or `CMC_USE_FIXTURES=1` | Frozen snapshot in `fixtures/`. Every response is labeled **not live**. |
| `keyless` | `CMC_DATA_MODE=keyless` | `GET https://pro-api.coinmarketcap.com/public-api/...` with no key. Same response shape as Pro, but rate-limited. Optional; the submission path is Pro + key. |
Authenticated Pro integration is the real production path. Keyless access was used during development to verify endpoint shapes and to record honest evidence. It is not a substitute for `CMC_API_KEY`.
## CMC API endpoints used
All calls are `GET` against the Pro host when a key is present, or the `/public-api` prefix in keyless mode.
| Endpoint | Used by | Why |
| --- | --- | --- |
| `GET /v3/cryptocurrency/quotes/latest` | `get_quotes`, `market_brief` | Current quotes for named assets |
| `GET /v3/cryptocurrency/listings/latest` | `screen_listings`, `market_brief` | Sorted, filterable market tape / screener |
| `GET /v2/cryptocurrency/info` | `get_asset_profile` | Description, links, tags, category |
| `GET /v1/global-metrics/quotes/latest` | `get_market_backdrop`, `market_brief` | Market cap, volume, dominance |
| `GET /v3/fear-and-greed/latest` | `get_market_backdrop`, `market_brief` | Sentiment context |
| `GET /v1/altcoin-season-index/latest` | `get_market_backdrop`, `market_brief` | Market-regime context |
| `GET /v2/tools/price-conversion` | `convert_amount` | Amount conversion at CMC rates |
The official Python SDK (`coinmarketcap-sdk`) exists and was considered. This project uses a small `httpx` client instead so that:
- the key is read only from `CMC_API_KEY` (the SDK examples use `CMC_PRO_API_KEY`);
- fixture and keyless modes share one code path;
- the key never appears in returned payloads or error messages.
## Evidence of a real API call
### Keyless calls already made (no key, real HTTP)
On 29 Nov 2026, 22:27–22:30 HKT (14:27–14:30 UTC), this project called the Keyless Public API from the build machine. Raw bodies and headers are in [`evidence/keyless-real-calls/`](evidence/keyless-real-calls/). No `X-CMC_PRO_API_KEY` header was sent.
Successful example — `GET /public-api/v3/cryptocurrency/listings/latest?limit=15&convert=USD&sort=market_cap` — HTTP 200, `status.error_code` `"0"`, `status.timestamp` `2026-11-29T14:30:13.714Z`. The body lists BTC, ETH, USDT, and the rest of the top 15 as of that moment. That snapshot is the default listings fixture.
A later listings call — `GET /public-api/v3/cryptocurrency/listings/latest?limit=50&sort=percent_change_24h&sort_dir=desc&convert=USD` — HTTP 200 at `2026-11-29T15:06:17.884Z` (23:06 HKT). That 50-row body is the fixture universe for 24h-change sorts, so fixture “top gainers” are not a re-sort of the top-15 market-cap slice. The same sort had returned HTTP 500 earlier the same evening (`evidence/keyless-real-calls/gainers.json`); the successful retry is `gainers50.json`.
Successful quotes example — `GET /public-api/v3/cryptocurrency/quotes/latest?symbol=BTC,ETH&convert=USD&skip_invalid=true` — HTTP 200 at `2026-11-29T14:27:27.965Z`. The response contained **20** assets that share the BTC or ETH tickers, which is why the agent disambiguates by active status and `cmc_rank`.
### Authenticated Pro API calls (redacted)
On 29 Nov 2026, 23:21 HKT (15:21 UTC), this project made authenticated Pro API calls from the build machine with `X-CMC_PRO_API_KEY` set from the gitignored local `CMC_API_KEY`. Redacted request/response files are in [`evidence/pro-api-placeholder/`](evidence/pro-api-placeholder/). The header value in those files is `[REDACTED]`; the raw key is not in the repo.
All of the following returned `status.error_code` `0` (or `"0"`) and `credit_count` 1:
| File | Request | Notes |
| --- | --- | --- |
| `REDACTED_PRO_QUOTES_LATEST.json` | `GET /v3/cryptocurrency/quotes/latest?symbol=BTC,ETH&convert=USD&skip_invalid=true` | HTTP-equivalent success at `2026-11-29T15:21:01.209Z` |
| `REDACTED_PRO_LISTINGS_LATEST.json` | `GET /v3/cryptocurrency/listings/latest?sort=percent_change_24h&sort_dir=desc&limit=5&convert=USD` | First success at 15:21:02Z; recaptured at `2026-11-29T15:25:14.478Z` so the excerpt keeps all **5** requested rows, not 2 |
| `REDACTED_PRO_INFO.json` | `GET /v2/cryptocurrency/info?id=1,1027` | `2026-11-29T15:21:03.109Z` |
| `REDACTED_PRO_GLOBAL_METRICS.json` | `GET /v1/global-metrics/quotes/latest?convert=USD` | `2026-11-29T15:21:03.820Z` |
| `REDACTED_PRO_FEAR_AND_GREED.json` | `GET /v3/fear-and-greed/latest` | `2026-11-29T15:21:04.740Z`; `value` 9 |
| `REDACTED_PRO_ALTCOIN_SEASON.json` | `GET /v1/altcoin-season-index/latest` | `2026-11-29T15:21:05.558Z` |
| `REDACTED_PRO_PRICE_CONVERSION_USD.json` | `GET /v2/tools/price-conversion?amount=1&symbol=BTC&convert=USD` | Success at `2026-11-29T15:25:16.063Z` |
`GET /v2/tools/price-conversion?amount=1&symbol=BTC&convert=USD,ETH` did **not** succeed. The API returned HTTP 400, `error_code` 400, `credit_count` 0, message **“Your plan is limited to 1 convert options.”** That is a Basic-plan limit on multiple `convert` currencies in one request, not a missing/null `id` (the client already omits a null `id`). The same call with `convert=USD` or `convert=ETH` alone returned `error_code` 0. The honest 400 record is `REDACTED_PRO_PRICE_CONVERSION.json` (recaptured with the error message at `2026-11-29T15:25:15.311Z`). The agent now retries one single-convert call per target when that specific 400 is returned; the plan was not upgraded.
The live code path is `CmcClient._from_http()` in `src/cmc_market_agent/cmc_client.py`.
## What the API made possible — and where it got in the way
**What it made possible**
- One vendor covers the questions a desk actually asks: a price, a ranked tape, and the market backdrop (dominance, Fear & Greed, Altcoin Season) without stitching several data sources.
- The Keyless Public API uses the **same JSON envelope** as the keyed Pro API, so a fixture / keyless / Pro client can share parsers.
- v3 listings filters (`sort`, `tag`, price and market-cap ranges) are enough to build a small screener directly on the endpoint, which is what `screen_listings` does.
**Obstacles actually hit while building** (not hypothetical)
1. **Anonymous rate limit.** After a short burst of keyless probes, `GET /v2/tools/price-conversion` and `GET /v3/index/cmc100-latest` returned HTTP 429, `error_code` 1022: “You've reached the limit for anonymous access.” Fixture conversions are derived from snapshot USD prices and labeled as such. `cmc100-latest` is not used by the agent.
2. **Ticker reuse.** `quotes/latest?symbol=BTC,ETH` returned 20 rows, including inactive and low-ranked names that reuse those tickers. The tool therefore picks a canonical asset and surfaces collisions instead of treating a symbol as unique.
3. **Mixed `quote` shapes.** v3 cryptocurrency quotes/listings return `quote` as an **array** of `{symbol, price, ...}`. v1 global metrics still returns `quote` as a **currency-keyed object** (`{"USD": {...}}`). The client normalizes both.
4. **Mixed `tags` shapes.** `listings/latest` returned tags as strings (`"layer-1"`). `quotes/latest` returned objects (`{"slug": "layer-1", "name": "...", "category": "..."}`).
5. **Mixed `error_code` types.** Some endpoints return `"0"` (string), others `0` (int). Status checks compare `str(code) == "0"`.
6. **A listings sort call returned HTTP 500** (`INTERNAL_SERVER_ERROR`) on keyless `sort=percent_change_24h` immediately after the 429s. A later unsliced top-15 market-cap listings call succeeded, and an even later retry of `sort=percent_change_24h&limit=50` (29 Nov 2026, 23:06 HKT) returned HTTP 200. Live Pro/keyless mode now always sends the requested `sort` to `GET /v3/cryptocurrency/listings/latest` rather than re-ranking a short market-cap slice. Fixture mode uses the dedicated 50-row `percent_change_24h` snapshot for 24h gainers, and documents it when a sort has no matching snapshot (it then falls back to the 15-row market-cap slice and says so). Unfiltered 24h gainers include illiquid and zero-market-cap names; pass `volume_24h_min` or `market_cap_min` to tighten. The 50-row fixture is still a frozen snapshot of the first 50 rows, not the entire CMC universe.
7. **Official SDK env var name.** SDK examples use `CMC_PRO_API_KEY`. This hackathon brief requires `CMC_API_KEY`, so the project implements its own client.
8. **Basic-plan convert limit.** Authenticated `GET /v2/tools/price-conversion?amount=1&symbol=BTC&convert=USD,ETH` returned HTTP 400, `error_code` 400: “Your plan is limited to 1 convert options” (`credit_count` 0). This is not a paid-endpoint 403, and it is not a bad `id` parameter. Single-currency converts (`convert=USD` or `convert=ETH`) succeeded with `error_code` 0 and 1 credit on the same Basic plan. The client now falls back to one successful single-convert call per target when that 400 is returned. The plan was not upgraded.
9. **Listings min/max filters are page-scoped.** A Pro call with `sort=percent_change_24h`, `limit=5`, and `volume_24h_min=100000` returned only one asset, because CMC applies the floor to the first 5 sorted rows, not the whole market. The screener now fetches a 200-row sorted window when a range filter is set, then keeps the requested number of matches.
## Project layout
```
src/cmc_market_agent/ # client, tools, MCP server, demo API
web/index.html # demo chat UI (`?demo=1` auto-plays a short walkthrough)
fixtures/ # frozen keyless snapshots (labeled, not live)
evidence/ # real keyless captures + redacted Pro captures
demo/ # fixture-mode and live-Pro screen recordings of the local UI
demo-screenshots/ # screenshots labeled fixture or live Pro
scripts/capture_pro_evidence.py
submission-draft.md # DoraHacks submit-build draft only (not submitted)
```
## Later submission steps (not done in this repo)
Do these before the deadline, **Thu 10 Dec 2026, 7:59am HKT** (Wed 9 Dec 2026, 23:59 UTC):
1. Register on CoinMarketCap API and DoraHacks with the **same email**, and keep the key in local `CMC_API_KEY` only. (Done for the API account used in the redacted Pro captures.)
2. Publish this repo as a **public GitHub** repository. (Not done; no remote has been added.)
3. Demo videos: fixture-mode `demo/market-desk-fixture-demo.mp4`, and live Pro `demo/market-desk-live-pro-demo.mp4`. This project is not deployed to a paid host.
4. Submit on DoraHacks, naming every endpoint above, attaching the redacted Pro evidence, and selecting track **AI Agents and Automation**. (Not submitted.)
5. Post on X with **#BuildwithCMC** and the DoraHacks project link. (Not posted.)
## License
MIT. CoinMarketCap data remains subject to CoinMarketCap’s API terms.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues