-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update test files to use ParsedMessage
Updates test files to work with the ParsedMessage stream type aliases and fixes a line length issue in test_201_client_hangs_on_logging.py. Github-Issue:#201
- Loading branch information
Showing
4 changed files
with
117 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import logging | ||
|
||
import anyio | ||
import pytest | ||
|
||
from mcp.server.fastmcp import Context, FastMCP | ||
from mcp.shared.memory import create_connected_server_and_client_session | ||
from mcp.types import TextContent | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@pytest.mark.anyio | ||
async def test_client_hangs_on_logging(): | ||
""" | ||
Test case for issue #201: Client hangs when ctx.info() logging with pure Python | ||
client. | ||
This test creates a FastMCP server with a tool that uses ctx.info() for logging. | ||
The issue is that when this tool is called, the client hangs and doesn't complete. | ||
""" | ||
# Create a FastMCP server with a tool that uses ctx.info() for logging | ||
server = FastMCP("test-server") | ||
|
||
@server.tool() | ||
async def simple_tool(x: float, y: float) -> str: | ||
"""A simple tool without logging""" | ||
return str(x * y) | ||
|
||
@server.tool() | ||
async def tool_with_logging(x: float, y: float, ctx: Context) -> str: | ||
"""A tool that uses ctx.info() for logging - this causes the client to hang""" | ||
await ctx.info(f"Processing tool with x={x} and y={y}") | ||
logger.debug("Inside tool_with_logging") | ||
await ctx.report_progress(1, 2) | ||
return str(x * y) | ||
|
||
# Create a client session connected to the server | ||
async with create_connected_server_and_client_session( | ||
server._mcp_server | ||
) as client_session: | ||
# First test that a simple tool works correctly | ||
result = await client_session.call_tool("simple_tool", {"x": 5, "y": 7}) | ||
assert isinstance(result.content[0], TextContent) | ||
assert result.content[0].text == "35.0" | ||
|
||
# Create an event to signal when the task is done | ||
done_event = anyio.Event() | ||
|
||
# Use a separate task for calling the tool that might hang | ||
async def call_logging_tool(): | ||
try: | ||
with anyio.fail_after(5): # Add a timeout to prevent test from hanging | ||
result = await client_session.call_tool( | ||
"tool_with_logging", {"x": 3, "y": 4} | ||
) | ||
assert isinstance(result.content[0], TextContent) | ||
assert result.content[0].text == "12.0" | ||
done_event.set() | ||
except Exception as e: | ||
logger.error(f"Error calling tool_with_logging: {e}") | ||
raise | ||
|
||
# Start the task | ||
async with anyio.create_task_group() as tg: | ||
tg.start_soon(call_logging_tool) | ||
|
||
# Wait for the task to complete or timeout | ||
with anyio.fail_after(10): | ||
await done_event.wait() | ||
|
||
# If we get here without hanging or timeout exceptions, the test passes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters