|
| 1 | +from typing import List, Literal |
| 2 | + |
| 3 | +import anyio |
| 4 | +import pytest |
| 5 | + |
| 6 | +from mcp.shared.memory import ( |
| 7 | + create_connected_server_and_client_session as create_session, |
| 8 | +) |
| 9 | +from mcp.types import ( |
| 10 | + LoggingMessageNotificationParams, |
| 11 | + TextContent, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +class LoggingCollector: |
| 16 | + def __init__(self): |
| 17 | + self.log_messages: List[LoggingMessageNotificationParams] = [] |
| 18 | + |
| 19 | + async def __call__(self, params: LoggingMessageNotificationParams) -> None: |
| 20 | + self.log_messages.append(params) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.anyio |
| 24 | +async def test_logging_callback(): |
| 25 | + from mcp.server.fastmcp import FastMCP |
| 26 | + |
| 27 | + server = FastMCP("test") |
| 28 | + logging_collector = LoggingCollector() |
| 29 | + |
| 30 | + # Create a simple test tool |
| 31 | + @server.tool("test_tool") |
| 32 | + async def test_tool() -> bool: |
| 33 | + # The actual tool is very simple and just returns True |
| 34 | + return True |
| 35 | + |
| 36 | + # Create a function that can send a log notification |
| 37 | + @server.tool("test_tool_with_log") |
| 38 | + async def test_tool_with_log( |
| 39 | + message: str, level: Literal["debug", "info", "warning", "error"], logger: str |
| 40 | + ) -> bool: |
| 41 | + """Send a log notification to the client.""" |
| 42 | + await server.get_context().log( |
| 43 | + level=level, |
| 44 | + message=message, |
| 45 | + logger_name=logger, |
| 46 | + ) |
| 47 | + return True |
| 48 | + |
| 49 | + async with anyio.create_task_group() as tg: |
| 50 | + async with create_session( |
| 51 | + server._mcp_server, logging_callback=logging_collector |
| 52 | + ) as client_session: |
| 53 | + |
| 54 | + async def listen_session(): |
| 55 | + try: |
| 56 | + async for message in client_session.incoming_messages: |
| 57 | + if isinstance(message, Exception): |
| 58 | + raise message |
| 59 | + except anyio.EndOfStream: |
| 60 | + pass |
| 61 | + |
| 62 | + tg.start_soon(listen_session) |
| 63 | + |
| 64 | + # First verify our test tool works |
| 65 | + result = await client_session.call_tool("test_tool", {}) |
| 66 | + assert result.isError is False |
| 67 | + assert isinstance(result.content[0], TextContent) |
| 68 | + assert result.content[0].text == "true" |
| 69 | + |
| 70 | + # Now send a log message via our tool |
| 71 | + log_result = await client_session.call_tool( |
| 72 | + "test_tool_with_log", |
| 73 | + { |
| 74 | + "message": "Test log message", |
| 75 | + "level": "info", |
| 76 | + "logger": "test_logger", |
| 77 | + }, |
| 78 | + ) |
| 79 | + assert log_result.isError is False |
| 80 | + assert len(logging_collector.log_messages) == 1 |
| 81 | + assert logging_collector.log_messages[ |
| 82 | + 0 |
| 83 | + ] == LoggingMessageNotificationParams( |
| 84 | + level="info", logger="test_logger", data="Test log message" |
| 85 | + ) |
0 commit comments