Skip to content

Commit 2a54c9e

Browse files
saravmajesticclaude
andcommitted
fix(auth): sign-out also removes datamate MCP entries carrying the gateway key
Standalone `datamate add` writes `Authorization: Bearer <gateway key>` into MCP entries in `altimate-code.json`, so new sessions kept connecting as the signed-out user after `auth logout`. - Add `AltimateApi.signOut(projectDir)`: removes MCP entries in the global and project config whose `Authorization` header is exactly the saved key, then deletes `~/.altimate/altimate.json` - CLI `auth logout altimate-backend` and TUI `/logout` both use it and report the removed entries - CLI error names the credential file and says to delete it manually - TUI `/logout` passes `throwOnError` so a failed auth-store removal is reported instead of showing "Signed out" - Docs: Windows path, removed MCP entries, running sessions need a restart - Test MCP entry removal (project + global, unrelated entry kept) Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a345963 commit 2a54c9e

5 files changed

Lines changed: 93 additions & 6 deletions

File tree

‎docs/docs/configure/providers.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Managed LLM access with dynamic routing across Sonnet 4.6, Opus 4.6, GPT-5.4, GP
4343

4444
For pricing, security, and data handling details, see the [Altimate LLM Gateway guide](https://help.altimate.ai/datamates/user-guide/components/llm-gateway/).
4545

46-
To sign out, run `altimate providers logout altimate-backend` (or `/logout` in the TUI). This removes the saved key from `~/.altimate/altimate.json` and the auth store, so workspace requests stop until you sign in again.
46+
To sign out, run `altimate providers logout altimate-backend` (or `/logout` in the TUI). This deletes `~/.altimate/altimate.json` (`%USERPROFILE%\.altimate\altimate.json` on Windows), removes the auth-store entry, and removes datamate MCP servers in the global and current project config that carry the saved key. New sessions stop using the key; restart any session that is already running, or run `/logout` inside it. Datamate MCP servers configured in other projects are not removed.
4747

4848
!!! tip "Automatic model selection"
4949
When Altimate credentials are configured and no model is explicitly chosen, the Altimate LLM Gateway is selected automatically. You can override this by setting `model` in your config or by restricting the `provider` section to specific providers only.

‎packages/opencode/src/altimate/api/client.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from "path"
33
import { rm } from "node:fs/promises"
44
import { Global } from "../../global"
55
import { Filesystem } from "../../util/filesystem"
6+
import { findAllConfigPaths, listMcpInConfig, readMcpEntryFromDisk, removeMcpFromConfig } from "../../mcp/config"
67

78
const DEFAULT_MCP_URL = "https://mcpserver.getaltimate.com/sse"
89
// altimate_change start — default Altimate API URL when user omits it from the TUI credential entry
@@ -147,11 +148,33 @@ export namespace AltimateApi {
147148
)
148149
}
149150

150-
/** Remove the stored gateway credential file (sign out). No-op if absent. */
151+
/** Delete ~/.altimate/altimate.json, including any non-credential fields such as `mcpServerUrl`. No-op if absent. */
151152
export async function clearCredentials(): Promise<void> {
152153
await rm(credentialsPath(), { force: true })
153154
}
154155

156+
/** Sign out of the gateway: remove MCP entries that carry the saved key, then delete the credential file.
157+
* Standalone `datamate add` writes the key into MCP headers (`buildMcpConfig`); matching the exact key
158+
* leaves user-authored entries alone. Only the global config and `projectDir` are searched. */
159+
export async function signOut(projectDir: string): Promise<Array<{ name: string; path: string }>> {
160+
const key = await getCredentials().then(
161+
(creds) => creds.altimateApiKey,
162+
() => undefined,
163+
)
164+
const removed: Array<{ name: string; path: string }> = []
165+
if (key) {
166+
for (const configPath of await findAllConfigPaths(projectDir, Global.Path.config)) {
167+
for (const name of await listMcpInConfig(configPath)) {
168+
const entry = await readMcpEntryFromDisk(name, configPath)
169+
if (entry?.type !== "remote" || entry.headers?.Authorization !== `Bearer ${key}`) continue
170+
if (await removeMcpFromConfig(name, configPath)) removed.push({ name, path: configPath })
171+
}
172+
}
173+
}
174+
await clearCredentials()
175+
return removed
176+
}
177+
155178
const VALID_TENANT_REGEX = /^[a-z_][a-z0-9_-]*$/
156179

157180
/** True if `name` is a well-formed instance/tenant name. */

‎packages/opencode/src/cli/cmd/providers.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,12 @@ export const ProvidersLogoutCommand = effectCmd({
650650
if (provider === "altimate-backend") {
651651
// Browser sign-in persists the gateway key to ~/.altimate/altimate.json, which the provider
652652
// loader and every workspace call read first, so sign-out must remove it too.
653-
yield* cliTry("Failed to remove Altimate credential: ", () => AltimateApi.clearCredentials())
653+
const removed = yield* cliTry(
654+
`Failed to sign out of Altimate (if ${AltimateApi.credentialsPath()} remains, delete it manually): `,
655+
() => AltimateApi.signOut(process.cwd()),
656+
)
657+
for (const entry of removed) yield* Prompt.log.info(`Removed MCP server "${entry.name}" from ${entry.path}`)
658+
if (removed.length > 0) yield* Prompt.log.info("Add the datamate again after signing in to restore it.")
654659
}
655660
// altimate_change end
656661
yield* Effect.orDie(authSvc.remove(provider))

‎packages/opencode/src/plugin/tui/altimate/provider-credentials.tsx‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ function show(api: TuiPluginApi) {
8686
// opencode-side and unreachable from packages/tui.
8787
async function logout(api: TuiPluginApi) {
8888
try {
89-
await AltimateApi.clearCredentials()
89+
const removed = await AltimateApi.signOut(api.state.path.directory)
9090
// A pasted url::instance::key in the auth store keeps the provider loader's fallback path live.
91-
await api.client.auth.remove({ providerID: PROVIDER_ID })
91+
await api.client.auth.remove({ providerID: PROVIDER_ID }, { throwOnError: true })
9292
await api.client.instance.dispose()
93-
api.ui.toast({ variant: "success", message: "Signed out of Altimate LLM Gateway" })
93+
const mcpNote = removed.length > 0 ? ` and removed ${removed.map((entry) => entry.name).join(", ")}` : ""
94+
api.ui.toast({ variant: "success", message: `Signed out of Altimate LLM Gateway${mcpNote}` })
9495
} catch (err) {
9596
api.ui.toast({ variant: "error", message: err instanceof Error ? err.message : "Sign-out failed" })
9697
}

‎packages/opencode/test/cli/providers-logout.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,62 @@ describe("providers logout", () => {
153153
}),
154154
120_000,
155155
)
156+
157+
cliIt.live(
158+
"removes datamate MCP entries that carry the signed-out gateway key",
159+
({ home, opencode }) =>
160+
Effect.gen(function* () {
161+
const altimatePath = path.join(home, ".altimate", "altimate.json")
162+
// The CLI runs with cwd=home, so home is the project directory.
163+
const projectConfigPath = path.join(home, "altimate-code.json")
164+
const globalConfigPath = path.join(home, ".config", "altimate-code", "altimate-code.json")
165+
const datamateEntry = (key: string) => ({
166+
type: "remote",
167+
url: "https://mcp.test",
168+
oauth: false,
169+
headers: { Authorization: `Bearer ${key}`, "x-datamate-id": "1", "x-tenant": "tenant" },
170+
enabled: true,
171+
})
172+
const unrelated = datamateEntry("someone-elses-key")
173+
174+
yield* Effect.promise(() => fs.mkdir(path.dirname(altimatePath), { recursive: true }))
175+
yield* Effect.promise(() => fs.mkdir(path.dirname(globalConfigPath), { recursive: true }))
176+
yield* Effect.promise(() =>
177+
fs.writeFile(
178+
altimatePath,
179+
JSON.stringify({
180+
altimateUrl: "https://api.test",
181+
altimateInstanceName: "tenant",
182+
altimateApiKey: "gateway-test-key",
183+
}),
184+
{ mode: 0o600 },
185+
),
186+
)
187+
yield* Effect.promise(() =>
188+
fs.writeFile(
189+
projectConfigPath,
190+
JSON.stringify({ mcp: { "datamate-sales": datamateEntry("gateway-test-key"), unrelated } }),
191+
),
192+
)
193+
yield* Effect.promise(() =>
194+
fs.writeFile(
195+
globalConfigPath,
196+
JSON.stringify({ mcp: { "datamate-ops": datamateEntry("gateway-test-key") } }),
197+
),
198+
)
199+
200+
const logout = yield* opencode.spawn(["providers", "logout", "altimate-backend"], {
201+
env: { OPENCODE_AUTH_CONTENT: "" },
202+
})
203+
opencode.expectExit(logout, 0, "providers logout altimate-backend with datamate MCP entries")
204+
expect(logout.stdout).toContain('Removed MCP server "datamate-sales"')
205+
expect(logout.stdout).toContain('Removed MCP server "datamate-ops"')
206+
expect(logout.stdout).toContain("Logout successful")
207+
expect(JSON.parse(yield* Effect.promise(() => fs.readFile(projectConfigPath, "utf8")))).toEqual({
208+
mcp: { unrelated },
209+
})
210+
expect(JSON.parse(yield* Effect.promise(() => fs.readFile(globalConfigPath, "utf8")))).toEqual({ mcp: {} })
211+
}),
212+
120_000,
213+
)
156214
})

0 commit comments

Comments
 (0)