composio-sdk 0.1.1

Minimal Rust SDK for Composio Tool Router REST API
Documentation
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# Composio Rust SDK


> A minimal, type-safe Rust SDK for integrating AI agents with 1000+ external services through the Composio platform.

[![Crates.io](https://img.shields.io/crates/v/composio-sdk.svg)](https://crates.io/crates/composio-sdk)
[![Documentation](https://docs.rs/composio-sdk/badge.svg)](https://docs.rs/composio-sdk)
[![License](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg)](LICENSE)
[![Rust Version](https://img.shields.io/badge/rust-1.70%2B-blue.svg)](https://www.rust-lang.org)
[![GitHub](https://img.shields.io/badge/github-DotViegas%2Fcomposio--sdk--rust-blue)](https://github.com/DotViegas/composio-sdk-rust)

## What is Composio?


Composio is a platform that connects AI agents to external services like GitHub, Gmail, Slack, and 1000+ other applications. Instead of building integrations for each service yourself, Composio provides:

- **Universal API**: One SDK to access all services
- **Authentication Management**: OAuth, API keys, and other auth methods handled automatically
- **Tool Discovery**: AI agents can discover and use tools at runtime
- **Sandboxed Execution**: Safe environment for running code and commands
- **Event Triggers**: React to events from connected services

Think of it as a "universal adapter" that lets your AI agent interact with any external service through a consistent interface.

## Why This SDK?


This is a **pure Rust implementation** of the Composio SDK, designed for:

- **Performance**: Native async/await with Tokio, minimal memory footprint (~2 MB)
- **Type Safety**: Compile-time guarantees with Rust's type system
- **Reliability**: Automatic retries, comprehensive error handling
- **Self-Contained**: All dependencies bundled, no external setup required
- **Production Ready**: Built for real-world applications with proper error handling and logging

## How It Works


### The Big Picture


![Composio SDK Architecture](architecture.png)

The architecture shows how your AI agent interacts with external services through the Composio SDK:

1. **Your AI Agent** uses the Composio Rust SDK
2. **SDK Components** provide sessions, meta tools, and wizard guidance
3. **Composio Platform** handles authentication and routing
4. **External Services** (GitHub, Gmail, Slack, 1000+ apps) are accessed through a unified interface

### Core Concepts


#### 1. Sessions (User Isolation)


Every user gets their own session. This ensures:
- User A's GitHub credentials don't mix with User B's
- Each user can connect different accounts (work email vs personal email)
- Tools execute with the correct user's permissions

```rust
// Create a session for a specific user
let session = client
    .create_session("user_123")
    .toolkits(vec!["github", "gmail"])
    .send()
    .await?;
```

#### 2. Meta Tools (Runtime Discovery)


Instead of hardcoding which tools your agent can use, meta tools let the agent discover and use tools dynamically:

- **COMPOSIO_SEARCH_TOOLS**: "Find me tools to send emails"
- **COMPOSIO_MANAGE_CONNECTIONS**: "Connect my Gmail account"
- **COMPOSIO_MULTI_EXECUTE_TOOL**: "Run these 5 tools in parallel"
- **COMPOSIO_REMOTE_WORKBENCH**: "Run this Python code in a sandbox"
- **COMPOSIO_REMOTE_BASH_TOOL**: "Execute this bash command safely"

This is powerful because your agent can adapt to new tasks without code changes.

#### 3. Native Rust Meta Tools


We've implemented 4 of the 5 meta tools in **pure Rust** (no Python dependencies):

```rust
use composio_sdk::meta_tools::*;

// Search for tools
let search = ToolSearch::new(Arc::new(client));
let tools = search.search("send email", &session_id).await?;

// Execute multiple tools in parallel
let executor = MultiExecutor::new(Arc::new(client));
let results = executor.execute_parallel(&session_id, tool_calls).await?;

// Manage OAuth connections
let manager = ConnectionManager::new(Arc::new(client));
let is_connected = manager.is_connected(&session_id, "github").await?;

// Execute bash commands
let bash = BashExecutor::new();
let result = bash.execute("ls -la").await?;
```

Only the Workbench uses remote Python execution (by design, for data processing).

#### 4. Wizard Instructions (AI Guidance)


The SDK includes bundled "Skills" - best practices and patterns for using Composio effectively. The wizard module generates instructions for AI agents:

```rust
use composio_sdk::wizard::generate_wizard_instructions;

// Generate instructions for GitHub integration
let instructions = generate_wizard_instructions(Some("github"))?;

// Your AI agent reads these instructions to learn:
// - How to create sessions correctly
// - How to handle authentication
// - Common pitfalls to avoid
// - Toolkit-specific best practices
```

This helps AI agents use Composio correctly without trial and error.

## Architecture


### Module Structure


```
composio-sdk/
├── src/
│   ├── client.rs           # HTTP client, API communication
│   ├── session.rs          # Session management
│   ├── config.rs           # Configuration
│   ├── error.rs            # Error types
│   ├── retry.rs            # Retry logic with exponential backoff
│   │
│   ├── models/             # Data structures
│   │   ├── request.rs      # API request types
│   │   ├── response.rs     # API response types
│   │   └── enums.rs        # Enumerations
│   │
│   ├── meta_tools/         # Native Rust implementations
│   │   ├── search.rs       # Tool discovery
│   │   ├── multi_executor.rs  # Parallel execution
│   │   ├── connections.rs  # OAuth management
│   │   ├── bash.rs         # Command execution
│   │   └── workbench.rs    # Python sandbox (hybrid)
│   │
│   └── wizard/             # AI guidance system
│       ├── skills.rs       # Skills extraction
│       ├── generator.rs    # Instruction generation
│       └── validator.rs    # Pattern validation
│
└── skills/                 # Bundled best practices (33 files)
    ├── AGENTS.md           # Consolidated reference
    ├── SKILL.md            # Metadata
    └── rules/              # 31 rule files
```

### Data Flow


```
1. Your Code
   2. ComposioClient (HTTP client with retry logic)
   3. Session (user-scoped context)
   4. Meta Tools (discovery, execution, auth)
   5. Composio API (backend.composio.dev)
   6. External Services (GitHub, Gmail, etc.)
```

### Key Design Decisions

**Why Sessions?**
- Isolates users from each other
- Manages authentication per user
- Provides consistent context for tool execution

**Why Meta Tools?**
- Enables runtime tool discovery
- Reduces context window usage (only 5 tools vs 1000+)
- Allows agents to adapt to new tasks

**Why Native Rust?**
- Better performance (no Python overhead)
- Easier deployment (single binary)
- Type safety at compile time
- Smaller memory footprint

**Why Bundled Skills?**
- No external dependencies
- Always available at compile time
- Consistent behavior across installations
- Helps AI agents learn best practices

## Quick Start

### Installation

```toml
[dependencies]
composio-sdk = "0.1.0"
tokio = { version = "1.0", features = ["full"] }
```

### Basic Usage


```rust
use composio_sdk::{ComposioClient, ComposioError};

#[tokio::main]

async fn main() -> Result<(), ComposioError> {
    // 1. Create client
    let client = ComposioClient::builder()
        .api_key(std::env::var("COMPOSIO_API_KEY")?)
        .build()?;

    // 2. Create session for a user
    let session = client
        .create_session("user_123")
        .toolkits(vec!["github", "gmail"])
        .send()
        .await?;

    // 3. Execute a tool
    let result = session
        .execute_tool(
            "GITHUB_CREATE_ISSUE",
            serde_json::json!({
                "owner": "composio",
                "repo": "composio",
                "title": "Test issue",
                "body": "Created via Rust SDK"
            })
        )
        .await?;

    println!("Issue created: {:?}", result.data);
    Ok(())
}
```

### With Meta Tools


```rust
use composio_sdk::meta_tools::ToolSearch;
use std::sync::Arc;

// Let the agent discover tools at runtime
let search = ToolSearch::new(Arc::new(client));
let tools = search.search("create GitHub issue", &session_id).await?;

// Agent now knows which tools to use
for tool in tools {
    println!("Found: {} - {}", tool.slug, tool.description);
}
```

### With Wizard Instructions


```rust
use composio_sdk::wizard::generate_wizard_instructions;

// Generate instructions for your AI agent
let instructions = generate_wizard_instructions(Some("github"))?;

// Feed these instructions to your AI agent
// The agent learns best practices automatically
```

## Real-World Example


Here's how you might build a GitHub automation agent:

```rust
use composio_sdk::{ComposioClient, meta_tools::*};
use std::sync::Arc;

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Setup
    let client = Arc::new(ComposioClient::builder()
        .api_key(std::env::var("COMPOSIO_API_KEY")?)
        .build()?);
    
    let session = client
        .create_session("user_123")
        .toolkits(vec!["github"])
        .send()
        .await?;
    
    let session_id = session.session_id();
    
    // 1. Check if GitHub is connected
    let conn_manager = ConnectionManager::new(client.clone());
    if !conn_manager.is_connected(session_id, "github").await? {
        // Create auth link for user
        let link = conn_manager.create_auth_link(
            session_id,
            "github",
            Some("https://myapp.com/callback")
        ).await?;
        
        println!("Please connect GitHub: {}", link.redirect_url);
        return Ok(());
    }
    
    // 2. Search for relevant tools
    let search = ToolSearch::new(client.clone());
    let tools = search.search("list GitHub repositories", session_id).await?;
    
    println!("Agent can use these tools:");
    for tool in &tools {
        println!("  - {}: {}", tool.slug, tool.name);
    }
    
    // 3. Execute tools in parallel
    let executor = MultiExecutor::new(client.clone());
    let tool_calls = vec![
        ToolCall {
            tool_slug: "GITHUB_GET_REPOS".to_string(),
            arguments: serde_json::json!({ "owner": "composio" }),
            connected_account_id: None,
        },
        ToolCall {
            tool_slug: "GITHUB_GET_ISSUES".to_string(),
            arguments: serde_json::json!({ 
                "owner": "composio", 
                "repo": "composio" 
            }),
            connected_account_id: None,
        },
    ];
    
    let results = executor.execute_parallel(session_id, tool_calls).await?;
    println!("Executed {} tools successfully", results.successful);
    
    Ok(())
}
```

## Features


### Core Features

- ✅ Session management with user isolation
- ✅ Tool execution (regular and meta tools)
- ✅ Toolkit listing and filtering
- ✅ Authentication link creation
- ✅ Automatic retry with exponential backoff
- ✅ Comprehensive error handling

### Native Rust Meta Tools

- ✅ Tool search and discovery
- ✅ Multi-tool parallel execution
- ✅ Connection management (OAuth)
- ✅ Bash command execution
- ✅ Workbench (hybrid: Rust wrapper + remote Python)

### Wizard System

- ✅ Bundled Skills content (33 files)
- ✅ Instruction generation for AI agents
- ✅ Pattern validation
- ✅ Toolkit-specific guidance

### Performance

- ~2 MB memory footprint
- ✅ Async/await with Tokio
- ✅ Zero-copy deserialization where possible
- ✅ Efficient Arc-based sharing

## Configuration


Customize client behavior:

```rust
use std::time::Duration;

let client = ComposioClient::builder()
    .api_key("your-api-key")
    .base_url("https://backend.composio.dev/api/v3")
    .timeout(Duration::from_secs(30))
    .max_retries(3)
    .initial_retry_delay(Duration::from_secs(1))
    .max_retry_delay(Duration::from_secs(10))
    .build()?;
```

## Authentication Patterns


### In-Chat Authentication (Default)


The agent automatically prompts users when authentication is needed:

```rust
let session = client
    .create_session("user_123")
    .manage_connections(true)  // Default
    .send()
    .await?;

// Agent will automatically handle auth when needed
```

### Manual Authentication


Pre-authenticate users during onboarding:

```rust
// Create auth link
let link = session
    .create_auth_link("github", Some("https://yourapp.com/callback"))
    .await?;

println!("Redirect user to: {}", link.redirect_url);

// Wait for connection
link.wait_for_connection(Duration::from_secs(300)).await?;
```

## Error Handling


The SDK provides detailed error information:

```rust
use composio_sdk::ComposioError;

match session.execute_tool("INVALID_TOOL", serde_json::json!({})).await {
    Ok(result) => println!("Success: {:?}", result),
    Err(ComposioError::ApiError { status, message, suggested_fix, .. }) => {
        eprintln!("API error ({}): {}", status, message);
        if let Some(fix) = suggested_fix {
            eprintln!("Suggested fix: {}", fix);
        }
    }
    Err(ComposioError::NetworkError(e)) => {
        eprintln!("Network error: {}", e);
    }
    Err(e) => {
        eprintln!("Other error: {}", e);
    }
}
```

## Examples


The SDK includes comprehensive examples:

```bash
# Basic usage

cargo run --example basic_usage

# Authentication flows

cargo run --example authentication

# Meta tools

cargo run --example meta_tools_usage

# Wizard instructions

cargo run --example wizard_instructions

# Tool execution

cargo run --example tool_execution
```

See the [`examples/`](examples/) directory for complete working examples.

## Documentation


- **API Docs**: [docs.rs/composio-sdk]https://docs.rs/composio-sdk
- **Composio Platform**: [docs.composio.dev]https://docs.composio.dev
- **Meta Tools Guide**: [README_META_TOOLS.md]README_META_TOOLS.md
- **Wizard System**: [WIZARD_INSTRUCTIONS.md]WIZARD_INSTRUCTIONS.md
- **Skills Documentation**: [SKILLS_BUNDLED.md]SKILLS_BUNDLED.md

## Requirements


- Rust 1.70 or later
- Tokio runtime
- Composio API key ([get one here]https://app.composio.dev)

## Performance


The SDK is optimized for production use:

- **Library size**: 2.45 MB (release build)
- **Runtime overhead**: 112 bytes (client) + 296 bytes (session builder)
- **Initialization time**: ~200 µs (client creation)
- **Memory footprint**: Minimal, suitable for resource-constrained environments

See [MEMORY_FOOTPRINT_REPORT.md](MEMORY_FOOTPRINT_REPORT.md) for detailed analysis.

## Development


```bash
# Clone the repository

git clone https://github.com/DotViegas/composio-sdk-rust.git
cd composio-sdk-rust/composio-sdk

# Set your API key

export COMPOSIO_API_KEY="your_api_key_here"

# Run tests

cargo test

# Run examples

cargo run --example basic_usage

# Build documentation

cargo doc --open
```

See [DEVELOPMENT.md](DEVELOPMENT.md) for detailed development guide.

## Contributing


Contributions are welcome! Please see [CONTRIBUTING.md](../CONTRIBUTING.md) for guidelines.

## License


Licensed under either of:

- Apache License, Version 2.0 ([LICENSE-APACHE]LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT]LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.

## Support


- [GitHub Issues]https://github.com/DotViegas/composio-sdk-rust/issues
- [Discord Community]https://discord.gg/composio (Composio Platform)
- [Documentation]https://docs.composio.dev (Composio Platform)

## Acknowledgments


This SDK was created by [DotViegas](https://github.com/DotViegas) for [ZeroClaw](https://github.com/zeroclaw), a lightweight Rust AI assistant. It follows the design patterns from the official [Composio Python SDK](https://github.com/ComposioHQ/composio) while providing native Rust implementations for better performance and type safety.

---

**Made with ❤️ for the Rust community**