session

package
v1.0.511 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package session holds per-session protocol state for the MCP kernel: the bounded outstanding-request table, response correlation, cancellation with ownership/direction rules, completion, and expiration/cleanup.

The load-bearing invariant is direction isolation (MCP-PROTO-015 / #925): all correlation state is keyed by (session, direction, request-id), the same JSON-RPC id may be outstanding in BOTH directions at once, and no operation in one direction may resolve, cancel, complete, overwrite, release or delete the other direction's state. A second, closely-related guard (MCP-PROTO-013): an outstanding-request entry is released ONLY on trustworthy same-session correlation — never on an id lifted from a message the kernel just rejected — so a hostile peer cannot delete a legitimate in-flight request by naming its id.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CancelResult

type CancelResult int

CancelResult is the outcome of a cancellation.

const (
	// CancelApplied — a pending request owned by the canceller was released.
	CancelApplied CancelResult = iota + 1
	// CancelLate — the target was already resolved (completed or cancelled) —
	// tolerated no-op, no state changed.
	CancelLate
	// CancelNoTarget — no request with this id in this direction — tolerated no-op,
	// all state retained.
	CancelNoTarget
	// CancelRejectedOppositeDirection — the id is outstanding ONLY in the other
	// direction — rejected, the other direction's entry retained.
	CancelRejectedOppositeDirection
	// CancelRejectedWrongOwner — the id is pending in this direction but owned by a
	// different requestor — rejected, retained.
	CancelRejectedWrongOwner
	// CancelRejectedInitialize — the target is the initialize request, which must
	// not be cancelled — rejected, retained.
	CancelRejectedInitialize
)

func (CancelResult) Applied

func (r CancelResult) Applied() bool

Applied reports whether the cancellation released a request.

type Clock

type Clock func() time.Time

Clock returns the current time. It is injected so tests are deterministic and so the kernel never reads a wall clock implicitly.

type CorrelateResult

type CorrelateResult int

CorrelateResult is the outcome of correlating an inbound response.

const (
	// Completed — the response matched a pending request, which is now released.
	Completed CorrelateResult = iota + 1
	// ToleratedAfterCancel — the response arrived after the request was cancelled;
	// tolerated per the cancellation spec, not a fault.
	ToleratedAfterCancel
)

type Manager

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

Manager owns all live sessions and the per-capability outstanding-request budget. It bounds the session count and, INDEPENDENTLY per capability, the total outstanding requests across sessions, and drives expiration. Every mutating method is safe for concurrent use.

Lock discipline (deadlock-free by construction): the outstanding counters are atomic, so the request hot paths (RegisterRequest / CorrelateResponse / Cancel) hold only the session lock and never acquire the manager lock. The manager lock is acquired only by session-directory operations (Open / Close / Sweep / Get / counts), which may then take a session lock — a single, consistent lock order (manager → session), so no ABBA cycle is possible.

func NewManager

func NewManager(gatewayLim, mgmtLim limits.Limits, clk Clock) *Manager

NewManager builds a Manager with independent Gateway and Management limit sets and an injected clock. A nil clock defaults to time.Now.

func (*Manager) Close

func (m *Manager) Close(id string)

Close terminates a session and releases its outstanding budget back to the per-capability total. Idempotent.

func (*Manager) Get

func (m *Manager) Get(id string) (*Session, bool)

Get returns a live session by id.

func (*Manager) Open

func (m *Manager) Open(id string, capability protocol.Capability, role protocol.PeerRole) (*Session, error)

Open creates a new session bound to a capability and peer role. It fails if the id is already open or the Manager is at its (capability-specific) session cap.

func (*Manager) OutstandingFor

func (m *Manager) OutstandingFor(capability protocol.Capability) int

OutstandingFor returns the fleet-wide pending count for one capability.

func (*Manager) SessionCount

func (m *Manager) SessionCount() int

SessionCount returns the number of live sessions.

func (*Manager) Sweep

func (m *Manager) Sweep() int

Sweep expires sessions idle for longer than their SessionTTL, and — on sessions that are still active — expires individual outstanding requests older than the same TTL so a peer that keeps a session busy (e.g. pinging) while withholding responses cannot pin the outstanding budget indefinitely. It is a bounded scan over live sessions (no per-entry timer goroutine) and returns the number of SESSIONS reclaimed. Callers invoke it on a cadence; it uses the injected clock.

func (*Manager) TotalOutstanding

func (m *Manager) TotalOutstanding() int

TotalOutstanding returns the fleet-wide pending-request count across both capabilities.

type Session

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

Session is one MCP protocol session. All fields are guarded by mu; callers use the methods, never the fields.

func (*Session) Admit

func (s *Session) Admit(dir protocol.Direction, class jsonrpc.Class, method string) (protocol.Admission, error)

Admit gates a methodful message (request or notification) through the lifecycle rules (bootstrap + one-time handshake) and the reviewed method registry — including the wire-class check (a notification-only method must not carry an id and vice versa) — then advances the lifecycle state on an accepted handshake method. Responses are NOT admitted here — they are correlated (CorrelateResponse).

func (*Session) Cancel

func (s *Session) Cancel(dir protocol.Direction, owner string, id jsonrpc.ID) CancelResult

Cancel applies a cancellation naming a request id, owned by owner, in direction dir. It enforces same-direction + owning-requestor + initialize-exempt rules and NEVER deletes state it is not entitled to: an opposite-direction id, a wrong-owner id, and the initialize request are all left intact. A late cancellation (target already resolved) and an unknown id are tolerated no-ops.

func (*Session) Capability

func (s *Session) Capability() protocol.Capability

Capability returns the session's surface.

func (*Session) CorrelateResponse

func (s *Session) CorrelateResponse(dir protocol.Direction, id jsonrpc.ID) (CorrelateResult, error)

CorrelateResponse matches an inbound response to a pending request in the SAME direction and releases it. It NEVER touches the other direction, and it releases ONLY on an exact same-direction pending match — an uncorrelated response deletes nothing (the remote-state-deletion guard, MCP-PROTO-013). A second completion of an already-completed request is a duplicate-completion fault; a response after a cancellation is tolerated.

func (*Session) ID

func (s *Session) ID() string

ID returns the session id.

func (*Session) PendingCount

func (s *Session) PendingCount() int

PendingCount returns the number of outstanding requests across both directions.

func (*Session) RegisterRequest

func (s *Session) RegisterRequest(dir protocol.Direction, owner string, id jsonrpc.ID, method string) error

RegisterRequest records an in-flight request in the (direction) outstanding table so its response or cancellation can be correlated. It enforces per-requestor id-uniqueness (a duplicate id already outstanding in this direction is rejected) and the per-session + fleet outstanding bounds. It does NOT interact with the other direction.

func (*Session) Role

func (s *Session) Role() protocol.PeerRole

Role returns the session's peer role (leg).

func (*Session) SetNegotiatedVersion

func (s *Session) SetNegotiatedVersion(v protocol.Version) error

SetNegotiatedVersion records the version chosen at initialize. It fails if a version is already negotiated (negotiation happens once) or the version is not supported (default deny — MCP-PROTO-010).

func (*Session) State

func (s *Session) State() protocol.State

State returns the current lifecycle state.

func (*Session) Version

func (s *Session) Version() (protocol.Version, bool)

Version returns the negotiated version and whether one has been negotiated.

Jump to

Keyboard shortcuts

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