Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dsp-ant committed Jan 27, 2025
1 parent 43a30c1 commit 978cfe3
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions tests/server/fastmcp/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@ async def async_tool(x: int, ctx: Context) -> str:

@pytest.mark.anyio
async def test_context_logging(self):
from unittest.mock import patch

import mcp.server.session

"""Test that context logging methods work."""
mcp = FastMCP()

Expand All @@ -529,12 +533,26 @@ async def logging_tool(msg: str, ctx: Context) -> str:
return f"Logged messages for {msg}"

mcp.add_tool(logging_tool)
async with client_session(mcp._mcp_server) as client:
result = await client.call_tool("logging_tool", {"msg": "test"})
assert len(result.content) == 1
content = result.content[0]
assert isinstance(content, TextContent)
assert "Logged messages for test" in content.text

with patch("mcp.server.session.ServerSession.send_log_message") as mock_log:
async with client_session(mcp._mcp_server) as client:
result = await client.call_tool("logging_tool", {"msg": "test"})
assert len(result.content) == 1
content = result.content[0]
assert isinstance(content, TextContent)
assert "Logged messages for test" in content.text

assert mock_log.call_count == 4
mock_log.assert_any_call(
level="debug", data="Debug message", logger=None
)
mock_log.assert_any_call(level="info", data="Info message", logger=None)
mock_log.assert_any_call(
level="warning", data="Warning message", logger=None
)
mock_log.assert_any_call(
level="error", data="Error message", logger=None
)

@pytest.mark.anyio
async def test_optional_context(self):
Expand Down Expand Up @@ -563,8 +581,8 @@ def test_resource() -> str:

@mcp.tool()
async def tool_with_resource(ctx: Context) -> str:
data, mime_type = await ctx.read_resource("test://data")
return f"Read resource: {data} with mime type {mime_type}"
data = await ctx.read_resource("test://data")
return f"Read resource: {data}"

async with client_session(mcp._mcp_server) as client:
result = await client.call_tool("tool_with_resource", {})
Expand Down

0 comments on commit 978cfe3

Please sign in to comment.