Skip to content

IAgentExecutionContextAccessor

NexusLabs.Needlr.AgentFramework

NexusLabs.Needlr.AgentFramework.Context

IAgentExecutionContextAccessor Interface

Provides access to the ambient IAgentExecutionContext for the current async flow. Follows the IHttpContextAccessor pattern: registered as a singleton, backed by System.Threading.AsyncLocal<>, so concurrent orchestrations see their own contexts.

public interface IAgentExecutionContextAccessor

Remarks

Tools MUST NOT take identity fields (UserId, OrchestrationId, etc.) as method parameters. They read them from Current. If Current is null, the tool should throw — there is no default context, no anonymous user.

Trusted callers (API middleware, workflow engine, scenario harness) establish a context by calling BeginScope(IAgentExecutionContext) and wrapping agent execution in the returned disposable:

using (accessor.BeginScope(context))
{
    await agent.RunAsync(prompt);
}

Properties

IAgentExecutionContextAccessor.Current Property

The current execution context for this async flow, or null if called outside any established scope.

NexusLabs.Needlr.AgentFramework.Context.IAgentExecutionContext? Current { get; }

Property Value

IAgentExecutionContext

Methods

IAgentExecutionContextAccessor.BeginScope(IAgentExecutionContext) Method

Establishes a new execution context for the duration of the returned scope. When the scope is disposed, the previous context (if any) is restored.

System.IDisposable BeginScope(NexusLabs.Needlr.AgentFramework.Context.IAgentExecutionContext context);

Parameters

context IAgentExecutionContext

Returns

System.IDisposable