Skip to content

Commit 2a80690

Browse files
authored
Introduce a /version API (#667)
<!-- Provide a brief summary of your changes --> ## Motivation and Context <!-- Why is this change needed? What problem does it solve? --> The following PR introduces a `/version` API that serves the build details along with the version of the API server being served. The `/version` endpoint allows users to programmatically query which exact version of the registry is running in production for debugging. ~~auditing, and compatibility checks~~. ## How Has This Been Tested? <!-- Have you tested this in a real application? Which scenarios were tested? --> ## Breaking Changes <!-- Will users need to update their code or configurations? --> ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [ ] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [ ] My code follows the repository's style guidelines - [ ] New and existing tests pass locally - [ ] I have added appropriate error handling - [ ] I have added or updated documentation as needed ## Additional context <!-- Add any other context, implementation notes, or design decisions --> Signed-off-by: Radoslav Dimitrov <[email protected]>
1 parent d9099e9 commit 2a80690

File tree

7 files changed

+142
-10
lines changed

7 files changed

+142
-10
lines changed

cmd/registry/main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/modelcontextprotocol/registry/internal/api"
15+
v0 "github.com/modelcontextprotocol/registry/internal/api/handlers/v0"
1516
"github.com/modelcontextprotocol/registry/internal/config"
1617
"github.com/modelcontextprotocol/registry/internal/database"
1718
"github.com/modelcontextprotocol/registry/internal/importer"
@@ -39,7 +40,7 @@ func main() {
3940

4041
// Show version information if requested
4142
if *showVersion {
42-
log.Printf("MCP Registry v%s\n", Version)
43+
log.Printf("MCP Registry %s\n", Version)
4344
log.Printf("Git commit: %s\n", GitCommit)
4445
log.Printf("Build time: %s\n", BuildTime)
4546
return
@@ -102,8 +103,15 @@ func main() {
102103
}
103104
}()
104105

106+
// Prepare version information
107+
versionInfo := &v0.VersionBody{
108+
Version: Version,
109+
GitCommit: GitCommit,
110+
BuildTime: BuildTime,
111+
}
112+
105113
// Initialize HTTP server
106-
server := api.NewServer(cfg, registryService, metrics)
114+
server := api.NewServer(cfg, registryService, metrics, versionInfo)
107115

108116
// Start server in a goroutine so it doesn't block signal handling
109117
go func() {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package v0
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"strings"
7+
8+
"github.com/danielgtaylor/huma/v2"
9+
)
10+
11+
// VersionBody represents the version information
12+
type VersionBody struct {
13+
Version string `json:"version" example:"v1.0.0" doc:"Application version"`
14+
GitCommit string `json:"git_commit" example:"abc123d" doc:"Git commit SHA"`
15+
BuildTime string `json:"build_time" example:"2025-10-14T12:00:00Z" doc:"Build timestamp"`
16+
}
17+
18+
// RegisterVersionEndpoint registers the version endpoint with a custom path prefix
19+
func RegisterVersionEndpoint(api huma.API, pathPrefix string, versionInfo *VersionBody) {
20+
huma.Register(api, huma.Operation{
21+
OperationID: "get-version" + strings.ReplaceAll(pathPrefix, "/", "-"),
22+
Method: http.MethodGet,
23+
Path: pathPrefix + "/version",
24+
Summary: "Get version information",
25+
Description: "Returns the version, git commit, and build time of the registry application",
26+
Tags: []string{"version"},
27+
}, func(_ context.Context, _ *struct{}) (*Response[VersionBody], error) {
28+
return &Response[VersionBody]{
29+
Body: *versionInfo,
30+
}, nil
31+
})
32+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package v0_test
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"github.com/danielgtaylor/huma/v2"
9+
"github.com/danielgtaylor/huma/v2/adapters/humago"
10+
"github.com/stretchr/testify/assert"
11+
12+
v0 "github.com/modelcontextprotocol/registry/internal/api/handlers/v0"
13+
)
14+
15+
func TestVersionEndpoint(t *testing.T) {
16+
// Test cases
17+
testCases := []struct {
18+
name string
19+
versionInfo *v0.VersionBody
20+
expectedBody map[string]string
21+
}{
22+
{
23+
name: "returns version information",
24+
versionInfo: &v0.VersionBody{
25+
Version: "v1.2.3",
26+
GitCommit: "abc123def456",
27+
BuildTime: "2025-10-14T12:00:00Z",
28+
},
29+
expectedBody: map[string]string{
30+
"version": "v1.2.3",
31+
"git_commit": "abc123def456",
32+
"build_time": "2025-10-14T12:00:00Z",
33+
},
34+
},
35+
{
36+
name: "returns dev version information",
37+
versionInfo: &v0.VersionBody{
38+
Version: "dev",
39+
GitCommit: "unknown",
40+
BuildTime: "unknown",
41+
},
42+
expectedBody: map[string]string{
43+
"version": "dev",
44+
"git_commit": "unknown",
45+
"build_time": "unknown",
46+
},
47+
},
48+
}
49+
50+
for _, tc := range testCases {
51+
t.Run(tc.name, func(t *testing.T) {
52+
// Create a new test API
53+
mux := http.NewServeMux()
54+
api := humago.New(mux, huma.DefaultConfig("Test API", "1.0.0"))
55+
56+
// Register the version endpoint
57+
v0.RegisterVersionEndpoint(api, "/v0", tc.versionInfo)
58+
59+
// Create a test request
60+
req := httptest.NewRequest(http.MethodGet, "/v0/version", nil)
61+
w := httptest.NewRecorder()
62+
63+
// Serve the request
64+
mux.ServeHTTP(w, req)
65+
66+
// Check the status code
67+
assert.Equal(t, http.StatusOK, w.Code)
68+
69+
// Check the response body
70+
body := w.Body.String()
71+
assert.Contains(t, body, `"version":"`+tc.expectedBody["version"]+`"`)
72+
assert.Contains(t, body, `"git_commit":"`+tc.expectedBody["git_commit"]+`"`)
73+
assert.Contains(t, body, `"build_time":"`+tc.expectedBody["build_time"]+`"`)
74+
})
75+
}
76+
}

internal/api/openapi_compliance_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/stretchr/testify/require"
1515
"gopkg.in/yaml.v3"
1616

17+
v0 "github.com/modelcontextprotocol/registry/internal/api/handlers/v0"
1718
"github.com/modelcontextprotocol/registry/internal/api/router"
1819
"github.com/modelcontextprotocol/registry/internal/config"
1920
)
@@ -42,8 +43,15 @@ func TestOpenAPIEndpointCompliance(t *testing.T) {
4243
JWTPrivateKey: "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20", // 32-byte hex key
4344
}
4445

46+
// Create version info for testing
47+
versionInfo := &v0.VersionBody{
48+
Version: "test",
49+
GitCommit: "test",
50+
BuildTime: "test",
51+
}
52+
4553
// Register V0 routes exactly like production does
46-
router.RegisterV0Routes(api, cfg, nil, nil) // nil service and metrics for schema testing
54+
router.RegisterV0Routes(api, cfg, nil, nil, versionInfo) // nil service and metrics for schema testing
4755

4856
// Get the OpenAPI schema
4957
req := httptest.NewRequest(http.MethodGet, "/openapi.yaml", nil)

internal/api/router/router.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"go.opentelemetry.io/otel/attribute"
1414
"go.opentelemetry.io/otel/metric"
1515

16+
v0 "github.com/modelcontextprotocol/registry/internal/api/handlers/v0"
1617
"github.com/modelcontextprotocol/registry/internal/config"
1718
"github.com/modelcontextprotocol/registry/internal/service"
1819
"github.com/modelcontextprotocol/registry/internal/telemetry"
@@ -129,7 +130,7 @@ func handle404(w http.ResponseWriter, r *http.Request) {
129130
}
130131

131132
// NewHumaAPI creates a new Huma API with all routes registered
132-
func NewHumaAPI(cfg *config.Config, registry service.RegistryService, mux *http.ServeMux, metrics *telemetry.Metrics) huma.API {
133+
func NewHumaAPI(cfg *config.Config, registry service.RegistryService, mux *http.ServeMux, metrics *telemetry.Metrics, versionInfo *v0.VersionBody) huma.API {
133134
// Create Huma API configuration
134135
humaConfig := huma.DefaultConfig("Official MCP Registry", "1.0.0")
135136
humaConfig.Info.Description = "A community driven registry service for Model Context Protocol (MCP) servers.\n\n[GitHub repository](https://github.com/modelcontextprotocol/registry) | [Documentation](https://github.com/modelcontextprotocol/registry/tree/main/docs)"
@@ -165,6 +166,10 @@ func NewHumaAPI(cfg *config.Config, registry service.RegistryService, mux *http.
165166
Name: "ping",
166167
Description: "Simple ping endpoint for testing connectivity",
167168
},
169+
{
170+
Name: "version",
171+
Description: "Version information endpoint for retrieving build and version details",
172+
},
168173
}
169174

170175
// Add metrics middleware with options
@@ -173,8 +178,8 @@ func NewHumaAPI(cfg *config.Config, registry service.RegistryService, mux *http.
173178
))
174179

175180
// Register routes for all API versions
176-
RegisterV0Routes(api, cfg, registry, metrics)
177-
RegisterV0_1Routes(api, cfg, registry, metrics)
181+
RegisterV0Routes(api, cfg, registry, metrics, versionInfo)
182+
RegisterV0_1Routes(api, cfg, registry, metrics, versionInfo)
178183

179184
// Add /metrics for Prometheus metrics using promhttp
180185
mux.Handle("/metrics", metrics.PrometheusHandler())

internal/api/router/v0.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@ import (
1212
)
1313

1414
func RegisterV0Routes(
15-
api huma.API, cfg *config.Config, registry service.RegistryService, metrics *telemetry.Metrics,
15+
api huma.API, cfg *config.Config, registry service.RegistryService, metrics *telemetry.Metrics, versionInfo *v0.VersionBody,
1616
) {
1717
v0.RegisterHealthEndpoint(api, "/v0", cfg, metrics)
1818
v0.RegisterPingEndpoint(api, "/v0")
19+
v0.RegisterVersionEndpoint(api, "/v0", versionInfo)
1920
v0.RegisterServersEndpoints(api, "/v0", registry)
2021
v0.RegisterEditEndpoints(api, "/v0", registry, cfg)
2122
v0auth.RegisterAuthEndpoints(api, "/v0", cfg)
2223
v0.RegisterPublishEndpoint(api, "/v0", registry, cfg)
2324
}
2425

2526
func RegisterV0_1Routes(
26-
api huma.API, cfg *config.Config, registry service.RegistryService, metrics *telemetry.Metrics,
27+
api huma.API, cfg *config.Config, registry service.RegistryService, metrics *telemetry.Metrics, versionInfo *v0.VersionBody,
2728
) {
2829
v0.RegisterHealthEndpoint(api, "/v0.1", cfg, metrics)
2930
v0.RegisterPingEndpoint(api, "/v0.1")
31+
v0.RegisterVersionEndpoint(api, "/v0.1", versionInfo)
3032
v0.RegisterServersEndpoints(api, "/v0.1", registry)
3133
v0.RegisterEditEndpoints(api, "/v0.1", registry, cfg)
3234
v0auth.RegisterAuthEndpoints(api, "/v0.1", cfg)

internal/api/server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/danielgtaylor/huma/v2"
1111

12+
v0 "github.com/modelcontextprotocol/registry/internal/api/handlers/v0"
1213
"github.com/modelcontextprotocol/registry/internal/api/router"
1314
"github.com/modelcontextprotocol/registry/internal/config"
1415
"github.com/modelcontextprotocol/registry/internal/service"
@@ -42,11 +43,11 @@ type Server struct {
4243
}
4344

4445
// NewServer creates a new HTTP server
45-
func NewServer(cfg *config.Config, registryService service.RegistryService, metrics *telemetry.Metrics) *Server {
46+
func NewServer(cfg *config.Config, registryService service.RegistryService, metrics *telemetry.Metrics, versionInfo *v0.VersionBody) *Server {
4647
// Create HTTP mux and Huma API
4748
mux := http.NewServeMux()
4849

49-
api := router.NewHumaAPI(cfg, registryService, mux, metrics)
50+
api := router.NewHumaAPI(cfg, registryService, mux, metrics, versionInfo)
5051

5152
// Wrap the mux with trailing slash middleware
5253
handler := TrailingSlashMiddleware(mux)

0 commit comments

Comments
 (0)