feat(mcp): 增强MCP服务器异常处理和错误描述

- 添加_iter_leaf_exceptions函数用于处理嵌套异常组
- 实现_describe_mcp_exception函数提供详细的MCP服务器错误信息
- 改进connect_mcp_servers中的错误处理,使用更精确的错误描述
- 在日志记录中包含更具体的错误详情

feat(outlook): 优化Outlook集成异常处理

- 添加_iter_leaf_exceptions函数用于异常处理
- 创建_coerce_outlook_mcp_exception函数统一异常转换
- 改进_call_outlook_mcp_tool中的异常捕获和处理
- 对认证令牌获取和MCP调用添加专门的超时和HTTP错误处理

feat(web): 改进Web会话定时任务结果记录

- 实现_record_cron_result_for_web_session函数
- 为Web模式下的独立定时任务执行结果提供持久化存储
- 支持将定时任务响应消息添加到目标会话中
- 确保前端可以显示定时任务执行结果
This commit is contained in:
2026-03-20 17:56:45 +08:00
parent bfa77204bf
commit 5e85129869
3 changed files with 140 additions and 23 deletions

View File

@ -31,6 +31,38 @@ class OutlookIntegrationError(RuntimeError):
"""Raised when the Outlook integration backend is unavailable or misconfigured."""
def _iter_leaf_exceptions(exc: BaseException) -> list[BaseException]:
if isinstance(exc, BaseExceptionGroup):
leaves: list[BaseException] = []
for sub_exc in exc.exceptions:
leaves.extend(_iter_leaf_exceptions(sub_exc))
return leaves
return [exc]
def _coerce_outlook_mcp_exception(exc: BaseException, *, url: str) -> OutlookIntegrationError:
if isinstance(exc, OutlookIntegrationError):
return exc
leaves = _iter_leaf_exceptions(exc)
for leaf in leaves:
if isinstance(leaf, httpx.TimeoutException):
return OutlookIntegrationError(f"Outlook MCP 请求超时:{url}")
if isinstance(leaf, httpx.ConnectError):
return OutlookIntegrationError(f"Outlook MCP 无法连接:{url}")
if isinstance(leaf, httpx.HTTPStatusError):
return OutlookIntegrationError(f"Outlook MCP 返回 HTTP {leaf.response.status_code}{url}")
if isinstance(leaf, httpx.HTTPError):
detail = str(leaf).strip() or leaf.__class__.__name__
return OutlookIntegrationError(f"Outlook MCP 网络错误:{detail}")
detail_source = leaves[0] if leaves else exc
detail = str(detail_source).strip() or detail_source.__class__.__name__
return OutlookIntegrationError(
f"Outlook MCP 调用失败:{detail_source.__class__.__name__}: {detail}"
)
@dataclass(frozen=True)
class OutlookDefaults:
"""Default values exposed to the web setup form."""
@ -187,32 +219,43 @@ async def _call_outlook_mcp_tool(
from mcp import ClientSession, types
from mcp.client.streamable_http import streamable_http_client
url = _outlook_mcp_url(config)
backend_id = _require_backend_identity(config)
client = _authz_client(config)
token_response = await client.issue_token(
client_id=config.backend_identity.client_id,
client_secret=config.backend_identity.client_secret,
audience=f"mcp:{OUTLOOK_SERVER_ID}",
scopes=scopes or ["list_tools", f"tool:{tool_name}"],
)
try:
token_response = await client.issue_token(
client_id=config.backend_identity.client_id,
client_secret=config.backend_identity.client_secret,
audience=f"mcp:{OUTLOOK_SERVER_ID}",
scopes=scopes or ["list_tools", f"tool:{tool_name}"],
)
except httpx.TimeoutException as exc:
raise OutlookIntegrationError("AuthZ token 请求超时。") from exc
except httpx.HTTPError as exc:
detail = str(exc).strip() or exc.__class__.__name__
raise OutlookIntegrationError(f"AuthZ token 获取失败:{detail}") from exc
access_token = str(token_response.get("access_token") or "").strip()
if not access_token:
raise OutlookIntegrationError("Failed to obtain an Outlook MCP access token.")
async with AsyncExitStack() as stack:
http_client = await stack.enter_async_context(
httpx.AsyncClient(
headers={"Authorization": f"Bearer {access_token}"},
follow_redirects=True,
trust_env=False,
try:
async with AsyncExitStack() as stack:
http_client = await stack.enter_async_context(
httpx.AsyncClient(
headers={"Authorization": f"Bearer {access_token}"},
follow_redirects=True,
trust_env=False,
)
)
)
read, write, _ = await stack.enter_async_context(
streamable_http_client(_outlook_mcp_url(config), http_client=http_client)
)
session = await stack.enter_async_context(ClientSession(read, write))
await session.initialize()
result = await session.call_tool(tool_name, arguments=arguments)
read, write, _ = await stack.enter_async_context(
streamable_http_client(url, http_client=http_client)
)
session = await stack.enter_async_context(ClientSession(read, write))
await session.initialize()
result = await session.call_tool(tool_name, arguments=arguments)
except Exception as exc: # noqa: BLE001
raise _coerce_outlook_mcp_exception(exc, url=url) from exc
parts: list[str] = []
for block in result.content: