engine

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCancelled     = errors.New("operation cancelled")
	ErrInvalidUserID = errors.New("invalid user id")
	ErrInternal      = errors.New("internal error")
)

Functions

This section is empty.

Types

type Engine

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

Engine is the core of an MCP server, coordinating sessions, message routing, and protocol handling. It is protocol-agnostic and can be used with different transport layers (e.g., HTTP, stdio) by implementing the necessary session host and I/O handlers.

func (*Engine) DeleteSession

func (e *Engine) DeleteSession(ctx context.Context, sess *SessionHandle) error

DeleteSession validates ownership, returns the session protocol version for convenience, and deletes the session from the host. Idempotent at the host layer; returns ErrSessionNotFound if not owned or already gone.

func (*Engine) HandleClientResponse

func (e *Engine) HandleClientResponse(ctx context.Context, sess *SessionHandle, res *jsonrpc.Response) error

HandleClientResponse validates the session ownership and forwards a client JSON-RPC response into the Engine rendezvous via the inter-instance fanout. This allows any node to satisfy the waiting request, regardless of where the response was received.

func (*Engine) HandleNotification

func (e *Engine) HandleNotification(ctx context.Context, sess *SessionHandle, note *jsonrpc.Request) error

HandleNotification processes an incoming JSON-RPC notification from a client. It verifies the session and republishes the notification to all instances without actually handling the notification itself. The actual handling is done in the consumer loop for these messages.

func (*Engine) HandleRequest

func (e *Engine) HandleRequest(ctx context.Context, sess *SessionHandle, req *jsonrpc.Request) (*jsonrpc.Response, error)

func (*Engine) InitializeSession

func (e *Engine) InitializeSession(ctx context.Context, userID string, req *mcp.InitializeRequest) (*SessionHandle, *mcp.InitializeResult, error)

InitializeSession handles the MCP initialize handshake, creating a session record, wiring negotiated capabilities, and returning the InitializeResult payload alongside a session handle for subsequent requests.

func (*Engine) LoadSession added in v0.7.4

func (e *Engine) LoadSession(ctx context.Context, sessID, userID string, requestScopedWriter MessageWriter) (*SessionHandle, error)

LoadSession retrieves and validates session metadata, returning a handle. It verifies the session belongs to the specified user and is not revoked. It also performs a best-effort TTL touch. If a writers are provided, it sets up capabilities according to the session's negotiated capabilities and leverages the relevant writers for outbound messages.

func (*Engine) PublishToSession

func (e *Engine) PublishToSession(ctx context.Context, sessID, userID string, msg jsonrpc.Message) (string, error)

PublishToSession validates ownership and appends a JSON-RPC message to the per-session client-facing stream. Returns the assigned event ID.

func (*Engine) Run

func (e *Engine) Run(ctx context.Context) error

func (*Engine) StreamSession

func (e *Engine) StreamSession(ctx context.Context, sess *SessionHandle, lastEventID string, handler sessions.MessageHandlerFunction) error

StreamSession validates the session ownership and subscribes the caller to the per-session client-facing stream starting after lastEventID. It is a thin wrapper over the host that centralizes auth/ownership checks in the Engine.

type EngineOption

type EngineOption func(*Engine)

EngineOption configures a Engine.

func WithHandshakeTTL

func WithHandshakeTTL(d time.Duration) EngineOption

WithHandshakeTTL sets the TTL for a pending session awaiting the client's notifications/initialized message. Default is 30s.

func WithLogger

func WithLogger(l *slog.Logger) EngineOption

WithLogger sets a custom logger for the Engine.

func WithSessionMaxLifetime

func WithSessionMaxLifetime(d time.Duration) EngineOption

WithSessionMaxLifetime sets an absolute maximum lifetime horizon (0 = disabled).

func WithSessionTTL

func WithSessionTTL(d time.Duration) EngineOption

WithSessionTTL overrides the sliding TTL used for sessions.

type MessageWriter

type MessageWriter interface {
	WriteMessage(ctx context.Context, msg jsonrpc.Message) error
}

type MessageWriterFunc

type MessageWriterFunc func(ctx context.Context, msg jsonrpc.Message) error

func NewMessageWriterFunc

func NewMessageWriterFunc(f func(ctx context.Context, msg jsonrpc.Message) error) MessageWriterFunc

func (MessageWriterFunc) WriteMessage

func (f MessageWriterFunc) WriteMessage(ctx context.Context, msg jsonrpc.Message) error

type SessionHandle

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

func (*SessionHandle) DeleteData added in v0.7.2

func (s *SessionHandle) DeleteData(ctx context.Context, key string) error

DeleteData removes the key (idempotent).

func (*SessionHandle) GetData added in v0.7.2

func (s *SessionHandle) GetData(ctx context.Context, key string) ([]byte, bool, error)

GetData retrieves raw bytes for this session key. Returns ok=false if absent.

func (*SessionHandle) GetElicitationCapability

func (s *SessionHandle) GetElicitationCapability() (cap sessions.ElicitationCapability, ok bool)

func (*SessionHandle) GetRootsCapability

func (s *SessionHandle) GetRootsCapability() (cap sessions.RootsCapability, ok bool)

func (*SessionHandle) GetSamplingCapability

func (s *SessionHandle) GetSamplingCapability() (cap sessions.SamplingCapability, ok bool)

func (*SessionHandle) ProtocolVersion

func (s *SessionHandle) ProtocolVersion() string

func (*SessionHandle) PutData added in v0.7.2

func (s *SessionHandle) PutData(ctx context.Context, key string, value []byte) error

PutData stores raw bytes for this session key via the SessionHost.

func (*SessionHandle) SessionID

func (s *SessionHandle) SessionID() string

func (*SessionHandle) State

func (s *SessionHandle) State() sessions.SessionState

func (*SessionHandle) UserID

func (s *SessionHandle) UserID() string

type SessionHandleOption

type SessionHandleOption func(*SessionHandle)

Jump to

Keyboard shortcuts

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