Repository navigation
fix(backend): report denied MCP memory reads instead of an empty success - #10743
Merged
Merged
Conversation
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
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
approved these changes
Jul 28, 2026
kodjima33
left a comment
Collaborator
There was a problem hiding this comment.
Denied MCP memory reads now report the refusal instead of an indistinguishable empty success; scoped, tested, CI green.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
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. 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.pyis 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.routers/mcp_sse.py:866get_memoriesreturn {"memories": []}routers/mcp_sse.py:1094search_memoriesreturn {"memories": []}routers/mcp.py:399GET /v1/mcp/memoriesreturn []routers/mcp.py:303GET /v1/mcp/memories/searchreturn []Change
One owner for "report this, or serve an empty result":
mcp_denied_read_payloadinutils/mcp_memories.pyreturns the finished client-visible payload for every authorization or indeterminate-rollout state, andNoneforSHADOW_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 failuresCommit 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.pyby 16 lines. The third commit collapses that to the single-payload shape above, cutting the frozen file's growth to 7 lines and shrinkingrouters/mcp.pyby 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.pyis 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_stateandmissing_app_key_scope_grant. Neither can reach it:missing_app_key_scope_grantis refused earlier byauthorize_memory_external_default_memory_read—mcp_sse.py:826/:1076raiseToolExecutionErrorcode-32009, andmcp.py:277raisesHTTPException. It never reaches the empty return.missing_rollout_stateis allowed through to legacy bymcp_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
-32009error. 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_ONLYis deliberately left returning empty. It is not an authorization denial.docs/epics/memory_normative_architecture.mddefines 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:SHADOW_ONLYstill returns empty without error, on all four sites — passed before and after, so the change is boundedVerification
Backend, aarch64, system
python3.12+pytest 9.0.3.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
python3fallback documented intest.shbecause the pinned CPython 3.11.15 has nolinux-aarch64build, soscripts/sync-python-deps.shcannot 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