hypermcp

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2025 License: MIT Imports: 6 Imported by: 0

README

hypermcp - Reusable MCP Server Infrastructure

hypermcp is a reusable package that provides common infrastructure for building Model Context Protocol (MCP) servers. It handles all the boilerplate so you can focus on implementing your custom tools and resources.

Features

  • ✅ MCP server setup and lifecycle management
  • ✅ HTTP client with logging
  • ✅ Caching layer (with optional disable)
  • ✅ Transport abstraction (stdio, future: Streamable HTTP)
  • ✅ Structured logging with zap
  • ✅ Helper methods for registering tools and resources
  • ✅ Automatic stats tracking

Quick Start

1. Create a New MCP Server
package main

import (
    "context"
    "github.com/rayprogramming/hypermcp"
    "github.com/rayprogramming/hypermcp/cache"
    "go.uber.org/zap"
)

func main() {
    // Setup logger
    logger, _ := zap.NewProduction()
    defer logger.Sync()

    // Configure server
    cfg := hypermcp.Config{
        Name:         "my-mcp-server",
        Version:      "1.0.0",
        CacheEnabled: true,
        CacheConfig: cache.Config{
            MaxCost:     100 * 1024 * 1024, // 100MB
            NumCounters: 10_000,
            BufferItems: 64,
        },
    }

    // Create base server
    srv, err := hypermcp.New(cfg, logger)
    if err != nil {
        logger.Fatal("failed to create server", zap.Error(err))
    }

    // Register your tools and resources
    registerFeatures(srv)

    // Log registration stats
    srv.LogRegistrationStats()

    // Run with stdio transport
    ctx := context.Background()
    if err := hypermcp.RunWithTransport(ctx, srv, hypermcp.TransportStdio, logger); err != nil {
        logger.Fatal("server failed", zap.Error(err))
    }
}
2. Implement Your Providers

Create providers that use the shared infrastructure:

package providers

import (
    "context"
    "github.com/modelcontextprotocol/go-sdk/mcp"
    "github.com/rayprogramming/hypermcp"
    "github.com/rayprogramming/hypermcp/cache"
    "github.com/rayprogramming/hypermcp/httpx"
    "go.uber.org/zap"
)

type MyProvider struct {
    httpClient *httpx.Client
    cache      *cache.Cache
    logger     *zap.Logger
}

func NewMyProvider(srv *hypermcp.Server) *MyProvider {
    return &MyProvider{
        httpClient: srv.HTTPClient(),
        cache:      srv.Cache(),
        logger:     srv.Logger(),
    }
}

func (p *MyProvider) MyTool(
    ctx context.Context,
    req *mcp.CallToolRequest,
    input MyToolInput,
) (*mcp.CallToolResult, MyToolOutput, error) {
    // Your implementation here
    // Use p.httpClient, p.cache, p.logger as needed
}
3. Register Your Features
func registerFeatures(srv *hypermcp.Server) {
    // Create providers
    myProvider := providers.NewMyProvider(srv)

    // Register tools
    srv.AddTool(
        &mcp.Tool{
            Name:        "my_tool",
            Description: "Does something cool",
        },
        myProvider.MyTool,
    )

    // Register resources
    srv.AddResource(
        &mcp.Resource{
            URI:         "myresource://data",
            Name:        "My Resource",
            Description: "Provides some data",
            MIMEType:    "application/json",
        },
        func(ctx context.Context, req *mcp.ReadResourceRequest) (*mcp.ReadResourceResult, error) {
            // Your resource implementation
        },
    )
}

API Reference

Server Creation
func New(cfg Config, logger *zap.Logger) (*Server, error)

Creates a new MCP server with common infrastructure.

Configuration
type Config struct {
    Name         string        // Server name
    Version      string        // Server version
    CacheEnabled bool          // Enable caching
    CacheConfig  cache.Config  // Cache configuration
}
Server Methods
  • HTTPClient() *httpx.Client - Get the shared HTTP client
  • Cache() *cache.Cache - Get the cache instance
  • Logger() *zap.Logger - Get the logger
  • MCP() *mcp.Server - Get the underlying MCP server
  • AddTool(tool, handler) - Register a tool
  • AddResource(resource, handler) - Register a resource
  • AddResourceTemplate(template, handler) - Register a resource template
  • LogRegistrationStats() - Log tool/resource counts
  • Run(ctx, transport) - Start the server
  • Shutdown(ctx) - Gracefully shutdown
Transport
func RunWithTransport(ctx context.Context, srv *Server, transportType TransportType, logger *zap.Logger) error

Starts the server with the specified transport.

Available transports:

  • TransportStdio - Standard input/output (recommended for most use cases)
  • TransportStreamableHTTP - Streamable HTTP (for servers handling multiple client connections, not yet implemented)

Transport Types

hypermcp supports the MCP specification's recommended transports:

  • Default choice for most MCP servers
  • Client launches server as subprocess
  • Communication over stdin/stdout
  • Simpler setup and deployment
  • Clients SHOULD support stdio whenever possible (per MCP spec)
Streamable HTTP Transport
  • For servers handling multiple concurrent clients
  • HTTP-based with optional Server-Sent Events
  • Replaces the deprecated HTTP+SSE transport
  • More complex but supports advanced scenarios
  • Note: Not yet implemented in this package

Benefits

For You
  • 🚀 Fast Setup: Get a server running in minutes, not hours
  • 🔧 Focus on Features: Spend time on tools, not boilerplate
  • 📦 Batteries Included: HTTP client, caching, logging all configured
  • 🎯 Best Practices: Follows MCP patterns and Go idioms
For Your Users
  • Performance: Built-in caching and connection pooling
  • 📊 Observability: Structured logging with zap
  • 🛡️ Reliability: Proper error handling and graceful shutdown

Examples

See the hypermcp implementation in this repository for a complete example with:

  • WIP

Dependencies

  • github.com/modelcontextprotocol/go-sdk - MCP SDK
  • go.uber.org/zap - Structured logging
  • github.com/dgraph-io/ristretto - Caching (via pkg/cache)

Documentation

Overview

Package hypermcp provides reusable MCP server infrastructure

Package hypermcp provides reusable MCP server infrastructure

Package hypermcp provides reusable MCP server infrastructure

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunWithTransport

func RunWithTransport(ctx context.Context, srv *Server, transportType TransportType, logger *zap.Logger) error

RunWithTransport starts the server with the specified transport type

For most use cases, use TransportStdio (the default and recommended transport). TransportStreamableHTTP is for advanced scenarios requiring multiple concurrent clients.

Types

type Config

type Config struct {
	Name         string
	Version      string
	CacheEnabled bool
	CacheConfig  cache.Config
}

Config holds server configuration

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server wraps the MCP server with common infrastructure

func New

func New(cfg Config, logger *zap.Logger) (*Server, error)

New creates a new MCP server with common infrastructure

func (*Server) Cache

func (s *Server) Cache() *cache.Cache

Cache returns the cache instance

func (*Server) HTTPClient

func (s *Server) HTTPClient() *httpx.Client

HTTPClient returns the shared HTTP client

func (*Server) IncrementResourceCount

func (s *Server) IncrementResourceCount()

IncrementResourceCount increments the resource counter (call after AddResource/AddResourceTemplate)

func (*Server) IncrementToolCount

func (s *Server) IncrementToolCount()

IncrementToolCount increments the tool counter (call after AddTool)

func (*Server) LogRegistrationStats

func (s *Server) LogRegistrationStats()

LogRegistrationStats logs the number of registered tools and resources

func (*Server) Logger

func (s *Server) Logger() *zap.Logger

Logger returns the logger instance

func (*Server) MCP

func (s *Server) MCP() *mcp.Server

MCP returns the underlying MCP server for direct access if needed

func (*Server) Run

func (s *Server) Run(ctx context.Context, transport mcp.Transport) error

Run starts the server with the given transport

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown performs cleanup

type ServerInfo

type ServerInfo struct {
	Name      string
	Version   string
	Commit    string
	BuildDate string
}

ServerInfo holds version and build information for the MCP server

func (ServerInfo) String

func (si ServerInfo) String() string

String returns a formatted version string

type TransportType

type TransportType string

TransportType defines the type of transport to use

const (
	// TransportStdio uses standard input/output (recommended for most use cases)
	TransportStdio TransportType = "stdio"
	// TransportStreamableHTTP uses Streamable HTTP (for servers handling multiple client connections)
	TransportStreamableHTTP TransportType = "streamable-http"
)

Directories

Path Synopsis
Package httpx provides a shared HTTP client with retry logic, timeouts, and helpers
Package httpx provides a shared HTTP client with retry logic, timeouts, and helpers

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL