Enhance license validation in query_mcp function to raise HTTPException for missing licenses
taylorwalton committed
Aug 19, 2025 at 09:54 UTC
77fcc6ad1f34cf73f64576b9bf88b6218c79b53b
1 file changed
+14
-7
backend/app/integrations/copilot_mcp/routes/copilot_mcp.py
+14
-7
@@ -4,6 +4,7 @@ from fastapi import APIRouter
4
from fastapi import Depends
5
from fastapi import Query
6
from fastapi import Security
7
+from fastapi import HTTPException
8
from loguru import logger
9
from sqlalchemy.ext.asyncio import AsyncSession
10
@@ -213,18 +214,24 @@ async def query_mcp(request: MCPQueryRequest, session: AsyncSession = Depends(ge
214
if MCPService.is_cloud_service(request.mcp_server):
215
try:
216
await is_feature_enabled("SOCFORTRESS AI", session=session)
217
+
218
+ # Will raise HTTPException(404) if no license record exists
219
license_info = await get_license(session)
220
license_key = license_info.license_key
221
+
222
+ # If a license record exists but the key is missing, mirror get_license behavior
223
+ if not license_key:
224
+ raise HTTPException(status_code=404, detail="No license found. A license must be created first.")
225
+
226
logger.info(f"Retrieved license key for cloud service {request.mcp_server.value}")
227
+
228
+ except HTTPException as http_exc:
229
+ # Surface the HTTPException unchanged
230
+ raise http_exc
231
except Exception as e:
232
+ # Unexpected errors -> 500
233
logger.error(f"Failed to get license key for cloud service: {str(e)}")
221
- return MCPQueryResponse(
222
- message=f"License validation failed: {str(e)}",
223
- success=False,
224
- result=None,
225
- structured_result=None,
226
- execution_time=0.0,
227
- )
234
+ raise HTTPException(status_code=500, detail=f"License validation failed: {str(e)}")
235
236
# Use the modular service to execute the query
237
return await MCPService.execute_query(request, license_key=license_key)