Interface SessionIdGenerator<T>

Type Parameters:
T - the transport-specific request type (a Netty HttpRequest for the HTTP transport); use Object for a request-independent generator like DEFAULT
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface SessionIdGenerator<T>
Derives the session id for a newly initialized session from the incoming initialize request. Lets operators base the id on request headers (tenant id, auth subject, a client-supplied id, …) or the request URI.

Only consulted when sessions are enabled (session(s -> s.enabled(true))); stateless servers never create a session and never call this.

T is the transport-specific request type handed to generate(dev.tachyonmcp.api.runtime.InteractionContext, T). The HTTP transport passes a Netty HttpRequest; keep the type variable open (e.g. SessionIdGenerator<? super HttpRequest>) so a generator stays reusable across transports and does not pin the public API to a concrete request type.

Example — derive the id from a tenant header, falling back to a random id:


 SessionIdGenerator<HttpRequest> byTenant = (ctx, request) -> {
     String tenant = request.headers().get("X-Tenant-Id");
     return tenant != null ? "sess_" + tenant + "_" + UUID.randomUUID() : SessionIdGenerator.DEFAULT.generate(ctx, request);
 };

 server.session(s -> s.enabled(true).sessionIdGenerator(byTenant));
 
Author:
Konstantin Pavlov
  • Field Details

  • Method Details

    • generate

      String generate(InteractionContext channelContext, T request)
      Derives a session id from the initialize request (headers, URI, …).

      Contract for the initialize flow:

      • A null return value or any thrown exception causes the server to respond with an internal-error and abort session creation.
      • A blank string ("" or whitespace-only) is accepted as-is and used directly as the session id — the generator must produce a non-blank value if a valid id is required.
      Parameters:
      channelContext - the per-channel interaction (protocol version, lifecycle phase, attribute scratch space); may be null in contexts that create a session without an established channel
      request - the incoming initialize request; may be null when readsRequest() is false
      Returns:
      the session id to assign; must be non-blank when a valid id is required
    • readsRequest

      default boolean readsRequest()
      Whether generate(dev.tachyonmcp.api.runtime.InteractionContext, T) inspects the request (headers, URI, …). When false, the transport skips detaching a per-request snapshot for the async session-creation dispatch.

      Defaults to true so every custom generator is handed a valid request; override to false for a request-independent id (like DEFAULT) to opt into the fast path.