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.
This commit is contained in:
Claude 2025-11-30 00:09:55 +00:00
parent de023b66c7
commit 7c162691db
No known key found for this signature in database
4 changed files with 35 additions and 14 deletions

View file

@ -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}")