mirror of
https://github.com/langgenius/dify.git
synced 2026-04-05 06:19:25 +08:00
fix: remove redundant cast in MCP base session (#34461)
Signed-off-by: majiayu000 <1835304752@qq.com>
This commit is contained in:
@@ -4,7 +4,7 @@ from collections.abc import Callable
|
|||||||
from concurrent.futures import Future, ThreadPoolExecutor, TimeoutError
|
from concurrent.futures import Future, ThreadPoolExecutor, TimeoutError
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from types import TracebackType
|
from types import TracebackType
|
||||||
from typing import Any, Self, cast
|
from typing import Any, Self
|
||||||
|
|
||||||
from httpx import HTTPStatusError
|
from httpx import HTTPStatusError
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@@ -338,12 +338,11 @@ class BaseSession[
|
|||||||
validated_request = self._receive_request_type.model_validate(
|
validated_request = self._receive_request_type.model_validate(
|
||||||
message.message.root.model_dump(by_alias=True, mode="json", exclude_none=True)
|
message.message.root.model_dump(by_alias=True, mode="json", exclude_none=True)
|
||||||
)
|
)
|
||||||
validated_request = cast(ReceiveRequestT, validated_request)
|
|
||||||
|
|
||||||
responder = RequestResponder[ReceiveRequestT, SendResultT](
|
responder = RequestResponder[ReceiveRequestT, SendResultT](
|
||||||
request_id=message.message.root.id,
|
request_id=message.message.root.id,
|
||||||
request_meta=validated_request.root.params.meta if validated_request.root.params else None,
|
request_meta=validated_request.root.params.meta if validated_request.root.params else None,
|
||||||
request=validated_request,
|
request=validated_request, # type: ignore[arg-type] # mypy can't narrow constrained TypeVar from model_validate
|
||||||
session=self,
|
session=self,
|
||||||
on_complete=lambda r: self._in_flight.pop(r.request_id, None),
|
on_complete=lambda r: self._in_flight.pop(r.request_id, None),
|
||||||
)
|
)
|
||||||
@@ -359,15 +358,14 @@ class BaseSession[
|
|||||||
notification = self._receive_notification_type.model_validate(
|
notification = self._receive_notification_type.model_validate(
|
||||||
message.message.root.model_dump(by_alias=True, mode="json", exclude_none=True)
|
message.message.root.model_dump(by_alias=True, mode="json", exclude_none=True)
|
||||||
)
|
)
|
||||||
notification = cast(ReceiveNotificationT, notification)
|
|
||||||
# Handle cancellation notifications
|
# Handle cancellation notifications
|
||||||
if isinstance(notification.root, CancelledNotification):
|
if isinstance(notification.root, CancelledNotification):
|
||||||
cancelled_id = notification.root.params.requestId
|
cancelled_id = notification.root.params.requestId
|
||||||
if cancelled_id in self._in_flight:
|
if cancelled_id in self._in_flight:
|
||||||
self._in_flight[cancelled_id].cancel()
|
self._in_flight[cancelled_id].cancel()
|
||||||
else:
|
else:
|
||||||
self._received_notification(notification)
|
self._received_notification(notification) # type: ignore[arg-type]
|
||||||
self._handle_incoming(notification)
|
self._handle_incoming(notification) # type: ignore[arg-type]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# For other validation errors, log and continue
|
# For other validation errors, log and continue
|
||||||
logger.warning("Failed to validate notification: %s. Message was: %s", e, message.message.root)
|
logger.warning("Failed to validate notification: %s. Message was: %s", e, message.message.root)
|
||||||
|
|||||||
Reference in New Issue
Block a user