Skip to content

Commit ca47428

Browse files
committed
Add tool: list_keyspaces [org] database branch
1 parent 854ed8e commit ca47428

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

internal/cmd/mcp/server.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,65 @@ func HandleListBranches(ctx context.Context, request mcp.CallToolRequest, ch *cm
169169
return mcp.NewToolResultText(string(branchNamesJSON)), nil
170170
}
171171

172+
// HandleListKeyspaces implements the list_keyspaces tool
173+
func HandleListKeyspaces(ctx context.Context, request mcp.CallToolRequest, ch *cmdutil.Helper) (*mcp.CallToolResult, error) {
174+
// Get the PlanetScale client
175+
client, err := ch.Client()
176+
if err != nil {
177+
return nil, fmt.Errorf("failed to initialize PlanetScale client: %w", err)
178+
}
179+
180+
// Get the required database parameter
181+
dbArg, ok := request.Params.Arguments["database"]
182+
if !ok || dbArg == "" {
183+
return nil, fmt.Errorf("database parameter is required")
184+
}
185+
database := dbArg.(string)
186+
187+
// Get the required branch parameter
188+
branchArg, ok := request.Params.Arguments["branch"]
189+
if !ok || branchArg == "" {
190+
return nil, fmt.Errorf("branch parameter is required")
191+
}
192+
branch := branchArg.(string)
193+
194+
// Get the organization
195+
orgName, err := getOrganization(request, ch)
196+
if err != nil {
197+
return nil, err
198+
}
199+
200+
// Get the list of keyspaces
201+
keyspaces, err := client.Keyspaces.List(ctx, &planetscale.ListKeyspacesRequest{
202+
Organization: orgName,
203+
Database: database,
204+
Branch: branch,
205+
})
206+
if err != nil {
207+
switch cmdutil.ErrCode(err) {
208+
case planetscale.ErrNotFound:
209+
return nil, fmt.Errorf("database %s or branch %s does not exist in organization %s", database, branch, orgName)
210+
default:
211+
return nil, fmt.Errorf("failed to list keyspaces: %w", err)
212+
}
213+
}
214+
215+
// Extract the keyspace names
216+
keyspaceNames := make([]string, 0, len(keyspaces))
217+
for _, keyspace := range keyspaces {
218+
keyspaceNames = append(keyspaceNames, keyspace.Name)
219+
}
220+
221+
// Convert to JSON
222+
keyspaceNamesJSON, err := json.Marshal(keyspaceNames)
223+
if err != nil {
224+
return nil, fmt.Errorf("failed to marshal keyspace names: %w", err)
225+
}
226+
227+
// Return the JSON array as text
228+
return mcp.NewToolResultText(string(keyspaceNamesJSON)), nil
229+
}
230+
172231
// getToolDefinitions returns the list of all available MCP tools
173232
func getToolDefinitions() []ToolDef {
174233
return []ToolDef{
@@ -200,6 +259,23 @@ func getToolDefinitions() []ToolDef {
200259
),
201260
handler: HandleListBranches,
202261
},
262+
{
263+
tool: mcp.NewTool("list_keyspaces",
264+
mcp.WithDescription("List all keyspaces within a branch"),
265+
mcp.WithString("database",
266+
mcp.Description("The database name"),
267+
mcp.Required(),
268+
),
269+
mcp.WithString("branch",
270+
mcp.Description("The branch name"),
271+
mcp.Required(),
272+
),
273+
mcp.WithString("org",
274+
mcp.Description("The organization name (uses default organization if not specified)"),
275+
),
276+
),
277+
handler: HandleListKeyspaces,
278+
},
203279
}
204280
}
205281

0 commit comments

Comments
 (0)