KsContextBinding
Describes how a single piece of per-call context is propagated across the wire. Implementations are referenced from KsContext.binding on a meta-annotation and are responsible for:
identifying the value on the wire (wireKey);
encoding / decoding the value for transports that carry it as a string (toWire / fromWire).
The binding is the CoroutineContext.Key for its element — a single source of truth for both the wire identity and the coroutine-context identity. The recommended shape is to declare the binding as a named companion object on the element type itself:
class AuthToken(val token: String) : CoroutineContext.Element {
override val key get() = Key
companion object Key : KsContextBinding<AuthToken> {
override val wireKey = "x-auth-token"
override fun toWire(value: AuthToken) = value.token
override fun fromWire(encoded: String) = AuthToken(encoded)
}
}Reference the binding at the annotation site by its companion name:
@KsContext(binding = AuthToken.Key::class)Handlers then read the propagated value with the standard coroutine-context lookup, naming the element type directly:
val token = coroutineContext[AuthToken]The element type T must be a CoroutineContext.Element so it can sit directly in the running coroutine's context.
Implementations should be stateless and safe to reference from a KClass-literal in an annotation argument.