# Add a Config File to a Tokio Server

Yes. An agent moves your Tokio server's bind address, port and connection limit into a TOML file, keeps the flags as overrides, tests the precedence and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-config-file/tokio/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads where every setting lives today before it writes a file to hold them. In a server laid out like tokio-rs/mini-redis, `src/bin/server.rs` parses one flag with clap's derive API, `--port` as an `Option<u16>`, falls back to `DEFAULT_PORT` (`6379`, a `pub const` in `src/lib.rs`), and binds the listener to `127.0.0.1:{port}` with the host written into the format string, so the server cannot listen on another interface without a code change. `src/server.rs` sets `MAX_CONNECTIONS` to `250` as a `const` that sizes a `Semaphore`, under a comment that says a real application will want to make this value configurable. Logging reads `RUST_LOG` through `tracing_subscriber::fmt::try_init()`, and the `otel` feature swaps in an OpenTelemetry pipeline. The client binary in `src/bin/cli.rs` already takes `--hostname` with a default of `127.0.0.1` and `--port`, so the names for the server's settings are already in the repository.

It then designs the file around those three settings and nothing else: `bind` (a host string), `port` and `max_connections`, in a `Config` struct that derives `serde::Deserialize` with `#[serde(default)]` on each field so a partial file is valid. `serde` and `toml` join `[dependencies]` in `Cargo.toml`; nothing else changes there, and `Cargo.lock` picks up the two crates and theirs. A new `--config <path>` flag on the server's `Cli` names the file; when it is absent, no file is read and the defaults match what the binary does today. Precedence is fixed and tested: a flag beats the file, and the file beats the compiled-in default, so `--port` keeps working exactly as before for anyone who scripts it.

The connection limit is the part that touches the library. `MAX_CONNECTIONS` stops being a `const` and becomes a field the `Listener` takes when `server::run` builds the `Semaphore`, which means `run` gains a parameter. The agent changes every caller to pass it, the binary and the integration tests under `tests/` that start a server on an ephemeral port, and keeps the default at `250` so the tests' behaviour does not move. It leaves `DEFAULT_PORT` where it is, since the client binary and the examples import it.

In its sandbox the agent runs `cargo build`, `cargo build --features otel`, `cargo test` and `cargo fmt --all --check`, the four commands `.github/workflows/ci.yml` runs, and adds tests of its own: a unit test that parses a TOML string with one field set and checks the other two are defaults, one that parses an unknown key and checks the error names it, and one that builds the `Cli` from argument vectors to prove the flag-over-file order. A sample `mini-redis.toml` with every key commented goes beside `README.md`, which gains a short section under Running showing `cargo run --bin mini-redis-server -- --config mini-redis.toml`.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add a TOML config file to this Tokio server. Read src/bin/server.rs, src/server.rs, src/lib.rs, src/bin/cli.rs and tests/server.rs first. Add a Config struct with bind, port and max_connections, each with a default equal to today's behaviour (127.0.0.1, DEFAULT_PORT, 250), loaded from a file named by a new --config flag on the server binary. A flag overrides the file and the file overrides the default; an unknown key is an error that names the key. Make the Semaphore size in server::run come from the config and update every caller, including the integration tests. Add serde and toml as dependencies and nothing else. Write unit tests for defaults, unknown keys and precedence, run cargo build, cargo build --features otel, cargo test and cargo fmt --all --check, and quote the results. Add a commented sample file and a short README section. Open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that changes `Cargo.toml` and `Cargo.lock`, `src/bin/server.rs`, `src/server.rs`, the integration tests that call `server::run`, and `README.md`, and adds a config module with its tests and a sample `mini-redis.toml`. The description shows the three settings, their defaults, the precedence order, and quotes the four CI commands and the new unit tests passing.

The run's record keeps the `cargo test` output with the new test names visible, and a reviewer can see that the default build still binds `127.0.0.1:6379` with a limit of 250 by reading the defaults test rather than starting the server.

Before merging, a reviewer decides two things the agent left as defaults: whether `bind` should default to all interfaces for container use (the agent keeps the loopback address the code had), and whether a missing file named by `--config` should be an error (it is, in the pull request) or a warning.

## Run it on every Linear issue labelled config

A team that keeps a backlog of settings to externalise adds an automation to the Project with this prompt and a `linear` trigger on `linear.issue.labeled` with that label in its `labels`. Each labelled issue, say one asking for a TLS section or a per-command timeout, starts one run on its own branch with the issue as its context; the trigger adds the issue after the prompt and fills in no placeholder, and the agent's own comment on the issue starts nothing, because a change made by an app integration never fires the trigger.

The automation's `max_runs_per_hour` caps a burst of labels: a fire past it is created `queued` and starts when the hour has room. From your own server the same run starts with this request and the issue URL in `resources`, with a `request_id` that is a UUID v5 of the issue id, so a retried start returns the run already begun rather than a second branch.

## Why run it on Rush?

A config file is a small feature with a wide blast radius: it changes a public function's signature, every test that calls it, two binaries and the README, and the precedence rules are the kind of thing that is right in the author's head and wrong in one branch. In a Rush sandbox the agent runs the full CI set and its own precedence tests before anyone reads the diff, and the record shows each command's output.

The pull request is the whole story, with the sample file, the defaults and the test names in one place. Across every Tokio service a team runs, the same prompt produces one pull request per repository, each shaped by that repository's own flags and constants.

## Frequently asked questions

### Does --port still work without a file?

Yes. With no `--config`, no file is read and every setting is its compiled-in default, so `--port 7000` behaves as it does today. With a file, the flag still wins over the file's `port`.

### Why TOML and not environment variables?

Because the ask is a file, and TOML is what Cargo already uses in the repository. Environment variables are easy to add on top as a second override layer; the pull request leaves that out to keep the precedence rule to two steps, flag over file over default.

### Will the connection limit change for existing deployments?

No. The default stays at 250, the value the `const` had, and the integration tests keep starting servers with it. Only a file or flag that names `max_connections` changes the `Semaphore` size.

### What happens on a typo in the file?

The server refuses to start and the error names the unknown key, because the config struct is deserialized with unknown fields denied. A silently ignored `max_conections` would be worse than a crash at boot, and there is a test for it.

## More Tokio jobs

- [Add Code Coverage to CI for a Tokio Service](https://getrush.ai/run/add-coverage-to-ci/tokio/)
- [Add Criterion Benchmarks to a Tokio Server](https://getrush.ai/run/add-criterion-benchmarks/tokio/)
- [Add Dependabot to a Tokio Project](https://getrush.ai/run/add-dependabot/tokio/)

## The same job on other stacks

- [Add a Config File to a Click CLI](https://getrush.ai/run/add-config-file/click/)
- [Add a Config File to a Go CLI](https://getrush.ai/run/add-config-file/go-cli/)
- [Add a Config File to a Rust CLI](https://getrush.ai/run/add-config-file/rust-cli/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
