Skip to main content
Glama
LAZY-LOADING.mdโ€ข4.62 kB
# Lazy Loading Implementation This MCP server implements **lazy loading of configurations** as required by Smithery for optimal deployment performance. ## ๐ŸŽฏ Smithery Requirements As per [Smithery documentation](https://smithery.ai/docs/build/deployments#tool-lists), servers should perform lazy loading of configurations to ensure fast discovery and deployment. ## โœ… **FIXED: "failedToFetchConfigSchema" Error** The "failedToFetchConfigSchema" error has been **completely resolved** by implementing true lazy loading: **๐Ÿ”ง Root Cause Fixed:** - โŒ **Before:** `dotenv.config()` ran at module import time - โŒ **Before:** `tryAuthenticatePocketBase()` was async and accessed env vars during tool registration - โŒ **Before:** Environment variables accessed during server startup **โœ… **After:** Complete Lazy Loading** - โœ… `dotenv.config()` only runs when tools are invoked - โœ… `tryAuthenticatePocketBase()` is synchronous with no config access - โœ… Zero configuration loading during schema fetching - โœ… Server starts instantly without any external dependencies ## ๐Ÿ”ง Implementation Details ### Before (Eager Loading) ```javascript // โŒ Configurations loaded immediately at startup const pb = new PocketBase(process.env.POCKETBASE_URL || 'http://127.0.0.1:8090'); const DOCUMENTS_COLLECTION = process.env.DOCUMENTS_COLLECTION || 'documents'; const DEBUG = process.env.DEBUG === 'true'; ``` ### After (Lazy Loading) โœ… ```javascript // โœ… Configurations loaded only when needed let pb = null; let DOCUMENTS_COLLECTION = null; let DEBUG = null; let configInitialized = false; function initializeConfig() { if (configInitialized) return; pb = new PocketBase(process.env.POCKETBASE_URL || 'http://127.0.0.1:8090'); DOCUMENTS_COLLECTION = process.env.DOCUMENTS_COLLECTION || 'documents'; DEBUG = process.env.DEBUG === 'true'; configInitialized = true; } ``` ## ๐Ÿš€ Benefits 1. **Fast Startup**: Server starts immediately without waiting for PocketBase connection 2. **Discovery Mode**: Works during Smithery discovery even without credentials 3. **Dynamic Configuration**: Can accept Smithery query parameters at runtime 4. **Resource Efficient**: Only initializes what's needed when needed ## ๐Ÿ”„ Lazy Loading Points ### 1. Tool Invocation (FIXED) The key fix was ensuring authentication only happens when tools are invoked: ```javascript async ({ url }) => { // โœ… Only check if credentials exist (no actual connection) const canAuth = await tryAuthenticatePocketBase(); if (!canAuth) { throw new Error('PocketBase authentication required.'); } // โœ… Actually authenticate only when tool is invoked await authenticateWhenNeeded(); // ... rest of tool logic } ``` **Key Functions:** - `tryAuthenticatePocketBase()` - Only checks if credentials exist (no connection) - `authenticateWhenNeeded()` - Actually connects to PocketBase when needed ### 2. HTTP Endpoints Configurations loaded on first HTTP request: ```javascript app.all('/mcp', async (req, res) => { if (Object.keys(req.query).length > 0) { const smitheryConfig = parseSmitheryConfig(req.query); applySmitheryConfig(smitheryConfig); // Force reinitialize with new settings configInitialized = false; initializeConfig(); } // ... handle request }); ``` ### 3. Server Startup (CRITICAL FIX) The main issue was server trying to authenticate during startup: ```javascript // โŒ BEFORE (caused failedToFetchConfigSchema) async function main() { await authenticatePocketBase(); // โŒ This breaks lazy loading! // ... start server } // โœ… AFTER (proper lazy loading) async function main() { console.error('โšก Lazy loading enabled - PocketBase connection deferred until first tool use'); // ... start server without any authentication } ``` ## ๐Ÿงช Testing Run the lazy loading test: ```bash node test-lazy-loading.js ``` This test verifies: - Server starts without attempting PocketBase connection - No authentication errors during startup - Configurations are only loaded when tools are invoked ## ๐Ÿ“‹ Checklist - โœ… Server starts without configuration loading - โœ… PocketBase connection is lazy - โœ… Environment variables read on-demand - โœ… Smithery query parameters supported - โœ… All tools work with lazy loading - โœ… HTTP endpoints support lazy loading - โœ… Resources support lazy loading - โœ… Test coverage for lazy loading ## ๐Ÿ”— References - [Smithery Deployment Documentation](https://smithery.ai/docs/build/deployments#tool-lists) - [Model Context Protocol Specification](https://modelcontextprotocol.io/specification/)

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/DynamicEndpoints/documentation-mcp-using-pocketbase'

If you have feedback or need assistance with the MCP directory API, please join our Discord server