Documentation
¶
Overview ¶
Package hypermcp provides reusable MCP server infrastructure.
This package simplifies building Model Context Protocol (MCP) servers by providing common infrastructure components including HTTP client with retry logic, caching, structured logging, and transport abstraction.
Example usage:
cfg := hypermcp.Config{
Name: "my-server",
Version: "1.0.0",
CacheEnabled: true,
}
srv, err := hypermcp.New(cfg, logger)
if err != nil {
log.Fatal(err)
}
hypermcp.AddTool(srv, tool, handler)
srv.AddResource(resource, handler)
hypermcp.RunWithTransport(ctx, srv, hypermcp.TransportStdio, logger)
Package hypermcp provides reusable MCP server infrastructure ¶
Package hypermcp provides reusable MCP server infrastructure
Index ¶
- func AddTool[In, Out any](s *Server, tool *mcp.Tool, handler mcp.ToolHandlerFor[In, Out])
- func RunWithTransport(ctx context.Context, srv *Server, transportType TransportType, ...) error
- type Config
- type Server
- func (s *Server) AddResource(resource *mcp.Resource, handler mcp.ResourceHandler)
- func (s *Server) AddResourceTemplate(template *mcp.ResourceTemplate, handler mcp.ResourceHandler)
- func (s *Server) Cache() *cache.Cache
- func (s *Server) HTTPClient() *httpx.Client
- func (s *Server) IncrementResourceCount()
- func (s *Server) IncrementToolCount()
- func (s *Server) LogRegistrationStats()
- func (s *Server) Logger() *zap.Logger
- func (s *Server) MCP() *mcp.Server
- func (s *Server) Run(ctx context.Context, transport mcp.Transport) error
- func (s *Server) Shutdown(ctx context.Context) error
- type ServerInfo
- type TransportType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddTool ¶ added in v0.2.0
AddTool registers a tool with the MCP server and automatically increments the tool counter.
This is a generic function that provides type-safe tool registration. The input and output types are inferred from the handler function signature. If the tool's input or output schema is nil, it will be automatically generated from the type parameters.
Example:
type Input struct {
Message string `json:"message"`
}
type Output struct {
Result string `json:"result"`
}
hypermcp.AddTool(srv, &mcp.Tool{Name: "echo"}, func(ctx context.Context, req *mcp.CallToolRequest, input Input) (*mcp.CallToolResult, Output, error) {
return nil, Output{Result: input.Message}, nil
})
func RunWithTransport ¶
func RunWithTransport(ctx context.Context, srv *Server, transportType TransportType, logger *zap.Logger) error
RunWithTransport starts the MCP server with the specified transport.
The function logs the selected transport and blocks until the context is canceled or an error occurs. Currently only stdio transport is implemented.
Types ¶
type Config ¶
Config holds server configuration.
Name and Version are required fields and will be validated. CacheEnabled determines whether to initialize a full cache instance.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server wraps the MCP server with common infrastructure.
It provides access to shared resources like HTTP client, cache, and logger, along with helper methods for registering tools and resources with automatic counter tracking.
func New ¶
New creates a new MCP server with common infrastructure.
It initializes the HTTP client with retry logic, creates a cache instance (if enabled), and sets up the underlying MCP server. The configuration is validated before creating the server.
Returns an error if the configuration is invalid or if cache creation fails.
func (*Server) AddResource ¶ added in v0.2.0
func (s *Server) AddResource(resource *mcp.Resource, handler mcp.ResourceHandler)
AddResource registers a resource with the MCP server and automatically increments the resource counter.
Resources provide static or dynamic content that can be read by MCP clients.
Example:
srv.AddResource(&mcp.Resource{
URI: "myapp://data",
Name: "Application Data",
}, func(ctx context.Context, req *mcp.ReadResourceRequest) (*mcp.ReadResourceResult, error) {
return &mcp.ReadResourceResult{...}, nil
})
func (*Server) AddResourceTemplate ¶ added in v0.2.0
func (s *Server) AddResourceTemplate(template *mcp.ResourceTemplate, handler mcp.ResourceHandler)
AddResourceTemplate registers a resource template with the MCP server and automatically increments the resource counter.
Resource templates allow parameterized URIs using URI template syntax (RFC 6570).
Example:
srv.AddResourceTemplate(&mcp.ResourceTemplate{
URITemplate: "myapp://users/{userId}",
Name: "User Data",
}, func(ctx context.Context, req *mcp.ReadResourceRequest) (*mcp.ReadResourceResult, error) {
userId := req.Params.URI // Extract from actual request
return &mcp.ReadResourceResult{...}, nil
})
func (*Server) Cache ¶
Cache returns the cache instance.
Even when CacheEnabled is false, a minimal cache instance is returned.
func (*Server) HTTPClient ¶
HTTPClient returns the shared HTTP client.
The client includes retry logic, proper timeouts, and is safe for concurrent use.
func (*Server) IncrementResourceCount ¶
func (s *Server) IncrementResourceCount()
IncrementResourceCount increments the resource counter.
This is called automatically by AddResource and AddResourceTemplate, so you typically don't need to call it manually.
func (*Server) IncrementToolCount ¶
func (s *Server) IncrementToolCount()
IncrementToolCount increments the tool counter.
This is called automatically by AddTool, so you typically don't need to call it manually.
func (*Server) LogRegistrationStats ¶
func (s *Server) LogRegistrationStats()
LogRegistrationStats logs the number of registered tools and resources.
This is useful for debugging and verifying that all expected features were registered.
func (*Server) Logger ¶
Logger returns the logger instance.
This is the same logger passed to New() during server creation.
func (*Server) MCP ¶
MCP returns the underlying MCP server for direct access if needed.
Most users should prefer using the helper methods (AddTool, AddResource, etc.) rather than accessing the MCP server directly.
func (*Server) Run ¶
Run starts the server with the given transport.
This method blocks until the context is canceled or an error occurs. Most users should use RunWithTransport instead of calling this directly.
func (*Server) Shutdown ¶
Shutdown performs cleanup and gracefully shuts down the server.
This method closes the cache and logs final statistics. It checks if the provided context was canceled during the cleanup process and returns an error if so.
Example:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Printf("shutdown error: %v", err)
}
type ServerInfo ¶
ServerInfo holds version and build information for the MCP server.
This struct can be populated at build time using ldflags:
go build -ldflags="-X main.version=1.0.0 -X main.commit=abc123 -X main.buildDate=2025-01-15"
func (ServerInfo) String ¶
func (si ServerInfo) String() string
String returns a formatted version string with commit and build date information.
Format: "version (commit: hash, built: date)"
type TransportType ¶
type TransportType string
TransportType defines the type of transport to use.
The MCP specification defines two standard transports: stdio and Streamable HTTP.
const ( // TransportStdio uses standard input/output for communication. // This is the recommended transport for most MCP servers where the client // launches the server as a subprocess. TransportStdio TransportType = "stdio" // TransportStreamableHTTP uses HTTP-based transport for multiple client connections. // This replaces the deprecated HTTP+SSE transport and is suitable for servers // that need to handle multiple concurrent clients. // Note: Not yet implemented in this library. TransportStreamableHTTP TransportType = "streamable-http" )