modelcontextprotocol / modelcontextprotocol/python-sdk

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

未关闭 适合新手
#3,480 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Python
星标
24.3k
派生
4k
平均合并
1 天 1 小时
30 天内合并 PR
31

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

阅读 src/mcp/shared/metadata_utils.py::get_display_name,并将其当前优先级与拟议的调整进行比较。当对每个列出的对象都首先检查 title、Tool annotations.title 仍作为中间 fallback、obj.name 仍作为最终 fallback 时,工作即完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend-api-design
Issue 类型
重构
难度
1/5
预计耗时
1 小时以内
活跃度
活跃
描述清晰度
描述清楚
新手友好度
88/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。