config.py•4.92 kB
"""
Configuration settings for RTIdeas MCP Server
Handles MongoDB connection and MCP server settings
"""
import os
from pathlib import Path
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Project paths
PROJECT_ROOT = Path(__file__).parent.parent
DATA_DIR = PROJECT_ROOT / "data"
RAW_DATA_DIR = DATA_DIR / "raw"
# MongoDB configuration
MONGODB_URI = os.getenv("MONGODB_URI")
MONGODB_DATABASE = os.getenv("MONGODB_DATABASE", "rtideas")
# MCP Server settings
MCP_SERVER_NAME = "rtideas-mcp"
MCP_SERVER_VERSION = "1.0.0"
# Logging settings
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
# Feature flags
ENABLE_AUTO_ANALYSIS = os.getenv("ENABLE_AUTO_ANALYSIS", "true").lower() == "true"
ENABLE_WEBSOCKET = os.getenv("ENABLE_WEBSOCKET", "true").lower() == "true"
# API settings
API_TIMEOUT = int(os.getenv("API_TIMEOUT", "30")) # seconds
MAX_RESULTS = int(os.getenv("MAX_RESULTS", "100"))
# Web server settings
API_HOST = os.getenv("API_HOST", "0.0.0.0")
API_PORT = int(os.getenv("API_PORT", "8000"))
API_RELOAD = os.getenv("API_RELOAD", "false").lower() == "true"
# CORS settings
CORS_ORIGINS = os.getenv("CORS_ORIGINS", "*").split(",")
CORS_CREDENTIALS = os.getenv("CORS_CREDENTIALS", "true").lower() == "true"
# Search settings
DEFAULT_SEARCH_LIMIT = int(os.getenv("DEFAULT_SEARCH_LIMIT", "10"))
MAX_SEARCH_LIMIT = int(os.getenv("MAX_SEARCH_LIMIT", "100"))
# Clustering settings
DEFAULT_CLUSTER_METHOD = os.getenv("DEFAULT_CLUSTER_METHOD", "hdbscan")
DEFAULT_CLUSTER_K = int(os.getenv("DEFAULT_CLUSTER_K", "8"))
# Similarity settings
DEFAULT_SIMILARITY_THRESHOLD = float(os.getenv("DEFAULT_SIMILARITY_THRESHOLD", "0.6"))
def validate_config() -> tuple[bool, list[str]]:
"""
Validate the configuration settings.
Returns:
Tuple of (is_valid, list_of_errors)
"""
errors = []
# Validate MongoDB configuration
if not MONGODB_URI:
errors.append("MONGODB_URI environment variable is required")
# Validate numeric settings
if API_TIMEOUT <= 0:
errors.append("API_TIMEOUT must be a positive integer")
if MAX_RESULTS <= 0:
errors.append("MAX_RESULTS must be a positive integer")
if DEFAULT_SEARCH_LIMIT <= 0:
errors.append("DEFAULT_SEARCH_LIMIT must be a positive integer")
if MAX_SEARCH_LIMIT <= 0:
errors.append("MAX_SEARCH_LIMIT must be a positive integer")
if DEFAULT_CLUSTER_K < 2:
errors.append("DEFAULT_CLUSTER_K must be at least 2")
if not (0.0 <= DEFAULT_SIMILARITY_THRESHOLD <= 1.0):
errors.append("DEFAULT_SIMILARITY_THRESHOLD must be between 0.0 and 1.0")
# Validate cluster method
valid_cluster_methods = ["hdbscan", "kmeans", "louvain"]
if DEFAULT_CLUSTER_METHOD not in valid_cluster_methods:
errors.append(f"DEFAULT_CLUSTER_METHOD must be one of: {valid_cluster_methods}")
return (len(errors) == 0, errors)
def get_config_summary() -> dict:
"""
Get a summary of the current configuration.
Returns:
Dictionary with configuration details
"""
return {
"mongodb": {
"uri": MONGODB_URI[:50] + "..." if MONGODB_URI and len(MONGODB_URI) > 50 else MONGODB_URI,
"database": MONGODB_DATABASE,
},
"server": {
"name": MCP_SERVER_NAME,
"version": MCP_SERVER_VERSION,
"timeout": API_TIMEOUT,
"host": API_HOST,
"port": API_PORT,
"reload": API_RELOAD,
},
"search": {
"default_limit": DEFAULT_SEARCH_LIMIT,
"max_limit": MAX_SEARCH_LIMIT,
},
"clustering": {
"default_method": DEFAULT_CLUSTER_METHOD,
"default_k": DEFAULT_CLUSTER_K,
},
"similarity": {
"default_threshold": DEFAULT_SIMILARITY_THRESHOLD,
},
"features": {
"auto_analysis": ENABLE_AUTO_ANALYSIS,
"websocket": ENABLE_WEBSOCKET,
},
"cors": {
"origins": CORS_ORIGINS,
"credentials": CORS_CREDENTIALS,
},
"logging": {
"level": LOG_LEVEL,
}
}
# Create directories if they don't exist
RAW_DATA_DIR.mkdir(parents=True, exist_ok=True)
if __name__ == "__main__":
# Print configuration when run directly
import json
print("=" * 60)
print("RTIdeas MCP Server Configuration")
print("=" * 60)
is_valid, errors = validate_config()
if is_valid:
print("✅ Configuration is valid\n")
else:
print("❌ Configuration has errors:\n")
for error in errors:
print(f" - {error}")
print()
print("Configuration Summary:")
print(json.dumps(get_config_summary(), indent=2))