π The fast, Pythonic way to build MCP servers and clients.
Keep ResourceLink content on prompt messages (#5897)
Asem ahmed3f776fa6ce51
diff --git a/docs/servers/prompts.mdx b/docs/servers/prompts.mdxindex e0c384cfb..87b447cef 100644--- a/docs/servers/prompts.mdx+++ b/docs/servers/prompts.mdx@@ -288,13 +288,13 @@ Message(["item1", "item2"])`Message` accepts two fields:-**`content`** - The message content. Strings pass through directly. Other types (dict, list, BaseModel) are automatically JSON-serialized to text.+**`content`** - The message content. Strings pass through directly. MCP content blocks (`TextContent`, `ImageContent`, `AudioContent`, `ResourceLink`, and `EmbeddedResource`) pass through unchanged. Other types (dict, list, BaseModel) are automatically JSON-serialized to text.**`role`** - The message role, either `"user"` (default) or `"assistant"`.<Card title="Message"><ParamField body="content" type="Any" required>- The content data. Strings pass through directly. Other types (dict, list, BaseModel) are automatically JSON-serialized.+ The content data. Strings and MCP content blocks (`TextContent`, `ImageContent`, `AudioContent`, `ResourceLink`, `EmbeddedResource`) pass through unchanged. Other types (dict, list, BaseModel) are automatically JSON-serialized.</ParamField><ParamField body="role" type="Literal['user', 'assistant']" default="user">The message role.diff --git a/fastmcp_slim/fastmcp/prompts/base.py b/fastmcp_slim/fastmcp/prompts/base.pyindex ef38cd62c..39fd74525 100644--- a/fastmcp_slim/fastmcp/prompts/base.py+++ b/fastmcp_slim/fastmcp/prompts/base.py@@ -18,6 +18,7 @@ from mcp_types import (Icon,ImageContent,PromptMessage,+ ResourceLink,TextContent,)from mcp_types import Prompt as SDKPrompt@@ -38,8 +39,9 @@ logger = get_logger(__name__)class Message(pydantic.BaseModel):"""Wrapper for prompt message with auto-serialization.- Accepts any content - strings pass through, other types- (dict, list, BaseModel) are JSON-serialized to text.+ Accepts any content - strings and MCP content blocks (TextContent,+ ImageContent, AudioContent, ResourceLink, EmbeddedResource) pass through+ unchanged, other types (dict, list, BaseModel) are JSON-serialized to text.Example:```python@@ -58,7 +60,7 @@ class Message(pydantic.BaseModel):"""role: Literal["user", "assistant"]- content: TextContent | ImageContent | AudioContent | EmbeddedResource+ content: TextContent | ImageContent | AudioContent | ResourceLink | EmbeddedResourcedef __init__(self,@@ -69,17 +71,22 @@ class Message(pydantic.BaseModel):Args:content: The message content. str passes through directly.- TextContent, ImageContent, AudioContent, and- EmbeddedResource pass through.+ TextContent, ImageContent, AudioContent, ResourceLink,+ and EmbeddedResource pass through.Other types (dict, list, BaseModel) are JSON-serialized.role: The message role, either "user" or "assistant"."""# Handle already-wrapped content typesif isinstance(- content, (TextContent, ImageContent, AudioContent, EmbeddedResource)+ content,+ (TextContent, ImageContent, AudioContent, ResourceLink, EmbeddedResource),):normalized_content: (- TextContent | ImageContent | AudioContent | EmbeddedResource+ TextContent+ | ImageContent+ | AudioContent+ | ResourceLink+ | EmbeddedResource) = contentelif isinstance(content, str):normalized_content = TextContent(type="text", text=content)diff --git a/fastmcp_slim/fastmcp/server/middleware/caching.py b/fastmcp_slim/fastmcp/server/middleware/caching.pyindex cf7f844f2..ec2de6928 100644--- a/fastmcp_slim/fastmcp/server/middleware/caching.py+++ b/fastmcp_slim/fastmcp/server/middleware/caching.py@@ -169,6 +169,7 @@ class CacheableMessage(FastMCPBaseModel):mcp_types.TextContent| mcp_types.ImageContent| mcp_types.AudioContent+ | mcp_types.ResourceLink| mcp_types.EmbeddedResource)diff --git a/tests/prompts/test_prompt.py b/tests/prompts/test_prompt.pyindex 8cfcb2fe5..1c7c72889 100644--- a/tests/prompts/test_prompt.py+++ b/tests/prompts/test_prompt.py@@ -3,7 +3,12 @@ from typing import Annotated, Anyimport pytestfrom mcp import MCPError-from mcp_types import INVALID_PARAMS, EmbeddedResource, TextResourceContents+from mcp_types import (+ INVALID_PARAMS,+ EmbeddedResource,+ ResourceLink,+ TextResourceContents,+)from pydantic import Fieldfrom fastmcp import Client, FastMCP@@ -785,6 +790,33 @@ class TestMessage:assert isinstance(mcp_msg.content, ImageContent)assert mcp_msg.content.data == "base64data"+ def test_message_passthrough_resource_link(self):+ """Test Message passes through ResourceLink without JSON serialization."""+ link = ResourceLink(+ type="resource_link",+ uri="docs://guide",+ name="guide",+ mime_type="text/markdown",+ )+ msg = Message(link, role="user")+ assert isinstance(msg.content, ResourceLink)+ assert msg.content == link+ assert msg.to_mcp_prompt_message().content == link++ async def test_message_resource_link_reaches_client(self):+ """Test a ResourceLink message is delivered as a resource_link block."""+ mcp = FastMCP()+ link = ResourceLink(type="resource_link", uri="docs://guide", name="guide")++ @mcp.prompt+ def review_guide() -> list[Message]:+ return [Message("Please review this document:"), Message(link)]++ async with Client(mcp) as client:+ result = await client.get_prompt("review_guide")++ assert result.messages[1].content == link+class TestPromptResult:def test_promptresult_from_string(self):diff --git a/tests/server/middleware/test_caching.py b/tests/server/middleware/test_caching.pyindex 7ded3c81d..f3a9b8439 100644--- a/tests/server/middleware/test_caching.py+++ b/tests/server/middleware/test_caching.py@@ -725,6 +725,31 @@ class TestCacheableToolResult:assert cached_tool_result.is_error is True+class TestCachedPromptResult:+ async def test_resource_link_survives_cache(self):+ link = mcp_types.ResourceLink(+ type="resource_link", uri="docs://guide", name="guide"+ )+ mcp = FastMCP("PromptCachingTestServer")+ mcp.add_middleware(ResponseCachingMiddleware(cache_storage=MemoryStore()))++ call_count = 0++ @mcp.prompt+ def review_guide() -> list[Message]:+ nonlocal call_count+ call_count += 1+ return [Message(link)]++ async with Client(mcp) as client:+ first = await client.get_prompt("review_guide")+ second = await client.get_prompt("review_guide")++ assert call_count == 1+ assert first.messages[0].content == link+ assert second.messages[0].content == link++class TestErrorResultsAreNotCached:"""Regression tests for issue #4395: an error result was cached for the fullTTL, so a transient failure permanently shadowed the tool until it expired."""diff --git a/tests/server/providers/proxy/test_proxy_server.py b/tests/server/providers/proxy/test_proxy_server.pyindex a8a27c027..3fb96dd06 100644--- a/tests/server/providers/proxy/test_proxy_server.py+++ b/tests/server/providers/proxy/test_proxy_server.py@@ -20,6 +20,7 @@ from fastmcp.client.transports import FastMCPTransport, StreamableHttpTransportfrom fastmcp.client.transports.base import TransportOptionsfrom fastmcp.exceptions import ToolErrorfrom fastmcp.mcp_config import MCPConfig+from fastmcp.prompts import Messagefrom fastmcp.resources import ResourceContent, ResourceResultfrom fastmcp.server import create_proxyfrom fastmcp.server.middleware import Middleware@@ -984,6 +985,25 @@ class TestPrompts:assert proxy_result.messages[1].content.data == "iVBORw0KGgoAAAANSUhEUg=="assert proxy_result.messages[1].content.mime_type == "image/png"+ async def test_proxy_prompt_preserves_resource_link(self):+ """A resource_link block from the backend is relayed, not flattened to text."""+ link = mcp_types.ResourceLink(+ type="resource_link",+ uri="docs://guide",+ name="guide",+ mime_type="text/markdown",+ )+ backend = FastMCP("Backend")++ @backend.prompt+ def review_guide() -> list[Message]:+ return [Message("Please review this document:"), Message(link)]++ async with Client(create_proxy(backend)) as client:+ result = await client.get_prompt("review_guide")++ assert result.messages[1].content == link+async def test_proxy_handles_multiple_concurrent_tasks_correctly(proxy_server: FastMCPProxy,