Skip to content

fix(mcp): validate org fetch before committing session - #95872

Draft
aashish00021 wants to merge 1 commit into
PostHog:masterfrom
aashish00021:fix/mcp-switch-organization-validate-before-commit
Draft

fix(mcp): validate org fetch before committing session#95872
aashish00021 wants to merge 1 commit into
PostHog:masterfrom
aashish00021:fix/mcp-switch-organization-validate-before-commit

Conversation

@aashish00021

Copy link
Copy Markdown

Problem

An agent calling switch-organization with an organization id it cannot access is told the switch succeeded. The handler wrote orgId to the session cache and returned a success message before (and independent of) fetching the organization, so a failed fetch was silently ignored. Every later org-scoped call then failed with an opaque 403/404 that never mentioned the switch, and the stale orgId persisted in the token-keyed cache for its full 7-day TTL.

switch-project had the identical shape and was already fixed in #71976 — the fix was never mirrored onto switch-organization.

Closes #78629

Changes

  • switch-organization now fetches the organization first; only on success does it commit orgId and the cached org to the session.
  • On a failed fetch, it throws a descriptive error (naming organizations-get as the recovery path) instead of reporting success. The original API error is preserved as cause, matching switch-project's pattern for keeping recoverable not-found/no-access failures out of exception tracking.
  • No change to the success-path response shape or to switch-project.

How did you test this code?

  • Added services/mcp/tests/unit/switch-organization.test.ts, mirroring the existing switch-project.test.ts pattern: one case asserting a failed fetch does not commit orgId and preserves the error cause, one case asserting a successful fetch commits orgId and the cached org. Both pass (npx vitest run tests/unit/switch-organization.test.ts tests/unit/switch-project.test.ts — 6/6 passed, including the 4 pre-existing switch-project cases, confirming no regression there).
  • npx tsgo --noEmit in services/mcp: no errors introduced by this change (confirmed identical pre-existing error count with and without the diff, via git stash/stash pop; all pre-existing errors are unrelated @posthog/quill UI-app build artifacts, not touching tools/organizations or tools/projects).
  • oxlint --quiet and oxfmt --check on both changed files: clean.
  • Did not manually exercise this against a live PostHog org/session — no live environment set up for this change; relying on the unit tests above plus the direct mirror of the already-shipped switch-project fix.

Automatic notifications

  • Publish to changelog?

🤖 Agent context

Found via an isolated multi-repo scan for well-defined, unclaimed backend bugs. Root-caused by diffing organizations/setActive.ts against the already-fixed projects/setActive.ts, which shares the exact same fetch-then-cache shape and already documents the failure mode in its own comments. The fix, tests, lint/format, and typecheck were all done and verified in this session before opening this PR.

Autonomy: Human-driven (agent-assisted)

switch-organization wrote orgId to the session cache and returned a
success string before/independent of fetching the organization, so
an agent switching to an id it cannot access was told the switch
worked. Subsequent org-nested reads then failed with opaque
403/404s that didn't mention the switch, and the stale orgId
persisted in the token-keyed cache (7-day TTL).

switch-project had the identical shape and was already fixed in
PostHog#71976. Mirror that pattern here: fetch the organization first,
throw a descriptive error naming the recovery path on failure, and
only commit orgId (and the cached org) once the fetch succeeds.

Fixes PostHog#78629

Signed-off-by: aashish00021 <aashishbhardwaj07@icloud.com>
@trunk-io

trunk-io Bot commented Sep 7, 2026

Copy link
Copy Markdown

Merging to master in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

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.

MCP: switch-organization commits unvalidated state and reports success on a failed org fetch

1 participant