场景:本地MCP开发完后是否发现CLINE上显示的Parameters 显示No description
方法1 :使用参数元数据 (Annotated)
可以使用 Pydantic 的with 类提供有关参数的其他元数据Annotated。这种方法更受欢迎,因为它更现代,并且将类型提示与验证规则分开
参考代码:
from typing import Annotated
from pydantic import Field@mcp.tool
def deal_image(image_url: Annotated[str, Field(description="URL of the image to process")],resize: Annotated[bool, Field(description="Whether to resize the image")] = False,width: Annotated[int, Field(description="Target width in pixels", ge=1, le=2000)] = 800,format: Annotated[Literal["jpeg", "png", "webp"], Field(description="Output image format")] = "jpeg"
) -> dict:"""Process an image with optional resizing."""# Implementation...
方法2:使用字段(Field)
参考代码:
@mcp.tool
def getDataFromSql(query: str = Field(description="Search query string"),limit: int = Field(10, description="Maximum number of results", ge=1, le=100)
) -> list:"""Search the database with the provided query."""# Implementation...
具体实现代码
def register(mcp):@mcp.tool(name='text2Voice', description='文字转语音')def text2Voice(text: Annotated[str, Field(description="传入文本")] ) -> dict[str, Any] | None:"""语音转文字text:传入文本"""res=utils.Text2Voice(text=text,download=True)logging.info(f"text2Voice res: {res}")return res
效果