FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: alias-normalize CallToolResult structuredContent after validate by coder058 · Pull Request #3468 · modelcontextprotocol/python-sdk · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 9 additions & 1 deletion src/mcp/server/mcpserver/utilities/func_metadata.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,15 @@ def convert_result(self, result: Any) -> CallToolResult | InputRequiredResult:
output_model = self.output_model if self.output_schema is not None else None
if isinstance(result, CallToolResult):
if output_model is not None and not result.is_error:
self._output_adapter(output_model).validate_python(result.structured_content)
# Match the plain-return path: accept Python or alias keys, then emit
# wire keys (aliases) so structuredContent agrees with outputSchema.
adapter = self._output_adapter(output_model)
validated = adapter.validate_python(result.structured_content, by_alias=True, by_name=True)
if isinstance(validated, BaseModel):
structured_content = validated.model_dump(mode="json", by_alias=True)
else:
structured_content = adapter.dump_python(validated, mode="json", by_alias=True)
return result.model_copy(update={"structured_content": structured_content})
return result

unstructured_content = _convert_to_content(result)
Expand Down
30 changes: 29 additions & 1 deletion tests/server/mcpserver/test_func_metadata.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pytest
from dirty_equals import IsPartialDict
from mcp_types import CallToolResult, ContentBlock, EmbeddedResource, InputRequiredResult, TextContent
from pydantic import BaseModel, Field, ValidationError
from pydantic import BaseModel, ConfigDict, Field, ValidationError
from typing_extensions import NotRequired, ReadOnly, Required

from mcp import MCPDeprecationWarning
Expand Down Expand Up @@ -1322,6 +1322,34 @@ def func_with_aliases() -> ModelWithAliases: # pragma: no cover
assert structured_content_defaults["second"] is None


def test_call_tool_result_structured_content_alias_normalized():
"""CallToolResult.structured_content must be alias-normalized like plain returns."""

class ModelWithAliases(BaseModel):
model_config = ConfigDict(populate_by_name=True)
field_first: str | None = Field(default=None, alias="first")
field_second: str | None = Field(default=None, alias="second")

def via_call_tool_result() -> Annotated[CallToolResult, ModelWithAliases]:
return CallToolResult(
content=[],
structured_content={"field_first": "hello", "field_second": "world"},
)

meta = func_metadata(via_call_tool_result)
assert meta.output_schema is not None
assert "first" in meta.output_schema["properties"]
assert "field_first" not in meta.output_schema["properties"]

converted = meta.convert_result(via_call_tool_result())
assert isinstance(converted, CallToolResult)
structured_content = converted.structured_content
assert structured_content is not None
assert structured_content == {"first": "hello", "second": "world"}
assert "field_first" not in structured_content
assert "field_second" not in structured_content


def test_basemodel_reserved_names():
"""Test that functions with parameters named after BaseModel methods work correctly"""

Expand Down
Loading

Back | FazBrowse Home | New Git URL