Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix(cli): whoami respects the org selected via `orgs switch` (#3832)
xHai8467efdfb95c
diff --git a/.changeset/whoami-respect-switched-org.md b/.changeset/whoami-respect-switched-org.mdnew file mode 100644index 000000000..6eea99251--- /dev/null+++ b/.changeset/whoami-respect-switched-org.md@@ -0,0 +1,5 @@+---+'@composio/cli': patch+---++Fix `composio whoami` reporting the wrong organization after `composio orgs switch`. The command resolved session info without the `x-org-id` header, so the backend fell back to the API key's home org and ignored the switch. It now forwards the selected global org, matching how consumer commands resolve org context.diff --git a/ts/packages/cli/src/commands/whoami.cmd.ts b/ts/packages/cli/src/commands/whoami.cmd.tsindex 3471c6b97..41a50cabb 100644--- a/ts/packages/cli/src/commands/whoami.cmd.ts+++ b/ts/packages/cli/src/commands/whoami.cmd.ts@@ -31,6 +31,8 @@ export const whoamiCmd = Command.make('whoami', {}).pipe(const sessionInfo = yield* getSessionInfoByUserApiKey({baseURL: ctx.data.baseURL,userApiKey: apiKey,+ // Reflect the org selected via `composio orgs switch`, not the key's home org.+ orgId: Option.getOrUndefined(ctx.data.orgId),}).pipe(Effect.option);const email = Option.map(sessionInfo, info => info.org_member.email).pipe(Option.getOrUndefineddiff --git a/ts/packages/cli/src/services/composio-clients.ts b/ts/packages/cli/src/services/composio-clients.tsindex 6e0fdd723..0723fdd8b 100644--- a/ts/packages/cli/src/services/composio-clients.ts+++ b/ts/packages/cli/src/services/composio-clients.ts@@ -1000,15 +1000,21 @@ export const getSessionInfo = (params: {});/**- * Calls GET /api/v3/auth/session/info using only the x-user-api-key header.- * Unlike getSessionInfo which requires org/project IDs, this variant resolves- * session metadata from the UAK alone — useful during login before org/project- * context is known.+ * Calls GET /api/v3/auth/session/info using the x-user-api-key header.+ * Unlike getSessionInfo which requires both org AND project IDs, this variant+ * resolves session metadata from the UAK alone — useful during login before+ * org/project context is known.+ *+ * When `orgId` is provided, it is forwarded as `x-org-id` so the backend+ * resolves the session against the caller's currently-selected global org+ * (set via `composio orgs switch`). Without it, the backend falls back to the+ * API key's home org, which makes the response ignore any org switch.* Uses plain fetch since this endpoint is not available in @composio/client.*/export const getSessionInfoByUserApiKey = (params: {baseURL: string;userApiKey: string;+ orgId?: string;}): Effect.Effect<SessionInfoResponse, HttpServerError | HttpDecodingError> =>Effect.gen(function* () {const response = yield* Effect.tryPromise({@@ -1018,6 +1024,7 @@ export const getSessionInfoByUserApiKey = (params: {redirect: 'error',headers: {'x-user-api-key': params.userApiKey,+ ...(params.orgId ? { 'x-org-id': params.orgId } : {}),'User-Agent': '@composio/cli',Accept: '*/*','Content-Type': 'application/json',diff --git a/ts/packages/cli/test/src/services/composio-clients.test.ts b/ts/packages/cli/test/src/services/composio-clients.test.tsnew file mode 100644index 000000000..7290b3102--- /dev/null+++ b/ts/packages/cli/test/src/services/composio-clients.test.ts@@ -0,0 +1,50 @@+import { afterEach, describe, expect, it, vi } from 'vitest';+import { Effect } from 'effect';+import { getSessionInfoByUserApiKey } from 'src/services/composio-clients';++const emptyOkResponse = () =>+ new Response(JSON.stringify({}), {+ status: 200,+ headers: { 'Content-Type': 'application/json' },+ });++describe('getSessionInfoByUserApiKey org context', () => {+ afterEach(() => {+ vi.restoreAllMocks();+ });++ // The response body does not satisfy SessionInfoResponse, so decoding fails —+ // but the fetch call (whose headers we assert) has already happened by then.+ const runIgnoringDecode = (params: { baseURL: string; userApiKey: string; orgId?: string }) =>+ Effect.runPromise(getSessionInfoByUserApiKey(params).pipe(Effect.ignore));++ it('[Given] orgId [Then] forwards it as x-org-id so the backend honors the switched org', async () => {+ const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(emptyOkResponse());++ await runIgnoringDecode({+ baseURL: 'https://backend.composio.dev',+ userApiKey: 'uak_test',+ orgId: 'ok_selected_org',+ });++ const [url, init] = fetchSpy.mock.calls[0]!;+ expect(String(url)).toContain('/api/v3/auth/session/info');+ const headers = new Headers((init as RequestInit).headers);+ expect(headers.get('x-user-api-key')).toBe('uak_test');+ expect(headers.get('x-org-id')).toBe('ok_selected_org');+ });++ it('[Given] no orgId [Then] omits x-org-id (login-time behavior is unchanged)', async () => {+ const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(emptyOkResponse());++ await runIgnoringDecode({+ baseURL: 'https://backend.composio.dev',+ userApiKey: 'uak_test',+ });++ const [, init] = fetchSpy.mock.calls[0]!;+ const headers = new Headers((init as RequestInit).headers);+ expect(headers.get('x-user-api-key')).toBe('uak_test');+ expect(headers.has('x-org-id')).toBe(false);+ });+});