A reference FastAPI service that demonstrates how to integrate Nacos for configuration and service discovery together with the MCP (Model Context Protocol) ecosystem. The project is managed with PDM and targets Python 3.13.
- FastAPI application with custom lifespan that mounts MCP tooling and registers the service to Nacos.
- Runtime configuration syncing from Nacos with YAML/JSON parsing and in-memory cache snapshots.
- Structured logging via Loguru with trace ID propagation utilities (
src/utils). - PDM scripts for common workflows (
dev,start,lint,fmt,typecheck,test).
app/ FastAPI entrypoints, Nacos integration, settings helper
src/utils/ Logging and trace helpers shared across the app
logs/ Local log output (ignored in Docker image)
.nacos-data/ Local Nacos snapshot storage (ignored in Docker image)
- Python 3.13
- PDM
- Nacos server 3.x (tested with 3.0.3) and reachable credentials/namespace (
NACOS_SERVER_ADDR,NACOS_USERNAME,NACOS_NAMESPACE, ...) - Optional: Docker 24+ for containerized deployments
- Upload the sample configuration
nacos-fastapi-mcp-example.yamlto the Nacos Config Service.- Data ID:
nacos-fastapi-mcp-example.yaml - Group:
DEFAULT_GROUP - Namespace: align with
NACOS_NAMESPACEin your environment
- Data ID:
- Adjust the YAML contents as needed for your runtime (database DSN, feature flags, etc.).
- Ensure the Nacos HTTP/GRPC endpoints are accessible from where this FastAPI service runs.
- Copy the environment example and adjust values for your setup:
cp .env.example .env
- Install dependencies with PDM (a virtualenv will be created automatically if configured):
pdm install
- Development mode with hot reload:
pdm run dev
- Production-style single process (relies on the
.envconfiguration):pdm run start
The application exposes:
GET /health— lightweight health probe.GET /config/nacos— current runtime configuration snapshot from Nacos.
The repository ships with a Dockerfile. Because .env is excluded by .dockerignore, pass it at runtime.
Build the image:
docker build -t nacos-fastapi-mcp-example:v0.1.0 .Run the container (map ports and supply the .env file):
docker run -d \
--name nacos-fastapi-mcp-example \
--env-file .env \
-p 8000:8000 \
nacos-fastapi-mcp-example:v0.1.0Adjust the port mapping if you expose a different APP_PORT.
- The default
.env.exampleenables Nacos registration; ensure the Nacos address and credentials are reachable, or toggleMCP_REGISTER_ENABLED/APP_WORKERSfor local debugging. - Logging output lives in
logs/locally; mount a volume or overrideAPP_LOG_PATHif you want to persist logs inside Docker.
For additional context in Chinese, see README.zh.md.