mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-11-25 04:21:26 +01:00
Co-authored-by: Alperen Keleş <19610315+alpaylan@users.noreply.github.com> Co-authored-by: HootRock <261611789+HootRock@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QCgr8md9CNeZCywsn9adrG
62 lines
1.7 KiB
Python
62 lines
1.7 KiB
Python
import asyncio
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from fastmcp import Client, FastMCP
|
|
from fastmcp.client.logging import LogMessage
|
|
from fastmcp.client.transports import ClientTransport, StdioTransport
|
|
|
|
pytestmark = [pytest.mark.client_process, pytest.mark.timeout(30)]
|
|
|
|
SERVER_SCRIPT = """
|
|
import sys
|
|
from fastmcp import Context, FastMCP
|
|
from fastmcp.server import create_proxy
|
|
|
|
server = FastMCP("logging-backend")
|
|
|
|
@server.tool
|
|
async def chatty(ctx: Context) -> str:
|
|
for i in range(5):
|
|
await ctx.log(f"message {i}", level="info")
|
|
return "done"
|
|
|
|
if sys.argv[1] == "proxy":
|
|
server = create_proxy(server)
|
|
server.run(show_banner=False)
|
|
"""
|
|
|
|
EXPECTED = [{"msg": f"message {i}", "extra": None} for i in range(5)]
|
|
|
|
|
|
@pytest.fixture
|
|
def server_script(tmp_path: Path) -> Path:
|
|
script = tmp_path / "logging_server.py"
|
|
script.write_text(SERVER_SCRIPT, encoding="utf-8")
|
|
return script
|
|
|
|
|
|
async def collect_logs(target: ClientTransport | FastMCP) -> list:
|
|
received = []
|
|
complete = asyncio.Event()
|
|
|
|
async def on_log(message: LogMessage) -> None:
|
|
received.append(message.data)
|
|
if len(received) == len(EXPECTED):
|
|
complete.set()
|
|
|
|
async with Client(target, log_handler=on_log) as client:
|
|
assert (await client.call_tool("chatty")).data == "done"
|
|
try:
|
|
await asyncio.wait_for(complete.wait(), timeout=2)
|
|
except TimeoutError:
|
|
pass
|
|
return received
|
|
|
|
|
|
@pytest.mark.subprocess_heavy
|
|
async def test_proxy_served_over_stdio_forwards_every_log(server_script: Path):
|
|
transport = StdioTransport(sys.executable, [str(server_script), "proxy"])
|
|
assert await collect_logs(transport) == EXPECTED
|