Overall structure
The server is organized into three main components:- Main server (
main.py) - FastMCP server setup, transport configuration, and monitoring middleware - Helpers (
helpers/) - API client modules for interacting with external services - Tools (
tools/) - MCP tool implementations that expose functionality to AI assistants
Main server
The main server (main.py) configures and runs the FastMCP server with HTTP transport.
FastMCP setup
The server uses FastMCP with specific configuration:- Stateless HTTP mode: Avoids βSession not foundβ errors with MCP clients that donβt maintain session IDs properly (like Claude Code, Cline, OpenAI Codex)
- Transport security: DNS rebinding protection and origin validation
- Single endpoint: All MCP communication happens through
POST /mcp
Transport security
The server implements DNS rebinding protection as required by the MCP specification:Monitoring middleware
Thewith_monitoring() function wraps the FastMCP app to add:
- Health endpoint:
GET /healthreturns server status and version - Matomo tracking: Tracks requests to
/mcpin the background - Request logging: Logs all HTTP requests
Helpers directory
Thehelpers/ directory contains API client modules that interact with external services:
datagouv_api_client.py- Main data.gouv.fr API client (datasets, dataservices, resources)tabular_api_client.py- Tabular API for querying structured data from resourcesmetrics_api_client.py- Metrics API for dataset and resource analyticscrawler_api_client.py- Crawler API for downloading and parsing resourcesmatomo.py- Matomo analytics trackingsentry.py- Sentry error and performance monitoringenv_config.py- Environment configuration and URL management
- Uses
httpx.AsyncClientfor async HTTP requests - Handles session management (can accept an existing session or create its own)
- Implements error handling and logging
- Respects environment configuration (prod vs. demo)
Example: data.gouv.fr API client
The main API client provides functions for:Tools directory
Thetools/ directory contains MCP tool implementations. Each tool is in its own file and registers itself with FastMCP.
Available tools
Dataset tools:search_datasets.py- Search for datasets by keywordsget_dataset_info.py- Get detailed dataset informationlist_dataset_resources.py- List all resources in a dataset
get_resource_info.py- Get detailed resource informationquery_resource_data.py- Query data from resources via Tabular APIdownload_and_parse_resource.py- Download and parse resources directly
search_dataservices.py- Search for external APIsget_dataservice_info.py- Get dataservice metadataget_dataservice_openapi_spec.py- Fetch and summarize OpenAPI specs
get_metrics.py- Get usage metrics for datasets and resources
Tool registration
Tools are registered intools/__init__.py:
register_*_tool() function that uses the @mcp.tool() decorator to register the tool with FastMCP.
Monitoring integration
Matomo analytics
Matomo tracking is integrated into the monitoring middleware:- Tracks all requests to
/mcp - Runs asynchronously in the background (doesnβt block responses)
- Captures user-agent and request metadata
- Fails silently if tracking fails
Sentry error monitoring
Sentry is initialized at startup (init_sentry()) and provides:
- Error tracking and reporting
- Performance monitoring with configurable sample rates
- Environment-specific tagging (local, preprod, prod)
- No PII (Personally Identifiable Information) collection
SENTRY_DSN environment variable.
Environment configuration
The server supports multiple environments:- Production:
DATAGOUV_API_ENV=prod(default) - uses https://www.data.gouv.fr - Demo:
DATAGOUV_API_ENV=demo- uses https://demo.data.gouv.fr
MCP_HOST- Host to bind to (default:0.0.0.0, use127.0.0.1for local dev)MCP_PORT- Port for HTTP server (default:8000)MCP_ENV- Environment name reported to Sentry (default:local)LOG_LEVEL- Python logging level (default:INFO)SENTRY_DSN- Sentry DSN for monitoring (optional)SENTRY_SAMPLE_RATE- Sampling rate for traces/profiles (default:1.0)