[Server] bugfix: Cache discovery is unavailable #113 #120
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 Description
The cached discovery functionality was not working correctly because the
DiscoveryLoaderwas not calling$registry->setDiscoveryState($discoveryState)when using cached results. This caused a critical issue where:Cache Hit Scenario: When
CachedDiscovererreturned cachedDiscoveryStatefrom the cache, theDiscoveryLoaderwould receive the cached state but never apply it to the registry viasetDiscoveryState().Registry State Mismatch: The registry would remain empty or contain stale data, even though the discovery process had successfully retrieved cached results.
Silent Failure: This bug was particularly insidious because it would fail silently - the cache would appear to work (no errors thrown), but the discovered MCP elements (tools, resources, prompts, resource templates) would not be available in the registry.
Root Cause Analysis
The issue occurred during the refactoring of the discovery process in PR #111. The original
Discoverer::discover()method was responsible for both:$this->registry->setDiscoveryState($discoveryState)However, when the discovery logic was moved to
DiscoveryLoader, the responsibility for setting the discovery state was not properly transferred. TheDiscoveryLoaderwas calling the discoverer but not applying the returnedDiscoveryStateto the registry.Technical Details
Before Fix:
After Fix:
Impact
Solution
This fix ensures that:
DiscoveryLoaderproperly captures theDiscoveryStatereturned by the discoverer (whether cached or fresh)DiscoveryStateis applied to the registry viasetDiscoveryState()Testing
The fix maintains backward compatibility and ensures that:
stdio-cached-discoveryexample now functions correctlyFiles Changed
src/Capability/Registry/Loader/DiscoveryLoader.php: Added missingsetDiscoveryState()callsrc/Capability/Discovery/Discoverer.php: Removed redundantsetDiscoveryState()call (now handled by loader)Summary
This fix resolves the core issue where cached discovery results were not being applied to the registry, ensuring that MCP servers can properly utilize cached discovery for improved performance while maintaining full functionality.
Type: Bug Fix
Breaking Change: No
Related Issue: Fixes cached discovery functionality introduced in PR #111