-
Notifications
You must be signed in to change notification settings - Fork 64
feat: support v0.3 agent card parsing in A2ACardResolver #417
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
Open
darrelmiller
wants to merge
1
commit into
main
Choose a base branch
from
v03-card-upcast
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,10 +68,24 @@ public async Task<AgentCard> GetAgentCardAsync(CancellationToken cancellationTok | |
|
|
||
| response.EnsureSuccessStatusCode(); | ||
|
|
||
| using var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); | ||
| // Buffer the response so we can attempt multiple deserialization strategies | ||
| var bytes = await response.Content.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false); | ||
|
|
||
| return await JsonSerializer.DeserializeAsync(responseStream, A2AJsonUtilities.JsonContext.Default.AgentCard, cancellationToken).ConfigureAwait(false) ?? | ||
| throw new A2AException("Failed to parse agent card JSON."); | ||
| try | ||
| { | ||
| return JsonSerializer.Deserialize(bytes, A2AJsonUtilities.JsonContext.Default.AgentCard) | ||
| ?? throw new A2AException("Failed to parse agent card JSON."); | ||
| } | ||
| catch (JsonException ex) when (ex.Message.Contains("supportedInterfaces") || | ||
| ex.Message.Contains("skills") || | ||
| ex.Message.Contains("defaultInputModes") || | ||
| ex.Message.Contains("defaultOutputModes")) | ||
| { | ||
| // The card is missing v1.0 required properties — attempt v0.3 upcast | ||
| _logger.AttemptingV03AgentCardUpcast(ex); | ||
| return UpcastV03AgentCard(bytes) | ||
| ?? throw new A2AException($"Failed to parse JSON: {ex.Message}"); | ||
| } | ||
| } | ||
| catch (JsonException ex) | ||
| { | ||
|
|
@@ -88,4 +102,133 @@ public async Task<AgentCard> GetAgentCardAsync(CancellationToken cancellationTok | |
| throw new A2AException("HTTP request failed", ex); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Attempts to parse a v0.3 agent card and upcast it to a v1.0 <see cref="AgentCard"/>. | ||
| /// A v0.3 card has a top-level "url" and optional "preferredTransport" instead of "supportedInterfaces". | ||
| /// </summary> | ||
| /// <param name="bytes">The raw JSON bytes of the agent card response.</param> | ||
| /// <returns>An upcast v1.0 <see cref="AgentCard"/> if the JSON is a valid v0.3 card; otherwise <c>null</c>.</returns> | ||
| private static AgentCard? UpcastV03AgentCard(byte[] bytes) | ||
| { | ||
| using var doc = JsonDocument.Parse(bytes); | ||
| var root = doc.RootElement; | ||
|
|
||
| // v0.3 cards MUST have a "url" property | ||
| if (!root.TryGetProperty("url", out var urlElement) || urlElement.ValueKind != JsonValueKind.String) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var url = urlElement.GetString(); | ||
| if (string.IsNullOrEmpty(url)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| // Determine the protocol binding from preferredTransport (defaults to JSONRPC) | ||
| var protocolBinding = ProtocolBindingNames.JsonRpc; | ||
| if (root.TryGetProperty("preferredTransport", out var transportElement)) | ||
| { | ||
| var transport = transportElement.ValueKind == JsonValueKind.String | ||
| ? transportElement.GetString() | ||
| : transportElement.ValueKind == JsonValueKind.Object && transportElement.TryGetProperty("value", out var val) | ||
| ? val.GetString() | ||
| : null; | ||
|
|
||
| if (!string.IsNullOrEmpty(transport)) | ||
| { | ||
| protocolBinding = transport.ToUpperInvariant() switch | ||
| { | ||
| "JSONRPC" or "JSON-RPC" => ProtocolBindingNames.JsonRpc, | ||
| "HTTP+JSON" or "HTTP_JSON" or "REST" => ProtocolBindingNames.HttpJson, | ||
| "GRPC" => ProtocolBindingNames.Grpc, | ||
| _ => transport | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| // Build the supportedInterfaces list from the v0.3 url + preferredTransport | ||
| var interfaces = new List<AgentInterface> | ||
| { | ||
| new() | ||
| { | ||
| ProtocolBinding = protocolBinding, | ||
| Url = url, | ||
| ProtocolVersion = root.TryGetProperty("protocolVersion", out var pv) ? pv.GetString() ?? "0.3" : "0.3", | ||
| } | ||
| }; | ||
|
|
||
| // Also include additionalInterfaces if present (a v0.3 extension) | ||
| if (root.TryGetProperty("additionalInterfaces", out var addlInterfaces) && addlInterfaces.ValueKind == JsonValueKind.Array) | ||
| { | ||
| foreach (var iface in addlInterfaces.EnumerateArray()) | ||
| { | ||
| var ai = JsonSerializer.Deserialize(iface.GetRawText(), A2AJsonUtilities.JsonContext.Default.AgentInterface); | ||
| if (ai is not null) | ||
| { | ||
| interfaces.Add(ai); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Extract common fields | ||
| var card = new AgentCard | ||
| { | ||
| Name = root.TryGetProperty("name", out var name) ? name.GetString() ?? "" : "", | ||
| Description = root.TryGetProperty("description", out var desc) ? desc.GetString() ?? "" : "", | ||
| Version = root.TryGetProperty("version", out var ver) ? ver.GetString() ?? "0.3" : "0.3", | ||
|
Comment on lines
+178
to
+180
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If any of these properties exist in the JSON but are not strings (e.g., they are numbers, booleans, or objects), calling Name = root.TryGetProperty("name", out var name) && name.ValueKind == JsonValueKind.String ? name.GetString() ?? "" : "",
Description = root.TryGetProperty("description", out var desc) && desc.ValueKind == JsonValueKind.String ? desc.GetString() ?? "" : "",
Version = root.TryGetProperty("version", out var ver) && ver.ValueKind == JsonValueKind.String ? ver.GetString() ?? "0.3" : "0.3", |
||
| SupportedInterfaces = interfaces, | ||
| Capabilities = new AgentCapabilities(), | ||
| DefaultInputModes = ["text/plain"], | ||
| DefaultOutputModes = ["text/plain"], | ||
| Skills = [], | ||
| }; | ||
|
|
||
| // Parse capabilities | ||
| if (root.TryGetProperty("capabilities", out var caps) && caps.ValueKind == JsonValueKind.Object) | ||
| { | ||
| if (caps.TryGetProperty("streaming", out var streaming)) | ||
| card.Capabilities.Streaming = streaming.ValueKind == JsonValueKind.True; | ||
| if (caps.TryGetProperty("pushNotifications", out var push)) | ||
| card.Capabilities.PushNotifications = push.ValueKind == JsonValueKind.True; | ||
| } | ||
|
|
||
| // Parse default modes if present | ||
| if (root.TryGetProperty("defaultInputModes", out var inputModes) && inputModes.ValueKind == JsonValueKind.Array) | ||
| { | ||
| card.DefaultInputModes = inputModes.EnumerateArray() | ||
| .Where(e => e.ValueKind == JsonValueKind.String) | ||
| .Select(e => e.GetString()!) | ||
| .ToList(); | ||
| } | ||
|
|
||
| if (root.TryGetProperty("defaultOutputModes", out var outputModes) && outputModes.ValueKind == JsonValueKind.Array) | ||
| { | ||
| card.DefaultOutputModes = outputModes.EnumerateArray() | ||
| .Where(e => e.ValueKind == JsonValueKind.String) | ||
| .Select(e => e.GetString()!) | ||
| .ToList(); | ||
| } | ||
|
|
||
| // Parse skills if present | ||
| if (root.TryGetProperty("skills", out var skills) && skills.ValueKind == JsonValueKind.Array) | ||
| { | ||
| foreach (var skillElement in skills.EnumerateArray()) | ||
| { | ||
| var skill = JsonSerializer.Deserialize(skillElement.GetRawText(), A2AJsonUtilities.JsonContext.Default.AgentSkill); | ||
| if (skill is not null) | ||
| { | ||
| card.Skills.Add(skill); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (root.TryGetProperty("documentationUrl", out var docUrl)) | ||
| card.DocumentationUrl = docUrl.GetString(); | ||
| if (root.TryGetProperty("iconUrl", out var iconUrl)) | ||
| card.IconUrl = iconUrl.GetString(); | ||
|
|
||
| return card; | ||
| } | ||
| } | ||
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
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.
Relying on
ex.Message.Contains(...)to detect missing properties is fragile and error-prone for two main reasons:JsonExceptionmessages is an implementation detail of the .NET runtime and can change between versions or patches.Instead, catch any
JsonException, attempt the upcast, and if the upcast succeeds, return the upcast card. If it fails (returnsnull), rethrow the original exception (throw;) to let the outer catch block handle it.