|
1 | 1 | from typing import Literal
|
2 | 2 |
|
3 |
| -import anyio |
4 | 3 | import pytest
|
5 | 4 |
|
| 5 | +import mcp.types as types |
6 | 6 | from mcp.shared.memory import (
|
7 | 7 | create_connected_server_and_client_session as create_session,
|
8 | 8 | )
|
| 9 | +from mcp.shared.session import RequestResponder |
9 | 10 | from mcp.types import (
|
10 | 11 | LoggingMessageNotificationParams,
|
11 | 12 | TextContent,
|
@@ -46,40 +47,37 @@ async def test_tool_with_log(
|
46 | 47 | )
|
47 | 48 | return True
|
48 | 49 |
|
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: |
| 50 | + # Create a message handler to catch exceptions |
| 51 | + async def message_handler( |
| 52 | + message: RequestResponder[types.ServerRequest, types.ClientResult] |
| 53 | + | types.ServerNotification |
| 54 | + | Exception, |
| 55 | + ) -> None: |
| 56 | + if isinstance(message, Exception): |
| 57 | + raise message |
53 | 58 |
|
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 |
| 59 | + async with create_session( |
| 60 | + server._mcp_server, |
| 61 | + logging_callback=logging_collector, |
| 62 | + message_handler=message_handler, |
| 63 | + ) as client_session: |
| 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" |
61 | 69 |
|
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 |
| - ) |
| 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[0] == LoggingMessageNotificationParams( |
| 82 | + level="info", logger="test_logger", data="Test log message" |
| 83 | + ) |
0 commit comments