Fix middleware schema caching error #7
4 changed files with 35 additions and 14 deletions
|
|
@ -54,7 +54,8 @@ MONITORING_ENABLED=true
|
||||||
MONITORING_INTERVAL_HOURS=24
|
MONITORING_INTERVAL_HOURS=24
|
||||||
|
|
||||||
# Directory to store schema cache (persisted between restarts)
|
# 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
|
# Logging Configuration
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ class Settings:
|
||||||
default_factory=lambda: int(os.getenv("MONITORING_INTERVAL_HOURS", "24"))
|
default_factory=lambda: int(os.getenv("MONITORING_INTERVAL_HOURS", "24"))
|
||||||
)
|
)
|
||||||
SCHEMA_CACHE_DIR: str = field(
|
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
|
# Authentication Configuration
|
||||||
|
|
|
||||||
|
|
@ -62,21 +62,21 @@ def main():
|
||||||
# Register all tools
|
# Register all tools
|
||||||
register_all_tools(mcp)
|
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
|
# Setup authentication middleware if enabled
|
||||||
if settings.MCP_AUTH_ENABLED:
|
if settings.MCP_AUTH_ENABLED:
|
||||||
logger.info("Setting up authentication middleware")
|
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)
|
mcp_app.add_middleware(AuthenticationMiddleware)
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,16 @@ class SchemaRegistry:
|
||||||
for endpoint, schema_data in data.items():
|
for endpoint, schema_data in data.items():
|
||||||
self.schemas[endpoint] = EndpointSchema.from_dict(schema_data)
|
self.schemas[endpoint] = EndpointSchema.from_dict(schema_data)
|
||||||
logger.info(f"Loaded {len(self.schemas)} cached schemas")
|
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:
|
except Exception as e:
|
||||||
logger.warning(f"Failed to load cached schemas: {e}")
|
logger.warning(f"Failed to load cached schemas: {e}")
|
||||||
|
|
||||||
|
|
@ -83,6 +93,16 @@ class SchemaRegistry:
|
||||||
self.schema_history[endpoint] = [
|
self.schema_history[endpoint] = [
|
||||||
EndpointSchema.from_dict(h) for h in history
|
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:
|
except Exception as e:
|
||||||
logger.warning(f"Failed to load schema history: {e}")
|
logger.warning(f"Failed to load schema history: {e}")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue