Add meta to Client methods (#1923)

This commit is contained in:
Marcelo Trylesinski 2026-01-22 14:50:39 +01:00 • committed by GitHub
commit f0ab53e194
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 221 additions and 156 deletions

View file

@ -1,5 +1,4 @@
import pytest
from pydantic import AnyUrl
from mcp import Client
from mcp.server.fastmcp import FastMCP
@ -88,14 +87,14 @@ async def test_resource_template_client_interaction():
assert "resource://users/{user_id}/profile" in templates
# Read a resource with valid parameters
result = await session.read_resource(AnyUrl("resource://users/123/posts/456"))
result = await session.read_resource("resource://users/123/posts/456")
contents = result.contents[0]
assert isinstance(contents, TextResourceContents)
assert contents.text == "Post 456 by user 123"
assert contents.mime_type == "text/plain"
# Read another resource with valid parameters
result = await session.read_resource(AnyUrl("resource://users/789/profile"))
result = await session.read_resource("resource://users/789/profile")
contents = result.contents[0]
assert isinstance(contents, TextResourceContents)
assert contents.text == "Profile for user 789"
@ -103,7 +102,7 @@ async def test_resource_template_client_interaction():
# Verify invalid resource URIs raise appropriate errors
with pytest.raises(Exception): # Specific exception type may vary
await session.read_resource(AnyUrl("resource://users/123/posts")) # Missing post_id
await session.read_resource("resource://users/123/posts") # Missing post_id
with pytest.raises(Exception): # Specific exception type may vary
await session.read_resource(AnyUrl("resource://users/123/invalid")) # Invalid template
await session.read_resource("resource://users/123/invalid") # Invalid template

View file

@ -1,7 +1,6 @@
import base64
import pytest
from pydantic import AnyUrl
from mcp import Client, types
from mcp.server.fastmcp import FastMCP
@ -46,12 +45,12 @@ async def test_fastmcp_resource_mime_type():
assert bytes_resource.mime_type == "image/png", "Bytes resource mime type not respected"
# Also verify the content can be read correctly
string_result = await client.read_resource(AnyUrl("test://image"))
string_result = await client.read_resource("test://image")
assert len(string_result.contents) == 1
assert getattr(string_result.contents[0], "text") == base64_string, "Base64 string mismatch"
assert string_result.contents[0].mime_type == "image/png", "String content mime type not preserved"
bytes_result = await client.read_resource(AnyUrl("test://image_bytes"))
bytes_result = await client.read_resource("test://image_bytes")
assert len(bytes_result.contents) == 1
assert base64.b64decode(getattr(bytes_result.contents[0], "blob")) == image_bytes, "Bytes mismatch"
assert bytes_result.contents[0].mime_type == "image/png", "Bytes content mime type not preserved"
@ -104,12 +103,12 @@ async def test_lowlevel_resource_mime_type():
assert bytes_resource.mime_type == "image/png", "Bytes resource mime type not respected"
# Also verify the content can be read correctly
string_result = await client.read_resource(AnyUrl("test://image"))
string_result = await client.read_resource("test://image")
assert len(string_result.contents) == 1
assert getattr(string_result.contents[0], "text") == base64_string, "Base64 string mismatch"
assert string_result.contents[0].mime_type == "image/png", "String content mime type not preserved"
bytes_result = await client.read_resource(AnyUrl("test://image_bytes"))
bytes_result = await client.read_resource("test://image_bytes")
assert len(bytes_result.contents) == 1
assert base64.b64decode(getattr(bytes_result.contents[0], "blob")) == image_bytes, "Bytes mismatch"
assert bytes_result.contents[0].mime_type == "image/png", "Bytes content mime type not preserved"

View file

@ -5,7 +5,6 @@ with parameters like 'text/html;profile=mcp-app' which are valid per RFC 2045.
"""
import pytest
from pydantic import AnyUrl
from mcp import Client
from mcp.server.fastmcp import FastMCP
@ -63,6 +62,6 @@ async def test_mime_type_preserved_in_read_resource():
async with Client(mcp) as client:
# Read the resource
result = await client.read_resource(AnyUrl("ui://my-widget"))
result = await client.read_resource("ui://my-widget")
assert len(result.contents) == 1
assert result.contents[0].mime_type == "text/html;profile=mcp-app"

View file

@ -16,13 +16,10 @@ async def test_progress_token_zero_first_call():
mock_session.send_progress_notification = AsyncMock()
# Create request context with progress token 0
mock_meta = MagicMock()
mock_meta.progress_token = 0 # This is the key test case - token is 0
request_context = RequestContext(
request_id="test-request",
session=mock_session,
meta=mock_meta,
meta={"progress_token": 0},
lifespan_context=None,
)

View file

@ -1,6 +1,5 @@
import anyio
import pytest
from pydantic import AnyUrl
from mcp import Client
from mcp.server.fastmcp import FastMCP
@ -76,7 +75,7 @@ async def test_messages_are_executed_concurrently_tools_and_resources():
# Start the tool first (it will wait on event)
tg.start_soon(client_session.call_tool, "sleep")
# Then the resource (it will set the event)
tg.start_soon(client_session.read_resource, AnyUrl("slow://slow_resource"))
tg.start_soon(client_session.read_resource, "slow://slow_resource")
# Verify that both ran concurrently
assert call_order == [