Skip to content

fix(corezoid): allow login/logout to switch environments mid-session#9

Open
Starushenko wants to merge 2 commits into
corezoid:mainfrom
Starushenko:feat/login-env-switch-mid-session
Open

fix(corezoid): allow login/logout to switch environments mid-session#9
Starushenko wants to merge 2 commits into
corezoid:mainfrom
Starushenko:feat/login-env-switch-mid-session

Conversation

@Starushenko
Copy link
Copy Markdown

The login handler refused to apply account_url, workspace_id, and stage_id arguments whenever the corresponding in-memory variable was already populated (v != "" && accountURL == "" etc.). Combined with logout only resetting apiToken, this meant that once the MCP process started with one set of credentials, the user could not actually switch to a different environment without restarting Claude Code: the args were silently ignored, .env changes were ignored, and every tool call kept hitting the host captured at startup.

Changes:

  • login: after findAndLoadDotEnv(), unconditionally refresh accountURL, workspaceID, stageID, apiURL from env so a swapped .env actually takes effect. apiToken keeps its conditional refresh so a freshly-issued OAuth token is not clobbered by a stale value.
  • login: apply account_url, workspace_id, stage_id arguments unconditionally — these express explicit caller intent and should always win over any cached state. When account_url changes, clear the derived COREZOID_API_URL so it is re-fetched for the new host.
  • login: derive COREZOID_API_URL whenever a token is present but apiURL is empty, not only inside the OAuth-success branch. This handles the common case where ACCESS_TOKEN is supplied directly in .env (browser OAuth is broken on private on-prem instances, see OAuth PKCE login fails on private on-prem instances #7) and the case where account_url just changed.
  • logout: clear accountURL, workspaceID, stageID, apiURL in addition to apiToken so a follow-up login starts from a clean slate.

🤖 Generated with Claude Code

The login handler refused to apply `account_url`, `workspace_id`, and
`stage_id` arguments whenever the corresponding in-memory variable was
already populated (`v != "" && accountURL == ""` etc.). Combined with
logout only resetting `apiToken`, this meant that once the MCP process
started with one set of credentials, the user could not actually switch
to a different environment without restarting Claude Code: the args
were silently ignored, `.env` changes were ignored, and every tool call
kept hitting the host captured at startup.

Changes:
- login: after `findAndLoadDotEnv()`, unconditionally refresh
  `accountURL`, `workspaceID`, `stageID`, `apiURL` from env so a
  swapped `.env` actually takes effect. `apiToken` keeps its
  conditional refresh so a freshly-issued OAuth token is not clobbered
  by a stale value.
- login: apply `account_url`, `workspace_id`, `stage_id` arguments
  unconditionally — these express explicit caller intent and should
  always win over any cached state. When `account_url` changes, clear
  the derived `COREZOID_API_URL` so it is re-fetched for the new host.
- login: derive `COREZOID_API_URL` whenever a token is present but
  `apiURL` is empty, not only inside the OAuth-success branch. This
  handles the common case where `ACCESS_TOKEN` is supplied directly
  in `.env` (browser OAuth is broken on private on-prem instances,
  see corezoid#7) and the case where `account_url`
  just changed.
- logout: clear `accountURL`, `workspaceID`, `stageID`, `apiURL` in
  addition to `apiToken` so a follow-up `login` starts from a clean
  slate.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
The MCP tool `pull-folder` is advertised as exporting "a Corezoid
folder/stage" and takes a generic `folder_id`, but internally
`downloadStageRecursively` hardcoded `obj_type=stage` for every ID.

On the server, `stage` (root container of a project) and `folder`
(any sub-container) are distinct object types and the wrong one is
rejected:

  - sub-folder ID with `obj_type=stage`  -> proc:error,
    "Object stage with id N does not exist"
  - stage ID with `obj_type=folder`      -> proc:error,
    "This object does not exist or is in the trash"

`PullZip` only inspects `request_proc` and `ops[0].download_url`, so
this inner error is swallowed and re-emitted as the generic
"failed to export process: no download_url in response". The result
is that `pull-folder` works against a stage ID but appears
mysteriously broken for every regular sub-folder ID a user might
hand it from the Corezoid UI. Same root cause produced the
plugin-side timeouts and `unexpected end of JSON input` errors
reported in the env-switch debugging session that preceded this
commit.

Fix: in `downloadStageRecursively` try `obj_type=folder` first and
fall back to `obj_type=stage` on any error. One redundant request
only in the rare case the caller passed a real stage ID; no
behaviour change on the success path; no public API change; the
auto-pull triggered from the `login` flow continues to work because
the retry covers stage IDs.

Verified by direct curl against an on-prem AZ dev instance
(`corezoid-tst.unibank.lan`) -- folder ID 29245 returns a
`download_url` with `obj_type=folder` and the previous "no
download_url" error with `obj_type=stage`; stage ID 21194 returns
`download_url` with `obj_type=stage` and the "does not exist or is
in the trash" error with `obj_type=folder`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
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.

2 participants