kit

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 5 Imported by: 0

README

kit — transport-agnostic endpoint primitives

kit defines the core Endpoint type and context helpers shared by all HOROS services. A single endpoint serves both HTTP handlers and MCP tools through the same middleware chain.

Quick start

type Endpoint func(ctx context.Context, request any) (response any, err error)
type Middleware func(Endpoint) Endpoint

endpoint := kit.Chain(
    audit.Middleware(logger, "action"),
    auth.RequireAuth,
)(baseEndpoint)

Context helpers

ctx = kit.WithUserID(ctx, "user123")
ctx = kit.WithRequestID(ctx, idgen.New())
ctx = kit.WithTransport(ctx, "mcp_quic")
ctx = kit.WithTraceID(ctx, traceID)

uid := kit.GetUserID(ctx)

MCP bridge

RegisterMCPTool exposes a kit Endpoint as an MCP tool:

kit.RegisterMCPTool(mcpServer, tool, endpoint, func(req *mcp.CallToolRequest) (*kit.MCPDecodeResult, error) {
    var input MyInput
    json.Unmarshal(req.Params.Arguments, &input)
    return &kit.MCPDecodeResult{Request: input}, nil
})

Exported API

Symbol Description
Endpoint func(ctx, request any) (response any, err error)
Middleware func(Endpoint) Endpoint
Chain(outer, others...) Compose middlewares (outer is outermost)
WithUserID / GetUserID User identity in context
WithHandle / GetHandle Username in context
WithTransport / GetTransport "http" or "mcp_quic"
WithRequestID / GetRequestID Request correlation ID
WithTraceID / GetTraceID Cross-service trace ID
RegisterMCPTool Bridge Endpoint to MCP tool
MCPDecodeResult Decoded request + context enrichment

Documentation

Index

Constants

View Source
const (
	UserIDKey     contextKey = "kit_user_id"
	HandleKey     contextKey = "kit_handle"
	TransportKey  contextKey = "kit_transport" // "http", "mcp_quic"
	RequestIDKey  contextKey = "kit_request_id"
	TraceIDKey    contextKey = "kit_trace_id"
	SessionIDKey  contextKey = "kit_session_id"
	RemoteAddrKey contextKey = "kit_remote_addr"
	RoleKey       contextKey = "kit_role"
)

Variables

This section is empty.

Functions

func GetHandle

func GetHandle(ctx context.Context) string

func GetRemoteAddr

func GetRemoteAddr(ctx context.Context) string

func GetRequestID

func GetRequestID(ctx context.Context) string

func GetRole

func GetRole(ctx context.Context) string

func GetSessionID

func GetSessionID(ctx context.Context) string

func GetTraceID

func GetTraceID(ctx context.Context) string

func GetTransport

func GetTransport(ctx context.Context) string

func GetUserID

func GetUserID(ctx context.Context) string

func RegisterMCPTool

func RegisterMCPTool(srv *mcp.Server, tool *mcp.Tool, endpoint Endpoint, decode func(*mcp.CallToolRequest) (*MCPDecodeResult, error))

RegisterMCPTool registers an Endpoint as an MCP tool on the given server. The decode function extracts the typed request from MCP arguments.

Migration note (feb 2026): signature changed from mcp-go to official SDK. - srv: *server.MCPServer → *mcp.Server - tool: mcp.Tool (value) → *mcp.Tool (pointer) - decode param: mcp.CallToolRequest (value) → *mcp.CallToolRequest (pointer) - Arguments are now in req.Params.Arguments as json.RawMessage, not map[string]any. Callers must update their decode functions accordingly.

func WithHandle

func WithHandle(ctx context.Context, h string) context.Context

func WithRemoteAddr

func WithRemoteAddr(ctx context.Context, addr string) context.Context

func WithRequestID

func WithRequestID(ctx context.Context, id string) context.Context

func WithRole

func WithRole(ctx context.Context, role string) context.Context

func WithSessionID

func WithSessionID(ctx context.Context, id string) context.Context

func WithTraceID

func WithTraceID(ctx context.Context, id string) context.Context

func WithTransport

func WithTransport(ctx context.Context, t string) context.Context

func WithUserID

func WithUserID(ctx context.Context, id string) context.Context

Types

type Endpoint

type Endpoint func(ctx context.Context, request any) (response any, err error)

Endpoint is a transport-agnostic action function. Each action (create node, vote, search) is an Endpoint. HTTP handlers and MCP tools both dispatch to the same Endpoints.

type MCPDecodeResult

type MCPDecodeResult struct {
	Request   any
	EnrichCtx func(context.Context) context.Context
}

MCPDecodeResult holds the decoded request and an optional context enrichment.

type Middleware

type Middleware func(Endpoint) Endpoint

Middleware wraps an Endpoint with cross-cutting concerns (audit, auth, tracing).

func Chain

func Chain(outer Middleware, others ...Middleware) Middleware

Chain composes middlewares so the first is outermost. Chain(a, b, c)(endpoint) == a(b(c(endpoint)))

Jump to

Keyboard shortcuts

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