fix: alias-normalize CallToolResult structuredContent after validate

Closed
CO
2mo ago
+38-2

Fixes #3467

Summary

FuncMetadata.convert_result validates CallToolResult.structured_content but returns the result unchanged. Plain model returns validate then model_dump/dump_python with by_alias=True, so wire keys match outputSchema. The CallToolResult path skipped that dump, so Python field names that pass validation (populate_by_name / by_name) could be emitted while outputSchema advertises aliases.

Motivation and Context

Clients validating structuredContent against the published outputSchema reject otherwise-valid tool results when aliases are used. This aligns the CallToolResult short-circuit with the plain-return path (#1073 / #1099). Distinct from #3100 / #3118 (schema generation mode).

How Has This Been Tested?

  • Added test_call_tool_result_structured_content_alias_normalized
  • py -3.12 -m pytest tests/server/mcpserver/test_func_metadata.py — 59 passed
  • Standalone repro from #3467: CallToolResult path now emits {"first":...} instead of {"field_first":...}

Breaking Changes

None intended. Wire output for CallToolResult tools that previously leaked Python field names will now use aliases (matching schema / plain returns).

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I am assigned to the linked issue (or it is labeled help wanted, or I'm a maintainer)
  • I have disclosed any AI assistance and can explain the change in my own words
  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Smallest fix: after validate_python(..., by_alias=True, by_name=True) on CallToolResult.structured_content, dump with by_alias=True and model_copy the normalized dict — same serialization as the plain-return path.

AI disclosure

This PR was prepared with AI assistance (Cursor / Grok): issue draft, repro, patch, and PR text. Changes were verified locally on current main (08a3bc8) with the new regression test and the full test_func_metadata.py file.