# Mato Grosso do Sul Open Data MCP

> Mato Grosso do Sul Open Data connects your AI client to public datasets from Mato Grosso do Sul, Brazil. It allows you to list data packages, search records in CSVs using keywords, or execute complex SQL queries against the raw state data store. You don't need to download files; you just ask for the specific record set.

## Overview
- **Category:** data-analytics
- **Price:** Free
- **Tags:** open-data, brazil, public-records, ckan, sql-query

## Description

This MCP Server gives your AI client direct access to public datasets from Mato Grosso do Sul, Brazil. You bypass the hassle of manually downloading files and filtering massive spreadsheets by letting your agent query the raw state data store directly.

When you use this server, your agent can perform three major functions: discovering available data, inspecting its structure, and executing specific queries against it.

To start, you need to know what records exist. You can run `list_packages` to pull a full inventory of every public dataset package hosted on the MS Open Data Portal. To figure out who owns that data or which department published it, your agent runs `list_organizations` for all contributing government entities and uses `list_groups` to list internal working groups responsible for maintaining those records.

Once you identify a potential dataset, you pull its full details using `get_package`, which fetches metadata about a single package. If you need to know the exact column names or format of a specific file linked within that set, run `get_resource` to pull the structural schema for that resource link.

For instance, if you're hunting for records related to water usage, and you suspect they come from a department called 'Environmental Services,' your agent can use `list_organizations` first. Then, it runs `list_packages` to narrow down the specific dataset package ID associated with that service area.

The core power of this server lies in querying the data itself. Instead of downloading everything and writing custom Python scripts, you just ask for what you need. You can run a general keyword search across structured CSV or spreadsheet resources using `datastore_search`. This tool lets you query records based on natural language terms—say, finding all entries mentioning 'irrigation pump' regardless of which column it appears in.

When the general search isn't precise enough, your agent hits the advanced tools. You execute complex, custom SQL queries directly against the raw data store using `datastore_search_sql`. This is where you filter and analyze records with precision; for example, writing a statement like `SELECT * FROM agriculture WHERE yield > 500 AND year = 2023` tells the server exactly what records to pull. It’s built for analysts who need reliable programmatic data collection without ever touching a download button.

The combination of these tools means your agent doesn't just read documentation; it acts like a database analyst, mapping out the available datasets, verifying their internal structure, and then pulling only the precise record set you requested. It keeps everything contained within the state’s public records environment.

## Tools

### datastore_search_sql
Allows execution of custom SQL queries to select and filter specific columns from the data set.

### datastore_search
Searches records across the data store based on general keywords provided by the user.

### get_package
Retrieves detailed metadata about a single, specified dataset package by its identifier.

### get_resource
Pulls the structural details or schema for a specific file link within a larger data set.

### list_groups
Returns a list of all defined working groups associated with the open data records.

### list_organizations
Lists every government organization that contributes data to the portal.

### list_packages
Returns a complete list of all public dataset packages available for querying.

## Prompt Examples

**Prompt:** 
```
List all the public data packages available in Mato Grosso do Sul.
```

**Response:** 
```
I've retrieved the list of available packages. There are several datasets including 'frota-de-veiculos', 'receita-corrente-liquida', and 'gastos-com-pessoal'. Which one would you like to explore?
```

**Prompt:** 
```
Search for records containing 'Campo Grande' in the resource ID 550e8400-e29b-41d4-a716-446655440000.
```

**Response:** 
```
Searching the DataStore... I found 15 records matching 'Campo Grande' in that resource. Most relate to urban infrastructure projects. Should I list the specific values for you?
```

**Prompt:** 
```
Run a SQL query to select all columns from resource 'receita-2023' where the value is greater than 1000000.
```

**Response:** 
```
Executing SQL query on the DataStore... The query returned 8 high-value entries. The largest entry is for 'Transferências Constitucionais' with a value of 5,400,000. Would you like the full breakdown?
```

## Capabilities

### List all available datasets
Retrieves a list of every public data package hosted on the MS Open Data Portal.

### Search records by keyword
Queries and finds specific entries within structured CSV or spreadsheet resources using natural language search terms.

### Run advanced SQL queries
Executes complex, custom SQL statements against the data store to filter and analyze records (e.g., `SELECT * FROM table WHERE value > 1000`).

### Get dataset metadata
Fetches detailed information—like schema or description—for a single, specified data package.

### Identify data owners and groups
Lists the government organizations and internal working groups responsible for creating or maintaining the public records.

## Use Cases

### Tracking infrastructure spending
A researcher needs to compare construction spending across different municipal areas. They first use `list_packages` to find the right financial dataset, then they run a targeted `datastore_search_sql` query: `SELECT * FROM 'finance-data' WHERE area = 'X' AND value > 10000`. The agent returns only the relevant records for comparison.

### Identifying data gaps
A developer needs to build an app that tracks public transportation. They run `list_organizations` to see which groups manage vehicle or route data, and then use `get_resource` on those organizations' datasets to map out the specific field names they need for their database model.

### Comparing resource availability
A student is writing a paper comparing historical records. They first run `list_packages` to see all available time-series data, then use `get_package` on the relevant package to ensure it has enough granularity (e.g., daily vs. monthly) before committing to an analysis.

### Finding specific incidents
An analyst suspects a record about 'Campo Grande' exists in a large, complex dataset. They don't know the exact table name, so they start by using `datastore_search` with the keyword 'Campo Grande'. The agent quickly returns matching records and their source package.

## Benefits

- Analyze raw data without downloading files. Instead of exporting a massive CSV and running local filters, use `datastore_search` to filter and retrieve the exact records you need right in your chat interface.
- Write complex queries like a seasoned DBA. The `datastore_search_sql` tool lets you execute advanced SQL statements—like joins or aggregates—directly against the state data store. This is massive time savings.
- Automate metadata collection. Use `list_packages` and `get_package` to build an inventory of all available datasets automatically, skipping the tedious process of manually visiting dozens of government web pages.
- Understand data ownership instantly. Running `list_organizations` gives you a clear map of which state body is accountable for every dataset, improving research traceability.
- Inspect data structures on demand. If you find a resource link but don't know its schema, `get_resource` tells you exactly what columns and fields are available before you try to query it.

## How It Works

The bottom line is: it runs state-level queries against public records without needing an intermediary download step.

1. Subscribe to the server and provide your Mato Grosso do Sul Data Portal API Key.
2. Direct your AI client (Claude, Cursor, etc.) to run a data query using a specific tool name, like `datastore_search` or `list_packages`.
3. The agent executes the function call, fetches the raw public data through the server, and returns the filtered results directly to you.

## Frequently Asked Questions

**How do I list all available datasets using the list_packages tool?**
Call `list_packages` first. This returns a list of packages—think of them as high-level folders containing groups of data. You'll need to select one package ID from that list before you can query its contents.

**Can I use datastore_search for complex joins?**
No, `datastore_search` is designed for simple keyword lookups within a single resource. For joining data across multiple sources or applying filters like 'AND' and 'OR', you must use the dedicated `datastore_search_sql` tool.

**What if I don't know the exact file structure?**
Run `get_resource`. Give it a specific resource link or ID, and this tool will pull the metadata, showing you exactly what fields are available. It's like getting the table of contents before reading chapters.

**Do I need to know every organization name?**
Not necessarily. Use `list_organizations` first. This gives you a list of contributors, allowing you to narrow down your search scope and focus on the most relevant data sources for your project.

**How do I authenticate before running a query with datastore_search?**
You must supply your Mato Grosso do Sul Data Portal API Key in the server configuration. Without this key, any tool call will fail immediately with an authentication error.

**Can datastore_search_sql handle complex data types like dates or coordinates?**
Yes, it supports standard SQL functions for date manipulation and geometry queries. Use the 'YYYY-MM-DD' format to ensure accurate filtering on time-series records.

**What happens if I run get_resource with an invalid ID?**
The tool returns a clear 404 error indicating the resource was not found. Before querying, check the parent package metadata using `get_package` to verify the correct link.

**Are there rate limits when calling list_packages repeatedly?**
The server follows the official API rate limit policy of the state portal. For high-volume data collection, your agent needs to implement an exponential backoff strategy.

**How do I find specific datasets about health or education?**
Use the `list_packages` tool to see all available dataset names, then use `get_package` with a specific ID to see the resources and descriptions related to that topic.

**Can I perform complex filtering on the data records?**
Yes! Use the `datastore_search_sql` tool to execute standard SQL queries (e.g., SELECT, WHERE, GROUP BY) directly against the DataStore resources for precise analysis.

**How do I see which government departments provide the data?**
Use the `list_organizations` action. It will return a list of all state entities and departments that have published data on the portal.