|
| 1 | +package apikey |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/mark3labs/mcp-go/mcp" |
| 9 | + "github.com/mark3labs/mcp-go/server" |
| 10 | + "github.com/pkg/errors" |
| 11 | + "github.com/shurcooL/graphql" |
| 12 | + |
| 13 | + "github.com/spacelift-io/spacectl/internal/cmd/authenticated" |
| 14 | +) |
| 15 | + |
| 16 | +func RegisterMCPTools(s *server.MCPServer) { |
| 17 | + registerListAPIKeysTool(s) |
| 18 | + registerGetAPIKeyTool(s) |
| 19 | +} |
| 20 | + |
| 21 | +type apiKeyNode struct { |
| 22 | + ID string `graphql:"id" json:"id"` |
| 23 | + Name string `graphql:"name" json:"name"` |
| 24 | + Description string `graphql:"description" json:"description"` |
| 25 | + Type string `graphql:"type" json:"type"` |
| 26 | + Admin bool `graphql:"admin" json:"admin"` |
| 27 | + Creator string `graphql:"creator" json:"creator"` |
| 28 | + CreatedAt int `graphql:"createdAt" json:"createdAt"` |
| 29 | + LastUsedAt *int `graphql:"lastUsedAt" json:"lastUsedAt"` |
| 30 | + Deleted bool `graphql:"deleted" json:"deleted"` |
| 31 | + Teams []string `graphql:"teams" json:"teams"` |
| 32 | + TeamCount int `graphql:"teamCount" json:"teamCount"` |
| 33 | + SpaceCount int `graphql:"spaceCount" json:"spaceCount"` |
| 34 | + IsMachineUser bool `graphql:"isMachineUser" json:"isMachineUser"` |
| 35 | + ExpiresAt *int `graphql:"expiresAt" json:"expiresAt"` |
| 36 | +} |
| 37 | + |
| 38 | +type apiKeyDetail struct { |
| 39 | + ID string `graphql:"id" json:"id"` |
| 40 | + Name string `graphql:"name" json:"name"` |
| 41 | + Description string `graphql:"description" json:"description"` |
| 42 | + Type string `graphql:"type" json:"type"` |
| 43 | + Admin bool `graphql:"admin" json:"admin"` |
| 44 | + Creator string `graphql:"creator" json:"creator"` |
| 45 | + CreatedAt int `graphql:"createdAt" json:"createdAt"` |
| 46 | + LastUsedAt *int `graphql:"lastUsedAt" json:"lastUsedAt"` |
| 47 | + Deleted bool `graphql:"deleted" json:"deleted"` |
| 48 | + Teams []string `graphql:"teams" json:"teams"` |
| 49 | + TeamCount int `graphql:"teamCount" json:"teamCount"` |
| 50 | + SpaceCount int `graphql:"spaceCount" json:"spaceCount"` |
| 51 | + IsMachineUser bool `graphql:"isMachineUser" json:"isMachineUser"` |
| 52 | + ExpiresAt *int `graphql:"expiresAt" json:"expiresAt"` |
| 53 | + AccessRules []apiKeyAccessRule `graphql:"accessRules" json:"accessRules"` |
| 54 | +} |
| 55 | + |
| 56 | +type apiKeyAccessRule struct { |
| 57 | + Space string `graphql:"space" json:"space"` |
| 58 | + SpaceAccessLevel string `graphql:"spaceAccessLevel" json:"spaceAccessLevel"` |
| 59 | +} |
| 60 | + |
| 61 | +func registerListAPIKeysTool(s *server.MCPServer) { |
| 62 | + tool := mcp.NewTool("list_api_keys", |
| 63 | + mcp.WithDescription(`Retrieve the list of all API keys for the Spacelift account. Returns key metadata including name, type, creator, and usage information. Does not return the secret values.`), |
| 64 | + mcp.WithToolAnnotation(mcp.ToolAnnotation{ |
| 65 | + Title: "List API Keys", |
| 66 | + ReadOnlyHint: mcp.ToBoolPtr(true), |
| 67 | + }), |
| 68 | + ) |
| 69 | + |
| 70 | + s.AddTool(tool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 71 | + authenticated.Ensure(ctx, nil) |
| 72 | + |
| 73 | + var query struct { |
| 74 | + APIKeys []apiKeyNode `graphql:"apiKeys"` |
| 75 | + } |
| 76 | + |
| 77 | + if err := authenticated.Client().Query(ctx, &query, map[string]any{}); err != nil { |
| 78 | + return nil, errors.Wrap(err, "failed to query API keys") |
| 79 | + } |
| 80 | + |
| 81 | + if len(query.APIKeys) == 0 { |
| 82 | + return mcp.NewToolResultText("No API keys found."), nil |
| 83 | + } |
| 84 | + |
| 85 | + keysJSON, err := json.Marshal(query.APIKeys) |
| 86 | + if err != nil { |
| 87 | + return nil, errors.Wrap(err, "failed to marshal API keys to JSON") |
| 88 | + } |
| 89 | + |
| 90 | + output := fmt.Sprintf("Found %d API keys:\n%s", len(query.APIKeys), string(keysJSON)) |
| 91 | + return mcp.NewToolResultText(output), nil |
| 92 | + }) |
| 93 | +} |
| 94 | + |
| 95 | +func registerGetAPIKeyTool(s *server.MCPServer) { |
| 96 | + tool := mcp.NewTool("get_api_key", |
| 97 | + mcp.WithDescription(`Retrieve detailed information about a specific Spacelift API key including its access rules and space permissions.`), |
| 98 | + mcp.WithToolAnnotation(mcp.ToolAnnotation{ |
| 99 | + Title: "Get API Key", |
| 100 | + ReadOnlyHint: mcp.ToBoolPtr(true), |
| 101 | + }), |
| 102 | + mcp.WithString("api_key_id", mcp.Description("The ID of the API key to retrieve"), mcp.Required()), |
| 103 | + ) |
| 104 | + |
| 105 | + s.AddTool(tool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 106 | + authenticated.Ensure(ctx, nil) |
| 107 | + apiKeyID, err := request.RequireString("api_key_id") |
| 108 | + if err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + |
| 112 | + var query struct { |
| 113 | + APIKey *apiKeyDetail `graphql:"apiKey(id: $id)"` |
| 114 | + } |
| 115 | + |
| 116 | + variables := map[string]any{ |
| 117 | + "id": graphql.ID(apiKeyID), |
| 118 | + } |
| 119 | + |
| 120 | + if err := authenticated.Client().Query(ctx, &query, variables); err != nil { |
| 121 | + return nil, errors.Wrapf(err, "failed to query for API key ID %q", apiKeyID) |
| 122 | + } |
| 123 | + |
| 124 | + if query.APIKey == nil { |
| 125 | + return mcp.NewToolResultText("API key not found"), nil |
| 126 | + } |
| 127 | + |
| 128 | + keyJSON, err := json.Marshal(query.APIKey) |
| 129 | + if err != nil { |
| 130 | + return nil, errors.Wrap(err, "failed to marshal API key to JSON") |
| 131 | + } |
| 132 | + |
| 133 | + return mcp.NewToolResultText(string(keyJSON)), nil |
| 134 | + }) |
| 135 | +} |
0 commit comments