Skip to main content
Glama

un-comtrade-mcp-server

Get Trade Balance

comtrade_get_trade_balance
Read-only

Compute the trade balance for a country over one or more periods — total exports minus total imports — with an optional HS or SITC commodity filter whose codes must not overlap. Keyless preview makes two requests per served period, at least 4 seconds apart; authenticated calls batch periods. Returns the export and import values per period and, when both flows have data, the signed balance (USD) 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.

Tool call template

Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.

{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "arguments": {
      "reporter_code": "<reporter_code>",
      "period": "<period>"
    },
    "name": "comtrade_get_trade_balance"
  }
}

Input Schema

NameRequiredDescriptionDefault
freqNoFrequency for both flows: A=annual YYYY, M=monthly YYYYMM (01–12). Omit to infer it from the periods; an explicit value must match them.
periodYesPeriods, all annual (YYYY) or all monthly (YYYYMM); mixed lengths are rejected. Serves the first 4 distinct periods keyless (12 with authenticated access); the rest, and any period whose export or import request is rate limited, hits a UN Comtrade server error or network failure, or runs out of time, are listed in unservedPeriods. Input ceiling 120.
cmd_codeNoHS or SITC commodity codes whose values the balance sums; omit for the overall balance across all commodities. Exact duplicates count once. Overlapping codes are rejected because their shared trade would be summed twice: a code beside one of its sub-codes (27 and 2709), TOTAL beside any other entry, or AG<n> beside another AG selector or a code whose length is not n. Serves the first 20 and discloses the remainder; input ceiling 200.
partner_codeNoM49 partner code (0–999); defaults to World (0). The balance uses the reporter’s values. A partner code with a reporter-valid counterpart (840 → 842, 158 → 490) that returns no data fails with no_data naming that counterpart.
reporter_codeYesM49 numeric code of the reporting country (0–999). A partner-only code (0 World, 250 Metropolitan France, 158 Taiwan, Province of China) with no data fails with partner_only_reporter, naming the code to query instead when one exists: 251 shares the ISO-3 identity of 250, and UN Comtrade publishes the trade of 158 under 490 (Other Asia, nes).
classificationNoGoods classification: HS/H0–H6 or SITC S1–S4/SS.HS

Output Schema

NameRequiredDescriptionDefault
errorNoPresent when the call failed. Absent on success.
noticeNoPeriod failures, periods without export or import data, possible incompleteness at the preview cap, and commodity edition guidance for missing selectors.
periodsNoBalance computation per period.
cmdCodesNoCommodity codes used in the query (absent when querying all commodities).
partnerCodeNoM49 partner code used.
partnerDescNoPartner name supplied by the returned records, or its numeric code when unavailable.
mirrorCaveatNoStandard caveat about mirror asymmetry — balance reflects this country's reported values.
reporterCodeNoM49 reporter code used.
reporterDescNoReporter country name.
referenceNoticeNoReference descriptions unavailable; codes and trade values retained.
unservedPeriodsNoPeriods not served: past the period cap (4 keyless, 12 authenticated) or whose export or import request was rate limited, hit a UN Comtrade server error or network failure, or ran out of time; query them separately.
unservedCmdCodesNoCommodity codes beyond the 20-code work cap; query them separately.
cmdCodesWithoutRecordsNoServed commodity selectors without records in any completed period. Omitted when the result is truncated.

Schema Changelog

Changes observed during successful MCP inspections.

  1. Changed2 schema fields changed
    • addedOutput schema / properties / cmdCodesWithoutRecords
      Added value: +{
      +  "description": "Served commodity selectors without records in any completed period. Omitted when the result is truncated.",
      +  "items": {
      +    "type": "string"
      +  },
      +  "type": "array"
      +}
    • changedOutput schema / properties / notice / description
      Previous value: -"Periods left out because a flow was rate limited (with the wait), hit a UN Comtrade server error or network failure, or ran out of time; served periods with no export or import data; and possible incompleteness when a preview response reaches its cap."New value: +"Period failures, periods without export or import data, possible incompleteness at the preview cap, and commodity edition guidance for missing selectors."
  2. Changed11 schema fields changed
    • changedInput schema / properties / cmd_code / description
      Previous value: -"HS or SITC commodity codes to restrict the balance calculation. Serves the first 20 and discloses the remainder; input ceiling 200. Omit to compute the overall trade balance across all commodities."New value: +"HS or SITC commodity codes whose values the balance sums; omit for the overall balance across all commodities. Exact duplicates count once. Overlapping codes are rejected because their shared trade would be summed twice: a code beside one of its sub-codes (27 and 2709), TOTAL beside any other entry, or AG<n> beside another AG selector or a code whose length is not n. Serves the first 20 and discloses the remainder; input ceiling 200."
    • changedInput schema / properties / partner_code / description
      Previous value: -"M49 partner code (0–999); defaults to World (0). The balance uses the reporter’s values."New value: +"M49 partner code (0–999); defaults to World (0). The balance uses the reporter’s values. A partner code with a reporter-valid counterpart (840 → 842, 158 → 490) that returns no data fails with no_data naming that counterpart."
    • changedInput schema / properties / period / description
      Previous value: -"Periods, all annual (YYYY) or all monthly (YYYYMM); mixed lengths are rejected. Serves the first 4 keyless (12 with authenticated access); the rest, and any period whose export or import request is rate limited or runs out of time, are listed in unservedPeriods. Input ceiling 120."New value: +"Periods, all annual (YYYY) or all monthly (YYYYMM); mixed lengths are rejected. Serves the first 4 distinct periods keyless (12 with authenticated access); the rest, and any period whose export or import request is rate limited, hits a UN Comtrade server error or network failure, or runs out of time, are listed in unservedPeriods. Input ceiling 120."
    • changedInput schema / properties / reporter_code / description
      Previous value: -"M49 numeric code of the reporting country (0–999). A partner-only code (0 World, 250 Metropolitan France) with no data fails with partner_only_reporter, naming the reporter-valid code when one exists (250 → 251)."New value: +"M49 numeric code of the reporting country (0–999). A partner-only code (0 World, 250 Metropolitan France, 158 Taiwan, Province of China) with no data fails with partner_only_reporter, naming the code to query instead when one exists: 251 shares the ISO-3 identity of 250, and UN Comtrade publishes the trade of 158 under 490 (Other Asia, nes)."
    • changedOutput schema / properties / error / properties / data / properties / reason / description
      Previous value: -"Machine-readable failure mode. Declared by this tool: `keyless_hs_edition_unsupported`: Explicit HS editions require authenticated data access. `invalid_reporter_filter`: Reporter omission lacks an explicit partner. `upstream_validation`: UN Comtrade rejects the query dimensions. `invalid_upstream_response`: UN Comtrade returns malformed trade data. `no_data`: No trade data available for the reporter and period combination. `partner_only_reporter`: No served period has data and the reporter code is partner-only in UN Comtrade reference data. `rate_limited`: No period was served and UN Comtrade rate-limited a flow through its retries. `deadline_exceeded`: No period had both flows complete within the call's time budget. Other values are possible when a failure originates below the handler."New value: +"Machine-readable failure mode. Declared by this tool: `keyless_hs_edition_unsupported`: Explicit HS editions require authenticated data access. `invalid_reporter_filter`: Reporter omission lacks an explicit partner. `overlapping_cmd_codes`: Two cmd_code entries return overlapping rows: a code and one of its sub-codes, TOTAL and another entry, or an AG selector and another level. `upstream_validation`: UN Comtrade rejects the query dimensions. `invalid_upstream_response`: UN Comtrade returns malformed trade data. `no_data`: No trade data available for the reporter and period combination. `partner_only_reporter`: No served period has data and the reporter code is partner-only in UN Comtrade reference data. `rate_limited`: No period was served and UN Comtrade rate-limited a flow through its retries. `upstream_unavailable`: No requested period completed and at least one request failed on a UN Comtrade server error or network failure after retries. `deadline_exceeded`: No period had both flows complete within the call's time budget. Other values are possible when a failure originates below the handler."
    • changedOutput schema / properties / error / properties / data / properties / reason / examples
      Previous value: -[
      -  "keyless_hs_edition_unsupported",
      -  "invalid_reporter_filter",
      -  "upstream_validation",
      -  "invalid_upstream_response",
      -  "no_data",
      -  "partner_only_reporter",
      -  "rate_limited",
      -  "deadline_exceeded"
      -]New value: +[
      +  "keyless_hs_edition_unsupported",
      +  "invalid_reporter_filter",
      +  "overlapping_cmd_codes",
      +  "upstream_validation",
      +  "invalid_upstream_response",
      +  "no_data",
      +  "partner_only_reporter",
      +  "rate_limited",
      +  "upstream_unavailable",
      +  "deadline_exceeded"
      +]
    • changedOutput schema / properties / notice / description
      Previous value: -"Periods left out because a flow was rate limited (with the wait) or ran out of time, and possible incompleteness when a preview response reaches its cap."New value: +"Periods left out because a flow was rate limited (with the wait), hit a UN Comtrade server error or network failure, or ran out of time; served periods with no export or import data; and possible incompleteness when a preview response reaches its cap."
    • changedOutput schema / properties / periods / items / properties / balanceUsd / description
      Previous value: -"Trade balance in USD: exports minus imports. Positive = surplus."New value: +"Trade balance in USD: exports minus imports, only when both flows have data for the period. Positive = surplus."
    • changedOutput schema / properties / periods / items / properties / coverageRatio / description
      Previous value: -"Export coverage ratio: exports / imports. >1 = surplus, <1 = deficit. Absent when imports is zero."New value: +"Export coverage ratio: exports / imports. >1 = surplus, <1 = deficit. Only when both flows have data and imports are above zero."
    • changedOutput schema / properties / periods / items / properties / note / description
      Previous value: -"Data note, e.g. when only one flow direction has data."New value: +"Data note when a flow has no data for this period: export only, import only, or neither."
    • changedOutput schema / properties / unservedPeriods / description
      Previous value: -"Periods not served: past the period cap (4 keyless, 12 authenticated) or whose export or import request was rate limited or ran out of time; query them separately."New value: +"Periods not served: past the period cap (4 keyless, 12 authenticated) or whose export or import request was rate limited, hit a UN Comtrade server error or network failure, or ran out of time; query them separately."
  3. Changed8 schema fields changed
    • removedInput schema / properties / freq / default
      Removed value: -"A"
    • changedInput schema / properties / freq / description
      Previous value: -"Frequency: A=annual YYYY, M=monthly YYYYMM (01–12)."New value: +"Frequency for both flows: A=annual YYYY, M=monthly YYYYMM (01–12). Omit to infer it from the periods; an explicit value must match them."
    • changedInput schema / properties / period / description
      Previous value: -"Annual periods (YYYY) or monthly periods (YYYYMM). Serves the first 12 and discloses the remainder; input ceiling 120."New value: +"Periods, all annual (YYYY) or all monthly (YYYYMM); mixed lengths are rejected. Serves the first 4 keyless (12 with authenticated access); the rest, and any period whose export or import request is rate limited or runs out of time, are listed in unservedPeriods. Input ceiling 120."
    • changedInput schema / properties / reporter_code / description
      Previous value: -"M49 numeric code of the reporting country (0–999)."New value: +"M49 numeric code of the reporting country (0–999). A partner-only code (0 World, 250 Metropolitan France) with no data fails with partner_only_reporter, naming the reporter-valid code when one exists (250 → 251)."
    • changedOutput schema / properties / error / properties / data / properties / reason / description
      Previous value: -"Machine-readable failure mode. Declared by this tool: `keyless_hs_edition_unsupported`: Explicit HS editions require authenticated data access. `invalid_reporter_filter`: Reporter omission lacks an explicit partner. `upstream_validation`: UN Comtrade rejects the query dimensions. `invalid_upstream_response`: UN Comtrade returns malformed trade data. `no_data`: No trade data available for the reporter and period combination. Other values are possible when a failure originates below the handler."New value: +"Machine-readable failure mode. Declared by this tool: `keyless_hs_edition_unsupported`: Explicit HS editions require authenticated data access. `invalid_reporter_filter`: Reporter omission lacks an explicit partner. `upstream_validation`: UN Comtrade rejects the query dimensions. `invalid_upstream_response`: UN Comtrade returns malformed trade data. `no_data`: No trade data available for the reporter and period combination. `partner_only_reporter`: No served period has data and the reporter code is partner-only in UN Comtrade reference data. `rate_limited`: No period was served and UN Comtrade rate-limited a flow through its retries. `deadline_exceeded`: No period had both flows complete within the call's time budget. Other values are possible when a failure originates below the handler."
    • changedOutput schema / properties / error / properties / data / properties / reason / examples
      Previous value: -[
      -  "keyless_hs_edition_unsupported",
      -  "invalid_reporter_filter",
      -  "upstream_validation",
      -  "invalid_upstream_response",
      -  "no_data"
      -]New value: +[
      +  "keyless_hs_edition_unsupported",
      +  "invalid_reporter_filter",
      +  "upstream_validation",
      +  "invalid_upstream_response",
      +  "no_data",
      +  "partner_only_reporter",
      +  "rate_limited",
      +  "deadline_exceeded"
      +]
    • changedOutput schema / properties / notice / description
      Previous value: -"Possible incompleteness of the balance when a preview response reaches its cap."New value: +"Periods left out because a flow was rate limited (with the wait) or ran out of time, and possible incompleteness when a preview response reaches its cap."
    • changedOutput schema / properties / unservedPeriods / description
      Previous value: -"Periods beyond the 12-period work cap; query them separately."New value: +"Periods not served: past the period cap (4 keyless, 12 authenticated) or whose export or import request was rate limited or ran out of time; query them separately."
  4. Changed15 schema fields changed
    • removedInput schema / properties / classification / enum
      Removed value: -[
      -  "HS",
      -  "H0",
      -  "H1",
      -  "H2",
      -  "H3",
      -  "H4",
      -  "H5",
      -  "H6",
      -  "S1",
      -  "S2",
      -  "S3",
      -  "S4",
      -  "SS"
      -]
    • addedInput schema / properties / cmd_code / items / pattern
      Added value: +"^(?:[A-Z0-9]+(?:\\.[A-Z0-9]+)*)?$"
    • changedInput schema / properties / freq / description
      Previous value: -"Frequency: A=annual, M=monthly."New value: +"Frequency: A=annual YYYY, M=monthly YYYYMM (01–12)."
    • removedInput schema / properties / freq / enum
      Removed value: -[
      -  "A",
      -  "M"
      -]
    • addedInput schema / properties / partner_code
      Added value: +{
      +  "default": 0,
      +  "description": "M49 partner code (0–999); defaults to World (0). The balance uses the reporter’s values.",
      +  "maximum": 999,
      +  "minimum": 0,
      +  "type": "integer"
      +}
    • removedInput schema / properties / period / items / minLength
      Removed value: -4
    • addedInput schema / properties / period / items / pattern
      Added value: +"^[0-9]{4}(?:0[1-9]|1[0-2])?$"
    • changedInput schema / properties / reporter_code / description
      Previous value: -"M49 numeric code of the reporting country."New value: +"M49 numeric code of the reporting country (0–999)."
    • changedInput schema / properties / reporter_code / maximum
      Previous value: -9007199254740991New value: +999
    • changedInput schema / properties / reporter_code / minimum
      Previous value: --9007199254740991New value: +0
    • changedOutput schema / anyOf
      Previous value: -[
      -  {
      -    "not": {
      -      "required": [
      -        "error"
      -      ]
      -    },
      -    "required": [
      -      "periods",
      -      "reporterCode",
      -      "reporterDesc",
      -      "mirrorCaveat"
      -    ]
      -  },
      -  {
      -    "required": [
      -      "error"
      -    ]
      -  }
      -]New value: +[
      +  {
      +    "not": {
      +      "required": [
      +        "error"
      +      ]
      +    },
      +    "required": [
      +      "periods",
      +      "reporterCode",
      +      "reporterDesc",
      +      "partnerCode",
      +      "partnerDesc",
      +      "mirrorCaveat"
      +    ]
      +  },
      +  {
      +    "required": [
      +      "error"
      +    ]
      +  }
      +]
    • changedOutput schema / properties / error / properties / data / properties / reason / description
      Previous value: -"Machine-readable failure mode. Declared by this tool: `no_data`: No trade data available for the reporter and period combination. Other values are possible when a failure originates below the handler."New value: +"Machine-readable failure mode. Declared by this tool: `keyless_hs_edition_unsupported`: Explicit HS editions require authenticated data access. `invalid_reporter_filter`: Reporter omission lacks an explicit partner. `upstream_validation`: UN Comtrade rejects the query dimensions. `invalid_upstream_response`: UN Comtrade returns malformed trade data. `no_data`: No trade data available for the reporter and period combination. Other values are possible when a failure originates below the handler."
    • changedOutput schema / properties / error / properties / data / properties / reason / examples
      Previous value: -[
      -  "no_data"
      -]New value: +[
      +  "keyless_hs_edition_unsupported",
      +  "invalid_reporter_filter",
      +  "upstream_validation",
      +  "invalid_upstream_response",
      +  "no_data"
      +]
    • addedOutput schema / properties / partnerCode
      Added value: +{
      +  "description": "M49 partner code used.",
      +  "type": "number"
      +}
    • addedOutput schema / properties / partnerDesc
      Added value: +{
      +  "description": "Partner name supplied by the returned records, or its numeric code when unavailable.",
      +  "type": "string"
      +}
  5. First observed

TDQS

A4.4/5.0
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Beyond the readOnlyHint/openWorldHint annotations, it discloses rate-limit shape (two requests per served period, at least four seconds apart keyless), batching behavior when authenticated, failure handling (rate limits, server errors, timeouts surfaced in unservedPeriods), and a substantive caveat about mirror asymmetry in reported values. This is exactly the behavioral context annotations cannot carry.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The operation is front-loaded in the first clause and the rest is dense with load-bearing detail (rate limits, return shape, mirror caveat). It is packed tightly rather than padded, though the run of caveats and error notes makes it more encyclopedic than scannable.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a six-parameter, open-world read tool, the definition covers rate limits, partial-failure reporting, error codes, the reporter-versus-partner reporting caveat, and the export/import/balance/coverage return surface, with the output schema available for exact shapes. Nothing essential to calling it correctly is missing.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents frequency inference, period mixing rules, overlap rejection, and the reporter/partner code error semantics. The description adds only the cross-tool pointer for resolving reporter codes and reinforces the values-are-as-reported meaning, so it sits at the baseline rather than adding much new parameter meaning.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The first clause gives a specific verb and resource ('Compute the trade balance for a country over one or more periods') and even states the formula (total exports minus total imports), which distinguishes it from the sibling flow-retrieval tool comtrade_get_trade_flows. An agent can identify the operation without opening the schema.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

It establishes clear context — optional commodity filter, keyless versus authenticated throughput — and points to comtrade_lookup_countries as the prerequisite for resolving reporter codes. It stops short of naming an explicit alternative for retrieving raw flows instead of a derived balance, so the when-to-use-versus-sibling reasoning is only partly supplied.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Try in Browser

Glama MCP Gateway

Add one secure layer between your agents and this server.