|
| 1 | +/* |
| 2 | +Copyright 2026. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package mcp |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/mark3labs/mcp-go/mcp" |
| 26 | + |
| 27 | + sreportalv1alpha1 "github.com/golgoth31/sreportal/api/v1alpha1" |
| 28 | + "github.com/golgoth31/sreportal/internal/adapter" |
| 29 | +) |
| 30 | + |
| 31 | +// FQDNDetails represents detailed information about a specific FQDN |
| 32 | +type FQDNDetails struct { |
| 33 | + Name string `json:"name"` |
| 34 | + Source string `json:"source"` |
| 35 | + Group string `json:"group"` |
| 36 | + Description string `json:"description,omitempty"` |
| 37 | + RecordType string `json:"record_type"` |
| 38 | + Targets []string `json:"targets"` |
| 39 | + Portal string `json:"portal,omitempty"` |
| 40 | + Namespace string `json:"namespace,omitempty"` |
| 41 | + LastSeen string `json:"last_seen,omitempty"` |
| 42 | + DNSResource string `json:"dns_resource,omitempty"` |
| 43 | +} |
| 44 | + |
| 45 | +// handleGetFQDNDetails handles the get_fqdn_details tool call |
| 46 | +func (s *Server) handleGetFQDNDetails(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 47 | + // Extract required parameter |
| 48 | + fqdn, err := request.RequireString("fqdn") |
| 49 | + if err != nil { |
| 50 | + return mcp.NewToolResultError("fqdn parameter is required"), nil |
| 51 | + } |
| 52 | + |
| 53 | + // Normalize the FQDN for comparison |
| 54 | + fqdnLower := strings.ToLower(strings.TrimSuffix(fqdn, ".")) |
| 55 | + |
| 56 | + // Search in all DNS resources |
| 57 | + var dnsList sreportalv1alpha1.DNSList |
| 58 | + if err := s.client.List(ctx, &dnsList); err != nil { |
| 59 | + return mcp.NewToolResultError(fmt.Sprintf("failed to list DNS resources: %v", err)), nil |
| 60 | + } |
| 61 | + |
| 62 | + // Look for the FQDN in DNS status |
| 63 | + for _, dns := range dnsList.Items { |
| 64 | + for _, grp := range dns.Status.Groups { |
| 65 | + for _, fqdnStatus := range grp.FQDNs { |
| 66 | + fqdnStatusLower := strings.ToLower(strings.TrimSuffix(fqdnStatus.FQDN, ".")) |
| 67 | + if fqdnStatusLower == fqdnLower { |
| 68 | + details := FQDNDetails{ |
| 69 | + Name: fqdnStatus.FQDN, |
| 70 | + Source: grp.Source, |
| 71 | + Group: grp.Name, |
| 72 | + Description: fqdnStatus.Description, |
| 73 | + RecordType: fqdnStatus.RecordType, |
| 74 | + Targets: fqdnStatus.Targets, |
| 75 | + Portal: dns.Spec.PortalRef, |
| 76 | + Namespace: dns.Namespace, |
| 77 | + DNSResource: fmt.Sprintf("%s/%s", dns.Namespace, dns.Name), |
| 78 | + } |
| 79 | + if !fqdnStatus.LastSeen.IsZero() { |
| 80 | + details.LastSeen = fqdnStatus.LastSeen.Format("2006-01-02T15:04:05Z07:00") |
| 81 | + } |
| 82 | + |
| 83 | + jsonBytes, err := json.MarshalIndent(details, "", " ") |
| 84 | + if err != nil { |
| 85 | + return mcp.NewToolResultError(fmt.Sprintf("failed to marshal details: %v", err)), nil |
| 86 | + } |
| 87 | + |
| 88 | + return mcp.NewToolResultText(fmt.Sprintf("FQDN details for '%s':\n\n%s", fqdn, string(jsonBytes))), nil |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Also check DNSRecords directly |
| 95 | + var dnsRecordList sreportalv1alpha1.DNSRecordList |
| 96 | + if err := s.client.List(ctx, &dnsRecordList); err != nil { |
| 97 | + return mcp.NewToolResultError(fmt.Sprintf("failed to list DNSRecord resources: %v", err)), nil |
| 98 | + } |
| 99 | + |
| 100 | + var allEndpoints []sreportalv1alpha1.EndpointStatus |
| 101 | + for _, rec := range dnsRecordList.Items { |
| 102 | + allEndpoints = append(allEndpoints, rec.Status.Endpoints...) |
| 103 | + } |
| 104 | + |
| 105 | + if len(allEndpoints) > 0 { |
| 106 | + groups := adapter.EndpointStatusToGroups(allEndpoints, s.groupMapping) |
| 107 | + for _, grp := range groups { |
| 108 | + for _, fqdnStatus := range grp.FQDNs { |
| 109 | + fqdnStatusLower := strings.ToLower(strings.TrimSuffix(fqdnStatus.FQDN, ".")) |
| 110 | + if fqdnStatusLower == fqdnLower { |
| 111 | + details := FQDNDetails{ |
| 112 | + Name: fqdnStatus.FQDN, |
| 113 | + Source: grp.Source, |
| 114 | + Group: grp.Name, |
| 115 | + RecordType: fqdnStatus.RecordType, |
| 116 | + Targets: fqdnStatus.Targets, |
| 117 | + } |
| 118 | + if !fqdnStatus.LastSeen.IsZero() { |
| 119 | + details.LastSeen = fqdnStatus.LastSeen.Format("2006-01-02T15:04:05Z07:00") |
| 120 | + } |
| 121 | + |
| 122 | + jsonBytes, err := json.MarshalIndent(details, "", " ") |
| 123 | + if err != nil { |
| 124 | + return mcp.NewToolResultError(fmt.Sprintf("failed to marshal details: %v", err)), nil |
| 125 | + } |
| 126 | + |
| 127 | + return mcp.NewToolResultText(fmt.Sprintf("FQDN details for '%s':\n\n%s", fqdn, string(jsonBytes))), nil |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return mcp.NewToolResultText(fmt.Sprintf("FQDN '%s' not found.", fqdn)), nil |
| 134 | +} |
0 commit comments