modelcontextprotocol / modelcontextprotocol/python-sdk

Simplify `src/mcp/shared/metadata_utils.py::get_display_name` logic.

Đang mở Phù hợp với người mới
#3,480 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
Python
Star
24.3k
Fork
4k
Merge trung bình
1 ngày 1 giờ
Pull request đã merge (30 ngày)
31

Mô tả

Current Code Logic

As of now, src/mcp/shared/metadata_utils.py::get_display_name works like this:

def get_display_name(obj: Tool | Resource | Prompt | ResourceTemplate | Implementation) -> str:
    """Get the display name for an MCP object with proper precedence.

    This is a client-side utility function designed to help MCP clients display
    human-readable names in their user interfaces. When servers provide a 'title'
    field, it should be preferred over the programmatic 'name' field for display.

    For tools: title > annotations.title > name
    For other objects: title > name

    Example:
        ```python
        # In a client displaying available tools
        tools = await session.list_tools()
        for tool in tools.tools:
            display_name = get_display_name(tool)
            print(f"Available tool: {display_name}")
        ```

    Args:
        obj: An MCP object with name and optional title fields

    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:
            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

However, code logic here can simplify, since title-first precedence applies to every object type.

Only Tool-specific not None annotations.title fallback needs a special middle branch.

Proposed Logical Adjustments
# All objects have title-first precedence, regardless of being a Tool or not.
if hasattr(obj, "title") and obj.title is not None:
    return obj.title

# Special middle branch for Tool-specific not None `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

# All other objects: name as last fallback.
return obj.name

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Đọc src/mcp/shared/metadata_utils.py::get_display_name và so sánh thứ tự ưu tiên hiện tại của nó với điều chỉnh được đề xuất. Công việc hoàn tất khi title được kiểm tra đầu tiên cho mọi đối tượng được liệt kê, Tool annotations.title vẫn là fallback ở giữa và obj.name vẫn là fallback cuối cùng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
backend-api-design
Loại issue
Tái cấu trúc
Độ khó
1/5
Thời gian dự kiến
Dưới một giờ
Mức độ hoạt động
Sôi nổi
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
88/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.