Skip to content

Add resource Link #974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/mcp/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,14 +733,28 @@ class EmbeddedResource(BaseModel):
model_config = ConfigDict(extra="allow")


Content = TextContent | ImageContent | AudioContent | EmbeddedResource
class ResourceLink(Resource):
"""
A resource that the server is capable of reading, included in a prompt or tool call result.

Note: resource links returned by tools are not guaranteed to appear in the results of `resources/list` requests.
"""

type: Literal["resource_link"]


ContentBlock = TextContent | ImageContent | AudioContent | ResourceLink | EmbeddedResource
"""A content block that can be used in prompts and tool results."""

Content: TypeAlias = ContentBlock
# """DEPRECATED: Content is deprecated, you should use ContentBlock directly."""
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaving content just in case someone uses it from SDK and not to break them



class PromptMessage(BaseModel):
"""Describes a message returned as part of a prompt."""

role: Role
content: Content
content: ContentBlock
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per above comment

model_config = ConfigDict(extra="allow")


Expand Down Expand Up @@ -859,7 +873,7 @@ class CallToolRequest(Request[CallToolRequestParams, Literal["tools/call"]]):
class CallToolResult(Result):
"""The server's response to a tool call."""

content: list[Content]
content: list[ContentBlock]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per above comment

isError: bool = False


Expand Down
32 changes: 31 additions & 1 deletion tests/server/fastmcp/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
ProgressNotification,
PromptReference,
ReadResourceResult,
ResourceLink,
ResourceListChangedNotification,
ResourceTemplateReference,
SamplingMessage,
Expand Down Expand Up @@ -147,6 +148,25 @@ async def tool_with_progress(message: str, ctx: Context, steps: int = 3) -> str:
def echo(message: str) -> str:
return f"Echo: {message}"

# Tool that returns ResourceLinks
@mcp.tool(description="Lists files and returns resource links", title="List Files Tool")
def list_files() -> list[ResourceLink]:
"""Returns a list of resource links for files matching the pattern."""

# Mock some file resources for testing
file_resources = [
{
"type": "resource_link",
"uri": "file:///project/README.md",
"name": "README.md",
"mimeType": "text/markdown",
}
]

result: list[ResourceLink] = [ResourceLink.model_validate(file_json) for file_json in file_resources]

return result

# Tool with sampling capability
@mcp.tool(description="A tool that uses sampling to generate content", title="Sampling Tool")
async def sampling_tool(prompt: str, ctx: Context) -> str:
Expand Down Expand Up @@ -753,7 +773,17 @@ async def call_all_mcp_features(session: ClientSession, collector: NotificationC
assert isinstance(tool_result.content[0], TextContent)
assert tool_result.content[0].text == "Echo: hello"

# 2. Tool with context (logging and progress)
# 2. Test tool that returns ResourceLinks
list_files_result = await session.call_tool("list_files")
assert len(list_files_result.content) == 1

# Rest should be ResourceLinks
content = list_files_result.content[0]
assert isinstance(content, ResourceLink)
assert str(content.uri).startswith("file:///")
assert content.name is not None
assert content.mimeType is not None

# Test progress callback functionality
progress_updates = []

Expand Down
Loading