Skip to content

fix: eliminate redundant DB queries in read_resource and invoke_resource#2718

Merged
crivetimihai merged 2 commits intomainfrom
fix/resource-query-reduction
Feb 5, 2026
Merged

fix: eliminate redundant DB queries in read_resource and invoke_resource#2718
crivetimihai merged 2 commits intomainfrom
fix/resource-query-reduction

Conversation

@crivetimihai
Copy link
Copy Markdown
Member

Summary

  • Use joinedload(DbResource.gateway) on the initial db.get() call so the gateway relationship is eager-loaded in a single query instead of a separate lazy-load
  • Pass pre-fetched resource_obj and gateway_obj to invoke_resource() via new optional kwargs, skipping the internal re-fetch queries (Q5/Q6)
  • Guard Q3/Q4 URI-based lookups with resource_db is None so they only run for URI-only requests (not when resource was already found by ID)
  • Check enabled status in Python after Q2 instead of issuing a separate filtered query
  • Net effect: reduces per-request query count from 6 to 2 for resource-by-ID requests

Test plan

  • pytest tests/unit/mcpgateway/services/test_resource_service.py -x
  • pytest tests/unit/mcpgateway/services/test_resource_service_plugins.py -x
  • Verify read_resource(db, resource_id=...) returns correct content
  • Verify read_resource(db, resource_uri=...) still works (URI-only path unchanged)
  • Verify inactive resources raise ResourceNotFoundError

Steps 3-4 of load test RCA: reduce per-request query count from 6 to 2
for resource-by-ID requests.

Step 3: After Q2 (db.get), check enabled in Python and guard Q3/Q4 with
resource_db is None so they only run for URI-only lookups.

Step 4: Add joinedload(DbResource.gateway) to Q2, pass pre-fetched
resource_obj and gateway_obj to invoke_resource() to skip Q5/Q6.

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
@crivetimihai crivetimihai force-pushed the fix/resource-query-reduction branch from b02cae9 to 5044c23 Compare February 5, 2026 22:24
@crivetimihai crivetimihai merged commit 87ab45c into main Feb 5, 2026
46 checks passed
@crivetimihai crivetimihai deleted the fix/resource-query-reduction branch February 5, 2026 22:24
kcostell06 pushed a commit to kcostell06/mcp-context-forge that referenced this pull request Feb 24, 2026
…rce (IBM#2718)

* fix: eliminate redundant DB queries in read_resource and invoke_resource

Steps 3-4 of load test RCA: reduce per-request query count from 6 to 2
for resource-by-ID requests.

Step 3: After Q2 (db.get), check enabled in Python and guard Q3/Q4 with
resource_db is None so they only run for URI-only lookups.

Step 4: Add joinedload(DbResource.gateway) to Q2, pass pre-fetched
resource_obj and gateway_obj to invoke_resource() to skip Q5/Q6.

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>

* lint

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>

---------

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
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.

1 participant