Skip to content

Fix/embedding initialization and single instance - #134

Merged
thomaszang merged 7 commits into
mainfrom
fix/embedding-initialization-and-single-instance
Oct 21, 2026
Merged

thomaszang merged 7 commits into
mainfrom
fix/embedding-initialization-and-single-instance

Conversation

@thomaszang

Copy link
Copy Markdown
Member

No description provided.

Remove --port flag from start script to use pure stdio mode instead of HTTP mode.
HTTP mode with --port spawns a new instance per request, causing the multiple
instance problem. Direct stdio mode maintains a single persistent instance.

Before: npx -y supergateway --port 9881 --stdio "node ./build/start.js"
After: node ./build/start.js

This ensures only one MCP server instance runs at a time.
Complete fix for initialization order and async handling:

1. Move ALL initialization BEFORE server.connect():
   - Get server configuration
   - Generate/load embeddings (with await)
   - Load documentation files
   - Setup telemetry
   - THEN connect to transport

2. Add missing await keywords:
   - await retrieveAndStore() - line 1898
   - await retrieveAndStoreRule() - line 1910

3. Use correct embeddings path:
   - ./embeddings/ instead of ./build/embeddings/

This ensures:
- Server only signals ready when fully initialized
- Embeddings complete before accepting requests
- No race conditions with stdio mode
- Single instance handles all requests correctly
…ition

Changes:
- Make retrieveAndStoreRule() async and return Promise<void>
- Add await before createEmbeddings() call
- Fix embeddings.d.ts to declare createEmbeddings returns Promise<void>

This ensures rule embeddings fully complete before server.connect() is called.
Previously, retrieveAndStoreRule fired createEmbeddings without await, causing
embeddings to generate in the background after the server reported ready.
@thomaszang
thomaszang requested a review from yaosheng79 October 21, 2026 07:56
The path should go up two levels from build/lib/ to reach project root:
- Before: path.join(__dirname, "..", "embeddings") → build/embeddings/
- After: path.join(__dirname, "..", "..", "embeddings") → ./embeddings/

This fixes the issue where embeddings were written to build/embeddings/
but the code was checking ./embeddings/, causing regeneration on every start.
Keep embeddings in build/embeddings/ as requested:
- Revert embeddings.js path back to ../embeddings (from build/lib)
- Update index.ts to check build/embeddings
- Remove /embeddings/ from gitignore (not needed)

Embeddings survive rebuilds because tsc doesn't clear the build directory,
it only replaces .js files. The build/embeddings folder persists.
@thomaszang
thomaszang merged commit 2093f6b into main Oct 21, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants