Skip to content

.NET: Add Foundry hosted session client samples - #8227

Open
Roger Barreto (rogerbarreto) wants to merge 3 commits into
microsoft:mainfrom
rogerbarreto:hosted-foundry-session-sample
Open

.NET: Add Foundry hosted session client samples#8227
Roger Barreto (rogerbarreto) wants to merge 3 commits into
microsoft:mainfrom
rogerbarreto:hosted-foundry-session-sample

Conversation

@rogerbarreto

Copy link
Copy Markdown
Member

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

  • What are the major changes?
    • Add FoundryHostedSession, a client sample that creates a hosted session through AgentAdministrationClient, waits for it to become active, attaches it to an Agent Framework session, reuses it for multiple turns, and deletes it on exit.
    • Add UserIsolationAgent, a client sample that shares one hosted session across application users while maintaining a separate AgentSession and delegated x-ms-user-identity value for each user.
    • Document hosted sessions, conversations, session pools, user owned data partitioning, and the custom impersonation data action required by the calling principal.
    • Remove obsolete manual Foundry-Features headers from Skills and Toolbox samples. Current Foundry APIs no longer require the Toolbox header, and the SDK applies remaining feature opt-ins internally.
  • What is the impact of these changes?
    • Developers get runnable examples for explicit hosted session lifecycle management and delegated user isolation.
    • Existing samples use the current Foundry API shape with less custom pipeline and header configuration.
  • What do you want reviewers to focus on?
    • Confirm that the samples clearly separate hosted sandbox identity from conversation continuity.
    • Confirm that the user isolation guidance accurately describes trusted middle tier responsibilities and container owned data partitioning.
    • Confirm that removal of manual feature headers matches the current Foundry SDK and Toolbox endpoint contracts.

Related Issue

Fixes #8226

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (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.

@agent-framework-automation agent-framework-automation Bot added documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs .NET Usage: [Issues, PRs], Target: .Net workflows Usage: [Issues, PRs], Target: Workflows labels Sep 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 on null so 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.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@baywet Vincent Biret (baywet) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment on lines +60 to +62
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("User id> ");
Console.ResetColor();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +101 to +102
var runOptions = new ChatClientAgentRunOptions(
new ChatOptions().WithFoundryHostedAgentUserIdentity(userId));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs .NET Usage: [Issues, PRs], Target: .Net workflows Usage: [Issues, PRs], Target: Workflows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: Add hosted session and delegated user identity samples

4 participants