Documentation
¶
Overview ¶
Package registry contains shared types, utilities and constants for registry operations
Package registry provides utilities for working with MCP registry data structures and conversions between different registry formats.
This package serves as a central location for registry-related utilities, including metadata extraction, format conversions, and test utilities for building registry instances in a consistent, maintainable way.
Core Components ¶
- Tag Extraction: Utilities for extracting tags from upstream server metadata
- Test Utilities: Builder pattern functions for creating test registry data
- Format Conversions: Helpers for converting between registry formats
Tag Extraction ¶
The ExtractTags function extracts tags from the nested metadata structure used by the upstream MCP registry format. Tags are stored in the PublisherProvided metadata following ToolHive conventions:
tags := ExtractTags(server) // Returns []string of tags from server.Meta.PublisherProvided["provider"]["metadata"]["tags"]
Test Utilities ¶
The package provides a comprehensive set of test builder functions following the options pattern, enabling creation of realistic test data without manual struct construction or JSON unmarshalling:
// Create a test registry with servers
testRegistry := registry.NewTestUpstreamRegistry(
registry.WithVersion("1.0.0"),
registry.WithServers(
registry.NewTestServer("postgres",
registry.WithDescription("PostgreSQL MCP server"),
registry.WithOCIPackage("postgres:latest"),
registry.WithTags("database", "sql"),
registry.WithToolHiveMetadata("tier", "Official"),
),
),
)
Registry Builder Options ¶
Registry-level options for customizing the UpstreamRegistry:
- WithVersion: Set the registry version
- WithLastUpdated: Set the last updated timestamp
- WithServers: Add one or more servers to the registry
Server Builder Options ¶
Server-level options for customizing ServerJSON instances:
- WithDescription: Set server description
- WithServerVersion: Set server version
- WithOCIPackage: Add an OCI (container) package
- WithHTTPPackage: Add an HTTP (remote) package
- WithTags: Add tags (stored in provider.metadata.tags)
- WithNamespace: Prepend namespace to server name (e.g., "io.test/")
- WithMetadata: Add arbitrary top-level metadata
- WithToolHiveMetadata: Add ToolHive-specific metadata (stored in provider.toolhive)
Metadata Structure ¶
The package follows specific conventions for storing metadata in the upstream format:
- Tags: Meta.PublisherProvided["provider"]["metadata"]["tags"]
- ToolHive fields: Meta.PublisherProvided["provider"]["toolhive"]["field_name"]
- Custom metadata: Meta.PublisherProvided["custom_key"]
ToolHive-specific fields stored in the toolhive namespace include:
- tier: Server tier (Official, Community, Enterprise)
- status: Server status (Active, Deprecated, Experimental)
- transport: Transport protocol (stdio, http, sse, streamable-http)
- tools: Array of tool names provided by the server
- repository_url: Source repository URL
- metadata: Additional metadata (stars, pulls, last_updated)
- permissions: Permission requirements
- env_vars: Environment variable definitions
- args: Command-line arguments
- headers: HTTP headers (for remote servers)
- oauth_config: OAuth configuration (for remote servers)
- provenance: Provenance information
- target_port: Target port for HTTP servers
Usage in Tests ¶
The test utilities are designed to be used across all test files in the project, providing consistency and reducing code duplication:
// In any test file
import "github.com/stacklok/toolhive-registry-server/internal/registry"
func TestMyFunction(t *testing.T) {
reg := registry.NewTestUpstreamRegistry(
registry.WithServers(
registry.NewTestServer("my-server",
registry.WithOCIPackage("my-image:latest"),
),
),
)
// Use reg in your test...
}
Design Principles ¶
- Options Pattern: Provides flexibility and readability in test data construction
- Type Safety: Works directly with strongly-typed structs, no JSON marshalling
- Discoverability: Builder functions are self-documenting through option names
- Consistency: Ensures all tests use the same metadata structure conventions
- Maintainability: Changes to data structure only require updates in one place
Format Compatibility ¶
The utilities support creating test data that is compatible with:
- Upstream MCP registry format (primary)
- ToolHive registry format (via conversion functions)
- Filtering operations (name patterns, tags)
- API serialization (JSON output)
Index ¶
- Constants
- func EmptyToolHiveJSON() []byte
- func ExtractTags(server *upstream.ServerJSON) []string
- func InvalidJSON() []byte
- func NewTestServer(name string, opts ...ServerOption) upstreamv0.ServerJSON
- func NewTestToolHiveRegistry(opts ...ToolHiveRegistryOption) *toolhivetypes.Registry
- func NewTestUpstreamRegistry(opts ...UpstreamRegistryOption) *toolhivetypes.UpstreamRegistry
- func ToolHiveRegistryToJSON(reg *toolhivetypes.Registry) []byte
- func ToolHiveRegistryToPrettyJSON(reg *toolhivetypes.Registry) []byte
- type ImageServerOption
- func WithImageDescription(description string) ImageServerOption
- func WithImageStatus(status string) ImageServerOption
- func WithImageTags(tags ...string) ImageServerOption
- func WithImageTier(tier string) ImageServerOption
- func WithImageTools(tools ...string) ImageServerOption
- func WithImageTransport(transport string) ImageServerOption
- type RemoteServerOption
- func WithRemoteDescription(description string) RemoteServerOption
- func WithRemoteStatus(status string) RemoteServerOption
- func WithRemoteTags(tags ...string) RemoteServerOption
- func WithRemoteTier(tier string) RemoteServerOption
- func WithRemoteTools(tools ...string) RemoteServerOption
- func WithRemoteTransport(transport string) RemoteServerOption
- type ServerOption
- func WithDescription(description string) ServerOption
- func WithHTTPPackage(url string) ServerOption
- func WithMetadata(key string, value any) ServerOption
- func WithNamespace(namespace string) ServerOption
- func WithOCIPackage(image string) ServerOption
- func WithServerVersion(version string) ServerOption
- func WithTags(tags ...string) ServerOption
- func WithToolHiveMetadata(key string, value any) ServerOption
- type ToolHiveRegistryOption
- func WithImageServer(name, image string, opts ...ImageServerOption) ToolHiveRegistryOption
- func WithRemoteServerURL(name, url string, opts ...RemoteServerOption) ToolHiveRegistryOption
- func WithToolHiveLastUpdated(timestamp string) ToolHiveRegistryOption
- func WithToolHiveVersion(version string) ToolHiveRegistryOption
- type UpstreamRegistryOption
Constants ¶
const ( // UpstreamRegistrySchemaURL is the JSON schema URL for upstream registry validation UpstreamRegistrySchemaURL = "https://raw.githubusercontent.com/stacklok/toolhive/main/" + "pkg/registry/data/upstream-registry.schema.json" // UpstreamRegistryVersion is the default upstream registry schema version UpstreamRegistryVersion = "1.0.0" )
Variables ¶
This section is empty.
Functions ¶
func EmptyToolHiveJSON ¶ added in v0.3.0
func EmptyToolHiveJSON() []byte
EmptyToolHiveJSON returns an empty ToolHive registry JSON object
func ExtractTags ¶ added in v0.3.0
func ExtractTags(server *upstream.ServerJSON) []string
ExtractTags extracts tags from an upstream server It uses the conventions of the Toolhive conversions function in github.com/stacklok/toolhive/pkg/registry/converters/toolhive_to_upstream.go
func InvalidJSON ¶ added in v0.3.0
func InvalidJSON() []byte
InvalidJSON returns intentionally malformed JSON for testing error cases
func NewTestServer ¶ added in v0.3.0
func NewTestServer(name string, opts ...ServerOption) upstreamv0.ServerJSON
NewTestServer creates a new ServerJSON for testing with default values and applies any provided options
func NewTestToolHiveRegistry ¶ added in v0.3.0
func NewTestToolHiveRegistry(opts ...ToolHiveRegistryOption) *toolhivetypes.Registry
NewTestToolHiveRegistry creates a new ToolHive Registry for testing with default values and applies any provided options
func NewTestUpstreamRegistry ¶ added in v0.3.0
func NewTestUpstreamRegistry(opts ...UpstreamRegistryOption) *toolhivetypes.UpstreamRegistry
NewTestUpstreamRegistry creates a new UpstreamRegistry for testing with default values and applies any provided options
func ToolHiveRegistryToJSON ¶ added in v0.3.0
func ToolHiveRegistryToJSON(reg *toolhivetypes.Registry) []byte
ToolHiveRegistryToJSON converts a ToolHive Registry to JSON bytes
func ToolHiveRegistryToPrettyJSON ¶ added in v0.3.0
func ToolHiveRegistryToPrettyJSON(reg *toolhivetypes.Registry) []byte
ToolHiveRegistryToPrettyJSON converts a ToolHive Registry to pretty-printed JSON bytes
Types ¶
type ImageServerOption ¶ added in v0.3.0
type ImageServerOption func(*toolhivetypes.ImageMetadata)
ImageServerOption is a function that configures an ImageMetadata (OCI server) for testing
func WithImageDescription ¶ added in v0.3.0
func WithImageDescription(description string) ImageServerOption
WithImageDescription sets the server description
func WithImageStatus ¶ added in v0.3.0
func WithImageStatus(status string) ImageServerOption
WithImageStatus sets the server status
func WithImageTags ¶ added in v0.3.0
func WithImageTags(tags ...string) ImageServerOption
WithImageTags sets the server tags
func WithImageTier ¶ added in v0.3.0
func WithImageTier(tier string) ImageServerOption
WithImageTier sets the server tier
func WithImageTools ¶ added in v0.3.0
func WithImageTools(tools ...string) ImageServerOption
WithImageTools sets the server tools
func WithImageTransport ¶ added in v0.3.0
func WithImageTransport(transport string) ImageServerOption
WithImageTransport sets the server transport
type RemoteServerOption ¶ added in v0.3.0
type RemoteServerOption func(*toolhivetypes.RemoteServerMetadata)
RemoteServerOption is a function that configures a RemoteServerMetadata for testing
func WithRemoteDescription ¶ added in v0.3.0
func WithRemoteDescription(description string) RemoteServerOption
WithRemoteDescription sets the remote server description
func WithRemoteStatus ¶ added in v0.3.0
func WithRemoteStatus(status string) RemoteServerOption
WithRemoteStatus sets the remote server status
func WithRemoteTags ¶ added in v0.3.0
func WithRemoteTags(tags ...string) RemoteServerOption
WithRemoteTags sets the remote server tags
func WithRemoteTier ¶ added in v0.3.0
func WithRemoteTier(tier string) RemoteServerOption
WithRemoteTier sets the remote server tier
func WithRemoteTools ¶ added in v0.3.0
func WithRemoteTools(tools ...string) RemoteServerOption
WithRemoteTools sets the remote server tools
func WithRemoteTransport ¶ added in v0.3.0
func WithRemoteTransport(transport string) RemoteServerOption
WithRemoteTransport sets the remote server transport
type ServerOption ¶ added in v0.3.0
type ServerOption func(*upstreamv0.ServerJSON)
ServerOption is a function that configures a ServerJSON for testing
func WithDescription ¶ added in v0.3.0
func WithDescription(description string) ServerOption
WithDescription sets the server description
func WithHTTPPackage ¶ added in v0.3.0
func WithHTTPPackage(url string) ServerOption
WithHTTPPackage adds an HTTP (remote) package to the server
func WithMetadata ¶ added in v0.3.0
func WithMetadata(key string, value any) ServerOption
WithMetadata adds arbitrary metadata to the server
func WithNamespace ¶ added in v0.3.0
func WithNamespace(namespace string) ServerOption
WithNamespace prepends a namespace to the server name (e.g., "io.test/")
func WithOCIPackage ¶ added in v0.3.0
func WithOCIPackage(image string) ServerOption
WithOCIPackage adds an OCI (container) package to the server
func WithServerVersion ¶ added in v0.3.0
func WithServerVersion(version string) ServerOption
WithServerVersion sets the server version
func WithTags ¶ added in v0.3.0
func WithTags(tags ...string) ServerOption
WithTags adds tags to the server's metadata Tags are stored in Meta.PublisherProvided["provider"]["metadata"]["tags"]
func WithToolHiveMetadata ¶ added in v0.3.0
func WithToolHiveMetadata(key string, value any) ServerOption
WithToolHiveMetadata adds ToolHive-specific metadata in the provider.toolhive structure This is a helper to set nested metadata fields commonly used by ToolHive
type ToolHiveRegistryOption ¶ added in v0.3.0
type ToolHiveRegistryOption func(*toolhivetypes.Registry)
ToolHiveRegistryOption is a function that configures a ToolHive Registry for testing
func WithImageServer ¶ added in v0.3.0
func WithImageServer(name, image string, opts ...ImageServerOption) ToolHiveRegistryOption
WithImageServer adds an OCI/container image server to the registry
func WithRemoteServerURL ¶ added in v0.3.0
func WithRemoteServerURL(name, url string, opts ...RemoteServerOption) ToolHiveRegistryOption
WithRemoteServerURL adds a remote (HTTP/SSE) server to the registry
func WithToolHiveLastUpdated ¶ added in v0.3.0
func WithToolHiveLastUpdated(timestamp string) ToolHiveRegistryOption
WithToolHiveLastUpdated sets the registry last updated timestamp
func WithToolHiveVersion ¶ added in v0.3.0
func WithToolHiveVersion(version string) ToolHiveRegistryOption
WithToolHiveVersion sets the registry version
type UpstreamRegistryOption ¶ added in v0.3.0
type UpstreamRegistryOption func(*toolhivetypes.UpstreamRegistry)
UpstreamRegistryOption is a function that configures an UpstreamRegistry for testing
func WithLastUpdated ¶ added in v0.3.0
func WithLastUpdated(timestamp string) UpstreamRegistryOption
WithLastUpdated sets the registry last updated timestamp
func WithServers ¶ added in v0.3.0
func WithServers(servers ...upstreamv0.ServerJSON) UpstreamRegistryOption
WithServers adds servers to the registry
func WithVersion ¶ added in v0.3.0
func WithVersion(version string) UpstreamRegistryOption
WithVersion sets the registry version