|
| 1 | +"""Tests for the Streamable HTTP MCP client.""" |
| 2 | + |
| 3 | +import unittest |
| 4 | +from collections.abc import AsyncIterator |
| 5 | +from contextlib import AsyncExitStack, asynccontextmanager |
| 6 | +from typing import Any |
| 7 | +from unittest import mock |
| 8 | + |
| 9 | +from mistralai.extra.mcp.streamable_http import ( |
| 10 | + MCPClientStreamableHTTP, |
| 11 | + StreamableHTTPServerParams, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +class TestMCPClientStreamableHTTP(unittest.IsolatedAsyncioTestCase): |
| 16 | + async def test_headers_are_set_on_the_http_client(self) -> None: |
| 17 | + """Credentials must live on the httpx client (the transport's own headers |
| 18 | + argument is deprecated/ignored), so they are sent on every request.""" |
| 19 | + captured: dict[str, Any] = {} |
| 20 | + |
| 21 | + @asynccontextmanager |
| 22 | + async def fake_streamable_http_client(url: str, http_client: Any) -> AsyncIterator[Any]: |
| 23 | + captured["url"] = url |
| 24 | + captured["headers"] = dict(http_client.headers) |
| 25 | + yield object(), object(), lambda: None |
| 26 | + |
| 27 | + client = MCPClientStreamableHTTP( |
| 28 | + params=StreamableHTTPServerParams( |
| 29 | + url="http://mcp.example/mcp", |
| 30 | + headers={"Authorization": "Bearer gate", "Notion-Token": "ntn_x"}, |
| 31 | + ), |
| 32 | + name="test", |
| 33 | + ) |
| 34 | + |
| 35 | + with mock.patch( |
| 36 | + "mistralai.extra.mcp.streamable_http.streamable_http_client", |
| 37 | + fake_streamable_http_client, |
| 38 | + ): |
| 39 | + async with AsyncExitStack() as stack: |
| 40 | + read_stream, write_stream = await client._get_transport(stack) |
| 41 | + |
| 42 | + self.assertIsNotNone(read_stream) |
| 43 | + self.assertIsNotNone(write_stream) |
| 44 | + self.assertEqual(captured["url"], "http://mcp.example/mcp") |
| 45 | + # httpx normalizes header names to lower case |
| 46 | + self.assertEqual(captured["headers"]["authorization"], "Bearer gate") |
| 47 | + self.assertEqual(captured["headers"]["notion-token"], "ntn_x") |
| 48 | + |
| 49 | + async def test_follow_redirects_defaults_false_and_is_configurable(self) -> None: |
| 50 | + """Redirects are not followed by default (secret headers would be resent to |
| 51 | + the redirect target); the flag is forwarded to the httpx client when set.""" |
| 52 | + captured: dict[str, Any] = {} |
| 53 | + |
| 54 | + @asynccontextmanager |
| 55 | + async def fake_streamable_http_client(url: str, http_client: Any) -> AsyncIterator[Any]: |
| 56 | + captured["follow_redirects"] = http_client.follow_redirects |
| 57 | + yield object(), object(), lambda: None |
| 58 | + |
| 59 | + with mock.patch( |
| 60 | + "mistralai.extra.mcp.streamable_http.streamable_http_client", |
| 61 | + fake_streamable_http_client, |
| 62 | + ): |
| 63 | + default_client = MCPClientStreamableHTTP( |
| 64 | + params=StreamableHTTPServerParams(url="http://mcp.example/mcp"), |
| 65 | + name="test", |
| 66 | + ) |
| 67 | + async with AsyncExitStack() as stack: |
| 68 | + await default_client._get_transport(stack) |
| 69 | + self.assertFalse(captured["follow_redirects"]) |
| 70 | + |
| 71 | + opted_in_client = MCPClientStreamableHTTP( |
| 72 | + params=StreamableHTTPServerParams(url="http://mcp.example/mcp", follow_redirects=True), |
| 73 | + name="test", |
| 74 | + ) |
| 75 | + async with AsyncExitStack() as stack: |
| 76 | + await opted_in_client._get_transport(stack) |
| 77 | + self.assertTrue(captured["follow_redirects"]) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + unittest.main() |
0 commit comments