fix(resources): fetch federated resource live when cached content is NULL - #5467
fix(resources): fetch federated resource live when cached content is NULL#5467Ewertonslv wants to merge 1 commit into
Conversation
…NULL
In cache gateway mode, read_resource reads `content = resource_db.content`
for a federated resource. When both text_content and binary_content are NULL
(e.g. resources created via the migration / re-discovery path), the
Resource.content property raises ValueError("Resource has no content") before
the RESOLVE CONTENT block. The transport layer catches it and returns empty
content, so the client silently receives an empty read instead of the
upstream's real content.
Catch that ValueError in the cache-mode branch and, when the resource is
federated (has a gateway), build a metadata-only ResourceContent so the
existing RESOLVE CONTENT block fetches it live from the gateway -- the same
path already used for resources whose cached content is an empty string.
Non-federated resources have nothing to fetch, so the original error is
preserved.
Adds regression tests for the NULL-content federated read (fetches live) and
the non-federated case (error preserved, no live fetch attempted).
Refs IBM#5450
Signed-off-by: Ewerton Silva <ewertoncom297@gmail.com>
|
Correction to my checklist above: I ticked "the linked issue is not labeled For coordination: this and #5463 were opened the same day and both touch |
|
Hi @Ewertonslv — sincere apologies for leaving this PR open for so long without a proper review or merge. That's on us, and we're sorry for the lack of responsiveness. We're closing this PR now as part of a housekeeping pass on long-open contributions. If you're still interested in contributing this change, please feel free to:
We genuinely appreciate your contribution and hope to give it the attention it deserves. Thank you for taking the time to contribute to ContextForge! 🙏 |
|
Thanks @jonpspri — no worries. Rebased onto current On step 1 (tracking issue): #5450 already exists and is still open, so I did not file a duplicate — happy to open a fresh one if you'd rather. Still reproducible on today's |
🔗 Related Issue
Refs #5450 (scoped to the NULL-content case — see Scope below; not an auto-close).
📝 Summary
In
cachegateway mode,ResourceService.read_resourcereadscontent = resource_db.contentfor a federated resource. When bothtext_contentandbinary_contentareNULL(e.g. resources created via themigration / re-discovery path,
createdVia: rediscovery), theResource.contentproperty raises
ValueError("Resource has no content")before the RESOLVECONTENT block. The transport layer catches the exception and returns empty
content, so the client silently receives an empty read instead of the upstream's
real content.
Fix
Catch that
ValueErrorin the cache-mode branch and, when the resource isfederated (has a gateway), build a metadata-only
ResourceContentso theexisting RESOLVE CONTENT block fetches it live from the gateway — the same
path already used for resources whose cached content is an empty string. A
non-federated resource has nothing to fetch, so the original error is preserved
(re-raised).
Tests (before → after)
..._null_content_federated_fetches_live: NULL content + gateway → without thefix,
ValueErrorpropagates and the read is empty; with the fix, the content isfetched live via
invoke_resource. (RED→GREEN verified locally withgit stash.)..._null_content_no_gateway_preserved: NULL content + no gateway → the"no content" error is preserved and no live fetch is attempted.
pytest tests/unit/mcpgateway/services/test_resource_service.pypasses locally(full file green, including module doctests via
--doctest-modules).🔎 Scope / follow-up (binary resources)
The issue also notes that binary federated resources read empty for a
separate reason:
invoke_resource's SSE/StreamableHTTP helpers extract the livepayload with
getattr(contents[0], "text")unconditionally, so aBlobResourceContents(e.g.image/png) yieldsNone. Fixing that cleanlyrequires
invoke_resourceto signal which field (text vs blob) the upstreamreturned — both are strings, so they can't be disambiguated after the fact — i.e.
a small change to its return contract, plus routing in the RESOLVE block. That is
a deliberate design decision I left out of this PR to keep it focused and
low-risk. Happy to follow up with that in a separate PR if you'd like — just let
me know your preference on the
invoke_resourcereturn shape.📏 Reviewability
triage🏷️ Type of Change