Session Classes API Reference
All session classes follow the same base pattern. They differ only in their mode-specific methods.
Common Pattern
Every session class:
- Constructor:
new XxxSession(client, options?) - Properties:
client,sessionId,modeVersion,configurationVersion,policyVersion,auth,projection - Lifecycle:
start(input)→ mode-specific methods →commit(input) - Control plane:
cancel(reason?, auth?),suspend(reason?, auth?),resume(reason?, auth?)— thin wrappers over the client'scancelSession/suspendSession/resumeSessionfor this session's id. Suspension is non-terminal: TTL is banked and restored onresume(). - Metadata:
metadata(auth?)queries session state from runtime - Streaming:
openStream(auth?)opens aMacpStreamwith the session's auth - Projection:
session.projectionprovides typed local state
Common Options
interface SessionOptions {
sessionId?: string; // default: auto-generated UUIDv4
modeVersion?: string; // default: '1.0.0'
configurationVersion?: string; // default: 'config.default'
policyVersion?: string; // default: 'policy.default'
auth?: AuthConfig; // default: uses client.auth
}Common start() Input
{
intent: string; // session purpose
participants: string[]; // participant identifiers
ttlMs: number; // time-to-live in milliseconds
maxSuspendMs?: number; // proto ≥ 0.1.5; max cumulative suspend (0 = runtime default)
contextId?: string; // optional shared-context reference
extensions?: Record<string, Buffer>; // optional extension payloads by key
roots?: { uri: string; name?: string }[]; // optional coordination roots
sender?: string; // optional sender override
}Common commit() Input
{
action: string; // outcome descriptor (e.g., 'deployment.approved')
authorityScope: string; // scope of authority
reason: string; // auditable reason
commitmentId?: string; // default: auto-generated UUID
outcomePositive?: boolean; // default: inferred from action (endsWith 'rejected'/'failed'/'declined' → false)
sender?: string;
auth?: AuthConfig;
}Session Classes Summary
| Class | Mode | Key Methods |
|---|---|---|
DecisionSession | macp.mode.decision.v1 | propose, evaluate, raiseObjection, vote |
ProposalSession | macp.mode.proposal.v1 | propose, counterPropose, accept, reject, withdraw |
TaskSession | macp.mode.task.v1 | request, acceptTask, rejectTask, update, complete, fail |
HandoffSession | macp.mode.handoff.v1 | offer, addContext, acceptHandoff, decline |
QuorumSession | macp.mode.quorum.v1 | requestApproval, approve, reject, abstain |
BaseSession (custom modes)
BaseSession<P extends BaseProjection> is the abstract base for building
session helpers for custom modes registered via registerExtMode. It provides
the shared lifecycle (start, commit, cancel, suspend, resume,
metadata, openStream) plus the identity-guarded protected helpers
senderFor(sender?, auth?) and sendAndTrack(envelope, auth?). Subclasses
supply the mode string and a createProjection() factory. The five built-in
session classes pre-date BaseSession and implement their own equivalents,
but expose the same surface.
import { BaseSession } from 'macp-sdk-typescript';
class CustomSession extends BaseSession<CustomProjection> {
protected readonly mode = 'ext.custom.v1';
protected createProjection() { return new CustomProjection(); }
}Per-Method Auth Override
All mode-specific methods accept optional sender and auth fields:
await session.vote({
proposalId: 'p1',
vote: 'approve',
sender: 'alice', // populate envelope.sender
auth: Auth.devAgent('alice'), // use alice's credentials for this call
});This enables a single process to act on behalf of multiple agents within the same session.
Identity Guard
Every mode-specific method that accepts a sender parameter runs the SDK's
identity guard before the envelope is built. When auth.expectedSender is
configured (via Auth.bearer(token, { expectedSender })), a caller-supplied
sender that disagrees raises MacpIdentityMismatchError — client-side, before
any RPC hits the wire (RFC-MACP-0004 (Security) §4).
import { Auth, DecisionSession, MacpIdentityMismatchError } from 'macp-sdk-typescript';
const session = new DecisionSession(client, {
auth: Auth.bearer('alice-token', { expectedSender: 'alice' }),
});
try {
await session.propose({ proposalId: 'p1', option: 'x', sender: 'mallory' });
} catch (err) {
if (err instanceof MacpIdentityMismatchError) {
err.expectedSender; // 'alice'
err.actualSender; // 'mallory'
}
}The guard is silent when expectedSender is undefined. Auth.devAgent(...) and
the legacy Auth.bearer(token, 'hint') form preserve pre-0.2 behaviour — the
SDK resolves sender from the hint but does not reject mismatched overrides.
Projection Integration
Each sendAndTrack() call:
- Builds an envelope with
buildEnvelope()+ProtoRegistry.encodeKnownPayload() - Sends via
MacpClient.send()(throwsMacpAckErroron nack) - On success (
ack.ok === true), applies the envelope to the projection
Rejected messages are never applied to the projection.
For full details on each session class, see the Mode documentation.