Handling tool authentication with Azure Foundry AGUI in C#/Blazor

1 day ago 3
ARTICLE AD BOX

I have a Azure Foundry server setup with ChatGPT 4.1, and I have setup my instructions, and added my own MCP server as a Tool.

If I use the playground in Foundry then the agent responds suitably. When I try to use the MCP server I am provided with a link to authenticate (again this is expected as the mcp server requires authentication), if I authenticate then the playground can use the MCP and I get the data I expect.

I want to have a chat client written in Blazor, so I have setup a backend to establish the connection to Foundry with an API endpoint to call with chat messages (code below).

If I disable the MCP tool then the code works perfectly, messages are transferred to the Agent, and responses are streamed back which follow the instructions given to the Agent in Foundry.

If I enable the MCP tool then I get a response from Foundry, but it does not conform to the content types that the guidance suggests to use for streamed data (E.g. FunctionToolContent, ToolCallContent, DataContent, TextContent, ErrorContent) and as such does not allow me to identify it in order to display it. I am pretty sure that this response is meant to be the MCP Authentication response. I am pretty sure about this because if I remove my tool and add the "web search" (which does not require authentication) then I am able to trigger that tool successfully from my frontend (if I ask it to carry out a web search to find the answer to a question).

I have inspected the update, chatUpdate and the individual contents objects and there is nothing in the response to help me determine that this is meant to be the authentication link. My understanding is that the Microsoft AGUI library takes responses from Foundry and structures the responses to be of specific types to make them easier to handle, so it is possible that this isn't currently supported by the library (which would scupper my plans). I appreciate that AGUI is new as a protocol, but having tried using the OpenAI API in Foundry and having found it limited I was impressed with the ease of use of the AGUI libraries.

Does anyone know how I can identity and handle a tool authentication request on the frontend?

Backend code

using Microsoft.Agents.AI; using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore; WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Services.AddHttpClient().AddLogging(); builder.Services.AddAGUI(); WebApplication app = builder.Build(); const string endpoint = "<ENDPOINT_URL>"; const string agentId = "<AGENT_NAME>"; // Connect to the existing AI agent AIAgent agent = new AIProjectClient( new Uri(endpoint), new DefaultAzureCredential()) .AsAIAgent(agentId); // Map the AG-UI agent endpoint app.MapAGUI("/", agent); await app.RunAsync();

Frontend code

StringBuilder finalMessage = new StringBuilder(); messages.Add(new Microsoft.Extensions.AI.ChatMessage(Microsoft.Extensions.AI.ChatRole.User, text)); await foreach (AgentResponseUpdate update in agent.RunStreamingAsync(messages, session)) { ChatResponseUpdate chatUpdate = update.AsChatResponseUpdate(); // Display streaming text content if (update.Contents.Count > 0) { foreach (AIContent content in update.Contents) { if (content is DataContent dataContent) { finalMessage.Append("DATACONTENT:" + JsonSerializer.Serialize(dataContent)); } if (content is FunctionCallContent functionCallContent) { } if (content is ToolCallContent toolCallContent) { finalMessage.Append(JsonSerializer.Serialize(content)); } if (content is TextContent textContent) { finalMessage.Append(textContent.Text); InProgressMessage += textContent.Text; StateHasChanged(); } else if (content is ErrorContent errorContent) { finalMessage.Append("THERE WAS AN ERROR: " + errorContent.Message); InProgressMessage = finalMessage.ToString(); } } } else { finalMessage.Append(JsonSerializer.Serialize(update)); } } messages.Add(new ChatMessage(ChatRole.Assistant, finalMessage.ToString())); InProgressMessage = string.Empty;

For the avoidance of doubt, when I get the MCP authentication link response update.Contents.Count is 0, which means that it falls in to the last else statement with the content of update not providing anything of note.

Read Entire Article