Skip to content

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

Closed

Description

@Nitjsefnie

Bug Description

devops_mcp/server.py sets the FastMCP bind address with os.environ.setdefault, and the module calls load_dotenv() at import. So a FASTMCP_HOST or FASTMCP_PORT value in .env is already in the environment by the time run_mcp runs, setdefault leaves it in place, and the --host / --port flags passed on the command line are silently discarded.

The module's own docstring advertises devops-agent mcp --transport sse --port 8090 — the flag it ignores.

This matters beyond convenience: the failure runs in the unsafe direction. An operator who once put FASTMCP_HOST=0.0.0.0 in .env and later runs --host 127.0.0.1 to restrict the bind stays listening on all interfaces, with no warning and a zero exit code.

Steps to Reproduce

  1. Put FASTMCP_HOST=203.0.113.9 and FASTMCP_PORT=8888 in .env
  2. Run devops-agent mcp --transport sse --host 0.0.0.0 --port 9999
  3. Observe the values FastMCP binds with

Expected Behavior

An explicitly passed --host / --port takes precedence over .env, since the flag is the more specific intent. .env should supply the default when the flag is absent.

Actual Behavior

The .env values win and the flags are ignored:

after importing devops_mcp.server (its load_dotenv() at line 26 ran):
   os.environ['FASTMCP_HOST'] = '203.0.113.9'

running: devops-agent mcp --transport sse --host 0.0.0.0 --port 9999

mcp.run(transport='sse') reached. FastMCP will bind:
   FASTMCP_HOST = '203.0.113.9'
   FASTMCP_PORT = '8888'
(CLI exited with code 0)

Reproduced by driving the real devops_agent.cli.main() with real argv; the only stub was create_mcp_server, so the os.environ.setdefault calls executed for real. Neither variable was set in the ambient shell — confirmed before the run.

Environment

  • OS: Debian 13
  • Python Version: 3.13.14
  • Agent Version/Commit: ace3dc21ba2ee58ef748b39a31f7ec6049bd6d88 (main)
  • Deployment Method: local
  • Platform: n/a — reproduces from the CLI alone

Configuration

FASTMCP_HOST=203.0.113.9
FASTMCP_PORT=8888

Additional Context

Relevant lines on main:

  • devops_mcp/server.py:16 imports load_dotenv, :26 calls it at module scope
  • devops_mcp/server.py:80-81 — os.environ.setdefault("FASTMCP_HOST", host) / setdefault("FASTMCP_PORT", str(port))
  • devops_mcp/server.py:82 — mcp.run(transport=transport) immediately after, so nothing applies the flags later

Unrelated to #34, which is about the mcp[cli] pin resolving to 2.0.0 and breaking an import — mentioning it only because both touch the MCP server.

Found while documenting environment variables for #15.

Possible Solution

Unverified: assigning the value when the flag was explicitly passed, and falling back to setdefault (or to the env var) only when it was not, would give the flag precedence while keeping .env as the default source. That is a behaviour change, so it seemed better to file it than to fold it into a documentation PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions