From 2af18bc2e59da7aa1ce9aa5681e3de1de33d2673 Mon Sep 17 00:00:00 2001 From: Felix Weinberger Date: Thu, 15 Jan 2026 15:43:05 +0100 Subject: [PATCH 1/3] fix: add default values to Literal type fields in content types Content types like TextContent and ImageContent have single-value Literal type fields (e.g., type: Literal["text"]) that now default automatically so users don't have to pass them explicitly. Before: TextContent(type="text", text="hello") After: TextContent(text="hello") Github-Issue: #1731 --- src/mcp/types.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/mcp/types.py b/src/mcp/types.py index 2671eb3f7..5f0ee6216 100644 --- a/src/mcp/types.py +++ b/src/mcp/types.py @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) @@ -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 @@ -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") @@ -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") From 9ccf80e2cdb0020f9405963e549fe67ebd03e182 Mon Sep 17 00:00:00 2001 From: Felix Weinberger Date: Fri, 16 Jan 2026 10:05:46 +0100 Subject: [PATCH 2/3] test: add regression test for content type literal defaults Github-Issue: #1731 --- tests/test_types.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_types.py b/tests/test_types.py index 1c16c3cc6..0d392818b 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -9,12 +9,14 @@ CreateMessageRequestParams, CreateMessageResult, CreateMessageResultWithTools, + ImageContent, Implementation, InitializeRequest, InitializeRequestParams, JSONRPCMessage, JSONRPCRequest, ListToolsResult, + PromptReference, SamplingCapability, SamplingMessage, TextContent, @@ -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" + + @pytest.mark.anyio async def test_tool_use_content(): """Test ToolUseContent type for SEP-1577.""" From bfdb899c1d107bc902620e6c1ce14f7dc7dc0260 Mon Sep 17 00:00:00 2001 From: Felix Weinberger Date: Fri, 16 Jan 2026 10:58:18 +0100 Subject: [PATCH 3/3] remove test_content_type_literal_defaults test --- tests/test_types.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/tests/test_types.py b/tests/test_types.py index 0d392818b..1c16c3cc6 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -9,14 +9,12 @@ CreateMessageRequestParams, CreateMessageResult, CreateMessageResultWithTools, - ImageContent, Implementation, InitializeRequest, InitializeRequestParams, JSONRPCMessage, JSONRPCRequest, ListToolsResult, - PromptReference, SamplingCapability, SamplingMessage, TextContent, @@ -73,17 +71,6 @@ 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" - - @pytest.mark.anyio async def test_tool_use_content(): """Test ToolUseContent type for SEP-1577."""