|
| 1 | +package resources |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 8 | + "github.com/mongodb/openapi/tools/cli/pkg/apiversion" |
| 9 | + "github.com/mongodb/openapi/tools/cli/pkg/openapi" |
| 10 | + "github.com/mongodb/openapi/tools/mcp-server/internal/registry" |
| 11 | + "github.com/oasdiff/kin-openapi/openapi3" |
| 12 | +) |
| 13 | + |
| 14 | +// SpecStats holds counts of the spec's top-level components. |
| 15 | +type SpecStats struct { |
| 16 | + Paths int `json:"paths"` |
| 17 | + Operations int `json:"operations"` |
| 18 | + Schemas int `json:"schemas"` |
| 19 | + Tags int `json:"tags"` |
| 20 | +} |
| 21 | + |
| 22 | +// SpecOverview is the response body for the openapi://specs/{alias} resource. |
| 23 | +type SpecOverview struct { |
| 24 | + Alias string `json:"alias"` |
| 25 | + SourceType registry.SourceType `json:"sourceType"` |
| 26 | + Title string `json:"title,omitempty"` |
| 27 | + Description string `json:"description,omitempty"` |
| 28 | + Stats SpecStats `json:"stats"` |
| 29 | + LatestStableVersion string `json:"latestStableVersion"` |
| 30 | + AvailableVersions []string `json:"availableVersions"` |
| 31 | + HasPreview bool `json:"hasPreview"` |
| 32 | + HasUpcoming bool `json:"hasUpcoming"` |
| 33 | +} |
| 34 | + |
| 35 | +func handleAlias(reg *registry.Registry, req *mcp.ReadResourceRequest) (*mcp.ReadResourceResult, error) { |
| 36 | + alias, err := aliasFromURI(req.Params.URI) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + |
| 41 | + entry, err := reg.GetByAlias(alias) |
| 42 | + if err != nil { |
| 43 | + return nil, fmt.Errorf("spec with alias %q not found", alias) |
| 44 | + } |
| 45 | + |
| 46 | + overview := buildSpecOverview(entry) |
| 47 | + |
| 48 | + data, err := json.Marshal(overview) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + return &mcp.ReadResourceResult{ |
| 54 | + Contents: []*mcp.ResourceContents{ |
| 55 | + {URI: req.Params.URI, MIMEType: mimeTypeJSON, Text: string(data)}, |
| 56 | + }, |
| 57 | + }, nil |
| 58 | +} |
| 59 | + |
| 60 | +func buildSpecOverview(entry *registry.Entry) SpecOverview { |
| 61 | + overview := SpecOverview{ |
| 62 | + Alias: entry.Alias, |
| 63 | + SourceType: entry.SourceType, |
| 64 | + } |
| 65 | + |
| 66 | + if entry.Spec == nil { |
| 67 | + return overview |
| 68 | + } |
| 69 | + |
| 70 | + if entry.Spec.Info != nil { |
| 71 | + overview.Title = entry.Spec.Info.Title |
| 72 | + overview.Description = entry.Spec.Info.Description |
| 73 | + } |
| 74 | + |
| 75 | + if entry.Spec.Paths != nil { |
| 76 | + overview.Stats.Paths = len(entry.Spec.Paths.Map()) |
| 77 | + overview.Stats.Operations = countOperations(entry.Spec) |
| 78 | + } |
| 79 | + |
| 80 | + if entry.Spec.Components != nil { |
| 81 | + overview.Stats.Schemas = len(entry.Spec.Components.Schemas) |
| 82 | + } |
| 83 | + |
| 84 | + overview.Stats.Tags = len(entry.Spec.Tags) |
| 85 | + |
| 86 | + stable, hasPreview, hasUpcoming := extractVersions(entry.Spec) |
| 87 | + overview.HasPreview = hasPreview |
| 88 | + overview.HasUpcoming = hasUpcoming |
| 89 | + // ExtractVersions returns versions sorted ascending by date string (YYYY-MM-DD). |
| 90 | + overview.AvailableVersions = stable |
| 91 | + if len(stable) > 0 { |
| 92 | + overview.LatestStableVersion = stable[len(stable)-1] |
| 93 | + } |
| 94 | + |
| 95 | + return overview |
| 96 | +} |
| 97 | + |
| 98 | +func countOperations(spec *openapi3.T) int { |
| 99 | + count := 0 |
| 100 | + for _, item := range spec.Paths.Map() { |
| 101 | + count += len(item.Operations()) |
| 102 | + } |
| 103 | + return count |
| 104 | +} |
| 105 | + |
| 106 | +func extractVersions(spec *openapi3.T) (stable []string, hasPreview, hasUpcoming bool) { |
| 107 | + stable = []string{} |
| 108 | + all, err := openapi.ExtractVersions(spec) |
| 109 | + if err != nil || len(all) == 0 { |
| 110 | + return stable, false, false |
| 111 | + } |
| 112 | + for _, v := range all { |
| 113 | + switch { |
| 114 | + case apiversion.IsPreviewStabilityLevel(v): |
| 115 | + hasPreview = true |
| 116 | + case apiversion.IsUpcomingStabilityLevel(v): |
| 117 | + hasUpcoming = true |
| 118 | + default: |
| 119 | + stable = append(stable, v) |
| 120 | + } |
| 121 | + } |
| 122 | + return stable, hasPreview, hasUpcoming |
| 123 | +} |
0 commit comments