CxAgent.Core
The agent loop without a terminal.
Everything under cxagent's UI ships as a package: a conversation, the agent running it, tool execution, sub-agent delegation, permissions, MCP, plugins and resumable stores. You supply where the text goes; there is no UI dependency. cxagent is one consumer of it, and a Spectre.Console front end is another, in about a hundred lines.
dotnet add package CxAgent.Core
Targets net8.0, net9.0 and net10.0. MIT licensed.
The mental model
A session is a conversation. An agent is the loop that runs one turn of it. You supply where words go. Everything else follows from those three sentences.
You construct far less than people expect. SessionManager.Create builds what
sessions share — the resume database, the usage archive, the log directory, the permission rules
store, the command registry — and Open builds everything per-session. You never
construct an Agent or any of the built-in tools; the only tools you write are your own.
An app is four calls
using CxAgent.Core.Llm;
using CxAgent.Core.Permissions;
using CxAgent.Core.Sessions;
using CxAgent.Core.Storage;
var workingDirectory = Directory.GetCurrentDirectory(); // the permission boundary
var resolution = new AgentConfig // which model
{
Models = { ["local"] = new(ProviderKind.OpenAiCompatible, "qwen3.6-35b")
{ BaseUrl = "http://localhost:8771/v1" } },
}.Resolve();
using var manager = SessionManager.Create(new AppPaths("/var/lib/myapp"), // where its stores live
buildGate: store => PermissionDecider.WithPrompt(store, notice: null,
promptHook: (request, offerTrust, ct) => // how a human is asked
{
Console.Write($"{request.Kind}: {request.Shown} — allow once? [y/N] ");
return Task.FromResult(Console.ReadLine() == "y" ? PermissionChoice.Once : PermissionChoice.Deny);
}));
var session = manager.Open(workingDirectory, resolution, new SessionPorts
{
Observer = new ConsoleSink(), // where words go
ToolObserver = new NullToolSink(), // where tool activity goes
Policy = new PermissionPolicy(workingDirectory, manager.Rules!), // what requests are judged against
});
if (session.Submit("summarise this folder") is Session.SubmitOutcome.Started started)
await started.Turn;
The two observers it names are yours, and short:
using CxAgent.Core.Commands;
using CxAgent.Core.Llm;
using CxAgent.Core.Models;
using CxAgent.Core.Sessions;
sealed class ConsoleSink : ISessionObserver
{
public void UserTurnAdded(ChatMessageId id, string text) { }
public void AssistantTurnBegan(ChatMessageId id) { }
public void AssistantTextAppended(ChatMessageId id, string token) => Console.Write(token);
public void AssistantThinkingAppended(ChatMessageId id, string text) { }
public void AssistantThinkingEnded(ChatMessageId id, ThinkingSummary summary) { }
public void AssistantTurnEnded(ChatMessageId id) => Console.WriteLine();
public void AssistantLabelled(ChatMessageId id, string header) { }
public void Said(Message message) => Console.WriteLine(message.Text);
}
sealed class NullToolSink : IToolObserver
{
public void ToolsChanged(IReadOnlyList<Job> jobs) { }
public void ToolUpdated(Job job) { }
public void ToolProgressed(Job job) { }
public void ToolResourcesSampled(string jobId, ResourceSnapshot snapshot) { }
public void ToolOutputAppended(string jobId, string delta) { }
}
A model can come from config.json instead of code:
ConfigResolver.Resolve(new AppPaths(dir), env, useMock: false) returns the same
ResolvedConfig. ToolAgent
is this program run end to end, with tools of its own.
Submit is synchronous. It returns a receipt rather than a task, because each outcome
is a different thing for a caller to do: Started (a turn began — await it),
Queued (a turn was already running, and this joined it), Handled (the text
named a command, which has run), NoAgent (no model is wired), and two that only other
kinds of caller meet — Refused and Aside.
What is actually yours to write
| You supply | Required? | What it is |
|---|---|---|
ISessionObserver | yes | where words go — SessionPorts.Observer |
IToolObserver | yes | tool activity — SessionPorts.ToolObserver; a no-op is fine |
| a model | to run a turn | a ResolvedConfig from code, from cxagent's own config.json, or a test double. Without one, Submit answers NoAgent |
a buildGate with a prompt hook | to gate tools | how a human is asked. Leave it out and nothing is gated |
a PermissionPolicy | with a gate | the folder and edit mode a request is judged against — SessionPorts.Policy. A gate without one refuses everything |
your own IAgentTools | no | tools beside the built-ins — SessionPorts.Tools |
The text an observer receives is markdown, and Core escapes what it interpolates — so a path like
my_test_file.cs arrives with its underscores intact. Core writes no colour tags, so
there is nothing to strip.
Permissions
The engine is in here. The only thing left to you is asking a human. Trust per
folder, the working-directory boundary, edit modes, stored rules, the read-only command list, the
Auto classifier and the wrapper that gates every tool are all Core. What Core cannot do
is put a question on a screen, so you supply one function — the prompt hook: given this request,
what does the human say?
It reaches Core through buildGate, which SessionManager.Create calls with
the rules store it builds; hand that store and your prompt hook to
PermissionDecider.WithPrompt, and the decider it returns is the gate. Two halves, and one
without the other is silent. The gate gives Core a way to ask; SessionPorts.Policy gives
it the folder and edit mode to judge against. Omit the policy and every request is refused rather
than guessed at; pass no buildGate at all and nothing is gated — a legitimate headless
arrangement, but a choice rather than something to inherit by forgetting.
Tools
The agent gets read_file, write_file, replace_in_file,
glob, grep, run_shell, http_request,
web_fetch and web_search without you registering anything, plus
todowrite, ask_user and skill, memory when you set
AgentConfig.Memory, and — where the agent may delegate — agent to start a
sub-agent, agent_send to message one, and task_list,
task_output and task_stop for those running in the background. With a gate
wired, Core puts it in front of every tool that acts — the built-ins, MCP and plugin tools, and your
own — at construction, so none of them runs around it.
http_request and web_fetch are one HTTP executor used two ways.
http_request is for an API: any method, headers and a body, and the response back as the
server sent it. web_fetch takes only a URL and reads a page: it strips markup, scripts,
styles and navigation and hands back the readable text, so a page costs the model roughly what its
words cost. web_search finds the pages web_fetch reads.
You can add your own — a deploy(env), a queryOurWarehouse(sql) — by
writing an IAgentTool and listing it in SessionPorts.Tools. They are
offered beside the built-ins and, with a gate, wrapped in it on the way through; a name a built-in
already has stays the built-in's while that built-in is offered. Or offer fewer: a
ToolSelection narrows what an agent is handed, and a withheld tool is refused if called by
name rather than merely hidden.
ToolSelection = new ToolSelection([Tool.Inherited, Tool.Not.RunShell]),
Reference
- api.md — every public type, configuration, and tool selection
- tools.md
— injecting tools of your own: the
IAgentToolinterface and the two gates - The plugin contract — tools loaded from a plugin at run time, .NET or native
- The examples directory — five runnable apps, with what each one is for
The examples: ToolAgent (a console app with an injected tool), SpectreAgent (the same, with a rendered UI), ReadOnlyAgent (taking tools away — a selection you can watch hold), CalculatorPlugin and CalculatorAbiPlugin (a plugin end to end, managed and in one file of C).