Moonverse.ts is a Model Context Protocol (MCP) server that provides AI-powered search over MoonBit documentation and source code using Google's Gemini Interactions API.
The main server class that implements the MCP protocol using the @modelcontextprotocol/sdk.
Key Responsibilities:
- Manage server lifecycle
- Handle tool registration and requests (two tools: docs and source)
- Coordinate between file management and query processing
- Maintain separate caches for documentation and source files
Uses Google's GoogleAIFileManager to upload and manage both documentation and source code files.
Flow:
- Scan
store/directory for documentation files (.txt,.md) - Scan
source/directory recursively for source files (.mbt,.md,.json,.txt) - Upload files lazily on first query to Gemini's Interactions API
- Store file metadata in separate lists for documentation and source code
- Cache uploaded files in memory to avoid re-uploads
Handles two types of queries using Gemini's Interactions API with uploaded files as context.
Documentation Queries (query_moonbit_docs):
- Uses files from
store/directory - Focuses on answering based on official documentation
- Provides user-facing feature information
Source Code Queries (query_moonbit_source):
- Uses files from
source/directory - Focuses on implementation details and API insights
- Provides information about latest features and internal code
Flow:
- Receive query via MCP tool call (
query_moonbit_docsorquery_moonbit_source) - Initialize appropriate file set (documentation or source) if not already done
- Create context-aware prompt
- Send to Gemini Interactions API with file references
- Return AI-generated response
User (Claude Desktop)
↓
MCP Protocol
↓
Tool Selection (docs or source)
↓
MoonverseMCPServer.handleDocQuery() or handleSourceQuery()
↓
Initialize files (first time only)
↓
Gemini Interactions API (with file context)
↓
AI Response
↓
MCP Response
↓
User sees answer
moonverse.ts/
├── src/
│ └── index.ts # Main server implementation
├── store/ # Documentation files
│ ├── Agents.mbt.md
│ ├── ide.md
│ └── llms.txt
├── source/ # MoonBit core repository (optional)
│ └── (MoonBit source files)
├── dist/ # Compiled JavaScript
│ ├── index.js
│ └── index.d.ts
├── README.md # Project overview
├── USAGE.md # Usage guide
├── ARCHITECTURE.md # This file
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
└── claude_desktop_config.example.json # Example config
- Protocol for AI assistants to access external tools
- Uses stdio for communication
- Supports tools, resources, and prompts
- This project implements two tools for different query types
- Google's AI platform with advanced file handling
- File upload and management
- Context-aware generation with multiple files
- Supports multiple file types including source code
- Type-safe development
- ES2020 modules
- Strict mode for better error checking
Files are only uploaded on the first query to avoid startup delays and API costs. Separate initialization for docs and source.
Two separate tools allow users to explicitly choose between documentation queries (user-facing) and source code queries (implementation details).
The source directory is scanned recursively to handle complex repository structures with nested directories.
Uploaded file metadata is stored in memory in separate lists (docs and source) to avoid re-uploading on subsequent queries within the same session.
Beyond just .txt and .md, the system now supports .mbt (MoonBit source) and .json files for comprehensive source code analysis.
All errors are caught and returned as MCP responses with isError: true, ensuring the MCP client can handle failures gracefully.
Uses ES modules (type: "module") for compatibility with the MCP SDK and modern Node.js practices.
Uses stdio for MCP communication, making it compatible with Claude Desktop and other MCP clients.
- API Key Protection: API key is read from environment variables, never hardcoded
- File System Access: Limited to the
store/directory - Input Validation: All tool inputs are validated using Zod schemas
- Error Messages: Generic error messages to avoid leaking sensitive information
- Files are uploaded on each server start
- No caching between sessions
- Single-threaded Node.js process
- Persistent file store to avoid re-uploads
- Support for file search stores API (when available in SDK)
- Batch file uploads
- Caching layer for common queries
- Support for incremental document updates
Currently, the project requires manual testing with a valid Gemini API key. Future improvements could include:
- Unit tests for file processing logic
- Integration tests with mock Gemini API
- End-to-end tests with MCP client simulator
The server logs important events to stderr:
- File upload progress
- Query processing
- Errors and exceptions
These logs can be captured by the MCP host (e.g., Claude Desktop) for debugging.