kc

package
v0.2.0-dev3 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Default session configuration
	DefaultSessionDuration = 12 * time.Hour
	DefaultCleanupInterval = 30 * time.Minute
)
View Source
const (
	// Default expiry for signed session parameters (30 minutes)
	DefaultSignatureExpiry = 30 * time.Minute

	// Maximum allowed clock skew for signature validation
	MaxClockSkew = 5 * time.Minute
)

Variables

View Source
var (
	ErrSessionNotFound  = errors.New("MCP session not found or Kite session not associated, try to login again")
	ErrInvalidSessionID = errors.New("invalid MCP session ID, please try logging in again")
)
View Source
var (
	ErrInvalidSignature = errors.New("invalid session signature")
	ErrTamperedSession  = errors.New("session parameter has been tampered with")
	ErrExpiredSignature = errors.New("session signature has expired")
	ErrInvalidFormat    = errors.New("invalid session parameter format")
)

Functions

This section is empty.

Types

type CleanupHook

type CleanupHook func(session *MCPSession)

CleanupHook is called when a session is terminated or expires

type Config

type Config struct {
	APIKey             string                    // required
	APISecret          string                    // required
	Logger             *slog.Logger              // required
	InstrumentsConfig  *instruments.UpdateConfig // optional - defaults to instruments.DefaultUpdateConfig()
	InstrumentsManager *instruments.Manager      // optional - if provided, skips creating new instruments manager
	SessionSigner      *SessionSigner            // optional - if nil, creates new session signer
}

Config holds configuration for creating a new kc Manager

type KiteConnect

type KiteConnect struct {
	// Add fields here
	Client *kiteconnect.Client // TODO: this can be made private ?
}

KiteConnect wraps the Kite Connect client

func NewKiteConnect

func NewKiteConnect(apiKey string) *KiteConnect

NewKiteConnect creates a new KiteConnect instance

type KiteSessionData

type KiteSessionData struct {
	Kite *KiteConnect
}

type MCPSession

type MCPSession struct {
	ID         string
	Terminated bool
	CreatedAt  time.Time
	ExpiresAt  time.Time
	Data       any // Contains KiteSessionData
}

type Manager

type Manager struct {
	Logger *slog.Logger

	Instruments *instruments.Manager
	// contains filtered or unexported fields
}

func New

func New(cfg Config) (*Manager, error)

New creates a new kc Manager with the given configuration

func NewManager

func NewManager(apiKey, apiSecret string, logger *slog.Logger) (*Manager, error)

NewManager creates a new manager with default configuration Deprecated: Use New(Config{APIKey: apiKey, APISecret: apiSecret, Logger: logger}) instead

func (*Manager) CleanupExpiredSessions

func (m *Manager) CleanupExpiredSessions() int

CleanupExpiredSessions manually triggers cleanup of expired MCP sessions

func (*Manager) ClearSession

func (m *Manager) ClearSession(sessionID string)

func (*Manager) ClearSessionData

func (m *Manager) ClearSessionData(sessionID string) error

ClearSessionData clears the session data without terminating the session

func (*Manager) CompleteSession

func (m *Manager) CompleteSession(mcpSessionID, kiteRequestToken string) error

func (*Manager) ForceInstrumentsUpdate

func (m *Manager) ForceInstrumentsUpdate() error

ForceInstrumentsUpdate forces an immediate instruments update

func (*Manager) GenerateSession

func (m *Manager) GenerateSession() string

GenerateSession creates a new MCP session with Kite data and returns the session ID

func (*Manager) GetActiveSessionCount

func (m *Manager) GetActiveSessionCount() int

GetActiveSessionCount returns the number of active sessions

func (*Manager) GetInstrumentsStats

func (m *Manager) GetInstrumentsStats() instruments.UpdateStats

GetInstrumentsStats returns current instruments update statistics

func (*Manager) GetOrCreateSession

func (m *Manager) GetOrCreateSession(mcpSessionID string) (*KiteSessionData, bool, error)

GetOrCreateSession retrieves an existing Kite session or creates a new one atomically

func (*Manager) GetSession

func (m *Manager) GetSession(mcpSessionID string) (*KiteSessionData, error)

func (*Manager) HandleKiteCallback

func (m *Manager) HandleKiteCallback() func(w http.ResponseWriter, r *http.Request)

HandleKiteCallback returns an HTTP handler for Kite authentication callbacks

func (*Manager) SessionLoginURL

func (m *Manager) SessionLoginURL(mcpSessionID string) (string, error)

func (*Manager) SessionManager

func (m *Manager) SessionManager() *SessionRegistry

SessionManager returns the MCP session manager instance

func (*Manager) SessionSigner

func (m *Manager) SessionSigner() *SessionSigner

SessionSigner returns the session signer instance

func (*Manager) Shutdown

func (m *Manager) Shutdown()

Shutdown gracefully shuts down the manager and all its components

func (*Manager) StopCleanupRoutine

func (m *Manager) StopCleanupRoutine()

StopCleanupRoutine stops the background cleanup routine

func (*Manager) UpdateInstrumentsConfig

func (m *Manager) UpdateInstrumentsConfig(config *instruments.UpdateConfig)

UpdateInstrumentsConfig updates the instruments manager configuration

func (*Manager) UpdateSessionSignerExpiry

func (m *Manager) UpdateSessionSignerExpiry(duration time.Duration)

UpdateSessionSignerExpiry updates the signature expiry duration

type SessionRegistry

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

func NewSessionRegistry

func NewSessionRegistry(logger *slog.Logger) *SessionRegistry

NewSessionRegistry creates a new registry that manages MCP sessions and their associated Kite data

func NewSessionRegistryWithDuration

func NewSessionRegistryWithDuration(duration time.Duration, logger *slog.Logger) *SessionRegistry

NewSessionRegistryWithDuration creates a new session registry with custom duration

func (*SessionRegistry) AddCleanupHook

func (sm *SessionRegistry) AddCleanupHook(hook CleanupHook)

AddCleanupHook adds a cleanup function for the Kite session to be called when MCP sessions are terminated

func (*SessionRegistry) CleanupExpiredSessions

func (sm *SessionRegistry) CleanupExpiredSessions() int

CleanupExpiredSessions removes expired MCP sessions from memory and their associated Kite data

func (*SessionRegistry) Generate

func (sm *SessionRegistry) Generate() string

Generate creates a new MCP session ID and stores it in memory

func (*SessionRegistry) GenerateWithData

func (sm *SessionRegistry) GenerateWithData(data any) string

GenerateWithData creates a new MCP session ID with associated Kite data and stores it in memory

func (*SessionRegistry) GetOrCreateSessionData

func (sm *SessionRegistry) GetOrCreateSessionData(sessionID string, createDataFn func() any) (data any, isNew bool, err error)

GetOrCreateSessionData atomically validates session and retrieves/creates data to eliminate TOCTOU races

func (*SessionRegistry) GetSession

func (sm *SessionRegistry) GetSession(sessionID string) (*MCPSession, error)

GetSession retrieves a MCP session by ID (helper method)

func (*SessionRegistry) GetSessionData

func (sm *SessionRegistry) GetSessionData(sessionID string) (any, error)

GetSessionData retrieves the Kite data for a MCP session

func (*SessionRegistry) GetSessionDuration

func (sm *SessionRegistry) GetSessionDuration() time.Duration

GetSessionDuration returns the configured MCP session duration

func (*SessionRegistry) ListActiveSessions

func (sm *SessionRegistry) ListActiveSessions() []*MCPSession

ListActiveSessions returns all non-terminated MCP sessions (helper method)

func (*SessionRegistry) SetSessionDuration

func (sm *SessionRegistry) SetSessionDuration(duration time.Duration)

SetSessionDuration updates the MCP session duration for new sessions

func (*SessionRegistry) StartCleanupRoutine

func (sm *SessionRegistry) StartCleanupRoutine(ctx context.Context)

StartCleanupRoutine starts background cleanup goroutines for expired MCP sessions

func (*SessionRegistry) StopCleanupRoutine

func (sm *SessionRegistry) StopCleanupRoutine()

StopCleanupRoutine stops background cleanup goroutines for MCP sessions

func (*SessionRegistry) Terminate

func (sm *SessionRegistry) Terminate(sessionID string) (isNotAllowed bool, err error)

Terminate marks a MCP session ID as terminated and cleans up associated Kite session

func (*SessionRegistry) UpdateSessionData

func (sm *SessionRegistry) UpdateSessionData(sessionID string, data any) error

UpdateSessionData updates the Kite data for an existing MCP session

func (*SessionRegistry) Validate

func (sm *SessionRegistry) Validate(sessionID string) (isTerminated bool, err error)

Validate checks if a MCP session ID is valid and not terminated

type SessionSigner

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

SessionSigner handles HMAC signing and verification of session parameters

func NewSessionSigner

func NewSessionSigner() (*SessionSigner, error)

NewSessionSigner creates a new session signer with a random secret key

func NewSessionSignerWithKey

func NewSessionSignerWithKey(secretKey []byte) *SessionSigner

NewSessionSignerWithKey creates a new session signer with a provided secret key

func (*SessionSigner) GetSecretKey

func (s *SessionSigner) GetSecretKey() []byte

GetSecretKey returns the secret key (for testing purposes only)

func (*SessionSigner) SetSignatureExpiry

func (s *SessionSigner) SetSignatureExpiry(duration time.Duration)

SetSignatureExpiry sets the expiry duration for signed session parameters

func (*SessionSigner) SignRedirectParams

func (s *SessionSigner) SignRedirectParams(sessionID string) (string, error)

SignRedirectParams creates a signed redirect parameter string for Kite authentication

func (*SessionSigner) SignSessionID

func (s *SessionSigner) SignSessionID(sessionID string) string

SignSessionID creates a signed session parameter with timestamp and HMAC signature

func (*SessionSigner) ValidateSessionID

func (s *SessionSigner) ValidateSessionID(sessionID string) error

ValidateSessionID performs basic validation on a session ID format

func (*SessionSigner) VerifyRedirectParams

func (s *SessionSigner) VerifyRedirectParams(redirectParams string) (string, error)

VerifyRedirectParams verifies signed redirect parameters and extracts the session ID

func (*SessionSigner) VerifySessionID

func (s *SessionSigner) VerifySessionID(signedParam string) (string, error)

VerifySessionID verifies a signed session parameter and extracts the session ID

type TemplateData

type TemplateData struct {
	Title string
}

TemplateData holds data for template rendering

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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