Skip to content

Conversation

@JasonYeMSFT
Copy link
Member

@JasonYeMSFT JasonYeMSFT commented Nov 17, 2025

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

  • Required for All PRs
    • Read contribution guidelines
    • PR title clearly describes the change
    • Commit history is clean with descriptive messages (cleanup guide)
    • Added comprehensive tests for new/modified functionality
    • Updated servers/Azure.Mcp.Server/CHANGELOG.md and/or servers/Fabric.Mcp.Server/CHANGELOG.md for product changes (features, bug fixes, UI/UX, updated dependencies)
  • For MCP tool changes:
    • One tool per PR: This PR adds or modifies only one MCP tool for faster review cycles
    • Updated servers/Azure.Mcp.Server/README.md and/or servers/Fabric.Mcp.Server/README.md documentation
    • Validate README.md changes using script at eng/scripts/Process-PackageReadMe.ps1. See Package README
    • Updated command list in /servers/Azure.Mcp.Server/docs/azmcp-commands.md and/or /docs/fabric-commands.md
    • Run .\eng\scripts\Update-AzCommandsMetadata.ps1 to update tool metadata in azmcp-commands.md (required for CI)
    • For new or modified tool descriptions, ran ToolDescriptionEvaluator and obtained a score of 0.4 or more and a top 3 ranking for all related test prompts
    • For tools with new names, including new tools or renamed tools, update consolidated-tools.json
    • For new tools associated with Azure services or publicly available tools/APIs/products, add URL to documentation in the PR description
  • Extra steps for Azure MCP Server tool changes:
    • Updated test prompts in /servers/Azure.Mcp.Server/docs/e2eTestPrompts.md
    • 👉 For Community (non-Microsoft team member) PRs:
      • Security review: Reviewed code for security vulnerabilities, malicious code, or suspicious activities before running tests (crypto mining, spam, data exfiltration, etc.)
      • Manual tests run: added comment /azp run mcp - pullrequest - live to run Live Test Pipeline

Update changelog
@JasonYeMSFT JasonYeMSFT requested a review from a team as a code owner November 17, 2025 22:46
Copilot finished reviewing on behalf of JasonYeMSFT November 17, 2025 22:47
Copy link
Contributor

Copilot AI left a 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 AgentFileSearchResult and AgentFunctionCallContent model classes with proper JSON property attributes
  • Refactored CreateRequestMessage and CreateResponseMessage methods 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))]
Copy link

Copilot AI Nov 17, 2025

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.

Suggested change
[JsonSerializable(typeof(ThreadGetMessagesResult))]
[JsonSerializable(typeof(ThreadGetMessagesResult))]
[JsonSerializable(typeof(AgentFileSearchResult))]

Copilot uses AI. Check for mistakes.
Copy link
Member Author

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.

@JasonYeMSFT JasonYeMSFT self-assigned this Nov 17, 2025
};
}

internal static Microsoft.Extensions.AI.ChatMessage CreateResponseMessage(string toolCallId, JsonElement result, RunStep step)
Copy link
Contributor

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:

Suggested change
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)

@joshfree joshfree moved this from Untriaged to In Progress in Azure MCP Server Nov 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

2 participants