fix(mcpserver): handle bare typing.Dict and typing sequence generics in func_metadata

Closed
CH
1mo ago
+43-1

Fixes #3771

Motivation and Context

In src/mcp/server/mcpserver/utilities/func_metadata.py, return type inspection behaved inconsistently for unsubscripted typing generic counterparts compared to standard built-in types:

  1. typing.Dict: get_origin(typing.Dict) is dict but get_args(typing.Dict) is (). It fell through the len(args) == 2 check into the else branch, mistakenly forcing wrap_output = True (wrapped in {"result": ...}) instead of being treated as a top-level JSON object like dict or dict[str, Any].
  2. typing.List / typing.Tuple: typing.List in (list, tuple) evaluates to False, bypassing the opt-out gate at line 449 and unintentionally producing a wrapped Pydantic output model.

How Has This Been Tested?

  • Added unit tests in tests/server/mcpserver/test_func_metadata.py:
    • func_bare_typing_dict verifying output_schema is a partial object dict and wrap_output is False.
    • test_bare_typing_sequence_opts_out_of_structured_output verifying meta.output_schema is None for list, typing.List, tuple, and typing.Tuple.
  • Ran the test suite with pytest: all 66 tests in test_func_metadata.py pass.

Breaking Changes

None. This aligns unsubscripted typing generics (typing.Dict, typing.List, typing.Tuple) with standard Python dict, list, tuple behavior.

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