fastmcp/tests/client/tasks/test_prompt_task_mcp_message.py
Jeremiah Lowin 049bd22827
Replace type: ignore[attr-defined] with isinstance assertions in tests (#2665)
* Replace type: ignore[attr-defined] with isinstance assertions in tests

* Fix isinstance assertions in failing tests

- Fix enum test to check for ResponseEnum instead of str
- Fix binary resource test to check for BlobResourceContents instead of TextResourceContents
- Fix Root type tests to check attributes directly instead of isinstance checks

* Fix type errors without using type: ignore

- Remove execution methods from TransformingProvider (only handles transformations)
- Add execution methods to base Provider class with default implementations
- Fix type narrowing in tests using cast() instead of type: ignore
- Fix PromptResult type handling in prompt render tests
- Fix type narrowing in middleware test for arguments and structured_content
2025-12-21 16:37:20 -05:00

61 lines
2.2 KiB
Python

"""Test prompt task with mcp.types.PromptMessage return values.
Regression test for: PromptMessage object has no attribute 'to_mcp'
"""
import mcp.types
from mcp.types import TextContent
from fastmcp import FastMCP
from fastmcp.client import Client
async def test_prompt_task_with_mcp_prompt_message():
"""Prompt task returning mcp.types.PromptMessage should serialize correctly."""
mcp_server = FastMCP("test")
@mcp_server.prompt(task=True)
async def greeting(name: str) -> list[mcp.types.PromptMessage]:
return [
mcp.types.PromptMessage(
role="user",
content=mcp.types.TextContent(type="text", text=f"Hello {name}"),
)
]
async with Client(mcp_server) as client:
task = await client.get_prompt("greeting", {"name": "World"}, task=True)
result = await task.result()
assert isinstance(result.messages[0].content, TextContent)
assert "Hello World" in result.messages[0].content.text
async def test_prompt_task_with_multiple_mcp_prompt_messages():
"""Prompt task returning multiple mcp.types.PromptMessage objects."""
mcp_server = FastMCP("test")
@mcp_server.prompt(task=True)
async def conversation(topic: str) -> list[mcp.types.PromptMessage]:
return [
mcp.types.PromptMessage(
role="user",
content=mcp.types.TextContent(
type="text", text=f"Tell me about {topic}"
),
),
mcp.types.PromptMessage(
role="assistant",
content=mcp.types.TextContent(
type="text", text=f"{topic} is fascinating!"
),
),
]
async with Client(mcp_server) as client:
task = await client.get_prompt("conversation", {"topic": "space"}, task=True)
result = await task.result()
assert len(result.messages) == 2
assert isinstance(result.messages[0].content, TextContent)
assert "Tell me about space" in result.messages[0].content.text
assert isinstance(result.messages[1].content, TextContent)
assert "space is fascinating" in result.messages[1].content.text