XBag.AI.RunContext 0.1.0-preview.1

This is a prerelease version of XBag.AI.RunContext.
dotnet add package XBag.AI.RunContext --version 0.1.0-preview.1
                    
NuGet\Install-Package XBag.AI.RunContext -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" 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" Version="0.1.0-preview.1" />
                    
Directory.Packages.props
<PackageReference Include="XBag.AI.RunContext" />
                    
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 --version 0.1.0-preview.1
                    
#r "nuget: XBag.AI.RunContext, 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@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&version=0.1.0-preview.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=XBag.AI.RunContext&version=0.1.0-preview.1&prerelease
                    
Install as a Cake Tool

XBag.AI.RunContext

CI NuGet .NET 8 | 10 License: MIT

Typed per-request context for Microsoft.Extensions.AI tools. Your tools run deep inside FunctionInvokingChatClient, far from the HTTP request that knows who is calling. Put one typed value (user, tenant, token) on the request, and read it in any tool as RunContext<AppUser>.Current, or as a hidden tool parameter the model can neither see nor fill. A DI scope per tool call and an HTTP handler that stamps the current user's bearer token (for MCP and internal APIs, allowed origins only) come with it.

Package Contains
XBag.AI.RunContext (this one) RunContext<T>, WithRunContext for ChatOptions, BindRunContext<T>(), WithServiceScope(), RunContextBearerHandler<T>; depends on Microsoft.Extensions.AI
XBag.AI.RunContext.Agents Microsoft Agent Framework glue: AgentRunOptions.WithRunContext, AIAgentBuilder.UseRunContext<T>()

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: none; it is read, not inserted. The value rides on the request (ChatOptions.AdditionalProperties, which
               ChatClientAgent fills from AgentRunOptions) or on an AsyncLocal scope around the call, and is read
               INSIDE each tool call through FunctionInvokingChatClient.CurrentContext. Once per TOOL CALL (and per HTTP
               request for the bearer handler).

Install

dotnet add package XBag.AI.RunContext --prerelease

Example

A tool with a hidden AppUser parameter behind the real FunctionInvokingChatClient, buffered and streaming (this is ChatClientTests.cs, run in CI with a scripted model from XBag.AI.Testing):

public sealed record AppUser(string UserId, string Tenant, string AccessToken);

AIFunction myOrders = AIFunctionFactory.Create(
    (AppUser user, string durum) => $"{user.UserId} ({user.Tenant}): 3 {durum} sipariş",
    new AIFunctionFactoryOptions { Name = "siparislerim" }.BindRunContext<AppUser>());   // the model sees only "durum"

var model = new ScriptedChatClient()
    .CallTool("siparislerim", new { durum = "bekleyen" })
    .Reply(r => r.LastToolResult<string>()!);
IChatClient client = model.AsBuilder().UseFunctionInvocation().Build();

ChatOptions options = new ChatOptions { Tools = [myOrders] }.WithRunContext(murat);  // per request; the client is shared
ChatResponse response = await client.GetResponseAsync("Bekleyen siparişlerim neler?", options);

Assert.Equal("murat (İzmir Lojistik A.Ş.): 3 bekleyen sipariş", response.Text);

Or, without touching the options, a scope around the call (a streaming response must be consumed inside it):

using (RunContext<AppUser>.Begin(ayse))
{
    response = await client.GetStreamingResponseAsync("Teslim edilenler?", options).ToChatResponseAsync();
}

Calling an API or MCP server as the user

One long-lived HttpClient for everyone; each request gets the current user's token, and only the allowed origin ever sees it (BearerHandlerTests.cs runs 16 users concurrently through one agent):

using HttpClient http = new(new RunContextBearerHandler<AppUser>(
    async (user, ct) => await obo.GetTokenAsync(user, ct),          // your on-behalf-of exchange / cache
    [new Uri("https://api.izmir-lojistik.example")])
{
    InnerHandler = new SocketsHttpHandler { AllowAutoRedirect = false },
});

// An MCP client over the same HttpClient: new HttpClientTransport(transportOptions, http)

A DI scope per tool call

FunctionInvokingChatClient gives tools the provider it was built with, usually the root, where a scoped DbContext is refused (ValidateScopes) or shared by every request. WithServiceScope() opens a scope per call and disposes it after:

services.AddScoped(_ => new OrderDb(RunContext<AppUser>.GetRequired()));   // resolved inside the call: the user is there

AIFunction tool = AIFunctionFactory.Create(
    (IServiceProvider services, string durum) => services.GetRequiredService<OrderDb>().Count(durum),
    new AIFunctionFactoryOptions { Name = "siparis_say" }).WithServiceScope();

API

Type / member What it is
RunContext<T>.Current The current context or null: the one on the tool call's ChatOptions first, else the ambient scope
RunContext<T>.GetRequired() Same, throws InvalidOperationException when there is none
RunContext<T>.Begin(value) IDisposable scope (an AsyncLocal per T); nests and restores the outer value
ChatOptions.WithRunContext(value) Carries the value on this request's options
AdditionalPropertiesDictionary.SetRunContext(value) / GetRunContext<T>() The same, for any properties bag (key xbag.runcontext.<type>, value wrapped)
AIFunctionFactoryOptions.BindRunContext<T>() Parameters of type T are bound from the context and removed from the schema; no context → the call fails before the tool runs (or null for T? x = null)
AIFunction.WithServiceScope(services?) A fresh IServiceScope per call as AIFunctionArguments.Services
RunContextBearerHandler<T>(getToken, allowedOrigins) DelegatingHandler: Authorization: Bearer <getToken(context)> on allowed origins (HTTPS, or HTTP on loopback); RequireRunContext to refuse calls without a context

Several context types can live side by side (RunContext<AppUser>, RunContext<TenantSettings>); each has its own slot.

Security notes

  • The model cannot set the context. A bound parameter is not in the schema, and an argument the model sends under its name is ignored (tested).
  • The value never shows up in telemetry or cache keys. It is stored wrapped: OpenTelemetryChatClient with EnableSensitiveData puts every additional property on the span, and a record's ToString() would print the token there. The wrapper prints [run context: AppUser] and has no public members to serialize (tested with a real ActivityListener).
  • Tokens go to allowed origins only, compared on scheme, host and port. Turn off automatic redirects on the inner handler.

What this is not

  • Not authentication. ASP.NET Core decides who the user is; this carries the answer to the tools.
  • No token acquisition. getToken is yours (MSAL on-behalf-of, a forwarded JWT, a static key). A cached OBO provider is on the roadmap.
  • No flow across durable boundaries. The context flows with the async call; it does not survive a workflow checkpoint, a background response resumed later, or a hop to another process.
  • Not an authorization policy for tools (which user may call which tool).

If Microsoft ships a typed run context

Microsoft's stance in Microsoft.Extensions.AI 10.10 / Agent Framework 1.24 is "several supported patterns" (closures, AIFunctionArguments.Context / .Services, CurrentContext.Options.AdditionalProperties; stephentoub in microsoft/agent-framework#1193), and there is no first-class MCP header API in .NET (#1631; the official sample Agent_MCP_PerRun_AuthHeaders hand-writes the AsyncLocal + DelegatingHandler this package packages). Proposals for a first-class request context (#7852, #7729) are open. If one ships, RunContext<T> will read from it and keep the typed binding and the origin-checked handler; if a header_provider-style MCP API lands, the handler shrinks to a shim.

Versions

Depends on Microsoft.Extensions.AI 10.10.0 (for FunctionInvokingChatClient.CurrentContext). Tested on .NET 8 and .NET 10; trimming/AOT analyzers are clean. 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 (1)

Showing the top 1 NuGet packages that depend on XBag.AI.RunContext:

Package Downloads
XBag.AI.RunContext.Agents

Microsoft Agent Framework glue for XBag.AI.RunContext: AgentRunOptions.WithRunContext and AIAgentBuilder.UseRunContext give every agent run a typed user / tenant / credential context that its tools, context providers, sub-agents and HTTP/MCP calls can read, buffered and streaming. Not affiliated with Microsoft.

GitHub repositories

This package is not used by any popular GitHub repositories.

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