JKToolKit.CodexSDK
0.0.24
dotnet add package JKToolKit.CodexSDK --version 0.0.24
NuGet\Install-Package JKToolKit.CodexSDK -Version 0.0.24
<PackageReference Include="JKToolKit.CodexSDK" Version="0.0.24" />
<PackageVersion Include="JKToolKit.CodexSDK" Version="0.0.24" />
<PackageReference Include="JKToolKit.CodexSDK" />
paket add JKToolKit.CodexSDK --version 0.0.24
#r "nuget: JKToolKit.CodexSDK, 0.0.24"
#:package JKToolKit.CodexSDK@0.0.24
#addin nuget:?package=JKToolKit.CodexSDK&version=0.0.24
#tool nuget:?package=JKToolKit.CodexSDK&version=0.0.24
JKToolKit.CodexSDK (core)
JKToolKit.CodexSDK is a .NET client library that wraps the Codex CLI as a local subprocess and provides a strongly-typed, streaming-first API for:
- Starting Codex sessions (
codex exec) and streaming the resulting JSONL session log as typed .NET events - Resuming/attaching to existing sessions by session id / log path
- Running non-interactive reviews (
codex review) through the same process-launch infrastructure
This package also includes two stdio JSON-RPC integrations:
JKToolKit.CodexSDK.AppServer(codex app-server)JKToolKit.CodexSDK.McpServer(codex mcp-server)
Docs live under docs/:
What This Library Is (and isn’t)
It is:
- A local CLI wrapper: it starts
codexas a process and interacts with it through stdin/stdout/stderr and local files. - Streaming-first: it turns the Codex JSONL log into
IAsyncEnumerable<T>of typed events. - Testable: core dependencies are abstracted (
IFileSystem,ICodexPathProvider,ICodexProcessLauncher, …).
It is not:
- A general “OpenAI HTTP API” client.
- A full MCP client library (that’s intentionally minimal and scoped in
JKToolKit.CodexSDK.McpServer).
Core Concept
When you start a session via codex exec, Codex writes a JSONL session log to a file under the Codex sessions directory (commonly %USERPROFILE%\.codex\sessions on Windows).
JKToolKit.CodexSDK:
- Launches
codex execas a child process. - Captures the session id from process output.
- Resolves the JSONL log file for that session id.
- Tails the file as it grows (like
tail -f) and parses each JSON line into a typed event model.
This gives you a stable, .NET-native streaming pipeline even if Codex outputs “human text” to stdout/stderr.
How It Works (Pipeline)
For a live session:
CodexClient.StartSessionAsync(...)ICodexProcessLauncherstarts the process (codex exec ... -)ICodexSessionLocatorfinds the session JSONL fileIJsonlTaileryields appended JSONL linesIJsonlEventParsermaps JSONL records to typed event models- Your code consumes events via
await foreach
For a resumed session:
CodexClient.ResumeSessionAsync(...)ICodexSessionLocatorfinds the session JSONL file (by id or path)- Same tail+parse pipeline
Key Types
JKToolKit.CodexSDK.Public.CodexClient: main entry pointJKToolKit.CodexSDK.Public.CodexSessionHandle: a live or historical session handle (IAsyncDisposable)JKToolKit.CodexSDK.Public.EventStreamOptions: controls event filtering/stream optionsJKToolKit.CodexSDK.Public.Models.*: strongly-typed event models (SessionMetaEvent,ResponseItemEvent, …)
Getting Started
Prerequisites
- .NET 10 SDK
- Codex CLI installed (
codex/codex.cmdon PATH)
Start a session and stream events
using JKToolKit.CodexSDK.Public;
using JKToolKit.CodexSDK.Public.Models;
await using var client = new CodexClient(new CodexClientOptions());
var options = new CodexSessionOptions("<repo-path>", "Write a hello world program")
{
Model = CodexModel.Gpt52Codex,
ReasoningEffort = CodexReasoningEffort.Medium
};
await using var session = await client.StartSessionAsync(options);
await foreach (var evt in session.GetEventsAsync(EventStreamOptions.Default))
{
switch (evt)
{
case AgentMessageEvent msg:
Console.WriteLine(msg.Content);
break;
case ResponseItemEvent item when item.Payload.Message is { } m:
Console.WriteLine(string.Join("", m.TextParts));
break;
}
}
Run a non-interactive code review (codex review)
var review = await client.ReviewAsync(new CodexReviewOptions("<repo-path>")
{
CommitSha = "<sha>",
Prompt = "Focus on correctness, security, and performance."
});
Console.WriteLine(review.StandardOutput);
Dependency Injection (Optional)
The core library can register defaults via:
services.AddCodexClient();
This wires up:
- Path resolution (
DefaultCodexPathProvider) - Process launching (
CodexProcessLauncher) - JSONL tailing/parsing (
JsonlTailer,JsonlEventParser)
Extensibility / Forward Compatibility
Codex evolves quickly. JKToolKit.CodexSDK is designed so that:
- Unknown event shapes won’t break streaming; raw JSON is preserved where helpful.
- “Value object” model identifiers (e.g.
CodexModel) accept arbitrary strings.
To add support for new response_item payloads, extend the normalization logic in:
src/JKToolKit.CodexSDK/Infrastructure/JsonlEventParser.cs
Troubleshooting
- Session log not found: ensure Codex created
%USERPROFILE%\.codex\sessionsand that the session id was captured correctly. - Process launch fails: validate
codex --versionworks andCodexClientOptions.CodexExecutablePathif overridden. - No events streaming: confirm Codex is producing JSONL session logs for your command; check the resolved log file path.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.