55from fastapi import APIRouter , Header , HTTPException
66from fastapi .responses import JSONResponse
77
8- from consts .exceptions import MCPConnectionError
9- from consts .model import ToolInstanceInfoRequest , ToolInstanceSearchRequest
8+ from consts .exceptions import MCPConnectionError , TimeoutException , NotFoundException
9+ from consts .model import ToolInstanceInfoRequest , ToolInstanceSearchRequest , ToolValidateRequest
1010from services .tool_configuration_service import (
1111 search_tool_info_impl ,
1212 update_tool_info_impl ,
1313 update_tool_list ,
1414 list_all_tools ,
1515 load_last_tool_config_impl ,
16+ validate_tool_impl ,
1617)
1718from utils .auth_utils import get_current_user_id
1819
@@ -81,6 +82,7 @@ async def scan_and_update_tool(
8182 raise HTTPException (
8283 status_code = HTTPStatus .INTERNAL_SERVER_ERROR , detail = "Failed to update tool" )
8384
85+
8486@router .get ("/load_config/{tool_id}" )
8587async def load_last_tool_config (tool_id : int , authorization : Optional [str ] = Header (None )):
8688 try :
@@ -97,4 +99,38 @@ async def load_last_tool_config(tool_id: int, authorization: Optional[str] = Hea
9799 except Exception as e :
98100 logger .error (f"Failed to load tool config: { e } " )
99101 raise HTTPException (
100- status_code = HTTPStatus .INTERNAL_SERVER_ERROR , detail = "Failed to load tool config" )
102+ status_code = HTTPStatus .INTERNAL_SERVER_ERROR , detail = "Failed to load tool config" )
103+
104+
105+ @router .post ("/validate" )
106+ async def validate_tool (
107+ request : ToolValidateRequest ,
108+ authorization : Optional [str ] = Header (None )
109+ ):
110+ """Validate specific tool based on source type"""
111+ try :
112+ _ , tenant_id = get_current_user_id (authorization )
113+ result = await validate_tool_impl (request , tenant_id )
114+
115+ return JSONResponse (
116+ status_code = HTTPStatus .OK ,
117+ content = result
118+ )
119+ except MCPConnectionError as e :
120+ logger .error (f"MCP connection failed: { e } " )
121+ raise HTTPException (
122+ status_code = HTTPStatus .SERVICE_UNAVAILABLE ,
123+ detail = str (e )
124+ )
125+ except NotFoundException as e :
126+ logger .error (f"Tool not found: { e } " )
127+ raise HTTPException (
128+ status_code = HTTPStatus .NOT_FOUND ,
129+ detail = str (e )
130+ )
131+ except Exception as e :
132+ logger .error (f"Failed to validate tool: { e } " )
133+ raise HTTPException (
134+ status_code = HTTPStatus .INTERNAL_SERVER_ERROR ,
135+ detail = f"Failed to validate tool: { str (e )} "
136+ )
0 commit comments