MCP 服务器与组件的图标配置
MCP 协议中的 Icon 类型可为服务器及工具、资源、提示等组件提供图标,支持多尺寸、主题变体和 data URI 内嵌,帮助客户端在不同界面下清晰展示。
图标为您的 MCP 服务器和组件提供可视化表示,帮助客户端应用程序呈现更好的用户界面. 当显示在 MCP 客户端中时,图标帮助用户快速识别和导航您的服务器能力. ## 图标格式 图标使用来自MCP协议规格的标准的MCP图标类型,每个图标指定一个源的URL或数据URI,可选包括MIME类型,大小,以及主题信息. ```python from mcp.types import Icon icon = Icon( src="https://example.com/icon.png", mime_type="image/png", sizes=["48x48"] ) ``` 这些领域服务于不同的目的: - **斜弧**: URL 或数据 URI 指向图标图像 - **mime_type** (可选):图像的MIME类型(如"图像/png","图像/svg+xml"). - **大小** (可选):大小描述符的阵列(例如, ["48x48"], [任何一个]) - **主题** (可选): 图标设计的 UI 主题 `"light"` 或 时 间 `"dark"` ## 服务器图标 在您的服务器中添加图标和一个网站 URL , 以便在客户端应用程序中显示。 大小不同的多个图标帮助客户端选择显示上下文的最佳解析度 。 ```python from fastmcp import FastMCP from mcp.types import Icon mcp = FastMCP( name="WeatherService", website_url="https://weather.example.com", icons=[ Icon( src="https://weather.example.com/icon-48.png", mime_type="image/png", sizes=["48x48"] ), Icon( src="https://weather.example.com/icon-96.png", mime_type="image/png", sizes=["96x96"] ), ] ) ``` 服务器图标出现在MCP客户端界面中,以帮助用户识别自己可能已经安装的服务器之一. ## 组件图标 图标可以添加到单个工具、资源、资源模板和提示中。这可以帮助用户在视觉上区分不同的组件类型和目的。 ### 工具图标 ```python from mcp.types import Icon @mcp.tool( icons=[Icon(src="https://example.com/calculator-icon.png")] ) def calculate_sum(a: int, b: int) -> int: """Add two numbers together.""" return a + b ``` ### 资源图标 ```python @mcp.resource( "config://settings", icons=[Icon(src="https://example.com/config-icon.png")] ) def get_settings() -> dict: """Retrieve application settings.""" return {"theme": "dark", "language": "en"} ``` ### 资源模板图标 ```python @mcp.resource( "user://{user_id}/profile", icons=[Icon(src="https://example.com/user-icon.png")] ) def get_user_profile(user_id: str) -> dict: """Get a user's profile.""" return {"id": user_id, "name": f"User {user_id}"} ``` ### 提示图标 ```python @mcp.prompt( icons=[Icon(src="https://example.com/prompt-icon.png")] ) def analyze_code(code: str): """Create a prompt for code analysis.""" return f"Please analyze this code:\n\n{code}" ``` ## 主题变量 MCP客户端如VS Code和GitHub Desktop在光线或暗主题中使自己的界面成为了它们自己的界面,为其中一个设计的图标对另一个很难看到,比如暗的标志消失在暗的侧边栏中. `theme` 打开字段 `Icon` 告诉一个客户端UI主题一个图标是为其设计的,因此客户端可以显示保持可见的版本. 提供两个辅助图标 `theme` 值和客户端选择符合当前外观的值 : ```python from fastmcp import FastMCP from mcp.types import Icon mcp = FastMCP( name="WeatherService", icons=[ Icon(src="https://weather.example.com/icon-light.png", theme="light"), Icon(src="https://weather.example.com/icon-dark.png", theme="dark"), ], ) ``` 同一领域在工具、资源、资源模板和提示方面开展工作: ```python from mcp.types import Icon @mcp.tool( icons=[ Icon(src="https://example.com/calculator-light.png", theme="light"), Icon(src="https://example.com/calculator-dark.png", theme="dark"), ] ) def calculate_sum(a: int, b: int) -> int: """Add two numbers together.""" return a + b ``` 发声 `theme` 表示图标被假定适合任何主题,这是对一个具有足够对比度的单一图标的正确选择,可以清晰地对照光和暗背景阅读. ## 使用数据 URIs 对于小图标或当您想要直接嵌入图标而无需外部依赖时,请使用数据 URI。这种方法可以消除托管的需要,并确保图标总是可用的。 ```python from mcp.types import Icon # SVG icon as data URI svg_icon = Icon( src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCI+PHBhdGggZD0iTTEyIDJDNi40OCAyIDIgNi40OCAyIDEyczQuNDggMTAgMTAgMTAgMTAtNC40OCAxMC0xMFMxNy41MiAyIDEyIDJ6Ii8+PC9zdmc+", mime_type="image/svg+xml" ) @mcp.tool(icons=[svg_icon]) def my_tool() -> str: """A tool with an embedded SVG icon.""" return "result" ``` ### 从文件生成数据 URI FastMCP提供 `Image` 将本地图像文件转换为数据 URI 的工具类 。 ```python from mcp.types import Icon from fastmcp.utilities.types import Image # Generate a data URI from a local image file img = Image(path="./assets/brand/favicon.png") icon = Icon(src=img.to_data_uri()) @mcp.tool(icons=[icon]) def file_icon_tool() -> str: """A tool with an icon generated from a local file.""" return "result" ``` 当您有本地图像资产并想要直接嵌入到您的服务器定义中时, 这种方法是有用的 。
查看原始来源