|
1 | 1 | """Starter workspaces tools for SingleStore MCP server.""" |
2 | 2 |
|
3 | 3 | from datetime import datetime |
4 | | -from typing import Dict, Any |
| 4 | +from typing import Dict, Any, Optional |
5 | 5 | from pydantic import BaseModel, Field |
6 | 6 |
|
7 | 7 | from mcp.server.fastmcp import Context |
8 | 8 |
|
9 | 9 | from src.config import config |
10 | 10 | from src.api.common import build_request |
| 11 | +from src.api.tools.regions.utils import fetch_shared_tier_regions |
11 | 12 | from src.utils.uuid_validation import validate_workspace_id |
12 | 13 | from src.utils.elicitation import try_elicitation, ElicitationError |
13 | 14 | from src.logger import get_logger |
@@ -49,7 +50,11 @@ def list_virtual_workspaces() -> Dict[str, Any]: |
49 | 50 |
|
50 | 51 |
|
51 | 52 | async def create_starter_workspace( |
52 | | - ctx: Context, name: str, database_name: str, provider: str, region_name: str |
| 53 | + ctx: Context, |
| 54 | + name: str, |
| 55 | + database_name: str, |
| 56 | + provider: Optional[str] = None, |
| 57 | + region_name: Optional[str] = None, |
53 | 58 | ) -> Dict[str, Any]: |
54 | 59 | """ |
55 | 60 | Create a new starter workspace using the SingleStore SDK. |
@@ -100,6 +105,60 @@ async def create_starter_workspace( |
100 | 105 | ) |
101 | 106 |
|
102 | 107 | try: |
| 108 | + # If provider or region_name are not provided, fetch available regions and ask user to pick |
| 109 | + if provider is None or region_name is None: |
| 110 | + await ctx.info("Fetching available shared tier regions...") |
| 111 | + regions_data = fetch_shared_tier_regions() |
| 112 | + |
| 113 | + # Group regions by provider |
| 114 | + provider_regions = {} |
| 115 | + for region in regions_data: |
| 116 | + prov = region.get("provider") |
| 117 | + if prov not in provider_regions: |
| 118 | + provider_regions[prov] = [] |
| 119 | + provider_regions[prov].append(region.get("regionName")) |
| 120 | + |
| 121 | + # Create region selection schema |
| 122 | + class RegionSelection(BaseModel): |
| 123 | + provider: str = Field( |
| 124 | + description=f"Choose a cloud provider from: {', '.join(provider_regions.keys())}" |
| 125 | + ) |
| 126 | + region_name: str = Field( |
| 127 | + description="Choose a region name from the available regions for the selected provider" |
| 128 | + ) |
| 129 | + |
| 130 | + # Format region information for user |
| 131 | + region_info = "\n".join( |
| 132 | + [ |
| 133 | + f"**{prov}**: {', '.join([r for r in regions])}" |
| 134 | + for prov, regions in provider_regions.items() |
| 135 | + ] |
| 136 | + ) |
| 137 | + |
| 138 | + elicit_result, error = await try_elicitation( |
| 139 | + ctx=ctx, |
| 140 | + message=f"Please select a provider and region for your starter workspace:\n\n{region_info}", |
| 141 | + schema=RegionSelection, |
| 142 | + ) |
| 143 | + |
| 144 | + if error == ElicitationError.NOT_SUPPORTED: |
| 145 | + # Use first available region if elicitation not supported |
| 146 | + first_region = regions_data[0] |
| 147 | + provider = first_region.get("provider") |
| 148 | + region_name = first_region.get("regionName") |
| 149 | + await ctx.info(f"Using default region: {provider} - {region_name}") |
| 150 | + elif elicit_result.status == "success" and elicit_result.data: |
| 151 | + provider = elicit_result.data.provider |
| 152 | + region_name = elicit_result.data.region_name |
| 153 | + await ctx.info(f"Selected region: {provider} - {region_name}") |
| 154 | + else: |
| 155 | + return { |
| 156 | + "status": "cancelled", |
| 157 | + "message": "Workspace creation cancelled - no region selected", |
| 158 | + "workspace_name": name, |
| 159 | + "database_name": database_name, |
| 160 | + } |
| 161 | + |
103 | 162 | # Create the starter workspace using the API |
104 | 163 | payload = { |
105 | 164 | "name": name, |
@@ -127,7 +186,7 @@ async def create_starter_workspace( |
127 | 186 |
|
128 | 187 | except Exception as e: |
129 | 188 | error_msg = f"Failed to create starter workspace '{name}': {str(e)}" |
130 | | - ctx.error(error_msg) |
| 189 | + await ctx.error(error_msg) |
131 | 190 |
|
132 | 191 | return { |
133 | 192 | "status": "error", |
|
0 commit comments