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/)