# MDIC (Comércio Exterior) MCP

> MDIC (Comércio Exterior) connects your AI client directly to Brazil's Ministry of Development, Industry, Trade and Services (MDIC) open data portal. It lets you list all available trade datasets, search for specific packages by keyword, inspect dataset metadata, and query the full datastore using SQL-like parameters for export/import statistics.

## Overview
- **Category:** data-analytics
- **Price:** Free
- **Tags:** brazil-trade, export-import, economic-data, ckan-api, trade-statistics

## Description

**WHO IT'S FOR:** This server connects your AI client straight into Brazil’s Ministry of Development, Industry, Trade and Services (MDIC) open data portal. You use it when you need official Brazilian foreign trade statistics—you wanna pull the numbers directly from the source without downloading a mountain of files.

***

**How It Works:** Your agent handles the entire process: finding the right dataset, checking its scope, and running complex queries against the actual data rows. You never have to deal with manual CSV downloads again.

*   **Finding Datasets (Discovery):** First, you need to know what's available. Use `list_packages` if you want a complete inventory of every single trade dataset package hosted on the MDIC portal. If that list is too long and you know what keywords you're after—like 'exportação' or 'importação'—you can use `search_packages`. That narrows down the results instantly, pointing your agent right to relevant packages.

*   **Inspecting Data (Metadata):** Once you've identified a package ID, don't just assume it has what you need. You run `get_package` to pull detailed metadata for that specific dataset. This gives you tags and confirms the full scope of the data before you commit to running a query. If you then want to check the individual files within that confirmed package, `get_resource` provides all the necessary metadata and download paths for those resources.

*   **Querying the Data (Extraction):** This is where the money's at. You don’t just pull whole tables; you pull specific metrics. Using `search_datastore`, your agent executes queries that function like SQL directly against the underlying data rows. You can filter and limit the results to pull only the exact trade statistics—like specific export volumes or import values—that fit your criteria, making sure you get clean, usable numbers straight into your workflow.

## Tools

### get_package
Gets detailed metadata for a single, specified data package (dataset).

### get_resource
Retrieves metadata and links for a specific file resource within an MDIC package.

### list_packages
Lists every single available dataset (package) in the entire MDIC portal.

### search_datastore
Runs a query directly against the data rows to pull filtered results from a resource.

### search_packages
Finds relevant dataset packages using a keyword search string.

## Prompt Examples

**Prompt:** 
```
Search for MDIC datasets related to 'exportação'.
```

**Response:** 
```
I found several datasets. The most relevant are 'exportacao-municipios-detalhada' and 'comex-stat-exportacao'. Which one would you like to inspect for metadata?
```

**Prompt:** 
```
List the resources available in the 'comex-stat' package.
```

**Response:** 
```
The 'comex-stat' package contains 12 resources, including 'Exportações Mensais (CSV)' and 'Importações Mensais (CSV)'. I have the resource IDs ready if you want to query the data.
```

**Prompt:** 
```
Query the first 5 rows from resource ID d3b0c1... with a limit of 5.
```

**Response:** 
```
Retrieved 5 rows from the datastore. The data includes columns for 'Year', 'Month', 'Product_Code', and 'Value_FOB'. Would you like me to summarize these results?
```

## Capabilities

### List all available datasets
Retrieves a full inventory of every trade data package hosted on the MDIC portal.

### Search for specific dataset packages
Filters and finds relevant data packages using keywords like 'exportação' or 'importação'.

### Get metadata for a package
Retrieves detailed information, tags, and resource lists for one known dataset ID.

### Get file details and links
Provides metadata and download paths for individual data files within a selected package.

### Run filtered queries on data rows
Executes SQL-like searches directly against the datastore to pull specific, limited rows of trade statistics.

## Use Cases

### Tracking a specific product's export history
A user needs to see how the value of Product Code X changed over three years. They ask their agent, and the agent runs `search_packages` for 'comex-stat', then uses that ID with `search_datastore` to filter by the specific product code and date range, delivering only the needed rows.

### Identifying all available trade data
A new analyst joins the team and needs a complete list of everything MDIC offers. They simply trigger `list_packages`, which immediately provides every dataset name, eliminating manual portal browsing.

### Verifying file availability
An engineer knows they need the 'Monthly Exports' data but can't find the exact download link in the documentation. They run `get_package` first to confirm the dataset, then use that ID with `get_resource` to get the precise metadata and working URL.

### Comparing multiple market sectors
A researcher wants to compare trade data across three different geographic regions. They ask their agent to run targeted queries using `search_datastore`, passing in the necessary filters for all three areas simultaneously, getting a single comparative result set.

## Benefits

- Find the right data faster. Instead of clicking through dozens of links, use `search_packages` to filter datasets by terms like 'exportação' or 'comex-stat'.
- Skip manual downloads entirely. You don't grab a CSV; you ask your agent to run a query using `search_datastore`, and the structured data comes back instantly.
- Full transparency on resources. Use `get_resource` when you need confirmation of specific file IDs or download links before integrating them into a script.
- Discover every dataset available. If you don't know what data exists, start with `list_packages` to get the full inventory of all trade packages.
- Analyze trends, not just records. The server handles complex joins and filtering via `search_datastore`, letting your agent focus on summarizing results.

## How It Works

The bottom line is you don't need to know the API structure; you just talk to your AI client and it runs the right sequence of tools.

1. Subscribe to the server and connect your preferred AI client (Claude, Cursor, etc.).
2. (Optional) Provide an API key if you hit rate limits. Otherwise, nothing else is needed.
3. Ask your agent: 'Find all datasets about exportations,' or 'Query the top 5 rows from the comex-stat datastore.'

## Frequently Asked Questions

**How do I find out what datasets are available using search_packages?**
Run `search_packages` and pass the general topic, like 'exportação'. This tool filters through all MDIC packages to give you a shortlist of relevant dataset names.

**Do I need get_package before searching_datastore?**
No. While `get_package` gives detailed metadata, `search_datastore` is the action tool. You typically use discovery tools first to find the package ID, and then pass that ID directly into `search_datastore`.

**What does get_resource do?**
`get_resource` pulls metadata for a specific data file within a dataset. Use it if you need to confirm the exact download link or format for a resource ID you found earlier.

**Can I query any date range using search_datastore?**
Yes, `search_datastore` accepts SQL-like parameters. You specify your criteria (e.g., 'Year > 2020 AND Month = 1') to limit the rows returned.

**When using `list_packages` or other tools repeatedly, how do I handle API rate limits?**
You must provide an API key for high-volume usage. The server supports keys to bypass standard rate limits imposed by the MDIC portal. If you don't include a key, your AI client will receive a 429 error response when hitting predefined request thresholds.

**If I need to filter data from multiple criteria (e.g., Year AND Product Code) using `search_datastore`, how should I structure the query?**
You use standard SQL-like syntax for filtering within the tool's parameters. You combine conditions using logical operators like 'AND' or 'OR'. For example, specifying WHERE year = 2023 AND product_code = 'XYZ'.

**What specific metadata does `get_package` return about a dataset?**
It returns core information including the package title, description, list of contained resources (data files), and associated tags. This allows your AI client to understand the scope and purpose of the entire dataset before querying any single resource.

**What happens if I use `search_packages` with an invalid or misspelled query string?**
The tool returns a structured error message detailing the validation failure. It won't crash; instead, it tells your agent exactly which part of the search input was malformed and why, allowing for immediate correction.

**Can I search for specific trade terms like 'soybean' or 'iron ore'?**
Yes! Use the `search_packages` tool with your query string. It will return all datasets matching those terms within the MDIC portal.

**How do I access the actual data rows inside a CSV resource?**
Use the `search_datastore` tool by providing the `resource_id`. You can also apply filters and limits to retrieve exactly the data points you need.

**Is it possible to see the file format and download URL for a dataset?**
Yes. The `get_package` tool returns a list of resources, and `get_resource` provides specific metadata including the format (CSV, XLSX, etc.) and the direct download URL.