-
Notifications
You must be signed in to change notification settings - Fork 302
Fix foundry agents connect serialization bug #1205
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
base: main
Are you sure you want to change the base?
Fix foundry agents connect serialization bug #1205
Conversation
Update changelog
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.
Pull Request Overview
This PR fixes a serialization bug in the Foundry agents connect functionality. When agent run steps include file search tool call results, Azure MCP was creating anonymous objects that couldn't be serialized. The fix introduces statically typed AgentFileSearchResult model class and replaces overloaded methods that accepted arbitrary object parameters with type-safe overloads accepting known serializable types (string, JsonElement, IDictionary<string, string?>).
Key changes:
- Created
AgentFileSearchResultandAgentFunctionCallContentmodel classes with proper JSON property attributes - Refactored
CreateRequestMessageandCreateResponseMessagemethods from local functions to static methods with type-safe overloads - Registered
List<AgentFileSearchResult>in the JSON serialization context for AOT compatibility
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| tools/Azure.Mcp.Tools.Foundry/src/Services/FoundryService.cs | Refactored message creation from local functions to static methods with multiple type-safe overloads; converted anonymous objects to statically typed AgentFileSearchResult instances |
| tools/Azure.Mcp.Tools.Foundry/src/Models/AgentFunctionCallContent.cs | Added new model class for function call content with JSON serialization attributes (currently unused) |
| tools/Azure.Mcp.Tools.Foundry/src/Models/AgentFileSearchResult.cs | Added new model class to represent file search results with proper JSON property names for serialization |
| tools/Azure.Mcp.Tools.Foundry/src/Commands/FoundryJsonContext.cs | Registered List<AgentFileSearchResult> type in JSON source generation context for AOT serialization support |
| [JsonSerializable(typeof(AgentsCreateResult))] | ||
| [JsonSerializable(typeof(ThreadCreateResult))] | ||
| [JsonSerializable(typeof(ThreadListResult))] | ||
| [JsonSerializable(typeof(ThreadGetMessagesResult))] |
Copilot
AI
Nov 17, 2025
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.
The AgentFileSearchResult type should be added to the JSON serialization context. While List<AgentFileSearchResult> is registered on line 67, the individual type needs to be registered as well for proper AOT serialization. Add [JsonSerializable(typeof(AgentFileSearchResult))] before line 67.
| [JsonSerializable(typeof(ThreadGetMessagesResult))] | |
| [JsonSerializable(typeof(ThreadGetMessagesResult))] | |
| [JsonSerializable(typeof(AgentFileSearchResult))] |
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.
Is Copilot right? I don't think it's necessary.
| }; | ||
| } | ||
|
|
||
| internal static Microsoft.Extensions.AI.ChatMessage CreateResponseMessage(string toolCallId, JsonElement result, RunStep step) |
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.
Do we need all these different static methods given FunctionResultContent's constructor is string callId, object? result). Cant we just have:
| internal static Microsoft.Extensions.AI.ChatMessage CreateResponseMessage(string toolCallId, JsonElement result, RunStep step) | |
| internal static Microsoft.Extensions.AI.ChatMessage CreateResponseMessage(string toolCallId, object? result, RunStep step) |
What does this PR do?
When the agent run steps include file search tool call result, Azure MCP creates an annonymous object that is not serializable and cause the failure. The fix is to create staticly typed objects instead and register this static type in the Json serialization context. I replaced the function that accepts aribtrary object argument with a few overloads that accept known serializable types to prevent us from passing non-serialzable objects to it.
GitHub issue number?
#1170
Pre-merge Checklist
servers/Azure.Mcp.Server/CHANGELOG.mdand/orservers/Fabric.Mcp.Server/CHANGELOG.mdfor product changes (features, bug fixes, UI/UX, updated dependencies)servers/Azure.Mcp.Server/README.mdand/orservers/Fabric.Mcp.Server/README.mddocumentationeng/scripts/Process-PackageReadMe.ps1. See Package README/servers/Azure.Mcp.Server/docs/azmcp-commands.mdand/or/docs/fabric-commands.md.\eng\scripts\Update-AzCommandsMetadata.ps1to update tool metadata in azmcp-commands.md (required for CI)ToolDescriptionEvaluatorand obtained a score of0.4or more and a top 3 ranking for all related test promptsconsolidated-tools.json/servers/Azure.Mcp.Server/docs/e2eTestPrompts.mdcrypto mining, spam, data exfiltration, etc.)/azp run mcp - pullrequest - liveto run Live Test Pipeline