perf: add inventory cache for stateless server patterns#1636
Closed
SamMorrowDrums wants to merge 2 commits into
Closed
perf: add inventory cache for stateless server patterns#1636SamMorrowDrums wants to merge 2 commits into
SamMorrowDrums wants to merge 2 commits into
Conversation
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.
Summary
Add
CachedInventoryto build tool/resource/prompt definitions once at startup rather than per-request. This is particularly useful for the remote server pattern where a new server instance is created per request.Problem
In stateless deployments (like the remote MCP server), a new server and inventory is created for each incoming request. This means
AllTools(t),AllResources(t), andAllPrompts(t)are called repeatedly, rebuilding all ~130 tool definitions including JSON schema generation on every request.Related: modelcontextprotocol/go-sdk#685
Solution
Add a
CachedInventorythat:sync.Oncefor thread-safe initializationAPI
Key Features
InitInventoryCache(t)- Called once at startup with your translatorCachedInventoryBuilder()- Returns a builder pre-populated with cached definitionssync.OnceNewInventory(t)still works without cachingWhy This Works
The elegance is preserved because:
GetMe(t),GetTeams(t)pattern stays the sameAvailableTools(ctx), not at definition timeNullTranslationHelperanyway, this is fineFor Remote Server Integration
Testing
inventory_cache_test.goscript/lintpasses