list_models
Discover available AI models on Replicate for image generation and inference tasks. Filter results by owner to find specific models for your project needs.
Instructions
List available models on Replicate with optional filtering by owner.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| owner | No |
Implementation Reference
- The main asynchronous handler function that implements the core logic of the 'list_models' tool. It uses ReplicateClient to fetch models filtered by owner and converts the result to a typed ModelList response.async def list_models(owner: Optional[str] = None) -> ModelList: """List available models on Replicate. Args: owner: Optional owner username to filter models by Returns: ModelList containing the available models and pagination info Raises: RuntimeError: If the Replicate client fails to initialize Exception: If the API request fails """ async with ReplicateClient() as client: result = client.list_models(owner=owner) return ModelList( models=[Model(**model) for model in result["models"]], next_cursor=result.get("next_cursor"), total_count=result.get("total_models") )
- src/mcp_server_replicate/tools/model_tools.py:11-14 (registration)The FastMCP decorator that registers the 'list_models' function as a tool with the specified name and description.@mcp.tool( name="list_models", description="List available models on Replicate with optional filtering by owner.", )
- Pydantic schema defining the output structure of the list_models tool, consisting of a list of Model objects with pagination information.class ModelList(BaseModel): """Response format for listing models.""" models: List[Model] next_cursor: Optional[str] = None total_count: Optional[int] = None
- Supporting method in ReplicateClient that performs the actual API call to list models, formats the response, and handles pagination and owner filtering. Called directly by the tool handler.def list_models(self, owner: str | None = None, cursor: str | None = None) -> dict[str, Any]: """List available models on Replicate with pagination. Args: owner: Optional owner username to filter models cursor: Pagination cursor from previous response Returns: Dict containing models list, next cursor, and total count Raises: Exception: If the API request fails """ if not self.client: raise RuntimeError("Client not initialized. Check error property for details.") try: # Build params dict only including cursor if provided params = {} if cursor: params["cursor"] = cursor # Get models collection with pagination models = self.client.models.list(**params) # Get pagination info next_cursor = models.next_cursor if hasattr(models, "next_cursor") else None total_models = models.total if hasattr(models, "total") else None # Filter by owner if specified if owner: models = [m for m in models if m.owner == owner] # Format models with complete structure formatted_models = [] for model in models: model_data = { "id": f"{model.owner}/{model.name}", "owner": model.owner, "name": model.name, "description": model.description, "visibility": model.visibility, "github_url": getattr(model, "github_url", None), "paper_url": getattr(model, "paper_url", None), "license_url": getattr(model, "license_url", None), "run_count": getattr(model, "run_count", None), "cover_image_url": getattr(model, "cover_image_url", None), "default_example": getattr(model, "default_example", None), "featured": getattr(model, "featured", None), "tags": getattr(model, "tags", []), } # Add latest version info if available if model.latest_version: model_data["latest_version"] = { "id": model.latest_version.id, "created_at": model.latest_version.created_at, "cog_version": model.latest_version.cog_version, "openapi_schema": model.latest_version.openapi_schema, "model": f"{model.owner}/{model.name}", "replicate_version": getattr(model.latest_version, "replicate_version", None), "hardware": getattr(model.latest_version, "hardware", None), } formatted_models.append(model_data) return { "models": formatted_models, "next_cursor": next_cursor, "total_count": total_models, } except Exception as err: logger.error(f"Failed to list models: {str(err)}") raise Exception(f"Failed to list models: {str(err)}") from err