Metadata-Version: 2.5
Name: geosgb-mcp
Version: 0.9.0
Summary: MCP server for the geological data of the Geological Survey of Brazil (SGB/CPRM): mineral occurrences, outcrops and geological map units
Project-URL: Homepage, https://github.com/SidneyBissoli/geosgb-br-mcp
Project-URL: Repository, https://github.com/SidneyBissoli/geosgb-br-mcp
Project-URL: Issues, https://github.com/SidneyBissoli/geosgb-br-mcp/issues
Project-URL: Leia-me (pt-BR), https://github.com/SidneyBissoli/geosgb-br-mcp/blob/master/LEIA-ME.md
Author: Sidney
License: MIT
License-File: LICENSE
Keywords: brazil,geology,mcp,mining,rare-earth
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: GIS
Requires-Python: >=3.10
Requires-Dist: httpx>=0.27.0
Requires-Dist: mcp<3,>=2.0.0
Requires-Dist: pydantic>=2.0.0
Provides-Extra: dev
Requires-Dist: jsonschema>=4.0.0; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.23.0; extra == 'dev'
Requires-Dist: pytest>=8.0.0; extra == 'dev'
Description-Content-Type: text/markdown

# GeoSGB MCP — Brazilian geological data in your AI assistant

[![CI](https://github.com/SidneyBissoli/geosgb-br-mcp/actions/workflows/ci.yml/badge.svg)](https://github.com/SidneyBissoli/geosgb-br-mcp/actions/workflows/ci.yml)
![Python](https://img.shields.io/badge/python-3.10%2B-3776AB)
![MCP](https://img.shields.io/badge/MCP-stdio-1f6feb)
[![License: MIT](https://img.shields.io/badge/license-MIT-yellow)](https://github.com/SidneyBissoli/geosgb-br-mcp/blob/master/LICENSE)

🇧🇷 [Leia em Português](https://github.com/SidneyBissoli/geosgb-br-mcp/blob/master/LEIA-ME.md)

An [MCP](https://modelcontextprotocol.io) server that brings to Claude, ChatGPT or any MCP
client the data of **GeoSGB**, the geoportal of the **Geological Survey of Brazil
(SGB/CPRM)**: mineral occurrences, geological outcrops and the units of the geological map
of Brazil.

Getting at this data today means knowing the SGB's ArcGIS portal, its layer names and its
query syntax. With this server, geologists, researchers, students and mining-sector analysts
ask in plain language ("where are the rare earth occurrences in Goiás?") and get the records
from the official source, with coordinates, municipality, economic status and a reference to
where each value came from.

> **Independent project.** A community-built client of the geoportal's public services, not
> affiliated with or endorsed by the Geological Survey of Brazil. The data belong to the SGB;
> see [Data provenance and license](#data-provenance-and-license).

## Contents

- [Questions it answers](#questions-it-answers)
- [Installation](#installation)
- [Usage](#usage)
- [Data provenance and license](#data-provenance-and-license)
- [Built with](#built-with)
- [Development and tests](#development-and-tests)
- [Contributing](#contributing)
- [License](#license)

## Questions it answers

In plain language, inside the MCP client. The assistant picks the tool and the filters:

| Question | What the server does |
|---|---|
| Where are the rare earth occurrences in Goiás? | rare-earth search in GO: Catalão, Ouvidor, Nova Roma… |
| Which minerals occur around Araxá (MG)? | occurrences in a rectangle around Araxá: niobium, phosphorus, barium… |
| Which gold mines are recorded in Minas Gerais? | substance "Ouro", state "MG", economic status "Mina" |
| List lithium occurrences in Brazil | substance "Lítio", the whole country |
| Which geological unit lies beneath Belo Horizonte? | point query on the 1:1,000,000 geological map |
| Which outcrops has the SGB described in Ouro Preto? | outcrops by state and municipality |

**The source is in Portuguese.** You can ask in English, but substance names, economic
statuses and municipalities are matched with the source's Portuguese spelling, accents
included: "Ouro" (gold), "Lítio" (lithium), "Nióbio" (niobium), "Terras raras" (rare
earths). The assistant translates when it builds the filters, and `list_mineral_substances`
returns the exact names. Records come back with the source's Portuguese values.

A sample response, for the unit beneath downtown Belo Horizonte (excerpt; the full response
also carries eras, periods and the whole provenance block):

```json
{
  "count": 1,
  "units": [
    {
      "code": "A34bh",
      "name": "Complexo Belo Horizonte",
      "hierarchy": "Complexo",
      "lithotypes": "Biotita gnaisse, Gnaisse, Granodiorito, Migmatito, Ortognaisse",
      "age_min_ma": 2500.1,
      "age_max_ma": 3200.0,
      "eon_min": "Arqueano"
    }
  ]
}
```

And the footer that comes with every response, for clients that only show text (it is
written in Portuguese, like the source):

```text
---
Fonte: Serviço Geológico do Brasil (SGB/CPRM) — GeoSGB · https://geoportal.sgb.gov.br/server/rest/services/geologia/litoestratigrafia_1000000/MapServer/0/query?where=1%3D1&f=json&geometry=-43.94%2C-19.92&geometryType=esriGeometryPoint&spatialRel=esriSpatialRelIntersects&inSR=4326 · extraído em 04/12/2026 às 00:55 (UTC)
Licença: Serviço Geológico do Brasil - CPRM (copyrightText do serviço; a fonte não declara licença de uso).
```

## Installation

### Prerequisites

- [uv](https://docs.astral.sh/uv/getting-started/installation/) (recommended), which fetches
  Python and the dependencies by itself; **or** Python 3.10 or newer, with `pip`.
- Internet access: queries go to the SGB geoportal when the question is asked.
- No account, key or sign-up.

### Claude Desktop

Open the configuration file (*Settings → Developer → Edit Config*) and add:

```json
{
  "mcpServers": {
    "geosgb": {
      "command": "uvx",
      "args": ["geosgb-mcp"]
    }
  }
}
```

Restart Claude Desktop. The tools show up under the name `geosgb`.

### Claude Code

```bash
claude mcp add geosgb -- uvx geosgb-mcp
```

### Other MCP clients

Any client that runs servers over stdio works, with the same command:

```bash
uvx geosgb-mcp
```

### With pip

```bash
pip install geosgb-mcp
geosgb-mcp
```

### From source

```bash
git clone https://github.com/SidneyBissoli/geosgb-br-mcp
cd geosgb-br-mcp
pip install -e .
geosgb-mcp            # or: python -m geosgb_mcp.server
```

## Usage

### Tools

| Tool | What for | Required filter |
|---|---|---|
| `search_mineral_occurrences` | Mineral occurrences by substance, state, municipality, economic status and/or rectangle | none |
| `search_rare_earth_occurrences` | Ready-made rare-earth search by state and/or rectangle | none |
| `get_occurrence_details` | The full record of one occurrence: rocks, province, map sheet, project | the `id` |
| `list_mineral_substances` | Every substance, with the exact spelling the search requires | — |
| `get_geological_outcrops` | Outcrops: rock described in the field, not ore | state + municipality, or a rectangle of up to 1 square degree |
| `get_lithology_by_area` | Units of the 1:1,000,000 geological map in a rectangle or beneath a point | a rectangle of up to 25 square degrees, or a point |
| `search` / `fetch` | ChatGPT Deep Research contract: finds mineral substances and provinces and reads a document | — |

All tools are read-only. A request without the required filter is refused before it reaches
the portal, with a message saying what was missing.

### Resources

Read without calling the portal:

- `geosgb://guide`: the usage guide, with the tool flow, what each filter accepts and the
  source's limits (in Portuguese).
- `geosgb://reference/substances`: the recorded substances and mineral provinces, with the
  number of occurrences of each.

### Filling in the filters

- **State (`uf`)** is the two-letter code in capitals: `"MG"`, not `"mg"` or `"Minas"`.
- **Substance and municipality follow the source's spelling, accents included.** "Nióbio"
  finds hundreds of occurrences; "Niobio" finds none. When in doubt, the assistant calls
  `list_mineral_substances`.
- **Economic status** is one of four values: "Mina" (mine), "Garimpo" (artisanal mining),
  "Indeterminado" (undetermined) or "Não explotado" (not exploited).
- **Rectangle (bbox)** in WGS84 degrees, with all four corners (`bbox_xmin`, `bbox_ymin`,
  `bbox_xmax`, `bbox_ymax`) or none.
- **Rare earths:** by default the search looks at the substance. With
  `include_related_rocks=True` it adds the rocks that typically host them and returns many
  more records, almost all of them pegmatites; use it when the target is the rock, not the
  element.

### What to expect from the source

The SGB geoportal does not paginate: each query downloads everything that matches the
filter, and `limit`/`offset` are applied afterwards, in ascending id order (`limit` goes up
to 120, or 100 geological units, so that a page fits in what the client receives;
`total_count` says how many there are). A filtered search usually answers in
under a second; a whole state's occurrences take a few seconds; the full substance list takes
about 20 seconds the first time and then comes from a 24-hour cache. With a "cold" portal,
the first call can take much longer. That is why outcrops and geological units require a
cut: they are hundreds of thousands of points and tens of thousands of polygons.

In `get_lithology_by_area` the response is computed by the server: the portal returns
polygons and the server groups them by unit. The `area_deg2` field sums the area of the
whole polygons that touch the cut, in square degrees. It is for ranking the units, not for
measuring the area inside the cut.

## Data provenance and license

Every response says where it came from, in three places:

- in the result's `provenance` block: layer queried, filter applied, a URL that reproduces
  the query on the portal, and the extraction timestamp;
- in the response's `_meta`, for clients that audit or display the source;
- in the text footer shown above, for clients that only show text.

The SGB declares no license of use for these services: it publishes only the notice "Serviço
Geológico do Brasil - CPRM", and states at [sgb.gov.br/dados-abertos](https://www.sgb.gov.br/dados-abertos)
that it has no Open Data Plan. When using the data, cite:
**Fonte: Serviço Geológico do Brasil (SGB/CPRM) — GeoSGB**.

## Built with

- Python 3.10+, with the official [MCP SDK](https://github.com/modelcontextprotocol/python-sdk)
- [httpx](https://www.python-httpx.org/) for the queries to the SGB's ArcGIS REST portal
- [Pydantic](https://docs.pydantic.dev/) for the input and output schemas the server
  publishes to the client
- pytest and jsonschema in the tests; [mcpscore](https://pypi.org/project/mcpscore/) for the
  MCP conformance audit

## Development and tests

```bash
pip install -e ".[dev]"
pytest                       # offline suite, with a simulated portal (the one CI runs)
INTEGRATION_TESTS=1 pytest   # adds the suites that hit the real geoportal
```

The offline suite starts the real server in an in-memory MCP session and checks each tool
against the schema it publishes. CI also audits the server with `mcpscore` and requires a
full score. Every week a job checks that the portal's services still have the fields and
formats the server expects.

Each tool is tested in four layers, from one table of cases with fixed arguments and
content checks (`tests/support/tool_cases.py`):

1. **Coverage** (`tests/test_tool_coverage.py`, offline): every tool listed by the server has
   a case, a recording, a tool-selection query, an answer question and a worst case for the
   size gates; and every tool with `limit` has `offset`.
2. **Live** (`tests/test_tools_live.py` and `tests/test_tamanho.py`, with
   `INTEGRATION_TESTS=1`): each case against the real portal, and the worst case of each
   tool within the ~80,000 characters a client receives.
3. **Replay** (`tests/test_tool_replay.py`, offline): each case against the portal's
   recorded response in `tests/fixtures/tools/`, written only by
   `python scripts/record_tool_fixtures.py [case id]`.
4. **Evals** with a real model: `python -m evals.run_answers` asks one question per tool
   through `claude -p` and checks the facts in the answer; `python -m evals.run_selection`
   measures tool choice through the Anthropic API (paid; needs `ANTHROPIC_API_KEY`).

Two scripts regenerate the files in `src/geosgb_mcp/data/`:
`python scripts/gerar_referencia.py` (the substance and province list, read from the portal)
and `uv run --no-project --with pillow python scripts/gerar_icone.py` (the icon).

## Contributing

- **Found an error or odd data?** Open an
  [issue](https://github.com/SidneyBissoli/geosgb-br-mcp/issues) with the question asked,
  the tool and filters used, and what you expected to see. English or Portuguese.
- **Want to propose a change?** Open an issue before a large pull request, so we can agree
  on the design. In the pull request, run `pytest` and add tests for what changed.

## License

Code under the [MIT](https://github.com/SidneyBissoli/geosgb-br-mcp/blob/master/LICENSE) license. The data queried belong to the Geological Survey of
Brazil; see [Data provenance and license](#data-provenance-and-license).
