Skip to content

"Vibe-Trading: Your Personal Trading Agent"

29.2 KB
Raw Blame
"""Unit tests for the MCP client adapter core."""

from __future__ import annotations

import json
import logging
from dataclasses import dataclass
from datetime import date, datetime, timezone
from decimal import Decimal
from enum import Enum
from pathlib import Path
from typing import Any
from uuid import UUID

import httpx
import pytest
from fastmcp import Client, FastMCP
from fastmcp.client.client import CallToolResult
from fastmcp.exceptions import McpError, ToolError
from mcp import types as mcp_types
from pydantic import BaseModel, field_serializer

import src.tools.mcp as mcp_module
from src.config.schema import MCPServerConfig
from src.tools.mcp import (
    MCPServerAdapter,
    build_mcp_tool_wrappers,
    format_mcp_server_name_collision_warning,
    make_mcp_tool_name,
    normalize_mcp_tool_schema,
    resolve_mcp_server_tool_name_segments,
)


class _FakeClient:
    def __init__(self, state: dict[str, Any]) -> None:
        self._state = state

    async def __aenter__(self) -> "_FakeClient":
        return self

    async def __aexit__(self, exc_type, exc, tb) -> bool | None:
        return None

    async def list_tools(self) -> list[mcp_types.Tool]:
        self._state["list_calls"] += 1
        outcome = self._state["list_outcomes"].pop(0)
        if isinstance(outcome, Exception):
            raise outcome
        return outcome

    async def call_tool(
        self,
        name: str,
        arguments: dict[str, Any] | None = None,
        *,
        timeout: float | int | None = None,
        raise_on_error: bool = False,
    ) -> CallToolResult:
        self._state["call_calls"] += 1
        self._state["call_records"].append({
            "name": name,
            "arguments": arguments or {},
            "timeout": timeout,
            "raise_on_error": raise_on_error,
        })
        outcome = self._state["call_outcomes"].pop(0)
        if isinstance(outcome, Exception):
            raise outcome
        return outcome


def _make_factory(state: dict[str, Any]):
    def _factory() -> _FakeClient:
        return _FakeClient(state)

    return _factory


def _make_config(**overrides: Any) -> MCPServerConfig:
    payload = {
        "command": "uvx",
        "args": ["demo-server"],
        "enabled_tools": ["*"],
        "tool_timeout": 7,
    }
    payload.update(overrides)
    return MCPServerConfig.model_validate(payload)


def test_make_mcp_tool_name_is_stable() -> None:
    assert make_mcp_tool_name("Demo Server", "Price Quote") == "mcp_demo_server_price_quote"


def test_format_mcp_server_name_collision_warning_is_operator_facing() -> None:
    message = format_mcp_server_name_collision_warning("foo-bar", "foo_bar_deadbeef")

    assert message == (
        "Configured MCP server 'foo-bar' collides with another server after local name normalization. "
        "Using local tool prefix 'mcp_foo_bar_deadbeef_<tool>' to keep generated tool names unique. "
        "Rename the server in agent config if you want a different prefix."
    )


def test_resolve_mcp_server_tool_name_segments_disambiguates_collisions_stably() -> None:
    resolved = resolve_mcp_server_tool_name_segments(["foo-bar", "foo_bar", "demo"])
    reversed_resolved = resolve_mcp_server_tool_name_segments(["demo", "foo_bar", "foo-bar"])

    assert resolved["demo"] == "demo"
    assert resolved["foo-bar"].startswith("foo_bar_")
    assert resolved["foo_bar"].startswith("foo_bar_")
    assert resolved["foo-bar"] != resolved["foo_bar"]
    assert resolved["foo-bar"] == reversed_resolved["foo-bar"]
    assert resolved["foo_bar"] == reversed_resolved["foo_bar"]


def test_resolve_mcp_server_tool_name_segments_logs_operator_warning(
    caplog,
) -> None:
    with caplog.at_level(logging.WARNING):
        resolve_mcp_server_tool_name_segments(["foo-bar", "foo_bar"])

    assert any(
        "Using local tool prefix 'mcp_foo_bar_" in record.message
        and "Rename the server in agent config" in record.message
        for record in caplog.records
    )


def test_normalize_mcp_tool_schema_collapses_nullable_object() -> None:
    schema = normalize_mcp_tool_schema(
        {
            "anyOf": [
                {
                    "type": "object",
                    "properties": {
                        "symbol": {"type": ["string", "null"]},
                    },
                    "required": ["symbol"],
                },
                {"type": "null"},
            ]
        }
    )

    assert schema["type"] == "object"
    assert schema["properties"]["symbol"]["type"] == "string"
    assert schema["required"] == ["symbol"]


def test_normalize_mcp_tool_schema_recovers_from_malformed_properties() -> None:
    """A non-dict top-level ``properties`` (malformed remote schema) must still
    end up with the documented ``properties: {}`` fallback, not no key at all.
    """
    schema = normalize_mcp_tool_schema(
        {"type": "object", "properties": "oops", "required": ["symbol"]}
    )

    assert schema["properties"] == {}
    assert schema["required"] == ["symbol"]


def test_normalize_mcp_tool_schema_preserves_top_level_one_of_branches() -> None:
    schema = normalize_mcp_tool_schema(
        {
            "oneOf": [
                {
                    "type": "object",
                    "properties": {"symbol": {"type": "string"}},
                    "required": ["symbol"],
                },
                {
                    "type": "object",
                    "properties": {"cusip": {"type": "string"}},
                    "required": ["cusip"],
                },
            ]
        }
    )

    assert schema["type"] == "object"
    assert "oneOf" in schema
    assert schema["oneOf"][0]["properties"]["symbol"]["type"] == "string"
    assert schema["oneOf"][1]["properties"]["cusip"]["type"] == "string"


def test_build_mcp_tool_wrappers_filters_enabled_tools() -> None:
    state = {
        "list_calls": 0,
        "call_calls": 0,
        "call_records": [],
        "list_outcomes": [[
            mcp_types.Tool(name="allowed", description="Allowed", inputSchema={"type": "object"}),
            mcp_types.Tool(name="blocked", description="Blocked", inputSchema={"type": "object"}),
        ]],
        "call_outcomes": [],
    }

    tools = build_mcp_tool_wrappers(
        "demo",
        _make_config(enabled_tools=["allowed"]),
        client_factory=_make_factory(state),
    )

    assert [tool.name for tool in tools] == ["mcp_demo_allowed"]
    assert tools[0].is_readonly is False


def test_build_mcp_tool_wrappers_honors_local_server_name_override() -> None:
    state = {
        "list_calls": 0,
        "call_calls": 0,
        "call_records": [],
        "list_outcomes": [[
            mcp_types.Tool(name="quote", description="Quote", inputSchema={"type": "object"}),
        ]],
        "call_outcomes": [],
    }

    tools = build_mcp_tool_wrappers(
        "foo-bar",
        _make_config(),
        local_server_name="foo_bar_deadbeef",
        client_factory=_make_factory(state),
    )

    assert [tool.name for tool in tools] == ["mcp_foo_bar_deadbeef_quote"]


def test_remote_tool_execute_does_not_retry_timeout_and_strips_run_dir() -> None:
    state = {
        "list_calls": 0,
        "call_calls": 0,
        "call_records": [],
        "list_outcomes": [[
            mcp_types.Tool(
                name="quote",
                description="Quote lookup",
                inputSchema={
                    "type": "object",
                    "properties": {"symbol": {"type": "string"}},
                    "required": ["symbol"],
                },
            )
        ]],
        "call_outcomes": [TimeoutError("timed out")],
    }

    tool = build_mcp_tool_wrappers("demo", _make_config(), client_factory=_make_factory(state))[0]

    payload = json.loads(tool.execute(symbol="AAPL", run_dir="/tmp/run"))

    assert payload["status"] == "error"
    assert payload["server"] == "demo"
    assert payload["remote_tool"] == "quote"
    assert payload["tool"] == "mcp_demo_quote"
    assert payload["error"] == "timed out"
    assert state["call_calls"] == 1
    assert state["call_records"][0]["arguments"] == {"symbol": "AAPL"}
    assert state["call_records"][0]["timeout"] == 7
    assert state["call_records"][0]["raise_on_error"] is False


def test_remote_tool_execute_forwards_arguments_for_composed_schema() -> None:
    state = {
        "list_calls": 0,
        "call_calls": 0,
        "call_records": [],
        "list_outcomes": [[
            mcp_types.Tool(
                name="lookup",
                description="Lookup by symbol or cusip",
                inputSchema={
                    "oneOf": [
                        {
                            "type": "object",
                            "properties": {"symbol": {"type": "string"}},
                            "required": ["symbol"],
                        },
                        {
                            "type": "object",
                            "properties": {"cusip": {"type": "string"}},
                            "required": ["cusip"],
                        },
                    ]
                },
            )
        ]],
        "call_outcomes": [
            CallToolResult(content=[], structured_content={"ok": True}, meta=None, data={"ok": True}),
        ],
    }

    tool = build_mcp_tool_wrappers("demo", _make_config(), client_factory=_make_factory(state))[0]

    payload = json.loads(tool.execute(symbol="AAPL", run_dir="/tmp/run"))

    assert payload["status"] == "ok"
    assert state["call_records"][0]["arguments"] == {"symbol": "AAPL"}


def test_remote_tool_malformed_properties_still_yields_valid_schema() -> None:
    """A remote tool whose schema has a non-dict top-level ``properties`` used
    to end up with no ``properties`` key at all on ``tool.parameters`` — the
    exact object forwarded verbatim as the OpenAI-style function-calling
    schema (``to_openai_schema`` only falls back to a default when
    ``self.parameters`` is falsy, and a dict missing just one key is still
    truthy), so a malformed remote schema reached the LLM provider API as an
    invalid tool definition instead of the documented ``properties: {}``
    fallback."""
    state = {
        "list_calls": 0,
        "call_calls": 0,
        "call_records": [],
        "list_outcomes": [[
            mcp_types.Tool(
                name="lookup",
                description="Lookup by symbol",
                inputSchema={"type": "object", "properties": "oops", "required": ["symbol"]},
            )
        ]],
        "call_outcomes": [],
    }

    tool = build_mcp_tool_wrappers("demo", _make_config(), client_factory=_make_factory(state))[0]

    assert isinstance(tool.parameters.get("properties"), dict)
    assert tool.to_openai_schema()["function"]["parameters"]["properties"] == {}


@dataclass
class _RobinhoodPosition:
    """FastMCP-style generated dataclass nested in a Robinhood response."""

    symbol: str
    quantity: float
    opened_on: date
    updated_at: datetime


@dataclass
class _RobinhoodPortfolio:
    """Representative account/positions payload from Robinhood Agentic MCP."""

    account_number: str
    buying_power: float
    positions: list[_RobinhoodPosition]
    market_dates: dict[str, date]


def _execute_data_result(
    data: Any,
    *,
    structured_content: dict[str, Any] | None = None,
    meta: dict[str, Any] | None = None,
) -> dict[str, Any]:
    """Execute one fake MCP tool with a parsed FastMCP result."""
    state = {
        "list_calls": 0,
        "call_calls": 0,
        "call_records": [],
        "list_outcomes": [[
            mcp_types.Tool(
                name="get_data",
                description="Return test data",
                inputSchema={"type": "object", "properties": {}},
            )
        ]],
        "call_outcomes": [
            CallToolResult(
                content=[],
                structured_content=structured_content,
                meta=meta,
                data=data,
            )
        ],
    }
    tool = build_mcp_tool_wrappers(
        "demo",
        _make_config(enabled_tools=["get_data"]),
        client_factory=_make_factory(state),
    )[0]
    return json.loads(tool.execute())


def test_robinhood_dataclass_dates_are_json_safe_end_to_end() -> None:
    """A real FastMCP dataclass round trip must stay JSON-safe (#922)."""
    server = FastMCP("robinhood-regression")

    @server.tool
    def get_portfolio() -> _RobinhoodPortfolio:
        return _RobinhoodPortfolio(
            account_number="RH-123",
            buying_power=2500.75,
            positions=[
                _RobinhoodPosition(
                    symbol="AAPL",
                    quantity=3.5,
                    opened_on=date(2026, 7, 31),
                    updated_at=datetime(2026, 8, 1, 7, 30, tzinfo=timezone.utc),
                )
            ],
            market_dates={"next_open": date(2026, 8, 3)},
        )

    structured_portfolio = {
        "account_number": "RH-123",
        "buying_power": 2500.75,
        "positions": [
            {
                "symbol": "AAPL",
                "quantity": 3.5,
                "opened_on": "2026-07-31",
                "updated_at": "2026-08-01T07:30:00Z",
            }
        ],
        "market_dates": {"next_open": "2026-08-03"},
    }

    tool = build_mcp_tool_wrappers(
        "robinhood",
        _make_config(enabled_tools=["get_portfolio"]),
        client_factory=lambda: Client(server),
    )[0]
    payload = json.loads(tool.execute())

    assert payload["status"] == "ok"
    assert payload["data"] == structured_portfolio
    # The agent-facing result carries the value once; ``call_tool`` keeps both.
    assert "structured_content" not in payload


@pytest.mark.parametrize(
    ("data", "structured", "meta", "expected"),
    [
        (8, {"result": 8}, None, 8),
        (
            date(2026, 8, 3),
            {"result": "2026-08-03"},
            {"fastmcp": {"wrap_result": True}},
            "2026-08-03",
        ),
        (
            [date(2026, 8, 3)],
            {"result": ["2026-08-03"]},
            None,
            ["2026-08-03"],
        ),
    ],
)
def test_fastmcp_wrapped_results_keep_unwrapped_data_shape(
    data: Any,
    structured: dict[str, Any],
    meta: dict[str, Any] | None,
    expected: Any,
) -> None:
    """Keep FastMCP's primitive unwrapping across supported versions."""
    payload = _execute_data_result(
        data,
        structured_content=structured,
        meta=meta,
    )

    assert payload["data"] == expected
    assert "structured_content" not in payload


@dataclass
class _SingleResultObject:
    result: date


def test_single_result_object_schema_is_not_mistaken_for_fastmcp_wrapper() -> None:
    structured = {"result": "2026-08-03"}

    payload = _execute_data_result(
        _SingleResultObject(result=date(2026, 8, 3)),
        structured_content=structured,
    )

    assert payload["data"] == structured


def test_structured_content_remains_available_when_fastmcp_hydration_fails() -> None:
    structured = {"account_number": "RH-123", "buying_power": 2500.75}

    payload = _execute_data_result(None, structured_content=structured)

    assert payload["status"] == "ok"
    assert payload["data"] == structured
    assert "structured_content" not in payload


class _OrderState(Enum):
    OPEN = "open"


@dataclass
class _StandardJsonAdapters:
    amount: Decimal
    order_id: UUID
    output_path: Path
    state: _OrderState
    labels: set[str]
    range_pair: tuple[int, int]


def test_data_only_fallback_uses_pydantic_json_serialization() -> None:
    payload = _execute_data_result(
        _StandardJsonAdapters(
            amount=Decimal("1234.5600"),
            order_id=UUID("12345678-1234-5678-1234-567812345678"),
            output_path=Path("daily.json"),
            state=_OrderState.OPEN,
            labels={"beta", "alpha"},
            range_pair=(1, 5),
        )
    )

    assert payload["status"] == "ok"
    assert set(payload["data"].pop("labels")) == {"alpha", "beta"}
    assert payload["data"] == {
        "amount": "1234.5600",
        "order_id": "12345678-1234-5678-1234-567812345678",
        "output_path": "daily.json",
        "state": "open",
        "range_pair": [1, 5],
    }


def test_unsupported_mcp_value_returns_explicit_error_not_false_cycle() -> None:
    class OpaqueAccountValue:
        pass

    payload = _execute_data_result(OpaqueAccountValue())

    assert payload["status"] == "error"
    assert payload["error_type"] == "TypeError"
    assert "Unable to serialize MCP result type" in payload["error"]
    assert "OpaqueAccountValue" in payload["error"]
    assert "Circular reference detected" not in payload["error"]


def test_actual_mcp_result_cycle_returns_sanitized_error() -> None:
    cyclic: list[Any] = []
    cyclic.append(cyclic)

    payload = _execute_data_result(cyclic)

    assert payload["status"] == "error"
    assert payload["error_type"] == "TypeError"
    assert "Unable to serialize MCP result type builtins.list" in payload["error"]
    assert "ValueError" in payload["error"]
    assert "Circular reference detected" not in payload["error"]


def test_data_only_serialization_failure_does_not_leak_exception_text() -> None:
    class BrokenAccountValue(BaseModel):
        account_number: str

        @field_serializer("account_number")
        def serialize_account_number(self, value: str) -> str:
            del value
            raise RuntimeError("api_key=should-never-appear")

    payload = _execute_data_result(BrokenAccountValue(account_number="RH-123"))

    assert payload["status"] == "error"
    assert payload["error_type"] == "TypeError"
    assert "Unable to serialize MCP result type" in payload["error"]
    assert "PydanticSerializationError" in payload["error"]
    assert "should-never-appear" not in payload["error"]


def test_build_mcp_tool_wrappers_disambiguates_colliding_local_names() -> None:
    state = {
        "list_calls": 0,
        "call_calls": 0,
        "call_records": [],
        "list_outcomes": [[
            mcp_types.Tool(name="price-quote", description="Hyphen", inputSchema={"type": "object"}),
            mcp_types.Tool(name="price quote", description="Space", inputSchema={"type": "object"}),
        ]],
        "call_outcomes": [],
    }

    tools = build_mcp_tool_wrappers("demo", _make_config(), client_factory=_make_factory(state))
    names = [tool.name for tool in tools]

    assert names[0] == "mcp_demo_price_quote"
    assert names[1].startswith("mcp_demo_price_quote_")
    assert len(set(names)) == 2


def test_build_mcp_tool_wrappers_retries_transient_discovery_failure() -> None:
    state = {
        "list_calls": 0,
        "call_calls": 0,
        "call_records": [],
        "list_outcomes": [
            [McpError(mcp_types.ErrorData(code=mcp_types.CONNECTION_CLOSED, message="Connection closed"))][0],
            [mcp_types.Tool(name="quote", description="Quote", inputSchema={"type": "object"})],
        ],
        "call_outcomes": [],
    }

    tools = build_mcp_tool_wrappers("demo", _make_config(), client_factory=_make_factory(state))

    assert [tool.name for tool in tools] == ["mcp_demo_quote"]
    assert state["list_calls"] == 2


def test_build_mcp_tool_wrappers_single_attempt_does_not_retry_discovery() -> None:
    """max_list_tools_attempts=1 (authorize bootstrap) must not retry.

    Regression for #259: a retry opens a fresh client context that starts a
    second OAuth callback server, orphaning the user's in-progress sign-in. The
    authorize path passes max_list_tools_attempts=1 so the first transient
    failure propagates immediately and exactly one client context is opened.
    """
    transient = McpError(
        mcp_types.ErrorData(code=mcp_types.CONNECTION_CLOSED, message="Connection closed")
    )
    state = {
        "list_calls": 0,
        "call_calls": 0,
        "call_records": [],
        "list_outcomes": [
            transient,
            [mcp_types.Tool(name="quote", description="Quote", inputSchema={"type": "object"})],
        ],
        "call_outcomes": [],
    }

    with pytest.raises(McpError):
        build_mcp_tool_wrappers(
            "demo",
            _make_config(),
            client_factory=_make_factory(state),
            max_list_tools_attempts=1,
        )

    assert state["list_calls"] == 1


def test_remote_tool_execute_returns_normalized_error_payload_without_retry() -> None:
    state = {
        "list_calls": 0,
        "call_calls": 0,
        "call_records": [],
        "list_outcomes": [[
            mcp_types.Tool(name="quote", description="Quote", inputSchema={"type": "object"})
        ]],
        "call_outcomes": [ToolError("validation failed")],
    }

    tool = build_mcp_tool_wrappers("demo", _make_config(), client_factory=_make_factory(state))[0]

    payload = json.loads(tool.execute(symbol="AAPL"))

    assert payload == {
        "status": "error",
        "server": "demo",
        "remote_tool": "quote",
        "tool": "mcp_demo_quote",
        "error": "validation failed",
        "error_type": "ToolError",
    }


def test_build_mcp_tool_wrappers_wildcard_enabled_tools_passes_all() -> None:
    """enabledTools: ["*"] must pass every tool through without filtering."""
    state = {
        "list_calls": 0,
        "call_calls": 0,
        "call_records": [],
        "list_outcomes": [[
            mcp_types.Tool(name="alpha", description="A", inputSchema={"type": "object"}),
            mcp_types.Tool(name="beta", description="B", inputSchema={"type": "object"}),
            mcp_types.Tool(name="gamma", description="C", inputSchema={"type": "object"}),
        ]],
        "call_outcomes": [],
    }

    # enabled_tools=["*"] is the default in _make_config()
    tools = build_mcp_tool_wrappers("demo", _make_config(enabled_tools=["*"]), client_factory=_make_factory(state))

    assert [t.name for t in tools] == [
        "mcp_demo_alpha",
        "mcp_demo_beta",
        "mcp_demo_gamma",
    ]


def test_normalize_mcp_tool_schema_strips_null_from_any_of_branches() -> None:
    """anyOf with a null-only branch should have that branch removed."""
    schema = normalize_mcp_tool_schema(
        {
            "type": "object",
            "properties": {
                "value": {
                    "anyOf": [
                        {"type": "integer"},
                        {"type": "null"},
                    ]
                }
            },
        }
    )

    # The null branch in the anyOf must be stripped.
    value_schema = schema["properties"]["value"]
    any_of_branches = value_schema["anyOf"]
    assert all(branch != {"type": "null"} for branch in any_of_branches)
    assert {"type": "integer"} in any_of_branches


def test_normalize_mcp_tool_schema_collapses_nested_type_list_with_null() -> None:
    """type: ["string", "null"] at any nesting level must collapse to type: "string"."""
    schema = normalize_mcp_tool_schema(
        {
            "type": "object",
            "properties": {
                "label": {"type": ["string", "null"]},
            },
        }
    )

    assert schema["properties"]["label"]["type"] == "string"


def test_build_client_uses_stdio_transport(monkeypatch) -> None:
    captured: dict[str, Any] = {}

    class _DummyClient:
        pass

    def _fake_stdio_transport(**kwargs: Any) -> object:
        captured["transport"] = "stdio"
        captured["transport_kwargs"] = kwargs
        return object()

    def _fake_client(transport: object, **kwargs: Any) -> _DummyClient:
        captured["client_transport"] = transport
        captured["client_kwargs"] = kwargs
        return _DummyClient()

    monkeypatch.setattr("src.tools.mcp.StdioTransport", _fake_stdio_transport)
    monkeypatch.setattr("src.tools.mcp.Client", _fake_client)

    adapter = MCPServerAdapter("demo", _make_config(command="uvx", args=["demo-server"]))
    adapter._build_client()

    assert captured["transport"] == "stdio"
    assert captured["transport_kwargs"]["command"] == "uvx"
    assert captured["transport_kwargs"]["args"] == ["demo-server"]


def test_build_client_uses_sse_transport(monkeypatch) -> None:
    captured: dict[str, Any] = {}

    class _DummyClient:
        pass

    def _fake_sse_transport(**kwargs: Any) -> object:
        captured["transport"] = "sse"
        captured["transport_kwargs"] = kwargs
        return object()

    def _fake_client(transport: object, **kwargs: Any) -> _DummyClient:
        captured["client_transport"] = transport
        captured["client_kwargs"] = kwargs
        return _DummyClient()

    monkeypatch.setattr("src.tools.mcp.SSETransport", _fake_sse_transport)
    monkeypatch.setattr("src.tools.mcp.Client", _fake_client)

    adapter = MCPServerAdapter(
        "demo",
        _make_config(type="sse", command="", args=[], url="http://localhost:8900/sse", headers={"X-Test": "1"}),
    )
    adapter._build_client()

    assert captured["transport"] == "sse"
    assert captured["transport_kwargs"]["url"] == "http://localhost:8900/sse"
    assert captured["transport_kwargs"]["headers"] == {"X-Test": "1"}


def test_build_client_uses_streamable_http_transport(monkeypatch) -> None:
    captured: dict[str, Any] = {}

    class _DummyClient:
        pass

    def _fake_http_transport(**kwargs: Any) -> object:
        captured["transport"] = "streamableHttp"
        captured["transport_kwargs"] = kwargs
        return object()

    def _fake_client(transport: object, **kwargs: Any) -> _DummyClient:
        captured["client_transport"] = transport
        captured["client_kwargs"] = kwargs
        return _DummyClient()

    monkeypatch.setattr("src.tools.mcp.StreamableHttpTransport", _fake_http_transport)
    monkeypatch.setattr("src.tools.mcp.Client", _fake_client)

    adapter = MCPServerAdapter(
        "demo",
        _make_config(type="streamableHttp", command="", args=[], url="http://localhost:8900/mcp"),
    )
    adapter._build_client()

    assert captured["transport"] == "streamableHttp"
    assert captured["transport_kwargs"]["url"] == "http://localhost:8900/mcp"


def test_build_client_rejects_url_only_config_without_explicit_type() -> None:
    config = _make_config(type="sse", command="", args=[], url="http://localhost:8900/sse")
    config.type = None

    adapter = MCPServerAdapter("demo", config)

    with pytest.raises(ValueError, match="explicit type"):
        adapter._build_client()


class TestHttpErrorBodyIsReported:
    """A remote HTTP failure must carry the server's own explanation.

    ``httpx.HTTPStatusError`` stringifies to status + URL only. IBKR answers a
    token it will not accept with a 400 and
    ``{"error":"Status failed 500","statusCode":400}``, while an unauthenticated
    request gets a 401 — indistinguishable without the body, which is what made
    issue #1126 read as a malformed-request bug rather than an auth rejection.
    """

    @staticmethod
    def _status_error(status: int, body: str) -> httpx.HTTPStatusError:
        request = httpx.Request("POST", "https://api.example.test/v1/api/mcp")
        response = httpx.Response(status, text=body, request=request)
        return httpx.HTTPStatusError(
            f"Client error '{status}' for url '{request.url}'",
            request=request,
            response=response,
        )

    def test_the_message_includes_the_response_body(self) -> None:
        exc = self._status_error(400, '{"error":"Status failed 500","statusCode":400}')
        message = mcp_module._format_exception_message(exc)
        assert "Status failed 500" in message

    def test_an_empty_body_adds_nothing(self) -> None:
        exc = self._status_error(400, "   ")
        assert mcp_module._format_exception_message(exc) == str(exc)

    def test_an_oversized_body_is_truncated(self) -> None:
        exc = self._status_error(500, "x" * 5000)
        message = mcp_module._format_exception_message(exc)
        assert message.endswith("...")
        assert len(message) < 700

    def test_a_non_http_exception_is_unchanged(self) -> None:
        assert mcp_module._format_exception_message(ValueError("plain")) == "plain"

    def test_discovery_reraises_the_same_type_with_the_body(self, monkeypatch) -> None:
        exc = self._status_error(400, '{"error":"Status failed 500","statusCode":400}')

        def _raise(_operation):
            raise exc

        monkeypatch.setattr(mcp_module, "_run_sync", _raise)
        adapter = MCPServerAdapter.__new__(MCPServerAdapter)

        with pytest.raises(httpx.HTTPStatusError) as caught:
            MCPServerAdapter.discover_tools(adapter)

        assert "Status failed 500" in str(caught.value)
        assert caught.value.response.status_code == 400


@pytest.mark.parametrize("bad", [None, [], "oops", 42, False])
def test_malformed_properties_normalization_is_idempotent(bad):
    original = {"type": "object", "properties": bad, "required": ["symbol"]}
    normalized = normalize_mcp_tool_schema(original)
    assert normalized["properties"] == {}
    assert normalized["required"] == ["symbol"]
    assert normalize_mcp_tool_schema(normalized) == normalized
    assert original["properties"] == bad


@pytest.mark.parametrize("composition", ["anyOf", "oneOf", "allOf"])
def test_malformed_properties_does_not_override_composed_schema(composition):
    branch = {"type": "object", "properties": {"symbol": {"type": "string"}}}
    result = normalize_mcp_tool_schema({"type": "object", "properties": "oops", composition: [branch, {"type": "object", "properties": {"code": {"type": "integer"}}}]})
    assert result[composition][0] == branch
    assert len(result[composition]) == 2
    assert "properties" not in result