-
Notifications
You must be signed in to change notification settings - Fork 123
feat: ServerCallContext ports from Python implementation #285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package io.a2a.common; | ||
|
|
||
| /** | ||
| * Common A2A protocol headers and constants. | ||
| */ | ||
| public final class A2AHeaders { | ||
|
|
||
| /** | ||
| * HTTP header name for A2A extensions. | ||
| * Used to communicate which extensions are requested by the client. | ||
| */ | ||
| public static final String X_A2A_EXTENSIONS = "X-A2A-Extensions"; | ||
|
|
||
| private A2AHeaders() { | ||
| // Utility class | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
reference/grpc/src/main/java/io/a2a/server/grpc/quarkus/A2AExtensionsInterceptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package io.a2a.server.grpc.quarkus; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import io.grpc.Context; | ||
| import io.grpc.Contexts; | ||
| import io.grpc.Metadata; | ||
| import io.grpc.ServerCall; | ||
| import io.grpc.ServerCallHandler; | ||
| import io.grpc.ServerInterceptor; | ||
| import io.a2a.common.A2AHeaders; | ||
| import io.a2a.transport.grpc.context.GrpcContextKeys; | ||
|
|
||
| /** | ||
| * gRPC server interceptor that captures request metadata and context information, | ||
| * providing equivalent functionality to Python's grpc.aio.ServicerContext. | ||
| * | ||
| * This interceptor: | ||
| * - Extracts A2A extension headers from incoming requests | ||
| * - Captures ServerCall and Metadata for rich context access | ||
| * - Stores context information in gRPC Context for service method access | ||
| * - Provides proper equivalence to Python's ServicerContext | ||
| */ | ||
| @ApplicationScoped | ||
| public class A2AExtensionsInterceptor implements ServerInterceptor { | ||
|
|
||
|
|
||
| @Override | ||
| public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( | ||
| ServerCall<ReqT, RespT> serverCall, | ||
| Metadata metadata, | ||
| ServerCallHandler<ReqT, RespT> serverCallHandler) { | ||
|
|
||
| // Extract A2A extensions header | ||
| Metadata.Key<String> extensionsKey = | ||
| Metadata.Key.of(A2AHeaders.X_A2A_EXTENSIONS, Metadata.ASCII_STRING_MARSHALLER); | ||
| String extensions = metadata.get(extensionsKey); | ||
|
|
||
| // Create enhanced context with rich information (equivalent to Python's ServicerContext) | ||
| Context context = Context.current() | ||
| // Store complete metadata for full header access | ||
| .withValue(GrpcContextKeys.METADATA_KEY, metadata) | ||
| // Store method name (equivalent to Python's context.method()) | ||
| .withValue(GrpcContextKeys.METHOD_NAME_KEY, serverCall.getMethodDescriptor().getFullMethodName()) | ||
| // Store peer information for client connection details | ||
| .withValue(GrpcContextKeys.PEER_INFO_KEY, getPeerInfo(serverCall)); | ||
|
|
||
| // Store A2A extensions if present | ||
| if (extensions != null) { | ||
| context = context.withValue(GrpcContextKeys.EXTENSIONS_HEADER_KEY, extensions); | ||
| } | ||
|
|
||
| // Proceed with the call in the enhanced context | ||
| return Contexts.interceptCall(context, serverCall, metadata, serverCallHandler); | ||
| } | ||
|
|
||
| /** | ||
| * Safely extracts peer information from the ServerCall. | ||
| * | ||
| * @param serverCall the gRPC ServerCall | ||
| * @return peer information string, or "unknown" if not available | ||
| */ | ||
| private String getPeerInfo(ServerCall<?, ?> serverCall) { | ||
| try { | ||
| Object remoteAddr = serverCall.getAttributes().get(io.grpc.Grpc.TRANSPORT_ATTR_REMOTE_ADDR); | ||
| return remoteAddr != null ? remoteAddr.toString() : "unknown"; | ||
| } catch (Exception e) { | ||
| return "unknown"; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was just thinking some more about this and realized that I can probably modify #292 to add the authorization/api-key headers for gRPC in a similar way.
Currently, we are relying on the user configuring a gRPC
ClientInterceptorto specify the token/API key. Instead, I think I can updateGrpcTransportto make use of theClientCallInterceptorsso that theAuthInterceptorcould be used for gRPC like we use it for the other transports to add the appropriate header. And then, I can update this method to add the appropriate header to the gRPCmetadataif present.