langchain-ai/deepagents · error · ValueError

ServerSelection.server_name must not be empty

Error message

ServerSelection.server_name must not be empty

What it means

ServerSelection's __post_init__ requires a non-empty server_name because every MCP login/selection operation needs to identify which named server to act on. An empty string name cannot match any configured server, so construction fails immediately with a ValueError.

Source

Thrown at libs/code/deepagents_code/mcp_login_service.py:188

    server_name: str
    """Selected MCP server name (matches an `mcpServers` key)."""

    server_config: McpServerSpec
    """Validated server config payload for `mcp_auth.login`."""

    search_label: str = ""
    """Where the config came from — used in not-found errors."""

    def __post_init__(self) -> None:
        """Enforce the non-empty `server_name` invariant.

        Raises:
            ValueError: If `server_name` is empty.
        """
        if not self.server_name:
            msg = "ServerSelection.server_name must not be empty"
            raise ValueError(msg)


def resolve_mcp_config(
    config_path: str | None,
    *,
    trust_project_mcp: bool | None = None,
) -> ConfigResolution | ConfigResolutionError:
    """Resolve an MCP config dict for login without printing anything.

    Args:
        config_path: Explicit `--mcp-config` path, or `None` for auto-discovery.
        trust_project_mcp: Whether project configs have whole-config trust for
            the current session. Persisted approvals and denials still apply.

    Returns:
        A `ConfigResolution` on success, or a `ConfigResolutionError`
            describing why no usable config could be assembled.
    """

View on GitHub (pinned to a1af029e6e)

Solutions

  1. Provide the server name explicitly, matching a name defined under `mcp.servers` in your config.
  2. Guard the CLI/script input: fail early with a usage message if the argument is empty.
  3. Run `dcode mcp login list` to see valid server names and pick one.

Example fix

// before
ServerSelection(server_name=os.environ.get("MCP_SERVER", ""), ...)
// after
name = os.environ.get("MCP_SERVER")
if not name:
    raise SystemExit("usage: MCP_SERVER=<name> dcode mcp login")
ServerSelection(server_name=name, ...)
Defensive patterns

Strategy: validation

Validate before calling

server_name = (args.server or os.environ.get("MCP_SERVER") or "").strip()
if not server_name:
    raise SystemExit("server name required: dcode mcp login <server>")

Type guard

def non_empty_str(value: object) -> TypeGuard[str]:
    return isinstance(value, str) and bool(value.strip())

Try / catch

try:
    selection = ServerSelection(server_name=name)
except ValueError as exc:
    print(f"invalid selection: {exc}"); raise SystemExit(2)

Prevention

When it happens

Trigger: Constructing `ServerSelection(server_name="", ...)` directly, or passing an empty server argument into `dcode mcp login` / resolve_mcp_config-driven flows that build a ServerSelection.

Common situations: Shell variables expanding to empty (`dcode mcp login "$SERVER"` with unset SERVER); scripts splitting CLI args incorrectly; UI code defaulting the name to empty string before user picks a server.

Related errors


AI-assisted analysis of langchain-ai/deepagents@a1af029e6e (2026-08-29). Data as JSON: /api/errors/6e4bfdcea5450f49. Report an issue: GitHub.