Merge pull request #578 from szmadd/fix/mcp-test-failure-handling
fix: 修复MCP服务器测试失败时返回成功的问题
This commit is contained in:
commit
21a9c9f3eb
@ -231,13 +231,14 @@ async def test_mcp_server(
|
|||||||
await get_server_or_404(db, name)
|
await get_server_or_404(db, name)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tools = await get_all_mcp_tools(name)
|
tools = await get_all_mcp_tools(name, raise_on_error=True)
|
||||||
return {
|
return {
|
||||||
"success": True,
|
"success": True,
|
||||||
"message": f"连接成功,共发现 {len(tools)} 个工具",
|
"message": f"连接成功,共发现 {len(tools)} 个工具",
|
||||||
"tool_count": len(tools),
|
"tool_count": len(tools),
|
||||||
}
|
}
|
||||||
except Exception as test_error:
|
except Exception as test_error:
|
||||||
|
logger.warning(f"MCP server test failed for '{name}': {test_error}")
|
||||||
raise HTTPException(status_code=500, detail=f"连接失败: {str(test_error)}")
|
raise HTTPException(status_code=500, detail=f"连接失败: {str(test_error)}")
|
||||||
except HTTPException:
|
except HTTPException:
|
||||||
raise
|
raise
|
||||||
|
|||||||
@ -210,6 +210,7 @@ async def get_mcp_tools(
|
|||||||
disabled_tools: list[str] = None,
|
disabled_tools: list[str] = None,
|
||||||
cache: bool = True,
|
cache: bool = True,
|
||||||
force_refresh: bool = False,
|
force_refresh: bool = False,
|
||||||
|
raise_on_error: bool = False,
|
||||||
) -> list[Callable[..., Any]]:
|
) -> list[Callable[..., Any]]:
|
||||||
"""Get MCP tools for a specific server.
|
"""Get MCP tools for a specific server.
|
||||||
|
|
||||||
@ -288,11 +289,15 @@ async def get_mcp_tools(
|
|||||||
|
|
||||||
except AssertionError as e:
|
except AssertionError as e:
|
||||||
logger.warning(f"[assert] Failed to load tools from MCP server '{server_name}': {e}")
|
logger.warning(f"[assert] Failed to load tools from MCP server '{server_name}': {e}")
|
||||||
|
if raise_on_error:
|
||||||
|
raise
|
||||||
return []
|
return []
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(
|
logger.error(
|
||||||
f"Failed to load tools from MCP server '{server_name}': {e}, traceback: {traceback.format_exc()}"
|
f"Failed to load tools from MCP server '{server_name}': {e}, traceback: {traceback.format_exc()}"
|
||||||
)
|
)
|
||||||
|
if raise_on_error:
|
||||||
|
raise
|
||||||
return []
|
return []
|
||||||
|
|
||||||
# 3. Filtering (Apply to Return Value Only)
|
# 3. Filtering (Apply to Return Value Only)
|
||||||
@ -602,7 +607,7 @@ async def get_servers_config(names: list[str]) -> dict[str, dict[str, Any]]:
|
|||||||
return {name: MCP_SERVERS[name] for name in names if name in MCP_SERVERS}
|
return {name: MCP_SERVERS[name] for name in names if name in MCP_SERVERS}
|
||||||
|
|
||||||
|
|
||||||
async def get_all_mcp_tools(server_name: str) -> list:
|
async def get_all_mcp_tools(server_name: str, raise_on_error: bool = False) -> list:
|
||||||
"""Get all tools of an MCP server (no filtering).
|
"""Get all tools of an MCP server (no filtering).
|
||||||
|
|
||||||
For management UI to display tool list, supports viewing all tools and their enabled status.
|
For management UI to display tool list, supports viewing all tools and their enabled status.
|
||||||
@ -610,6 +615,7 @@ async def get_all_mcp_tools(server_name: str) -> list:
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
server_name: Server name
|
server_name: Server name
|
||||||
|
raise_on_error: Whether to raise an exception on error instead of returning empty list
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
List of all tools (unfiltered)
|
List of all tools (unfiltered)
|
||||||
@ -617,7 +623,15 @@ async def get_all_mcp_tools(server_name: str) -> list:
|
|||||||
config = MCP_SERVERS.get(server_name)
|
config = MCP_SERVERS.get(server_name)
|
||||||
if not config:
|
if not config:
|
||||||
logger.warning(f"MCP server '{server_name}' not found in cache")
|
logger.warning(f"MCP server '{server_name}' not found in cache")
|
||||||
|
if raise_on_error:
|
||||||
|
raise ValueError(f"MCP server '{server_name}' not found in cache")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
# Get all tools (no filtering, force refresh, no cache update)
|
# Get all tools (no filtering, force refresh, no cache update)
|
||||||
return await get_mcp_tools(server_name, disabled_tools=[], cache=False, force_refresh=True)
|
return await get_mcp_tools(
|
||||||
|
server_name,
|
||||||
|
disabled_tools=[],
|
||||||
|
cache=False,
|
||||||
|
force_refresh=True,
|
||||||
|
raise_on_error=raise_on_error,
|
||||||
|
)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user