Interface CallContextFactory

All Known Implementing Classes:
QuarkusCallContextFactory

public interface CallContextFactory
Factory interface for creating ServerCallContext from gRPC request context.

This interface provides an extension point for customizing how ServerCallContext instances are created in gRPC applications. The default implementation in GrpcHandler extracts standard information (user, metadata, headers, peer info, protocol version), but applications can provide their own implementation to add custom context data.

Default Behavior

When no CDI bean implementing this interface is provided, GrpcHandler creates contexts with:

  • User authentication from security context
  • gRPC metadata (headers)
  • Method name and peer information
  • A2A protocol version from A2A-Version header
  • Required extensions from A2A-Extensions header
  • Response observer for gRPC streaming

Custom Implementation Example

@ApplicationScoped
public class CustomCallContextFactory implements CallContextFactory {
    @Override
    public <V> ServerCallContext create(StreamObserver<V> responseObserver) {
        // Extract custom data from gRPC context
        Context grpcContext = Context.current();
        Metadata metadata = GrpcContextKeys.METADATA_KEY.get(grpcContext);
        String orgId = metadata.get(
            Metadata.Key.of("x-organization-id", Metadata.ASCII_STRING_MARSHALLER)
        );

        Map<String, Object> state = new HashMap<>();
        state.put("organization", orgId);
        state.put("grpc_response_observer", responseObserver);

        return new ServerCallContext(
            extractUser(),
            state,
            extractExtensions(grpcContext),
            extractVersion(grpcContext)
        );
    }
}
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    create(io.grpc.stub.StreamObserver<V> responseObserver)
    Creates a ServerCallContext from gRPC request context.
  • Method Details

    • create

      <V> ServerCallContext create(io.grpc.stub.StreamObserver<V> responseObserver)
      Creates a ServerCallContext from gRPC request context.

      This method is called for each incoming gRPC request to create the context that will be passed to the RequestHandler and eventually to the AgentExecutor.

      Implementations should extract information from the current Context using GrpcContextKeys to access metadata, method name, peer info, and A2A protocol headers.

      Type Parameters:
      V - the response type for the gRPC method
      Parameters:
      responseObserver - the gRPC response stream observer
      Returns:
      a new ServerCallContext with extracted authentication, metadata, and headers