|
| 1 | +"""ModelScope MCP Server Studio tools. |
| 2 | +
|
| 3 | +Provides tools for studio-related operations in the ModelScope MCP Server, |
| 4 | +such as searching for studios and retrieving studio details. |
| 5 | +""" |
| 6 | + |
| 7 | +from typing import Annotated, Literal |
| 8 | + |
| 9 | +from fastmcp import FastMCP |
| 10 | +from fastmcp.utilities import logging |
| 11 | +from pydantic import Field |
| 12 | + |
| 13 | +from ..client import default_client |
| 14 | +from ..settings import settings |
| 15 | +from ..types import Studio |
| 16 | + |
| 17 | +logger = logging.get_logger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +def register_studio_tools(mcp: FastMCP) -> None: |
| 21 | + """Register all studio-related tools with the MCP server. |
| 22 | +
|
| 23 | + Args: |
| 24 | + mcp (FastMCP): The MCP server instance |
| 25 | +
|
| 26 | + """ |
| 27 | + |
| 28 | + @mcp.tool( |
| 29 | + annotations={ |
| 30 | + "title": "Search Studios (创空间 AI 应用)", |
| 31 | + } |
| 32 | + ) |
| 33 | + async def search_studios( |
| 34 | + query: Annotated[ |
| 35 | + str, |
| 36 | + Field( |
| 37 | + description="Keyword to search for related studios. " |
| 38 | + "Leave empty to get all studios based on other filters." |
| 39 | + ), |
| 40 | + ] = "", |
| 41 | + domains: Annotated[ |
| 42 | + list[Literal["multi-modal", "cv", "nlp", "audio", "AutoML"]] | None, |
| 43 | + Field(description="Domain categories to filter by"), |
| 44 | + ] = None, |
| 45 | + sort: Annotated[ |
| 46 | + Literal["Default", "gmt_modified", "VisitsCount", "StarsCount"], |
| 47 | + Field(description="Sort order"), |
| 48 | + ] = "Default", |
| 49 | + limit: Annotated[int, Field(description="Maximum number of studios to return", ge=1, le=30)] = 10, |
| 50 | + ) -> list[Studio]: |
| 51 | + """Search for studios on ModelScope.""" |
| 52 | + url = f"{settings.main_domain}/api/v1/dolphin/studios" |
| 53 | + |
| 54 | + # Build criterion for filters |
| 55 | + criterion = [] |
| 56 | + |
| 57 | + # Add create_type filter (always include all types) |
| 58 | + criterion.append( |
| 59 | + { |
| 60 | + "category": "create_type", |
| 61 | + "predicate": "contains", |
| 62 | + "values": ["interactive", "programmatic"], |
| 63 | + } |
| 64 | + ) |
| 65 | + |
| 66 | + # Add domains filter |
| 67 | + if domains: |
| 68 | + criterion.append( |
| 69 | + { |
| 70 | + "category": "domains", |
| 71 | + "predicate": "contains", |
| 72 | + "values": domains, |
| 73 | + } |
| 74 | + ) |
| 75 | + |
| 76 | + request_data = { |
| 77 | + "Name": query, |
| 78 | + "Criterion": criterion, |
| 79 | + "SortBy": sort, |
| 80 | + "PageNumber": 1, |
| 81 | + "PageSize": limit, |
| 82 | + } |
| 83 | + |
| 84 | + response = default_client.put(url, json_data=request_data) |
| 85 | + |
| 86 | + studios_data = response.get("Data", {}).get("Studios", []) |
| 87 | + |
| 88 | + studios = [] |
| 89 | + for studio_data in studios_data: |
| 90 | + path = studio_data.get("Path", "") |
| 91 | + name = studio_data.get("Name", "") |
| 92 | + modelscope_url = f"{settings.main_domain}/studios/{path}/{name}" |
| 93 | + |
| 94 | + if not path or not name: |
| 95 | + logger.warning(f"Skipping studio with invalid path or name: {studio_data}") |
| 96 | + continue |
| 97 | + |
| 98 | + studio = Studio( |
| 99 | + id=str(studio_data.get("Id", "")), |
| 100 | + path=path, |
| 101 | + name=name, |
| 102 | + chinese_name=studio_data.get("ChineseName", ""), |
| 103 | + description=studio_data.get("Description", ""), |
| 104 | + created_by=studio_data.get("CreatedBy", ""), |
| 105 | + license=studio_data.get("License", ""), |
| 106 | + modelscope_url=modelscope_url, |
| 107 | + independent_url=studio_data.get("IndependentUrl"), |
| 108 | + cover_image=studio_data.get("CoverImage"), |
| 109 | + type=studio_data.get("Type", ""), |
| 110 | + status=studio_data.get("Status", ""), |
| 111 | + domains=studio_data.get("Domain") or [], |
| 112 | + stars=studio_data.get("Stars", 0), |
| 113 | + visits=studio_data.get("Visits", 0), |
| 114 | + created_at=studio_data.get("CreatedTime", 0), |
| 115 | + updated_at=studio_data.get("LastUpdatedTime", 0), |
| 116 | + deployed_at=studio_data.get("DeployedTime", 0), |
| 117 | + ) |
| 118 | + studios.append(studio) |
| 119 | + |
| 120 | + return studios |
0 commit comments