Skip to content

fix: explicit mcp --host/--port flags take precedence over .env - #46

Merged
OkiriGabriel merged 7 commits into
open-devops-agent:mainfrom
Nitjsefnie-OSC:fix/fastmcp-env-precedence-40
Aug 17, 2026
Merged

OkiriGabriel merged 7 commits into
open-devops-agent:mainfrom
Nitjsefnie-OSC:fix/fastmcp-env-precedence-40

Conversation

@Nitjsefnie

Copy link
Copy Markdown
Contributor

Summary
FASTMCP_HOST/FASTMCP_PORT in .env silently overrode the mcp --host/--port flags. Explicit flags now win; the env file supplies the value only when the flag is absent; built-in defaults remain the last resort.

Related Issues and Pull Requests
Fixes #40

Changes

  • devops_agent/cli.py: the mcp subcommand's --host/--port argparse defaults changed to None (mirroring serve), so an explicitly passed flag is detectable; help text notes the env fallback
  • devops_mcp/server.py: run_mcp(host=None, port=None) assigns explicitly passed values and setdefaults the rest, so the rule holds for direct callers too; stdio path untouched

Testing
Precedence proven end-to-end in all three directions with a real .env and real argv (flag beats .env; .env beats default; default otherwise), adversarially re-run independently. Full suite: only the pre-existing test_mcp.py pin failure (fixed upstream by #38 since). Known, documented non-blocking residual: an explicit value from one in-process run_mcp call persists for a second no-flag call in the same process — unreachable via the CLI (one blocking call per process); noted in the issue's thread for the embedding case.

Footer
Generated by Kimi K3 (brief, implementation, review, verification)

Nitjsefnie and others added 7 commits August 5, 2026 18:39
run_mcp set the FastMCP bind address with os.environ.setdefault, so a
FASTMCP_HOST/FASTMCP_PORT value loaded from .env at import silently
discarded explicitly passed --host/--port flags. An operator restricting
the bind with --host 127.0.0.1 could stay listening on 0.0.0.0 with no
warning.

Precedence is now: explicit flag > environment (.env) > built-in default.
The mcp CLI flags default to None so an explicit pass is detectable, and
run_mcp assigns the env vars when a value is given, falling back to
setdefault with the defaults 127.0.0.1/8090 only when it is not.

Fixes open-devops-agent#40

Co-Authored-By: Kimi K3 <[email protected]>
Brings in upstream's .env.example entries for PAGERDUTY_URGENCY_*,
fixing tests/test_env_example_coverage.py.

Co-Authored-By: Kimi K3 <[email protected]>
Construct the real FastMCP server and capture self.settings.host/port via a
monkeypatched FastMCP.run instead of stubbing create_mcp_server. Both tests
fail pre-fix: FastMCP 1.x freezes settings at construction from explicit
constructor params, so the env assignments in run_mcp never reach the
settings and the server always binds 127.0.0.1:8000.

Co-Authored-By: Kimi K3 <[email protected]>
FastMCP 1.x freezes settings at construction from explicit constructor
params, so the FASTMCP_HOST/FASTMCP_PORT assignments in run_mcp happened
too late to matter and the server always bound 127.0.0.1:8000.

Resolve precedence before create_mcp_server — explicit args over
FASTMCP_HOST/FASTMCP_PORT from the environment (e.g. loaded from .env)
over application defaults 127.0.0.1/8090 — keeping the existing
environment assignments, and pass the resolved host and integer port
explicitly to FastMCP(..., host=..., port=...).

Co-Authored-By: Kimi K3 <[email protected]>
stdio never binds TCP, so FASTMCP_PORT is irrelevant on that path; the
current branch resolves int(FASTMCP_PORT) anyway and raises ValueError.
Regression test fails until the stdio branch stops parsing the env.

Co-Authored-By: Kimi K3 <[email protected]>
stdio does not bind TCP, so the bind address is irrelevant there; the
branch now constructs the server with the explicit args or fixed defaults
without reading, validating, or mutating FASTMCP_HOST/FASTMCP_PORT.
FASTMCP_PORT=not-an-integer no longer raises before run(transport=stdio).
HTTP (sse/streamable-http) resolution is unchanged.

Co-Authored-By: Kimi K3 <[email protected]>
@OkiriGabriel

Copy link
Copy Markdown
Collaborator

Reviewed — CI is green across all checks. Explicit mcp --host/--port taking precedence over .env is correct. Merging to main.

@OkiriGabriel
OkiriGabriel merged commit 2e6bcf0 into open-devops-agent:main Aug 17, 2026
10 checks passed
@Nitjsefnie
Nitjsefnie deleted the fix/fastmcp-env-precedence-40 branch August 19, 2026 14:50
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.

[BUG] FASTMCP_HOST/PORT in .env silently override the mcp --host/--port flags

2 participants