場景:本地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
效果