Module 36: Terminal Auth and Logout
An agent that requires sign-in before any session can open, offering two auth methods (one the agent handles itself, one an interactive terminal login the client runs as a separate process) plus a logout capability.
What You’ll Learn
- Declaring
authMethods on @AcpAgent, with no @Initialize method needed
AuthMethod as an open union: AuthMethodAgent versus AuthMethodTerminal
- Why a terminal method reaches only a client that advertised
auth.terminal, with no hand-written check
@Authenticate, and why the client never calls it for a terminal method
- Rejecting
session/new with AUTHENTICATION_REQUIRED (-32000) before sign-in
@Logout, derived agentCapabilities.auth.logout, and checking supportsLogout() first
The Code
Declaring auth methods on @AcpAgent, with no @Initialize method
There’s no @Initialize method: the SDK derives everything a client needs from the class.
@AcpAgent(authMethods = @AuthMethod(...)) becomes authMethods; @Logout (below) advertises
agentCapabilities.auth.logout; agentInfo comes from @AcpAgent(name, version). A terminal method
is advertised only to a client that announced clientCapabilities.auth.terminal: the SDK does
that per-client check itself now, rather than the agent checking NegotiatedCapabilities by hand in
a written @Initialize method. Declaring an agent-type method (the default, as api-key is here)
without an @Authenticate handler to serve it fails the build, naming the method.
AuthMethod is an open union of two kinds. An agent-handled method (AuthMethodAgent, type AGENT,
the default) is the one the client calls authenticate for; a terminal method (AuthMethodTerminal,
type TERMINAL) is an interactive login the client runs, not something passed to authenticate.
An unknown auth method type from a newer agent reads as AuthMethodAgent.
Requiring sign-in
The agent-handled method
The terminal method
The client runs the agent’s own program again, as a separate process, with the method’s args and env added: not a different login tool, the same binary in a different mode.
The login stores credentials where the running agent process can find them; the client does not call authenticate for this method at all.
Client: advertising terminal auth, and dispatching the union
A client that never advertises auth.terminal sees only the agent method in authMethods(): the agent leaves the terminal method out entirely, rather than offering something the client can’t run.
session/new fails with AcpErrorCodes.AUTHENTICATION_REQUIRED (-32000) until the user is signed in, by either method, and fails again immediately after logout. AuthMethod is an open union: dispatch with instanceof, the same discipline as every other union type in the schema.
Source Code
View on GitHub
Running the Example
The demo runs seven steps against one client advertising terminal auth (initialize, a rejected session/new, the terminal login, a working session/new, logout, re-authenticating with the agent method) and a second client that doesn’t advertise terminal auth, which is offered only the agent method. No API key required.
Next Module
Module 37: Streamable HTTP and WebSocket: serve an agent over the network instead of stdio.