comtrade-mcp
by rawfuncdev
README.md
# comtrade-mcp
An [MCP](https://modelcontextprotocol.io) server that gives AI assistants (Claude Desktop, Claude Code, Cursor and others) direct access to **UN Comtrade** trade statistics.
It is built for the question export advisors ask every day: *"For this HS code, which countries should we target?"*
- Works **without an API key** (Comtrade's public preview endpoint). A free key raises the limits.
- Returns decision-ready numbers: rankings, market share, USD/kg unit values and CAGR, not raw dumps.
- Lightweight: two dependencies (`mcp`, `httpx`), runs locally.
## Tools
| Tool | What it answers |
|---|---|
| `search_country` | Comtrade code for "Germany", "DE", "TUR"... (common names like "Turkey" and "Vietnam" work too) |
| `search_hs` | HS codes by prefix (`0802`) or keyword (`hazelnut`) |
| `market_indicators` | TradeMap-style target-market table seen from one exporter (default Türkiye): imports, world share, USD/kg, value and quantity CAGR, last-year growth, trade balance, the exporter's shipments, market share and destination rank; supplier concentration (HHI) with a key |
| `top_importers` | The world's largest importers of an HS code: share, USD/kg, CAGR; optional mirror fill |
| `top_exporters` | The world's largest exporters (the competitor list): share, USD/kg, CAGR |
| `mirror_imports` | A country's reported imports next to what its partners say they shipped to it |
| `top_partners` | A country's export destinations or import sources for an HS code |
| `trend` | Year-by-year values and CAGR for a reporter/partner/HS combination |
| `trade_flow` | Raw value and weight for a specific query |
## Install
```bash
git clone https://github.com/rawfuncdev/comtrade-mcp.git
cd comtrade-mcp
python3 -m venv .venv && source .venv/bin/activate
pip install -e .
```
## Connect to Claude Desktop
Add to `claude_desktop_config.json` (use the absolute path of your clone):
```json
{
"mcpServers": {
"comtrade": {
"command": "/path/to/comtrade-mcp/.venv/bin/comtrade-mcp",
"env": { "COMTRADE_API_KEY": "" }
}
}
}
```
Restart Claude Desktop, then ask things like:
> Who are the top 10 importers of HS 080222 in 2023, and what do they pay per kg?
>
> Where does Türkiye export shelled hazelnuts, and how has Germany's import demand grown since 2019?
>
> Build a target-market table for HS 080222 from Türkiye's side. Flag markets that look under-reported.
## API key (optional)
Without a key, each call uses the public preview endpoint: one period per request, at most 500 rows, and a short-term rate limit (the server retries automatically). For heavier use, get a free subscription key at [comtradeplus.un.org](https://comtradeplus.un.org) and set `COMTRADE_API_KEY`. Never commit the key.
## Mirror data
Some countries report late, partially or not at all. Their real imports can be estimated from what their partners report exporting to them, the method TradeMap uses. Example, HS 080222 (shelled hazelnuts), 2024:
| Country | Own report | Partners' exports to it |
|---|---|---|
| Iraq | $0.14M | $2.43M |
| Poland | $43.0M | $192.1M |
| Germany | $584.6M | $555.2M (normal CIF/FOB gap) |
`mirror_imports` works without a key. A world-wide mirror scan (`mirror=True`) needs every bilateral row, so it requires `COMTRADE_API_KEY`.
## Notes on the data
- Values are as reported to UN Comtrade (imports are usually CIF, exports FOB). Recent years fill in gradually as countries report.
- `top_importers` excludes country groups (EU, ASEAN) to avoid double counting.
- Out of scope: tariffs (see ITC MacMap), national tariff lines beyond HS6, and company-level data.
- Without a key, a query that would return more than 500 rows stops with an error instead of returning silently truncated totals.
## Development
```bash
pip install -e ".[dev]"
pytest
```
Tests run offline against fixtures.
## License
MIT. Trade data © United Nations, subject to the [UN Comtrade terms of use](https://comtradeplus.un.org).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues