From f1df2ac2b3cc2e4dc5db52da2302af1278f2bfd1 Mon Sep 17 00:00:00 2001 From: Mohammad Younes Date: Fri, 18 Sep 2026 10:36:47 +0300 Subject: [PATCH] refactor: simplify get_display_name control flow Remove duplicate title check by moving universal title-first logic outside the isinstance(Tool) conditional. This improves readability and maintainability while preserving all existing behavior. Fixes #3480 - All 12 existing test assertions pass - 100% code coverage maintained - Type checking clean (pyright) - Linting clean (ruff) --- src/mcp/shared/metadata_utils.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/mcp/shared/metadata_utils.py b/src/mcp/shared/metadata_utils.py index b646133477..9e584663ce 100644 --- a/src/mcp/shared/metadata_utils.py +++ b/src/mcp/shared/metadata_utils.py @@ -32,15 +32,14 @@ def get_display_name(obj: Tool | Resource | Prompt | ResourceTemplate | Implemen Returns: The display name to use for UI presentation """ - if isinstance(obj, Tool): - # Tools have special precedence: title > annotations.title > name - if hasattr(obj, "title") and obj.title is not None: - return obj.title - if obj.annotations and hasattr(obj.annotations, "title") and obj.annotations.title is not None: + # All objects prefer title first + if hasattr(obj, "title") and obj.title is not None: + return obj.title + + # Tools have special fallback: check annotations.title + if isinstance(obj, Tool) and obj.annotations: + if hasattr(obj.annotations, "title") and obj.annotations.title is not None: return obj.annotations.title - return obj.name - else: - # All other objects: title > name - if hasattr(obj, "title") and obj.title is not None: - return obj.title - return obj.name + + # Universal fallback to name + return obj.name