Skip to main content
Glama
japan-estat-mcp

japan-estat-mcp

README.md
# japan-estat-mcp

A multi-user Model Context Protocol server for Japan's e-Stat API. It supports
stdio and authenticated Streamable HTTP, with the skeleton's optional LangGraph
agent, A2A endpoint, durable worker, Vault/Postgres persistence, and pg-boss
scheduling.

This is a new, standalone adaptation of the MIT-licensed
[skeleton-mcp](https://github.com/LesterAJohn/skeleton-mcp), based on commit
`bfbf26a6939a3c3c387a34bd2461f1bb5d0b8eec`. The existing local API documents
were preserved. The e-Stat app ID is a credential, stored only in Vault; the
agent configuration, task and schedule state remain in scoped Postgres tables.

## API versions and coverage

The adapter implements all callable operations and response formats listed in
the official e-Stat specifications for versions 1.0, 2.0, 2.1, and 3.0. The
machine-readable [endpoint inventory](inventory/operations.json) and generated
[endpoint-to-tool coverage](inventory/COVERAGE.md) map every HTTP method/path
and output-format variant to an MCP tool. There are 25 version-operation tools
covering 65 documented endpoint-format combinations.

| API version | Official specification | Operations |
| --- | --- | --- |
| 1.0 | [Official PDF](https://www.e-stat.go.jp/api/sites/default/files/uploads/2014/12/API-specVer1.0.pdf) (release 2014-10-31; document revision May 2018) | `getStatsList`, `getMetaInfo`, `getStatsData`, `postDataset`, `refDataset` |
| 2.0 | [Official HTML](https://www.e-stat.go.jp/api/api-info/e-stat-manual) (2015-01-30) | v1.0 operations plus `getDataCatalog` |
| 2.1 | [Official HTML](https://www.e-stat.go.jp/api/api-info/e-stat-manual2-1) (2016-07-14) | v2.0 operations plus `getStatsDatas` |
| 3.0 | [Official HTML](https://www.e-stat.go.jp/api/api-info/e-stat-manual3-0) (2019-07-26) | Seven operations, including `getDataCatalog` and `getStatsDatas` |

The official [specification index](https://www.e-stat.go.jp/api/en/api-info/api-spec)
notes that all versions are available over HTTPS. Implemented formats follow
each version's endpoint tables; the MCP schema rejects unsupported
version/operation/format combinations. XML, JSON, JSONP, and simple CSV outputs
are returned in their documented form. JSON is parsed as JSON; the other
formats are returned as redacted text. JSONP requires a validated `callback`.

Every API operation has a dedicated tool named
`estat_v<major>_<minor>_<snake_case_operation>`, for example
`estat_v3_0_get_stats_data`. Each tool takes a required `format` and a
version/operation-specific `parameters` object. Parameter names are from the
official versioned specification; `appId` is deliberately not accepted from
tool callers and is injected from Vault. Use `estat_discover_operations` for
the complete operation schemas and `estat_suggest_query` to produce a
schema-valid placeholder call without making a request.

`getStatsDatas` uses HTTP POST but is read-only. `postDataset` is mutating and
requires administrator authorization; `processMode=D` deletes a saved dataset.
Mutation requests are never retried. Read-only calls have a single bounded
retry for transient transport/upstream failures and honor `Retry-After` up to
one second. Requests time out after `ESTAT_TIMEOUT_MS` (default 15000 ms);
responses are capped at 5 MiB. Pagination uses the e-Stat `startPosition` /
`NEXT_KEY` flow. Errors and response bodies are scrubbed so the app ID is not
returned.

## Credentials and identity

Register with e-Stat and obtain an application ID before calling API tools.
`estat_credential_set` provisions or rotates the authenticated principal's
credential in Vault. `estat_credential_status` reveals only whether it exists;
`estat_credential_delete` deletes the local Vault value but does not revoke the
ID at e-Stat. Credential tools require `mcp:admin` for OAuth callers, or a
configured `MCP_ADMIN_AUTH_KEY` in key-authorized mode. Rotation does not
invalidate the previous e-Stat ID; revoke it through e-Stat separately.

Credentials use an exact SHA-256 digest of `(tenant_id, principal_type,
principal_id)` under `MCP_SERVER_NAME/estat/credentials/`. There is no
cross-user credential fallback. Identity is taken from authenticated context,
not caller-supplied tenant/principal fields. Stdio and static-token HTTP use
the configured default identity; use Vault-index tokens or verified OAuth
identities to isolate individual HTTP callers.

Read-only e-Stat tools need the caller's scoped app ID but no admin key.
`postDataset` additionally requires `mcp:admin` or the admin key; this gate
does not replace the authority and permissions of the e-Stat account itself.
All e-Stat tools are excluded from autonomous agent capabilities so caller
credentials and potentially large API responses cannot enter tasks,
checkpoints, or agent memory.

## OAuth scope contract

HTTP OAuth uses introspection; this server does not issue tokens. Provider
scope mappings are allowlists configured by the skeleton's OAuth environment
variables. e-Stat itself uses an app ID, not an upstream OAuth scope.

| MCP scope | Purpose and authorization |
| --- | --- |
| `mcp:invoke` | Required by default for Vault-index HTTP bearer tokens; permits authenticated invocation and is required for agent/A2A execution. |
| `mcp:read` | Accepted descriptive read capability; current direct read tools do not enforce it individually. |
| `mcp:write` | Authorizes ordinary mutating operations under OAuth-scope authorization; does not authorize `postDataset` or Vault credential administration. |
| `mcp:admin` | Admin-only Vault credentials/token lifecycle, agent administration, and e-Stat `postDataset`; also satisfies `mcp:write`. |
| `mcp:agent` | Alternative scope for agent invocation where configured. |
| `mcp:tenancy:admin` | Tenant-bounded user/account and token administration. |
| `mcp:agent-config:admin` | Tenant-bounded cross-user agent configuration administration. |
| `mcp:proxy:initialize` | Trusted hosted-proxy caller initialization; only accepted with a configured trusted proxy identity. |

OAuth providers may require an admission scope such as `mcp:invoke`; configure
`MCP_HTTP_OAUTH2_REQUIRED_SCOPES` or the provider-specific equivalent. Configure
the OAuth client's requested scopes to match the server's admission and
authorization policy. In `auto` mode verified OAuth callers use scopes;
stdio/static-token callers use `MCP_ADMIN_AUTH_KEY` for protected operations.
The default Vault-index token verifier requires `mcp:invoke`.

## Run locally

Requirements: Node.js 22+, Postgres, and Vault. Copy `.env.example` to `.env`,
provision infrastructure credentials locally, and never commit `.env`.
`MCP_SERVER_NAME=japan-estat-mcp` is the sole namespace source for Vault paths,
Postgres tables/schemas, checkpoint schema, and queue names. Changing it
changes the persistence namespace and requires an explicit migration plan.
`APP_NAME` is only a legacy migration input.

```sh
npm ci
npm run start:stdio
# or:
npm run start:http
npm run start:both
```

## Persistence and External Services Mode

For local containers, `docker compose up` starts Postgres, Raft-backed Vault,
the MCP HTTP server, and the dedicated agent worker. Run server and worker
with the same `MCP_SERVER_NAME`, database and Vault settings. Agent scheduling
requires `MCP_AGENT_WORKER_ENABLED=true` and `PGBOSS_ENABLED=true`; the worker
is a separate `npm run start:agent-worker` process. The supported external
services path is `docker-compose.external.yml`; configure `POSTGRES_HOST`,
`POSTGRES_PORT`, `POSTGRES_DB`, `POSTGRES_USER`, `POSTGRES_PASSWORD`,
`VAULT_ADDR`, and `VAULT_TOKEN` through deployment secrets. Keep queue and
application tables in the same Postgres database for transactional dispatch.

Agent inference is not configured automatically. Use the existing
`agent_config_*` tools and Vault credential tools to set up inference after
deployment. Agent readiness is distinct from Vault/Postgres health. See the
[inherited skeleton operations reference](docs/SKELETON-README.md), the
[onboarding playbook](agent/playbooks/service-onboarding.md), and
[`vault-production/README.md`](vault-production/README.md) for agent, A2A,
schedule, Vault Raft, managed unseal, backup, and recovery details.

## HTTP TLS

`MCP_HTTP_TLS_ENABLED=false` is the default, matching `.env.example`. HTTP is
for loopback development or a trusted internal connection behind a
TLS-terminating reverse proxy. Native TLS remains configurable:

- Set `MCP_HTTP_TLS_ENABLED=true`.
- Provision `MCP_HTTP_TLS_CERT_PATH` and `MCP_HTTP_TLS_KEY_PATH` externally.
- Optionally configure `MCP_HTTP_TLS_KEY_PASSPHRASE` and
  `MCP_HTTP_TLS_MIN_VERSION` (default `TLSv1.2`).
- Mount certificate and key files read-only in containers.

Startup fails if TLS files are missing or unreadable; it never falls back to
plaintext HTTP. Renew/rotate certificates externally and restart the process.
Use HTTPS client URLs and set `MCP_A2A_PUBLIC_URL` to the HTTPS address. Do not
expose plaintext HTTP outside loopback or a trusted TLS-terminating proxy.

HTTP retains bearer/OAuth authentication, request body limits, rate limits,
and optional IP/origin controls. The default listener is loopback.

## Configuration and checks

Common service variables:

| Variable | Purpose | Default |
| --- | --- | --- |
| `MCP_SERVER_NAME` | Server identity and persistence namespace | `japan-estat-mcp` |
| `ESTAT_TIMEOUT_MS` | e-Stat request timeout | `15000` |
| `MCP_HTTP_TLS_ENABLED` | Native HTTP TLS listener | `false` |
| `MCP_TRANSPORT_MODE` | `stdio`, `http`, or `both` | `stdio` |
| `MCP_HTTP_AUTH_MODE` | `token`, `oauth2`, or `both` | `token` |
| `MCP_AGENT_WORKER_ENABLED` | Agent worker activation | Set `true` in supported deployment |
| `PGBOSS_ENABLED` | Durable queues/scheduler | `true` |

See `.env.example` and the [skeleton environment reference](docs/SKELETON-README.md)
for Postgres, Vault, OAuth, token, agent, worker, A2A, and HTTP hardening
settings. `docker-compose.yml` preserves the `vault-unseal-key-init` startup
ordering. Do not commit credentials, Vault data, or unseal-key material.

Reproducible inventory and local test commands:

```sh
npm run inventory:generate
npm run inventory:check
npm test
```

`inventory:check` verifies deterministic output, unique documented endpoints,
tool mappings, required parameters, and test mapping. GitHub Actions runs
inventory and tests on pull requests and pushes. Install the versioned
pre-push check after Git initialization with
`git config core.hooksPath .githooks`; it runs inventory validation and tests.

Unit and mocked transport tests do not imply live API or infrastructure
verification. Disposable Postgres/Vault integration tests are opt-in and may
skip when services are unavailable. Controlled inference, live e-Stat, and
GitHub Actions checks require their respective external setup. No paid
inference provider or production credential is required for this build.