{"record":{"id":"a2faecf6e1dfc910","repo":"langchain-ai/deepagents","slug":"mcpreauthrequirederror-server-name","errorCode":null,"errorMessage":"MCPReauthRequiredError({server_name})","messagePattern":"MCPReauthRequiredError\\((.+?)\\)","errorType":"exception","errorClass":"MCPReauthRequiredError","httpStatus":null,"severity":"error","filePath":"libs/code/deepagents_code/mcp_auth.py","lineNumber":1137,"sourceCode":"        self.server_name = server_name\n        super().__init__(\n            f\"MCP server {server_name!r} needs re-authentication. \"\n            f\"Run `/mcp login {server_name}` in the TUI, or \"\n            f\"`dcode mcp login {server_name}` from the shell.\",\n        )\n\n\ndef _make_reauth_required_handlers(\n    server_name: str,\n) -> tuple[RedirectHandler, CallbackHandler]:\n    \"\"\"Return OAuth handlers that refuse to prompt and raise instead.\n\n    Used in non-interactive server mode so that a missing or expired token\n    surfaces as `MCPReauthRequiredError` rather than hanging on `input()`.\n    \"\"\"\n\n    async def redirect(_auth_url: str) -> None:  # noqa: RUF029\n        raise MCPReauthRequiredError(server_name)\n\n    async def callback() -> tuple[str, str | None]:  # noqa: RUF029\n        raise MCPReauthRequiredError(server_name)\n\n    return redirect, callback\n\n\ndef _make_paste_back_handlers(\n    *,\n    extra_auth_params: dict[str, str] | None = None,\n    ui: OAuthInteraction | None = None,\n) -> tuple[RedirectHandler, CallbackHandler]:\n    \"\"\"Create paste-back redirect and callback handlers for OAuth.\n\n    Args:\n        extra_auth_params: Extra query params to append to the auth URL.\n        ui: Interaction surface for the auth URL display and the\n            pasted-back callback URL prompt.","sourceCodeStart":1119,"sourceCodeEnd":1155,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/code/deepagents_code/mcp_auth.py#L1119-L1155","documentation":"In non-interactive (server) mode, the OAuth redirect handler must not block on input(); instead it raises MCPReauthRequiredError carrying the server name. This signals callers (e.g. the MCP tooling layer) that the user must run an interactive login elsewhere. Raised from the generated `redirect` handler returned by the non-interactive handler factory.","triggerScenarios":"An MCP OAuth flow starts in non-interactive/server mode and the provider redirects the browser to the callback URL with no user available to approve the device/paste-back flow, so `redirect` fires and raises.","commonSituations":"Running dcode as a headless server/agent process where no TTY or browser session exists, an expired token triggering a fresh OAuth redirect during an automated session, or CI runs that need MCP auth but cannot prompt.","solutions":["Run `/mcp login <server>` in the interactive TUI (or `dcode mcp login <server>` in a terminal) once, then retry the server session.","Ensure a valid, non-expired token exists in the token store before starting non-interactive mode.","Handle MCPReauthRequiredError in the server harness and surface it to the user instead of retrying the request."],"exampleFix":"// before\nresult = await mcp_client.call_tool(...)  # raises MCPReauthRequiredError\n// after\ntry:\n    result = await mcp_client.call_tool(...)\nexcept MCPReauthRequiredError as e:\n    print(f\"Run 'dcode mcp login {e.server_name}' interactively first.\")","handlingStrategy":"try-catch","validationCode":"# before starting non-interactive mode, ensure tokens exist and are fresh\nstore = McpTokenStore(server_name, url)\nif not store.path.exists():\n    raise SystemExit(f\"Run 'dcode mcp login {server_name}' before starting server mode.\")","typeGuard":null,"tryCatchPattern":"from deepagents_code.mcp_auth import MCPReauthRequiredError\ntry:\n    result = await run_mcp_session(server_name)\nexcept MCPReauthRequiredError as e:\n    log.warning(\"MCP server %s requires interactive login\", e.server_name)\n    return ExitCode.needs_auth","preventionTips":["Always run an interactive `dcode mcp login <server>` before deploying headless sessions.","Monitor token expiry and re-login proactively in long-lived server deployments.","Map MCPReauthRequiredError to a distinct exit code in automation so CI reports 'needs auth' clearly."],"tags":["mcp","oauth","authentication","non-interactive"],"backgroundTag":"oauth-reauth-required","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}