Skip to content

Commit e23d4e2

Browse files
committed
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 568748e commit e23d4e2

File tree

2 files changed

+58
-85
lines changed

2 files changed

+58
-85
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 & 55 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,39 +8,32 @@ 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

19-
// Package represents a package configuration
2017
type Package struct {
21-
// RegistryType indicates how to download packages (e.g., "npm", "pypi", "oci", "mcpb")
22-
RegistryType string `json:"registryType" minLength:"1"`
23-
// RegistryBaseURL is the base URL of the package registry
24-
RegistryBaseURL string `json:"registryBaseUrl,omitempty"`
25-
// Identifier is the package identifier - either a package name (for registries) or URL (for direct downloads)
26-
Identifier string `json:"identifier" minLength:"1"`
27-
Version string `json:"version" minLength:"1"`
28-
FileSHA256 string `json:"fileSha256,omitempty"`
29-
RunTimeHint string `json:"runtimeHint,omitempty"`
30-
Transport Transport `json:"transport,omitempty"`
31-
RuntimeArguments []Argument `json:"runtimeArguments,omitempty"`
32-
PackageArguments []Argument `json:"packageArguments,omitempty"`
33-
EnvironmentVariables []KeyValueInput `json:"environmentVariables,omitempty"`
18+
RegistryType string `json:"registryType" minLength:"1" doc:"Registry type indicating how to download packages (e.g., 'npm', 'pypi', 'oci', 'nuget', 'mcpb')" example:"npm"`
19+
RegistryBaseURL string `json:"registryBaseUrl,omitempty" format:"uri" doc:"Base URL of the package registry" example:"https://registry.npmjs.org"`
20+
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"`
21+
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"`
22+
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"`
23+
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"`
24+
Transport Transport `json:"transport,omitempty" doc:"Transport protocol configuration for the package"`
25+
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."`
26+
PackageArguments []Argument `json:"packageArguments,omitempty" doc:"A list of arguments to be passed to the package's binary."`
27+
EnvironmentVariables []KeyValueInput `json:"environmentVariables,omitempty" doc:"A mapping of environment variables to be set when running the package."`
3428
}
3529

36-
// Repository represents a source code repository as defined in the spec
3730
type Repository struct {
38-
URL string `json:"url"`
39-
Source string `json:"source"`
40-
ID string `json:"id,omitempty"`
41-
Subfolder string `json:"subfolder,omitempty"`
31+
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"`
32+
Source string `json:"source" doc:"Repository hosting service identifier. Used by registries to determine validation and API access methods." example:"github"`
33+
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"`
34+
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"`
4235
}
4336

44-
// Format represents the input format type
4537
type Format string
4638

4739
const (
@@ -51,57 +43,45 @@ const (
5143
FormatFilePath Format = "filepath"
5244
)
5345

54-
// Input represents a configuration input
5546
type Input struct {
56-
Description string `json:"description,omitempty"`
57-
IsRequired bool `json:"isRequired,omitempty"`
58-
Format Format `json:"format,omitempty"`
59-
Value string `json:"value,omitempty"`
60-
IsSecret bool `json:"isSecret,omitempty"`
61-
Default string `json:"default,omitempty"`
62-
Choices []string `json:"choices,omitempty"`
47+
Description string `json:"description,omitempty" doc:"A description of the input, which clients can use to provide context to the user."`
48+
IsRequired bool `json:"isRequired,omitempty" default:"false" doc:"Whether the input is required"`
49+
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."`
50+
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."`
51+
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."`
52+
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."`
53+
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."`
54+
Choices []string `json:"choices,omitempty" doc:"A list of possible values for the input. If provided, the user must select one of these values."`
6355
}
6456

65-
// InputWithVariables represents an input that can contain variables
6657
type InputWithVariables struct {
6758
Input `json:",inline"`
68-
Variables map[string]Input `json:"variables,omitempty"`
59+
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."`
6960
}
7061

71-
// KeyValueInput represents a named input with variables
7262
type KeyValueInput struct {
7363
InputWithVariables `json:",inline"`
74-
Name string `json:"name"`
64+
Name string `json:"name" doc:"Name of the header or environment variable." example:"SOME_VARIABLE"`
7565
}
7666

77-
// ArgumentType represents the type of argument
7867
type ArgumentType string
7968

8069
const (
8170
ArgumentTypePositional ArgumentType = "positional"
8271
ArgumentTypeNamed ArgumentType = "named"
8372
)
8473

85-
// Argument defines a type that can be either a PositionalArgument or a NamedArgument
8674
type Argument struct {
8775
InputWithVariables `json:",inline"`
88-
Type ArgumentType `json:"type"`
89-
Name string `json:"name,omitempty"`
90-
IsRepeated bool `json:"isRepeated,omitempty"`
91-
ValueHint string `json:"valueHint,omitempty"`
76+
Type ArgumentType `json:"type" doc:"Argument type: 'positional' or 'named'" example:"positional"`
77+
Name string `json:"name,omitempty" doc:"The flag name (for named arguments), including any leading dashes. Empty for positional arguments." example:"--port"`
78+
ValueHint string `json:"valueHint,omitempty" doc:"An identifier for positional arguments. Used in transport URL variable substitution." example:"file_path"`
79+
IsRepeated bool `json:"isRepeated,omitempty" default:"false" doc:"Whether the argument can be repeated multiple times."`
9280
}
9381

94-
// Icon represents an optionally-sized icon that can be displayed in a user interface
9582
type Icon struct {
96-
// Src is a standard URI pointing to an icon resource (HTTPS URL only for registry)
97-
Src string `json:"src" required:"true" format:"uri" maxLength:"255"`
98-
// MimeType is an optional MIME type override if the source MIME type is missing or generic
99-
MimeType *string `json:"mimeType,omitempty" enum:"image/png,image/jpeg,image/jpg,image/svg+xml,image/webp"`
100-
// Sizes is an optional array of strings that specify sizes at which the icon can be used
101-
// Each string should be in WxH format (e.g., "48x48", "96x96") or "any" for scalable formats
102-
Sizes []string `json:"sizes,omitempty" pattern:"^(\\d+x\\d+|any)$"`
103-
// Theme is an optional specifier for the theme this icon is designed for
104-
// "light" indicates the icon is designed for light backgrounds
105-
// "dark" indicates the icon is designed for dark backgrounds
106-
Theme *string `json:"theme,omitempty" enum:"light,dark"`
83+
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"`
84+
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"`
85+
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)$"`
86+
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."`
10787
}

0 commit comments

Comments
 (0)