refactor: decompose func_metadata() into smaller focused helpers#2352
refactor: decompose func_metadata() into smaller focused helpers#2352perhapzz wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
Extract the monolithic func_metadata() function into smaller, well-documented helper functions for improved readability, testability, and extensibility. Changes: - Extract _get_function_signature() for signature introspection - Extract _build_arg_model() for parameter model construction - Extract _resolve_return_annotation() for return type analysis - Extract _create_output_model() for type-based model dispatch - Extract _try_generate_strict_schema() for schema generation with error handling - Simplify _try_create_model_and_schema() to delegate to new helpers All existing tests pass without modification. No behavioral changes.
ad4ec6f to
a2fc49d
Compare
|
Hey @perhapzz, thanks for picking this up! As the author of #1700, here are my thoughts: The decomposition looks solid. The five helpers ( Acceptance criteria coverage:
One small thing I noticed: Line 158 in the diff introduces what looks like a typo in a comment: The original Overall: +1 from the issue author. The approach and scope are right. Happy to see this land. |
Summary
Refactors the monolithic
func_metadata()function infunc_metadata.pyinto smaller, well-documented helper functions for improved readability, testability, and extensibility.Motivation
Closes #1700
func_metadata()handled multiple responsibilities in a single ~150-line function: signature introspection, parameter model construction, return type analysis, type-based model dispatch, and schema generation. This made it difficult to understand, test individual behaviors, and extend with new type handling.Changes
Extracted the following focused helper functions:
_get_function_signature()_build_arg_model()_resolve_return_annotation()_create_output_model()_try_generate_strict_schema()Simplified
_try_create_model_and_schema()to delegate to the new helpers.The top-level
func_metadata()is now a concise orchestrator (~20 lines) that calls these helpers in sequence.Testing
test_func_metadata.pypass without modificationfunc_metadata(),FuncMetadata,ArgModelBase) is unchangedNotes
src/mcp/server/fastmcp/tosrc/mcp/server/mcpserver/since the issue was filed — the refactor targets the current location._create_output_model(), as suggested in the issue. Kept out of this PR to minimize scope.