chore(mcp): avoid unchecked search result casts

This commit is contained in:
panjianxing 2026-10-25 19:54:24 +08:00
parent f223a4a8c6
commit a047ce1315

View file

@ -11,7 +11,7 @@ from dataclasses import dataclass, replace
from datetime import datetime
from functools import partial, reduce
from types import EllipsisType
from typing import Any, Final, NoReturn, TypeAlias, cast, overload
from typing import Any, Final, NoReturn, TypeAlias, overload
import anyio
from fastapi import HTTPException
@ -469,31 +469,22 @@ async def _dispatch_mcp_tool_search( # pyright: ignore[reportUnknownParameterTy
) -> CallToolResult:
from litellm.proxy._experimental.mcp_server.tool_search import (
MCP_TOOL_SEARCH_TOOL_NAME,
handle_mcp_tool_search, # pyright: ignore[reportUnknownVariableType] # MCP SDK result type is unresolved
mcp_tool_search_settings,
)
from litellm.proxy._experimental.mcp_server.tool_search import (
handle_mcp_tool_search as untyped_handle_mcp_tool_search, # pyright: ignore[reportUnknownVariableType] # MCP SDK return type is unresolved
)
handle_mcp_tool_search: Final[Callable[..., Awaitable[CallToolResult]]] = cast( # pyright: ignore[reportUnknownVariableType] # typed view of the lazy import # cast-ok: MCP SDK return type is unresolved
Callable[..., Awaitable[CallToolResult]],
untyped_handle_mcp_tool_search,
)
settings: Final[MCPToolSearchSettings | ValidationError] = mcp_tool_search_settings()
if not isinstance(settings, ValidationError) and settings.embedding_model is not None:
return cast( # pyright: ignore[reportUnknownVariableType] # MCP SDK result type is unresolved # cast-ok: narrow the lazy handler's unresolved return
CallToolResult,
await handle_mcp_tool_search(
query=query,
top_k=top_k,
user_api_key_dict=user_api_key_auth,
client_ip=client_ip,
mcp_servers=mcp_servers,
mcp_auth_header=mcp_auth_header,
mcp_server_auth_headers=mcp_server_auth_headers,
oauth2_headers=oauth2_headers,
raw_headers=raw_headers,
),
return await handle_mcp_tool_search( # pyright: ignore[reportUnknownVariableType] # MCP SDK result type is unresolved
query=query,
top_k=top_k,
user_api_key_dict=user_api_key_auth,
client_ip=client_ip,
mcp_servers=mcp_servers,
mcp_auth_header=mcp_auth_header,
mcp_server_auth_headers=mcp_server_auth_headers,
oauth2_headers=oauth2_headers,
raw_headers=raw_headers,
)
start_time: Final = datetime.now() # noqa: DTZ005 # logging timestamps are naive
@ -519,19 +510,16 @@ async def _dispatch_mcp_tool_search( # pyright: ignore[reportUnknownParameterTy
)
raise
try:
result: Final[CallToolResult] = cast( # pyright: ignore[reportUnknownVariableType] # MCP SDK result type is unresolved # cast-ok: narrow the lazy handler's unresolved return
CallToolResult,
await handle_mcp_tool_search(
query=query,
top_k=top_k,
user_api_key_dict=user_api_key_auth,
client_ip=client_ip,
mcp_servers=mcp_servers,
mcp_auth_header=mcp_auth_header,
mcp_server_auth_headers=mcp_server_auth_headers,
oauth2_headers=oauth2_headers,
raw_headers=raw_headers,
),
result: Final[CallToolResult] = await handle_mcp_tool_search( # pyright: ignore[reportUnknownVariableType] # MCP SDK result type is unresolved
query=query,
top_k=top_k,
user_api_key_dict=user_api_key_auth,
client_ip=client_ip,
mcp_servers=mcp_servers,
mcp_auth_header=mcp_auth_header,
mcp_server_auth_headers=mcp_server_auth_headers,
oauth2_headers=oauth2_headers,
raw_headers=raw_headers,
)
except Exception as exc:
await fire_mcp_tool_call_failure_logging(