refactor: decompose func_metadata() into smaller focused helpers#2352
Open
perhapzz wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
refactor: decompose func_metadata() into smaller focused helpers#2352perhapzz wants to merge 1 commit intomodelcontextprotocol:mainfrom
perhapzz 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
3 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.