PrefectHQ/fastmcp · error · StateFileError

The Horizon host changed during login

Error message

The Horizon host changed during login

What it means

During `fastmcp deploy` login, login() compares the api_origin of the configuration loaded from an existing session snapshot against the host the user requested. If they differ, StateFileError('The Horizon host changed during login') is raised to prevent overwriting credentials tied to a different host.

Source

Thrown at fastmcp_slim/fastmcp/cli/deploy/command.py:225

            try:
                requested_configuration = configuration_store.set_api_origin(
                    host,
                    credentials=credentials,
                )
            except ValueError:
                _fail(
                    "login",
                    "invalid_host",
                    "The Horizon host must be an HTTP origin.",
                    json_output=json_output,
                )

        configuration, credential = _load_session_snapshot(credentials)
        if (
            requested_configuration is not None
            and configuration.api_origin != requested_configuration.api_origin
        ):
            raise StateFileError("The Horizon host changed during login")

        async def device_authorization():
            approval_status: Status | None = None

            def show_challenge(challenge: DeviceAuthorization) -> None:
                nonlocal approval_status
                emit_device_challenge(challenge, json_output=json_output)
                approval_status = start_device_approval_status(json_output=json_output)

            try:
                async with HorizonClient(configuration.api_origin) as client:
                    return await authorize_device(
                        client,
                        metadata=_device_metadata(),
                        on_challenge=show_challenge,
                        open_browser=not json_output and _can_open_browser(),
                        browser_opener=webbrowser.open,
                    )

View on GitHub (pinned to 1f02114297)

Solutions

  1. Point login back at the original host, or explicitly delete/move the existing credentials file first
  2. Re-run login with the correct --host matching your stored session
  3. Back up the credentials file, remove it, and log in fresh to the new host

Example fix

// before
login(host="staging.horizon.example")  # stored creds are for prod -> StateFileError
// after
rm ~/.fastmcp/horizon-credentials.json
login(host="staging.horizon.example")
Defensive patterns

Strategy: validation

Validate before calling

from fastmcp.cli.deploy.command import _load_session_snapshot
config, _ = _load_session_snapshot(credentials)
if config is not None and config.api_origin != requested_host:
    # delete or back up the old credentials before logging in to a new host
    credentials.unlink()

Try / catch

from fastmcp.cli.deploy.command import StateFileError
try:
    await login(host=host)
except StateFileError as e:
    if "host changed" in str(e):
        credentials.unlink(missing_ok=True)
        await login(host=host)

Prevention

When it happens

Trigger: Running login while the stored credentials file points at a different Horizon api_origin than the requested one (e.g. a --host flag or env var changed between sessions).

Common situations: Switching between staging and production Horizon instances; a teammate-shared credentials file; a proxy URL change; setting/unsetting a HORIZON_HOST-style environment variable.

Related errors


AI-assisted analysis of PrefectHQ/fastmcp@1f02114297 (2026-08-29). Data as JSON: /api/errors/5039d1e8398355e3. Report an issue: GitHub.