ClaudeAgentSdk.CSharp 0.1.0-preview.2

This is a prerelease version of ClaudeAgentSdk.CSharp.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package ClaudeAgentSdk.CSharp --version 0.1.0-preview.2
                    
NuGet\Install-Package ClaudeAgentSdk.CSharp -Version 0.1.0-preview.2
                    
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="ClaudeAgentSdk.CSharp" Version="0.1.0-preview.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ClaudeAgentSdk.CSharp" Version="0.1.0-preview.2" />
                    
Directory.Packages.props
<PackageReference Include="ClaudeAgentSdk.CSharp" />
                    
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 ClaudeAgentSdk.CSharp --version 0.1.0-preview.2
                    
#r "nuget: ClaudeAgentSdk.CSharp, 0.1.0-preview.2"
                    
#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 ClaudeAgentSdk.CSharp@0.1.0-preview.2
                    
#: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=ClaudeAgentSdk.CSharp&version=0.1.0-preview.2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=ClaudeAgentSdk.CSharp&version=0.1.0-preview.2&prerelease
                    
Install as a Cake Tool

Claude Agent SDK for C#

NuGet .NET License

A C# SDK for integrating with Claude Code CLI, enabling you to build AI-powered applications with Claude.

Note: This project is a C# port of the official claude-agent-sdk-python.

日本語版 README

Overview

Claude Agent SDK for C# provides two main APIs for interacting with Claude Code:

  • Query.RunAsync() / ClaudeAgent.QueryAsync() - Stateless, one-shot queries
  • ClaudeSDKClient - Stateful, interactive conversations with full control

Key Features

  • Two flexible APIs for different use cases
  • MCP (Model Context Protocol) support for custom tools
  • Custom agent definitions
  • Hooks for event handling (PreToolUse, PostToolUse, etc.)
  • Tool permission control
  • Streaming support with IAsyncEnumerable

Prerequisites

Installation

NuGet Package

dotnet add package ClaudeAgentSdk.CSharp

Or via Package Manager:

Install-Package ClaudeAgentSdk.CSharp

From Source

git clone https://github.com/harayuu9/claude-agent-sdk-csharp.git
cd claude-agent-sdk-csharp
dotnet build

Quick Start

Simple Query

using ClaudeAgentSdk.CSharp;

// One-shot query
await foreach (var message in Query.RunAsync("What is 2 + 2?"))
{
    if (message is AssistantMessage assistantMessage)
    {
        foreach (var block in assistantMessage.Content)
        {
            if (block is TextBlock textBlock)
                Console.WriteLine(textBlock.Text);
        }
    }
}

Interactive Conversation

using ClaudeAgentSdk.CSharp;

await using var client = new ClaudeSDKClient();

// Start conversation
await client.ConnectAsync("What is the capital of France?");
await foreach (var msg in client.ReceiveResponseAsync())
{
    // Process response
}

// Follow-up question
await client.QueryAsync("What's its population?");
await foreach (var msg in client.ReceiveResponseAsync())
{
    // Process response
}

Features

Options

Configure the SDK behavior with ClaudeAgentOptions:

var options = new ClaudeAgentOptions
{
    SystemPrompt = "You are a helpful Python expert.",
    Cwd = "/path/to/project",
    AllowedTools = ["Read", "Write", "Bash"],
    PermissionMode = PermissionMode.AcceptEdits,
    MaxTurns = 10
};

await foreach (var message in Query.RunAsync("Create a web server", options))
{
    // ...
}

MCP Tools

Define custom tools using the Model Context Protocol:

// Define a tool
var addTool = SdkMcpTool.Create<CalcArgs>(
    "add",
    "Add two numbers",
    async args => SdkMcpToolResult.FromText($"Result: {args.A + args.B}"));

// Create an MCP server
var server = SdkMcpServer.Create("calculator", "1.0.0", [addTool]);

var options = new ClaudeAgentOptions
{
    McpServers = new Dictionary<string, McpServerConfig>
    {
        ["calc"] = server
    }
};

Or use the [Tool] attribute:

public class CalculatorTools
{
    [Tool("add", "Add two numbers")]
    public async Task<SdkMcpToolResult> Add(CalcArgs args)
    {
        return SdkMcpToolResult.FromText($"Result: {args.A + args.B}");
    }
}

var server = SdkMcpServer.FromType<CalculatorTools>();

Custom Agents

Define specialized agents for specific tasks:

var options = new ClaudeAgentOptions
{
    Agents = new Dictionary<string, AgentDefinition>
    {
        ["code-reviewer"] = new AgentDefinition
        {
            Description = "Reviews code for issues and best practices",
            Prompt = "You are a code reviewer. Analyze code for bugs, security issues, and style.",
            Tools = ["Read", "Grep", "Glob"],
            Model = AgentModel.Sonnet
        }
    }
};

Hooks

Handle events during execution:

var options = new ClaudeAgentOptions
{
    Hooks = new Dictionary<HookEvent, List<HookMatcher>>
    {
        [HookEvent.PreToolUse] =
        [
            new HookMatcher
            {
                Matcher = "Bash",
                Hooks = [ValidateBashCommandAsync]
            }
        ]
    }
};

async Task<HookResult> ValidateBashCommandAsync(HookInput input)
{
    var command = input.ToolInput["command"]?.ToString();
    if (command?.Contains("rm -rf") == true)
        return HookResult.Block("Dangerous command blocked");
    return HookResult.Allow();
}

Tool Permission Control

Fine-grained control over tool execution:

var options = new ClaudeAgentOptions
{
    CanUseTool = async (callback) =>
    {
        if (callback.ToolName == "Bash")
        {
            var command = callback.Input["command"]?.ToString();
            if (command?.Contains("sudo") == true)
                return ToolPermissionResult.Deny("sudo not allowed");
        }
        return ToolPermissionResult.Allow();
    }
};

Examples

The example/ directory contains comprehensive examples demonstrating all SDK features:

cd claude-agent-sdk/example
dotnet run

Available examples:

  • Quick Start - Basic usage patterns
  • Streaming Mode - Interactive conversations
  • System Prompt - Custom system prompts
  • MCP Calculator - Custom tool integration
  • Hooks - Event handling
  • Tool Permissions - Access control
  • Custom Agents - Specialized agents
  • And more...

Testing

Run unit tests:

cd claude-agent-sdk/unit-test
dotnet test

Run E2E tests (requires Claude CLI):

dotnet test --filter "FullyQualifiedName~E2E"

License

MIT License - see LICENSE for details.

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 is compatible.  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.6 81 4/21/2026
0.1.0-preview.5 63 4/21/2026
0.1.0-preview.4 59 4/9/2026
0.1.0-preview.3 61 4/7/2026
0.1.0-preview.2 79 1/12/2026
0.1.0-preview.1 79 1/12/2026