Spaces:
Sleeping
Sleeping
Commit ·
75dc996
1
Parent(s): 4cb557e
feat: add local modern MCP bridge path
Browse files- requirements.txt +6 -1
- server.py +47 -27
- tests/test_modern_mcp.py +214 -0
- tests/test_transport_composition.py +96 -0
requirements.txt
CHANGED
|
@@ -1,3 +1,8 @@
|
|
| 1 |
-
mcp[cli]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
httpx>=0.28.1
|
| 3 |
uvicorn[standard]
|
|
|
|
| 1 |
+
mcp[cli]==2.0.0
|
| 2 |
+
mcp-types==2.0.0
|
| 3 |
+
httpx2==2.10.0
|
| 4 |
+
httpcore2==2.10.0
|
| 5 |
+
idna==3.18
|
| 6 |
+
truststore==0.10.4
|
| 7 |
httpx>=0.28.1
|
| 8 |
uvicorn[standard]
|
server.py
CHANGED
|
@@ -17,7 +17,11 @@ import os
|
|
| 17 |
import json
|
| 18 |
import httpx
|
| 19 |
from typing import Optional
|
| 20 |
-
from mcp.server.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# ── Configuration ──────────────────────────────────────────────
|
| 23 |
NODE_URL = os.getenv(
|
|
@@ -25,11 +29,7 @@ NODE_URL = os.getenv(
|
|
| 25 |
"https://kode-animator-thought-engine-node.hf.space"
|
| 26 |
)
|
| 27 |
|
| 28 |
-
mcp =
|
| 29 |
-
"thought-engine-bridge",
|
| 30 |
-
host="0.0.0.0",
|
| 31 |
-
port=7860,
|
| 32 |
-
)
|
| 33 |
|
| 34 |
|
| 35 |
# ── HTTP Helper ────────────────────────────────────────────────
|
|
@@ -365,37 +365,57 @@ async def thought_node_identity() -> str:
|
|
| 365 |
|
| 366 |
# ── Server Entry Point ─────────────────────────────────────────
|
| 367 |
|
| 368 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
|
| 370 |
if __name__ == "__main__":
|
| 371 |
import uvicorn
|
| 372 |
-
from starlette.requests import Request as StarletteRequest
|
| 373 |
-
from starlette.responses import JSONResponse
|
| 374 |
-
|
| 375 |
-
# Add root health endpoint — FastMCP only mounts /sse + /messages by default
|
| 376 |
-
async def health(request: StarletteRequest):
|
| 377 |
-
return JSONResponse({
|
| 378 |
-
"service": "Thought Engine MCP Bridge",
|
| 379 |
-
"status": "ONLINE",
|
| 380 |
-
"mcp_version": "1.0",
|
| 381 |
-
"bridge_target_node": NODE_URL,
|
| 382 |
-
"sse_endpoint": "/sse"
|
| 383 |
-
})
|
| 384 |
-
|
| 385 |
-
app.add_route("/", health)
|
| 386 |
-
app.add_route("/health", health)
|
| 387 |
|
| 388 |
os.environ["PYTHONIOENCODING"] = "utf-8"
|
| 389 |
os.environ["PYTHONUNBUFFERED"] = "1"
|
| 390 |
-
print("🌉 Thought Engine Bridge —
|
| 391 |
print(f" Node URL: {NODE_URL}")
|
| 392 |
|
| 393 |
-
# Standard uvicorn run with proxy headers enabled for Hugging Face Spaces
|
| 394 |
uvicorn.run(
|
| 395 |
app,
|
| 396 |
host="0.0.0.0",
|
| 397 |
port=7860,
|
| 398 |
-
proxy_headers=True,
|
| 399 |
-
forwarded_allow_ips="*"
|
| 400 |
)
|
| 401 |
-
|
|
|
|
| 17 |
import json
|
| 18 |
import httpx
|
| 19 |
from typing import Optional
|
| 20 |
+
from mcp.server.mcpserver import MCPServer
|
| 21 |
+
from starlette.applications import Starlette
|
| 22 |
+
from starlette.requests import Request as StarletteRequest
|
| 23 |
+
from starlette.responses import JSONResponse
|
| 24 |
+
from starlette.routing import Route
|
| 25 |
|
| 26 |
# ── Configuration ──────────────────────────────────────────────
|
| 27 |
NODE_URL = os.getenv(
|
|
|
|
| 29 |
"https://kode-animator-thought-engine-node.hf.space"
|
| 30 |
)
|
| 31 |
|
| 32 |
+
mcp = MCPServer("thought-engine-bridge")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
# ── HTTP Helper ────────────────────────────────────────────────
|
|
|
|
| 365 |
|
| 366 |
# ── Server Entry Point ─────────────────────────────────────────
|
| 367 |
|
| 368 |
+
async def health(request: StarletteRequest):
|
| 369 |
+
"""Bridge-local readiness. Does not call the Thought Engine Node."""
|
| 370 |
+
return JSONResponse({
|
| 371 |
+
"service": "Thought Engine MCP Bridge",
|
| 372 |
+
"status": "ONLINE",
|
| 373 |
+
"mcp_version": "2026-07-28",
|
| 374 |
+
"bridge_target_node": NODE_URL,
|
| 375 |
+
"streamable_http_endpoint": "/mcp",
|
| 376 |
+
"sse_endpoint": "/sse",
|
| 377 |
+
})
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
def build_app() -> Starlette:
|
| 381 |
+
"""Build the dual-transport ASGI application from official MCP SDK apps."""
|
| 382 |
+
modern = mcp.streamable_http_app(
|
| 383 |
+
streamable_http_path="/mcp",
|
| 384 |
+
json_response=True,
|
| 385 |
+
stateless_http=True,
|
| 386 |
+
host="0.0.0.0",
|
| 387 |
+
)
|
| 388 |
+
legacy = mcp.sse_app(
|
| 389 |
+
sse_path="/sse",
|
| 390 |
+
message_path="/messages/",
|
| 391 |
+
host="0.0.0.0",
|
| 392 |
+
)
|
| 393 |
+
return Starlette(
|
| 394 |
+
routes=[
|
| 395 |
+
Route("/", health, methods=["GET"]),
|
| 396 |
+
Route("/health", health, methods=["GET"]),
|
| 397 |
+
*modern.routes,
|
| 398 |
+
*legacy.routes,
|
| 399 |
+
],
|
| 400 |
+
lifespan=modern.router.lifespan_context,
|
| 401 |
+
)
|
| 402 |
+
|
| 403 |
+
|
| 404 |
+
app = build_app()
|
| 405 |
+
|
| 406 |
|
| 407 |
if __name__ == "__main__":
|
| 408 |
import uvicorn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
|
| 410 |
os.environ["PYTHONIOENCODING"] = "utf-8"
|
| 411 |
os.environ["PYTHONUNBUFFERED"] = "1"
|
| 412 |
+
print("🌉 Thought Engine Bridge — modern /mcp + legacy /sse starting...")
|
| 413 |
print(f" Node URL: {NODE_URL}")
|
| 414 |
|
|
|
|
| 415 |
uvicorn.run(
|
| 416 |
app,
|
| 417 |
host="0.0.0.0",
|
| 418 |
port=7860,
|
| 419 |
+
proxy_headers=True,
|
| 420 |
+
forwarded_allow_ips="*",
|
| 421 |
)
|
|
|
tests/test_modern_mcp.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import asyncio
|
| 4 |
+
import importlib
|
| 5 |
+
import json
|
| 6 |
+
import socket
|
| 7 |
+
import sys
|
| 8 |
+
from contextlib import asynccontextmanager
|
| 9 |
+
|
| 10 |
+
import httpx2
|
| 11 |
+
import pytest
|
| 12 |
+
import uvicorn
|
| 13 |
+
from mcp import Client
|
| 14 |
+
from mcp.client.sse import sse_client
|
| 15 |
+
from mcp.client.streamable_http import streamable_http_client
|
| 16 |
+
from starlette.applications import Starlette
|
| 17 |
+
from starlette.requests import Request
|
| 18 |
+
from starlette.responses import JSONResponse
|
| 19 |
+
from starlette.routing import Route
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
EXPECTED_IDENTITY = {
|
| 23 |
+
"node": "Thought Engine Node",
|
| 24 |
+
"contract": "C-THOUGHT-NODE-001",
|
| 25 |
+
"status": "ACTIVE",
|
| 26 |
+
"session_count": 7,
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def _free_port() -> int:
|
| 31 |
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
| 32 |
+
sock.bind(("127.0.0.1", 0))
|
| 33 |
+
return int(sock.getsockname()[1])
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
async def _wait_for_server(port: int) -> None:
|
| 37 |
+
for _ in range(100):
|
| 38 |
+
try:
|
| 39 |
+
reader, writer = await asyncio.open_connection("127.0.0.1", port)
|
| 40 |
+
except OSError:
|
| 41 |
+
await asyncio.sleep(0.02)
|
| 42 |
+
continue
|
| 43 |
+
writer.close()
|
| 44 |
+
await writer.wait_closed()
|
| 45 |
+
return
|
| 46 |
+
raise RuntimeError("local server did not start")
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@asynccontextmanager
|
| 50 |
+
async def _serve(app):
|
| 51 |
+
port = _free_port()
|
| 52 |
+
server = uvicorn.Server(uvicorn.Config(app, host="127.0.0.1", port=port, log_level="warning"))
|
| 53 |
+
task = asyncio.create_task(server.serve())
|
| 54 |
+
await _wait_for_server(port)
|
| 55 |
+
try:
|
| 56 |
+
yield port
|
| 57 |
+
finally:
|
| 58 |
+
server.should_exit = True
|
| 59 |
+
await task
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def _load_bridge():
|
| 63 |
+
sys.modules.pop("server", None)
|
| 64 |
+
return importlib.import_module("server")
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
async def _modern_client(port: int, *, requests: list[str] | None = None):
|
| 68 |
+
async def record(request: httpx2.Request) -> None:
|
| 69 |
+
if requests is None or request.method != "POST":
|
| 70 |
+
return
|
| 71 |
+
try:
|
| 72 |
+
body = json.loads(request.content)
|
| 73 |
+
except Exception:
|
| 74 |
+
return
|
| 75 |
+
method = body.get("method")
|
| 76 |
+
if method:
|
| 77 |
+
requests.append(method)
|
| 78 |
+
|
| 79 |
+
http_client = httpx2.AsyncClient(event_hooks={"request": [record]})
|
| 80 |
+
transport = streamable_http_client(
|
| 81 |
+
f"http://127.0.0.1:{port}/mcp",
|
| 82 |
+
http_client=http_client,
|
| 83 |
+
)
|
| 84 |
+
return http_client, Client(transport, mode="auto", cache=None)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
@pytest.mark.asyncio
|
| 88 |
+
async def test_modern_discovery_proves_2026_07_28_without_initialize() -> None:
|
| 89 |
+
bridge = _load_bridge()
|
| 90 |
+
methods: list[str] = []
|
| 91 |
+
async with _serve(bridge.app) as port:
|
| 92 |
+
http_client, client = await _modern_client(port, requests=methods)
|
| 93 |
+
async with http_client, client:
|
| 94 |
+
await client.list_tools()
|
| 95 |
+
assert client.protocol_version == "2026-07-28"
|
| 96 |
+
assert client.session.discover_result is not None
|
| 97 |
+
assert methods[0] == "server/discover"
|
| 98 |
+
assert "initialize" not in methods
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
@pytest.mark.asyncio
|
| 102 |
+
async def test_modern_tools_list_exposes_thought_node_identity() -> None:
|
| 103 |
+
bridge = _load_bridge()
|
| 104 |
+
async with _serve(bridge.app) as port:
|
| 105 |
+
http_client, client = await _modern_client(port)
|
| 106 |
+
async with http_client, client:
|
| 107 |
+
tools = await client.list_tools()
|
| 108 |
+
assert any(tool.name == "thought_node_identity" for tool in tools.tools)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
@pytest.mark.asyncio
|
| 112 |
+
async def test_thought_node_identity_preserves_node_rest_mapping() -> None:
|
| 113 |
+
calls: list[tuple[str, str]] = []
|
| 114 |
+
|
| 115 |
+
async def root(request: Request) -> JSONResponse:
|
| 116 |
+
calls.append((request.method, request.url.path))
|
| 117 |
+
return JSONResponse(EXPECTED_IDENTITY)
|
| 118 |
+
|
| 119 |
+
async def node_health(request: Request) -> JSONResponse:
|
| 120 |
+
calls.append((request.method, request.url.path))
|
| 121 |
+
return JSONResponse({"status": "ok"})
|
| 122 |
+
|
| 123 |
+
node_app = Starlette(routes=[Route("/", root), Route("/health", node_health)])
|
| 124 |
+
async with _serve(node_app) as node_port:
|
| 125 |
+
bridge = _load_bridge()
|
| 126 |
+
bridge.NODE_URL = f"http://127.0.0.1:{node_port}"
|
| 127 |
+
async with _serve(bridge.app) as bridge_port:
|
| 128 |
+
for _ in range(2):
|
| 129 |
+
http_client, client = await _modern_client(bridge_port)
|
| 130 |
+
async with http_client, client:
|
| 131 |
+
result = await client.call_tool("thought_node_identity", {})
|
| 132 |
+
payload = json.loads(result.content[0].text)
|
| 133 |
+
assert client.protocol_version == "2026-07-28"
|
| 134 |
+
assert payload == EXPECTED_IDENTITY
|
| 135 |
+
assert calls == [("GET", "/"), ("GET", "/")]
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
@pytest.mark.asyncio
|
| 139 |
+
async def test_modern_disconnect_then_second_connection_succeeds() -> None:
|
| 140 |
+
bridge = _load_bridge()
|
| 141 |
+
async with _serve(bridge.app) as port:
|
| 142 |
+
for _ in range(2):
|
| 143 |
+
http_client, client = await _modern_client(port)
|
| 144 |
+
async with http_client, client:
|
| 145 |
+
tools = await client.list_tools()
|
| 146 |
+
assert client.protocol_version == "2026-07-28"
|
| 147 |
+
assert any(tool.name == "thought_node_identity" for tool in tools.tools)
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
@pytest.mark.asyncio
|
| 151 |
+
async def test_modern_path_does_not_require_legacy_initialize_session() -> None:
|
| 152 |
+
bridge = _load_bridge()
|
| 153 |
+
methods: list[str] = []
|
| 154 |
+
async with _serve(bridge.app) as port:
|
| 155 |
+
http_client, client = await _modern_client(port, requests=methods)
|
| 156 |
+
async with http_client, client:
|
| 157 |
+
result = await client.call_tool("thought_onboard", {"client_name": "S3"})
|
| 158 |
+
assert "Welcome to the Thought Engine" in result.content[0].text
|
| 159 |
+
assert client.protocol_version == "2026-07-28"
|
| 160 |
+
assert "server/discover" in methods
|
| 161 |
+
assert "initialize" not in methods
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
@pytest.mark.asyncio
|
| 165 |
+
async def test_legacy_sse_remains_available_after_modern_calls() -> None:
|
| 166 |
+
bridge = _load_bridge()
|
| 167 |
+
async with _serve(bridge.app) as port:
|
| 168 |
+
http_client, modern = await _modern_client(port)
|
| 169 |
+
async with http_client, modern:
|
| 170 |
+
await modern.list_tools()
|
| 171 |
+
|
| 172 |
+
async with Client(sse_client(f"http://127.0.0.1:{port}/sse"), mode="legacy", cache=None) as legacy:
|
| 173 |
+
tools = await legacy.list_tools()
|
| 174 |
+
assert any(tool.name == "thought_node_identity" for tool in tools.tools)
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
@pytest.mark.asyncio
|
| 178 |
+
async def test_modern_remains_available_after_legacy_disconnect() -> None:
|
| 179 |
+
bridge = _load_bridge()
|
| 180 |
+
async with _serve(bridge.app) as port:
|
| 181 |
+
async with Client(sse_client(f"http://127.0.0.1:{port}/sse"), mode="legacy", cache=None) as legacy:
|
| 182 |
+
await legacy.list_tools()
|
| 183 |
+
|
| 184 |
+
http_client, modern = await _modern_client(port)
|
| 185 |
+
async with http_client, modern:
|
| 186 |
+
tools = await modern.list_tools()
|
| 187 |
+
assert modern.protocol_version == "2026-07-28"
|
| 188 |
+
assert any(tool.name == "thought_node_identity" for tool in tools.tools)
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
@pytest.mark.asyncio
|
| 192 |
+
async def test_health_is_bridge_local_and_advertises_both_transports() -> None:
|
| 193 |
+
bridge = _load_bridge()
|
| 194 |
+
node_calls = 0
|
| 195 |
+
|
| 196 |
+
class FailingAsyncClient:
|
| 197 |
+
async def __aenter__(self):
|
| 198 |
+
nonlocal node_calls
|
| 199 |
+
node_calls += 1
|
| 200 |
+
raise AssertionError("health must not call the Node")
|
| 201 |
+
|
| 202 |
+
async def __aexit__(self, *args):
|
| 203 |
+
return None
|
| 204 |
+
|
| 205 |
+
bridge.httpx.AsyncClient = FailingAsyncClient
|
| 206 |
+
async with _serve(bridge.app) as port:
|
| 207 |
+
async with httpx2.AsyncClient() as client:
|
| 208 |
+
response = await client.get(f"http://127.0.0.1:{port}/health")
|
| 209 |
+
assert response.status_code == 200
|
| 210 |
+
payload = response.json()
|
| 211 |
+
assert payload["status"] == "ONLINE"
|
| 212 |
+
assert payload["streamable_http_endpoint"] == "/mcp"
|
| 213 |
+
assert payload["sse_endpoint"] == "/sse"
|
| 214 |
+
assert node_calls == 0
|
tests/test_transport_composition.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import asyncio
|
| 4 |
+
import socket
|
| 5 |
+
|
| 6 |
+
import pytest
|
| 7 |
+
import uvicorn
|
| 8 |
+
from mcp import Client
|
| 9 |
+
from mcp.client.sse import sse_client
|
| 10 |
+
from mcp.client.streamable_http import streamable_http_client
|
| 11 |
+
from mcp.server.mcpserver import MCPServer
|
| 12 |
+
from starlette.applications import Starlette
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def _free_port() -> int:
|
| 16 |
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
| 17 |
+
sock.bind(("127.0.0.1", 0))
|
| 18 |
+
return int(sock.getsockname()[1])
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def _dual_transport_app(server: MCPServer) -> Starlette:
|
| 22 |
+
modern = server.streamable_http_app(host="127.0.0.1")
|
| 23 |
+
legacy = server.sse_app(host="127.0.0.1")
|
| 24 |
+
return Starlette(
|
| 25 |
+
routes=[*modern.routes, *legacy.routes],
|
| 26 |
+
lifespan=modern.router.lifespan_context,
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
async def _wait_for_server(port: int) -> None:
|
| 31 |
+
for _ in range(100):
|
| 32 |
+
try:
|
| 33 |
+
reader, writer = await asyncio.open_connection("127.0.0.1", port)
|
| 34 |
+
except OSError:
|
| 35 |
+
await asyncio.sleep(0.02)
|
| 36 |
+
continue
|
| 37 |
+
writer.close()
|
| 38 |
+
await writer.wait_closed()
|
| 39 |
+
return
|
| 40 |
+
raise RuntimeError("local test server did not start")
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
async def _exercise_fresh_dual_transport_server() -> None:
|
| 44 |
+
server = MCPServer("dual-transport-proof")
|
| 45 |
+
|
| 46 |
+
@server.tool()
|
| 47 |
+
async def identity() -> str:
|
| 48 |
+
return "dual-ok"
|
| 49 |
+
|
| 50 |
+
app = _dual_transport_app(server)
|
| 51 |
+
port = _free_port()
|
| 52 |
+
uvicorn_server = uvicorn.Server(
|
| 53 |
+
uvicorn.Config(app, host="127.0.0.1", port=port, log_level="warning")
|
| 54 |
+
)
|
| 55 |
+
task = asyncio.create_task(uvicorn_server.serve())
|
| 56 |
+
await _wait_for_server(port)
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
async with Client(
|
| 60 |
+
streamable_http_client(f"http://127.0.0.1:{port}/mcp"),
|
| 61 |
+
mode="auto",
|
| 62 |
+
cache=None,
|
| 63 |
+
) as modern_client:
|
| 64 |
+
modern_tools = await modern_client.list_tools()
|
| 65 |
+
modern_result = await modern_client.call_tool("identity", {})
|
| 66 |
+
assert modern_client.protocol_version == "2026-07-28"
|
| 67 |
+
assert any(tool.name == "identity" for tool in modern_tools.tools)
|
| 68 |
+
assert modern_result.content[0].text == "dual-ok"
|
| 69 |
+
|
| 70 |
+
async with Client(
|
| 71 |
+
sse_client(f"http://127.0.0.1:{port}/sse"),
|
| 72 |
+
mode="legacy",
|
| 73 |
+
cache=None,
|
| 74 |
+
) as legacy_client:
|
| 75 |
+
legacy_tools = await legacy_client.list_tools()
|
| 76 |
+
legacy_result = await legacy_client.call_tool("identity", {})
|
| 77 |
+
assert any(tool.name == "identity" for tool in legacy_tools.tools)
|
| 78 |
+
assert legacy_result.content[0].text == "dual-ok"
|
| 79 |
+
|
| 80 |
+
async with Client(
|
| 81 |
+
streamable_http_client(f"http://127.0.0.1:{port}/mcp"),
|
| 82 |
+
mode="auto",
|
| 83 |
+
cache=None,
|
| 84 |
+
) as second_modern_client:
|
| 85 |
+
second_result = await second_modern_client.call_tool("identity", {})
|
| 86 |
+
assert second_modern_client.protocol_version == "2026-07-28"
|
| 87 |
+
assert second_result.content[0].text == "dual-ok"
|
| 88 |
+
finally:
|
| 89 |
+
uvicorn_server.should_exit = True
|
| 90 |
+
await task
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
@pytest.mark.asyncio
|
| 94 |
+
async def test_one_server_instance_supports_modern_and_legacy_lifecycles() -> None:
|
| 95 |
+
await _exercise_fresh_dual_transport_server()
|
| 96 |
+
await _exercise_fresh_dual_transport_server()
|