Skip to content

fix(backend): report denied MCP memory reads instead of an empty success - #10743

Merged
kodjima33 merged 4 commits into
mainfrom
fix/mcp-denied-memory-read-error
Jul 28, 2026
Merged

kodjima33 merged 4 commits into
mainfrom
fix/mcp-denied-memory-read-error

Conversation

@mdmohsin7

@mdmohsin7 mdmohsin7 commented Jul 27, 2026 •

Copy link
Copy Markdown
Member

Problem

When the memory authorization check denied a hosted-MCP memory read, the handler returned an empty result set with a success status:

if not mcp_legacy_read_authorized(memory_list_results):
    return {"memories": []}

The client saw "you have no memories" rather than "this key is not authorized" — the failure mode most likely to make a user believe their data was deleted. An empty success is indistinguishable from a genuinely empty account, so the user had no signal anything was wrong and nothing to report, support could not separate the two without a per-account Firestore trace, and it silently contradicted the memories list screen, which showed the same user's memories fine.

Scope: the issue named 2 sites, there are 4

routers/mcp.py is the REST MCP surface and carries the identical shape. Fixing only the SSE pair would repair the symptom on one surface and leave the same contract violated on the other, so all four are fixed here. The widened scope is deliberate, not creep.

Site Tool / route Before
routers/mcp_sse.py:866 get_memories return {"memories": []}
routers/mcp_sse.py:1094 search_memories return {"memories": []}
routers/mcp.py:399 GET /v1/mcp/memories return []
routers/mcp.py:303 GET /v1/mcp/memories/search return []

Change

One owner for "report this, or serve an empty result": mcp_denied_read_payload in utils/mcp_memories.py returns the finished client-visible payload for every authorization or indeterminate-rollout state, and None for SHADOW_ONLY. The payload mirrors the existing grant-denial shape so both refusals read the same to a client.

Each surface then adds only its own raise, keeping its existing error mechanism:

  • routers/mcp.py → HTTPException(403, detail={'enabled': False, 'reason': ..., 'consumer': 'mcp'})
  • routers/mcp_sse.py → ToolExecutionError(str(payload), code=-32009), the code already used for MCP authorization failures

Commit order note

The first two commits landed a two-function classifier plus a per-surface raiser helper, which grew the ratchet-frozen routers/mcp_sse.py by 16 lines. The third commit collapses that to the single-payload shape above, cutting the frozen file's growth to 7 lines and shrinking routers/mcp.py by 9. Those commits are already pushed and this repo does not force-push, so the reshape is a follow-up commit rather than a rewrite; the description above is the shape being merged.

backend/routers/mcp_sse.py is frozen by the oversized-product-file ratchet at 1858 lines. Its baseline is raised to the exact new count (1865) with a justification, because the remaining 7 lines are the two raises this fix exists to add and cannot be removed without leaving a surface unrepaired.

Which reasons are reported

The states the caller cannot see or act on: malformed_rollout_state, rollout_read_failed, missing_mcp_default_memory_grant, uid_mismatch, unsupported_consumer, unsupported_rollout_schema, memory_reads_disabled.

Two corrections to the issue

1. The stated reason set is wrong, and so is the scale claim. The issue says the two reasons reaching this branch are missing_rollout_state and missing_app_key_scope_grant. Neither can reach it:

  • missing_app_key_scope_grant is refused earlier by authorize_memory_external_default_memory_read — mcp_sse.py:826/:1076 raise ToolExecutionError code -32009, and mcp.py:277 raises HTTPException. It never reaches the empty return.
  • missing_rollout_state is allowed through to legacy by mcp_legacy_read_authorized (utils/mcp_memories.py:130), so it does not reach the empty return either.

Consequently the issue's evidence — "MCP keys created without scopes have no grant, so every memory read through them returns empty while the account has thousands of memories" — does not hold: those keys already receive an explicit -32009 error. The bug is real for the reason set above, but the affected population is narrower than the issue implies, and the headline scale claim is unsupported. No live measurement was taken.

2. SHADOW_ONLY is deliberately left returning empty. It is not an authorization denial. docs/epics/memory_normative_architecture.md defines shadow mode as "Legacy authoritative; memory audit artifacts only", so converting it to an error would be wrong in the other direction. It is a separate concern from this fix and is untouched here.

Tests

Added to tests/unit/test_mcp_data_endpoints.py, which already exercises both the REST handlers and MCP tool dispatch. Parametrized over all four sites × the seven reported reasons, asserting both directions:

  • every reported reason raises carrying its reason, on all four sites — 28 failed before this change, pass after
  • SHADOW_ONLY still returns empty without error, on all four sites — passed before and after, so the change is bounded

Verification

Backend, aarch64, system python3.12 + pytest 9.0.3.

BACKEND_UNIT_TEST_FILE_LIST=<15 files> bash test.sh   ->  275 passed, 0 failed

Covering test_mcp_data_endpoints (66), test_mcp_memory_adapter (27), test_mcp_search_memories (20), test_lock_bypass_fixes (61), test_mcp_apps_results (40), test_non_active_route_admin_endpoint (17), test_dev_api_canonical_grant_ordering (10), test_product_memory_router (8), test_mcp_conversation_by_id_poison (7), test_mcp_conversations_poison (5), test_app_client_response_models (4), test_mcp_sse_pagination_bounds (3), test_mcp_action_item_writes (3), test_mcp_profile_contact (2), test_mcp_search_conversations_poison (2).

Local runs used the python3 fallback documented in test.sh because the pinned CPython 3.11.15 has no linux-aarch64 build, so scripts/sync-python-deps.sh cannot create the pinned venv on this machine. CI on the pinned interpreter is the authoritative gate for this PR.

Closes #10735

Invariants: INV-MEM-1
Failure-Class: new

Both MCP surfaces refuse a default-memory read in the same place — after
`mcp_legacy_read_authorized` declines the legacy collection — but each surface
then decided on its own what to return. Give that decision one owner.

`mcp_denied_read_reason` answers "report this, or serve an empty result": it
returns the decision's reason for every authorization or indeterminate-rollout
state, and None for SHADOW_ONLY, which is not a denial. `mcp_memory_read_denied_detail`
builds the client-visible payload, mirroring the existing grant-denial shape.

No caller uses these yet, so behavior is unchanged.
When the memory authorization check denied a hosted-MCP memory read, the handler
returned an empty result set with a success status. The client saw "you have no
memories" rather than "this key is not authorized" — the failure mode most likely
to make a user believe their data was deleted. Support could not separate the two
without a per-account Firestore trace, and it silently contradicted the memories
list screen, which showed the same user's memories fine.

Each surface now raises from the shared classification, so a refused read carries
its reason:
  routers/mcp.py:303      search_memories   -> HTTPException 403 + reason detail
  routers/mcp.py:399      get_memories      -> HTTPException 403 + reason detail
  routers/mcp_sse.py:866  get_memories      -> ToolExecutionError code -32009
  routers/mcp_sse.py:1094 search_memories   -> ToolExecutionError code -32009

The issue named two sites. There are four: routers/mcp.py is the REST MCP surface
and carries the identical shape. Fixing only the SSE pair would leave the same
contract violated on the other surface, so both are repaired here. The two
surfaces keep their own error mechanisms; only the classification is shared.

Reported reasons are the states the caller cannot see or act on:
malformed_rollout_state, rollout_read_failed, missing_mcp_default_memory_grant,
uid_mismatch, unsupported_consumer, unsupported_rollout_schema,
memory_reads_disabled.

SHADOW_ONLY deliberately keeps returning empty. It is not an authorization
denial — during shadow mode legacy remains authoritative — so reporting it as an
auth error would be wrong in the other direction.

The issue also states the reaching reasons are missing_rollout_state and
missing_app_key_scope_grant. Neither can reach this branch:
missing_app_key_scope_grant is already refused earlier by
authorize_memory_external_default_memory_read (mcp_sse.py:826/:1076,
mcp.py:277), and missing_rollout_state is allowed through to legacy by
mcp_legacy_read_authorized. The reasons above are what actually arrive here.

Tests (added, and confirmed failing before the change):
  tests/unit/test_mcp_data_endpoints.py
  - each reported reason raises with its reason on all four sites
    (28 failed before this change, pass after)
  - SHADOW_ONLY still returns empty without error on all four sites
    (passed before and after, so the change is bounded)

Verification (backend, aarch64, system python3.12 + pytest 9.0.3):
  BACKEND_UNIT_TEST_FILE_LIST=<15 files> bash test.sh -> 275 passed, 0 failed

Failure-Class: new
@mdmohsin7 mdmohsin7 added bug Something isn't working backend Backend Task (python) labels Jul 27, 2026
A read refused by authorization or an indeterminate rollout state was returned as
a successful empty collection, which the caller cannot distinguish from a
genuinely empty account. Four MCP call sites across two surfaces each decided
independently to return an empty result rather than report the refusal.

Records the shared classifier and its behavioral test as the class's guard
artifact, so the class lands with a reusable guard surface.
…sifier

`backend/routers/mcp_sse.py` is frozen by the oversized-product-file ratchet at
1858 lines, and the first shape of this fix grew it by 16: a two-function
classifier in utils plus a per-surface raiser helper in each router.

Collapse the two utils functions into one. `mcp_denied_read_payload` now returns
the finished client-visible payload, or None for SHADOW_ONLY, so the
classification and the wire shape stay together and each surface adds only its
own raise. Both per-surface raiser helpers go away; the four call sites raise
inline, keeping their own error mechanism.

That cuts the frozen file's growth from 16 lines to 7, and drops
`backend/routers/mcp.py` by 9 as well. Behavior is identical — the same payload
reaches the same four call sites, verified by the same tests.

Raises the mcp_sse.py ratchet baseline to its exact new count with a
justification, since the remaining 7 lines are the two raises this fix exists to
add and cannot be removed without leaving a surface unrepaired.

Verification (backend, aarch64, system python3.12 + pytest 9.0.3):
  BACKEND_UNIT_TEST_FILE_LIST=tests/unit/test_mcp_data_endpoints.py bash test.sh
  -> 66 passed, 0 failed
  scripts/pr-preflight --pr-body-file ... -> 21 checks PASS

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Denied MCP memory reads now report the refusal instead of an indistinguishable empty success; scoped, tested, CI green.

@kodjima33
kodjima33 merged commit 79d1816 into main Jul 28, 2026
39 checks passed
@kodjima33
kodjima33 deleted the fix/mcp-denied-memory-read-error branch July 28, 2026 14:38
undivisible pushed a commit to Archit-lal/omi that referenced this pull request Jul 29, 2026
…ess (BasedHardware#10743)

* refactor(backend): classify a refused MCP memory read once

Both MCP surfaces refuse a default-memory read in the same place — after
`mcp_legacy_read_authorized` declines the legacy collection — but each surface
then decided on its own what to return. Give that decision one owner.

`mcp_denied_read_reason` answers "report this, or serve an empty result": it
returns the decision's reason for every authorization or indeterminate-rollout
state, and None for SHADOW_ONLY, which is not a denial. `mcp_memory_read_denied_detail`
builds the client-visible payload, mirroring the existing grant-denial shape.

No caller uses these yet, so behavior is unchanged.

* fix(backend): report denied MCP memory reads instead of an empty success

When the memory authorization check denied a hosted-MCP memory read, the handler
returned an empty result set with a success status. The client saw "you have no
memories" rather than "this key is not authorized" — the failure mode most likely
to make a user believe their data was deleted. Support could not separate the two
without a per-account Firestore trace, and it silently contradicted the memories
list screen, which showed the same user's memories fine.

Each surface now raises from the shared classification, so a refused read carries
its reason:
  routers/mcp.py:303      search_memories   -> HTTPException 403 + reason detail
  routers/mcp.py:399      get_memories      -> HTTPException 403 + reason detail
  routers/mcp_sse.py:866  get_memories      -> ToolExecutionError code -32009
  routers/mcp_sse.py:1094 search_memories   -> ToolExecutionError code -32009

The issue named two sites. There are four: routers/mcp.py is the REST MCP surface
and carries the identical shape. Fixing only the SSE pair would leave the same
contract violated on the other surface, so both are repaired here. The two
surfaces keep their own error mechanisms; only the classification is shared.

Reported reasons are the states the caller cannot see or act on:
malformed_rollout_state, rollout_read_failed, missing_mcp_default_memory_grant,
uid_mismatch, unsupported_consumer, unsupported_rollout_schema,
memory_reads_disabled.

SHADOW_ONLY deliberately keeps returning empty. It is not an authorization
denial — during shadow mode legacy remains authoritative — so reporting it as an
auth error would be wrong in the other direction.

The issue also states the reaching reasons are missing_rollout_state and
missing_app_key_scope_grant. Neither can reach this branch:
missing_app_key_scope_grant is already refused earlier by
authorize_memory_external_default_memory_read (mcp_sse.py:826/:1076,
mcp.py:277), and missing_rollout_state is allowed through to legacy by
mcp_legacy_read_authorized. The reasons above are what actually arrive here.

Tests (added, and confirmed failing before the change):
  tests/unit/test_mcp_data_endpoints.py
  - each reported reason raises with its reason on all four sites
    (28 failed before this change, pass after)
  - SHADOW_ONLY still returns empty without error on all four sites
    (passed before and after, so the change is bounded)

Verification (backend, aarch64, system python3.12 + pytest 9.0.3):
  BACKEND_UNIT_TEST_FILE_LIST=<15 files> bash test.sh -> 275 passed, 0 failed

Failure-Class: new

* chore(failure-classes): register denial-rendered-as-empty-success

A read refused by authorization or an indeterminate rollout state was returned as
a successful empty collection, which the caller cannot distinguish from a
genuinely empty account. Four MCP call sites across two surfaces each decided
independently to return an empty result rather than report the refusal.

Records the shared classifier and its behavioral test as the class's guard
artifact, so the class lands with a reusable guard surface.

* refactor(backend): return the whole denied-read payload from one classifier

`backend/routers/mcp_sse.py` is frozen by the oversized-product-file ratchet at
1858 lines, and the first shape of this fix grew it by 16: a two-function
classifier in utils plus a per-surface raiser helper in each router.

Collapse the two utils functions into one. `mcp_denied_read_payload` now returns
the finished client-visible payload, or None for SHADOW_ONLY, so the
classification and the wire shape stay together and each surface adds only its
own raise. Both per-surface raiser helpers go away; the four call sites raise
inline, keeping their own error mechanism.

That cuts the frozen file's growth from 16 lines to 7, and drops
`backend/routers/mcp.py` by 9 as well. Behavior is identical — the same payload
reaches the same four call sites, verified by the same tests.

Raises the mcp_sse.py ratchet baseline to its exact new count with a
justification, since the remaining 7 lines are the two raises this fix exists to
add and cannot be removed without leaving a surface unrepaired.

Verification (backend, aarch64, system python3.12 + pytest 9.0.3):
  BACKEND_UNIT_TEST_FILE_LIST=tests/unit/test_mcp_data_endpoints.py bash test.sh
  -> 66 passed, 0 failed
  scripts/pr-preflight --pr-body-file ... -> 21 checks PASS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend Task (python) bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hosted MCP returns an empty 200 instead of an error when a memory read is denied

2 participants