Skip to content
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
18 changes: 9 additions & 9 deletions src/mcp/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ class GetPromptRequest(Request[GetPromptRequestParams, Literal["prompts/get"]]):
class TextContent(BaseModel):
"""Text content for a message."""

type: Literal["text"]
type: Literal["text"] = "text"
text: str
"""The text content of the message."""
annotations: Annotations | None = None
Expand All @@ -1030,7 +1030,7 @@ class TextContent(BaseModel):
class ImageContent(BaseModel):
"""Image content for a message."""

type: Literal["image"]
type: Literal["image"] = "image"
data: str
"""The base64-encoded image data."""
mimeType: str
Expand All @@ -1050,7 +1050,7 @@ class ImageContent(BaseModel):
class AudioContent(BaseModel):
"""Audio content for a message."""

type: Literal["audio"]
type: Literal["audio"] = "audio"
data: str
"""The base64-encoded audio data."""
mimeType: str
Expand All @@ -1076,7 +1076,7 @@ class ToolUseContent(BaseModel):
in the next user message.
"""

type: Literal["tool_use"]
type: Literal["tool_use"] = "tool_use"
"""Discriminator for tool use content."""

name: str
Expand Down Expand Up @@ -1104,7 +1104,7 @@ class ToolResultContent(BaseModel):
from the assistant. It contains the output of executing the requested tool.
"""

type: Literal["tool_result"]
type: Literal["tool_result"] = "tool_result"
"""Discriminator for tool result content."""

toolUseId: str
Expand Down Expand Up @@ -1171,7 +1171,7 @@ class EmbeddedResource(BaseModel):
of the LLM and/or the user.
"""

type: Literal["resource"]
type: Literal["resource"] = "resource"
resource: TextResourceContents | BlobResourceContents
annotations: Annotations | None = None
meta: dict[str, Any] | None = Field(alias="_meta", default=None)
Expand All @@ -1189,7 +1189,7 @@ class ResourceLink(Resource):
Note: resource links returned by tools are not guaranteed to appear in the results of `resources/list` requests.
"""

type: Literal["resource_link"]
type: Literal["resource_link"] = "resource_link"


ContentBlock = TextContent | ImageContent | AudioContent | ResourceLink | EmbeddedResource
Expand Down Expand Up @@ -1580,7 +1580,7 @@ def content_as_list(self) -> list[SamplingMessageContentBlock]:
class ResourceTemplateReference(BaseModel):
"""A reference to a resource or resource template definition."""

type: Literal["ref/resource"]
type: Literal["ref/resource"] = "ref/resource"
uri: str
"""The URI or URI template of the resource."""
model_config = ConfigDict(extra="allow")
Expand All @@ -1589,7 +1589,7 @@ class ResourceTemplateReference(BaseModel):
class PromptReference(BaseModel):
"""Identifies a prompt."""

type: Literal["ref/prompt"]
type: Literal["ref/prompt"] = "ref/prompt"
name: str
"""The name of the prompt or prompt template"""
model_config = ConfigDict(extra="allow")
Expand Down
13 changes: 13 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
CreateMessageRequestParams,
CreateMessageResult,
CreateMessageResultWithTools,
ImageContent,
Implementation,
InitializeRequest,
InitializeRequestParams,
JSONRPCMessage,
JSONRPCRequest,
ListToolsResult,
PromptReference,
SamplingCapability,
SamplingMessage,
TextContent,
Expand Down Expand Up @@ -71,6 +73,17 @@ async def test_method_initialization():
assert initialize_request.params.protocolVersion == LATEST_PROTOCOL_VERSION


def test_content_type_literal_defaults():
"""Content types should default their Literal type field.

This allows instantiation without explicitly passing the type discriminator,
e.g., TextContent(text="hello") instead of TextContent(type="text", text="hello").
"""
assert TextContent(text="hello").type == "text"
assert ImageContent(data="base64", mimeType="image/png").type == "image"
assert PromptReference(name="test").type == "ref/prompt"
Comment thread
felixweinberger marked this conversation as resolved.
Outdated


@pytest.mark.anyio
async def test_tool_use_content():
"""Test ToolUseContent type for SEP-1577."""
Expand Down