From 191c66dab50a5040d9791a169c276d3f2d8bbb0e Mon Sep 17 00:00:00 2001 From: westey <164392973+westey-m@users.noreply.github.com> Date: Fri, 13 Feb 2026 17:36:21 +0000 Subject: [PATCH] Fixes for bug bash. --- .../Program.cs | 15 ++++++++------- .../Agents/Agent_Step10_AsMcpTool/README.md | 4 ++-- .../Agents/Agent_Step16_ChatReduction/Program.cs | 15 +++++++++++++-- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step06_PersistedConversations/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step06_PersistedConversations/Program.cs index e22c377929..d3331cb2b8 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step06_PersistedConversations/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step06_PersistedConversations/Program.cs @@ -1,5 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. +#pragma warning disable CA1869 // Cache and reuse 'JsonSerializerOptions' instances + // This sample shows how to create and use a simple AI agent with a conversation that can be persisted to disk. using System.Text.Json; @@ -30,15 +32,14 @@ // Serialize the session state to a JsonElement, so it can be stored for later use. JsonElement serializedSession = await agent.SerializeSessionAsync(session); -// Save the serialized session to a temporary file (for demonstration purposes). -string tempFilePath = Path.GetTempFileName(); -await File.WriteAllTextAsync(tempFilePath, JsonSerializer.Serialize(serializedSession)); - -// Load the serialized session from the temporary file (for demonstration purposes). -JsonElement reloadedSerializedSession = JsonElement.Parse(await File.ReadAllTextAsync(tempFilePath)); +// In a real application, you would typically write the serialized session to a file or +// database for persistence, and read it back when resuming the conversation. +// Here we'll just write the serialized session to console (for demonstration purposes). +Console.WriteLine("\n--- Serialized session ---\n"); +Console.WriteLine(JsonSerializer.Serialize(serializedSession, new JsonSerializerOptions { WriteIndented = true }) + "\n"); // Deserialize the session state after loading from storage. -AgentSession resumedSession = await agent.DeserializeSessionAsync(reloadedSerializedSession); +AgentSession resumedSession = await agent.DeserializeSessionAsync(serializedSession); // Run the agent again with the resumed session. Console.WriteLine(await agent.RunAsync("Now tell the same joke in the voice of a pirate, and add some emojis to the joke.", resumedSession)); diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step10_AsMcpTool/README.md b/dotnet/samples/GettingStarted/Agents/Agent_Step10_AsMcpTool/README.md index c56c9a7a68..56701066d1 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step10_AsMcpTool/README.md +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step10_AsMcpTool/README.md @@ -11,9 +11,9 @@ Alternatively, use the QuickstartClient sample from this repository: https://git To use the [MCP Inspector](https://modelcontextprotocol.io/docs/tools/inspector), follow these steps: 1. Open a terminal in the Agent_Step10_AsMcpTool project directory. -1. Run the `npx @modelcontextprotocol/inspector dotnet run` command to start the MCP Inspector. Make sure you have [node.js](https://nodejs.org/en/download/) and npm installed. +1. Run the `npx @modelcontextprotocol/inspector dotnet run --framework net10.0` command to start the MCP Inspector. Make sure you have [node.js](https://nodejs.org/en/download/) and npm installed. ```bash - npx @modelcontextprotocol/inspector dotnet run + npx @modelcontextprotocol/inspector dotnet run --framework net10.0 ``` 1. When the inspector is running, it will display a URL in the terminal, like this: ``` diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step16_ChatReduction/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step16_ChatReduction/Program.cs index 875f3375a4..c95f46a6c0 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step16_ChatReduction/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step16_ChatReduction/Program.cs @@ -38,18 +38,29 @@ // Get the chat history to see how many messages are stored. // We can use the ChatHistoryProvider, that is also used by the agent, to read the // chat history from the session state, and see how the reducer is affecting the stored messages. +// Here we expect to see 2 messages, the original user message and the agent response message. var provider = agent.GetService(); List? chatHistory = provider?.GetMessages(session); Console.WriteLine($"\nChat history has {chatHistory?.Count} messages.\n"); // Invoke the agent a few more times. Console.WriteLine(await agent.RunAsync("Tell me a joke about a robot.", session)); + +// Now we expect to see 4 messages in the chat history, 2 input and 2 output. +// While the target number of messages is 2, the default time for the InMemoryChatHistoryProvider +// to trigger the reducer is just before messages are contributed to a new agent run. +// So at this time, we have not yet triggered the reducer for the most recently added messages, +// and they are still in the chat history. +chatHistory = provider?.GetMessages(session); Console.WriteLine($"\nChat history has {chatHistory?.Count} messages.\n"); + Console.WriteLine(await agent.RunAsync("Tell me a joke about a lemur.", session)); +chatHistory = provider?.GetMessages(session); Console.WriteLine($"\nChat history has {chatHistory?.Count} messages.\n"); // At this point, the chat history has exceeded the limit and the original message will not exist anymore, -// so asking a follow up question about it will not work as expected. -Console.WriteLine(await agent.RunAsync("Tell me the joke about the pirate again, but add emojis and use the voice of a parrot.", session)); +// so asking a follow up question about it may not work as expected. +Console.WriteLine(await agent.RunAsync("What was the first joke I asked you to tell again?", session)); +chatHistory = provider?.GetMessages(session); Console.WriteLine($"\nChat history has {chatHistory?.Count} messages.\n");