Skip to content

Latest commit

 

History

History
123 lines (78 loc) · 9.41 KB

RUN.md

File metadata and controls

123 lines (78 loc) · 9.41 KB

Running Clean Model Context Protocol

There are two pre-configured environments: Development for dotnet run and docker for Docker Compose. Every other environment name counts as deployed, including Production, which is what the image runs as unless you say otherwise. A deployed copy with no authentication refuses to start, so nobody ships an open API by accident; set Authentication:AllowAnonymous to true (Authentication__AllowAnonymous=true) if open is what you want. Logs go to the console in both. Traces, metrics and logs also leave over OTLP whenever OTEL_EXPORTER_OTLP_ENDPOINT is set, which Docker Compose does for you.

Database

The app stores its data in a SQLite file, movie-reviews.db, next to the app. In Docker the file is /app/data/movie-reviews.db, on the movie-reviews-data volume, so it survives a restart. On startup the app applies any pending Entity Framework Core migrations. When Database:SeedSampleData is true, it also seeds the same sample data into an empty database. It's on in appsettings.Development.json and appsettings.docker.json and off everywhere else, so a deployed copy never starts with fake data. Delete the file, or run docker compose down -v, to start again. Change ConnectionStrings:MovieReviews to point somewhere else, or to Data Source=MovieReviews;Mode=Memory;Cache=Shared to keep everything in memory.

To add a migration after changing the data model:

dotnet tool install --global dotnet-ef
dotnet ef migrations add <Name> --project ./src/Infrastructure --output-dir Databases/MovieReviews/Migrations

.NET CLI

launchSettings.json sets the ports, environment and logging, so one command is enough.

dotnet run --project ./src/Presentation

It listens on https://localhost:7321, and on http://localhost:7320, which redirects there. If your browser or curl doesn't trust the .NET development certificate yet, run dotnet dev-certs https --trust once.

Docker Compose

docker-compose.yml starts the app, the MCP Inspector already pointed at it, and the Aspire dashboard, which shows its logs, traces and metrics. The app sends everything to an OpenTelemetry Collector, and the collector forwards it to the dashboard, the same shape as a production setup. The dashboard is only a container that receives OTLP; nothing in the solution depends on Aspire. The dashboard has no login and the collector accepts OTLP from anyone on ports 7323 and 7324. That's on purpose for a developer's machine, so don't run this compose file anywhere other people can reach.

docker compose up -d --build

The container runs as the image's non-root app user.

To shut the containers down:

docker compose down

Chat demo

Want to ask for things in plain English? The optional demo profile adds Open WebUI, already connected to the server:

docker compose --profile demo up -d --build

Open http://localhost:7326, dismiss the release notes and ask it to list the movies. It uses Kilo's free gateway as the model, so there's no account or key, but it only works online, and your prompts and tool results go to Kilo, a third party. Keep it to sample data. Solutions created with --include-auth leave it out, because Open WebUI has no token to send. Stop it with docker compose --profile demo down.

Ports

Every Lean Mean Clean template has its own block of ten ports on your machine, so this solution runs beside one made from another template without a clash. This one uses 7320 to 7329:

Port What listens
7320 The app over HTTP, from Docker Compose or dotnet run
7321 The app over HTTPS, dotnet run only
7322 The Aspire dashboard
7323 The OpenTelemetry Collector, OTLP over gRPC
7324 The OpenTelemetry Collector, OTLP over HTTP
7325 The MCP Inspector
7326 The chat demo

The app listens on 7320 everywhere: under dotnet run, inside its container and on your machine, so docker run -p 7320:7320 works the same way. The dashboard and collector keep their usual ports inside their containers. To move the app, change applicationUrl in launchSettings.json, ASPNETCORE_HTTP_PORTS, EXPOSE and the HEALTHCHECK in the Dockerfile, and its mapping in docker-compose.yml. When you deploy the image, point the platform's target port at 7320. The Inspector also listens on 7325 inside its container, because it only accepts calls from the origin it listens on. 7327 to 7329 are spare for anything you add.

Telemetry

The app always collects OpenTelemetry traces and metrics, and exports them, along with its logs, only when OTEL_EXPORTER_OTLP_ENDPOINT is set. Every other standard OTEL_* variable works too, such as OTEL_SERVICE_NAME, OTEL_EXPORTER_OTLP_PROTOCOL and OTEL_EXPORTER_OTLP_HEADERS, so sending it to Grafana, Honeycomb, Seq or a collector is a settings change.

In production, send the app's telemetry to a collector rather than straight to a backend. Run one beside the app (a sidecar or agent) or as a shared gateway, and give it your backend's exporter. The collector then owns batching, retries, sampling and scrubbing, and changing backend never touches the app. otel-collector.yaml is a starting point: an OTLP receiver, the memory limiter and batch processors, and one exporter to swap.

To see a dotnet run in the dashboard, start the collector and the dashboard, and point the app at the collector:

docker compose up -d otel-collector
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:7323 dotnet run --project ./src/Presentation

Every span, metric and log carries service.version, so you can always tell which build sent it.

Trying it with a token

With an authority set, every call needs a token from it. To try the app on your machine without one, let .NET make you a development token:

dotnet user-jwts create --project src/Presentation

It prints a token, adds a UserSecretsId to the project, keeps the signing key in your user secrets and writes the issuer and audiences into appsettings.Development.json. From then on a dotnet run accepts its tokens instead of your authority's. Nothing changes anywhere else: user secrets and that file only load in Development, and the app refuses to start with development tokens set up in any other environment. If you commit the file, everyone's dotnet run switches to development tokens, and each person makes their own with the same command.

In the MCP Inspector, put the token in Authentication, under Bearer Token, before you connect. Run dotnet user-jwts create again for a fresh token, and dotnet user-jwts clear --project src/Presentation and delete the Authentication block from appsettings.Development.json to go back to your authority.

HTTPS

The app redirects HTTP to HTTPS whenever it has an HTTPS port, which is why dotnet run sends http://localhost:7320 on to https://localhost:7321. Outside the Development environment it also sends an HSTS header. The container only listens on HTTP, so in production put it behind a proxy or load balancer that ends TLS, and set ASPNETCORE_FORWARDEDHEADERS_ENABLED=true so the app knows the original request was HTTPS.

Browser origins

Browsers send an Origin header, and the MCP spec asks servers to check it so a web page can't reach a local server by DNS rebinding. Requests to /mcp with no Origin, as desktop clients send them, or from localhost on any port go through. Anything else gets a 403. To let a web page on another host call the server, add its origin to Mcp:AllowedOrigins, for example Mcp__AllowedOrigins__0=https://chat.example.com. A page on a different origin from the server also needs a CORS policy, which the template leaves to you. A refused origin is logged as a warning, so it's easy to see which one to add.

Rate limiting

The rate limiter is in the code but off. Turn it on by setting how many requests each client IP address may make in each window, in appsettings.json or as environment variables (RateLimiting__PermitLimit=100, RateLimiting__Window=00:01:00). Past the limit, calls get 429 Too Many Requests with a Retry-After header. Health checks are never limited. Behind a reverse proxy every call looks like it comes from the proxy, so set ASPNETCORE_FORWARDEDHEADERS_ENABLED=true (as in HTTPS above) and the app reads the client's address from the proxy's X-Forwarded-For header, or limit at the proxy instead.

Tests

dotnet test

The tests never touch movie-reviews.db. Each test builds its own in-memory SQLite database from the migrations and the same fixed seed, so no test can change what another one sees and the results are the same every time.