resolvemcp

package
v1.0.68 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package resolvemcp exposes registered database models as Model Context Protocol (MCP) tools and resources over HTTP/SSE transport.

It mirrors the resolvespec package patterns:

  • Same model registration API
  • Same filter, sort, cursor pagination, preload options
  • Same lifecycle hook system

Usage:

handler := resolvemcp.NewHandlerWithGORM(db)
handler.RegisterModel("public", "users", &User{})

r := mux.NewRouter()
resolvemcp.SetupMuxRoutes(r, handler, "http://localhost:8080")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetEntity

func GetEntity(ctx context.Context) string

func GetModel

func GetModel(ctx context.Context) interface{}

func GetModelPtr

func GetModelPtr(ctx context.Context) interface{}

func GetSchema

func GetSchema(ctx context.Context) string

func GetTableName

func GetTableName(ctx context.Context) string

func NewSSEServer

func NewSSEServer(handler *Handler, baseURL, basePath string) *server.SSEServer

NewSSEServer creates an *server.SSEServer that can be mounted manually, useful when integrating with non-Mux routers or adding extra middleware.

sseServer := resolvemcp.NewSSEServer(handler, "http://localhost:8080", "/mcp")
http.Handle("/mcp/", http.StripPrefix("/mcp", sseServer))

func SetupMuxRoutes

func SetupMuxRoutes(muxRouter *mux.Router, handler *Handler, baseURL string)

SetupMuxRoutes mounts the MCP HTTP/SSE endpoints on the given Gorilla Mux router.

baseURL is the public-facing base URL of the server (e.g. "http://localhost:8080"). It is sent to MCP clients during the SSE handshake so they know where to POST messages.

Two routes are registered:

  • GET /mcp/sse — SSE connection endpoint (client subscribes here)
  • POST /mcp/message — JSON-RPC message endpoint (client sends requests here)

To protect these routes with authentication, wrap the mux router or apply middleware before calling SetupMuxRoutes.

func WithEntity

func WithEntity(ctx context.Context, entity string) context.Context

func WithModel

func WithModel(ctx context.Context, model interface{}) context.Context

func WithModelPtr

func WithModelPtr(ctx context.Context, modelPtr interface{}) context.Context

func WithSchema

func WithSchema(ctx context.Context, schema string) context.Context

func WithTableName

func WithTableName(ctx context.Context, tableName string) context.Context

Types

type Handler

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

Handler exposes registered database models as MCP tools and resources.

func NewHandler

func NewHandler(db common.Database, registry common.ModelRegistry) *Handler

NewHandler creates a Handler with the given database and model registry.

func NewHandlerWithBun

func NewHandlerWithBun(db *bun.DB) *Handler

NewHandlerWithBun creates a Handler backed by a Bun database connection.

func NewHandlerWithDB

func NewHandlerWithDB(db common.Database) *Handler

NewHandlerWithDB creates a Handler using an existing common.Database and a new registry.

func NewHandlerWithGORM

func NewHandlerWithGORM(db *gorm.DB) *Handler

NewHandlerWithGORM creates a Handler backed by a GORM database connection.

func (*Handler) GetDatabase

func (h *Handler) GetDatabase() common.Database

GetDatabase returns the underlying database.

func (*Handler) Hooks

func (h *Handler) Hooks() *HookRegistry

Hooks returns the hook registry.

func (*Handler) MCPServer

func (h *Handler) MCPServer() *server.MCPServer

MCPServer returns the underlying MCP server, e.g. to add custom tools.

func (*Handler) RegisterModel

func (h *Handler) RegisterModel(schema, entity string, model interface{}) error

RegisterModel registers a model and immediately exposes it as MCP tools and a resource.

type HookContext

type HookContext struct {
	Context      context.Context
	Handler      *Handler
	Schema       string
	Entity       string
	Model        interface{}
	Options      common.RequestOptions
	Operation    string
	ID           string
	Data         interface{}
	Result       interface{}
	Error        error
	Query        common.SelectQuery
	Abort        bool
	AbortMessage string
	AbortCode    int
	Tx           common.Database
}

HookContext contains all the data available to a hook

type HookFunc

type HookFunc func(*HookContext) error

HookFunc is the signature for hook functions

type HookRegistry

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

HookRegistry manages all registered hooks

func NewHookRegistry

func NewHookRegistry() *HookRegistry

func (*HookRegistry) Clear

func (r *HookRegistry) Clear(hookType HookType)

func (*HookRegistry) ClearAll

func (r *HookRegistry) ClearAll()

func (*HookRegistry) Execute

func (r *HookRegistry) Execute(hookType HookType, ctx *HookContext) error

func (*HookRegistry) HasHooks

func (r *HookRegistry) HasHooks(hookType HookType) bool

func (*HookRegistry) Register

func (r *HookRegistry) Register(hookType HookType, hook HookFunc)

func (*HookRegistry) RegisterMultiple

func (r *HookRegistry) RegisterMultiple(hookTypes []HookType, hook HookFunc)

type HookType

type HookType string

HookType defines the type of hook to execute

const (
	// BeforeHandle fires after model resolution, before operation dispatch.
	BeforeHandle HookType = "before_handle"

	BeforeRead HookType = "before_read"
	AfterRead  HookType = "after_read"

	BeforeCreate HookType = "before_create"
	AfterCreate  HookType = "after_create"

	BeforeUpdate HookType = "before_update"
	AfterUpdate  HookType = "after_update"

	BeforeDelete HookType = "before_delete"
	AfterDelete  HookType = "after_delete"
)

Jump to

Keyboard shortcuts

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