more fixes

This commit is contained in:
David Soria Parra 2025-01-27 15:49:59 +00:00
commit 978cfe3f92
No known key found for this signature in database

View file

@ -518,6 +518,10 @@ class TestContextInjection:
@pytest.mark.anyio @pytest.mark.anyio
async def test_context_logging(self): async def test_context_logging(self):
from unittest.mock import patch
import mcp.server.session
"""Test that context logging methods work.""" """Test that context logging methods work."""
mcp = FastMCP() mcp = FastMCP()
@ -529,12 +533,26 @@ class TestContextInjection:
return f"Logged messages for {msg}" return f"Logged messages for {msg}"
mcp.add_tool(logging_tool) mcp.add_tool(logging_tool)
async with client_session(mcp._mcp_server) as client:
result = await client.call_tool("logging_tool", {"msg": "test"}) with patch("mcp.server.session.ServerSession.send_log_message") as mock_log:
assert len(result.content) == 1 async with client_session(mcp._mcp_server) as client:
content = result.content[0] result = await client.call_tool("logging_tool", {"msg": "test"})
assert isinstance(content, TextContent) assert len(result.content) == 1
assert "Logged messages for test" in content.text 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 @pytest.mark.anyio
async def test_optional_context(self): async def test_optional_context(self):
@ -563,8 +581,8 @@ class TestContextInjection:
@mcp.tool() @mcp.tool()
async def tool_with_resource(ctx: Context) -> str: async def tool_with_resource(ctx: Context) -> str:
data, mime_type = await ctx.read_resource("test://data") data = await ctx.read_resource("test://data")
return f"Read resource: {data} with mime type {mime_type}" return f"Read resource: {data}"
async with client_session(mcp._mcp_server) as client: async with client_session(mcp._mcp_server) as client:
result = await client.call_tool("tool_with_resource", {}) result = await client.call_tool("tool_with_resource", {})