Skip to content

Commit 9a9c710

Browse files
domdomeggrdimitrov
authored andcommitted
Improve golang type documentation and examples
Enhanced Go struct tags throughout pkg/api/v0/types.go and pkg/model/types.go to better align with the OpenAPI specification: - Added comprehensive doc tags with detailed field descriptions - Added example tags showing valid values for fields - Added format tags (uri, date-time) for better type specification - Added pattern and enum tags for validation constraints - Added placeholder field to Input struct matching OpenAPI spec - Removed redundant comments in favor of inline struct tags These changes improve code clarity and make the types more self-documenting, while maintaining compatibility with the OpenAPI schema definitions.
1 parent 69f45c4 commit 9a9c710

File tree

2 files changed

+58
-81
lines changed

2 files changed

+58
-81
lines changed

pkg/api/v0/types.go

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,46 @@ import (
66
"github.com/modelcontextprotocol/registry/pkg/model"
77
)
88

9-
// RegistryExtensions represents registry-generated metadata
109
type RegistryExtensions struct {
11-
Status model.Status `json:"status"`
12-
PublishedAt time.Time `json:"publishedAt"`
13-
UpdatedAt time.Time `json:"updatedAt,omitempty"`
14-
IsLatest bool `json:"isLatest"`
10+
Status model.Status `json:"status" enum:"active,deprecated,deleted" doc:"Server lifecycle status"`
11+
PublishedAt time.Time `json:"publishedAt" format:"date-time" doc:"Timestamp when the server was first published to the registry"`
12+
UpdatedAt time.Time `json:"updatedAt,omitempty" format:"date-time" doc:"Timestamp when the server entry was last updated"`
13+
IsLatest bool `json:"isLatest" doc:"Whether this is the latest version of the server"`
1514
}
1615

17-
// ResponseMeta represents the top-level metadata in API responses
1816
type ResponseMeta struct {
19-
Official *RegistryExtensions `json:"io.modelcontextprotocol.registry/official,omitempty"`
17+
Official *RegistryExtensions `json:"io.modelcontextprotocol.registry/official,omitempty" doc:"Official MCP registry metadata"`
2018
}
2119

22-
// ServerResponse represents the new API response format with separated metadata
2320
type ServerResponse struct {
24-
Server ServerJSON `json:"server"`
25-
Meta ResponseMeta `json:"_meta"`
21+
Server ServerJSON `json:"server" doc:"Server configuration and metadata"`
22+
Meta ResponseMeta `json:"_meta" doc:"Registry-managed metadata"`
2623
}
2724

28-
// ServerListResponse represents the paginated server list response
2925
type ServerListResponse struct {
30-
Servers []ServerResponse `json:"servers"`
31-
Metadata Metadata `json:"metadata"`
26+
Servers []ServerResponse `json:"servers" doc:"List of server entries"`
27+
Metadata Metadata `json:"metadata" doc:"Pagination metadata"`
3228
}
3329

34-
// ServerMeta represents the structured metadata with known extension fields
3530
type ServerMeta struct {
36-
PublisherProvided map[string]interface{} `json:"io.modelcontextprotocol.registry/publisher-provided,omitempty"`
31+
PublisherProvided map[string]interface{} `json:"io.modelcontextprotocol.registry/publisher-provided,omitempty" doc:"Publisher-provided metadata for downstream registries"`
3732
}
3833

39-
// ServerJSON represents complete server information as defined in the MCP spec, with extension support
4034
type ServerJSON struct {
41-
Schema string `json:"$schema" required:"true" minLength:"1"`
42-
Name string `json:"name" minLength:"1" maxLength:"200"`
43-
Description string `json:"description" minLength:"1" maxLength:"100"`
44-
Title string `json:"title,omitempty" minLength:"1" maxLength:"100"`
45-
Repository model.Repository `json:"repository,omitempty"`
46-
Version string `json:"version"`
47-
WebsiteURL string `json:"websiteUrl,omitempty"`
48-
Icons []model.Icon `json:"icons,omitempty"`
49-
Packages []model.Package `json:"packages,omitempty"`
50-
Remotes []model.Transport `json:"remotes,omitempty"`
51-
Meta *ServerMeta `json:"_meta,omitempty"`
35+
Schema string `json:"$schema" required:"true" minLength:"1" format:"uri" doc:"JSON Schema URI for this server.json format" example:"https://static.modelcontextprotocol.io/schemas/2025-09-29/server.schema.json"`
36+
Name string `json:"name" minLength:"3" maxLength:"200" pattern:"^[a-zA-Z0-9.-]+/[a-zA-Z0-9._-]+$" doc:"Server name in reverse-DNS format. Must contain exactly one forward slash separating namespace from server name." example:"io.github.user/weather"`
37+
Description string `json:"description" minLength:"1" maxLength:"100" doc:"Clear human-readable explanation of server functionality." example:"MCP server providing weather data and forecasts via OpenWeatherMap API"`
38+
Title string `json:"title,omitempty" minLength:"1" maxLength:"100" doc:"Optional human-readable title or display name for the MCP server." example:"Weather API"`
39+
Repository model.Repository `json:"repository,omitempty" doc:"Optional repository metadata for the MCP server source code."`
40+
Version string `json:"version" doc:"Version string for this server. SHOULD follow semantic versioning." example:"1.0.2"`
41+
WebsiteURL string `json:"websiteUrl,omitempty" format:"uri" doc:"Optional URL to the server's homepage, documentation, or project website." example:"https://modelcontextprotocol.io/examples"`
42+
Icons []model.Icon `json:"icons,omitempty" doc:"Optional set of sized icons that the client can display in a user interface."`
43+
Packages []model.Package `json:"packages,omitempty" doc:"Array of package configurations"`
44+
Remotes []model.Transport `json:"remotes,omitempty" doc:"Array of remote configurations"`
45+
Meta *ServerMeta `json:"_meta,omitempty" doc:"Extension metadata using reverse DNS namespacing for vendor-specific data"`
5246
}
5347

54-
// Metadata represents pagination metadata
5548
type Metadata struct {
56-
NextCursor string `json:"nextCursor,omitempty"`
57-
Count int `json:"count"`
49+
NextCursor string `json:"nextCursor,omitempty" doc:"Pagination cursor for retrieving the next page of results. Use this exact value in the cursor query parameter of your next request."`
50+
Count int `json:"count" doc:"Number of items in current page"`
5851
}

pkg/model/types.go

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package model
22

3-
// Status represents the lifecycle status of a server
43
type Status string
54

65
const (
@@ -9,11 +8,10 @@ const (
98
StatusDeleted Status = "deleted"
109
)
1110

12-
// Transport represents transport configuration with optional URL templating
1311
type Transport struct {
14-
Type string `json:"type"`
15-
URL string `json:"url,omitempty"`
16-
Headers []KeyValueInput `json:"headers,omitempty"`
12+
Type string `json:"type" doc:"Transport type (stdio, streamable-http, or sse)" example:"stdio"`
13+
URL string `json:"url,omitempty" doc:"URL for streamable-http or sse transports" example:"https://api.example.com/mcp"`
14+
Headers []KeyValueInput `json:"headers,omitempty" doc:"HTTP headers for streamable-http or sse transports"`
1715
}
1816

1917
// Package represents a package configuration.
@@ -25,39 +23,37 @@ type Transport struct {
2523
// - MCPB: RegistryType, Identifier (download URL), FileSHA256 (required)
2624
type Package struct {
2725
// RegistryType indicates how to download packages (e.g., "npm", "pypi", "oci", "nuget", "mcpb")
28-
RegistryType string `json:"registryType" minLength:"1"`
26+
RegistryType string `json:"registryType" minLength:"1" doc:"Registry type indicating how to download packages (e.g., 'npm', 'pypi', 'oci', 'nuget', 'mcpb')" example:"npm"`
2927
// RegistryBaseURL is the base URL of the package registry (used by npm, pypi, nuget; not used by oci, mcpb)
30-
RegistryBaseURL string `json:"registryBaseUrl,omitempty"`
28+
RegistryBaseURL string `json:"registryBaseUrl,omitempty" format:"uri" doc:"Base URL of the package registry" example:"https://registry.npmjs.org"`
3129
// Identifier is the package identifier:
3230
// - For NPM/PyPI/NuGet: package name or ID
3331
// - For OCI: full image reference (e.g., "ghcr.io/owner/repo:v1.0.0")
3432
// - For MCPB: direct download URL
35-
Identifier string `json:"identifier" minLength:"1"`
33+
Identifier string `json:"identifier" minLength:"1" doc:"Package identifier - either a package name (for registries) or URL (for direct downloads)" example:"@modelcontextprotocol/server-brave-search"`
3634
// Version is the package version (used by npm, pypi, nuget; not used by oci, mcpb where version is in the identifier)
37-
Version string `json:"version,omitempty" minLength:"1"`
35+
Version string `json:"version" minLength:"1" doc:"Package version. Must be a specific version. Version ranges are rejected (e.g., '^1.2.3', '~1.2.3', '>=1.2.3', '1.x', '1.*')." example:"1.0.2"`
3836
// FileSHA256 is the SHA-256 hash for integrity verification (required for mcpb, optional for others)
39-
FileSHA256 string `json:"fileSha256,omitempty"`
37+
FileSHA256 string `json:"fileSha256,omitempty" pattern:"^[a-f0-9]{64}$" doc:"SHA-256 hash of the package file for integrity verification. Required for MCPB packages and optional for other package types. Authors are responsible for generating correct SHA-256 hashes when creating server.json. If present, MCP clients must validate the downloaded file matches the hash before running packages to ensure file integrity." example:"fe333e598595000ae021bd27117db32ec69af6987f507ba7a63c90638ff633ce"`
4038
// RunTimeHint suggests the appropriate runtime for the package
41-
RunTimeHint string `json:"runtimeHint,omitempty"`
39+
RunTimeHint string `json:"runtimeHint,omitempty" doc:"A hint to help clients determine the appropriate runtime for the package. This field should be provided when runtimeArguments are present." example:"npx"`
4240
// Transport is required and specifies the transport protocol configuration
43-
Transport Transport `json:"transport"`
41+
Transport Transport `json:"transport,omitempty" doc:"Transport protocol configuration for the package"`
4442
// RuntimeArguments are passed to the package's runtime command (e.g., docker, npx)
45-
RuntimeArguments []Argument `json:"runtimeArguments,omitempty"`
43+
RuntimeArguments []Argument `json:"runtimeArguments,omitempty" doc:"A list of arguments to be passed to the package's runtime command (such as docker or npx). The runtimeHint field should be provided when runtimeArguments are present."`
4644
// PackageArguments are passed to the package's binary
47-
PackageArguments []Argument `json:"packageArguments,omitempty"`
45+
PackageArguments []Argument `json:"packageArguments,omitempty" doc:"A list of arguments to be passed to the package's binary."`
4846
// EnvironmentVariables are set when running the package
49-
EnvironmentVariables []KeyValueInput `json:"environmentVariables,omitempty"`
47+
EnvironmentVariables []KeyValueInput `json:"environmentVariables,omitempty" doc:"A mapping of environment variables to be set when running the package."`
5048
}
5149

52-
// Repository represents a source code repository as defined in the spec
5350
type Repository struct {
54-
URL string `json:"url"`
55-
Source string `json:"source"`
56-
ID string `json:"id,omitempty"`
57-
Subfolder string `json:"subfolder,omitempty"`
51+
URL string `json:"url" format:"uri" doc:"Repository URL for browsing source code. Should support both web browsing and git clone operations." example:"https://github.com/modelcontextprotocol/servers"`
52+
Source string `json:"source" doc:"Repository hosting service identifier. Used by registries to determine validation and API access methods." example:"github"`
53+
ID string `json:"id,omitempty" doc:"Repository identifier from the hosting service (e.g., GitHub repo ID). Owned and determined by the source forge. Should remain stable across repository renames and may be used to detect repository resurrection attacks - if a repository is deleted and recreated, the ID should change. For GitHub, use: gh api repos/<owner>/<repo> --jq '.id'" example:"b94b5f7e-c7c6-d760-2c78-a5e9b8a5b8c9"`
54+
Subfolder string `json:"subfolder,omitempty" doc:"Optional relative path from repository root to the server location within a monorepo or nested package structure. Must be a clean relative path." example:"src/everything"`
5855
}
5956

60-
// Format represents the input format type
6157
type Format string
6258

6359
const (
@@ -67,57 +63,45 @@ const (
6763
FormatFilePath Format = "filepath"
6864
)
6965

70-
// Input represents a configuration input
7166
type Input struct {
72-
Description string `json:"description,omitempty"`
73-
IsRequired bool `json:"isRequired,omitempty"`
74-
Format Format `json:"format,omitempty"`
75-
Value string `json:"value,omitempty"`
76-
IsSecret bool `json:"isSecret,omitempty"`
77-
Default string `json:"default,omitempty"`
78-
Choices []string `json:"choices,omitempty"`
67+
Description string `json:"description,omitempty" doc:"A description of the input, which clients can use to provide context to the user."`
68+
IsRequired bool `json:"isRequired,omitempty" default:"false" doc:"Whether the input is required"`
69+
Format Format `json:"format,omitempty" default:"string" enum:"string,number,boolean,filepath" doc:"Specifies the input format. Supported values include filepath, which should be interpreted as a file on the user's filesystem."`
70+
Value string `json:"value,omitempty" doc:"The value for the input. If this is not set, the user may be prompted to provide a value. Identifiers wrapped in {curly_braces} will be replaced with the corresponding properties from the input variables map."`
71+
IsSecret bool `json:"isSecret,omitempty" default:"false" doc:"Indicates whether the input is a secret value (e.g., password, token). If true, clients should handle the value securely."`
72+
Default string `json:"default,omitempty" doc:"The default value for the input. This should be a valid value for the input. If you want to provide input examples or guidance, use the placeholder field instead."`
73+
Placeholder string `json:"placeholder,omitempty" doc:"A placeholder for the input to be displaying during configuration. This is used to provide examples or guidance about the expected form or content of the input."`
74+
Choices []string `json:"choices,omitempty" doc:"A list of possible values for the input. If provided, the user must select one of these values."`
7975
}
8076

81-
// InputWithVariables represents an input that can contain variables
8277
type InputWithVariables struct {
8378
Input `json:",inline"`
84-
Variables map[string]Input `json:"variables,omitempty"`
79+
Variables map[string]Input `json:"variables,omitempty" doc:"A map of variable names to their values. Keys in the input value that are wrapped in {curly_braces} will be replaced with the corresponding variable values."`
8580
}
8681

87-
// KeyValueInput represents a named input with variables
8882
type KeyValueInput struct {
8983
InputWithVariables `json:",inline"`
90-
Name string `json:"name"`
84+
Name string `json:"name" doc:"Name of the header or environment variable." example:"SOME_VARIABLE"`
9185
}
9286

93-
// ArgumentType represents the type of argument
9487
type ArgumentType string
9588

9689
const (
9790
ArgumentTypePositional ArgumentType = "positional"
9891
ArgumentTypeNamed ArgumentType = "named"
9992
)
10093

101-
// Argument defines a type that can be either a PositionalArgument or a NamedArgument
10294
type Argument struct {
10395
InputWithVariables `json:",inline"`
104-
Type ArgumentType `json:"type"`
105-
Name string `json:"name,omitempty"`
106-
IsRepeated bool `json:"isRepeated,omitempty"`
107-
ValueHint string `json:"valueHint,omitempty"`
96+
Type ArgumentType `json:"type" doc:"Argument type: 'positional' or 'named'" example:"positional"`
97+
Name string `json:"name,omitempty" doc:"The flag name (for named arguments), including any leading dashes. Empty for positional arguments." example:"--port"`
98+
ValueHint string `json:"valueHint,omitempty" doc:"An identifier for positional arguments. Used in transport URL variable substitution." example:"file_path"`
99+
IsRepeated bool `json:"isRepeated,omitempty" default:"false" doc:"Whether the argument can be repeated multiple times."`
108100
}
109101

110-
// Icon represents an optionally-sized icon that can be displayed in a user interface
111102
type Icon struct {
112-
// Src is a standard URI pointing to an icon resource (HTTPS URL only for registry)
113-
Src string `json:"src" required:"true" format:"uri" maxLength:"255"`
114-
// MimeType is an optional MIME type override if the source MIME type is missing or generic
115-
MimeType *string `json:"mimeType,omitempty" enum:"image/png,image/jpeg,image/jpg,image/svg+xml,image/webp"`
116-
// Sizes is an optional array of strings that specify sizes at which the icon can be used
117-
// Each string should be in WxH format (e.g., "48x48", "96x96") or "any" for scalable formats
118-
Sizes []string `json:"sizes,omitempty" pattern:"^(\\d+x\\d+|any)$"`
119-
// Theme is an optional specifier for the theme this icon is designed for
120-
// "light" indicates the icon is designed for light backgrounds
121-
// "dark" indicates the icon is designed for dark backgrounds
122-
Theme *string `json:"theme,omitempty" enum:"light,dark"`
103+
Src string `json:"src" required:"true" format:"uri" maxLength:"255" doc:"A standard URI pointing to an icon resource. Must be an HTTPS URL. Consumers SHOULD take steps to ensure URLs serving icons are from the same domain as the server or a trusted domain. Consumers SHOULD take appropriate precautions when consuming SVGs as they can contain executable JavaScript." example:"https://example.com/icon.png"`
104+
MimeType *string `json:"mimeType,omitempty" enum:"image/png,image/jpeg,image/jpg,image/svg+xml,image/webp" doc:"Optional MIME type override if the source MIME type is missing or generic. Must be one of: image/png, image/jpeg, image/jpg, image/svg+xml, image/webp." example:"image/png"`
105+
Sizes []string `json:"sizes,omitempty" doc:"Optional array of strings that specify sizes at which the icon can be used. Each string should be in WxH format (e.g., '48x48', '96x96') or 'any' for scalable formats like SVG. If not provided, the client should assume that the icon can be used at any size." items.pattern:"^(\\d+x\\d+|any)$"`
106+
Theme *string `json:"theme,omitempty" enum:"light,dark" doc:"Optional specifier for the theme this icon is designed for. 'light' indicates the icon is designed to be used with a light background, and 'dark' indicates the icon is designed to be used with a dark background. If not provided, the client should assume the icon can be used with any theme."`
123107
}

0 commit comments

Comments
 (0)