Skip to content

feat(cli): support env vars in mcp dev (#339)#2506

Open
GitAashishG wants to merge 2 commits intomodelcontextprotocol:mainfrom
GitAashishG:docs-339-mcp-dev-env-vars
Open

feat(cli): support env vars in mcp dev (#339)#2506
GitAashishG wants to merge 2 commits intomodelcontextprotocol:mainfrom
GitAashishG:docs-339-mcp-dev-env-vars

Conversation

@GitAashishG
Copy link
Copy Markdown

Summary

Closes #339.

mcp install already accepts --env-var/-v and --env-file/-f, but mcp dev did not — so there was no way to pass environment variables to a server when iterating with the inspector. This PR adds the same two flags to mcp dev and routes the resolved values into the inspector subprocess environment.

Changes

  • Add --env-var/-v and --env-file/-f options to mcp dev (mirrors mcp install).
  • Extract a shared _resolve_env(env_file, env_vars) helper; inline -v values override .env values.
  • Refactor mcp install to use the helper (no behavior change, removes duplication).
  • Document the new flags under "Development Mode" in README.v2.md.
  • Add unit tests for _resolve_env covering: no inputs, inline vars, values containing =, dotenv loading, and inline-overrides-dotenv.

Validation

  • uv run --frozen ruff format / ruff check — clean
  • uv run --frozen pyright src/mcp/cli/cli.py tests/cli/test_utils.py — 0 errors
  • uv run --frozen pytest tests/cli/test_utils.py — 14 passed

Add `--env-var/-v` and `--env-file/-f` options to the `mcp dev`
command so the inspector subprocess inherits caller-provided
environment variables — mirroring what `mcp install` already accepts.
Inline `-v` flags override values loaded from `--env-file`.

Extract the shared resolution logic into `_resolve_env` and refactor
`mcp install` to use it (no behavior change). Document the new flags
in README.v2.md and add unit tests for `_resolve_env`.

Closes modelcontextprotocol#339
Add tests for the dotenv-missing, dotenv-raises, and malformed-`-v`
branches so coverage stays at 100% on `src/mcp/cli/cli.py`. Also drop
the now-obsolete `# pragma: no cover` from `_parse_env_var` (it's
exercised through `_resolve_env`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[docs] How to run FastMCP with args or env vars?

1 participant