Add resource Link (#974)

This commit is contained in:
Inna Harper 2025-06-17 20:23:07 +01:00 • committed by GitHub
commit d0443a1832
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 66 additions and 22 deletions

View file

@ -7,16 +7,16 @@ from typing import Any, Literal
import pydantic_core
from pydantic import BaseModel, Field, TypeAdapter, validate_call
from mcp.types import Content, TextContent
from mcp.types import ContentBlock, TextContent
class Message(BaseModel):
"""Base class for all prompt messages."""
role: Literal["user", "assistant"]
content: Content
content: ContentBlock
def __init__(self, content: str | Content, **kwargs: Any):
def __init__(self, content: str | ContentBlock, **kwargs: Any):
if isinstance(content, str):
content = TextContent(type="text", text=content)
super().__init__(content=content, **kwargs)
@ -27,7 +27,7 @@ class UserMessage(Message):
role: Literal["user", "assistant"] = "user"
def __init__(self, content: str | Content, **kwargs: Any):
def __init__(self, content: str | ContentBlock, **kwargs: Any):
super().__init__(content=content, **kwargs)
@ -36,7 +36,7 @@ class AssistantMessage(Message):
role: Literal["user", "assistant"] = "assistant"
def __init__(self, content: str | Content, **kwargs: Any):
def __init__(self, content: str | ContentBlock, **kwargs: Any):
super().__init__(content=content, **kwargs)

View file

@ -54,7 +54,7 @@ from mcp.server.transport_security import TransportSecuritySettings
from mcp.shared.context import LifespanContextT, RequestContext, RequestT
from mcp.types import (
AnyFunction,
Content,
ContentBlock,
GetPromptResult,
TextContent,
ToolAnnotations,
@ -260,7 +260,7 @@ class FastMCP:
request_context = None
return Context(request_context=request_context, fastmcp=self)
async def call_tool(self, name: str, arguments: dict[str, Any]) -> Sequence[Content]:
async def call_tool(self, name: str, arguments: dict[str, Any]) -> Sequence[ContentBlock]:
"""Call a tool by name with arguments."""
context = self.get_context()
result = await self._tool_manager.call_tool(name, arguments, context=context)
@ -878,12 +878,12 @@ class FastMCP:
def _convert_to_content(
result: Any,
) -> Sequence[Content]:
) -> Sequence[ContentBlock]:
"""Convert a result to a sequence of content objects."""
if result is None:
return []
if isinstance(result, Content):
if isinstance(result, ContentBlock):
return [result]
if isinstance(result, Image):

View file

@ -384,7 +384,7 @@ class Server(Generic[LifespanResultT, RequestT]):
def decorator(
func: Callable[
...,
Awaitable[Iterable[types.Content]],
Awaitable[Iterable[types.ContentBlock]],
],
):
logger.debug("Registering handler for CallToolRequest")

View file

@ -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."""
class PromptMessage(BaseModel):
"""Describes a message returned as part of a prompt."""
role: Role
content: Content
content: ContentBlock
model_config = ConfigDict(extra="allow")
@ -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]
isError: bool = False