Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

218 KB
Raw Blame
import asyncio
import base64
import importlib
import json
import os
import sys
from collections.abc import AsyncIterator, Callable
from contextlib import AbstractAsyncContextManager, asynccontextmanager
from pathlib import Path
from types import ModuleType
from typing import Final
from unittest.mock import AsyncMock, MagicMock, Mock, patch

import anyio
import httpx2
import pytest
from mcp import ClientSession, MCPError
from mcp.client.session import ClientRequestContext, ElicitationFnT
from mcp.client.streamable_http import streamable_http_client
from mcp.server.mcpserver import Context
from mcp.server.mcpserver import MCPServer as UpstreamServer
from mcp.server.transport_security import TransportSecuritySettings
from mcp.shared.message import SessionMessage
from mcp.types import (
    CONNECTION_CLOSED,
    REQUEST_TIMEOUT,
    CallToolRequestParams,
    CallToolResult,
    DiscoverResult,
    ElicitRequestParams,
    ElicitResult,
    ErrorData,
    Implementation,
    InitializeResult,
    JSONRPCError,
    JSONRPCMessage,
    JSONRPCNotification,
    JSONRPCRequest,
    JSONRPCResponse,
    LoggingMessageNotificationParams,
    ServerCapabilities,
)
from mcp_types.version import LATEST_HANDSHAKE_VERSION
from pydantic import BaseModel, ConfigDict, TypeAdapter, ValidationError
from starlette.applications import Starlette
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from starlette.requests import Request
from starlette.responses import Response
from starlette.types import ASGIApp, Message, Receive, Scope, Send

# Add the parent directory to the path so we can import litellm
import litellm.experimental_mcp_client.client as mcp_client_module
from litellm.experimental_mcp_client.client import (
    MCPClient,
    MCPSigV4Auth,
    PersistentMCPSession,
    UpstreamSessionClosedError,
    _first_non_cancelled_cause,
    _TransportContext,
    _TransportStreams,
    as_mcp_read_timeout,
    strip_auth_scheme,
)
from litellm.proxy._experimental.mcp_server.faults.list_outcomes import (
    ServerListFault,
    classify_list_exception,
    list_fault_http_status,
    upstream_rate_limit,
)
from litellm.proxy._experimental.mcp_server.mcp_server_manager import (
    _format_byok_openapi_auth_header,
)
from litellm.proxy._experimental.mcp_server.outbound_credentials.httpx_auth import StaticHeaderAuth
from litellm.types.mcp import MCPAuth, MCPStdioConfig, MCPTransport
from litellm.types.mcp_server.mcp_server_manager import MCPServer
from tests._support.clock_loops import AutojumpClockLoop, ManualClockLoop

_JSONRPC_MESSAGE_ADAPTER: Final = TypeAdapter(JSONRPCMessage)


@pytest.mark.asyncio
async def test_tool_continuation_reaches_modern_upstream() -> None:
    from mcp.types import ElicitResult, TextContent

    def respond(request: httpx2.Request) -> httpx2.Response:
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        assert isinstance(payload, JSONRPCRequest)
        if payload.method == "server/discover":
            return httpx2.Response(
                200,
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "result": {
                        "supportedVersions": ["2026-07-28"],
                        "capabilities": {"tools": {}},
                        "resultType": "complete",
                        "cacheScope": "private",
                        "ttlMs": 0,
                    },
                },
            )
        if payload.method == "tools/list":
            return httpx2.Response(
                200,
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "result": {
                        "tools": [{"name": "confirm", "inputSchema": {"type": "object"}}],
                        "resultType": "complete",
                        "cacheScope": "private",
                        "ttlMs": 0,
                    },
                },
            )
        assert payload.method == "tools/call"
        assert payload.params is not None
        assert payload.params.get("requestState") == "opaque-upstream-state"
        assert payload.params.get("inputResponses") == {"confirmation": {"action": "accept"}}
        return httpx2.Response(
            200,
            json={
                "jsonrpc": "2.0",
                "id": payload.id,
                "result": {
                    "resultType": "complete",
                    "content": [{"type": "text", "text": "confirmed"}],
                    "isError": False,
                },
            },
        )

    client: Final = _MockTransportClient(respond, server_url="https://example.com/mcp", protocol_version="2026-07-28")
    result: Final = await client.call_tool(
        CallToolRequestParams(
            name="confirm",
            request_state="opaque-upstream-state",
            input_responses={"confirmation": ElicitResult(action="accept")},
        ),
        raise_on_error=True,
    )
    assert isinstance(result, CallToolResult)
    assert result.content == [TextContent(type="text", text="confirmed")]
    assert result.is_error is False


@pytest.mark.asyncio
@pytest.mark.parametrize("modern_caller", [False, True])
@pytest.mark.parametrize(
    "elicitation_mode,sampling", [("form", False), ("form", True), ("url", False), ("url", True), ("none", True)]
)
async def test_modern_input_request_uses_existing_elicitation_callback(
    modern_caller: bool, sampling: bool, elicitation_mode: str
) -> None:
    from queue import SimpleQueue

    from mcp.types import (
        CreateMessageRequestParams,
        CreateMessageResult,
        ElicitRequestParams,
        ElicitResult,
        TextContent,
    )

    from litellm.proxy._experimental.mcp_server.interactions import BoundInputRequiredResult

    observed: Final[SimpleQueue[str]] = SimpleQueue()
    sampled: Final = CreateMessageResult(
        role="assistant", content=TextContent(type="text", text="sampled"), model="test"
    )
    expected_responses: Final = {
        **({"consent": {"action": "accept"}} if elicitation_mode != "none" else {}),
        **({"sample": sampled.model_dump(by_alias=True, exclude_none=True)} if sampling else {}),
    }

    async def elicit(context: object, params: ElicitRequestParams) -> ElicitResult:
        if params.mode == "url":
            assert params.elicitation_id, "Legacy URL input must carry its required elicitation ID"
        observed.put(params.message)
        return ElicitResult(action="accept")

    async def sample(context: object, params: CreateMessageRequestParams) -> CreateMessageResult:
        assert params.max_tokens == 10
        observed.put("sample")
        return sampled

    def respond(request: httpx2.Request) -> httpx2.Response:
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        assert isinstance(payload, JSONRPCRequest)
        if payload.method == "server/discover":
            result = {
                "supportedVersions": ["2026-07-28"],
                "capabilities": {"tools": {}},
                "resultType": "complete",
                "cacheScope": "private",
                "ttlMs": 0,
            }
        elif payload.method == "tools/list":
            result = {
                "tools": [{"name": "confirm", "inputSchema": {"type": "object"}}],
                "resultType": "complete",
                "cacheScope": "private",
                "ttlMs": 0,
            }
        elif not (payload.params or {}).get("requestState"):
            result = {
                "resultType": "input_required",
                "requestState": "pending",
                "inputRequests": {
                    **(
                        {
                            "consent": {
                                "method": "elicitation/create",
                                "params": {
                                    "message": "Confirm operation",
                                    **(
                                        {"mode": "form", "requestedSchema": {"type": "object", "properties": {}}}
                                        if elicitation_mode == "form"
                                        else {"mode": "url", "url": "https://example.com/confirm"}
                                    ),
                                },
                            }
                        }
                        if elicitation_mode != "none"
                        else {}
                    ),
                    **(
                        {"sample": {"method": "sampling/createMessage", "params": {"messages": [], "maxTokens": 10}}}
                        if sampling
                        else {}
                    ),
                },
            }
        else:
            assert (payload.params or {}).get("requestState") == "pending"
            assert (payload.params or {}).get("inputResponses") == expected_responses
            result = {"resultType": "complete", "content": [{"type": "text", "text": "confirmed"}], "isError": False}
        return httpx2.Response(200, json={"jsonrpc": "2.0", "id": payload.id, "result": result})

    client: Final = _MockTransportClient(
        respond,
        server_url="https://example.com/mcp",
        protocol_version="2026-07-28",
        elicitation_callback=elicit,
        sampling_callback=sample,
    )
    result: Final = await client.call_tool(
        CallToolRequestParams(name="confirm", arguments={}), raise_on_error=True, allow_input_required=modern_caller
    )
    if modern_caller and elicitation_mode != "none":
        assert isinstance(result, BoundInputRequiredResult)
        assert tuple(result.input_requests or {}) == ("consent",)
        assert result.gateway_responses == ({"sample": sampled} if sampling else None)
        resumed: Final = await client.call_tool(
            CallToolRequestParams(
                name="confirm",
                arguments={},
                request_state=result.request_state,
                input_responses={"consent": ElicitResult(action="accept"), **(result.gateway_responses or {})},
            ),
            raise_on_error=True,
            allow_input_required=True,
        )
        assert isinstance(resumed, CallToolResult)
        assert resumed.content == [TextContent(type="text", text="confirmed")]
    else:
        assert isinstance(result, CallToolResult)
        assert result.content == [TextContent(type="text", text="confirmed")]
    assert sorted(observed.get_nowait() for _ in range(observed.qsize())) == sorted(
        ([] if modern_caller or elicitation_mode == "none" else ["Confirm operation"])
        + (["sample"] if sampling else [])
    )


def _initialized(instructions: str | None = None) -> InitializeResult:
    return InitializeResult(
        protocol_version=LATEST_HANDSHAKE_VERSION,
        capabilities=ServerCapabilities(),
        server_info=Implementation(name="test", version="1"),
        instructions=instructions,
    )


class _MockTransportClient(MCPClient):
    """An MCPClient whose streamable-HTTP transport runs on an httpx2 MockTransport."""

    def __init__(
        self,
        respond,
        *,
        http_transport: httpx2.AsyncBaseTransport | None = None,
        transport_context: Callable[[str, httpx2.AsyncClient], _TransportContext] | None = None,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self._respond = respond
        self._http_transport = http_transport
        self._transport_context = transport_context

    def _create_transport_context(self) -> tuple[_TransportContext, httpx2.AsyncClient]:
        transport: Final = self._http_transport or httpx2.MockTransport(self._respond)
        http_client: Final = self._create_httpx_client_factory(transport=transport)(
            headers=self._get_auth_headers(), timeout=httpx2.Timeout(self.timeout)
        )
        if self._transport_context is not None:
            return self._transport_context(self.server_url, http_client), http_client
        return streamable_http_client(self.server_url, http_client=http_client), http_client


def _post_status_responder(failure_method: str, status_code: int) -> Callable[[httpx2.Request], httpx2.Response]:
    def respond(request: httpx2.Request) -> httpx2.Response:
        if request.method != "POST":
            return httpx2.Response(200)
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        assert isinstance(payload, (JSONRPCRequest, JSONRPCNotification))
        if payload.method == "initialize":
            assert isinstance(payload, JSONRPCRequest)
            return httpx2.Response(
                200,
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "result": {
                        "protocolVersion": LATEST_HANDSHAKE_VERSION,
                        "capabilities": {"tools": {}},
                        "serverInfo": {"name": "status", "version": "1"},
                    },
                },
            )
        if payload.method == failure_method:
            return httpx2.Response(status_code, headers={"Retry-After": "7"})
        if payload.method == "tools/list":
            assert isinstance(payload, JSONRPCRequest)
            return httpx2.Response(200, json={"jsonrpc": "2.0", "id": payload.id, "result": {"tools": []}})
        return httpx2.Response(202)

    return respond


class _FakeExceptionGroup(Exception):
    """Duck-typed stand-in for an anyio/builtin ExceptionGroup.

    The production unwrapper reads ``.exceptions`` rather than depending on the
    builtin ``ExceptionGroup`` type, so this exercises the same code path on
    every Python version.
    """

    def __init__(self, message, exceptions):
        super().__init__(message)
        self.exceptions = tuple(exceptions)


class TestMCPClient:
    """Test MCP Client stdio functionality"""

    def test_mcp_client_stdio_init(self):
        """Test MCPClient initialization with stdio config"""
        stdio_config = MCPStdioConfig(command="python", args=["-m", "my_mcp_server"], env={"DEBUG": "1"})

        client = MCPClient(transport_type=MCPTransport.stdio, stdio_config=stdio_config)

        assert client.transport_type == MCPTransport.stdio
        assert client.stdio_config == stdio_config
        assert client.stdio_config is not None
        assert client.stdio_config.get("command") == "python"
        assert client.stdio_config.get("args") == ["-m", "my_mcp_server"]

    @pytest.mark.asyncio
    async def test_mcp_client_stdio_connect_error(self):
        """Test MCP client stdio connection error handling"""
        # Test missing stdio_config
        client = MCPClient(transport_type=MCPTransport.stdio)

        async def _noop(session):
            return None

        with pytest.raises(ValueError, match="stdio_config is required for stdio transport"):
            await client.run_with_session(_noop)

    @pytest.mark.asyncio
    @patch("litellm.experimental_mcp_client.client.stdio_client")
    @patch("litellm.experimental_mcp_client.client.ClientSession")
    async def test_mcp_client_stdio_connect_success(self, mock_session, mock_stdio_client):
        """Test successful stdio connection"""
        # Setup mocks - create proper async context manager
        mock_transport = (MagicMock(), MagicMock())
        mock_stdio_ctx = AsyncMock()
        mock_stdio_ctx.__aenter__.return_value = mock_transport
        mock_stdio_ctx.__aexit__.return_value = None
        mock_stdio_client.return_value = mock_stdio_ctx

        mock_session_instance = AsyncMock()
        mock_session_instance.initialize = AsyncMock(return_value=_initialized())
        mock_session_ctx = AsyncMock()
        mock_session_ctx.__aenter__.return_value = mock_session_instance
        mock_session_ctx.__aexit__.return_value = None
        mock_session.return_value = mock_session_ctx

        stdio_config = MCPStdioConfig(command="python", args=["-m", "my_mcp_server"], env={"DEBUG": "1"})

        client = MCPClient(transport_type=MCPTransport.stdio, stdio_config=stdio_config)

        async def _operation(session):
            return "ok"

        await client.run_with_session(_operation)

        # Verify stdio_client was called with correct parameters
        mock_stdio_client.assert_called_once()
        call_args = mock_stdio_client.call_args[0][0]
        assert call_args.command == "python"
        assert call_args.args == ["-m", "my_mcp_server"]
        assert call_args.env == {"DEBUG": "1"}

    @pytest.mark.asyncio
    @patch.object(mcp_client_module, "streamable_http_client")
    @patch.dict(
        os.environ,
        {
            "SSL_CERT_FILE": "/path/to/custom/ca-bundle.pem",
            "SSL_CERTIFICATE": "/path/to/client-cert.pem",
        },
    )
    async def test_mcp_client_ssl_configuration_from_env(self, mock_streamable_http_client):
        """Test that MCP client uses SSL configuration from environment variables"""
        # Setup mocks - create proper async context manager
        mock_transport = (MagicMock(), MagicMock())
        mock_http_ctx = AsyncMock()
        mock_http_ctx.__aenter__.return_value = mock_transport
        mock_http_ctx.__aexit__.return_value = None
        mock_streamable_http_client.return_value = mock_http_ctx

        # Mock the session
        with patch("litellm.experimental_mcp_client.client.ClientSession") as mock_session:
            mock_session_instance = AsyncMock()
            mock_session_instance.initialize = AsyncMock(return_value=_initialized())
            mock_session_ctx = AsyncMock()
            mock_session_ctx.__aenter__.return_value = mock_session_instance
            mock_session_ctx.__aexit__.return_value = None
            mock_session.return_value = mock_session_ctx

            client = MCPClient(
                server_url="https://mcp-server.example.com",
                transport_type=MCPTransport.http,
            )

            async def _operation(session):
                return "ok"

            await client.run_with_session(_operation)

            # Verify streamablehttp_client was called
            mock_streamable_http_client.assert_called_once()
            call_kwargs = mock_streamable_http_client.call_args[1]
            assert "http_client" in call_kwargs
            http_client = call_kwargs["http_client"]
            assert isinstance(http_client, httpx2.AsyncClient)

            # Test the factory still creates a client with proper SSL config
            httpx_factory = client._create_httpx_client_factory()
            test_client = httpx_factory(headers={"test": "header"})

            assert test_client is not None
            assert isinstance(test_client, httpx2.AsyncClient)
            assert test_client.headers is not None
            await test_client.aclose()

    @pytest.mark.asyncio
    @patch.object(mcp_client_module, "sse_client")
    async def test_mcp_client_ssl_verify_parameter(self, mock_sse_client):
        """Test that MCP client uses ssl_verify parameter when provided"""
        # Setup mocks - create proper async context manager
        mock_transport = (MagicMock(), MagicMock())
        mock_sse_ctx = AsyncMock()
        mock_sse_ctx.__aenter__.return_value = mock_transport
        mock_sse_ctx.__aexit__.return_value = None
        mock_sse_client.return_value = mock_sse_ctx

        # Mock the session
        with patch("litellm.experimental_mcp_client.client.ClientSession") as mock_session:
            mock_session_instance = AsyncMock()
            mock_session_instance.initialize = AsyncMock(return_value=_initialized())
            mock_session_ctx = AsyncMock()
            mock_session_ctx.__aenter__.return_value = mock_session_instance
            mock_session_ctx.__aexit__.return_value = None
            mock_session.return_value = mock_session_ctx

            # Test with ssl_verify=False
            client = MCPClient(
                server_url="https://mcp-server.example.com",
                transport_type=MCPTransport.sse,
                ssl_verify=False,
            )

            async def _operation(session):
                return "ok"

            await client.run_with_session(_operation)

            # Verify sse_client was called
            mock_sse_client.assert_called_once()
            call_kwargs = mock_sse_client.call_args[1]

            # Verify httpx_client_factory was passed
            assert "httpx_client_factory" in call_kwargs
            httpx_factory = call_kwargs["httpx_client_factory"]

            # Test the factory creates a client with SSL verification disabled
            # When ssl_verify=False, the factory should disable SSL verification
            test_client = httpx_factory(headers={"test": "header"})

            # Verify the client was created successfully
            assert test_client is not None
            assert isinstance(test_client, httpx2.AsyncClient)
            # Verify it has the expected properties
            assert test_client.headers is not None
            # Clean up
            await test_client.aclose()

    @pytest.mark.asyncio
    @patch.object(mcp_client_module, "streamable_http_client")
    async def test_mcp_client_ssl_verify_custom_path(self, mock_streamable_http_client):
        """Test that MCP client uses custom CA bundle path from ssl_verify parameter"""
        # Setup mocks - create proper async context manager
        mock_transport = (MagicMock(), MagicMock())
        mock_http_ctx = AsyncMock()
        mock_http_ctx.__aenter__.return_value = mock_transport
        mock_http_ctx.__aexit__.return_value = None
        mock_streamable_http_client.return_value = mock_http_ctx

        # Mock the session
        with patch("litellm.experimental_mcp_client.client.ClientSession") as mock_session:
            mock_session_instance = AsyncMock()
            mock_session_instance.initialize = AsyncMock(return_value=_initialized())
            mock_session_ctx = AsyncMock()
            mock_session_ctx.__aenter__.return_value = mock_session_instance
            mock_session_ctx.__aexit__.return_value = None
            mock_session.return_value = mock_session_ctx

            # Test with custom CA bundle path
            custom_ca_path = "/custom/path/to/ca-bundle.pem"
            client = MCPClient(
                server_url="https://mcp-server.example.com",
                transport_type=MCPTransport.http,
                ssl_verify=custom_ca_path,
            )

            async def _operation(session):
                return "ok"

            await client.run_with_session(_operation)

            # Verify streamablehttp_client was called
            mock_streamable_http_client.assert_called_once()
            call_kwargs = mock_streamable_http_client.call_args[1]
            assert "http_client" in call_kwargs
            http_client = call_kwargs["http_client"]
            assert isinstance(http_client, httpx2.AsyncClient)

            httpx_factory = client._create_httpx_client_factory()
            test_client = httpx_factory(headers={"test": "header"})

            assert test_client is not None
            assert isinstance(test_client, httpx2.AsyncClient)
            assert test_client.headers is not None
            await test_client.aclose()

    def test_token_auth_header_generation(self):
        """Test that token auth generates correct Authorization header"""
        client = MCPClient(
            server_url="http://example.com/sse",
            transport_type="sse",
            auth_type=MCPAuth.token,
            auth_value="my-secret-token",
        )

        headers = client._get_auth_headers()

        assert "Authorization" in headers
        assert headers["Authorization"] == "token my-secret-token"

    def test_token_auth_compatibility_with_existing_auth_types(self):
        """Verify existing auth types are not affected by token auth addition"""
        # Test bearer token
        client = MCPClient(
            server_url="http://example.com/sse",
            transport_type="sse",
            auth_type=MCPAuth.bearer_token,
            auth_value="bearer-token",
        )
        headers = client._get_auth_headers()
        assert headers["Authorization"] == "Bearer bearer-token"

        # Test API key
        client = MCPClient(
            server_url="http://example.com/sse",
            transport_type="sse",
            auth_type=MCPAuth.api_key,
            auth_value="api-key",
        )
        headers = client._get_auth_headers()
        assert headers["X-API-Key"] == "api-key"

        # Test basic auth (gets base64 encoded)
        client = MCPClient(
            server_url="http://example.com/sse",
            transport_type="sse",
            auth_type=MCPAuth.basic,
            auth_value="user:pass",
        )
        headers = client._get_auth_headers()
        assert headers["Authorization"].startswith("Basic ")

    def test_token_auth_with_extra_headers(self):
        """Test that token auth works alongside extra headers"""
        client = MCPClient(
            server_url="http://example.com/sse",
            transport_type="sse",
            auth_type=MCPAuth.token,
            auth_value="my-token",
            extra_headers={"X-Custom-Header": "custom-value"},
        )

        headers = client._get_auth_headers()

        assert headers["Authorization"] == "token my-token"
        assert headers["X-Custom-Header"] == "custom-value"

    def test_get_auth_headers_strips_static_header_whitespace(self):
        """
        Static header names/values must be stripped of surrounding whitespace.

        h11 rejects header values with leading/trailing whitespace as an
        "Illegal header value", which silently aborts the MCP connection. A
        stray space in a configured static header value would otherwise make
        every request to that server fail with an opaque error.
        """
        client = MCPClient(
            server_url="http://example.com/mcp",
            transport_type="http",
            extra_headers={"X-Db-Url": " mew://host ", "  X-Pad  ": "v"},
        )

        headers = client._get_auth_headers()

        assert headers["X-Db-Url"] == "mew://host"
        assert headers["X-Pad"] == "v"

    def test_token_auth_enum_value(self):
        """Test that MCPAuth.token enum exists and has correct value"""
        assert hasattr(MCPAuth, "token")
        assert MCPAuth.token.value == "token"


# ---------------------------------------------------------------------------
# _last_initialize_instructions capture
# ---------------------------------------------------------------------------


class TestMCPClientInstructionsCapture:
    """Tests for _last_initialize_instructions capture during session init."""

    def test_initial_value_is_none(self):
        """Fresh client has no cached instructions."""
        client = MCPClient(
            server_url="http://example.com/mcp",
            transport_type="http",
        )
        assert client._last_initialize_instructions is None

    @pytest.mark.asyncio
    @patch("litellm.experimental_mcp_client.client.ClientSession")
    async def test_captures_instructions_from_initialize(self, mock_session_cls):
        """Instructions from upstream initialize() are captured and stripped."""
        client = MCPClient(
            server_url="http://example.com/mcp",
            transport_type="http",
        )

        mock_session = AsyncMock()
        init_result = _initialized("  upstream says hello  ")
        mock_session.initialize = AsyncMock(return_value=init_result)

        session_ctx = MagicMock()
        session_ctx.__aenter__ = AsyncMock(return_value=mock_session)
        session_ctx.__aexit__ = AsyncMock(return_value=False)
        mock_session_cls.return_value = session_ctx

        transport_ctx = MagicMock()
        transport_ctx.__aenter__ = AsyncMock(return_value=(MagicMock(), MagicMock()))
        transport_ctx.__aexit__ = AsyncMock(return_value=False)

        async def _op(session):
            return "done"

        await client._execute_session_operation(transport_ctx, _op)
        assert client._last_initialize_instructions == "upstream says hello"

    @pytest.mark.asyncio
    @patch("litellm.experimental_mcp_client.client.ClientSession")
    async def test_none_instructions_stays_none(self, mock_session_cls):
        """When upstream returns no instructions the field stays None."""
        client = MCPClient(
            server_url="http://example.com/mcp",
            transport_type="http",
        )

        mock_session = AsyncMock()
        init_result = _initialized()
        mock_session.initialize = AsyncMock(return_value=init_result)

        session_ctx = MagicMock()
        session_ctx.__aenter__ = AsyncMock(return_value=mock_session)
        session_ctx.__aexit__ = AsyncMock(return_value=False)
        mock_session_cls.return_value = session_ctx

        transport_ctx = MagicMock()
        transport_ctx.__aenter__ = AsyncMock(return_value=(MagicMock(), MagicMock()))
        transport_ctx.__aexit__ = AsyncMock(return_value=False)

        async def _op(session):
            return "done"

        await client._execute_session_operation(transport_ctx, _op)
        assert client._last_initialize_instructions is None


# ---------------------------------------------------------------------------
# Transport error surfacing
# ---------------------------------------------------------------------------


class TestFirstNonCancelledCause:
    """Unwrapping the real cause out of a (possibly nested) exception group."""

    def test_returns_plain_non_cancelled(self):
        err = ValueError("boom")
        assert _first_non_cancelled_cause(err) is err

    def test_returns_none_for_plain_cancelled(self):
        assert _first_non_cancelled_cause(asyncio.CancelledError()) is None

    def test_unwraps_group_to_non_cancelled_leaf(self):
        target = httpx2.ConnectError("refused")
        group = _FakeExceptionGroup("g", [asyncio.CancelledError(), target])
        assert _first_non_cancelled_cause(group) is target

    def test_unwraps_nested_group(self):
        target = httpx2.LocalProtocolError("Illegal header value")
        inner = _FakeExceptionGroup("inner", [asyncio.CancelledError(), target])
        outer = _FakeExceptionGroup("outer", [asyncio.CancelledError(), inner])
        assert _first_non_cancelled_cause(outer) is target

    def test_all_cancelled_returns_none(self):
        group = _FakeExceptionGroup("g", [asyncio.CancelledError(), asyncio.CancelledError()])
        assert _first_non_cancelled_cause(group) is None

    @pytest.mark.skipif(sys.version_info < (3, 11), reason="builtin ExceptionGroup requires 3.11+")
    def test_unwraps_builtin_exception_group(self):
        target = httpx2.ConnectError("refused")
        group = ExceptionGroup("transport failed", [target])  # noqa: F821
        assert _first_non_cancelled_cause(group) is target


class TestExecuteSessionOperationSurfacesTransportError:
    """_execute_session_operation should surface the real transport failure.

    When the upstream transport's task group fails (illegal header, connection
    refused, ...), the in-flight ``session.initialize()`` is cancelled and the
    real error only appears when the transport context exits. The opaque
    ``CancelledError`` must be replaced with that real cause.
    """

    def _make_session(self, mock_session_cls, initialize):
        mock_session = AsyncMock()
        mock_session.initialize = initialize
        session_ctx = MagicMock()
        session_ctx.__aenter__ = AsyncMock(return_value=mock_session)
        session_ctx.__aexit__ = AsyncMock(return_value=False)
        mock_session_cls.return_value = session_ctx

    def _make_transport(self, aexit_side_effect):
        transport_ctx = MagicMock()
        transport_ctx.__aenter__ = AsyncMock(return_value=(MagicMock(), MagicMock()))
        transport_ctx.__aexit__ = AsyncMock(side_effect=aexit_side_effect)
        return transport_ctx

    @pytest.mark.asyncio
    @patch("litellm.experimental_mcp_client.client.ClientSession")
    async def test_surfaces_connect_error_over_cancelled(self, mock_session_cls):
        client = MCPClient(server_url="http://example.com/mcp", transport_type="http")
        self._make_session(
            mock_session_cls,
            AsyncMock(side_effect=asyncio.CancelledError("cancelled by group")),
        )
        connect_error = httpx2.ConnectError("All connection attempts failed")
        transport_ctx = self._make_transport(_FakeExceptionGroup("transport", [connect_error]))

        async def _op(session):
            return "done"

        with pytest.raises(httpx2.ConnectError):
            await client._execute_session_operation(transport_ctx, _op)

    @pytest.mark.asyncio
    @patch("litellm.experimental_mcp_client.client.ClientSession")
    async def test_genuine_cancellation_is_not_replaced(self, mock_session_cls):
        client = MCPClient(server_url="http://example.com/mcp", transport_type="http")
        self._make_session(mock_session_cls, AsyncMock(side_effect=asyncio.CancelledError()))
        transport_ctx = self._make_transport(_FakeExceptionGroup("teardown", [asyncio.CancelledError()]))

        async def _op(session):
            return "done"

        with pytest.raises(asyncio.CancelledError):
            await client._execute_session_operation(transport_ctx, _op)

    @pytest.mark.asyncio
    @pytest.mark.parametrize("failure_phase", ("early", "late", "mixed"))
    @patch("litellm.experimental_mcp_client.client.ClientSession")
    async def test_response_close_preserves_cancellation_and_original_errors(self, session_class, failure_phase):
        closed: Final = asyncio.Event()
        close_error: Final = httpx2.ReadError("response close failed")
        connect_error: Final = httpx2.ConnectError("another request failed before cancellation")
        cancelled: Final = asyncio.CancelledError("caller cancelled")

        class FailingCloseStream(httpx2.AsyncByteStream):
            async def __aiter__(self) -> AsyncIterator[bytes]:
                yield b"pending"

            async def aclose(self) -> None:
                closed.set()
                raise close_error

        client: Final = MCPClient(server_url="https://example.com/mcp")
        async with client._create_httpx_client_factory(
            transport=httpx2.MockTransport(lambda _: httpx2.Response(200, stream=FailingCloseStream()))
        )() as http_client:
            response: Final = await http_client.send(http_client.build_request("POST", client.server_url), stream=True)

            async def initialize():
                if failure_phase == "early":
                    await response.aclose()
                raise cancelled

            async def close_transport(*args):
                if failure_phase == "early":
                    return
                try:
                    await response.aclose()
                except httpx2.ReadError as error:
                    failures: Final = [error, connect_error] if failure_phase == "mixed" else [error]
                    raise _FakeExceptionGroup("transport", [_FakeExceptionGroup("reader", failures)])

            self._make_session(session_class, initialize)
            expected: Final = (
                close_error if failure_phase == "early" else connect_error if failure_phase == "mixed" else cancelled
            )
            with pytest.raises(type(expected)) as caught:
                await client._execute_session_operation(self._make_transport(close_transport), AsyncMock(), http_client)
            assert caught.value is expected
            assert closed.is_set()

    @pytest.mark.asyncio
    @patch("litellm.experimental_mcp_client.client.ClientSession")
    async def test_cleanup_error_after_success_is_swallowed(self, mock_session_cls):
        client = MCPClient(server_url="http://example.com/mcp", transport_type="http")
        init_result = _initialized()
        self._make_session(mock_session_cls, AsyncMock(return_value=init_result))
        transport_ctx = self._make_transport(_FakeExceptionGroup("late", [httpx2.ConnectError("late cleanup error")]))

        async def _op(session):
            return "done"

        result = await client._execute_session_operation(transport_ctx, _op)
        assert result == "done"

    @pytest.mark.asyncio
    @patch("litellm.experimental_mcp_client.client.ClientSession")
    async def test_session_entry_failure_still_closes_transport(self, session_class):
        failure: Final = RuntimeError("session dispatcher did not start")
        session_class.return_value.__aenter__ = AsyncMock(side_effect=failure)
        closed: Final = asyncio.Event()

        async def close_transport(*args):
            await anyio.lowlevel.checkpoint()
            closed.set()

        transport: Final = self._make_transport(close_transport)
        client: Final = MCPClient(server_url="https://example.com/mcp")
        with pytest.raises(RuntimeError) as caught:
            await client._execute_session_operation(transport, AsyncMock())
        assert caught.value is failure
        assert closed.is_set()

    @pytest.mark.asyncio
    @pytest.mark.parametrize("original_error", (False, True))
    @patch("litellm.experimental_mcp_client.client.ClientSession")
    async def test_session_exit_cancellation_preserves_original_failure(self, session_class, original_error):
        self._make_session(session_class, AsyncMock(return_value=_initialized()))
        cancelled: Final = asyncio.CancelledError("cancelled while closing session")
        session_class.return_value.__aexit__ = AsyncMock(side_effect=cancelled)
        original: Final = RuntimeError("operation failed")
        transport: Final = self._make_transport(None)
        client: Final = MCPClient(server_url="https://example.com/mcp")

        async def operation(session):
            if original_error:
                raise original
            return "done"

        with pytest.raises(RuntimeError if original_error else asyncio.CancelledError) as caught:
            await client._execute_session_operation(transport, operation)
        assert caught.value is (original if original_error else cancelled)
        transport.__aexit__.assert_awaited_once()

    @pytest.mark.asyncio
    @pytest.mark.parametrize("signal_type", (SystemExit, KeyboardInterrupt))
    @pytest.mark.parametrize("phase", ("session", "transport"))
    @patch("litellm.experimental_mcp_client.client.ClientSession")
    async def test_cleanup_preserves_process_exit(self, session_class, phase, signal_type):
        self._make_session(session_class, AsyncMock(return_value=_initialized()))
        signal: Final = signal_type("process stopping")
        if phase == "session":
            session_class.return_value.__aexit__ = AsyncMock(side_effect=signal)
        transport: Final = self._make_transport(signal if phase == "transport" else None)
        client: Final = MCPClient(server_url="https://example.com/mcp")
        with pytest.raises(signal_type) as caught:
            await client._execute_session_operation(transport, AsyncMock(return_value="done"))
        assert caught.value is signal
        transport.__aexit__.assert_awaited_once()

    @pytest.mark.asyncio
    @patch("litellm.experimental_mcp_client.client.ClientSession")
    async def test_session_and_termination_share_one_cleanup_deadline(self, session_class):
        self._make_session(session_class, AsyncMock(return_value=_initialized()))
        deleting: Final = asyncio.Event()

        async def close_session(*args):
            await anyio.sleep(1)

        async def respond(request: httpx2.Request) -> httpx2.Response:
            deleting.set()
            await anyio.sleep_forever()
            raise AssertionError("termination unexpectedly resumed")

        client: Final = MCPClient(server_url="https://example.com/mcp")
        http_client: Final = client._create_httpx_client_factory(transport=httpx2.MockTransport(respond))()
        session_class.return_value.__aexit__ = AsyncMock(side_effect=close_session)

        async def close_transport(*args):
            await http_client.delete(client.server_url)

        before: Final = anyio.current_time()
        try:
            with pytest.raises(asyncio.CancelledError):
                await client._execute_session_operation(
                    self._make_transport(close_transport), AsyncMock(return_value="completed"), http_client=http_client
                )
            assert deleting.is_set()
            assert 4.8 <= anyio.current_time() - before < 5.8
        finally:
            await http_client.aclose()


class TestMCPClientResolvedAuth:
    """A pre-resolved httpx2.Auth is attached to the upstream client's auth= slot."""

    @pytest.mark.asyncio
    async def test_resolved_auth_feeds_the_auth_slot(self):
        resolved = httpx2.Auth()
        client = MCPClient(server_url="https://upstream.example.com", resolved_auth=resolved)
        http_client = client._create_httpx_client_factory()()
        try:
            assert http_client.auth is resolved
        finally:
            await http_client.aclose()

    @pytest.mark.asyncio
    async def test_resolved_auth_takes_precedence_over_aws_auth(self):
        resolved = httpx2.Auth()
        client = MCPClient(
            server_url="https://upstream.example.com",
            resolved_auth=resolved,
            aws_auth=httpx2.Auth(),
        )
        http_client = client._create_httpx_client_factory()()
        try:
            assert http_client.auth is resolved
        finally:
            await http_client.aclose()

    @pytest.mark.asyncio
    async def test_without_resolved_auth_falls_back_to_aws_auth(self):
        aws = httpx2.Auth()
        client = MCPClient(server_url="https://upstream.example.com", aws_auth=aws)
        http_client = client._create_httpx_client_factory()()
        try:
            assert http_client.auth is aws
        finally:
            await http_client.aclose()


def _rendered_log_message(call):
    message = str(call.args[0])
    values = call.args[1:]
    return message % values if values else message


def _all_logged_messages(mock_logger):
    return " ".join(
        _rendered_log_message(call)
        for level in ("info", "debug", "warning", "error", "exception")
        for call in getattr(mock_logger, level).call_args_list
        if call.args
    )


@pytest.mark.asyncio
async def test_call_tool_does_not_log_arguments():
    from mcp.types import CallToolRequestParams

    secret = "ssn-123-45-6789"
    client = MCPClient(server_url="http://test-server")
    client.run_with_session = AsyncMock(return_value=MagicMock())
    params = CallToolRequestParams(name="search_tool", arguments={"input": secret, "model": "gpt-5-mini"})

    with patch.object(mcp_client_module, "verbose_logger") as mock_logger:
        await client.call_tool(params)

    logged = _all_logged_messages(mock_logger)
    assert "search_tool" in logged
    assert secret not in logged
    assert "gpt-5-mini" not in logged


@pytest.mark.asyncio
async def test_get_prompt_does_not_log_arguments():
    from mcp.types import GetPromptRequestParams

    secret = "ssn-987-65-4321"
    client = MCPClient(server_url="http://test-server")
    client.run_with_session = AsyncMock(return_value=MagicMock())
    params = GetPromptRequestParams(name="my_prompt", arguments={"input": secret})

    with patch.object(mcp_client_module, "verbose_logger") as mock_logger:
        await client.get_prompt(params)

    logged = _all_logged_messages(mock_logger)
    assert "my_prompt" in logged
    assert secret not in logged


if __name__ == "__main__":
    pytest.main([__file__])


@pytest.mark.asyncio
async def test_call_tool_raise_on_error_logs_at_debug_not_error():
    """When the caller opts into raise_on_error it owns the exception and logs it at the fitting
    level (an expected pass-through re-auth 401 is info, not error). call_tool must therefore not emit
    its own error-level line in that mode, so error-rate alerts do not trip on the expected signal;
    the swallow path (raise_on_error=False) still logs at error since nothing downstream will."""
    from mcp.types import CallToolRequestParams

    client = MCPClient(transport_type=MCPTransport.stdio)
    boom = RuntimeError("upstream boom")

    async def _raise(_operation, **_kwargs):
        raise boom

    params = CallToolRequestParams(name="t", arguments={})

    with patch.object(client, "run_with_session", side_effect=_raise) as mock_rws:
        with patch.object(mcp_client_module, "verbose_logger") as mock_log:
            with pytest.raises(RuntimeError):
                await client.call_tool(params, raise_on_error=True)
            assert not mock_log.error.called, "raise_on_error path must not log at error"
            debug_msgs = [str(c.args[0]) for c in mock_log.debug.call_args_list if c.args]
            assert any("call_tool failed" in m for m in debug_msgs), "the demoted failure line must go to debug"
            assert mock_rws.call_args.kwargs.get("quiet_on_error") is True, (
                "call_tool must forward quiet_on_error so run_with_session also demotes its own failure line"
            )

    with patch.object(client, "run_with_session", side_effect=_raise):
        with patch.object(mcp_client_module, "verbose_logger") as mock_log:
            result = await client.call_tool(params, raise_on_error=False)
            assert result.is_error is True
            assert mock_log.error.called, "swallow path must keep error-level visibility"


@pytest.mark.asyncio
async def test_list_tools_raise_on_error_logs_at_debug_not_error():
    """list_tools must mirror call_tool: when the caller opts into raise_on_error it owns the
    exception, so an expected pass-through re-auth 401 does not emit an error/exception line that
    would trip error-rate alerts. The swallow path still logs the full exception."""
    client = MCPClient(transport_type=MCPTransport.stdio)
    boom = RuntimeError("upstream boom")

    async def _raise(_operation, **_kwargs):
        raise boom

    with patch.object(client, "run_with_session", side_effect=_raise) as mock_rws:
        with patch.object(mcp_client_module, "verbose_logger") as mock_log:
            with pytest.raises(RuntimeError):
                await client.list_tools(raise_on_error=True)
            assert not mock_log.error.called, "raise_on_error path must not log at error"
            assert not mock_log.exception.called, "raise_on_error path must not log a traceback"
            debug_msgs = [str(c.args[0]) for c in mock_log.debug.call_args_list if c.args]
            assert any("list_tools failed" in m for m in debug_msgs), "the demoted failure line must go to debug"
            assert mock_rws.call_args.kwargs.get("quiet_on_error") is True, (
                "list_tools must forward quiet_on_error so run_with_session also demotes its own failure line"
            )

    with patch.object(client, "run_with_session", side_effect=_raise):
        with patch.object(mcp_client_module, "verbose_logger") as mock_log:
            result = await client.list_tools(raise_on_error=False)
            assert result == []
            assert mock_log.exception.called, "swallow path must keep full exception visibility"


@pytest.mark.asyncio
async def test_run_with_session_quiet_on_error_demotes_warning_to_debug():
    """run_with_session logs its failure at warning by default (an operator signal for an unexpected
    outage), but when the caller owns the exception (quiet_on_error=True, set by call_tool / list_tools
    under raise_on_error) it must demote that line to debug so an expected pass-through re-auth does not
    emit a warning per call."""
    client = MCPClient(transport_type=MCPTransport.stdio)
    boom = RuntimeError("session boom")

    async def _op(_session):
        raise boom

    async def _fake_exec(_transport_ctx, _operation, http_client=None):
        raise boom

    with patch.object(client, "_create_transport_context", return_value=(object(), None)):
        with patch.object(client, "_execute_session_operation", side_effect=_fake_exec):
            with patch.object(mcp_client_module, "verbose_logger") as mock_log:
                with pytest.raises(RuntimeError):
                    await client.run_with_session(_op, quiet_on_error=True)
                assert not mock_log.warning.called, "quiet_on_error must not emit a warning"
                debug_msgs = [str(c.args[0]) for c in mock_log.debug.call_args_list if c.args]
                assert any("run_with_session failed" in m for m in debug_msgs), "the failure line must go to debug"

            with patch.object(mcp_client_module, "verbose_logger") as mock_log:
                with pytest.raises(RuntimeError):
                    await client.run_with_session(_op)
                warning_msgs = [str(c.args[0]) for c in mock_log.warning.call_args_list if c.args]
                assert any("run_with_session failed" in m for m in warning_msgs), (
                    "the default path must keep the operator-visible warning"
                )


class _ScriptedUpstream:
    """An in-memory MCP upstream that answers ``initialize`` and then follows one script for
    ``tools/list``.

    ``answer=None`` ends the response stream without a JSON-RPC reply, which is what a
    streamable-HTTP upstream does when its SSE stream closes early: the SDK drops the message and
    the request is never resolved and never fails. Anything else is sent back as that JSON-RPC
    error, the shape an upstream application uses to report its own failure.
    """

    def __init__(self, tools_list_error: ErrorData | None = None):
        self._tools_list_error = tools_list_error
        self._to_client_tx, self._to_client_rx = anyio.create_memory_object_stream(10)
        self._from_client_tx, self._from_client_rx = anyio.create_memory_object_stream(10)
        self._task_group = None

    async def __aenter__(self):
        self._task_group = anyio.create_task_group()
        await self._task_group.__aenter__()
        self._task_group.start_soon(self._serve)
        return self._to_client_rx, self._from_client_tx

    async def __aexit__(self, *_exc_info):
        self._task_group.cancel_scope.cancel()
        return await self._task_group.__aexit__(None, None, None)

    async def _send(self, message):
        await self._to_client_tx.send(SessionMessage(message))

    async def _serve(self):
        async for session_message in self._from_client_rx:
            request = session_message.message
            method = getattr(request, "method", None)
            if method == "initialize":
                result = InitializeResult(
                    protocolVersion=LATEST_HANDSHAKE_VERSION,
                    capabilities=ServerCapabilities(),
                    serverInfo=Implementation(name="scripted-upstream", version="1.0.0"),
                )
                await self._send(
                    JSONRPCResponse(
                        jsonrpc="2.0",
                        id=request.id,
                        result=result.model_dump(by_alias=True, mode="json", exclude_none=True),
                    )
                )
            elif method == "tools/list" and self._tools_list_error is not None:
                await self._send(JSONRPCError(jsonrpc="2.0", id=request.id, error=self._tools_list_error))


class _ScriptedClient(MCPClient):
    """An MCPClient whose transport is a scripted in-memory upstream instead of a real connection,
    so the real ``ClientSession`` and its real timeout machinery are what run."""

    def __init__(self, *, timeout: float, tools_list_error: ErrorData | None = None):
        super().__init__(server_url="http://upstream.local/mcp", timeout=timeout)
        self._upstream = _ScriptedUpstream(tools_list_error=tools_list_error)

    def _create_transport_context(self):
        return self._upstream, None


@pytest.mark.asyncio
async def test_list_tools_fails_on_its_own_timeout_when_the_upstream_never_answers():
    """An upstream that accepts the request and never answers must fail the client's own timeout.

    Without a session read timeout the request waits forever, so discovery only ends when an outer
    cancel scope kills it. That is the reported symptom: a cancelled list_tools, no tools, and a
    fault that blames the gateway. The outer guard here is 20x the client timeout, so a run that
    reaches it proves nothing bounded the request.

    The classification is asserted here, off a real ``ClientSession`` running its real read timeout,
    rather than off a hand-built exception. A hand-built fixture encodes what we currently believe
    the SDK raises and would keep passing after the SDK stopped raising it, at which point the
    translation would quietly stop matching and the fault would silently downgrade to ``internal``.
    Driving the real path makes an SDK bump that breaks the discriminator fail loudly instead.
    """
    client = _ScriptedClient(timeout=0.5)

    started = asyncio.get_running_loop().time()
    with pytest.raises(TimeoutError) as exc_info:
        await asyncio.wait_for(client.list_tools(raise_on_error=True), timeout=10)
    elapsed = asyncio.get_running_loop().time() - started

    assert elapsed < 5, f"the request must end on the client's own 0.5s timeout, took {elapsed:.2f}s"

    fault = classify_list_exception(exc_info.value)
    assert fault.tag == "timeout", "an upstream that stopped answering must not be classified as the gateway's fault"
    assert list_fault_http_status(fault) == 504


@pytest.mark.asyncio
async def test_upstream_json_rpc_error_408_is_not_reported_as_a_client_timeout():
    """The SDK reports its own elapsed read timeout and relays an upstream JSON-RPC error through
    the same exception class and the same numeric field, and JSON-RPC error codes are a different
    namespace from HTTP status codes. An upstream answering with application code 408 must keep
    travelling as ``MCPError`` so it is never blamed on the gateway as a 504.

    This is the other half of the pair: the same real transport and the same real session, so one
    mechanism pins both directions.
    """
    client = _ScriptedClient(
        timeout=30,
        tools_list_error=ErrorData(code=REQUEST_TIMEOUT, message="re-authenticate and retry"),
    )

    with pytest.raises(MCPError) as exc_info:
        await asyncio.wait_for(client.list_tools(raise_on_error=True), timeout=10)

    assert not isinstance(exc_info.value, TimeoutError), "an upstream application error is not a gateway timeout"
    assert exc_info.value.error.code == REQUEST_TIMEOUT

    fault = classify_list_exception(exc_info.value)
    assert fault.tag != "timeout", "an upstream's own application error must never be reported as a gateway timeout"
    assert list_fault_http_status(fault) != 504


def _raise_mcp_error_while_handling_a_timeout(code: int, message: str) -> MCPError:
    """An ``MCPError`` carrying the context chain it would have if it were raised while a
    ``TimeoutError`` was in flight, which is how the SDK raises its own read timeout."""
    try:
        try:
            raise TimeoutError()
        except TimeoutError:
            raise MCPError(code=code, message=message)
    except MCPError as raised:
        return raised


def test_as_mcp_read_timeout_separates_the_sdk_timeout_from_a_relayed_upstream_error():
    """Neither signal alone is enough. The code alone cannot separate the SDK's own timeout from an
    upstream JSON-RPC error that happens to use 408, and the context chain alone cannot separate it
    from any other relayed error that surfaces while a timeout is being handled, so both must hold.
    """
    timeout_code = REQUEST_TIMEOUT

    translated = as_mcp_read_timeout(_raise_mcp_error_while_handling_a_timeout(timeout_code, "Timed out while waiting"))
    assert isinstance(translated, TimeoutError)
    assert str(translated) == "Timed out while waiting"

    relayed_408 = MCPError(code=timeout_code, message="upstream said 408")
    assert as_mcp_read_timeout(relayed_408) is None, "an upstream 408 with no elapsed timeout is not our timeout"

    relayed_other = _raise_mcp_error_while_handling_a_timeout(-32603, "upstream internal error")
    assert as_mcp_read_timeout(relayed_other) is None, "a non-timeout code is not our timeout, whatever the chain"

    assert as_mcp_read_timeout(MCPError(code=-32603, message="boom")) is None
    assert as_mcp_read_timeout(RuntimeError("not an MCPError")) is None


@pytest.mark.asyncio
async def test_read_timeout_logs_an_actionable_line_that_quiet_on_error_cannot_demote():
    """The reported failure surfaced only as "MCP Client list_tools was cancelled", which names
    neither the server nor the elapsed budget. An upstream that stops answering is always
    operator-actionable, so this line stays at warning even for callers that own the exception."""
    client = _ScriptedClient(timeout=0.5)

    with patch.object(mcp_client_module, "verbose_logger") as mock_log:
        with pytest.raises(TimeoutError):
            await asyncio.wait_for(client.list_tools(raise_on_error=True), timeout=10)

    warnings = [str(call.args[0]) % tuple(call.args[1:]) for call in mock_log.warning.call_args_list if call.args]
    timeout_lines = [line for line in warnings if "timed out after" in line]
    assert timeout_lines, f"expected an actionable timeout warning, got {warnings}"
    assert "http://upstream.local/mcp" in timeout_lines[0], "the line must name the server that stopped answering"
    assert "0.5s" in timeout_lines[0], "the line must name the budget that elapsed"


class TestAuthSchemeNormalization:
    """MCP egress must emit exactly one authorization scheme.

    Callers supply both a bare credential and a complete header value (the latter whenever it is
    passed through from ``x-mcp-auth`` / ``Authorization``), and the second shape used to be given
    a second scheme, which upstream servers reject as a malformed token.
    """

    @pytest.mark.parametrize(
        "auth_type, auth_value",
        [
            (MCPAuth.bearer_token, "bare-token"),
            (MCPAuth.bearer_token, "Bearer bare-token"),
            (MCPAuth.bearer_token, "bearer bare-token"),
            (MCPAuth.bearer_token, "  BEARER   bare-token"),
            (MCPAuth.oauth2, "bare-token"),
            (MCPAuth.oauth2, "Bearer bare-token"),
            (MCPAuth.oauth2_token_exchange, "bare-token"),
            (MCPAuth.oauth2_token_exchange, "Bearer bare-token"),
        ],
    )
    def test_bearer_family_emits_exactly_one_scheme(self, auth_type, auth_value):
        client = MCPClient(server_url="http://example.com/mcp", auth_type=auth_type, auth_value=auth_value)

        assert client._get_auth_headers()["Authorization"] == "Bearer bare-token"

    @pytest.mark.parametrize("auth_value", ["bare-token", "token bare-token", "TOKEN bare-token"])
    def test_token_scheme_emits_exactly_one_scheme(self, auth_value):
        client = MCPClient(server_url="http://example.com/mcp", auth_type=MCPAuth.token, auth_value=auth_value)

        assert client._get_auth_headers()["Authorization"] == "token bare-token"

    @pytest.mark.parametrize(
        "auth_type, auth_value",
        [
            (MCPAuth.bearer_token, "Bearertoken"),
            (MCPAuth.oauth2, "Bearer.eyJzdWIiOiJhYmMifQ.sig"),
            (MCPAuth.token, "tokenish"),
        ],
    )
    def test_a_credential_merely_starting_with_the_scheme_text_is_left_intact(self, auth_type, auth_value):
        """RFC 7235 requires whitespace between scheme and credential, so a token whose first
        characters happen to spell the scheme is a credential, not a schemed value."""
        client = MCPClient(server_url="http://example.com/mcp", auth_type=auth_type, auth_value=auth_value)

        scheme = "token" if auth_type == MCPAuth.token else "Bearer"
        assert client._get_auth_headers()["Authorization"] == f"{scheme} {auth_value}"

    @pytest.mark.parametrize(
        "auth_type, auth_value, expected",
        [
            (MCPAuth.bearer_token, "Bearer ", "Bearer Bearer"),
            (MCPAuth.bearer_token, "Bearer    ", "Bearer Bearer"),
        ],
    )
    def test_a_scheme_with_no_credential_behind_it_still_produces_a_header(self, auth_type, auth_value, expected):
        """Treating this as a schemed value would leave nothing to send, and a request with no
        Authorization at all is harder to diagnose upstream than a visibly wrong one."""
        client = MCPClient(server_url="http://example.com/mcp", auth_type=auth_type, auth_value=auth_value)

        assert client._get_auth_headers()["Authorization"] == expected

    def test_basic_with_a_scheme_and_no_credential_still_produces_a_header(self):
        client = MCPClient(server_url="http://example.com/mcp", auth_type=MCPAuth.basic, auth_value="Basic ")

        assert "Authorization" in client._get_auth_headers()

    def test_basic_accepts_an_already_encoded_schemed_value_without_re_encoding_it(self):
        """Stripping the scheme at header-build time cannot fix this shape: ``to_basic_auth`` has by
        then encoded the whole ``Basic ...`` string, leaving no prefix to find."""
        encoded = base64.b64encode(b"user:pass").decode()

        client = MCPClient(
            server_url="http://example.com/mcp",
            auth_type=MCPAuth.basic,
            auth_value=f"Basic {encoded}",
        )

        header = client._get_auth_headers()["Authorization"]
        assert header == f"Basic {encoded}"
        assert base64.b64decode(header.split(" ", 1)[1]) == b"user:pass"

    @pytest.mark.parametrize("auth_value", ["user:pass", "Basic user:pass", "basic user:pass"])
    def test_basic_always_emits_encoded_credentials(self, auth_value):
        """A schemed value whose remainder is raw rather than encoded is still a username/password
        pair, so it is encoded rather than forwarded as an invalid RFC 7617 header."""
        client = MCPClient(server_url="http://example.com/mcp", auth_type=MCPAuth.basic, auth_value=auth_value)

        header = client._get_auth_headers()["Authorization"]
        assert base64.b64decode(header.split(" ", 1)[1]) == b"user:pass"

    def test_authorization_auth_type_is_passed_through_verbatim(self):
        """``MCPAuth.authorization`` means the caller owns the whole header value."""
        client = MCPClient(
            server_url="http://example.com/mcp",
            auth_type=MCPAuth.authorization,
            auth_value="Bearer Bearer deliberately-doubled",
        )

        assert client._get_auth_headers()["Authorization"] == "Bearer Bearer deliberately-doubled"

    def test_api_key_credential_is_not_treated_as_a_schemed_value(self):
        client = MCPClient(
            server_url="http://example.com/mcp",
            auth_type=MCPAuth.api_key,
            auth_value="Bearer looks-schemed",
        )

        assert client._get_auth_headers()["X-API-Key"] == "Bearer looks-schemed"


@pytest.mark.parametrize(
    "auth_value, scheme, expected",
    [
        ("Bearer abc", "Bearer", "abc"),
        ("bearer abc", "Bearer", "abc"),
        ("Bearer\tabc", "Bearer", "abc"),
        ("bearer\t\tabc", "Bearer", "abc"),
        ("  Bearer   abc  ", "Bearer", "abc  "),
        ("abc", "Bearer", "abc"),
        ("Bearerabc", "Bearer", "Bearerabc"),
        ("Basic abc", "Bearer", "Basic abc"),
        ("token abc", "token", "abc"),
        ("Basic abc", "Basic", "abc"),
        ("Bearer ", "Bearer", "Bearer "),
        ("Bearer   ", "Bearer", "Bearer   "),
    ],
)
def test_strip_auth_scheme(auth_value, scheme, expected):
    assert strip_auth_scheme(auth_value, scheme) == expected


@pytest.mark.parametrize(
    "auth_type, auth_value, expected",
    [
        (MCPAuth.bearer_token, "Bearer jwt", "Bearer jwt"),
        (MCPAuth.bearer_token, "jwt", "Bearer jwt"),
        (MCPAuth.api_key, "ApiKey secret", "ApiKey secret"),
        (MCPAuth.api_key, "secret", "ApiKey secret"),
        (MCPAuth.basic, "Basic dXNlcjpwYXNz", "Basic dXNlcjpwYXNz"),
    ],
)
def test_openapi_byok_auth_header_emits_exactly_one_scheme(auth_type, auth_value, expected):
    """A non-BYOK server short-circuits ``_resolve_byok_mcp_auth_header``, so this formatter also
    receives the deprecated global ``x-mcp-auth``, which is already a complete header value."""
    server = MCPServer(
        server_id="s1",
        name="openapi-server",
        url="http://example.com/mcp",
        transport=MCPTransport.http,
        auth_type=auth_type,
        spec_path="/tmp/spec.json",
    )

    assert server.is_byok is False
    assert _format_byok_openapi_auth_header(server, auth_value) == expected


def test_mcp_extra_matches_proxy_extra_and_supports_streamable_http():
    try:
        import tomllib
    except ImportError:
        tomllib = pytest.importorskip("tomli")
    from packaging.requirements import Requirement

    pyproject_path = Path(__file__).parents[3] / "pyproject.toml"
    with pyproject_path.open("rb") as f:
        project = tomllib.load(f)
    extras = project["project"]["optional-dependencies"]

    sdk2_names: Final = frozenset(("mcp", "httpx2", "pydantic"))
    mcp_extra: Final = {Requirement(req).name: req for req in extras["mcp"]}
    assert mcp_extra == {
        name: req
        for req in extras["proxy"]
        if (name := Requirement(req).name) in sdk2_names
    }

    specifier: Final = Requirement(mcp_extra["mcp"]).specifier
    assert not specifier.contains("2.2.0")
    assert specifier.contains("2.3.0")
    with (pyproject_path.parent / "uv.lock").open("rb") as f:
        locked = tomllib.load(f)
    mcp_versions: Final = [package["version"] for package in locked["package"] if package["name"] == "mcp"]
    assert len(mcp_versions) == 1
    assert specifier.contains(mcp_versions[0])


@pytest.mark.parametrize("module", ["mcp", "mcp_types", "httpx2", "httpcore2"])
def test_base_sdk_guard_rejects_mcp_dependencies(tmp_path: Path, module: str) -> None:
    import subprocess
    import sys

    (tmp_path / f"{module}.py").write_text("")
    checker = Path(__file__).parents[2] / "base_sdk_tests" / "check_base_sdk_install.py"
    result = subprocess.run(
        [
            sys.executable,
            "-S",
            "-c",
            "import runpy, sys; sys.path.insert(0, sys.argv[2]); "
            "runpy.run_path(sys.argv[1])['check_environment_is_base_only']()",
            str(checker),
            str(tmp_path),
        ],
        capture_output=True,
        text=True,
        check=False,
    )
    assert result.returncode != 0, f"base-only guard accepted installed {module}"
    assert f"{module} installed" in result.stderr


@pytest.mark.parametrize(
    "auth_type, default_header",
    [
        (MCPAuth.oauth2, "Authorization"),
        (MCPAuth.bearer_token, "Authorization"),
        (MCPAuth.api_key, "X-API-Key"),
    ],
)
def test_v1_auth_headers_default_to_the_auth_type_slot(auth_type: MCPAuth, default_header: str) -> None:
    client = MCPClient(server_url="http://up.example.com/mcp", auth_type=auth_type)
    client.update_auth_value("tok")
    assert default_header in client._get_auth_headers()


@pytest.mark.parametrize("auth_type", [MCPAuth.oauth2, MCPAuth.bearer_token, MCPAuth.api_key])
def test_v1_auth_headers_honor_the_configured_slot(auth_type: MCPAuth) -> None:
    """The v1 stack mints its own client_credentials token (oauth2_token_cache) and writes it here,
    so leaving this table hardcoded makes the knob a silent no-op for every server that resolves
    through v1 rather than the v2 resolver."""
    client = MCPClient(
        server_url="http://up.example.com/mcp",
        auth_type=auth_type,
        auth_header_name="esb-oauth",
    )
    client.update_auth_value("tok")
    headers = client._get_auth_headers()
    assert "esb-oauth" in headers
    assert "Authorization" not in headers
    assert "X-API-Key" not in headers


def test_v1_static_headers_still_win_their_own_slot():
    # extra_headers (which carries static_headers) is applied last on the v1 path, so a static
    # Authorization survives untouched while the resolved credential sits on its own header.
    client = MCPClient(
        server_url="http://up.example.com/mcp",
        auth_type=MCPAuth.oauth2,
        auth_header_name="esb-oauth",
        extra_headers={"Authorization": "Bearer static-upstream-mcp-token"},
    )
    client.update_auth_value("minted")
    headers = client._get_auth_headers()
    assert headers["esb-oauth"] == "Bearer minted"
    assert headers["Authorization"] == "Bearer static-upstream-mcp-token"


@pytest.mark.asyncio
async def test_sdk_same_origin_redirect_lists_and_calls_tools() -> None:
    def respond(request: httpx2.Request) -> httpx2.Response:
        if request.url.path == "/mcp":
            return httpx2.Response(307, headers={"Location": "/final/mcp"})
        assert request.url == "https://upstream.example.com/final/mcp"
        assert request.headers["x-upstream-token"] == "Bearer synthetic-token"
        if request.method != "POST":
            return httpx2.Response(405)
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        if not isinstance(payload, JSONRPCRequest):
            return httpx2.Response(202)
        match payload.method:
            case "initialize":
                return httpx2.Response(
                    200,
                    json={
                        "jsonrpc": "2.0",
                        "id": payload.id,
                        "result": {
                            "protocolVersion": LATEST_HANDSHAKE_VERSION,
                            "capabilities": {"tools": {}},
                            "serverInfo": {"name": "redirect-test", "version": "1"},
                        },
                    },
                )
            case "tools/list":
                return httpx2.Response(
                    200,
                    json={
                        "jsonrpc": "2.0",
                        "id": payload.id,
                        "result": {"tools": [{"name": "add", "inputSchema": {"type": "object"}}]},
                    },
                )
            case "tools/call":
                assert payload.params is not None
                assert payload.params["name"] == "add"
                assert payload.params["arguments"] == {"a": 2, "b": 3}
                return httpx2.Response(
                    200,
                    json={
                        "jsonrpc": "2.0",
                        "id": payload.id,
                        "result": {"content": [{"type": "text", "text": "5"}], "isError": False},
                    },
                )
            case _:
                pytest.fail(f"Unexpected MCP request: {payload.method}")

    responder: Final = Mock(side_effect=respond)
    client: Final = _MockTransportClient(
        responder,
        server_url="https://upstream.example.com/mcp",
        auth_type=MCPAuth.bearer_token,
        auth_value="synthetic-token",
        auth_header_name="x-upstream-token",
        timeout=5,
    )
    with anyio.fail_after(10):
        tools: Final = await client.list_tools(raise_on_error=True)
        result: Final = await client.call_tool(
            CallToolRequestParams(name="add", arguments={"a": 2, "b": 3}), raise_on_error=True
        )
    assert [tool.name for tool in tools] == ["add"]
    assert result.is_error is False
    assert len(result.content) == 1
    assert result.content[0].type == "text"
    assert result.content[0].text == "5"
    assert any(call.args[0].url.path == "/mcp" for call in responder.call_args_list)


@pytest.mark.asyncio
@pytest.mark.parametrize("operation", ("list", "call"))
async def test_sdk_cross_origin_redirect_never_contacts_destination(operation: str) -> None:
    responder: Final = Mock(
        return_value=httpx2.Response(307, headers={"Location": "https://destination.example.com/mcp"})
    )
    client: Final = _MockTransportClient(
        responder,
        server_url="https://upstream.example.com/mcp",
        auth_type=MCPAuth.bearer_token,
        auth_value="synthetic-token",
        auth_header_name="x-upstream-token",
        timeout=5,
    )
    pending_operation: Final = (
        client.list_tools(raise_on_error=True)
        if operation == "list"
        else client.call_tool(CallToolRequestParams(name="add", arguments={"a": 2, "b": 3}), raise_on_error=True)
    )
    with anyio.fail_after(10), pytest.raises(MCPError):
        await pending_operation
    assert responder.call_count == 1
    request: Final = responder.call_args.args[0]
    assert request.method == "POST"
    assert request.url == "https://upstream.example.com/mcp"
    assert request.headers["x-upstream-token"] == "Bearer synthetic-token"
    assert all(call.args[0].url.host != "destination.example.com" for call in responder.call_args_list)


@pytest.mark.asyncio
async def test_a_custom_credential_header_is_stripped_when_a_redirect_crosses_origin():
    """httpx drops Authorization across origins but keeps every other header, so a credential the
    operator moved to its own slot would be replayed to whatever host the upstream redirects to.
    Verified against real httpx redirect handling, not a hand-built request.
    """
    seen: list[tuple[str, str]] = []

    def handler(request: httpx2.Request) -> httpx2.Response:
        seen.append((request.url.host, request.headers.get("esb-oauth", "<stripped>")))
        if request.url.host == "upstream.example.com":
            return httpx2.Response(302, headers={"Location": "https://attacker.example.com/collect"})
        return httpx2.Response(200)

    client = MCPClient(
        server_url="https://upstream.example.com/mcp",
        auth_type=MCPAuth.oauth2,
        auth_header_name="esb-oauth",
    )
    client.update_auth_value("minted-token")
    factory = client._create_httpx_client_factory()
    async with factory(headers=client._get_auth_headers(), timeout=None) as http_client:
        http_client._transport = httpx2.MockTransport(handler)
        await http_client.get("https://upstream.example.com/mcp")

    assert seen[0] == ("upstream.example.com", "Bearer minted-token")
    assert seen[1] == ("attacker.example.com", "<stripped>")


@pytest.mark.asyncio
async def test_authorization_is_left_to_httpx_and_needs_no_guard():
    # The default slot is already protected by httpx, so the client must not install a guard for it
    # and must not interfere with the ordinary Authorization path.
    url = "https://upstream.example.com/mcp"
    from litellm.types.mcp import credential_redirect_hook

    def guard_for(client: MCPClient):
        return credential_redirect_hook(client.server_url, client._credential_slot)

    assert guard_for(MCPClient(server_url=url, auth_type=MCPAuth.oauth2)) is None
    assert guard_for(MCPClient(server_url=url, resolved_auth=StaticHeaderAuth("Bearer x"))) is None
    # a v2 resolver slot is discovered from the auth object, without the caller naming it again
    custom = MCPClient(server_url=url, resolved_auth=StaticHeaderAuth("Bearer x", header_name="esb-oauth"))
    assert guard_for(custom) is not None
    # and the same answer arrives via the v1 configured slot
    assert guard_for(MCPClient(server_url=url, auth_header_name="ESB-OAuth")) is not None


def test_an_injected_header_cannot_shadow_the_configured_credential_slot():
    """The v2 path drops a colliding injected header so the resolved credential wins its slot. The
    v1 path applies extra_headers last, so without this it silently sends the injected value and the
    upstream rejects a credential the gateway thought it had sent.
    """
    client = MCPClient(
        server_url="https://upstream.example.com/mcp",
        auth_type=MCPAuth.oauth2,
        auth_header_name="esb-oauth",
        extra_headers={"esb-oauth": "Bearer injected", "X-Trace": "keep"},
    )
    client.update_auth_value("minted-token")
    headers = client._get_auth_headers()
    assert headers["esb-oauth"] == "Bearer minted-token"
    assert headers["X-Trace"] == "keep"


def test_without_a_configured_slot_the_existing_precedence_is_unchanged():
    # extra_headers winning over authentication_token is long-standing v1 behavior; the fix above
    # must apply only to the slot the operator explicitly named.
    client = MCPClient(
        server_url="https://upstream.example.com/mcp",
        auth_type=MCPAuth.oauth2,
        extra_headers={"Authorization": "Bearer injected"},
    )
    client.update_auth_value("minted-token")
    assert client._get_auth_headers()["Authorization"] == "Bearer injected"


_REDIRECT_CASES = [
    ("https://upstream.example.com/mcp", "https://upstream.example.com/other"),  # same origin
    ("https://upstream.example.com/mcp", "https://upstream.example.com:443/other"),  # explicit default port
    ("https://upstream.example.com/mcp", "https://attacker.example.com/collect"),  # different host
    ("https://upstream.example.com/mcp", "http://upstream.example.com/collect"),  # scheme downgrade
    ("https://upstream.example.com/mcp", "https://upstream.example.com:8443/other"),  # different port
    ("https://upstream.example.com/mcp", "https://sub.upstream.example.com/x"),  # different host
    ("http://upstream.example.com/mcp", "https://upstream.example.com/other"),  # http -> https upgrade
    ("http://upstream.example.com/mcp", "http://upstream.example.com/other"),  # same origin, plain http
]


@pytest.mark.parametrize("start,target", _REDIRECT_CASES)
@pytest.mark.asyncio
async def test_the_guard_agrees_with_httpx_about_authorization(start: str, target: str) -> None:
    """Our custom slot must be dropped on exactly the redirects where httpx drops Authorization.

    The rule is mirrored rather than imported, so this drives real httpx and compares the two
    outcomes. A future httpx that changes its redirect rule reds here instead of silently leaving
    the custom slot forwarded where Authorization is not (or stripped where it is not needed).
    """
    seen: list[tuple[str, str, str]] = []

    def handler(request: httpx2.Request) -> httpx2.Response:
        seen.append(
            (
                str(request.url),
                request.headers.get("authorization", "<stripped>"),
                request.headers.get("esb-oauth", "<stripped>"),
            )
        )
        if str(request.url) == start:
            return httpx2.Response(302, headers={"Location": target})
        return httpx2.Response(200)

    client = MCPClient(server_url=start, auth_type=MCPAuth.oauth2, auth_header_name="esb-oauth")
    factory = client._create_httpx_client_factory()
    async with factory(headers={"Authorization": "Bearer AUTH", "esb-oauth": "Bearer ESB"}, timeout=None) as http:
        http._transport = httpx2.MockTransport(handler)
        await http.get(start)

    _url, authorization, esb = seen[-1]
    assert (authorization == "<stripped>") == (esb == "<stripped>"), (
        f"httpx and the guard disagree for {target}: authorization={authorization!r} esb-oauth={esb!r}"
    )


def test_a_differently_cased_injected_header_cannot_shadow_the_slot() -> None:
    # HTTP header names are case-insensitive and v2 drops the collision case-insensitively, so an
    # exact-key check here would leave both spellings in the dict and let the injected value win.
    client = MCPClient(
        server_url="https://upstream.example.com/mcp",
        auth_type=MCPAuth.oauth2,
        auth_header_name="esb-oauth",
        extra_headers={"ESB-OAuth": "Bearer injected", "X-Trace": "keep"},
    )
    client.update_auth_value("minted-token")
    headers = client._get_auth_headers()
    assert [v for k, v in headers.items() if k.lower() == "esb-oauth"] == ["Bearer minted-token"]
    assert headers["X-Trace"] == "keep"


@pytest.mark.asyncio
@pytest.mark.parametrize(
    ("content_type", "body", "expected_type"),
    [
        ("text/html", b"<html>secret-page</html>", MCPError),
        ("application/json", b"secret-invalid-json", MCPError),
        ("application/json", b"", MCPError),
        ("application/json", b'{"secret":"invalid-rpc"}', MCPError),
        ("application/json", b'{"jsonrpc":"2.0","id":0}', MCPError),
        ("application/json", b'{"jsonrpc":"2.0","id":0,"result":{"secret":"bad-schema"}}', ValidationError),
    ],
)
async def test_invalid_http_response_surfaces_without_waiting_for_timeout(
    content_type: str, body: bytes, expected_type: type[Exception]
) -> None:
    from litellm.proxy._experimental.mcp_server.rest_endpoints import _connection_error_message

    def respond(request: httpx2.Request) -> httpx2.Response:
        if expected_type is ValidationError:
            return httpx2.Response(200, json={**json.loads(body), "id": json.loads(request.content)["id"]})
        return httpx2.Response(200, headers={"Content-Type": content_type}, content=body)

    async with httpx2.AsyncClient(transport=httpx2.MockTransport(respond)) as http_client:
        client: Final = MCPClient(server_url="https://example.com/mcp", timeout=30)
        with pytest.raises(expected_type) as caught:
            await asyncio.wait_for(
                client._execute_session_operation(
                    streamable_http_client(client.server_url, http_client=http_client),
                    lambda session: session.list_tools(),
                ),
                timeout=3,
            )

    message: Final = _connection_error_message(caught.value, client.server_url, 30)
    assert "unsupported content type" in message or "invalid MCP response" in message
    assert "secret" not in message
    assert "timed out" not in message


@pytest.mark.asyncio
@pytest.mark.parametrize("status_code", [200, 401, 403, 429, 503])
async def test_http_response_handler_preserves_success_and_http_errors(status_code: int) -> None:
    def respond(request: httpx2.Request) -> httpx2.Response:
        if request.method == "DELETE":
            return httpx2.Response(200)
        payload: Final = json.loads(request.content)
        if "id" not in payload:
            return httpx2.Response(202)
        result: Final = (
            {
                "protocolVersion": payload["params"]["protocolVersion"],
                "capabilities": {},
                "serverInfo": {"name": "test", "version": "1"},
            }
            if payload["method"] == "initialize"
            else {"tools": []}
        )
        return httpx2.Response(status_code, json={"jsonrpc": "2.0", "id": payload["id"], "result": result})

    client: Final = MCPClient(server_url="https://example.com/mcp", timeout=30)
    async with client._create_httpx_client_factory(transport=httpx2.MockTransport(respond))() as http_client:
        operation: Final = client._execute_session_operation(
            streamable_http_client(client.server_url, http_client=http_client),
            lambda session: session.list_tools(),
            http_client=http_client,
        )
        if status_code == 200:
            result: Final = await asyncio.wait_for(operation, timeout=3)
            assert result.tools == []
        else:
            with pytest.raises(httpx2.HTTPStatusError) as caught:
                await asyncio.wait_for(operation, timeout=3)
            assert caught.value.response.status_code == status_code


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "failure_method,status_code",
    [
        ("notifications/initialized", 429),
        ("notifications/initialized", 503),
        ("tools/list", 429),
    ],
)
async def test_list_tools_classifies_upstream_status_for_post_failures(failure_method: str, status_code: int) -> None:
    client: Final = _MockTransportClient(
        _post_status_responder(failure_method, status_code),
        server_url="https://example.com/mcp",
    )
    with pytest.raises(httpx2.HTTPStatusError) as caught:
        await client.list_tools(raise_on_error=True)

    assert classify_list_exception(caught.value) == ServerListFault(tag="upstream_error", status_code=status_code)
    rate_limit: Final = upstream_rate_limit(caught.value)
    if status_code == 429:
        assert rate_limit is not None
        assert rate_limit.retry_after_seconds == 7
    else:
        assert rate_limit is None


@pytest.mark.asyncio
async def test_http_status_check_allows_auth_refresh_before_rejecting() -> None:
    from litellm.proxy._experimental.mcp_server.outbound_credentials.client_credentials import (
        ClientCredentialsBearerAuth,
    )

    seen = []

    async def refresh(failed):
        assert failed == "stale"
        return "fresh"

    def respond(request):
        seen.append(request.headers["authorization"])
        return httpx2.Response(401 if len(seen) == 1 else 200, json={"ok": True})

    from litellm.proxy._experimental.mcp_server.outbound_credentials.types import ClientCredentialsConfig

    auth = ClientCredentialsBearerAuth("stale", refresh, ClientCredentialsConfig())
    client = MCPClient(server_url="https://example.com/mcp", resolved_auth=auth)
    async with client._create_httpx_client_factory(transport=httpx2.MockTransport(respond))() as http_client:
        response = await http_client.post(client.server_url, json={"method": "tools/list"})
        assert response.status_code == 200
    assert seen == ["Bearer stale", "Bearer fresh"]


@pytest.mark.asyncio
async def test_http_response_handler_preserves_notifications_and_tool_listing() -> None:
    notification: Final = {
        "jsonrpc": "2.0",
        "method": "notifications/message",
        "params": {"level": "info", "data": "Listing tools"},
    }
    logging_callback: Final = AsyncMock()

    def respond(request: httpx2.Request) -> httpx2.Response:
        if request.method == "DELETE":
            return httpx2.Response(200)
        payload: Final = json.loads(request.content)
        if "id" not in payload:
            return httpx2.Response(202)
        if payload["method"] == "initialize":
            return httpx2.Response(
                200,
                json={
                    "jsonrpc": "2.0",
                    "id": payload["id"],
                    "result": {
                        "protocolVersion": payload["params"]["protocolVersion"],
                        "capabilities": {"logging": {}, "tools": {}},
                        "serverInfo": {"name": "test", "version": "1"},
                    },
                },
            )
        response: Final = {
            "jsonrpc": "2.0",
            "id": payload["id"],
            "result": {"tools": [{"name": "search", "inputSchema": {"type": "object"}}]},
        }
        return httpx2.Response(
            200,
            headers={"Content-Type": "text/event-stream"},
            content="".join(f"event: message\ndata: {json.dumps(message)}\n\n" for message in (notification, response)),
        )

    async with httpx2.AsyncClient(transport=httpx2.MockTransport(respond)) as http_client:
        client: Final = MCPClient(server_url="https://example.com/mcp", timeout=30, logging_callback=logging_callback)
        result: Final = await asyncio.wait_for(
            client._execute_session_operation(
                streamable_http_client(client.server_url, http_client=http_client), lambda session: session.list_tools()
            ),
            timeout=3,
        )

    assert [tool.name for tool in result.tools] == ["search"]
    logging_callback.assert_awaited_once_with(LoggingMessageNotificationParams(level="info", data="Listing tools"))


@pytest.mark.asyncio
async def test_invalid_tool_list_schema_is_identified_as_an_upstream_response() -> None:
    from litellm.proxy._experimental.mcp_server.rest_endpoints import _connection_error_message

    def respond(request: httpx2.Request) -> httpx2.Response:
        if request.method == "DELETE":
            return httpx2.Response(200)
        payload: Final = json.loads(request.content)
        if "id" not in payload:
            return httpx2.Response(202)
        result: Final = (
            {
                "protocolVersion": payload["params"]["protocolVersion"],
                "capabilities": {},
                "serverInfo": {"name": "test", "version": "1"},
            }
            if payload["method"] == "initialize"
            else {"tools": "secret-invalid-tools"}
        )
        return httpx2.Response(200, json={"jsonrpc": "2.0", "id": payload["id"], "result": result})

    async with httpx2.AsyncClient(transport=httpx2.MockTransport(respond)) as http_client:
        client: Final = MCPClient(server_url="https://example.com/mcp", timeout=30)
        with pytest.raises(ValidationError) as caught:
            await asyncio.wait_for(
                client._execute_session_operation(
                    streamable_http_client(client.server_url, http_client=http_client),
                    lambda session: session.list_tools(),
                ),
                timeout=3,
            )

    message: Final = _connection_error_message(caught.value, client.server_url, 30)
    assert "invalid MCP response" in message
    assert "secret" not in message


class _DiagnosticSSEStream(httpx2.AsyncByteStream):
    def __init__(self, messages: asyncio.Queue[bytes | Exception | None]) -> None:
        self.messages = messages

    async def __aiter__(self) -> AsyncIterator[bytes]:
        yield b"event: endpoint\ndata: /messages\n\n"
        while True:
            message: Final = await self.messages.get()
            if message is None:
                return
            if isinstance(message, Exception):
                raise message
            yield b"event: message\ndata: " + message + b"\n\n"


_DIAGNOSTIC_STDIO_SERVER: Final = """
import json, sys
mode, failure_method = sys.argv[1:]
for line in sys.stdin:
    request = json.loads(line)
    if "method" not in request or "id" not in request:
        continue
    if request["method"] == failure_method:
        if mode == "bad-json":
            print("secret-invalid-json", flush=True)
            continue
        if mode == "closed":
            sys.exit(0)
        if mode == "silent":
            print(json.dumps({"jsonrpc": "2.0", "method": "notifications/message", "params": {"level": "info", "data": "Waiting"}}), flush=True)
            continue
    if request["method"] == "initialize":
        result = {"protocolVersion": request["params"]["protocolVersion"], "capabilities": {"tools": {}, "logging": {}}, "serverInfo": {"name": "diagnostic", "version": "1"}}
    elif request["method"] == "tools/list":
        print(json.dumps({"jsonrpc": "2.0", "method": "notifications/message", "params": {"level": "info", "data": "Listing tools"}}), flush=True)
        print(json.dumps({"jsonrpc": "2.0", "id": "unmatched", "result": {}}), flush=True)
        print(json.dumps({"jsonrpc": "2.0", "id": "server-ping", "method": "ping"}), flush=True)
        result = {"tools": [{"name": "ping", "inputSchema": {"type": "object"}}]}
    else:
        result = {"content": [{"type": "text", "text": "pong"}], "isError": False}
    print(json.dumps({"jsonrpc": "2.0", "id": request["id"], "result": result}), flush=True)
"""


def _diagnostic_transport(transport: MCPTransport, mode: str, failure_method: str) -> _TransportContext:
    from mcp import StdioServerParameters
    from mcp.client.sse import sse_client
    from mcp.client.stdio import stdio_client

    if transport == MCPTransport.stdio:
        return stdio_client(
            StdioServerParameters(
                command=sys.executable, args=["-u", "-c", _DIAGNOSTIC_STDIO_SERVER, mode, failure_method]
            )
        )
    messages: Final[asyncio.Queue[bytes | Exception | None]] = asyncio.Queue()

    async def respond(request: httpx2.Request) -> httpx2.Response:
        if request.method == "GET":
            return httpx2.Response(
                200, headers={"Content-Type": "text/event-stream"}, stream=_DiagnosticSSEStream(messages)
            )
        payload: Final = json.loads(request.content)
        if "method" not in payload or "id" not in payload:
            return httpx2.Response(202)
        if payload["method"] == failure_method and mode != "ok":
            if mode == "bad-json":
                await messages.put(b"secret-invalid-json")
            elif mode == "io-error":
                await messages.put(httpx2.ReadError("secret-read-error"))
            elif mode == "closed":
                await messages.put(None)
            elif mode == "silent":
                await messages.put(
                    b'{"jsonrpc":"2.0","method":"notifications/message","params":{"level":"info","data":"Waiting"}}'
                )
            return httpx2.Response(202)
        if payload["method"] == "tools/list":
            for message in (
                {
                    "jsonrpc": "2.0",
                    "method": "notifications/message",
                    "params": {"level": "info", "data": "Listing tools"},
                },
                {"jsonrpc": "2.0", "id": "unmatched", "result": {}},
                {"jsonrpc": "2.0", "id": "server-ping", "method": "ping"},
            ):
                await messages.put(json.dumps(message).encode())
        result: Final = (
            {
                "protocolVersion": payload["params"]["protocolVersion"],
                "capabilities": {"tools": {}, "logging": {}},
                "serverInfo": {"name": "diagnostic", "version": "1"},
            }
            if payload["method"] == "initialize"
            else {"tools": [{"name": "ping", "inputSchema": {"type": "object"}}]}
            if payload["method"] == "tools/list"
            else {"content": [{"type": "text", "text": "pong"}], "isError": False}
        )
        await messages.put(json.dumps({"jsonrpc": "2.0", "id": payload["id"], "result": result}).encode())
        return httpx2.Response(202)

    def factory(
        headers: dict[str, str] | None = None,
        timeout: httpx2.Timeout | None = None,
        auth: httpx2.Auth | None = None,
    ) -> httpx2.AsyncClient:
        return httpx2.AsyncClient(transport=httpx2.MockTransport(respond), headers=headers, timeout=timeout, auth=auth)

    return sse_client("https://example.com/sse", httpx_client_factory=factory)


@pytest.mark.asyncio
@pytest.mark.parametrize("transport", [MCPTransport.sse, MCPTransport.stdio])
@pytest.mark.parametrize("failure_method", ["initialize", "tools/list"])
async def test_transport_parsing_failure_is_preserved(transport: MCPTransport, failure_method: str) -> None:
    client: Final = MCPClient(server_url="https://example.com/sse", transport_type=transport, timeout=0.2)
    with pytest.raises(ValidationError):
        await asyncio.wait_for(
            client._execute_session_operation(
                _diagnostic_transport(transport, "bad-json", failure_method), lambda session: session.list_tools()
            ),
            timeout=3,
        )


def test_sse_read_failure_is_preserved() -> None:
    loop: Final = AutojumpClockLoop()

    async def run() -> None:
        client: Final = MCPClient(server_url="https://example.com/sse", transport_type=MCPTransport.sse, timeout=0.2)
        with pytest.raises(httpx2.ReadError, match="secret-read-error"):
            await asyncio.wait_for(
                client._execute_session_operation(
                    _diagnostic_transport(MCPTransport.sse, "io-error", "tools/list"),
                    lambda session: session.list_tools(),
                ),
                timeout=3,
            )

    try:
        loop.run_until_complete(run())
    finally:
        loop.run_until_complete(loop.shutdown_asyncgens())
        loop.run_until_complete(loop.shutdown_default_executor())
        loop.close()


@pytest.mark.asyncio
@pytest.mark.parametrize("protocol_version", ["auto", "2025-06-18"])
@pytest.mark.parametrize("transport", [MCPTransport.sse, MCPTransport.stdio])
@pytest.mark.parametrize("mode", ["ok", "closed", "silent"])
async def test_transport_completion_and_normal_messages(
    transport: MCPTransport, mode: str, protocol_version: str
) -> None:
    from mcp import ClientSession

    from litellm.proxy._experimental.mcp_server.rest_endpoints import _connection_error_message

    logging_callback: Final = AsyncMock()
    read_timeout: Final = 0.2 if mode == "silent" else 30
    client: Final = MCPClient(
        server_url="https://example.com/sse",
        transport_type=transport,
        timeout=read_timeout,
        logging_callback=logging_callback,
        protocol_version=protocol_version,
    )

    async def operation(session: ClientSession) -> CallToolResult:
        tools: Final = await session.list_tools()
        assert [tool.name for tool in tools.tools] == ["ping"]
        return await session.call_tool("ping", {})

    pending: Final = client._execute_session_operation(_diagnostic_transport(transport, mode, "tools/list"), operation)
    if mode == "ok":
        result: Final = await asyncio.wait_for(pending, timeout=3)
        assert result.is_error is False
        assert result.content[0].text == "pong"
        logging_callback.assert_awaited_once_with(LoggingMessageNotificationParams(level="info", data="Listing tools"))
    else:
        with pytest.raises(MCPError) as caught:
            await asyncio.wait_for(pending, timeout=3)
        if mode == "closed":
            assert "connection was closed" in _connection_error_message(caught.value, client.server_url, read_timeout)
        else:
            assert isinstance(as_mcp_read_timeout(caught.value), TimeoutError)


@pytest.mark.asyncio
@pytest.mark.parametrize("transport", [MCPTransport.sse, MCPTransport.stdio])
async def test_transport_cancellation_cleans_up_a_pending_request(transport: MCPTransport) -> None:
    ready: Final = asyncio.Event()

    async def on_log(message: LoggingMessageNotificationParams) -> None:
        if message.data == "Waiting":
            ready.set()

    client: Final = MCPClient(
        server_url="https://example.com/sse", transport_type=transport, timeout=30, logging_callback=on_log
    )
    task: Final = asyncio.create_task(
        client._execute_session_operation(
            _diagnostic_transport(transport, "silent", "tools/list"), lambda session: session.list_tools()
        )
    )
    try:
        await asyncio.wait_for(ready.wait(), timeout=3)
    finally:
        task.cancel()
        with pytest.raises(asyncio.CancelledError):
            await asyncio.wait_for(task, timeout=3)


class _InterruptedHTTPBody(httpx2.AsyncByteStream):
    async def __aiter__(self) -> AsyncIterator[bytes]:
        yield b'{"jsonrpc":'
        raise httpx2.RemoteProtocolError("secret-incomplete-response")


@pytest.mark.asyncio
async def test_interrupted_http_response_preserves_the_transport_failure() -> None:
    def respond(request: httpx2.Request) -> httpx2.Response:
        return httpx2.Response(200, headers={"Content-Type": "application/json"}, stream=_InterruptedHTTPBody())

    client: Final = MCPClient(server_url="https://example.com/mcp", timeout=30)
    async with client._create_httpx_client_factory(transport=httpx2.MockTransport(respond))() as http_client:
        with pytest.raises(httpx2.RemoteProtocolError, match="secret-incomplete-response"):
            await asyncio.wait_for(
                client._execute_session_operation(
                    streamable_http_client(client.server_url, http_client=http_client),
                    lambda session: session.list_tools(),
                    http_client=http_client,
                ),
                timeout=3,
            )


@pytest.mark.asyncio
async def test_empty_http_event_stream_uses_the_existing_request_deadline() -> None:
    def respond(request: httpx2.Request) -> httpx2.Response:
        return httpx2.Response(200, headers={"Content-Type": "text/event-stream"}, content=b"")

    async with httpx2.AsyncClient(transport=httpx2.MockTransport(respond)) as http_client:
        client: Final = MCPClient(server_url="https://example.com/mcp", timeout=0.2)
        with pytest.raises(MCPError) as caught:
            await asyncio.wait_for(
                client._execute_session_operation(
                    streamable_http_client(client.server_url, http_client=http_client),
                    lambda session: session.list_tools(),
                ),
                timeout=3,
            )
    assert caught.value.error.code == CONNECTION_CLOSED
    assert "SSE stream ended" in caught.value.error.message


@pytest.mark.asyncio
@pytest.mark.parametrize("method", ("prompts/list", "resources/list", "resources/templates/list"))
@pytest.mark.parametrize(
    "outcome",
    (
        "absent",
        "other_capability",
        "supported",
        "method_not_found",
        "internal_error",
        "unauthorized",
        "timeout",
        "initialize_not_found",
    ),
)
@pytest.mark.parametrize("raise_on_error", (False, True))
async def test_optional_discovery_capabilities_and_errors(
    method: str, outcome: str, caplog: pytest.LogCaptureFixture, raise_on_error: bool
) -> None:
    import logging
    from unittest.mock import Mock

    from mcp.types import JSONRPCRequest

    capability: Final = "prompts" if method == "prompts/list" else "resources"
    field: Final = {
        "prompts/list": "prompts",
        "resources/list": "resources",
        "resources/templates/list": "resourceTemplates",
    }[method]
    advertised: Final = "resources" if capability == "prompts" else "prompts"
    entry: Final = {
        "prompts/list": {"name": "example"},
        "resources/list": {"name": "example", "uri": "test://example"},
        "resources/templates/list": {"name": "example", "uriTemplate": "test://{name}"},
    }[method]

    def respond(request: httpx2.Request) -> httpx2.Response:
        if request.method == "DELETE":
            return httpx2.Response(200)
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        if not isinstance(payload, JSONRPCRequest):
            return httpx2.Response(202)
        if outcome == "initialize_not_found":
            return httpx2.Response(
                200,
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "error": {"code": -32601, "message": "Initialization rejected"},
                },
            )
        if payload.method == "initialize":
            return httpx2.Response(
                200,
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "result": {
                        "protocolVersion": (payload.params or {})["protocolVersion"],
                        "capabilities": {}
                        if outcome == "absent"
                        else {advertised if outcome == "other_capability" else capability: {}},
                        "serverInfo": {"name": "discovery", "version": "1"},
                    },
                },
            )
        if outcome == "timeout":
            raise httpx2.ReadTimeout("Optional list timed out", request=request)
        if outcome == "unauthorized":
            return httpx2.Response(401)
        if outcome in ("method_not_found", "internal_error", "absent", "other_capability"):
            return httpx2.Response(
                200,
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "error": {
                        "code": -32603 if outcome == "internal_error" else -32601,
                        "message": "Optional list rejected",
                    },
                },
            )
        return httpx2.Response(200, json={"jsonrpc": "2.0", "id": payload.id, "result": {field: [entry]}})

    responder: Final = Mock(side_effect=respond)
    caplog.set_level(logging.DEBUG, logger="LiteLLM")
    client: Final = _MockTransportClient(responder, server_url="https://example.com/mcp")
    operation: Final = {
        "prompts/list": client.list_prompts,
        "resources/list": client.list_resources,
        "resources/templates/list": client.list_resource_templates,
    }[method]
    if raise_on_error and outcome in ("internal_error", "unauthorized", "timeout", "initialize_not_found"):
        with pytest.raises((MCPError, httpx2.HTTPError)):
            await operation(raise_on_error=True)
        return
    result: Final = await operation(raise_on_error=raise_on_error)

    requests: Final = tuple(
        _JSONRPC_MESSAGE_ADAPTER.validate_json(call.args[0].content)
        for call in responder.call_args_list
        if call.args[0].method == "POST"
    )
    assert sum(isinstance(request, JSONRPCRequest) and request.method == method for request in requests) == (
        0 if outcome in ("absent", "other_capability", "initialize_not_found") else 1
    )
    assert [item.name for item in result] == (["example"] if outcome == "supported" else [])
    failures: Final = tuple(
        record for record in caplog.records if record.name == "LiteLLM" and record.levelno >= logging.WARNING
    )
    if outcome in ("internal_error", "unauthorized", "timeout", "initialize_not_found"):
        assert any(record.levelno == logging.ERROR and "failed" in record.message for record in failures)
    else:
        assert failures == ()
    if outcome == "method_not_found":
        assert any(
            record.levelno == logging.DEBUG and "Optional list rejected" in record.message for record in caplog.records
        )


@pytest.mark.asyncio
@pytest.mark.parametrize("supports_first", (True, False))
async def test_optional_discovery_uses_each_sessions_capabilities(supports_first: bool) -> None:
    from unittest.mock import Mock

    from mcp.types import JSONRPCRequest

    capabilities: Final = iter(({"resources": {}}, {}) if supports_first else ({}, {"resources": {}}))

    def respond(request: httpx2.Request) -> httpx2.Response:
        if request.method == "DELETE":
            return httpx2.Response(200)
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        if not isinstance(payload, JSONRPCRequest):
            return httpx2.Response(202)
        result: Final = (
            {
                "protocolVersion": (payload.params or {})["protocolVersion"],
                "capabilities": next(capabilities),
                "serverInfo": {"name": "changing", "version": "1"},
            }
            if payload.method == "initialize"
            else {"resources": [{"name": "example", "uri": "test://example"}]}
        )
        return httpx2.Response(200, json={"jsonrpc": "2.0", "id": payload.id, "result": result})

    responder: Final = Mock(side_effect=respond)
    client: Final = _MockTransportClient(responder, server_url="https://example.com/mcp")
    first: Final = await client.list_resources()
    second: Final = await client.list_resources()

    assert [item.name for item in first] == (["example"] if supports_first else [])
    assert [item.name for item in second] == ([] if supports_first else ["example"])
    requests: Final = tuple(
        _JSONRPC_MESSAGE_ADAPTER.validate_json(call.args[0].content)
        for call in responder.call_args_list
        if call.args[0].method == "POST"
    )
    assert sum(isinstance(request, JSONRPCRequest) and request.method == "resources/list" for request in requests) == 1


@pytest.mark.asyncio
@pytest.mark.parametrize("method", ("prompts/list", "resources/list", "resources/templates/list"))
async def test_optional_discovery_preserves_cancellation(method: str) -> None:
    from mcp.types import JSONRPCRequest

    ready: Final = asyncio.Event()
    pending: Final = asyncio.Event()

    async def respond(request: httpx2.Request) -> httpx2.Response:
        if request.method == "DELETE":
            return httpx2.Response(200)
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        if not isinstance(payload, JSONRPCRequest):
            return httpx2.Response(202)
        if payload.method == "initialize":
            return httpx2.Response(
                200,
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "result": {
                        "protocolVersion": (payload.params or {})["protocolVersion"],
                        "capabilities": {"resources": {}, "prompts": {}},
                        "serverInfo": {"name": "pending", "version": "1"},
                    },
                },
            )
        if not (payload.params or {}).get("cursor"):
            field: Final = {
                "prompts/list": "prompts",
                "resources/list": "resources",
                "resources/templates/list": "resourceTemplates",
            }[method]
            return httpx2.Response(
                200, json={"jsonrpc": "2.0", "id": payload.id, "result": {field: [], "nextCursor": "pending-page"}}
            )
        ready.set()
        await pending.wait()
        return httpx2.Response(202)

    client: Final = _MockTransportClient(respond, server_url="https://example.com/mcp")
    operation: Final = {
        "prompts/list": client.list_prompts,
        "resources/list": client.list_resources,
        "resources/templates/list": client.list_resource_templates,
    }[method]
    task: Final = asyncio.create_task(operation())
    try:
        await asyncio.wait_for(ready.wait(), timeout=3)
    finally:
        task.cancel()
        with pytest.raises(asyncio.CancelledError):
            await asyncio.wait_for(task, timeout=3)


@pytest.mark.asyncio
@pytest.mark.parametrize("method", ("prompts/list", "resources/list", "resources/templates/list"))
@pytest.mark.parametrize("session_id", (None, "pagination-session"))
@pytest.mark.parametrize("empty_middle", (False, True))
async def test_optional_discovery_collects_all_pages(method: str, session_id: str | None, empty_middle: bool) -> None:
    from mcp.types import Prompt, PromptArgument, Resource, ResourceTemplate

    field: Final = {
        "prompts/list": "prompts",
        "resources/list": "resources",
        "resources/templates/list": "resourceTemplates",
    }[method]
    entries: Final = tuple(
        {
            "prompts/list": Prompt(
                name=f"item-{index}",
                description="prompt description",
                arguments=[PromptArgument(name="query", required=True)],
            ),
            "resources/list": Resource(
                name=f"item-{index}",
                uri=f"test://item/{index}",
                mime_type="text/plain",
                description="resource description",
            ),
            "resources/templates/list": ResourceTemplate(
                name=f"item-{index}", uri_template=f"test://item/{index}/{{query}}", mime_type="text/plain"
            ),
        }[method]
        for index in range(5)
    )

    def respond(request: httpx2.Request) -> httpx2.Response:
        if request.method == "GET":
            return httpx2.Response(405)
        if request.method == "DELETE":
            return httpx2.Response(200)
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        if not isinstance(payload, JSONRPCRequest):
            return httpx2.Response(202)
        if payload.method == "initialize":
            return httpx2.Response(
                200,
                headers={"mcp-session-id": session_id} if session_id else {},
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "result": {
                        "protocolVersion": (payload.params or {})["protocolVersion"],
                        "capabilities": {"prompts": {}, "resources": {}},
                        "serverInfo": {"name": "paged", "version": "1"},
                    },
                },
            )
        assert payload.method == method
        assert request.headers.get("mcp-session-id") == session_id
        cursor: Final = (payload.params or {}).get("cursor")
        assert cursor in (None, "opaque:/second+page", "opaque:/last+page")
        page: Final = (
            entries[:3] if cursor is None else (() if empty_middle and cursor == "opaque:/second+page" else entries[3:])
        )
        next_cursor: Final = (
            "opaque:/second+page"
            if cursor is None
            else "opaque:/last+page"
            if empty_middle and cursor == "opaque:/second+page"
            else ""
        )
        return httpx2.Response(
            200,
            json={
                "jsonrpc": "2.0",
                "id": payload.id,
                "result": {
                    field: [item.model_dump(mode="json", by_alias=True) for item in page],
                    "nextCursor": next_cursor,
                },
            },
        )

    responder: Final = Mock(side_effect=respond)
    client: Final = _MockTransportClient(responder, server_url="https://example.com/mcp")
    operation: Final = {
        "prompts/list": client.list_prompts,
        "resources/list": client.list_resources,
        "resources/templates/list": client.list_resource_templates,
    }[method]
    assert await operation(raise_on_error=True) == list(entries)
    requests: Final = tuple(
        _JSONRPC_MESSAGE_ADAPTER.validate_json(call.args[0].content)
        for call in responder.call_args_list
        if call.args[0].method == "POST"
    )
    assert sum(isinstance(request, JSONRPCRequest) and request.method == "initialize" for request in requests) == 1
    assert tuple(
        (request.params or {}).get("cursor")
        for request in requests
        if isinstance(request, JSONRPCRequest) and request.method == method
    ) == ((None, "opaque:/second+page", "opaque:/last+page") if empty_middle else (None, "opaque:/second+page"))
    assert sum(call.args[0].method == "DELETE" for call in responder.call_args_list) == (1 if session_id else 0)


@pytest.mark.parametrize("method", ("prompts/list", "resources/list", "resources/templates/list"))
@pytest.mark.parametrize(
    "failure", ("repeat", "cycle", "cap", "method_not_found", "internal_error", "unauthorized", "deadline")
)
@pytest.mark.parametrize("strict", (False, True))
def test_optional_discovery_rejects_incomplete_walks(
    method: str, failure: str, strict: bool, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
    monkeypatch.setattr(
        mcp_client_module,
        "MCP_TOOL_LISTING_MAX_PAGES",
        3 if failure in ("cycle", "repeat") else 2,
        raising=False,
    )
    monkeypatch.setattr(mcp_client_module, "MCP_TOOL_LISTING_TIMEOUT", 0.05)
    field: Final = {
        "prompts/list": "prompts",
        "resources/list": "resources",
        "resources/templates/list": "resourceTemplates",
    }[method]
    entry: Final = {
        "prompts/list": {"name": "first"},
        "resources/list": {"name": "first", "uri": "test://first"},
        "resources/templates/list": {"name": "first", "uriTemplate": "test://{name}"},
    }[method]
    loop: Final = ManualClockLoop()

    async def run() -> None:
        cancelled: Final = asyncio.Event()

        async def respond(request: httpx2.Request) -> httpx2.Response:
            payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
            if not isinstance(payload, JSONRPCRequest):
                return httpx2.Response(202)
            if payload.method == "initialize":
                return httpx2.Response(
                    200,
                    json={
                        "jsonrpc": "2.0",
                        "id": payload.id,
                        "result": {
                            "protocolVersion": (payload.params or {})["protocolVersion"],
                            "capabilities": {"prompts": {}, "resources": {}},
                            "serverInfo": {"name": "interrupted", "version": "1"},
                        },
                    },
                )
            assert payload.method == method
            cursor: Final = (payload.params or {}).get("cursor")
            if cursor is not None:
                if failure == "deadline":
                    loop.advance(0.15)
                    try:
                        for _ in range(1_000):
                            await asyncio.sleep(0)
                    except asyncio.CancelledError:
                        cancelled.set()
                        raise
                    return httpx2.Response(500)
                if failure == "unauthorized":
                    return httpx2.Response(401)
                if failure in ("method_not_found", "internal_error"):
                    return httpx2.Response(
                        200,
                        json={
                            "jsonrpc": "2.0",
                            "id": payload.id,
                            "error": {
                                "code": -32601 if failure == "method_not_found" else -32603,
                                "message": "Later page unavailable",
                            },
                        },
                    )
            if failure == "deadline" and cursor is None:
                loop.advance(0.1)
            next_cursor: Final = (
                "private-cursor-2" if cursor == "private-cursor-1" and failure != "repeat" else "private-cursor-1"
            )
            return httpx2.Response(
                200, json={"jsonrpc": "2.0", "id": payload.id, "result": {field: [entry], "nextCursor": next_cursor}}
            )

        responder: Final = AsyncMock(side_effect=respond)
        client: Final = _MockTransportClient(responder, server_url="https://example.com/mcp", timeout=0.2)
        operation: Final = {
            "prompts/list": client.list_prompts,
            "resources/list": client.list_resources,
            "resources/templates/list": client.list_resource_templates,
        }[method]
        if strict:
            error_type: Final = {
                "internal_error": MCPError,
                "unauthorized": httpx2.HTTPStatusError,
                "deadline": TimeoutError,
            }.get(failure, RuntimeError)
            with pytest.raises(error_type):
                await operation(raise_on_error=True)
        else:
            assert await operation() == []
        assert len(
            tuple(
                payload
                for call in responder.call_args_list
                if isinstance(payload := _JSONRPC_MESSAGE_ADAPTER.validate_json(call.args[0].content), JSONRPCRequest)
                and payload.method == method
            )
        ) == (3 if failure == "cycle" else 2)
        assert "private-cursor" not in caplog.text
        if failure == "deadline":
            assert cancelled.is_set()

    try:
        loop.run_until_complete(run())
    finally:
        loop.run_until_complete(loop.shutdown_asyncgens())
        loop.run_until_complete(loop.shutdown_default_executor())
        loop.close()


@pytest.mark.asyncio
@pytest.mark.parametrize("method", ("prompts/list", "resources/list", "resources/templates/list"))
async def test_optional_discovery_allows_exhaustion_at_page_cap(method: str, monkeypatch: pytest.MonkeyPatch) -> None:
    monkeypatch.setattr(mcp_client_module, "MCP_TOOL_LISTING_MAX_PAGES", 2, raising=False)
    field: Final = {
        "prompts/list": "prompts",
        "resources/list": "resources",
        "resources/templates/list": "resourceTemplates",
    }[method]

    def respond(request: httpx2.Request) -> httpx2.Response:
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        if not isinstance(payload, JSONRPCRequest):
            return httpx2.Response(202)
        if payload.method == "initialize":
            result: Final = {
                "protocolVersion": (payload.params or {})["protocolVersion"],
                "capabilities": {"prompts": {}, "resources": {}},
                "serverInfo": {"name": "empty-pages", "version": "1"},
            }
            return httpx2.Response(200, json={"jsonrpc": "2.0", "id": payload.id, "result": result})
        assert payload.method == method
        return httpx2.Response(
            200,
            json={
                "jsonrpc": "2.0",
                "id": payload.id,
                "result": {field: [], "nextCursor": None if (payload.params or {}).get("cursor") else "last-page"},
            },
        )

    responder: Final = Mock(side_effect=respond)
    client: Final = _MockTransportClient(responder, server_url="https://example.com/mcp")
    operation: Final = {
        "prompts/list": client.list_prompts,
        "resources/list": client.list_resources,
        "resources/templates/list": client.list_resource_templates,
    }[method]
    assert await operation(raise_on_error=True) == []
    assert (
        sum(
            isinstance(payload := _JSONRPC_MESSAGE_ADAPTER.validate_json(call.args[0].content), JSONRPCRequest)
            and payload.method == method
            for call in responder.call_args_list
        )
        == 2
    )


def test_standalone_client_request_does_not_require_proxy_dependencies():
    import textwrap

    from tests.test_litellm_rust.support.child_interpreter import run_child_interpreter

    script = textwrap.dedent("""
        import asyncio
        import sys
        for name in ("fastapi", "orjson", "email_validator", "annotated_doc"):
            sys.modules[name] = None
        import httpx2
        from litellm.experimental_mcp_client.client import MCPClient

        async def request():
            upstream = MCPClient(server_url="https://upstream.example/mcp")
            transport = httpx2.MockTransport(lambda request: httpx2.Response(200))
            async with upstream._create_httpx_client_factory(transport=transport)() as client:
                response = await client.post(upstream.server_url, json={"method": "tools/call"})
                assert response.status_code == 200
            assert all(sys.modules[name] is None for name in ("fastapi", "orjson", "email_validator", "annotated_doc"))

        asyncio.run(request())
        print("standalone request succeeded")
    """)
    result = run_child_interpreter(script, timeout=60)
    assert result.returncode == 0, result.stderr
    assert result.stdout.strip() == "standalone request succeeded"


def test_client_import_before_proxy_credentials_succeeds_in_fresh_process():
    import subprocess

    result = subprocess.run(
        [
            sys.executable,
            "-c",
            "import litellm.experimental_mcp_client.client; from litellm.proxy._experimental.mcp_server.mcp_server_manager import MCPServerManager; print(MCPServerManager.__name__)",
        ],
        capture_output=True,
        text=True,
        timeout=60,
        check=False,
    )
    assert result.returncode == 0, result.stderr
    assert result.stdout.strip() == "MCPServerManager"


@pytest.mark.asyncio
@pytest.mark.parametrize("resolved", (False, True))
async def test_discovery_auth_fingerprint_tracks_effective_credentials(resolved: bool) -> None:
    from litellm.proxy._experimental.mcp_server.outbound_credentials.httpx_auth import StaticHeaderAuth

    def client(token: str) -> MCPClient:
        return MCPClient(
            server_url="https://example.com/mcp",
            auth_type=MCPAuth.api_key,
            auth_value=None if resolved else token,
            resolved_auth=StaticHeaderAuth(token) if resolved else None,
        )

    original: Final = await client("private-original-credential").discovery_auth_fingerprint()
    repeated: Final = await client("private-original-credential").discovery_auth_fingerprint()
    replaced: Final = await client("private-replaced-credential").discovery_auth_fingerprint()
    assert original == repeated
    assert await client("private-original-credential").subscription_auth_fingerprint() == original
    assert await client("private-replaced-credential").subscription_auth_fingerprint() == replaced
    assert original != replaced
    assert len(original) == 64
    assert "private-original-credential" not in original


@pytest.mark.asyncio
async def test_discovery_auth_fingerprint_tracks_aws_sigv4_credential_identity() -> None:
    def client(access_key_id: str) -> MCPClient:
        return MCPClient(
            server_url="https://example.com/mcp",
            auth_type=MCPAuth.aws_sigv4,
            aws_auth=MCPSigV4Auth(
                aws_access_key_id=access_key_id,
                aws_secret_access_key="private-secret",
                aws_region_name="us-east-1",
            ),
        )

    original: Final = await client("AKIAORIGINAL").discovery_auth_fingerprint()
    repeated: Final = await client("AKIAORIGINAL").discovery_auth_fingerprint()
    rotated: Final = await client("AKIAROTATED").discovery_auth_fingerprint()
    assert original == repeated
    assert await client("AKIAORIGINAL").subscription_auth_fingerprint() == original
    assert await client("AKIAROTATED").subscription_auth_fingerprint() == rotated
    assert original != rotated
    assert "AKIAORIGINAL" not in original


@pytest.mark.asyncio
async def test_request_auth_preview_uses_the_same_effective_headers_as_egress() -> None:
    from litellm.proxy._experimental.mcp_server.outbound_credentials.httpx_auth import StaticHeaderAuth

    client: Final = MCPClient(
        server_url="https://upstream.example/mcp",
        auth_type=MCPAuth.bearer_token,
        resolved_auth=StaticHeaderAuth("Bearer resolved"),
        extra_headers={"X-Trace": "trace"},
    )
    request: Final = await client.prepare_request_auth()
    assert request.method == "POST"
    assert str(request.url) == "https://upstream.example/mcp"
    assert request.headers["Authorization"] == "Bearer resolved"
    assert request.headers["X-Trace"] == "trace"


@pytest.mark.asyncio
@pytest.mark.parametrize("rpc_error", [False, True])
async def test_expired_session_preserves_sdk_error_and_next_operation_reinitializes(rpc_error: bool) -> None:
    from mcp.types import INVALID_REQUEST, METHOD_NOT_FOUND

    requests = []

    def respond(request: httpx2.Request) -> httpx2.Response:
        if request.method != "POST":
            return httpx2.Response(405)
        payload = json.loads(request.content)
        if "id" not in payload:
            return httpx2.Response(202)
        requests.append((payload["method"], request.headers.get("mcp-session-id")))
        if payload["method"] == "initialize":
            return httpx2.Response(
                200,
                headers={"mcp-session-id": f"session-{len(requests)}"},
                json={
                    "jsonrpc": "2.0",
                    "id": payload["id"],
                    "result": {
                        "protocolVersion": "2025-06-18",
                        "capabilities": {},
                        "serverInfo": {"name": "expiry-test", "version": "1"},
                    },
                },
            )
        if len(requests) == 2:
            if rpc_error:
                return httpx2.Response(
                    404,
                    json={
                        "jsonrpc": "2.0",
                        "id": payload["id"],
                        "error": {"code": METHOD_NOT_FOUND, "message": "Tool catalog unavailable"},
                    },
                )
            return httpx2.Response(404)
        return httpx2.Response(200, json={"jsonrpc": "2.0", "id": payload["id"], "result": {"tools": []}})

    client = MCPClient(server_url="https://example.com/mcp", timeout=3)
    async with client._create_httpx_client_factory(transport=httpx2.MockTransport(respond))() as http_client:
        with pytest.raises(MCPError) as caught:
            await client._execute_session_operation(
                streamable_http_client(client.server_url, http_client=http_client), lambda session: session.list_tools()
            )
        assert caught.value.error.code == (METHOD_NOT_FOUND if rpc_error else INVALID_REQUEST)
        assert caught.value.error.message == ("Tool catalog unavailable" if rpc_error else "Session terminated")
        result = await client._execute_session_operation(
            streamable_http_client(client.server_url, http_client=http_client), lambda session: session.list_tools()
        )
    assert result.tools == []
    assert requests == [
        ("initialize", None),
        ("tools/list", "session-1"),
        ("initialize", None),
        ("tools/list", "session-3"),
    ]


@pytest.mark.asyncio
async def test_404_before_session_initialization_preserves_method_not_found() -> None:
    from mcp.types import METHOD_NOT_FOUND

    client = MCPClient(server_url="https://example.com/mcp", timeout=3)
    transport = httpx2.MockTransport(lambda request: httpx2.Response(404))
    async with client._create_httpx_client_factory(transport=transport)() as http_client:
        with pytest.raises(MCPError) as caught:
            await client._execute_session_operation(
                streamable_http_client(client.server_url, http_client=http_client), lambda session: session.list_tools()
            )
    assert caught.value.error.code == METHOD_NOT_FOUND
    assert caught.value.error.message == "Not Found"


@pytest.mark.parametrize("missing_module", ("mcp", "httpx2", "mcp.types", "openai.types.chat"))
def test_public_mcp_import_missing_dependency(missing_module: str) -> None:
    with patch.dict(sys.modules):
        for name in tuple(sys.modules):
            if name.startswith(("litellm.experimental_mcp_client", "mcp.", "mcp_types.")) or name == "mcp":
                del sys.modules[name]
        with patch.dict(sys.modules, {missing_module: None}):
            with pytest.raises(ImportError) as caught:
                importlib.import_module("litellm.experimental_mcp_client.client")

    if missing_module in ("mcp", "httpx2"):
        assert "pip install 'litellm[mcp]'" in str(caught.value)
        assert isinstance(caught.value.__cause__, ModuleNotFoundError)
        assert caught.value.__cause__.name == missing_module
    else:
        assert isinstance(caught.value, ModuleNotFoundError)
        assert caught.value.name == missing_module
        assert caught.value.__cause__ is None
        assert "litellm[mcp]" not in str(caught.value)


def test_public_mcp_import_preserves_incompatible_sdk_error() -> None:
    with patch.dict(sys.modules):
        for name in tuple(sys.modules):
            if name.startswith("litellm.experimental_mcp_client"):
                del sys.modules[name]
        with patch.dict(sys.modules, {"mcp": ModuleType("mcp")}):
            with pytest.raises(ImportError, match="cannot import name 'ClientSession'") as caught:
                importlib.import_module("litellm.experimental_mcp_client.client")

    assert not isinstance(caught.value, ModuleNotFoundError)
    assert caught.value.__cause__ is None
    assert "litellm[mcp]" not in str(caught.value)


@pytest.mark.asyncio
@pytest.mark.parametrize("grouped", (False, True))
@pytest.mark.parametrize("raise_on_error", (False, True))
@pytest.mark.parametrize("termination", ("ok", "failure", "hang"))
async def test_outer_deadline_delivers_session_termination(
    termination: str, grouped: bool, raise_on_error: bool
) -> None:
    deleted: Final = asyncio.Event()
    started: Final = asyncio.Event()
    caller_deadline: Final[asyncio.Future[anyio.CancelScope]] = asyncio.get_running_loop().create_future()

    async def respond(request: httpx2.Request) -> httpx2.Response:
        await anyio.lowlevel.checkpoint()
        if request.method == "DELETE":
            first_termination: Final = not deleted.is_set()
            deleted.set()
            if termination == "hang" and first_termination:
                await anyio.sleep_forever()
            return httpx2.Response(500 if termination == "failure" else 200)
        if request.method == "GET":
            return httpx2.Response(405)
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        if not isinstance(payload, JSONRPCRequest):
            return httpx2.Response(202)
        if payload.method == "initialize":
            return httpx2.Response(
                200,
                headers={"mcp-session-id": "cancel-owned-session"},
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "result": {
                        "protocolVersion": "2025-11-25",
                        "capabilities": {"tools": {}},
                        "serverInfo": {"name": "cancellation-peer", "version": "1"},
                    },
                },
            )
        if payload.method == "tools/list":
            return httpx2.Response(200, json={"jsonrpc": "2.0", "id": payload.id, "result": {"tools": []}})
        started.set()
        caller_deadline.result().deadline = anyio.current_time()
        await anyio.sleep_forever()
        raise AssertionError("cancelled request resumed")

    client: Final = _MockTransportClient(respond, server_url="https://example.com/mcp", timeout=30)

    async def invoke() -> None:
        with anyio.fail_after(None) as deadline:
            caller_deadline.set_result(deadline)
            pending: Final = client.call_tool(
                CallToolRequestParams(name="slow", arguments={}), raise_on_error=raise_on_error
            )
            if grouped:
                await asyncio.gather(pending)
            else:
                await pending

    with anyio.fail_after(20):
        with pytest.raises(TimeoutError):
            await invoke()
    assert started.is_set()
    assert deleted.is_set(), "Cancellation must deliver DELETE before returning to the caller"

    assert anyio.current_time() - caller_deadline.result().deadline < 6.5
    assert await client.list_tools(raise_on_error=True) == []


@pytest.mark.asyncio
@pytest.mark.parametrize("original_error", (False, True))
async def test_task_cancellation_during_cleanup_preserves_failure(original_error: bool) -> None:
    deleting: Final = asyncio.Event()
    drained: Final = asyncio.Event()
    original: Final = RuntimeError("operation failed before teardown")

    async def respond(request: httpx2.Request) -> httpx2.Response:
        if request.method == "DELETE":
            deleting.set()
            try:
                await anyio.sleep_forever()
            finally:
                drained.set()
        if request.method == "GET":
            return httpx2.Response(405)
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        if not isinstance(payload, JSONRPCRequest):
            return httpx2.Response(202)
        return httpx2.Response(
            200,
            headers={"mcp-session-id": "cleanup-session"},
            json={
                "jsonrpc": "2.0",
                "id": payload.id,
                "result": {
                    "protocolVersion": (payload.params or {})["protocolVersion"],
                    "capabilities": {},
                    "serverInfo": {"name": "cleanup-peer", "version": "1"},
                },
            },
        )

    async def operation(session: mcp_client_module.ClientSession) -> str:
        if original_error:
            raise original
        return "completed"

    client: Final = _MockTransportClient(respond, server_url="https://example.com/mcp", timeout=30)
    task: Final = asyncio.create_task(client.run_with_session(operation))
    await asyncio.wait_for(deleting.wait(), 2)
    for _ in range(3):
        task.cancel()
        await asyncio.sleep(0)
    with pytest.raises(RuntimeError if original_error else asyncio.CancelledError) as caught:
        await task
    assert drained.is_set(), "Caller must wait for termination cleanup to finish"
    if original_error:
        assert caught.value is original
    else:
        assert task.cancelled()


@pytest.mark.asyncio
@pytest.mark.parametrize("original_error", (False, True))
@pytest.mark.parametrize("cancel_mode", ("task", "scope"))
async def test_http_close_cancellation_cannot_turn_into_success(original_error: bool, cancel_mode: str) -> None:
    closing: Final = asyncio.Event()
    drained: Final = asyncio.Event()
    original: Final = RuntimeError("failed before HTTP close")

    class ClosingHTTPClient(httpx2.AsyncClient):
        async def aclose(self) -> None:
            closing.set()
            try:
                await anyio.sleep_forever()
            finally:
                drained.set()

    class ClosingMCPClient(MCPClient):
        def _create_transport_context(self):
            http_client: Final = ClosingHTTPClient(transport=httpx2.MockTransport(lambda _: httpx2.Response(200)))
            return streamable_http_client(self.server_url, http_client=http_client), http_client

        async def _execute_session_operation(self, transport_ctx, operation, http_client=None):
            if original_error:
                raise original
            return "completed"

    client: Final = ClosingMCPClient(server_url="https://example.com/mcp")

    async def invoke() -> str:
        with anyio.fail_after(0.05 if cancel_mode == "scope" else None):
            return await client.run_with_session(AsyncMock())

    task: Final = asyncio.create_task(invoke())
    await asyncio.wait_for(closing.wait(), 2)
    if cancel_mode == "task":
        task.cancel()
    cancellation_type: Final = asyncio.CancelledError if cancel_mode == "task" else TimeoutError
    with pytest.raises(RuntimeError if original_error else cancellation_type) as caught:
        await task
    assert drained.is_set(), "Caller must wait for HTTP closure to finish"
    if original_error:
        assert caught.value is original
    elif cancel_mode == "task":
        assert task.cancelled()


@pytest.mark.asyncio
@pytest.mark.parametrize("protocol_version", ("auto", "2025-06-18"))
@pytest.mark.parametrize("cancel_mode", ("scope", "task", "wait_for", "read_timeout"))
@pytest.mark.parametrize("concurrency", (1, 5))
@pytest.mark.parametrize("termination", ("ok", "hang", "hang_body"))
@pytest.mark.parametrize("raise_on_error", (False, True))
async def test_cancellation_delivers_termination_over_tcp(
    cancel_mode: str, concurrency: int, termination: str, raise_on_error: bool, protocol_version: str
) -> None:
    started: Final = asyncio.Event()
    scope_ready: Final[asyncio.Future[anyio.CancelScope]] = asyncio.get_running_loop().create_future()
    terminations: Final[list[bytes]] = []
    starts: Final[list[bytes]] = []
    stop: Final = asyncio.Event()
    connections: Final[list[asyncio.Task[None]]] = []

    async def handle_connection(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
        connection: Final = asyncio.current_task()
        assert connection is not None
        connections.append(connection)
        try:
            request_line: Final = await reader.readline()
            if not request_line:
                return
            method: Final = request_line.split()[0]
            headers: Final = await reader.readuntil(b"\r\n\r\n")
            length: Final = next(
                (
                    int(line.split(b":", 1)[1])
                    for line in headers.splitlines()
                    if line.lower().startswith(b"content-length:")
                ),
                0,
            )
            try:
                body: Final = await reader.readexactly(length)
            except asyncio.IncompleteReadError:
                return
            if method == b"DELETE":
                terminations.append(body)
                if termination != "ok":
                    await stop.wait()
                    return
                writer.write(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n")
            elif method == b"GET":
                writer.write(b"HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\nConnection: close\r\n\r\n")
            else:
                payload: Final = json.loads(body)
                if payload["method"] == "tools/call":
                    if termination == "hang_body":
                        writer.write(
                            b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n"
                            b"Content-Length: 200\r\nConnection: close\r\n\r\n"
                        )
                        await writer.drain()
                    starts.append(body)
                    if len(starts) == concurrency:
                        started.set()
                    await stop.wait()
                    return
                if payload["method"] == "initialize":
                    if cancel_mode != "read_timeout":
                        await asyncio.sleep(0.75)
                    response: Final = json.dumps(
                        {
                            "jsonrpc": "2.0",
                            "id": payload["id"],
                            "result": {
                                "protocolVersion": "2025-06-18",
                                "capabilities": {"tools": {}},
                                "serverInfo": {"name": "tcp-peer", "version": "1"},
                            },
                        }
                    ).encode()
                    writer.write(
                        b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nMcp-Session-Id: tcp-session\r\n"
                        + f"Content-Length: {len(response)}\r\nConnection: close\r\n\r\n".encode()
                        + response
                    )
                else:
                    writer.write(b"HTTP/1.1 202 Accepted\r\nContent-Length: 0\r\nConnection: close\r\n\r\n")
            await writer.drain()
        finally:
            writer.close()
            await writer.wait_closed()

    listener: Final = await asyncio.start_server(handle_connection, "127.0.0.1", 0)
    port: Final = listener.sockets[0].getsockname()[1]
    client_timeout: Final = 2 if cancel_mode == "read_timeout" else 30
    client: Final = MCPClient(
        server_url=f"http://127.0.0.1:{port}/mcp",
        protocol_version=protocol_version,
        timeout=client_timeout,
    )

    async def calls():
        results: Final = await asyncio.gather(
            *(
                client.call_tool(CallToolRequestParams(name="slow", arguments={}), raise_on_error=raise_on_error)
                for _ in range(concurrency)
            ),
            return_exceptions=cancel_mode == "read_timeout",
        )
        if cancel_mode == "read_timeout":
            if raise_on_error:
                assert all(isinstance(result, TimeoutError) for result in results)
            else:
                assert all(isinstance(result, CallToolResult) and result.is_error for result in results)
        return results

    async def invoke():
        if cancel_mode == "scope":
            with anyio.fail_after(None) as scope:
                scope_ready.set_result(scope)
                return await calls()
        return await calls()

    try:
        task: Final = asyncio.create_task(invoke())
        await asyncio.wait_for(started.wait(), 30)
        if cancel_mode == "scope":
            (await scope_ready).deadline = anyio.current_time() + 0.2
        if cancel_mode == "task":
            task.cancel()
        expected_error: Final = (
            TimeoutError
            if cancel_mode == "read_timeout"
            else asyncio.CancelledError
            if cancel_mode == "task"
            else TimeoutError
        )
        if cancel_mode == "read_timeout":
            done, _ = await asyncio.wait((task,), timeout=8)
            assert task in done, "Read timeout and bounded cleanup must complete without external cancellation"
            await task
        elif cancel_mode == "wait_for":
            with pytest.raises(expected_error):
                await asyncio.wait_for(task, 0.2)
        else:
            with pytest.raises(expected_error):
                await task
        assert len(starts) == concurrency
        assert len(terminations) == concurrency, "Each cancelled call must send DELETE over a fresh TCP connection"
    finally:
        stop.set()
        if not task.done():
            task.cancel()
            await asyncio.wait((task,), timeout=8)
        listener.close()
        for connection in connections:
            connection.cancel()
        closed: Final = await asyncio.wait_for(asyncio.gather(*connections, return_exceptions=True), 2)
        assert all(result is None or isinstance(result, asyncio.CancelledError) for result in closed), closed
        await asyncio.wait_for(listener.wait_closed(), 2)


@pytest.mark.asyncio
@pytest.mark.parametrize("revision", ["2024-11-05", "2025-03-26", "2025-06-18", "2025-11-25", "auto"])
@pytest.mark.parametrize("accepted", [True, False])
@pytest.mark.parametrize("callbacks", [False, True])
async def test_configured_upstream_revision_is_offered_and_checked(revision, accepted, callbacks):
    from mcp.types import JSONRPCRequest
    from mcp_types.version import LATEST_HANDSHAKE_VERSION

    offered = LATEST_HANDSHAKE_VERSION if revision == "auto" else revision

    def respond(request):
        if request.method == "DELETE":
            return httpx2.Response(200)
        payload = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        if not isinstance(payload, JSONRPCRequest):
            return httpx2.Response(202)
        if payload.method == "initialize":
            assert payload.params["protocolVersion"] == offered
            assert ("sampling" in payload.params["capabilities"]) == callbacks
            assert ("elicitation" in payload.params["capabilities"]) == callbacks
            return httpx2.Response(
                200,
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "result": {
                        "protocolVersion": offered if accepted else "unsupported",
                        "capabilities": {"tools": {}},
                        "serverInfo": {"name": "upstream", "version": "1"},
                    },
                },
            )
        assert accepted, "No operation may execute after failed version negotiation"
        return httpx2.Response(
            200,
            json={
                "jsonrpc": "2.0",
                "id": payload.id,
                "result": {"tools": [{"name": "echo", "inputSchema": {"type": "object"}}]},
            },
        )

    client = _MockTransportClient(
        respond,
        server_url="https://example.com/mcp",
        protocol_version=revision,
        sampling_callback=AsyncMock() if callbacks else None,
        elicitation_callback=AsyncMock() if callbacks else None,
    )
    if accepted:
        result = await client.list_tools(raise_on_error=True)
        assert [tool.name for tool in result] == ["echo"]
    else:
        with pytest.raises((MCPError, RuntimeError), match="protocol version"):
            await client.list_tools(raise_on_error=True)


@pytest.mark.parametrize("revision", ["unknown", "", None])
def test_upstream_protocol_configuration_rejects_unavailable_modes(revision):
    from pydantic import ValidationError

    with pytest.raises(ValidationError):
        MCPClient(protocol_version=revision)


@pytest.mark.asyncio
@pytest.mark.parametrize("accepted", [True, False])
async def test_modern_upstream_requests_are_self_contained_without_initialization(accepted: bool) -> None:
    from queue import SimpleQueue

    from mcp.types import ToolsCapability

    methods: Final[SimpleQueue[str]] = SimpleQueue()

    def respond(request: httpx2.Request) -> httpx2.Response:
        if request.method != "POST":
            return httpx2.Response(405)
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        assert isinstance(payload, JSONRPCRequest)
        methods.put(payload.method)
        assert payload.method != "initialize", "Modern operations must not establish a legacy session"
        assert "mcp-session-id" not in request.headers
        assert request.headers["mcp-protocol-version"] == "2026-07-28"
        assert request.headers["authorization"] == "Bearer upstream-credential"
        assert payload.params is not None
        metadata: Final = payload.params["_meta"]
        assert metadata["io.modelcontextprotocol/protocolVersion"] == "2026-07-28"
        assert "io.modelcontextprotocol/clientCapabilities" in metadata
        if payload.method == "server/discover":
            discovery: Final = DiscoverResult(
                supported_versions=["2026-07-28"] if accepted else ["2025-11-25"],
                capabilities=ServerCapabilities(tools=ToolsCapability()),
                instructions="modern instructions",
            )
            return httpx2.Response(
                200,
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "result": discovery.model_dump(by_alias=True, exclude_none=True),
                },
            )
        assert accepted, "Rejected negotiation must prevent upstream execution"
        if payload.method == "tools/list":
            return httpx2.Response(
                200,
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "result": {
                        "resultType": "complete",
                        "cacheScope": "private",
                        "ttlMs": 0,
                        "tools": [{"name": "add", "inputSchema": {"type": "object"}}],
                    },
                },
            )
        assert payload.method == "tools/call"
        assert payload.params["arguments"] == {"a": 2, "b": 3}
        return httpx2.Response(
            200,
            json={
                "jsonrpc": "2.0",
                "id": payload.id,
                "result": {"resultType": "complete", "content": [{"type": "text", "text": "5"}], "isError": False},
            },
        )

    client: Final = _MockTransportClient(
        respond,
        server_url="https://example.com/mcp",
        protocol_version="2026-07-28",
        auth_type=MCPAuth.bearer_token,
        auth_value="upstream-credential",
    )
    params: Final = CallToolRequestParams(name="add", arguments={"a": 2, "b": 3})
    if accepted:
        result: Final = await client.call_tool(params, raise_on_error=True)
        assert result.content[0].text == "5"
        assert not result.is_error
        assert client._last_initialize_instructions == "modern instructions"
        assert tuple(methods.get_nowait() for _ in range(methods.qsize())) == (
            "server/discover",
            "tools/list",
            "tools/call",
        )
    else:
        with pytest.raises((MCPError, RuntimeError), match="protocol version"):
            await client.call_tool(params, raise_on_error=True)
        assert tuple(methods.get_nowait() for _ in range(methods.qsize())) == ("server/discover",)


@pytest.mark.asyncio
@pytest.mark.parametrize("paginated", (False, True))
@pytest.mark.parametrize("valid_annotation", (True, False))
async def test_modern_call_emits_listed_argument_headers(paginated: bool, valid_annotation: bool) -> None:
    from collections.abc import Mapping
    from queue import SimpleQueue

    from mcp.types import ToolsCapability

    calls: Final[SimpleQueue[str]] = SimpleQueue()

    def response_result(request: httpx2.Request, payload: JSONRPCRequest) -> Mapping[str, object]:
        assert payload.params is not None
        caller: Final = request.headers["authorization"].removeprefix("Bearer ")
        if payload.method == "server/discover":
            return DiscoverResult(
                supported_versions=["2026-07-28"], capabilities=ServerCapabilities(tools=ToolsCapability())
            ).model_dump(by_alias=True, exclude_none=True)
        if payload.method == "tools/list":
            if paginated and "cursor" not in payload.params:
                return {
                    "resultType": "complete",
                    "cacheScope": "private",
                    "ttlMs": 0,
                    "tools": [],
                    "nextCursor": "second",
                }
            return {
                "resultType": "complete",
                "cacheScope": "private",
                "ttlMs": 0,
                "tools": [
                    {
                        "name": "quote",
                        "inputSchema": {
                            "type": "object",
                            "properties": {
                                "workspace": {
                                    "type": "string" if valid_annotation else "number",
                                    "x-mcp-header": caller,
                                },
                                "count": {"type": "integer", "x-mcp-header": "Count"},
                                "preview": {"type": "boolean", "x-mcp-header": "Preview"},
                            },
                        },
                    }
                ],
            }
        assert payload.method == "tools/call"
        calls.put(caller)
        assert valid_annotation, "Invalid header definitions must prevent dispatch"
        assert request.headers.get(f"mcp-param-{caller.lower()}") == caller
        assert request.headers.get("mcp-param-count") == "3"
        assert request.headers.get("mcp-param-preview") == "true"
        assert set(key for key in request.headers if key.startswith("mcp-param-")) == {
            f"mcp-param-{caller.lower()}",
            "mcp-param-count",
            "mcp-param-preview",
        }
        assert payload.params["arguments"] == {"workspace": caller, "count": 3, "preview": True}
        return {"resultType": "complete", "content": [{"type": "text", "text": "quoted"}], "isError": False}

    def respond(request: httpx2.Request) -> httpx2.Response:
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        assert isinstance(payload, JSONRPCRequest)
        return httpx2.Response(
            200, json={"jsonrpc": "2.0", "id": payload.id, "result": response_result(request, payload)}
        )

    async def call_as(caller: str) -> None:
        client: Final = _MockTransportClient(
            respond,
            server_url="https://example.com/mcp",
            protocol_version="2026-07-28",
            auth_type=MCPAuth.bearer_token,
            auth_value=caller,
        )
        params: Final = CallToolRequestParams(
            name="quote", arguments={"workspace": caller, "count": 3, "preview": True}
        )
        if not valid_annotation:
            with pytest.raises(MCPError, match="schema is unavailable"):
                await client.call_tool(params, raise_on_error=True)
            return
        result: Final = await client.call_tool(params, raise_on_error=True)
        assert result.is_error is False
        assert result.content[0].text == "quoted"

    await asyncio.gather(call_as("Engineering"), call_as("Finance"))
    assert calls.qsize() == (2 if valid_annotation else 0)


@pytest.mark.asyncio
@pytest.mark.parametrize("stop", ("page_cap", "repeated_cursor"))
@pytest.mark.parametrize("schema_available", (False, True))
async def test_modern_call_requires_schema_from_bounded_listing(
    stop: str, schema_available: bool, monkeypatch: pytest.MonkeyPatch
) -> None:
    from queue import SimpleQueue

    from mcp.types import ToolsCapability

    monkeypatch.setattr("litellm.experimental_mcp_client.tools.MCP_TOOL_LISTING_MAX_PAGES", 2)
    methods: Final[SimpleQueue[str]] = SimpleQueue()

    def respond(request: httpx2.Request) -> httpx2.Response:
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        assert isinstance(payload, JSONRPCRequest)
        methods.put(payload.method)
        if payload.method == "server/discover":
            return httpx2.Response(
                200,
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "result": DiscoverResult(
                        supported_versions=["2026-07-28"], capabilities=ServerCapabilities(tools=ToolsCapability())
                    ).model_dump(by_alias=True, exclude_none=True),
                },
            )
        if payload.method == "tools/list":
            assert payload.params is not None
            return httpx2.Response(
                200,
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "result": {
                        "resultType": "complete",
                        "cacheScope": "private",
                        "ttlMs": 0,
                        "nextCursor": "loop"
                        if stop == "repeated_cursor"
                        else str(int(payload.params.get("cursor", "0")) + 1),
                        "tools": [
                            {
                                "name": "quote",
                                "inputSchema": {
                                    "type": "object",
                                    "properties": {"workspace": {"type": "string", "x-mcp-header": "Workspace"}},
                                },
                            }
                        ]
                        if schema_available
                        else [],
                    },
                },
            )
        assert payload.method == "tools/call"
        assert schema_available, "An unavailable schema must not permit dispatch without required headers"
        assert request.headers["mcp-param-workspace"] == "engineering"
        return httpx2.Response(
            200,
            json={
                "jsonrpc": "2.0",
                "id": payload.id,
                "result": {"resultType": "complete", "content": [{"type": "text", "text": "quoted"}], "isError": False},
            },
        )

    client: Final = _MockTransportClient(respond, server_url="https://example.com/mcp", protocol_version="2026-07-28")
    params: Final = CallToolRequestParams(name="quote", arguments={"workspace": "engineering"})
    if schema_available:
        result: Final = await client.call_tool(params, raise_on_error=True)
        assert not result.is_error
        assert result.content[0].text == "quoted"
    else:
        with pytest.raises(MCPError, match="schema is unavailable") as error:
            await client.call_tool(params, raise_on_error=True)
        assert error.value.code == -32603
    observed: Final = tuple(methods.get_nowait() for _ in range(methods.qsize()))
    assert observed.count("tools/list") == 2
    assert observed.count("tools/call") == int(schema_available)


def test_modern_call_uses_discovery_deadline_for_multiple_pages() -> None:
    from mcp.types import ToolsCapability

    loop: Final = AutojumpClockLoop()

    async def respond(request: httpx2.Request) -> httpx2.Response:
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        assert isinstance(payload, JSONRPCRequest)
        if payload.method == "server/discover":
            return httpx2.Response(
                200,
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "result": DiscoverResult(
                        supported_versions=["2026-07-28"], capabilities=ServerCapabilities(tools=ToolsCapability())
                    ).model_dump(by_alias=True, exclude_none=True),
                },
            )
        if payload.method == "tools/list":
            await asyncio.sleep(0.06)
            assert payload.params is not None
            page: Final = (
                {"tools": [], "nextCursor": "second"}
                if "cursor" not in payload.params
                else {
                    "tools": [
                        {
                            "name": "quote",
                            "inputSchema": {
                                "type": "object",
                                "properties": {"workspace": {"type": "string", "x-mcp-header": "Workspace"}},
                            },
                        }
                    ]
                }
            )
            return httpx2.Response(
                200,
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "result": {"resultType": "complete", "cacheScope": "private", "ttlMs": 0, **page},
                },
            )
        assert payload.method == "tools/call"
        assert request.headers["mcp-param-workspace"] == "engineering"
        return httpx2.Response(
            200,
            json={
                "jsonrpc": "2.0",
                "id": payload.id,
                "result": {"resultType": "complete", "content": [{"type": "text", "text": "quoted"}], "isError": False},
            },
        )

    async def run() -> None:
        client: Final = _MockTransportClient(
            respond, server_url="https://example.com/mcp", protocol_version="2026-07-28", timeout=0.1
        )
        assert [tool.name for tool in await client.list_tools(raise_on_error=True)] == ["quote"]
        result: Final = await client.call_tool(
            CallToolRequestParams(name="quote", arguments={"workspace": "engineering"}), raise_on_error=True
        )
        assert result.is_error is False
        assert result.content[0].text == "quoted"

    try:
        loop.run_until_complete(run())
    finally:
        loop.run_until_complete(loop.shutdown_asyncgens())
        loop.close()


@pytest.mark.asyncio
async def test_cancelled_modern_catalog_load_prevents_tool_execution() -> None:
    from queue import SimpleQueue

    from mcp.types import ToolsCapability

    listing_started: Final = asyncio.Event()
    hold_listing: Final = asyncio.Event()
    methods: Final[SimpleQueue[str]] = SimpleQueue()

    async def respond(request: httpx2.Request) -> httpx2.Response:
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        assert isinstance(payload, JSONRPCRequest)
        methods.put(payload.method)
        if payload.method == "tools/list":
            listing_started.set()
            await hold_listing.wait()
        assert payload.method == "server/discover", "A cancelled listing must not dispatch a tool call"
        discovery: Final = DiscoverResult(
            supported_versions=["2026-07-28"], capabilities=ServerCapabilities(tools=ToolsCapability())
        )
        return httpx2.Response(
            200,
            json={"jsonrpc": "2.0", "id": payload.id, "result": discovery.model_dump(by_alias=True, exclude_none=True)},
        )

    client: Final = _MockTransportClient(respond, server_url="https://example.com/mcp", protocol_version="2026-07-28")
    task: Final = asyncio.create_task(
        client.call_tool(CallToolRequestParams(name="quote", arguments={}), raise_on_error=True)
    )
    observed_listing: Final = asyncio.create_task(listing_started.wait())
    try:
        await asyncio.wait((task, observed_listing), return_when=asyncio.FIRST_COMPLETED)
        assert listing_started.is_set()
        task.cancel()
        with pytest.raises(asyncio.CancelledError):
            await task
        assert tuple(methods.get_nowait() for _ in range(methods.qsize())) == ("server/discover", "tools/list")
    finally:
        task.cancel()
        observed_listing.cancel()
        await asyncio.gather(task, observed_listing, return_exceptions=True)


def test_modern_upstream_rejects_legacy_sse_transport() -> None:
    with pytest.raises(ValueError, match="transport"):
        MCPClient(protocol_version="2026-07-28", transport_type=MCPTransport.sse)


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "method, field, item",
    [
        ("tools/list", "tools", {"name": "second", "inputSchema": {"type": "object"}}),
        ("prompts/list", "prompts", {"name": "second"}),
        ("resources/list", "resources", {"name": "second", "uri": "status://second"}),
        ("resources/templates/list", "resourceTemplates", {"name": "second", "uriTemplate": "status://{name}"}),
    ],
)
async def test_single_catalog_page_preserves_cursor_metadata_and_request_cursor(method, field, item):
    from mcp.types import (
        ListPromptsRequest,
        ListResourcesRequest,
        ListResourceTemplatesRequest,
        ListToolsRequest,
        PaginatedRequestParams,
    )

    def respond(request: httpx2.Request) -> httpx2.Response:
        if request.method != "POST":
            return httpx2.Response(405)
        payload = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        if not isinstance(payload, JSONRPCRequest):
            return httpx2.Response(202)
        if payload.method == "initialize":
            return httpx2.Response(
                200,
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "result": {
                        "protocolVersion": LATEST_HANDSHAKE_VERSION,
                        "capabilities": {"tools": {}, "prompts": {}, "resources": {}},
                        "serverInfo": {"name": "pagination", "version": "1"},
                    },
                },
            )
        assert payload.method == method
        assert payload.params is not None
        assert payload.params["cursor"] == "upstream-position"
        return httpx2.Response(
            200,
            json={
                "jsonrpc": "2.0",
                "id": payload.id,
                "result": {
                    field: [item],
                    "nextCursor": "upstream-next",
                    "_meta": {"revision": "revision-two"},
                },
            },
        )

    client = _MockTransportClient(respond, server_url="https://upstream.example.com/mcp")
    request_type = {
        "tools/list": ListToolsRequest,
        "prompts/list": ListPromptsRequest,
        "resources/list": ListResourcesRequest,
        "resources/templates/list": ListResourceTemplatesRequest,
    }[method]
    result = await client.list_page(request_type(params=PaginatedRequestParams(cursor="upstream-position")))
    assert result.model_dump(by_alias=True)[field][0]["name"] == "second"
    assert result.next_cursor == "upstream-next"
    assert result.meta == {"revision": "revision-two"}


@pytest.mark.asyncio
@pytest.mark.parametrize("method", ["prompts/list", "resources/list", "resources/templates/list"])
@pytest.mark.parametrize("failure", ["unadvertised", "method_missing", "upstream_error"])
@pytest.mark.parametrize("cursor", [None, "continuation"])
async def test_optional_catalog_distinguishes_absent_capability_from_failed_continuation(method, failure, cursor):
    from mcp.types import (
        ListPromptsRequest,
        ListResourcesRequest,
        ListResourceTemplatesRequest,
        PaginatedRequestParams,
    )

    methods = []

    def respond(request: httpx2.Request) -> httpx2.Response:
        if request.method != "POST":
            return httpx2.Response(405)
        payload = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        if not isinstance(payload, JSONRPCRequest):
            return httpx2.Response(202)
        methods.append(payload.method)
        if payload.method == "initialize":
            return httpx2.Response(
                200,
                json={
                    "jsonrpc": "2.0",
                    "id": payload.id,
                    "result": {
                        "protocolVersion": LATEST_HANDSHAKE_VERSION,
                        "capabilities": {} if failure == "unadvertised" else {"prompts": {}, "resources": {}},
                        "serverInfo": {"name": "optional", "version": "1"},
                    },
                },
            )
        assert payload.method == method
        return httpx2.Response(
            200,
            json={
                "jsonrpc": "2.0",
                "id": payload.id,
                "error": {
                    "code": -32601 if failure == "method_missing" else -32603,
                    "message": "Upstream unavailable",
                },
            },
        )

    client = _MockTransportClient(respond, server_url="https://upstream.example.com/mcp")
    request_type = {
        "prompts/list": ListPromptsRequest,
        "resources/list": ListResourcesRequest,
        "resources/templates/list": ListResourceTemplatesRequest,
    }[method]
    request = request_type(params=PaginatedRequestParams(cursor=cursor))
    if cursor is not None or failure == "upstream_error":
        with pytest.raises(MCPError):
            await client.list_page(request)
    else:
        result = await client.list_page(request)
        collection: Final = {
            "prompts/list": "prompts",
            "resources/list": "resources",
            "resources/templates/list": "resource_templates",
        }[method]
        assert getattr(result, collection) == []
        assert result.next_cursor is None
    if failure == "unadvertised":
        assert method not in methods


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "kind,field,entry",
    [
        ("prompts", "prompts", {"name": "example"}),
        ("resources", "resources", {"name": "example", "uri": "test://example"}),
        ("resource_templates", "resourceTemplates", {"name": "example", "uriTemplate": "test://{name}"}),
    ],
)
@pytest.mark.parametrize("ttl", [0, 5000])
@pytest.mark.parametrize("cleanup_phase", ["transport", "http_client"])
@pytest.mark.parametrize("cleanup_seconds", [0, 2, 6])
async def test_optional_discovery_retains_freshness_across_pages(
    kind: str, field: str, entry: dict[str, str], ttl: int, cleanup_phase: str, cleanup_seconds: int
) -> None:
    def respond(request: httpx2.Request) -> httpx2.Response:
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        assert isinstance(payload, JSONRPCRequest)
        if payload.method == "server/discover":
            discovery_result: Final = {
                "supportedVersions": ["2026-07-28"],
                "capabilities": {"prompts": {}, "resources": {}},
                "ttlMs": 0,
                "cacheScope": "private",
                "resultType": "complete",
            }
            return httpx2.Response(200, json={"jsonrpc": "2.0", "id": payload.id, "result": discovery_result})
        following: Final = bool((payload.params or {}).get("cursor"))
        listing_result: Final = {
            field: [entry],
            "ttlMs": ttl if following else 9000,
            "cacheScope": "private" if following else "public",
            "resultType": "complete",
            **({} if following else {"nextCursor": "next"}),
        }
        return httpx2.Response(200, json={"jsonrpc": "2.0", "id": payload.id, "result": listing_result})

    clock: Final = ManualClockLoop()

    class CleanupTransport(httpx2.AsyncBaseTransport):
        def __init__(self) -> None:
            self._transport = httpx2.MockTransport(respond)

        async def handle_async_request(self, request: httpx2.Request) -> httpx2.Response:
            return await self._transport.handle_async_request(request)

        async def aclose(self) -> None:
            await self._transport.aclose()
            if cleanup_phase == "http_client":
                clock.advance(cleanup_seconds)

    @asynccontextmanager
    async def transport_with_cleanup(url: str, http_client: httpx2.AsyncClient) -> AsyncIterator[_TransportStreams]:
        async with streamable_http_client(url, http_client=http_client) as streams:
            yield streams
        if cleanup_phase == "transport":
            clock.advance(cleanup_seconds)

    client: Final = _MockTransportClient(
        respond,
        http_transport=CleanupTransport(),
        transport_context=transport_with_cleanup,
        server_url="https://example.com/mcp",
        protocol_version="2026-07-28",
    )

    try:
        with patch.object(mcp_client_module, "time", Mock(monotonic=clock.time)):
            result: Final = await getattr(client, "list_" + kind + "_result")(raise_on_error=True)
    finally:
        clock.close()
    assert len(getattr(result, kind)) == 2
    assert result.cache_scope == "private"
    assert result.next_cursor is None
    assert result.ttl_ms == max(0, ttl - cleanup_seconds * 1000)
    assert len(await getattr(client, "list_" + kind)(raise_on_error=True)) == 2


def test_prompt_continuation_polling_respects_the_original_deadline() -> None:
    from mcp.types import GetPromptRequestParams

    def respond(request: httpx2.Request) -> httpx2.Response:
        payload: Final = _JSONRPC_MESSAGE_ADAPTER.validate_json(request.content)
        assert isinstance(payload, JSONRPCRequest)
        result: Final = (
            {
                "supportedVersions": ["2026-07-28"],
                "capabilities": {"prompts": {}},
                "resultType": "complete",
                "cacheScope": "private",
                "ttlMs": 0,
            }
            if payload.method == "server/discover"
            else {"resultType": "input_required", "requestState": "pending"}
        )
        return httpx2.Response(200, json={"jsonrpc": "2.0", "id": payload.id, "result": result})

    loop: Final = AutojumpClockLoop()
    client: Final = _MockTransportClient(
        respond, server_url="https://example.com/mcp", protocol_version="2026-07-28", timeout=0.12
    )
    try:
        with pytest.raises(TimeoutError):
            loop.run_until_complete(client.get_prompt(GetPromptRequestParams(name="pending")))
        assert loop.time() == pytest.approx(0.12)
    finally:
        loop.close()


@pytest.mark.asyncio
@pytest.mark.parametrize("mode", ("form", "url"))
@pytest.mark.parametrize("enabled", (False, True))
async def test_modern_elicitation_honors_server_permission(mode: str, enabled: bool) -> None:
    import anyio
    from mcp import ClientSession, MCPError
    from mcp.shared.message import SessionMessage
    from mcp.types import (
        ElicitRequest,
        ElicitRequestFormParams,
        ElicitRequestParams,
        ElicitRequestURLParams,
        ElicitResult,
        InputRequiredResult,
    )

    async def elicit(context: object, params: ElicitRequestParams) -> ElicitResult:
        return ElicitResult(action="accept")

    client: Final = MCPClient(server_url="https://example.com/mcp", elicitation_callback=elicit if enabled else None)
    request: Final = AsyncMock(
        return_value=InputRequiredResult(
            request_state="pending",
            input_requests={
                "consent": ElicitRequest(
                    params=ElicitRequestFormParams(message="Confirm", requested_schema={"type": "object"})
                    if mode == "form"
                    else ElicitRequestURLParams(message="Confirm", url="https://example.com/confirm")
                )
            },
        )
    )
    send, receive = anyio.create_memory_object_stream[SessionMessage](1)
    async with send, receive:
        session: Final = ClientSession(receive, send)
        if enabled:
            result: Final = await client._request_with_interaction(session, request, None, None, True)
            assert isinstance(result, InputRequiredResult)
            assert result.input_requests["consent"].params.mode == mode
        else:
            with pytest.raises(MCPError, match="Elicitation is disabled"):
                await client._request_with_interaction(session, request, None, None, True)
        request.assert_awaited_once_with(None, None)


class _StatefulUpstreamClient(MCPClient):
    """An MCPClient whose streamable-HTTP transport talks to an in-process stateful MCP server."""

    def __init__(self, app: ASGIApp, *, server_url: str, transport_type: MCPTransport) -> None:
        super().__init__(server_url=server_url, transport_type=transport_type)
        self._app = app

    def _create_transport_context(self) -> tuple[_TransportContext, httpx2.AsyncClient]:
        http_client: Final = self._create_httpx_client_factory(transport=httpx2.ASGITransport(app=self._app))(
            headers=self._get_auth_headers(), timeout=httpx2.Timeout(self.timeout)
        )
        return streamable_http_client(self.server_url, http_client=http_client), http_client


class _CleanupHoldingASGITransport(httpx2.ASGITransport):
    def __init__(self, app: ASGIApp, cleanup_started: asyncio.Event, release_cleanup: asyncio.Event) -> None:
        super().__init__(app=app)
        self._cleanup_started: Final = cleanup_started
        self._release_cleanup: Final = release_cleanup

    async def aclose(self) -> None:
        self._cleanup_started.set()
        await self._release_cleanup.wait()
        await super().aclose()


class _CleanupHoldingStatefulUpstreamClient(_StatefulUpstreamClient):
    def __init__(
        self,
        app: ASGIApp,
        *,
        server_url: str,
        transport_type: MCPTransport,
        cleanup_started: asyncio.Event,
        release_cleanup: asyncio.Event,
    ) -> None:
        super().__init__(app, server_url=server_url, transport_type=transport_type)
        self._cleanup_started: Final = cleanup_started
        self._release_cleanup: Final = release_cleanup

    def _create_transport_context(self) -> tuple[_TransportContext, httpx2.AsyncClient]:
        transport: Final = _CleanupHoldingASGITransport(
            self._app,
            self._cleanup_started,
            self._release_cleanup,
        )
        http_client: Final = self._create_httpx_client_factory(transport=transport)(
            headers=self._get_auth_headers(),
            timeout=httpx2.Timeout(self.timeout),
        )
        return streamable_http_client(self.server_url, http_client=http_client), http_client


class _InProcessUpstreamClient(MCPClient):
    def __init__(self, server: UpstreamServer, *, elicitation_callback: ElicitationFnT | None = None) -> None:
        super().__init__(
            server_url="http://upstream/mcp",
            transport_type=MCPTransport.http,
            elicitation_callback=elicitation_callback,
        )
        self._server = server

    def _create_transport_context(self) -> tuple[_TransportContext, httpx2.AsyncClient | None]:
        @asynccontextmanager
        async def connected() -> AsyncIterator[_TransportStreams]:
            server_to_client_send, client_receive = anyio.create_memory_object_stream[SessionMessage | Exception](0)
            client_to_server_send, server_receive = anyio.create_memory_object_stream[SessionMessage](0)
            server_task: Final = asyncio.create_task(
                self._server._lowlevel_server.run(
                    server_receive,
                    server_to_client_send,
                    self._server._lowlevel_server.create_initialization_options(),
                )
            )
            try:
                yield client_receive, client_to_server_send
            finally:
                await client_to_server_send.aclose()
                await client_receive.aclose()
                await asyncio.wait_for(server_task, 5)

        return connected(), None


class _ElicitationAnswer(BaseModel):
    label: str

    model_config = ConfigDict(frozen=True)


def _stateful_upstream() -> Starlette:
    service: Final = UpstreamServer("stateful")
    selected: Final[dict[str, str]] = {}

    @service.tool()
    def select_project(name: str, ctx: Context) -> str:
        selected[ctx.headers["mcp-session-id"]] = name
        return f"selected {name}"

    @service.tool()
    def create_feature(title: str, ctx: Context) -> str:
        return f"{selected[ctx.headers['mcp-session-id']]}/{title}"

    return service.streamable_http_app(
        stateless_http=False,
        json_response=True,
        transport_security=TransportSecuritySettings(enable_dns_rebinding_protection=False),
    )


def _upstream_with_status_for_method(method: str, status_code: int) -> Starlette:
    service: Final = UpstreamServer("status upstream")

    @service.tool()
    def protected_tool() -> str:
        return "protected"

    app: Final = service.streamable_http_app(
        stateless_http=False,
        json_response=True,
        transport_security=TransportSecuritySettings(enable_dns_rebinding_protection=False),
    )

    async def reject_method(request: Request, call_next: RequestResponseEndpoint) -> Response:
        if request.method == "POST":
            payload: Final = await request.json()
            if isinstance(payload, dict) and payload.get("method") == method:
                return Response(status_code=status_code, headers={"WWW-Authenticate": "Bearer"})
        return await call_next(request)

    app.add_middleware(BaseHTTPMiddleware, dispatch=reject_method)
    return app


def _upstream_with_malformed_response_for_method(method: str) -> Starlette:
    service: Final = UpstreamServer("malformed response upstream")

    @service.tool()
    def malformed_tool() -> str:
        return "unreachable"

    app: Final = service.streamable_http_app(
        stateless_http=False,
        json_response=True,
        transport_security=TransportSecuritySettings(enable_dns_rebinding_protection=False),
    )

    async def malformed_method(request: Request, call_next: RequestResponseEndpoint) -> Response:
        if request.method == "GET":
            return Response(content="data: {}\n\n", media_type="text/event-stream")
        if request.method == "POST":
            payload: Final = await request.json()
            if isinstance(payload, dict) and payload.get("method") == method:
                return Response(content="not a JSON-RPC message", media_type="text/event-stream")
        return await call_next(request)

    app.add_middleware(BaseHTTPMiddleware, dispatch=malformed_method)
    return app


@pytest.mark.asyncio
async def test_persistent_session_keeps_upstream_state_across_tool_calls() -> None:
    app: Final = _stateful_upstream()
    async with app.router.lifespan_context(app):
        client: Final = _StatefulUpstreamClient(app, server_url="http://upstream/mcp", transport_type=MCPTransport.http)
        per_call: Final = await client.call_tool(CallToolRequestParams(name="select_project", arguments={"name": "a"}))
        assert per_call.is_error is False
        fresh: Final = await client.call_tool(CallToolRequestParams(name="create_feature", arguments={"title": "b"}))
        assert fresh.is_error is True, "a fresh upstream session per call must not see the earlier selection"

        session: Final = client.open_persistent_session()
        try:
            selected: Final = await client.call_tool(
                CallToolRequestParams(name="select_project", arguments={"name": "a"}), persistent_session=session
            )
            created: Final = await client.call_tool(
                CallToolRequestParams(name="create_feature", arguments={"title": "b"}), persistent_session=session
            )
        finally:
            session.close()
        assert selected.is_error is False
        assert created.is_error is False
        assert created.content[0].text == "a/b"
        await asyncio.wait_for(session.wait_closed(), 5)
        assert session.closed


def _client_with_session(
    app: Starlette,
    admission: Callable[[], AbstractAsyncContextManager[None]] | None = None,
) -> tuple[_StatefulUpstreamClient, PersistentMCPSession]:
    client: Final = _StatefulUpstreamClient(app, server_url="http://upstream/mcp", transport_type=MCPTransport.http)
    return client, client.open_persistent_session(admission)


@pytest.mark.asyncio
async def test_cancelling_one_waiter_for_session_readiness_does_not_cancel_other_waiters() -> None:
    app: Final = _stateful_upstream()
    async with app.router.lifespan_context(app):
        initialize_started: Final = asyncio.Event()
        release_initialize: Final = asyncio.Event()

        async def gated_server(scope: Scope, receive: Receive, send: Send) -> None:
            async def gated_send(message: Message) -> None:
                if message["type"] == "http.response.body" and not initialize_started.is_set():
                    initialize_started.set()
                    await release_initialize.wait()
                await send(message)

            await app(scope, receive, gated_send)

        gated_app: Final[ASGIApp] = gated_server
        client: Final = _StatefulUpstreamClient(
            gated_app, server_url="http://upstream/mcp", transport_type=MCPTransport.http
        )
        session: Final = client.open_persistent_session()

        async def operation(_: ClientSession) -> str:
            return "ready"

        cancelled_waiter: Final = asyncio.create_task(session.run(operation))
        remaining_waiter: Final = asyncio.create_task(session.run(operation))
        try:
            await asyncio.wait_for(initialize_started.wait(), 5)
            await asyncio.sleep(0)
            cancelled_waiter.cancel()
            with pytest.raises(asyncio.CancelledError):
                await cancelled_waiter
            release_initialize.set()
            assert await asyncio.wait_for(remaining_waiter, 5) == "ready"
            assert not session.closed
        finally:
            release_initialize.set()
            session.close()
            await asyncio.wait_for(session.wait_closed(), 5)


@pytest.mark.asyncio
async def test_persistent_session_survives_a_caller_timeout_on_one_operation() -> None:
    app: Final = _stateful_upstream()
    async with app.router.lifespan_context(app):
        client, session = _client_with_session(app)
        try:
            with pytest.raises(asyncio.TimeoutError):
                await asyncio.wait_for(
                    client.call_tool(
                        CallToolRequestParams(name="select_project", arguments={"name": "a"}),
                        persistent_session=session,
                    ),
                    timeout=0,
                )
            selected: Final = await client.call_tool(
                CallToolRequestParams(name="select_project", arguments={"name": "c"}), persistent_session=session
            )
            created: Final = await client.call_tool(
                CallToolRequestParams(name="create_feature", arguments={"title": "d"}), persistent_session=session
            )
        finally:
            session.close()
        assert selected.is_error is False, "the session must outlive a timed out call"
        assert created.content[0].text == "c/d"
        await asyncio.wait_for(session.wait_closed(), 5)


@pytest.mark.asyncio
async def test_persistent_session_survives_recoverable_mcp_errors_and_closes_on_connection_closed() -> None:
    app: Final = _stateful_upstream()
    async with app.router.lifespan_context(app):
        client, session = _client_with_session(app)

        async def invalid_operation(_: ClientSession) -> str:
            raise MCPError(code=-32602, message="invalid params")

        try:
            with pytest.raises(MCPError):
                await session.run(invalid_operation)
            assert not session.closed
            selected: Final = await client.call_tool(
                CallToolRequestParams(name="select_project", arguments={"name": "recoverable"}),
                persistent_session=session,
            )
            created: Final = await client.call_tool(
                CallToolRequestParams(name="create_feature", arguments={"title": "still-open"}),
                persistent_session=session,
            )
        finally:
            session.close()
        assert selected.is_error is False
        assert created.content[0].text == "recoverable/still-open"
        await asyncio.wait_for(session.wait_closed(), 5)

    connection_app: Final = _stateful_upstream()
    async with connection_app.router.lifespan_context(connection_app):
        _, session = _client_with_session(connection_app)
        started: Final = asyncio.Event()
        release: Final = asyncio.Event()

        async def disconnected_operation(_: ClientSession) -> str:
            started.set()
            await release.wait()
            raise MCPError(code=CONNECTION_CLOSED, message="upstream connection closed")

        async def pending_operation(_: ClientSession) -> str:
            return "unused"

        active: Final = asyncio.create_task(session.run(disconnected_operation))
        await asyncio.wait_for(started.wait(), 5)
        pending: Final = asyncio.create_task(session.run(pending_operation))
        await asyncio.sleep(0)
        release.set()
        with pytest.raises(MCPError) as caught:
            await asyncio.wait_for(active, 5)
        assert caught.value.error.code == CONNECTION_CLOSED
        with pytest.raises((MCPError, UpstreamSessionClosedError)):
            await asyncio.wait_for(pending, 5)
        await asyncio.wait_for(session.wait_closed(), 5)
        assert session.closed


@pytest.mark.asyncio
async def test_ended_persistent_session_is_not_reusable_during_transport_cleanup() -> None:
    app: Final = _stateful_upstream()
    async with app.router.lifespan_context(app):
        cleanup_started: Final = asyncio.Event()
        release_cleanup: Final = asyncio.Event()
        client: Final = _CleanupHoldingStatefulUpstreamClient(
            app,
            server_url="http://upstream/mcp",
            transport_type=MCPTransport.http,
            cleanup_started=cleanup_started,
            release_cleanup=release_cleanup,
        )
        session: Final = client.open_persistent_session()

        async def disconnected_operation(_: ClientSession) -> str:
            raise MCPError(code=CONNECTION_CLOSED, message="upstream connection closed")

        try:
            with pytest.raises(MCPError) as caught:
                await asyncio.wait_for(session.run(disconnected_operation), 5)
            assert caught.value.error.code == CONNECTION_CLOSED
            await asyncio.wait_for(cleanup_started.wait(), 5)

            assert not session.reusable
            assert not session.closed
            session.retire()

            release_cleanup.set()
            await asyncio.wait_for(session.wait_closed(), 5)
            assert session.closed
        finally:
            release_cleanup.set()
            if not session.closed:
                session.close()
            await asyncio.wait_for(session.wait_closed(), 5)


@pytest.mark.asyncio
async def test_persistent_tool_call_preserves_upstream_http_status_error() -> None:
    app: Final = _upstream_with_status_for_method("tools/call", 401)
    async with app.router.lifespan_context(app):
        client: Final = _StatefulUpstreamClient(
            app,
            server_url="http://upstream/mcp",
            transport_type=MCPTransport.http,
        )
        session: Final = client.open_persistent_session()
        try:
            with pytest.raises(httpx2.HTTPStatusError) as caught:
                await client.call_tool(
                    CallToolRequestParams(name="protected_tool", arguments={}),
                    raise_on_error=True,
                    persistent_session=session,
                )
            assert caught.value.response.status_code == 401
            assert not session.reusable
        finally:
            session.close()
        await asyncio.wait_for(session.wait_closed(), 5)


@pytest.mark.asyncio
@pytest.mark.parametrize("status_code", [429, 503])
async def test_persistent_tool_call_classifies_initialized_notification_status(status_code: int) -> None:
    client: Final = _MockTransportClient(
        _post_status_responder("notifications/initialized", status_code),
        server_url="https://example.com/mcp",
    )
    session: Final = client.open_persistent_session()
    try:
        with pytest.raises(httpx2.HTTPStatusError) as caught:
            await client.call_tool(
                CallToolRequestParams(name="protected_tool", arguments={}),
                raise_on_error=True,
                persistent_session=session,
            )

        assert classify_list_exception(caught.value) == ServerListFault(tag="upstream_error", status_code=status_code)
        rate_limit: Final = upstream_rate_limit(caught.value)
        if status_code == 429:
            assert rate_limit is not None
            assert rate_limit.retry_after_seconds == 7
        else:
            assert rate_limit is None
    finally:
        if not session.closed:
            session.close()
        await asyncio.wait_for(session.wait_closed(), 5)


@pytest.mark.asyncio
async def test_persistent_session_remaps_stream_error_over_mcp_error() -> None:
    app: Final = _stateful_upstream()
    async with app.router.lifespan_context(app):
        client: Final = _StatefulUpstreamClient(
            app,
            server_url="http://upstream/mcp",
            transport_type=MCPTransport.http,
        )
        session: Final = client.open_persistent_session()
        request: Final = httpx2.Request("POST", "http://upstream/mcp")
        response: Final = httpx2.Response(401, request=request, headers={"WWW-Authenticate": "Bearer"})
        http_error: Final = httpx2.HTTPStatusError("401 Unauthorized", request=request, response=response)

        async def operation(_: ClientSession) -> str:
            stream_error: Final[asyncio.Future[Exception] | None] = session._stream_error
            assert stream_error is not None
            stream_error.set_result(http_error)
            raise MCPError(code=-32603, message="response stream closed")

        try:
            with pytest.raises(httpx2.HTTPStatusError) as caught:
                await session.run(operation)
            assert caught.value is http_error
            assert not session.reusable
        finally:
            session.close()
            await asyncio.wait_for(session.wait_closed(), 5)


@pytest.mark.asyncio
async def test_persistent_session_ends_when_a_malformed_response_is_remapped() -> None:
    app: Final = _upstream_with_malformed_response_for_method("tools/call")
    async with app.router.lifespan_context(app):
        client: Final = _StatefulUpstreamClient(
            app,
            server_url="http://upstream/mcp",
            transport_type=MCPTransport.http,
        )
        session: Final = client.open_persistent_session()
        try:

            async def ready(_: ClientSession) -> str:
                return "ready"

            assert await asyncio.wait_for(session.run(ready), 5) == "ready"
            stream_error: Final[asyncio.Future[Exception] | None] = session._stream_error
            assert stream_error is not None
            recorded_stream_error: Final = await asyncio.wait_for(asyncio.shield(stream_error), 5)
            assert isinstance(recorded_stream_error, ValidationError)

            with pytest.raises(ValidationError) as caught:
                await client.call_tool(
                    CallToolRequestParams(name="malformed_tool", arguments={}),
                    raise_on_error=True,
                    persistent_session=session,
                )
            assert caught.value is recorded_stream_error
            assert not session.reusable
            await asyncio.wait_for(session.wait_closed(), 5)
            assert session.closed
        finally:
            if not session.closed:
                session.close()
                await asyncio.wait_for(session.wait_closed(), 5)


@pytest.mark.asyncio
async def test_failed_persistent_initialize_is_not_reusable_during_transport_cleanup() -> None:
    app: Final = _upstream_with_status_for_method("initialize", 500)
    async with app.router.lifespan_context(app):
        cleanup_started: Final = asyncio.Event()
        release_cleanup: Final = asyncio.Event()
        client: Final = _CleanupHoldingStatefulUpstreamClient(
            app,
            server_url="http://upstream/mcp",
            transport_type=MCPTransport.http,
            cleanup_started=cleanup_started,
            release_cleanup=release_cleanup,
        )
        session: Final = client.open_persistent_session()
        pending: Final = asyncio.create_task(session.run(lambda _: "ready"))
        try:
            await asyncio.wait_for(cleanup_started.wait(), 5)
            assert not session.reusable
            assert not session.closed
            release_cleanup.set()
            with pytest.raises(httpx2.HTTPStatusError):
                await asyncio.wait_for(pending, 5)
            await asyncio.wait_for(session.wait_closed(), 5)
            assert session.closed
        finally:
            release_cleanup.set()
            if not session.closed:
                session.close()
            await asyncio.wait_for(session.wait_closed(), 5)


@pytest.mark.asyncio
async def test_persistent_session_routes_elicitation_to_the_current_clients_callback() -> None:
    service: Final = UpstreamServer("elicitation")

    @service.tool()
    async def ask_for_label(ctx: Context) -> str:
        response: Final = await ctx.elicit("What label should be used?", _ElicitationAnswer)
        return response.data.label if response.data is not None else response.action

    async def first_callback(context: ClientRequestContext, params: ElicitRequestParams) -> ElicitResult:
        return ElicitResult(action="accept", content={"label": "first"})

    async def second_callback(context: ClientRequestContext, params: ElicitRequestParams) -> ElicitResult:
        return ElicitResult(action="accept", content={"label": "second"})

    first_client: Final = _InProcessUpstreamClient(service, elicitation_callback=first_callback)
    second_client: Final = _InProcessUpstreamClient(service, elicitation_callback=second_callback)
    session: Final = first_client.open_persistent_session()
    try:
        first: Final = await first_client.call_tool(
            CallToolRequestParams(name="ask_for_label", arguments={}), persistent_session=session
        )
        second: Final = await second_client.call_tool(
            CallToolRequestParams(name="ask_for_label", arguments={}), persistent_session=session
        )
    finally:
        session.close()
    assert first.is_error is False and first.content[0].text == "first"
    assert second.is_error is False and second.content[0].text == "second"
    await asyncio.wait_for(session.wait_closed(), 5)


@pytest.mark.asyncio
async def test_persistent_session_without_elicitation_callback_uses_sdk_default() -> None:
    service: Final = UpstreamServer("elicitation default")

    @service.tool()
    async def ask_for_label(ctx: Context) -> str:
        response: Final = await ctx.elicit("What label should be used?", _ElicitationAnswer)
        return response.data.label if response.data is not None else response.action

    client: Final = _InProcessUpstreamClient(service)
    per_call: Final = await client.call_tool(CallToolRequestParams(name="ask_for_label", arguments={}))
    session: Final = client.open_persistent_session()
    try:
        persistent: Final = await client.call_tool(
            CallToolRequestParams(name="ask_for_label", arguments={}), persistent_session=session
        )
    finally:
        session.close()
    await asyncio.wait_for(session.wait_closed(), 5)
    assert persistent.model_dump(mode="json") == per_call.model_dump(mode="json")
    assert "elicitation callback is unavailable" not in str(persistent.model_dump(mode="json"))


@pytest.mark.asyncio
async def test_cancelled_queued_operation_does_not_run_and_session_accepts_a_later_operation() -> None:
    app: Final = _stateful_upstream()
    async with app.router.lifespan_context(app):
        _, session = _client_with_session(app)
        first_started: Final = asyncio.Event()
        release_first: Final = asyncio.Event()
        cancelled_operation_ran: Final = asyncio.Event()

        async def first_operation(_: ClientSession) -> str:
            first_started.set()
            await release_first.wait()
            return "first"

        async def cancelled_operation(_: ClientSession) -> str:
            cancelled_operation_ran.set()
            return "cancelled"

        async def third_operation(_: ClientSession) -> str:
            return "third"

        first_task: Final = asyncio.create_task(session.run(first_operation))
        try:
            await asyncio.wait_for(first_started.wait(), 5)
            second_task: Final = asyncio.create_task(session.run(cancelled_operation))
            await asyncio.sleep(0)
            assert session._queue.qsize() == 1
            second_task.cancel()
            with pytest.raises(asyncio.CancelledError):
                await second_task
            third_task: Final = asyncio.create_task(session.run(third_operation))
            await asyncio.sleep(0)
            assert session._queue.qsize() == 2
            release_first.set()
            assert await asyncio.wait_for(first_task, 5) == "first"
            assert await asyncio.wait_for(third_task, 5) == "third"
            assert not cancelled_operation_ran.is_set()
            assert not session.closed
        finally:
            release_first.set()
            session.close()
            await asyncio.wait_for(session.wait_closed(), 5)


@pytest.mark.asyncio
async def test_retiring_persistent_session_drains_in_flight_and_queued_operations() -> None:
    app: Final = _stateful_upstream()
    async with app.router.lifespan_context(app):
        _, session = _client_with_session(app)
        first_started: Final = asyncio.Event()
        release_first: Final = asyncio.Event()

        async def first_operation(_: ClientSession) -> str:
            first_started.set()
            await release_first.wait()
            return "first"

        async def queued_operation(_: ClientSession) -> str:
            return "queued"

        first_waiter: Final = asyncio.create_task(session.run(first_operation))
        try:
            await asyncio.wait_for(first_started.wait(), 5)
            queued_waiter: Final = asyncio.create_task(session.run(queued_operation))
            await asyncio.sleep(0)
            assert session._queue.qsize() == 1
            session.retire()
            release_first.set()

            results: Final = await asyncio.wait_for(asyncio.gather(first_waiter, queued_waiter), 5)
            assert results == ["first", "queued"]
            await asyncio.wait_for(session.wait_closed(), 5)
            assert session.closed
        finally:
            release_first.set()
            if not session.closed:
                session.close()
            await asyncio.wait_for(session.wait_closed(), 5)


@pytest.mark.asyncio
async def test_timed_out_persistent_operation_waiting_for_admission_is_skipped() -> None:
    app: Final = _stateful_upstream()
    async with app.router.lifespan_context(app):
        admission_slots: Final = asyncio.Semaphore(1)
        admission_requested: Final = asyncio.Event()

        def admission() -> AbstractAsyncContextManager[None]:
            admission_requested.set()
            return admission_slots

        _, session = _client_with_session(app, admission)
        operation_ran: Final = asyncio.Event()

        async def warmup(_: ClientSession) -> str:
            return "ready"

        async def timed_out_operation(_: ClientSession) -> str:
            operation_ran.set()
            return "expired"

        async def later_operation(_: ClientSession) -> str:
            return "later"

        try:
            assert await session.run(warmup) == "ready"
            admission_requested.clear()
            async with admission_slots:
                timed_out_call: Final = asyncio.create_task(session.run(timed_out_operation))
                await asyncio.wait_for(admission_requested.wait(), 1)
                with pytest.raises(asyncio.TimeoutError):
                    await asyncio.wait_for(timed_out_call, 0.05)
                assert not operation_ran.is_set()

            later_result: Final = await asyncio.wait_for(session.run(later_operation), 1)

            assert later_result == "later"
            assert session.reusable
            assert not operation_ran.is_set()
        finally:
            session.close()
            await asyncio.wait_for(session.wait_closed(), 5)


@pytest.mark.asyncio
async def test_persistent_session_holds_admission_until_initialize_completes() -> None:
    app: Final = _stateful_upstream()
    async with app.router.lifespan_context(app):
        initialize_seen: Final = asyncio.Event()
        admission_slots: Final = asyncio.Semaphore(1)

        async def observing_app(scope: Scope, receive: Receive, send: Send) -> None:
            async def observe_send(message: Message) -> None:
                if message["type"] == "http.response.body":
                    initialize_seen.set()
                await send(message)

            await app(scope, receive, observe_send)

        client: Final = _StatefulUpstreamClient(
            observing_app,
            server_url="http://upstream/mcp",
            transport_type=MCPTransport.http,
        )
        session: Final = client.open_persistent_session(lambda: admission_slots)

        async def operation(_: ClientSession) -> str:
            return "ready"

        run_task: Final = asyncio.create_task(session.run(operation))
        try:
            async with admission_slots:
                with pytest.raises(asyncio.TimeoutError):
                    await asyncio.wait_for(initialize_seen.wait(), 0.05)
                assert not session._ready.done()
                assert not run_task.done()

            result: Final = await asyncio.wait_for(run_task, 5)
            assert result == "ready"
            assert initialize_seen.is_set()
        finally:
            session.close()
            await asyncio.wait_for(session.wait_closed(), 5)
            await asyncio.gather(run_task, return_exceptions=True)


@pytest.mark.asyncio
async def test_persistent_session_releases_admission_when_connect_fails() -> None:
    async def failing_app(_: Scope, __: Receive, ___: Send) -> None:
        raise httpx2.ConnectError("connect failed")

    client: Final = _StatefulUpstreamClient(
        failing_app,
        server_url="http://upstream/mcp",
        transport_type=MCPTransport.http,
    )
    admission_slots: Final = asyncio.Semaphore(1)
    session: Final = client.open_persistent_session(lambda: admission_slots)

    async def operation(_: ClientSession) -> str:
        return "unreachable"

    try:
        with pytest.raises(httpx2.ConnectError, match="connect failed"):
            await asyncio.wait_for(session.run(operation), 1)
        await asyncio.wait_for(session.wait_closed(), 1)

        await asyncio.wait_for(admission_slots.acquire(), 0.1)
        admission_slots.release()
    finally:
        session.close()
        await asyncio.wait_for(session.wait_closed(), 5)


@pytest.mark.asyncio
async def test_failed_connect_is_not_reusable_during_admission_teardown() -> None:
    connect_failed: Final = asyncio.Event()
    admission_exit_started: Final = asyncio.Event()
    release_admission_exit: Final = asyncio.Event()

    async def failing_app(_: Scope, __: Receive, ___: Send) -> None:
        connect_failed.set()
        raise httpx2.ConnectError("connect failed")

    @asynccontextmanager
    async def blocking_admission() -> AsyncIterator[None]:
        try:
            yield
        finally:
            admission_exit_started.set()
            await release_admission_exit.wait()

    client: Final = _StatefulUpstreamClient(
        failing_app,
        server_url="http://upstream/mcp",
        transport_type=MCPTransport.http,
    )
    session: Final = client.open_persistent_session(blocking_admission)

    async def operation(_: ClientSession) -> str:
        return "unreachable"

    run_task: Final = asyncio.create_task(session.run(operation))
    try:
        await asyncio.wait_for(connect_failed.wait(), 1)
        await asyncio.wait_for(admission_exit_started.wait(), 1)

        assert not run_task.done()
        assert not session.reusable
        assert not session.closed

        release_admission_exit.set()
        with pytest.raises(httpx2.ConnectError, match="connect failed"):
            await asyncio.wait_for(run_task, 1)
        await asyncio.wait_for(session.wait_closed(), 1)
        assert session.closed
    finally:
        release_admission_exit.set()
        session.close()
        await asyncio.wait_for(session.wait_closed(), 5)
        await asyncio.gather(run_task, return_exceptions=True)


@pytest.mark.asyncio
async def test_persistent_session_admission_is_acquired_only_when_operation_runs() -> None:
    app: Final = _stateful_upstream()
    async with app.router.lifespan_context(app):
        admission_slots: Final = asyncio.Semaphore(2)
        _, session = _client_with_session(app, lambda: admission_slots)
        first_started: Final = asyncio.Event()
        release_first: Final = asyncio.Event()
        operation_order: Final[list[str]] = []

        async def first_operation(_: ClientSession) -> str:
            operation_order.append("first")
            first_started.set()
            await release_first.wait()
            return "first"

        async def second_operation(_: ClientSession) -> str:
            operation_order.append("second")
            return "second"

        async def third_operation(_: ClientSession) -> str:
            operation_order.append("third")
            return "third"

        first_task: Final = asyncio.create_task(session.run(first_operation))
        second_task: Final = asyncio.create_task(session.run(second_operation))
        third_task: Final = asyncio.create_task(session.run(third_operation))
        try:
            await asyncio.wait_for(first_started.wait(), 5)
            await asyncio.sleep(0)
            assert session._queue.qsize() == 2
            await asyncio.wait_for(admission_slots.acquire(), 0.1)
            try:
                assert admission_slots.locked()
            finally:
                admission_slots.release()
            assert operation_order == ["first"]

            release_first.set()
            results: Final = await asyncio.wait_for(
                asyncio.gather(first_task, second_task, third_task),
                5,
            )

            assert results == ["first", "second", "third"]
            assert operation_order == ["first", "second", "third"]
        finally:
            release_first.set()
            session.close()
            await asyncio.wait_for(session.wait_closed(), 5)
            await asyncio.gather(first_task, second_task, third_task, return_exceptions=True)


@pytest.mark.asyncio
async def test_retiring_idle_persistent_session_closes_and_rejects_new_operations() -> None:
    app: Final = _stateful_upstream()
    async with app.router.lifespan_context(app):
        _, session = _client_with_session(app)

        async def operation(_: ClientSession) -> str:
            return "ready"

        try:
            assert await session.run(operation) == "ready"
            session.retire()
            await asyncio.wait_for(session.wait_closed(), 1)

            assert session.closed
            with pytest.raises(UpstreamSessionClosedError):
                await session.run(operation)
        finally:
            if not session.closed:
                session.close()
            await asyncio.wait_for(session.wait_closed(), 5)


@pytest.mark.asyncio
async def test_closing_persistent_session_mid_operation_fails_the_waiter_instead_of_hanging() -> None:
    app: Final = _stateful_upstream()
    async with app.router.lifespan_context(app):
        _, session = _client_with_session(app)
        started: Final = asyncio.Event()

        async def slow_operation(_: object) -> str:
            started.set()
            await asyncio.sleep(30)
            return "never"

        waiter: Final = asyncio.ensure_future(session.run(slow_operation))
        await asyncio.wait_for(started.wait(), 5)
        session.close()
        with pytest.raises(RuntimeError, match="upstream MCP session closed"):
            await asyncio.wait_for(waiter, 5)
        await asyncio.wait_for(session.wait_closed(), 5)


@pytest.mark.asyncio
async def test_persistent_session_reports_an_upstream_cancellation_as_a_runtime_error_not_a_cancelled_caller() -> None:
    app: Final = _stateful_upstream()
    async with app.router.lifespan_context(app):
        client, session = _client_with_session(app)

        async def cancelled_upstream(_: object) -> str:
            raise asyncio.CancelledError()

        try:
            with pytest.raises(RuntimeError, match="cancelled"):
                await asyncio.wait_for(session.run(cancelled_upstream), 5)
            selected: Final = await client.call_tool(
                CallToolRequestParams(name="select_project", arguments={"name": "e"}), persistent_session=session
            )
        finally:
            session.close()
        assert selected.is_error is False, "an upstream cancellation must not be mistaken for a cancelled caller"
        await asyncio.wait_for(session.wait_closed(), 5)


@pytest.mark.asyncio
async def test_closing_persistent_session_fails_a_caller_blocked_on_a_full_queue_instead_of_hanging() -> None:
    from litellm.experimental_mcp_client.client import _MAX_PENDING_OPERATIONS

    app: Final = _stateful_upstream()
    async with app.router.lifespan_context(app):
        _, session = _client_with_session(app)
        started: Final = asyncio.Event()

        async def slow_operation(_: object) -> str:
            started.set()
            await asyncio.sleep(30)
            return "never"

        waiters: Final = tuple(
            asyncio.ensure_future(session.run(slow_operation)) for _ in range(_MAX_PENDING_OPERATIONS + 2)
        )
        await asyncio.wait_for(started.wait(), 5)
        await asyncio.sleep(0)
        session.close()
        outcomes: Final = await asyncio.wait_for(asyncio.gather(*waiters, return_exceptions=True), 5)
        assert all(isinstance(outcome, RuntimeError) for outcome in outcomes), outcomes
        await asyncio.wait_for(session.wait_closed(), 5)


@pytest.mark.asyncio
async def test_persistent_session_ends_after_a_broken_stream_so_the_next_call_gets_a_fresh_one() -> None:
    app: Final = _stateful_upstream()
    async with app.router.lifespan_context(app):
        _, session = _client_with_session(app)

        async def broken_stream(_: object) -> str:
            raise anyio.BrokenResourceError()

        with pytest.raises(anyio.BrokenResourceError):
            await asyncio.wait_for(session.run(broken_stream), 5)
        await asyncio.wait_for(session.wait_closed(), 5)
        assert session.closed, "a dead transport must end the session instead of being reused"


@pytest.mark.asyncio
@pytest.mark.parametrize("session_started_in_request", [True, False])
async def test_persistent_session_releases_the_current_requests_scope_response(session_started_in_request) -> None:
    from mcp.server.context import ServerRequestContext
    from litellm.proxy._experimental.mcp_server.mcp_context import active_mcp_request_ctx_var
    from litellm.proxy._experimental.mcp_server.scope_challenge import SCOPE_RESPONSE_KEY, OAuthScopeResponse

    app = _stateful_upstream()
    async with app.router.lifespan_context(app):
        client = _StatefulUpstreamClient(app, server_url="http://upstream/mcp", transport_type=MCPTransport.http)
        first = OAuthScopeResponse(AsyncMock(), "https://gateway.example", "/mcp")
        second = OAuthScopeResponse(AsyncMock(), "https://gateway.example", "/mcp")
        context = ServerRequestContext(
            session=MagicMock(),
            lifespan_context={},
            protocol_version="2025-11-25",
            method="tools/call",
            request=Request({"type": "http", SCOPE_RESPONSE_KEY: first}),
        )
        earlier_session = None if session_started_in_request else client.open_persistent_session()
        token = active_mcp_request_ctx_var.set(context)
        session = earlier_session if earlier_session is not None else client.open_persistent_session()
        try:
            selected = await client.call_tool(
                CallToolRequestParams(name="select_project", arguments={"name": "demo"}), persistent_session=session
            )
            assert not selected.is_error and not first.pending
            active_mcp_request_ctx_var.set(
                ServerRequestContext(
                    session=context.session,
                    lifespan_context={},
                    protocol_version="2025-11-25",
                    method="tools/call",
                    request=Request({"type": "http", SCOPE_RESPONSE_KEY: second}),
                )
            )
            created = await client.call_tool(
                CallToolRequestParams(name="create_feature", arguments={"title": "feature"}), persistent_session=session
            )
            assert not created.is_error and created.content[0].text == "demo/feature"
            assert not second.pending
        finally:
            active_mcp_request_ctx_var.reset(token)
            session.close()
            await asyncio.wait_for(session.wait_closed(), 5)

@pytest.mark.asyncio
@pytest.mark.parametrize("legacy_callback", [False, True])
async def test_tool_progress_preserves_message_and_legacy_callback(legacy_callback: bool) -> None:
    received = AsyncMock()

    async def legacy(progress: float, total: float | None) -> None:
        await received(progress, total)

    async def modern(progress: float, total: float | None, message: str | None) -> None:
        await received(progress, total, message)

    async def call_tool(**kwargs):
        await kwargs["progress_callback"](1.0, 2.0, "halfway")
        return CallToolResult(content=[])

    session = AsyncMock(spec=ClientSession, call_tool=AsyncMock(side_effect=call_tool))

    async def run(operation, **kwargs):
        return await operation(session)

    persistent = MagicMock(spec=PersistentMCPSession, run=AsyncMock(side_effect=run))
    client = MCPClient(server_url="https://example.com/mcp")
    result = await client.call_tool(
        CallToolRequestParams(name="slow"),
        host_progress_callback=legacy if legacy_callback else modern,
        persistent_session=persistent,
    )
    assert not result.is_error
    if legacy_callback:
        received.assert_awaited_once_with(1.0, 2.0)
    else:
        received.assert_awaited_once_with(1.0, 2.0, "halfway")


@pytest.mark.asyncio
async def test_cancelling_persistent_operation_stops_work_and_keeps_session_usable() -> None:
    app: Final = _stateful_upstream()
    async with app.router.lifespan_context(app):
        client, session = _client_with_session(app)
        started: Final = asyncio.Event()
        stopped: Final = asyncio.Event()

        async def slow_operation(_: ClientSession) -> None:
            started.set()
            try:
                await asyncio.Event().wait()
            finally:
                stopped.set()

        waiter: Final = asyncio.create_task(session.run(slow_operation))
        try:
            await asyncio.wait_for(started.wait(), 5)
            waiter.cancel()
            with pytest.raises(asyncio.CancelledError):
                await waiter
            await asyncio.sleep(0.1)
            assert stopped.is_set(), "Caller cancellation must stop the running upstream operation"
            result: Final = await asyncio.wait_for(
                client.call_tool(
                    CallToolRequestParams(name="select_project", arguments={"name": "after-cancellation"}),
                    persistent_session=session,
                ),
                5,
            )
            assert not result.is_error
        finally:
            session.close()
            await asyncio.wait_for(session.wait_closed(), 5)
            await asyncio.gather(waiter, return_exceptions=True)


@pytest.mark.asyncio
@pytest.mark.parametrize("header_name", ["Authorization", "x-upstream-token"])
async def test_subscription_fingerprint_preserves_context_across_managed_renewal(header_name):
    from litellm.proxy._experimental.mcp_server.outbound_credentials.client_credentials import ClientCredentialsBearerAuth
    from litellm.proxy._experimental.mcp_server.outbound_credentials.types import ClientCredentialsConfig

    async def refetch(token: str) -> str | None:
        return None

    config = ClientCredentialsConfig(client_id="service", header_name=header_name)

    def client(token, *, scopes=("read",), url="https://upstream.example/mcp", tenant="alice"):
        return MCPClient(
            server_url=url,
            extra_headers={"x-tenant": tenant},
            resolved_auth=ClientCredentialsBearerAuth(token, refetch, config, granted_scopes=scopes),
        )

    original = client("original")
    fingerprint = await original.subscription_auth_fingerprint()
    assert fingerprint == await client("renewed").subscription_auth_fingerprint()
    assert fingerprint != await client("renewed", scopes=("write",)).subscription_auth_fingerprint()
    assert fingerprint != await client("renewed", url="https://other.example/mcp").subscription_auth_fingerprint()
    assert fingerprint != await client("renewed", tenant="bob").subscription_auth_fingerprint()
    assert await original.discovery_auth_fingerprint() != await client("renewed").discovery_auth_fingerprint()
    assert fingerprint != await client("renewed", scopes=None).subscription_auth_fingerprint()
    assert await client("original", scopes=None).subscription_auth_fingerprint() != await client("renewed", scopes=None).subscription_auth_fingerprint()
    assert (await original.prepare_request_auth()).headers[header_name] == "Bearer original"


@pytest.mark.asyncio
async def test_notification_callback_keeps_stream_errors_separate_from_protocol_errors() -> None:
    from litellm.experimental_mcp_client.client import _receive_session_message

    stream_error = asyncio.get_running_loop().create_future()
    receiver = AsyncMock()
    await _receive_session_message(
        MCPError(code=-32602, message="Unsupported request"), stream_error=stream_error, on_notification=receiver
    )
    assert not stream_error.done()
    receiver.assert_not_awaited()
    first = OSError("Original transport failure")
    for failure in (first, ValueError("Later parser failure")):
        with pytest.raises(RuntimeError, match="MCP response stream failed"):
            await _receive_session_message(failure, stream_error=stream_error, on_notification=receiver)
    assert stream_error.result() is first
    receiver.assert_not_awaited()