Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ tab_width = 4
end_of_line = crlf
insert_final_newline = true

dotnet_diagnostic.CA1510.severity = none # Use ArgumentNullException throw helper
dotnet_diagnostic.IDE0060.severity = none # Caused by resource with no methods and no subresources
dotnet_diagnostic.IDE1006.severity = none # Some names may not match up with C# conventions
dotnet_diagnostic.IDE0290.severity = none # Don't prefer primary constructors
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,33 @@ await foreach (var message in client.Messages.CreateStreaming(parameters))
}
```

## `IChatClient`

The SDK provides an implementation of the `IChatClient` interface from the `Microsoft.Extensions.AI.Abstractions` library.
This enables `AnthropicClient` (and `Anthropic.Services.IBetaService`) to be used with other libraries that integrate with
these core abstractions. For example, tools in the MCP C# SDK (`ModelContextProtocol`) library can be used directly with an `AnthropicClient`
exposed via `IChatClient`.

```csharp
using Anthropic;
using Microsoft.Extensions.AI;
using ModelContextProtocol.Client;

// Configured using the ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN and ANTHROPIC_BASE_URL environment variables
IChatClient chatClient = client.AsIChatClient("claude-haiku-4-5")
.AsBuilder()
.UseFunctionInvocation()
.Build();

// Using McpClient from the MCP C# SDK
McpClient learningServer = await McpClient.CreateAsync(
new HttpClientTransport(new() { Endpoint = new("https://learn.microsoft.com/api/mcp") }));

ChatOptions options = new() { Tools = [.. await learningServer.ListToolsAsync()] };

Console.WriteLine(await chatClient.GetResponseAsync("Tell me about IChatClient", options));
```

## Binary responses

The SDK defines methods that return binary responses, which are used for API responses that shouldn't necessarily be parsed, like non-JSON data.
Expand Down
12 changes: 12 additions & 0 deletions examples/ChatClientExample/ChatClientExample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\src\Anthropic\Anthropic.csproj" />
<PackageReference Include="Microsoft.Extensions.AI" Version="10.0.0" />
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
32 changes: 32 additions & 0 deletions examples/ChatClientExample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Anthropic;
using Microsoft.Extensions.AI;

// Configured using the ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN and ANTHROPIC_BASE_URL environment variables
IChatClient chatClient = new AnthropicClient()
.AsIChatClient("claude-haiku-4-5")
.AsBuilder()
.UseFunctionInvocation()
.Build();

ChatOptions options = new()
{
Tools =
[
AIFunctionFactory.Create(
() => Environment.UserName,
"get_current_user_name",
"Gets the current user's name."
),
],
};

await foreach (
var update in chatClient.GetStreamingResponseAsync(
"Write a Haiku about the current user's name",
options
)
)
{
Console.Write(update);
}
Console.WriteLine();
Loading