-
Notifications
You must be signed in to change notification settings - Fork 4k
.Net: Initial check-in for the A2A Agent implementation #12050
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
markwallace-microsoft
merged 50 commits into
microsoft:main
from
markwallace-microsoft:users/markwallace/a2a_agent
Jun 25, 2025
Merged
Changes from all commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
73d2a06
Initial check-in for the A2A Agent implementation
markwallace-microsoft 1f90951
Merge branch 'main' into users/markwallace/a2a_agent
markwallace-microsoft 21c1858
Fix warning
markwallace-microsoft b8b3bf6
Add A2A Client and Server samples
markwallace-microsoft 0196641
Mark assembly as experimental
markwallace-microsoft aba8ada
Fix warnings
markwallace-microsoft d1297dd
Fix warnings
markwallace-microsoft 384de1d
Skip .net8.0 build for A2AClientServer
markwallace-microsoft 8469ddc
Skip .net8.0 build for A2AClientServer
markwallace-microsoft 192ebee
Skip .net8.0 build for A2AClientServer
markwallace-microsoft 908af44
Undo changes
markwallace-microsoft f2516dd
Bump package versions and add readme
markwallace-microsoft d2ba0d8
Revert to just net8.0
markwallace-microsoft 4edfb35
Merge Shawn's changes and support ChatCompletion or AzureAI agents
markwallace-microsoft 7d6ce75
Merge branch 'main' into users/markwallace/a2a_agent
markwallace-microsoft 84acb02
Fix formatting
markwallace-microsoft bec3c2d
Fix formatting
markwallace-microsoft 41a1d31
Merge branch 'main' into users/markwallace/a2a_agent
markwallace-microsoft 7940e93
Rename configu property
markwallace-microsoft 2244207
Merge branch 'users/markwallace/a2a_agent' of https://github.com/mark…
markwallace-microsoft b2439f5
Start work on streaming support
markwallace-microsoft 2ba30e3
Merge latest from main and resolve merge conflicts
markwallace-microsoft 3898ebe
Merge branch 'main' into users/markwallace/a2a_agent
markwallace-microsoft 9458511
Update the .slnx file
markwallace-microsoft 47d7e69
Merge branch 'main' into users/markwallace/a2a_agent
markwallace-microsoft 6844e8b
Update to new client API
markwallace-microsoft bf32517
Upgrade to latest SharpA2A
markwallace-microsoft bbf19ca
Merge branch 'main' into users/markwallace/a2a_agent
markwallace-microsoft a05b44d
Cleanup samples and test
markwallace-microsoft ea68957
Update the A2A demo README
markwallace-microsoft ea42d2e
Add unit tests
markwallace-microsoft 8a4d4ff
Add unit tests
markwallace-microsoft 513d831
Merge branch 'users/markwallace/a2a_agent' of https://github.com/mark…
markwallace-microsoft 059e6eb
Merge branch 'main' into users/markwallace/a2a_agent
markwallace-microsoft ad08d15
Address code review feedback
markwallace-microsoft 08af806
Address more code review feedback
markwallace-microsoft 99d8a39
Test with A2A Inspector
markwallace-microsoft 014165c
Fix unit tests
markwallace-microsoft 0ca3d3c
More code review feedback
markwallace-microsoft 7504b2c
More code review feedback
markwallace-microsoft 90d88c6
Merge branch 'users/markwallace/a2a_agent' of https://github.com/mark…
markwallace-microsoft 8bb1a7a
Merge branch 'main' into users/markwallace/a2a_agent
markwallace-microsoft 11262d9
Merge branch 'main' into users/markwallace/a2a_agent
markwallace-microsoft 634b0fe
Merge branch 'main' into users/markwallace/a2a_agent
markwallace-microsoft da456b7
Address more code review feedback
markwallace-microsoft e3cfe60
Merge branch 'users/markwallace/a2a_agent' of https://github.com/mark…
markwallace-microsoft ab6f5d7
Update the README.md
markwallace-microsoft cf8238d
Update the README.md
markwallace-microsoft c3996bf
Merge branch 'users/markwallace/a2a_agent' of https://github.com/mark…
markwallace-microsoft 743e8a0
Merge branch 'main' into users/markwallace/a2a_agent
markwallace-microsoft 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
24 changes: 24 additions & 0 deletions
24
dotnet/samples/Demos/A2AClientServer/A2AClient/A2AClient.csproj
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,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<UserSecretsId>5ee045b0-aea3-4f08-8d31-32d1a6f8fed0</UserSecretsId> | ||
<NoWarn>$(NoWarn);CS1591;VSTHRD111;CA2007;SKEXP0110</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="SharpA2A.Core" /> | ||
<PackageReference Include="System.CommandLine" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\..\src\Agents\A2A\Agents.A2A.csproj" /> | ||
<ProjectReference Include="..\..\..\..\src\Agents\Core\Agents.Core.csproj" /> | ||
<ProjectReference Include="..\..\..\..\src\Connectors\Connectors.OpenAI\Connectors.OpenAI.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
120 changes: 120 additions & 0 deletions
120
dotnet/samples/Demos/A2AClientServer/A2AClient/HostClientAgent.cs
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,120 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Microsoft.Extensions.Logging; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Agents; | ||
using Microsoft.SemanticKernel.Agents.A2A; | ||
using SharpA2A.Core; | ||
|
||
namespace A2A; | ||
|
||
internal sealed class HostClientAgent | ||
{ | ||
internal HostClientAgent(ILogger logger) | ||
{ | ||
this._logger = logger; | ||
} | ||
internal async Task InitializeAgentAsync(string modelId, string apiKey, string[] agentUrls) | ||
{ | ||
try | ||
{ | ||
this._logger.LogInformation("Initializing Semantic Kernel agent with model: {ModelId}", modelId); | ||
|
||
// Connect to the remote agents via A2A | ||
var createAgentTasks = agentUrls.Select(agentUrl => this.CreateAgentAsync(agentUrl)); | ||
var agents = await Task.WhenAll(createAgentTasks); | ||
var agentFunctions = agents.Select(agent => AgentKernelFunctionFactory.CreateFromAgent(agent)).ToList(); | ||
var agentPlugin = KernelPluginFactory.CreateFromFunctions("AgentPlugin", agentFunctions); | ||
|
||
// Define the Host agent | ||
var builder = Kernel.CreateBuilder(); | ||
builder.AddOpenAIChatCompletion(modelId, apiKey); | ||
builder.Plugins.Add(agentPlugin); | ||
var kernel = builder.Build(); | ||
kernel.FunctionInvocationFilters.Add(new ConsoleOutputFunctionInvocationFilter()); | ||
|
||
this.Agent = new ChatCompletionAgent() | ||
{ | ||
Kernel = kernel, | ||
Name = "HostClient", | ||
Instructions = | ||
""" | ||
You specialize in handling queries for users and using your tools to provide answers. | ||
""", | ||
Arguments = new KernelArguments(new PromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }), | ||
}; | ||
} | ||
catch (Exception ex) | ||
{ | ||
this._logger.LogError(ex, "Failed to initialize HostClientAgent"); | ||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// The associated <see cref="Agent"/> | ||
/// </summary> | ||
public Agent? Agent { get; private set; } | ||
|
||
#region private | ||
private readonly ILogger _logger; | ||
|
||
private async Task<A2AAgent> CreateAgentAsync(string agentUri) | ||
{ | ||
var httpClient = new HttpClient | ||
{ | ||
BaseAddress = new Uri(agentUri), | ||
Timeout = TimeSpan.FromSeconds(60) | ||
}; | ||
|
||
var client = new A2AClient(httpClient); | ||
var cardResolver = new A2ACardResolver(httpClient); | ||
var agentCard = await cardResolver.GetAgentCardAsync(); | ||
|
||
return new A2AAgent(client, agentCard!); | ||
} | ||
#endregion | ||
} | ||
|
||
internal sealed class ConsoleOutputFunctionInvocationFilter() : IFunctionInvocationFilter | ||
{ | ||
private static string IndentMultilineString(string multilineText, int indentLevel = 1, int spacesPerIndent = 4) | ||
{ | ||
// Create the indentation string | ||
var indentation = new string(' ', indentLevel * spacesPerIndent); | ||
|
||
// Split the text into lines, add indentation, and rejoin | ||
char[] NewLineChars = { '\r', '\n' }; | ||
string[] lines = multilineText.Split(NewLineChars, StringSplitOptions.None); | ||
|
||
return string.Join(Environment.NewLine, lines.Select(line => indentation + line)); | ||
} | ||
public async Task OnFunctionInvocationAsync(FunctionInvocationContext context, Func<FunctionInvocationContext, Task> next) | ||
{ | ||
Console.ForegroundColor = ConsoleColor.DarkGray; | ||
|
||
Console.WriteLine($"\nCalling Agent {context.Function.Name} with arguments:"); | ||
Console.ForegroundColor = ConsoleColor.Gray; | ||
|
||
foreach (var kvp in context.Arguments) | ||
{ | ||
Console.WriteLine(IndentMultilineString($" {kvp.Key}: {kvp.Value}")); | ||
} | ||
|
||
await next(context); | ||
|
||
if (context.Result.GetValue<object>() is ChatMessageContent[] chatMessages) | ||
{ | ||
Console.ForegroundColor = ConsoleColor.DarkGray; | ||
|
||
Console.WriteLine($"Response from Agent {context.Function.Name}:"); | ||
foreach (var message in chatMessages) | ||
{ | ||
Console.ForegroundColor = ConsoleColor.Gray; | ||
|
||
Console.WriteLine(IndentMultilineString($"{message}")); | ||
} | ||
} | ||
Console.ResetColor(); | ||
} | ||
} |
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,89 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.CommandLine; | ||
using System.CommandLine.Invocation; | ||
using System.Reflection; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Agents; | ||
|
||
namespace A2A; | ||
|
||
public static class Program | ||
{ | ||
public static async Task<int> Main(string[] args) | ||
{ | ||
// Create root command with options | ||
var rootCommand = new RootCommand("A2AClient"); | ||
rootCommand.SetHandler(HandleCommandsAsync); | ||
|
||
// Run the command | ||
return await rootCommand.InvokeAsync(args); | ||
} | ||
|
||
public static async System.Threading.Tasks.Task HandleCommandsAsync(InvocationContext context) | ||
{ | ||
await RunCliAsync(); | ||
} | ||
markwallace-microsoft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#region private | ||
private static async System.Threading.Tasks.Task RunCliAsync() | ||
{ | ||
// Set up the logging | ||
using var loggerFactory = LoggerFactory.Create(builder => | ||
{ | ||
builder.AddConsole(); | ||
builder.SetMinimumLevel(LogLevel.Information); | ||
}); | ||
var logger = loggerFactory.CreateLogger("A2AClient"); | ||
|
||
// Retrieve configuration settings | ||
IConfigurationRoot configRoot = new ConfigurationBuilder() | ||
.AddEnvironmentVariables() | ||
.AddUserSecrets(Assembly.GetExecutingAssembly()) | ||
.Build(); | ||
var apiKey = configRoot["A2AClient:ApiKey"] ?? throw new ArgumentException("A2AClient:ApiKey must be provided"); | ||
var modelId = configRoot["A2AClient:ModelId"] ?? "gpt-4.1"; | ||
var agentUrls = configRoot["A2AClient:AgentUrls"] ?? "http://localhost:5000/;http://localhost:5001/;http://localhost:5002/"; | ||
|
||
// Create the Host agent | ||
var hostAgent = new HostClientAgent(logger); | ||
await hostAgent.InitializeAgentAsync(modelId, apiKey, agentUrls!.Split(";")); | ||
AgentThread thread = new ChatHistoryAgentThread(); | ||
try | ||
{ | ||
while (true) | ||
{ | ||
// Get user message | ||
Console.Write("\nUser (:q or quit to exit): "); | ||
string? message = Console.ReadLine(); | ||
if (string.IsNullOrWhiteSpace(message)) | ||
{ | ||
Console.WriteLine("Request cannot be empty."); | ||
continue; | ||
} | ||
|
||
if (message == ":q" || message == "quit") | ||
{ | ||
break; | ||
} | ||
|
||
await foreach (AgentResponseItem<ChatMessageContent> response in hostAgent.Agent!.InvokeAsync(message, thread)) | ||
{ | ||
Console.ForegroundColor = ConsoleColor.Cyan; | ||
Console.WriteLine($"\nAgent: {response.Message.Content}"); | ||
Console.ResetColor(); | ||
|
||
thread = response.Thread; | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(ex, "An error occurred while running the A2AClient"); | ||
return; | ||
} | ||
} | ||
#endregion | ||
} |
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,26 @@ | ||
| ||
# A2A Client Sample | ||
Show how to create an A2A Client with a command line interface which invokes agents using the A2A protocol. | ||
|
||
## Run the Sample | ||
|
||
To run the sample, follow these steps: | ||
|
||
1. Run the A2A client: | ||
```bash | ||
cd A2AClient | ||
markwallace-microsoft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dotnet run | ||
``` | ||
2. Enter your request e.g. "Show me all invoices for Contoso?" | ||
|
||
## Set Secrets with Secret Manager | ||
|
||
The agent urls are provided as a ` ` delimited list of strings | ||
|
||
```text | ||
cd dotnet/samples/Demos/A2AClientServer/A2AClient | ||
|
||
dotnet user-secrets set "A2AClient:ModelId" "..." | ||
dotnet user-secrets set "A2AClient":ApiKey" "..." | ||
dotnet user-secrets set "A2AClient:AgentUrls" "http://localhost:5000/policy;http://localhost:5000/invoice;http://localhost:5000/logistics" | ||
``` |
24 changes: 24 additions & 0 deletions
24
dotnet/samples/Demos/A2AClientServer/A2AServer/A2AServer.csproj
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,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<UserSecretsId>5ee045b0-aea3-4f08-8d31-32d1a6f8fed0</UserSecretsId> | ||
<NoWarn>$(NoWarn);CS1591;VSTHRD111;CA2007;SKEXP0110</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="SharpA2A.Core" /> | ||
<PackageReference Include="SharpA2A.AspNetCore" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\..\src\Agents\A2A\Agents.A2A.csproj" /> | ||
<ProjectReference Include="..\..\..\..\src\Agents\AzureAI\Agents.AzureAI.csproj" /> | ||
<ProjectReference Include="..\..\..\..\src\Agents\Core\Agents.Core.csproj" /> | ||
<ProjectReference Include="..\..\..\..\src\Connectors\Connectors.OpenAI\Connectors.OpenAI.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.