1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//! Example demonstrating tool execution with the Composio SDK
//!
//! This example shows how to:
//! - Create a session for a user
//! - Execute a tool with arguments
//! - Handle the response and errors
//!
//! Run with: cargo run --example tool_execution
use composio_sdk::client::ComposioClient;
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize the Composio client with your API key
let client = ComposioClient::builder()
.api_key(std::env::var("COMPOSIO_API_KEY")?)
.build()?;
println!("Creating session for user...");
// Create a session for a specific user with GitHub toolkit enabled
let session = client
.create_session("user_123")
.toolkits(vec!["github"])
.send()
.await?;
println!("Session created: {}", session.session_id());
println!("MCP URL: {}", session.mcp_url());
println!();
// Example 1: Execute a tool to get GitHub repositories
println!("Example 1: Getting GitHub repositories...");
match session
.execute_tool(
"GITHUB_GET_REPOS",
json!({
"owner": "composio"
}),
)
.await
{
Ok(result) => {
println!("✓ Success!");
println!(" Log ID: {}", result.log_id);
if let Some(error) = result.error {
println!(" Tool Error: {}", error);
} else {
println!(" Result: {}", serde_json::to_string_pretty(&result.data)?);
}
}
Err(e) => {
eprintln!("✗ Error: {}", e);
}
}
println!();
// Example 2: Execute a tool to create a GitHub issue
println!("Example 2: Creating a GitHub issue...");
match session
.execute_tool(
"GITHUB_CREATE_ISSUE",
json!({
"owner": "composio",
"repo": "composio",
"title": "Test issue from Rust SDK",
"body": "This issue was created using the Composio Rust SDK as a demonstration."
}),
)
.await
{
Ok(result) => {
println!("✓ Success!");
println!(" Log ID: {}", result.log_id);
if let Some(error) = result.error {
println!(" Tool Error: {}", error);
} else {
println!(" Result: {}", serde_json::to_string_pretty(&result.data)?);
}
}
Err(e) => {
eprintln!("✗ Error: {}", e);
// Handle specific error types
match e {
composio_sdk::error::ComposioError::ApiError {
status,
message,
suggested_fix,
..
} => {
eprintln!(" Status: {}", status);
eprintln!(" Message: {}", message);
if let Some(fix) = suggested_fix {
eprintln!(" Suggested Fix: {}", fix);
}
}
_ => {}
}
}
}
println!();
// Example 3: Execute a tool with empty arguments
println!("Example 3: Listing GitHub user's repositories...");
match session
.execute_tool("GITHUB_LIST_USER_REPOS", json!({}))
.await
{
Ok(result) => {
println!("✓ Success!");
println!(" Log ID: {}", result.log_id);
if let Some(error) = result.error {
println!(" Tool Error: {}", error);
} else {
println!(" Result: {}", serde_json::to_string_pretty(&result.data)?);
}
}
Err(e) => {
eprintln!("✗ Error: {}", e);
}
}
Ok(())
}