un-comtrade-mcp-server

v0.1.8 pre-1.0

Access UN Comtrade international merchandise and services trade statistics — country lookups, HS commodity search, bilateral trade flows, balances, rankings, and data availability — via MCP. STDIO or Streamable HTTP.

un-comtrade.caseyjhand.com/mcp
claude mcp add --transport http un-comtrade-mcp-server https://un-comtrade.caseyjhand.com/mcp
codex mcp add un-comtrade-mcp-server --url https://un-comtrade.caseyjhand.com/mcp
{
  "mcpServers": {
    "un-comtrade-mcp-server": {
      "url": "https://un-comtrade.caseyjhand.com/mcp"
    }
  }
}
gemini mcp add --transport http un-comtrade-mcp-server https://un-comtrade.caseyjhand.com/mcp
{
  "mcpServers": {
    "un-comtrade-mcp-server": {
      "command": "bunx",
      "args": [
        "mcp-remote",
        "https://un-comtrade.caseyjhand.com/mcp"
      ]
    }
  }
}
{
  "mcpServers": {
    "un-comtrade-mcp-server": {
      "type": "http",
      "url": "https://un-comtrade.caseyjhand.com/mcp"
    }
  }
}
curl -X POST https://un-comtrade.caseyjhand.com/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"curl","version":"1.0.0"}}}'

Tools

9

comtrade_lookup_countries

Resolve country and area names to Comtrade M49 numeric codes used in all data queries. Accepts a partial name, full name, or ISO alpha-2/alpha-3 code (e.g. "United States", "USA", "US"). Returns matching entries with their numeric code, ISO identifiers, and a validAsReporter flag — regional groupings like "World" (code 0) are valid partners but not valid reporters. Use this before any trade data tool that requires reporter_code or partner_code. To aggregate across all partners, use partner code 0 (World) — no lookup needed.

read
invocation
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "comtrade_lookup_countries",
    "arguments": {
      "query": "<query>"
    }
  }
}
schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "query": {
      "type": "string",
      "minLength": 1,
      "description": "Country or area name fragment, full name, or ISO alpha-2/alpha-3 code to search for. Case-insensitive. Examples: \"Germany\", \"DEU\", \"DE\", \"united states\", \"china\"."
    },
    "role": {
      "default": "any",
      "description": "Filter by role: \"reporter\" returns only codes valid for the reporterCode parameter, \"partner\" returns only partner areas (includes regional groupings), \"any\" returns all matches regardless of role.",
      "type": "string",
      "enum": [
        "reporter",
        "partner",
        "any"
      ]
    },
    "include_groups": {
      "default": true,
      "description": "Whether to include regional and economic groupings (e.g. EU, ASEAN). Set to false to return only individual countries.",
      "type": "boolean"
    }
  },
  "required": [
    "query",
    "role",
    "include_groups"
  ],
  "additionalProperties": false
}
view source ↗

comtrade_search_commodities

Find HS commodity codes by keyword, partial description, or known code prefix. Returns matched codes at the requested aggregation level (2-, 4-, or 6-digit) with full descriptions, parent code, leaf status, and a recommended_query_code field indicating the best code level for subsequent trade queries (2-digit chapter for broad analysis, 4- or 6-digit for specific products). This is the critical resolution step — agents cannot derive HS codes from product names without this tool. Use aggr_level 2 to start broad; narrow with aggr_level 4 or 6 once you have a chapter. To get all commodities for an entire chapter, pass the 2-digit code as cmd_code in trade tools.

read
invocation
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "comtrade_search_commodities",
    "arguments": {
      "query": "<query>"
    }
  }
}
schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "query": {
      "type": "string",
      "minLength": 1,
      "description": "Keyword, partial description, or HS code prefix to search for. Examples: \"motor vehicles\", \"8471\", \"coffee\", \"84\" (chapter 84 = machinery)."
    },
    "classification": {
      "default": "HS",
      "description": "HS classification version. \"HS\" covers the combined dataset across all editions (H0–H6). Use a specific edition (e.g. \"H6\") for version-specific lookups.",
      "type": "string",
      "enum": [
        "HS",
        "H0",
        "H1",
        "H2",
        "H3",
        "H4",
        "H5",
        "H6"
      ]
    },
    "aggr_level": {
      "description": "Aggregation level: 2 = HS chapter (most general), 4 = heading, 6 = subheading (most specific). Omit to return all matching levels.",
      "anyOf": [
        {
          "type": "number",
          "const": 2
        },
        {
          "type": "number",
          "const": 4
        },
        {
          "type": "number",
          "const": 6
        }
      ]
    },
    "limit": {
      "default": 50,
      "description": "Maximum number of results to return.",
      "type": "integer",
      "minimum": 1,
      "maximum": 200
    }
  },
  "required": [
    "query",
    "classification",
    "limit"
  ],
  "additionalProperties": false
}
view source ↗

comtrade_list_service_categories

List EBOPS 2010 service trade categories — the equivalent of comtrade_search_commodities for the services domain. Returns category codes, descriptions, and parent codes. Filter by keyword to find a specific service type (e.g. "transport", "financial", "travel"), or browse from a parent category code. Use the returned id as service_code in comtrade_get_services_trade.

read
invocation
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "comtrade_list_service_categories",
    "arguments": {}
  }
}
schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "query": {
      "description": "Keyword or category code prefix to filter results. Examples: \"transport\", \"SA\", \"financial services\". Omit to list all EBOPS categories.",
      "type": "string"
    },
    "parent_code": {
      "description": "Return only sub-categories of this parent EBOPS code. Example: \"SA\" to see all transport service sub-categories.",
      "type": "string"
    },
    "limit": {
      "default": 100,
      "description": "Maximum number of categories to return.",
      "type": "integer",
      "minimum": 1,
      "maximum": 500
    }
  },
  "required": [
    "limit"
  ],
  "additionalProperties": false
}
view source ↗

comtrade_get_trade_flows

open-world

Fetch bilateral trade flow records — the primary data retrieval tool. Returns trade value in USD (primaryValue — FOB for exports, CIF for imports), quantity, net weight, and period/commodity/partner metadata for each row. Accepts multiple periods and commodity codes per call; pass partner_code 0 to get totals aggregated across all partners. Omit cmd_code or pass "TOTAL" to aggregate all commodities. Free-tier capped at 500 records per call. Use comtrade_lookup_countries to resolve reporter/partner codes and comtrade_search_commodities to resolve HS codes before calling this tool.

read
invocation
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "comtrade_get_trade_flows",
    "arguments": {
      "reporter_code": "<reporter_code>",
      "flow_code": "<flow_code>",
      "period": "<period>"
    }
  }
}
schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "reporter_code": {
      "type": "integer",
      "minimum": -9007199254740991,
      "maximum": 9007199254740991,
      "description": "M49 numeric code of the reporting country. Use comtrade_lookup_countries to resolve names to codes."
    },
    "flow_code": {
      "type": "string",
      "enum": [
        "M",
        "X",
        "RX",
        "RM"
      ],
      "description": "Trade flow direction: M=imports, X=exports, RX=re-exports, RM=re-imports."
    },
    "period": {
      "minItems": 1,
      "maxItems": 12,
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 4
      },
      "description": "One or more periods to query. Annual: \"YYYY\" (e.g. [\"2022\", \"2023\"]). Monthly: \"YYYYMM\" (e.g. [\"202201\", \"202202\"]). Maximum 12 periods per call."
    },
    "partner_code": {
      "description": "M49 code of the trade partner. Use 0 to aggregate across all partners (World total). Omit to get a disaggregated breakdown by partner country.",
      "type": "integer",
      "minimum": -9007199254740991,
      "maximum": 9007199254740991
    },
    "cmd_code": {
      "description": "HS commodity codes to query. Maximum 20 codes per call. Pass [\"TOTAL\"] or omit to aggregate all commodities. Use comtrade_search_commodities to find the right codes.",
      "maxItems": 20,
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "classification": {
      "default": "HS",
      "description": "HS classification version. \"HS\" covers the combined dataset.",
      "type": "string",
      "enum": [
        "HS",
        "H0",
        "H1",
        "H2",
        "H3",
        "H4",
        "H5",
        "H6"
      ]
    },
    "freq": {
      "default": "A",
      "description": "Frequency: A=annual, M=monthly.",
      "type": "string",
      "enum": [
        "A",
        "M"
      ]
    }
  },
  "required": [
    "reporter_code",
    "flow_code",
    "period",
    "classification",
    "freq"
  ],
  "additionalProperties": false
}
view source ↗

comtrade_get_trade_balance

open-world

Compute the trade balance for a country over one or more periods — total exports minus total imports — with optional commodity filter. Fetches export and import totals in parallel and returns the signed balance (USD), export value, import value, and coverage ratio (exports / imports). Note: mirror asymmetry is common — balance reflects values as reported by this country; the same flow may be recorded differently by the partner. Uses comtrade_lookup_countries to resolve reporter codes.

read
invocation
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "comtrade_get_trade_balance",
    "arguments": {
      "reporter_code": "<reporter_code>",
      "period": "<period>"
    }
  }
}
schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "reporter_code": {
      "type": "integer",
      "minimum": -9007199254740991,
      "maximum": 9007199254740991,
      "description": "M49 numeric code of the reporting country."
    },
    "period": {
      "minItems": 1,
      "maxItems": 12,
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 4
      },
      "description": "One or more annual periods (YYYY) or monthly periods (YYYYMM)."
    },
    "cmd_code": {
      "description": "HS commodity codes to restrict the balance calculation. Omit to compute the overall trade balance across all commodities.",
      "maxItems": 20,
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "classification": {
      "default": "HS",
      "description": "HS classification version.",
      "type": "string",
      "enum": [
        "HS",
        "H0",
        "H1",
        "H2",
        "H3",
        "H4",
        "H5",
        "H6"
      ]
    },
    "freq": {
      "default": "A",
      "description": "Frequency: A=annual, M=monthly.",
      "type": "string",
      "enum": [
        "A",
        "M"
      ]
    }
  },
  "required": [
    "reporter_code",
    "period",
    "classification",
    "freq"
  ],
  "additionalProperties": false
}
view source ↗

comtrade_get_top_partners

open-world

Rank trading partners for a reporter by trade value for a given commodity and flow direction. Returns the top N partners sorted by descending value — answers "who does country X mainly export product Y to?" or "where does country X mainly import product Z from?" for a single period. Fetches a disaggregated breakdown across all partners then sorts locally. Omit cmd_code to rank partners for total trade.

read
invocation
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "comtrade_get_top_partners",
    "arguments": {
      "reporter_code": "<reporter_code>",
      "flow_code": "<flow_code>",
      "period": "<period>"
    }
  }
}
schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "reporter_code": {
      "type": "integer",
      "minimum": -9007199254740991,
      "maximum": 9007199254740991,
      "description": "M49 numeric code of the reporting country."
    },
    "flow_code": {
      "type": "string",
      "enum": [
        "M",
        "X"
      ],
      "description": "Trade flow: M=imports, X=exports."
    },
    "period": {
      "type": "string",
      "minLength": 4,
      "description": "Single period: annual (YYYY) or monthly (YYYYMM)."
    },
    "cmd_code": {
      "description": "Single HS commodity code (e.g. \"84\", \"8471\"). Omit or pass \"TOTAL\" to rank partners for total merchandise trade.",
      "type": "string"
    },
    "limit": {
      "default": 10,
      "description": "Maximum number of top partners to return.",
      "type": "integer",
      "minimum": 1,
      "maximum": 50
    }
  },
  "required": [
    "reporter_code",
    "flow_code",
    "period",
    "limit"
  ],
  "additionalProperties": false
}
view source ↗

comtrade_get_top_commodities

open-world

Rank commodity categories for a reporter by trade value for a given flow direction and period. Returns the top N HS chapters (2-digit) or headings (4-digit) sorted by descending value — answers "what does country X mainly export?" or "what does country X mainly import?". Symmetric counterpart to comtrade_get_top_partners. Optionally filter to a single partner country.

read
invocation
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "comtrade_get_top_commodities",
    "arguments": {
      "reporter_code": "<reporter_code>",
      "flow_code": "<flow_code>",
      "period": "<period>"
    }
  }
}
schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "reporter_code": {
      "type": "integer",
      "minimum": -9007199254740991,
      "maximum": 9007199254740991,
      "description": "M49 numeric code of the reporting country."
    },
    "flow_code": {
      "type": "string",
      "enum": [
        "M",
        "X"
      ],
      "description": "Trade flow: M=imports, X=exports."
    },
    "period": {
      "type": "string",
      "minLength": 4,
      "description": "Single period: annual (YYYY) or monthly (YYYYMM)."
    },
    "aggr_level": {
      "default": 2,
      "description": "HS aggregation level: 2 = chapter (broader), 4 = heading (more specific).",
      "anyOf": [
        {
          "type": "number",
          "const": 2
        },
        {
          "type": "number",
          "const": 4
        }
      ]
    },
    "partner_code": {
      "description": "M49 partner code to filter to a specific bilateral relationship. Omit to rank commodities across all partners.",
      "type": "integer",
      "minimum": -9007199254740991,
      "maximum": 9007199254740991
    },
    "limit": {
      "default": 10,
      "description": "Maximum number of top commodity categories to return.",
      "type": "integer",
      "minimum": 1,
      "maximum": 50
    }
  },
  "required": [
    "reporter_code",
    "flow_code",
    "period",
    "aggr_level",
    "limit"
  ],
  "additionalProperties": false
}
view source ↗

comtrade_get_data_availability

Check which reporter/period/classification combinations have published data before constructing expensive trade queries. Returns dataset records with period, classification, record count, and publication date. Call this before querying recent periods — annual data is typically published 3–12 months after the reference year and not all countries report every year. Omit all parameters to browse the full availability index.

read
invocation
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "comtrade_get_data_availability",
    "arguments": {}
  }
}
schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "reporter_code": {
      "description": "M49 reporter code to check. Omit to browse availability across all reporters.",
      "type": "integer",
      "minimum": -9007199254740991,
      "maximum": 9007199254740991
    },
    "period": {
      "description": "Period to check: YYYY (annual) or YYYYMM (monthly). Omit to see all available periods for the reporter.",
      "type": "string"
    },
    "freq": {
      "default": "A",
      "description": "Frequency: A=annual, M=monthly.",
      "type": "string",
      "enum": [
        "A",
        "M"
      ]
    },
    "type_code": {
      "default": "C",
      "description": "Trade type: C=commodities (goods), S=services.",
      "type": "string",
      "enum": [
        "C",
        "S"
      ]
    },
    "classification": {
      "default": "HS",
      "description": "Classification code filter (e.g. \"HS\", \"H6\", \"EB10\").",
      "type": "string"
    }
  },
  "required": [
    "freq",
    "type_code",
    "classification"
  ],
  "additionalProperties": false
}
view source ↗

comtrade_get_services_trade

open-world

Fetch international trade-in-services data (EBOPS 2010 classification). Same bilateral structure as goods trade: returns flow value in USD, period, and reporter/partner/category metadata for each row. Use comtrade_list_service_categories to find the right service code before querying. Note: services trade data has more limited country and period coverage than goods trade.

read
invocation
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "comtrade_get_services_trade",
    "arguments": {
      "reporter_code": "<reporter_code>",
      "flow_code": "<flow_code>",
      "period": "<period>"
    }
  }
}
schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "reporter_code": {
      "type": "integer",
      "minimum": -9007199254740991,
      "maximum": 9007199254740991,
      "description": "M49 numeric code of the reporting country."
    },
    "flow_code": {
      "type": "string",
      "enum": [
        "M",
        "X"
      ],
      "description": "Trade flow: M=imports (credit received by reporter), X=exports (debit paid)."
    },
    "period": {
      "minItems": 1,
      "maxItems": 12,
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 4
      },
      "description": "One or more annual periods (YYYY) or monthly periods (YYYYMM)."
    },
    "partner_code": {
      "description": "M49 partner code. Use 0 for all partners combined. Omit for a disaggregated by-partner breakdown.",
      "type": "integer",
      "minimum": -9007199254740991,
      "maximum": 9007199254740991
    },
    "service_code": {
      "description": "EBOPS 2010 service category code (e.g. \"SA\" for transport). Use comtrade_list_service_categories to find available codes. Omit to get totals across all service categories.",
      "type": "string"
    }
  },
  "required": [
    "reporter_code",
    "flow_code",
    "period"
  ],
  "additionalProperties": false
}
view source ↗

Resources

2

Complete list of all Comtrade country and area codes — both reporters and partners — with M49 numeric codes, ISO identifiers, validAsReporter flag, and group membership. Bundled from reference data loaded at startup. Use comtrade_lookup_countries for name-based search; use this resource to enumerate the full list.

uri comtrade://countries mime application/json

Top-level HS commodity hierarchy at the requested aggregation level: 2 (HS chapters — 21 sections), 4 (headings — 97 chapters), or 6 (6-digit subheadings). Full leaf-level enumeration is large — use comtrade_search_commodities for keyword search. This resource provides the structural overview for navigation and code discovery.

uri comtrade://hs-classification/{level} mime application/json