XBag.AI.RunContext.Agents 0.1.0-preview.1

This is a prerelease version of XBag.AI.RunContext.Agents.
dotnet add package XBag.AI.RunContext.Agents --version 0.1.0-preview.1
                    
NuGet\Install-Package XBag.AI.RunContext.Agents -Version 0.1.0-preview.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="XBag.AI.RunContext.Agents" Version="0.1.0-preview.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="XBag.AI.RunContext.Agents" Version="0.1.0-preview.1" />
                    
Directory.Packages.props
<PackageReference Include="XBag.AI.RunContext.Agents" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add XBag.AI.RunContext.Agents --version 0.1.0-preview.1
                    
#r "nuget: XBag.AI.RunContext.Agents, 0.1.0-preview.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package XBag.AI.RunContext.Agents@0.1.0-preview.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=XBag.AI.RunContext.Agents&version=0.1.0-preview.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=XBag.AI.RunContext.Agents&version=0.1.0-preview.1&prerelease
                    
Install as a Cake Tool

XBag.AI.RunContext.Agents

CI NuGet .NET 8 | 10 License: MIT

Microsoft Agent Framework glue for XBag.AI.RunContext: give every agent run a typed user / tenant / credential context, and read it as RunContext<AppUser>.Current from the run's tools, context providers, chat middleware, sub-agents and HTTP/MCP calls. One singleton agent, many users at once, buffered and streaming.

Part of the XBag family of .NET packages by Murat Ay, the author of XBag, a .NET process-orchestration and decisioning platform. Community package, not affiliated with Microsoft.

Pipeline slot: agent layer, AIAgentBuilder.UseRunContext<T>() (DelegatingAIAgent), once per RUN: opens the context scope
               around the whole run and re-opens it around every streaming step. Without the decorator,
               AgentRunOptions.WithRunContext still reaches TOOLS (ChatClientAgent copies run options into ChatOptions).

Install

dotnet add package XBag.AI.RunContext.Agents --prerelease

Example

24 users run one agent concurrently; each tool call sees its own user (this is AgentTests.cs, run in CI on the real ChatClientAgent, buffered and streaming, with the context from the run options, a resolver, or the caller's scope):

AIFunction whoAmI = AIFunctionFactory.Create(
    async (AppUser? user = null) =>
    {
        await Task.Delay(Random.Shared.Next(1, 10));
        return user is null ? "kimse" : $"{user.UserId}@{user.Tenant}";
    },
    new AIFunctionFactoryOptions { Name = "ben_kimim" }.BindRunContext<AppUser>());

AIAgent agent = model.AsAIAgent(new ChatClientAgentOptions { ChatOptions = new() { Tools = [whoAmI] } })
    .AsBuilder()
    .UseRunContext<AppUser>()
    .Build();

string[] answers = await Task.WhenAll(users.Select(user => Task.Run(async () =>
    (await agent.RunAsync("Ben kimim?", options: new AgentRunOptions().WithRunContext(user))).Text)));

Assert.Equal(users.Select(u => $"Sen {u.UserId}@{u.Tenant}"), answers);

When a hosting protocol (AG-UI, A2A, Responses) starts the run, you do not build its options; resolve the user instead:

agent = agent.AsBuilder()
    .UseRunContext<AppUser>(run => AppUser.From(httpContextAccessor.HttpContext))   // or run.Session?.StateBag
    .Build();

Where the context is visible

Reader WithRunContext only + UseRunContext<T>() Caller's RunContext<T>.Begin
Tools (BindRunContext, RunContext<T>.Current), HTTP handlers they call yes yes yes
AIContextProviders, chat middleware outside tool calls no yes yes
Sub-agent via agent.AsAIFunction() yes (Agent Framework copies the properties) yes yes
Sub-agent a tool runs by hand (inner.RunAsync(q)) no yes yes
Streaming yes yes (re-opened per step) yes, if consumed inside the scope

Proven in AgentTests.cs, including each no.

API

Type / member What it is
AgentRunOptions.WithRunContext(value) Carries the value on this run's options
AIAgentBuilder.UseRunContext<T>(resolve?) Adds RunContextAgent<T>
RunContextAgent<T> : DelegatingAIAgent Per run: the value from the run options, else resolve(AgentRunContext), else none (an outer context still applies); scope around the run and around every streaming step

What this is not

  • No ASP.NET Core registration (services.AddRunContext<T>(http => ...)) yet: use a resolver with IHttpContextAccessor. See the roadmap.
  • No flow into durable work: a workflow resumed from a checkpoint or a background response polled later starts without it.

If Microsoft ships a typed run context

See XBag.AI.RunContext. AIAgent.CurrentRunContext (stable in 1.24.0) already carries Agent, Session, RequestMessages and RunOptions; a typed slot on it would make WithRunContext a one-line adapter and the streaming decorator unnecessary.

Versions

Depends on Microsoft.Agents.AI 1.24.0 (for AIAgentBuilder) and XBag.AI.RunContext. Tested on .NET 8 and .NET 10. Status: 0.1.0-preview.1, the API may change before 1.0.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0-preview.1 83 10/11/2026