Skip to main content
Glama
Census Concept Search MCP

Census Concept Search MCP

README.md
# Census Concept Search MCP

An [MCP](https://modelcontextprotocol.io) server that lets an AI assistant explore U.S. Census Bureau surveys and their variables. Built with [FastMCP](https://github.com/jlowin/fastmcp) and [uv](https://docs.astral.sh/uv/).

Data source: **[https://api.census.gov/data.json](https://api.census.gov/data.json)** — no API key required.

---

## What it does

The server exposes two tools:

| Tool | Purpose |
|---|---|
| `identify_surveys` | Find Census surveys by vintage year range or exact name. Returns each survey's title, year (`c_vintage`), and a link to its variable list (`c_variablesLink`). |
| `identify_variables` | Fetch variables for a specific survey. Pass the `c_variablesLink` from `identify_surveys`. Returns variable names, labels, and concept categories, paginated. |

**Typical workflow:**

1. Call `identify_surveys` to find surveys covering the years or topics of interest.
2. Pick a survey and pass its `c_variablesLink` to `identify_variables`.
3. Page through variables using `limit` / `offset` until you find what you need.

---

## Repo structure

```
census-concept-search-mcp/
├── README.md
├── main.py                        # Local dev entry point
├── pyproject.toml                 # Dependencies and package config (uv)
├── requirements.txt               # Runtime deps mirror (for buildpack hosts)
├── Dockerfile                     # Container image (HTTP, port 8080)
├── manifest.yaml                  # cloud.gov (Cloud Foundry) deploy
├── .env.example                   # Copy to .env for local development
├── src/
│   └── census_concept_search_mcp/
│       ├── app.py                 # Creates FastMCP instance, wires everything up
│       ├── config.py              # Pydantic settings (transport, host, port)
│       ├── models.py              # Shared Pydantic models
│       ├── utils.py               # fetch_json(), paginate()
│       ├── routes.py              # /health and /version HTTP routes
│       ├── census_api_client_wrapper.py  # CensusAPI: fetch, flatten, filter
│       ├── tools/
│       │   ├── __init__.py        # register_tools(mcp) aggregator
│       │   └── search_census_api.py      # identify_surveys + identify_variables
│       ├── prompts/
│       │   ├── __init__.py
│       │   └── example.py         # find_census_variables prompt
│       └── resources/
│           ├── __init__.py
│           └── example.py         # census://info/server static resource
├── tests/
│   ├── test_server.py             # Smoke + tool discoverability tests
│   ├── test_census_api_client.py  # CensusAPI unit tests (CSV fixtures, no network)
│   ├── test_search_census_api.py  # Tool helper unit tests (mocked CensusAPI)
│   └── data/                      # CSV fixtures used by tests
└── deploy/
    ├── README.md                  # Deployment target chooser
    ├── ibm/
    │   └── local-mcp-toolkit/     # watsonx Orchestrate local stdio toolkit
    └── databricks/                # Databricks Apps kit
```

---

## Getting started

### Prerequisites

- [uv](https://docs.astral.sh/uv/) — `pip install uv` or `brew install uv`

### Install and run

```bash
cp .env.example .env
uv sync
uv run python main.py
```

The server starts in **stdio** mode — it speaks JSON-RPC over stdin/stdout and waits for a client to connect. It does not print a listening URL; local MCP servers are launched *by* the client as a subprocess.

### Verify

```bash
uv sync --group dev
uv run pytest tests/ -v    # 40 tests, all should pass
uv run ruff check .        # lint
```

---

## Connect a client

### Claude Code

Create or edit `.mcp.json` in the repo root (or your user MCP settings):

```json
{
  "mcpServers": {
    "census-concept-search": {
      "command": "uv",
      "args": ["run", "python", "main.py"],
      "cwd": "/absolute/path/to/census-concept-search-mcp"
    }
  }
}
```

### Claude Desktop

Add the same block to:
- **macOS:** `~/Library/Application Support/Claude/claude_desktop_config.json`
- **Windows:** `%APPDATA%\Claude\claude_desktop_config.json`

Restart Claude Desktop, then ask something like:

> "Find Census surveys from 2010 to 2015, then show me variables related to employment in the first one."

---

## HTTP mode (cloud / container)

The server automatically switches to HTTP when a platform injects a port:

```bash
# Try it locally:
MCP_TRANSPORT=streamable-http uv run python main.py

# In another terminal:
curl http://localhost:8000/health   # {"status":"healthy","service":"census-concept-search-mcp"}
```

Cloud hosts (IBM Code Engine, Databricks Apps, cloud.gov) set `DATABRICKS_APP_PORT` or `PORT` automatically — no config change needed.

---

## Pagination

`identify_variables` returns a standard pagination envelope. When `has_more` is `true`, call again with `next_offset`:

```json
{
  "total": 847,
  "count": 100,
  "offset": 0,
  "items": [ ... ],
  "has_more": true,
  "next_offset": 100
}
```

---

## Deploying

See **[deploy/README.md](deploy/README.md)** for the full chooser. Two options are ready to use:

### IBM watsonx Orchestrate (local stdio toolkit)

Ships the server code directly to Orchestrate. No container, no HTTP endpoint — Orchestrate installs deps and runs `python server.py` over stdio internally.

```bash
# 1. Fill in the .env:
cp deploy/ibm/local-mcp-toolkit/.env.example deploy/ibm/local-mcp-toolkit/.env
#    Set WXO_INSTANCE_URL and WXO_ENV_NAME.

# 2. Export your API key (never written to disk):
export WXO_API_KEY="<your key>"

# 3. Register:
bash deploy/ibm/local-mcp-toolkit/register-toolkit.sh
```

The script stages a minimal copy of `src/census_concept_search_mcp/` alongside `server.py` and `requirements.txt`, then calls `orchestrate toolkits import`. The toolkit is registered as `census_concept_search_mcp_local`.

### Databricks Apps

See [deploy/databricks/README.md](deploy/databricks/README.md). The app is gated by workspace authentication.

### cloud.gov

The repo root includes a `manifest.yaml` for Cloud Foundry. Deploy with `cf push` after `cf login` / `cf target`.

---

## Configuration

All settings are read from environment variables or `.env`. Copy `.env.example` to get started — the defaults work for local stdio use.

| Variable | Default | Description |
|---|---|---|
| `MCP_TRANSPORT` | `stdio` | `stdio` or `streamable-http` |
| `MCP_HOST` | `0.0.0.0` | Bind address (HTTP mode only) |
| `MCP_PORT` | `8000` | Listen port (HTTP mode only) |

The Census API is public and requires no key.

---

## License

[MIT](LICENSE). See [SECURITY.md](SECURITY.md) for the vulnerability disclosure policy.