Skip to content

Commit dfed800

Browse files
author
jiangpeiling
committed
✨ MCP Tool test run #541
1 parent a320e92 commit dfed800

File tree

11 files changed

+902
-92
lines changed

11 files changed

+902
-92
lines changed

backend/apps/tool_config_app.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
from fastapi import APIRouter, Header, HTTPException
66
from 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
1010
from 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_remote_mcp_tool,
1617
)
1718
from 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}")
8587
async def load_last_tool_config(tool_id: int, authorization: Optional[str] = Header(None)):
8688
try:
@@ -97,4 +99,43 @@ 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_remote_mcp_tool(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 TimeoutException as e:
132+
raise HTTPException(
133+
status_code=HTTPStatus.REQUEST_TIMEOUT,
134+
detail=str(e)
135+
)
136+
except Exception as e:
137+
logger.error(f"Failed to validate tool: {e}")
138+
raise HTTPException(
139+
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
140+
detail=f"Failed to validate tool: {str(e)}"
141+
)

backend/consts/exceptions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,9 @@ class TTSConnectionException(Exception):
8585

8686
class VoiceConfigException(Exception):
8787
"""Raised when voice configuration is invalid."""
88+
pass
89+
90+
91+
class ToolExecutionException(Exception):
92+
"""Raised when mcp tool execution failed."""
8893
pass

backend/consts/model.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,14 @@ class TTSResponse(BaseModel):
340340
"""Response model for TTS conversion"""
341341
status: str = Field(..., description="Status of the TTS conversion")
342342
message: Optional[str] = Field(None, description="Additional message")
343+
344+
345+
class ToolValidateRequest(BaseModel):
346+
"""Request model for tool validation"""
347+
name: str = Field(..., description="Tool name to validate")
348+
source: str = Field(..., description="Tool source (local, mcp, langchain)")
349+
usage: Optional[str] = Field(None, description="Tool usage information")
350+
inputs: Optional[Dict[str, Any]] = Field(
351+
None, description="Tool inputs")
352+
params: Optional[Dict[str, Any]] = Field(
353+
None, description="Tool configuration parameters")

0 commit comments

Comments
 (0)