| Crates.io | misanthropy |
| lib.rs | misanthropy |
| version | 0.0.8 |
| created_at | 2024-06-25 06:04:44.289599+00 |
| updated_at | 2025-06-08 00:27:04.174618+00 |
| description | An interface to the Anthropic API |
| homepage | |
| repository | https://github.com/cortesi/misanthropy |
| max_upload_size | |
| id | 1282999 |
| size | 119,368 |
Misanthropy is set of Rust bindings for Anthropic API, providing easy access to Claude and other Anthropic models. It consists of two main components:
misanthropy: A Rust client library for the Anthropic API
misan: A command-line interface (CLI) tool for quick interactions with the API
Simple, idiomatic Rust interface for the Anthropic API
Support extended thinking and tool usage
Support for text and image content in messages
Support for streaming real-time responses
Configurable client with defaults for model and token limits
CLI tool for quick interactions with the API from the command line
Here's a basic example of using the misanthropy library:
use misanthropy::{Anthropic, MessagesRequest, Content};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Anthropic::from_env()?;
let mut request = MessagesRequest::default();
request.add_user(Content::text("Hello, Claude!"));
let response = client.messages(request).await?;
println!("{}", response.format_nicely());
Ok(())
}
For more examples, please check the examples
directory in the misanthropy crate. These examples demonstrate various
features and use cases of the library.
The misan CLI tool provides a command-line interface for interacting with the
Anthropic API. For usage instructions, run:
misan --help
ANTHROPIC_API_KEY: Set this environment variable with your Anthropic API key.Anthropic client or overridden per request.The library supports streaming responses for real-time interactions:
let mut stream = client.messages_stream(request)?;
while let Some(event) = stream.next().await {
match event {
Ok(event) => {
// Handle the streaming event
}
Err(e) => eprintln!("Error: {}", e),
}
}
The library supports defining and using tools in conversations. Tools are
defined using the schemars crate to generate JSON schemas for the tool
inputs.
schemars to your dependencies:[dependencies]
schemars = "0.8"
JsonSchema:use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
/// Get the current weather for a location.
#[derive(JsonSchema, Serialize, Deserialize)]
struct GetWeather {
/// The city and country, e.g., "London, UK"
location: String,
/// Temperature unit: "celsius" or "fahrenheit"
unit: Option<String>,
}
Tool from your input structure:use misanthropy::{Anthropic, MessagesRequest, Tool};
let weather_tool = Tool::new::<GetWeather>();
let request = MessagesRequest::default()
.with_tool(weather_tool)
.with_system(vec![Content::text("You can use the GetWeather tool to check the weather.")]);
if let Some(tool_use) = response.content.iter().find_map(|content| {
if let Content::ToolUse(tool_use) = content {
Some(tool_use)
} else {
None
}
}) {
if tool_use.name == "GetWeather" {
let weather_input: GetWeather = serde_json::from_value(tool_use.input.clone())?;
println!("Weather requested for: {}", weather_input.location);
// Here you would typically call an actual weather API
}
}
This approach allows you to define strongly-typed tool inputs that the AI can use, while also providing a way to handle the tool usage in your code.
This project is licensed under the MIT License - see the LICENSE file for details.