.NET: Add Foundry hosted session client samples - #8227
.NET: Add Foundry hosted session client samples#8227Roger Barreto (rogerbarreto) wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Closed stdin currently causes infinite loops that prevent hosted-session cleanup.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Adds .NET samples for explicit Foundry hosted-session management and multi-user conversation isolation.
Changes:
- Adds hosted-session lifecycle and delegated-user isolation samples.
- Documents pooling, permissions, and data partitioning.
- Removes obsolete manual Foundry feature headers.
File summaries
| File | Description |
|---|---|
UserIsolationAgent/UserIsolationAgent.csproj |
Configures the isolation sample. |
UserIsolationAgent/README.md |
Documents delegated identity and permissions. |
UserIsolationAgent/Program.cs |
Implements shared-session user isolation. |
UserIsolationAgent/.env.example |
Provides required settings. |
Using-Samples/README.md |
Lists the new samples. |
FoundryHostedSession/README.md |
Explains sessions, conversations, and pooling. |
FoundryHostedSession/Program.cs |
Implements explicit session lifecycle management. |
FoundryHostedSession/FoundryHostedSession.csproj |
Configures the session sample. |
FoundryHostedSession/.env.example |
Provides required settings. |
Hosted-ToolboxMcpSkills/Program.cs |
Removes the obsolete Toolbox header. |
Hosted-AgentSkills/README.md |
Documents SDK-managed feature opt-in. |
Hosted-AgentSkills/Program.cs |
Uses the standard administration client. |
InvokeFoundryToolboxMcp/Program.cs |
Removes custom header policy code. |
InvokeFoundryToolboxMcp.yaml |
Removes manual Toolbox headers. |
Claw_Step04/FoundrySkills.cs |
Removes the obsolete MCP header. |
Claw_Step03/FoundrySkills.cs |
Removes the obsolete MCP header. |
Agent_Step26/Program.cs |
Removes the obsolete MCP header. |
Agent_Step25/Program.cs |
Removes manual header configuration. |
agent-framework-dotnet.slnx |
Registers both new sample projects. |
Review details
Suppressed comments (1)
dotnet/samples/04-hosting/FoundryHostedAgents/responses/Using-Samples/UserIsolationAgent/Program.cs:77
- End-of-input at the message prompt also returns
null. Continuing here sends execution back to the user prompt, where closed stdin causes an infinite loop. Exit the loop onnullso the shared hosted session is cleaned up.
string? input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input))
{
continue;
}
- Files reviewed: 19/19 changed files
- Comments generated: 4
- Review effort level: Balanced
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
MAF Automated Review — Iteration 1
Result: Findings reported
Scope: full PR (1 commit(s)): e260200a7b4c
Model: gpt-5.6-sol-fast
Overview
The PR clearly separates hosted sandbox identity from conversation continuity, pins each logical user to a distinct Agent Framework session, and documents the trust and data-partitioning responsibilities of a middle tier. Session activation is bounded by terminal-state and timeout checks, while normal exceptions flow through explicit hosted-session deletion. One residual lifecycle defect remains: EOF is treated as blank input, so the clients can spin indefinitely instead of reaching cleanup.
Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
1 verified finding remained after source verification (1 medium) across 1 file. Details are attached to the affected lines below.
Affected areas: dotnet/samples/04-hosting/FoundryHostedAgents/responses/Using-Samples/FoundryHostedSession/Program.cs
Vincent Biret (baywet)
left a comment
There was a problem hiding this comment.
I'm not aware that Toolboxes and Skills are out of preview yet. Should we wait to remove the headers from the samples until there's a public announcement?
| Console.ForegroundColor = ConsoleColor.Green; | ||
| Console.Write("User id> "); | ||
| Console.ResetColor(); |
There was a problem hiding this comment.
Consider adding some very explicit warnings that this is not something people should do in production, but rather just showing in this example what happens when you have different users.
| var runOptions = new ChatClientAgentRunOptions( | ||
| new ChatOptions().WithFoundryHostedAgentUserIdentity(userId)); |
There was a problem hiding this comment.
This leaks a bit out of the abstraction. E.g. each time we call the agent, we need to know what kind of agent it is, and that we can set the user id here.
For the future, it would be nice to consider an option where we don't need that. Maybe setting the user id in the session once. It would still be leaky, but at least it's only when you create the session rather than on each run. Or maybe allowing a dev to attach middleware/decorator to the client before constructing the Agent that can pull the user id from ambient context (e.g. httpRequest headers) and attach it on each call.
Motivation & Context
Hosted agent client samples do not currently show how an application can explicitly create and reuse a Foundry hosted session, or how a trusted middle tier can share hosted session capacity while preserving separate user conversations.
These examples make the distinction between hosted sandbox state and conversation history concrete. They also document the permission required to delegate application user identity to a hosted agent.
Description & Review Guide
FoundryHostedSession, a client sample that creates a hosted session throughAgentAdministrationClient, waits for it to become active, attaches it to an Agent Framework session, reuses it for multiple turns, and deletes it on exit.UserIsolationAgent, a client sample that shares one hosted session across application users while maintaining a separateAgentSessionand delegatedx-ms-user-identityvalue for each user.Foundry-Featuresheaders from Skills and Toolbox samples. Current Foundry APIs no longer require the Toolbox header, and the SDK applies remaining feature opt-ins internally.Related Issue
Fixes #8226
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and the title prefix in sync automatically.