Skip to content

Commit 60f4b2d

Browse files
authored
Add comprehensive Unicode tests for streamable HTTP transport (#1381)
1 parent 9323efa commit 60f4b2d

File tree

1 file changed

+244
-0
lines changed

1 file changed

+244
-0
lines changed

tests/client/test_http_unicode.py

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
"""
2+
Tests for Unicode handling in streamable HTTP transport.
3+
4+
Verifies that Unicode text is correctly transmitted and received in both directions
5+
(server→client and client→server) using the streamable HTTP transport.
6+
"""
7+
8+
import multiprocessing
9+
import socket
10+
import time
11+
from collections.abc import Generator
12+
13+
import pytest
14+
15+
from mcp.client.session import ClientSession
16+
from mcp.client.streamable_http import streamablehttp_client
17+
18+
# Test constants with various Unicode characters
19+
UNICODE_TEST_STRINGS = {
20+
"cyrillic": "Слой хранилища, где располагаются",
21+
"cyrillic_short": "Привет мир",
22+
"chinese": "你好世界 - 这是一个测试",
23+
"japanese": "こんにちは世界 - これはテストです",
24+
"korean": "안녕하세요 세계 - 이것은 테스트입니다",
25+
"arabic": "مرحبا بالعالم - هذا اختبار",
26+
"hebrew": "שלום עולם - זה מבחן",
27+
"greek": "Γεια σου κόσμε - αυτό είναι δοκιμή",
28+
"emoji": "Hello 👋 World 🌍 - Testing 🧪 Unicode ✨",
29+
"math": "∑ ∫ √ ∞ ≠ ≤ ≥ ∈ ∉ ⊆ ⊇",
30+
"accented": "Café, naïve, résumé, piñata, Zürich",
31+
"mixed": "Hello世界🌍Привет안녕مرحباשלום",
32+
"special": "Line\nbreak\ttab\r\nCRLF",
33+
"quotes": '«French» „German" "English" 「Japanese」',
34+
"currency": "€100 £50 ¥1000 ₹500 ₽200 ¢99",
35+
}
36+
37+
38+
def run_unicode_server(port: int) -> None:
39+
"""Run the Unicode test server in a separate process."""
40+
# Import inside the function since this runs in a separate process
41+
from collections.abc import AsyncGenerator
42+
from contextlib import asynccontextmanager
43+
from typing import Any
44+
45+
import uvicorn
46+
from starlette.applications import Starlette
47+
from starlette.routing import Mount
48+
49+
import mcp.types as types
50+
from mcp.server import Server
51+
from mcp.server.streamable_http_manager import StreamableHTTPSessionManager
52+
from mcp.types import TextContent, Tool
53+
54+
# Need to recreate the server setup in this process
55+
server = Server(name="unicode_test_server")
56+
57+
@server.list_tools()
58+
async def list_tools() -> list[Tool]:
59+
"""List tools with Unicode descriptions."""
60+
return [
61+
Tool(
62+
name="echo_unicode",
63+
description="🔤 Echo Unicode text - Hello 👋 World 🌍 - Testing 🧪 Unicode ✨",
64+
inputSchema={
65+
"type": "object",
66+
"properties": {
67+
"text": {"type": "string", "description": "Text to echo back"},
68+
},
69+
"required": ["text"],
70+
},
71+
),
72+
]
73+
74+
@server.call_tool()
75+
async def call_tool(name: str, arguments: dict[str, Any] | None) -> list[TextContent]:
76+
"""Handle tool calls with Unicode content."""
77+
if name == "echo_unicode":
78+
text = arguments.get("text", "") if arguments else ""
79+
return [
80+
TextContent(
81+
type="text",
82+
text=f"Echo: {text}",
83+
)
84+
]
85+
else:
86+
raise ValueError(f"Unknown tool: {name}")
87+
88+
@server.list_prompts()
89+
async def list_prompts() -> list[types.Prompt]:
90+
"""List prompts with Unicode names and descriptions."""
91+
return [
92+
types.Prompt(
93+
name="unicode_prompt",
94+
description="Unicode prompt - Слой хранилища, где располагаются",
95+
arguments=[],
96+
)
97+
]
98+
99+
@server.get_prompt()
100+
async def get_prompt(name: str, arguments: dict[str, Any] | None) -> types.GetPromptResult:
101+
"""Get a prompt with Unicode content."""
102+
if name == "unicode_prompt":
103+
return types.GetPromptResult(
104+
messages=[
105+
types.PromptMessage(
106+
role="user",
107+
content=types.TextContent(
108+
type="text",
109+
text="Hello世界🌍Привет안녕مرحباשלום",
110+
),
111+
)
112+
]
113+
)
114+
raise ValueError(f"Unknown prompt: {name}")
115+
116+
# Create the session manager
117+
session_manager = StreamableHTTPSessionManager(
118+
app=server,
119+
json_response=False, # Use SSE for testing
120+
)
121+
122+
@asynccontextmanager
123+
async def lifespan(app: Starlette) -> AsyncGenerator[None, None]:
124+
async with session_manager.run():
125+
yield
126+
127+
# Create an ASGI application
128+
app = Starlette(
129+
debug=True,
130+
routes=[
131+
Mount("/mcp", app=session_manager.handle_request),
132+
],
133+
lifespan=lifespan,
134+
)
135+
136+
# Run the server
137+
config = uvicorn.Config(
138+
app=app,
139+
host="127.0.0.1",
140+
port=port,
141+
log_level="error",
142+
)
143+
uvicorn_server = uvicorn.Server(config)
144+
uvicorn_server.run()
145+
146+
147+
@pytest.fixture
148+
def unicode_server_port() -> int:
149+
"""Find an available port for the Unicode test server."""
150+
with socket.socket() as s:
151+
s.bind(("127.0.0.1", 0))
152+
return s.getsockname()[1]
153+
154+
155+
@pytest.fixture
156+
def running_unicode_server(unicode_server_port: int) -> Generator[str, None, None]:
157+
"""Start a Unicode test server in a separate process."""
158+
proc = multiprocessing.Process(target=run_unicode_server, kwargs={"port": unicode_server_port}, daemon=True)
159+
proc.start()
160+
161+
# Wait for server to be running
162+
max_attempts = 20
163+
attempt = 0
164+
while attempt < max_attempts:
165+
try:
166+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
167+
s.connect(("127.0.0.1", unicode_server_port))
168+
break
169+
except ConnectionRefusedError:
170+
time.sleep(0.1)
171+
attempt += 1
172+
else:
173+
raise RuntimeError(f"Server failed to start after {max_attempts} attempts")
174+
175+
try:
176+
yield f"http://127.0.0.1:{unicode_server_port}"
177+
finally:
178+
# Clean up - try graceful termination first
179+
proc.terminate()
180+
proc.join(timeout=2)
181+
if proc.is_alive():
182+
proc.kill()
183+
proc.join(timeout=1)
184+
185+
186+
@pytest.mark.anyio
187+
async def test_streamable_http_client_unicode_tool_call(running_unicode_server: str) -> None:
188+
"""Test that Unicode text is correctly handled in tool calls via streamable HTTP."""
189+
base_url = running_unicode_server
190+
endpoint_url = f"{base_url}/mcp"
191+
192+
async with streamablehttp_client(endpoint_url) as (read_stream, write_stream, _get_session_id):
193+
async with ClientSession(read_stream, write_stream) as session:
194+
await session.initialize()
195+
196+
# Test 1: List tools (server→client Unicode in descriptions)
197+
tools = await session.list_tools()
198+
assert len(tools.tools) == 1
199+
200+
# Check Unicode in tool descriptions
201+
echo_tool = tools.tools[0]
202+
assert echo_tool.name == "echo_unicode"
203+
assert echo_tool.description is not None
204+
assert "🔤" in echo_tool.description
205+
assert "👋" in echo_tool.description
206+
207+
# Test 2: Send Unicode text in tool call (client→server→client)
208+
for test_name, test_string in UNICODE_TEST_STRINGS.items():
209+
result = await session.call_tool("echo_unicode", arguments={"text": test_string})
210+
211+
# Verify server correctly received and echoed back Unicode
212+
assert len(result.content) == 1
213+
content = result.content[0]
214+
assert content.type == "text"
215+
assert f"Echo: {test_string}" == content.text, f"Failed for {test_name}"
216+
217+
218+
@pytest.mark.anyio
219+
async def test_streamable_http_client_unicode_prompts(running_unicode_server: str) -> None:
220+
"""Test that Unicode text is correctly handled in prompts via streamable HTTP."""
221+
base_url = running_unicode_server
222+
endpoint_url = f"{base_url}/mcp"
223+
224+
async with streamablehttp_client(endpoint_url) as (read_stream, write_stream, _get_session_id):
225+
async with ClientSession(read_stream, write_stream) as session:
226+
await session.initialize()
227+
228+
# Test 1: List prompts (server→client Unicode in descriptions)
229+
prompts = await session.list_prompts()
230+
assert len(prompts.prompts) == 1
231+
232+
prompt = prompts.prompts[0]
233+
assert prompt.name == "unicode_prompt"
234+
assert prompt.description is not None
235+
assert "Слой хранилища, где располагаются" in prompt.description
236+
237+
# Test 2: Get prompt with Unicode content (server→client)
238+
result = await session.get_prompt("unicode_prompt", arguments={})
239+
assert len(result.messages) == 1
240+
241+
message = result.messages[0]
242+
assert message.role == "user"
243+
assert message.content.type == "text"
244+
assert message.content.text == "Hello世界🌍Привет안녕مرحباשלום"

0 commit comments

Comments
 (0)