|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/raystack/compass/core/asset" |
| 8 | +) |
| 9 | + |
| 10 | +// formatAsset formats an asset as LLM-friendly markdown text. |
| 11 | +func formatAsset(a asset.Asset) string { |
| 12 | + var b strings.Builder |
| 13 | + |
| 14 | + fmt.Fprintf(&b, "## %s (%s)\n", a.Name, a.Type) |
| 15 | + fmt.Fprintf(&b, "Service: %s | URN: %s\n", a.Service, a.URN) |
| 16 | + |
| 17 | + if a.Description != "" { |
| 18 | + fmt.Fprintf(&b, "Description: %s\n", a.Description) |
| 19 | + } |
| 20 | + |
| 21 | + if len(a.Owners) > 0 { |
| 22 | + names := make([]string, 0, len(a.Owners)) |
| 23 | + for _, o := range a.Owners { |
| 24 | + if o.Email != "" { |
| 25 | + names = append(names, o.Email) |
| 26 | + } else { |
| 27 | + names = append(names, o.UUID) |
| 28 | + } |
| 29 | + } |
| 30 | + fmt.Fprintf(&b, "Owners: %s\n", strings.Join(names, ", ")) |
| 31 | + } |
| 32 | + |
| 33 | + if a.URL != "" { |
| 34 | + fmt.Fprintf(&b, "URL: %s\n", a.URL) |
| 35 | + } |
| 36 | + |
| 37 | + if len(a.Labels) > 0 { |
| 38 | + pairs := make([]string, 0, len(a.Labels)) |
| 39 | + for k, v := range a.Labels { |
| 40 | + pairs = append(pairs, fmt.Sprintf("%s=%s", k, v)) |
| 41 | + } |
| 42 | + fmt.Fprintf(&b, "Labels: %s\n", strings.Join(pairs, ", ")) |
| 43 | + } |
| 44 | + |
| 45 | + formatAssetData(&b, a.Data) |
| 46 | + |
| 47 | + return b.String() |
| 48 | +} |
| 49 | + |
| 50 | +// formatAssetData formats the Data map, extracting schema columns if present. |
| 51 | +func formatAssetData(b *strings.Builder, data map[string]interface{}) { |
| 52 | + if data == nil { |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + // Extract schema/columns if present (common in table/topic assets) |
| 57 | + if columns, ok := extractColumns(data); ok && len(columns) > 0 { |
| 58 | + fmt.Fprintf(b, "\nColumns (%d):\n", len(columns)) |
| 59 | + for _, col := range columns { |
| 60 | + name, _ := col["name"].(string) |
| 61 | + dataType, _ := col["data_type"].(string) |
| 62 | + desc, _ := col["description"].(string) |
| 63 | + |
| 64 | + if desc != "" { |
| 65 | + fmt.Fprintf(b, " - %s (%s): %s\n", name, dataType, desc) |
| 66 | + } else { |
| 67 | + fmt.Fprintf(b, " - %s (%s)\n", name, dataType) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// extractColumns tries to find column definitions in asset data. |
| 74 | +func extractColumns(data map[string]interface{}) ([]map[string]interface{}, bool) { |
| 75 | + // Try common paths: data.columns, data.schema.columns |
| 76 | + if cols, ok := data["columns"]; ok { |
| 77 | + return toMapSlice(cols) |
| 78 | + } |
| 79 | + if schema, ok := data["schema"].(map[string]interface{}); ok { |
| 80 | + if cols, ok := schema["columns"]; ok { |
| 81 | + return toMapSlice(cols) |
| 82 | + } |
| 83 | + } |
| 84 | + return nil, false |
| 85 | +} |
| 86 | + |
| 87 | +func toMapSlice(v interface{}) ([]map[string]interface{}, bool) { |
| 88 | + slice, ok := v.([]interface{}) |
| 89 | + if !ok { |
| 90 | + return nil, false |
| 91 | + } |
| 92 | + result := make([]map[string]interface{}, 0, len(slice)) |
| 93 | + for _, item := range slice { |
| 94 | + if m, ok := item.(map[string]interface{}); ok { |
| 95 | + result = append(result, m) |
| 96 | + } |
| 97 | + } |
| 98 | + return result, len(result) > 0 |
| 99 | +} |
| 100 | + |
| 101 | +// formatSearchResult formats a search result as a compact line. |
| 102 | +func formatSearchResult(sr asset.SearchResult) string { |
| 103 | + var b strings.Builder |
| 104 | + fmt.Fprintf(&b, "- **%s** (%s) — service: %s, urn: %s", sr.Title, sr.Type, sr.Service, sr.URN) |
| 105 | + if sr.Description != "" { |
| 106 | + desc := sr.Description |
| 107 | + if len(desc) > 120 { |
| 108 | + desc = desc[:120] + "..." |
| 109 | + } |
| 110 | + fmt.Fprintf(&b, "\n %s", desc) |
| 111 | + } |
| 112 | + return b.String() |
| 113 | +} |
| 114 | + |
| 115 | +// formatSearchResults formats a list of search results. |
| 116 | +func formatSearchResults(results []asset.SearchResult) string { |
| 117 | + if len(results) == 0 { |
| 118 | + return "No assets found." |
| 119 | + } |
| 120 | + |
| 121 | + var b strings.Builder |
| 122 | + fmt.Fprintf(&b, "Found %d assets:\n\n", len(results)) |
| 123 | + for _, sr := range results { |
| 124 | + b.WriteString(formatSearchResult(sr)) |
| 125 | + b.WriteString("\n") |
| 126 | + } |
| 127 | + return b.String() |
| 128 | +} |
| 129 | + |
| 130 | +// formatLineage formats lineage data as readable text. |
| 131 | +func formatLineage(urn string, lineage asset.Lineage) string { |
| 132 | + if len(lineage.Edges) == 0 { |
| 133 | + return fmt.Sprintf("No lineage found for %s.", urn) |
| 134 | + } |
| 135 | + |
| 136 | + var b strings.Builder |
| 137 | + fmt.Fprintf(&b, "Lineage for %s (%d edges):\n\n", urn, len(lineage.Edges)) |
| 138 | + |
| 139 | + upstreams := make([]string, 0) |
| 140 | + downstreams := make([]string, 0) |
| 141 | + |
| 142 | + for _, edge := range lineage.Edges { |
| 143 | + if edge.Target == urn { |
| 144 | + upstreams = append(upstreams, edge.Source) |
| 145 | + } else if edge.Source == urn { |
| 146 | + downstreams = append(downstreams, edge.Target) |
| 147 | + } else { |
| 148 | + // Transitive edges |
| 149 | + fmt.Fprintf(&b, " %s → %s\n", edge.Source, edge.Target) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if len(upstreams) > 0 { |
| 154 | + b.WriteString("Upstream (sources):\n") |
| 155 | + for _, u := range upstreams { |
| 156 | + fmt.Fprintf(&b, " ← %s\n", u) |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + if len(downstreams) > 0 { |
| 161 | + b.WriteString("Downstream (consumers):\n") |
| 162 | + for _, d := range downstreams { |
| 163 | + fmt.Fprintf(&b, " → %s\n", d) |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return b.String() |
| 168 | +} |
| 169 | + |
| 170 | +// formatTypes formats asset type counts. |
| 171 | +func formatTypes(types map[asset.Type]int) string { |
| 172 | + if len(types) == 0 { |
| 173 | + return "No asset types found." |
| 174 | + } |
| 175 | + |
| 176 | + var b strings.Builder |
| 177 | + b.WriteString("Asset types:\n\n") |
| 178 | + for t, count := range types { |
| 179 | + fmt.Fprintf(&b, "- %s: %d assets\n", t, count) |
| 180 | + } |
| 181 | + return b.String() |
| 182 | +} |
| 183 | + |
| 184 | +// formatAssets formats a list of assets as a summary list. |
| 185 | +func formatAssets(assets []asset.Asset, total uint32) string { |
| 186 | + if len(assets) == 0 { |
| 187 | + return "No assets found." |
| 188 | + } |
| 189 | + |
| 190 | + var b strings.Builder |
| 191 | + if total > 0 { |
| 192 | + fmt.Fprintf(&b, "Showing %d of %d assets:\n\n", len(assets), total) |
| 193 | + } else { |
| 194 | + fmt.Fprintf(&b, "Found %d assets:\n\n", len(assets)) |
| 195 | + } |
| 196 | + |
| 197 | + for _, a := range assets { |
| 198 | + fmt.Fprintf(&b, "- **%s** (%s) — service: %s, urn: %s\n", a.Name, a.Type, a.Service, a.URN) |
| 199 | + } |
| 200 | + return b.String() |
| 201 | +} |
0 commit comments