← All experiments

An MCP server for pharmaceutical trade data from UN Comtrade

Questions about who supplies a country's medicines meant hand-built UN Comtrade queries full of traps. I built an MCP server with 24 tools that query medicine trade and compute the indices the research literature uses.

  • Data
  • Tools
  • Health

Updated7 Jun 2026

The problem

I needed to answer questions about medicine trade from inside a Claude session: how much a country imports and exports, who its suppliers are, how dependent it is on one of them, and what it pays per kilogram. UN Comtrade has the data, but using it well means knowing which product codes count as medicines, which countries inflate their exports through re-exports, and which recent years are still incomplete.

What was out there

UN Comtrade publishes an official Python package for its API. It covers the queries, but not the analytical measures the research literature uses: supplier concentration, revealed comparative advantage, mirror reconciliation, unit values and trade balance indices. Comtrade also has no "World" reporter and carries no tariffs.

What I built

It is a local MCP server with 24 tools. The basic ones pull a country's imports and exports, time series, top partners, product breakdowns, monthly series and national tariff lines. Analytical tools compute supplier concentration, implicit prices in US dollars per kilogram, trade balance and net import reliance, mirror checks between what two countries report, revealed comparative advantage and a re-export signal, each tied to a published method. Two WTO tools add applied tariffs and a cross-check against WTO figures, and a methodology tool returns the method with a citable bibliography. Countries can be given by name in English or Spanish, by ISO code or by UN M49 code.

Dark terminal card. A question asks which countries Colombia buys its medicines from; below it the comtrade_pharma_top_partners call and a markdown table of Colombia's 2023 HS 30 imports, US$ 3.66 billion in total, led by the USA at 20.5 percent and Germany at 16.3 percent.
A plain question and a single tool call give Colombia's top medicine suppliers in 2023, with the US$ 3.66 billion total the page reports.
Dark terminal card. A question asks how exposed Colombia is if one supplier is disrupted; below it the comtrade_pharma_supply_risk call and its markdown answer: an HHI of 0.090, low diversified risk, 70 suppliers, top partner share 20.5 percent, top three 43.0 percent, no single-source dependence.
The supplier concentration tool answers with a published index and the thresholds that grade it.

Equipment

How I built it

  1. Built the first 7 tools on the Comtrade API with Claude Code.
  2. Reviewed the research that uses Comtrade for medicine trade, about 28 papers found through OpenAlex, Scite and Consensus, and full-text checked the methods of 4 key ones.
  3. Added the analytical tools one by one, sending each to Codex and Agy for independent review and fixing what they found.
  4. Compared the server with UN Comtrade's official package and adopted its simpler query mode, plus a free preview endpoint for use without a key.
  5. Added named product baskets so a query can include active ingredients from HS chapter 29, not only finished medicines in chapter 30.
  6. Wrote server-level instructions that warn the assistant about re-export hubs, incomplete recent years and missing tariffs before it writes a query.

Results

  • 24 tools. 22 only read data; the premium bulk and async tools write a file or submit a job.
  • Basket choice changes the answer: for China in 2022, finished medicines alone (HS 30) give about US$14.0 billion of exports, and the WTO basket with active ingredients about US$23.2 billion.
  • The premium bulk download matched the standard API exactly for Colombia's 2023 medicine trade: US$3.66 billion of imports and US$434.5 million of exports. A 12-month series summed exactly to the annual total.
  • In a 2023 check, summing every reporting country instead of the top 40 raised world pharmaceutical exports from US$771 billion to US$809 billion and moved Colombia's 2023 revealed comparative advantage from 0.230 to 0.239.
  • Reporter coverage drops sharply in recent years: about 166 countries for 2023, 138 for 2024 and 67 for 2025.

What I learned

Which products count as medicines changes the answer. HS chapter 30 alone understates China and India, because their active ingredients are classified in chapter 29. Adding them closed China's gap with WTO figures from 71% to within 5%, but it did not fix re-export hubs: the Netherlands stayed 46% below WTO.

The United States, Switzerland, France and India file under codes that differ from their standard UN codes, so a plain numeric query for them returned nothing until I mapped them.

Codex and Agy, reviewing independently, caught a bug I had missed: totals for a product basket took the largest commodity instead of the sum, which undercounted world totals.

Lab notes

  1. I added the WTO and CEPII baskets that include active ingredients, WTO tariff and cross-check tools, a re-export signal, a tool that rebuilds a non-reporting country from its partners' reports, a disk cache and 8 offline unit tests, for 24 tools in total.

  2. The analytical tools came next: supplier concentration, unit values, trade balance, mirror checks and revealed comparative advantage, each reviewed by Codex and Agy. From UN Comtrade's official Python package I took a cleaner query mode, a key-free fallback, monthly data and standard unit values.

  3. I started a server with 7 read-only tools for UN Comtrade medicine trade (snapshots, time series, top partners, product breakdowns and country comparisons) and read about 28 papers that use Comtrade, turning their methods into a list of tools to build.

© 2026 Johnattan García-Ruiz Last updated