fix: Implement LazyToolLoader pattern to resolve 'Client not initialized' error#1
Open
Adriftnote wants to merge 1 commit intothedotmack:mainfrom
Open
fix: Implement LazyToolLoader pattern to resolve 'Client not initialized' error#1Adriftnote wants to merge 1 commit intothedotmack:mainfrom
Adriftnote wants to merge 1 commit intothedotmack:mainfrom
Conversation
…zed" error - Add ToolCache class for 24-hour TTL tool caching (~/.mcp-cli-cache.json) - Modify registerServerTools to use lazy connection pattern - Add cache management commands (refresh, clear-cache) - Update help text to include new cache commands This fixes the "Client not initialized" error that occurred because the client was connecting during tool registration, then immediately disconnecting before tools were actually executed. The lazy loading pattern defers client connection until tool execution time. Performance improvements: - 2.1x faster execution (0.983s → 0.475s) - 10x less CPU usage (0.370s → 0.037s user time) - Reduced server load (cached tool discovery) Tested with multiple MCP servers: - sqlite_tiktok: 18 tables, all queries successful - sqlite_instagram: 30 tables, all queries successful - sqlite_dashboard: 10 tables, all queries successful Related pattern: Anthropic Tool Search with Embeddings https://github.com/anthropics/anthropic-cookbook/blob/main/tool_use/tool_search_with_embeddings.ipynb
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.
Fix: Implement LazyToolLoader pattern to resolve "Client not initialized" error
Background
Hi! 👋 First of all, thank you so much for creating
mcp-client-cli. I've been using claude-mem and it's been incredibly helpful for my workflow. I wanted to interact with MCP servers directly from the command line, so I found your CLI tool.While testing it with my SQLite MCP servers, I encountered the "Client not initialized" error. After diving into the code and debugging, I found the root cause and implemented a fix based on Anthropic's LazyToolLoader pattern.
This is my first time contributing to GitHub, so please let me know if I should change anything! 😅 I hope this helps improve the tool.
Problem
The CLI was experiencing a "Client not initialized" error because:
registerServerTools)Solution
Implemented LazyToolLoader pattern with tool caching:
1. Added
ToolCacheclass~/.mcp-cli-cache.json2. Modified
registerServerTools3. Added cache management commands
Performance Improvements
Testing
Tested with 3 MCP servers in my environment:
Basic functionality
Query execution
Cache management
All tests passed ✅
Related Work
This pattern is inspired by:
Files Changed
src/client/ToolCache.ts- New file (90 lines)src/cli/index.ts- Modified (59 lines changed)Thank you for reviewing! Let me know if you'd like any changes. 🙏