From 7c162691dbc8c366a185b16f8e67ece4ee8e6452 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 30 Nov 2025 00:09:55 +0000 Subject: [PATCH] Fix middleware and schema cache issues This commit addresses two critical issues in the MCP server: 1. CORS Middleware Fix: - Move CORS middleware outside the auth check so it's always enabled - CORS is required for browser-based MCP clients, regardless of auth - Fixes "OPTIONS /mcp HTTP/1.1 405 Method Not Allowed" error 2. Schema Cache Improvements: - Add specific handling for corrupted JSON cache files - Automatically remove corrupted cache files and log the action - Prevents startup failures due to malformed JSON - Better error messages to help diagnose cache issues 3. Configuration Updates: - Change default SCHEMA_CACHE_DIR from /app/data/schemas to ./data/schemas - Better default for local development (Docker still uses /app/data/schemas) - Update .env.example with clearer documentation These fixes improve robustness and make local development easier. --- .env.example | 3 ++- src/geoguessr_mcp/config.py | 2 +- src/geoguessr_mcp/main.py | 24 +++++++++---------- .../monitoring/schema/schema_registry.py | 20 ++++++++++++++++ 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/.env.example b/.env.example index a6127a3..b54a6a9 100644 --- a/.env.example +++ b/.env.example @@ -54,7 +54,8 @@ MONITORING_ENABLED=true MONITORING_INTERVAL_HOURS=24 # Directory to store schema cache (persisted between restarts) -SCHEMA_CACHE_DIR=/app/data/schemas +# Default: ./data/schemas (local dev) or /app/data/schemas (Docker) +SCHEMA_CACHE_DIR=./data/schemas # ============================================================================= # Logging Configuration diff --git a/src/geoguessr_mcp/config.py b/src/geoguessr_mcp/config.py index 203185d..6e94d18 100644 --- a/src/geoguessr_mcp/config.py +++ b/src/geoguessr_mcp/config.py @@ -30,7 +30,7 @@ class Settings: default_factory=lambda: int(os.getenv("MONITORING_INTERVAL_HOURS", "24")) ) SCHEMA_CACHE_DIR: str = field( - default_factory=lambda: os.getenv("SCHEMA_CACHE_DIR", "/app/data/schemas") + default_factory=lambda: os.getenv("SCHEMA_CACHE_DIR", "./data/schemas") ) # Authentication Configuration diff --git a/src/geoguessr_mcp/main.py b/src/geoguessr_mcp/main.py index 5a9f25f..0172c04 100644 --- a/src/geoguessr_mcp/main.py +++ b/src/geoguessr_mcp/main.py @@ -62,21 +62,21 @@ def main(): # Register all tools register_all_tools(mcp) + # Get the ASGI application + mcp_app = mcp.streamable_http_app() + + # Always add CORS middleware for browser compatibility + mcp_app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], + ) + # Setup authentication middleware if enabled if settings.MCP_AUTH_ENABLED: logger.info("Setting up authentication middleware") - - # Récupérez l'application ASGI via streamable_http_app - mcp_app = mcp.streamable_http_app() - - # Ajoutez les middlewares - mcp_app.add_middleware( - CORSMiddleware, - allow_origins=["*"], - allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], - ) mcp_app.add_middleware(AuthenticationMiddleware) logger.info( diff --git a/src/geoguessr_mcp/monitoring/schema/schema_registry.py b/src/geoguessr_mcp/monitoring/schema/schema_registry.py index 43bb6c4..2f08269 100644 --- a/src/geoguessr_mcp/monitoring/schema/schema_registry.py +++ b/src/geoguessr_mcp/monitoring/schema/schema_registry.py @@ -71,6 +71,16 @@ class SchemaRegistry: for endpoint, schema_data in data.items(): self.schemas[endpoint] = EndpointSchema.from_dict(schema_data) logger.info(f"Loaded {len(self.schemas)} cached schemas") + except json.JSONDecodeError as e: + logger.warning( + f"Failed to load cached schemas due to corrupted JSON: {e}. " + f"Removing corrupted cache file." + ) + try: + schema_file.unlink() + logger.info(f"Removed corrupted schema cache file: {schema_file}") + except Exception as rm_error: + logger.error(f"Failed to remove corrupted cache file: {rm_error}") except Exception as e: logger.warning(f"Failed to load cached schemas: {e}") @@ -83,6 +93,16 @@ class SchemaRegistry: self.schema_history[endpoint] = [ EndpointSchema.from_dict(h) for h in history ] + except json.JSONDecodeError as e: + logger.warning( + f"Failed to load schema history due to corrupted JSON: {e}. " + f"Removing corrupted history file." + ) + try: + history_file.unlink() + logger.info(f"Removed corrupted schema history file: {history_file}") + except Exception as rm_error: + logger.error(f"Failed to remove corrupted history file: {rm_error}") except Exception as e: logger.warning(f"Failed to load schema history: {e}")