#[cfg(feature = "mcp")]
mod mcp_example {
use agentix::{LlmEvent, McpTool, Provider, Request, Tool, ToolBundle};
use futures::StreamExt;
use std::env;
use std::time::Duration;
pub async fn run() -> Result<(), Box<dyn std::error::Error>> {
let api_key = env::var("ANTHROPIC_API_KEY")
.expect("ANTHROPIC_API_KEY must be set in your environment variables");
let http = reqwest::Client::new();
println!("Starting MCP server (this might take a few seconds)...");
let mcp_tool = McpTool::stdio("npx", &["-y", "@modelcontextprotocol/server-memory"])
.await?
.with_timeout(Duration::from_secs(30));
let mut bundle = ToolBundle::new();
bundle.push(mcp_tool);
println!("MCP server connected. Exposed tools:");
for tool in bundle.raw_tools() {
println!(" - {}", tool.function.name);
}
println!("\nSending request to Claude...");
let mut stream = Request::new(Provider::Anthropic, api_key)
.system_prompt("You are a helpful AI assistant. You have access to a memory tool.")
.user("Save the secret phrase 'RUST-IS-AWESOME' to memory, and then verify it by reading it back.")
.tools(bundle.raw_tools())
.stream(&http)
.await?;
println!("\nResponse stream:");
while let Some(event) = stream.next().await {
match event {
LlmEvent::Token(t) => {
print!("{t}");
}
LlmEvent::ToolCall(tc) => {
println!("\n\n[Model is calling MCP tool: {}]", tc.name);
println!("Arguments: {}", tc.arguments);
}
LlmEvent::Done => break,
LlmEvent::Error(e) => eprintln!("\nError: {e}"),
_ => {}
}
}
println!("\n");
Ok(())
}
}
#[cfg(feature = "mcp")]
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
mcp_example::run().await
}
#[cfg(not(feature = "mcp"))]
fn main() {
println!("Please run this example with the `mcp` feature enabled:");
println!("cargo run --example 05_mcp_client --features mcp");
}