claude-sdk-rs 1.0.0

Rust SDK for Claude AI with CLI integration - type-safe async API for Claude Code and direct SDK usage
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
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::env;

use crate::mcp::clients::services::SlackApiService;
use crate::mcp::core::error::WorkflowError;
use crate::mcp::core::external_mcp::{
    AuthConfig, BaseExternalMCPClient, ExternalMCPClientNode, ExternalMCPConfig, RetryConfig,
};
use crate::mcp::core::nodes::Node;
use crate::mcp::core::task::TaskContext;
use crate::mcp::protocol::{CallToolResult, ToolContent, ToolDefinition};
use crate::mcp::transport::{HttpPoolConfig, TransportType};

/// Configuration specific to Slack MCP client
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SlackClientConfig {
    /// Base URL for the Slack MCP server
    pub server_url: String,

    /// Bot token for Slack authentication
    pub bot_token: Option<String>,

    /// User token for Slack authentication (if needed for certain operations)
    pub user_token: Option<String>,

    /// Transport type to use for connection
    pub transport: TransportType,

    /// Retry configuration
    pub retry_config: Option<RetryConfig>,
}

impl Default for SlackClientConfig {
    fn default() -> Self {
        Self {
            server_url: env::var("SLACK_MCP_URL")
                .unwrap_or_else(|_| "http://localhost:8003".to_string()),
            bot_token: env::var("SLACK_BOT_TOKEN").ok(),
            user_token: env::var("SLACK_USER_TOKEN").ok(),
            transport: TransportType::Http {
                base_url: env::var("SLACK_MCP_URL")
                    .unwrap_or_else(|_| "http://localhost:8003".to_string()),
                pool_config: crate::mcp::transport::HttpPoolConfig::default(),
            },
            retry_config: None,
        }
    }
}

/// Slack client node for connecting to external Slack MCP servers
#[derive(Debug)]
pub struct SlackClientNode {
    base_client: BaseExternalMCPClient,
    slack_config: SlackClientConfig,
    api_service: Option<SlackApiService>,
}

impl SlackClientNode {
    /// Create a new SlackClientNode with the given configuration
    pub fn new(config: SlackClientConfig) -> Self {
        let mut auth = None;
        let mut headers = HashMap::new();

        // Add authorization headers based on available tokens
        if let Some(ref bot_token) = config.bot_token {
            headers.insert("Authorization".to_string(), format!("Bearer {}", bot_token));
        }

        if let Some(ref user_token) = config.user_token {
            headers.insert("X-Slack-User-Token".to_string(), user_token.clone());
        }

        if !headers.is_empty() {
            auth = Some(AuthConfig {
                api_key: config.bot_token.clone(),
                token: config.bot_token.clone(),
                headers: Some(headers),
            });
        }

        let external_config = ExternalMCPConfig {
            service_name: "slack".to_string(),
            transport: config.transport.clone(),
            auth,
            retry_config: config.retry_config.clone().unwrap_or_default(),
        };

        let api_service = config
            .bot_token
            .as_ref()
            .map(|token| SlackApiService::new(token.clone()));

        Self {
            base_client: BaseExternalMCPClient::new(external_config),
            slack_config: config,
            api_service,
        }
    }

    /// Create a new SlackClientNode with default configuration
    pub fn with_defaults() -> Self {
        Self::new(SlackClientConfig::default())
    }

    /// Create a new SlackClientNode with HTTP transport
    pub fn with_http_transport(
        base_url: String,
        bot_token: Option<String>,
        user_token: Option<String>,
    ) -> Self {
        let config = SlackClientConfig {
            server_url: base_url.clone(),
            bot_token,
            user_token,
            transport: TransportType::Http {
                base_url,
                pool_config: HttpPoolConfig::default(),
            },
            retry_config: None,
        };
        Self::new(config)
    }

    /// Create a new SlackClientNode with WebSocket transport
    pub fn with_websocket_transport(
        url: String,
        bot_token: Option<String>,
        user_token: Option<String>,
    ) -> Self {
        let config = SlackClientConfig {
            server_url: url.clone(),
            bot_token,
            user_token,
            transport: TransportType::WebSocket {
                url,
                heartbeat_interval: Some(std::time::Duration::from_secs(30)),
                reconnect_config: crate::mcp::transport::ReconnectConfig::default(),
            },
            retry_config: None,
        };
        Self::new(config)
    }

    /// Create a new SlackClientNode with stdio transport
    pub fn with_stdio_transport(
        command: String,
        args: Vec<String>,
        bot_token: Option<String>,
        user_token: Option<String>,
    ) -> Self {
        let config = SlackClientConfig {
            server_url: format!("stdio://{}", command),
            bot_token,
            user_token,
            transport: TransportType::Stdio {
                command,
                args,
                auto_restart: true,
                max_restarts: 3,
            },
            retry_config: None,
        };
        Self::new(config)
    }

    /// Send a message to a Slack channel
    pub async fn send_message(
        &mut self,
        channel: &str,
        text: &str,
        thread_ts: Option<&str>,
    ) -> Result<CallToolResult, WorkflowError> {
        // Use real API if available
        if let Some(ref api_service) = self.api_service {
            let result = api_service
                .post_message(channel, text, thread_ts, None)
                .await?;

            // Convert API response to CallToolResult format
            let content = vec![ToolContent::Text {
                text: serde_json::to_string_pretty(&result)
                    .unwrap_or_else(|_| "Failed to serialize message result".to_string()),
            }];

            return Ok(CallToolResult {
                content,
                is_error: Some(false),
            });
        }

        // Fall back to MCP server
        let mut args = HashMap::new();
        args.insert(
            "channel".to_string(),
            serde_json::Value::String(channel.to_string()),
        );
        args.insert(
            "text".to_string(),
            serde_json::Value::String(text.to_string()),
        );

        if let Some(thread_ts) = thread_ts {
            args.insert(
                "thread_ts".to_string(),
                serde_json::Value::String(thread_ts.to_string()),
            );
        }

        self.execute_tool("send_message", Some(args)).await
    }

    /// List channels in the workspace
    pub async fn list_channels(
        &mut self,
        exclude_archived: Option<bool>,
        types: Option<Vec<String>>,
    ) -> Result<CallToolResult, WorkflowError> {
        let mut args = HashMap::new();

        if let Some(exclude_archived) = exclude_archived {
            args.insert(
                "exclude_archived".to_string(),
                serde_json::Value::Bool(exclude_archived),
            );
        }

        if let Some(types) = types {
            args.insert(
                "types".to_string(),
                serde_json::Value::Array(
                    types
                        .into_iter()
                        .map(|t| serde_json::Value::String(t))
                        .collect(),
                ),
            );
        }

        self.execute_tool("list_channels", Some(args)).await
    }

    /// Get information about a user
    pub async fn get_user_info(&mut self, user_id: &str) -> Result<CallToolResult, WorkflowError> {
        let mut args = HashMap::new();
        args.insert(
            "user".to_string(),
            serde_json::Value::String(user_id.to_string()),
        );

        self.execute_tool("get_user_info", Some(args)).await
    }

    /// Get information about a channel
    pub async fn get_channel_info(
        &mut self,
        channel_id: &str,
    ) -> Result<CallToolResult, WorkflowError> {
        let mut args = HashMap::new();
        args.insert(
            "channel".to_string(),
            serde_json::Value::String(channel_id.to_string()),
        );

        self.execute_tool("get_channel_info", Some(args)).await
    }

    /// Get message history from a channel
    pub async fn get_channel_history(
        &mut self,
        channel: &str,
        limit: Option<u32>,
        oldest: Option<&str>,
        latest: Option<&str>,
    ) -> Result<CallToolResult, WorkflowError> {
        let mut args = HashMap::new();
        args.insert(
            "channel".to_string(),
            serde_json::Value::String(channel.to_string()),
        );

        if let Some(limit) = limit {
            args.insert("limit".to_string(), serde_json::Value::Number(limit.into()));
        }

        if let Some(oldest) = oldest {
            args.insert(
                "oldest".to_string(),
                serde_json::Value::String(oldest.to_string()),
            );
        }

        if let Some(latest) = latest {
            args.insert(
                "latest".to_string(),
                serde_json::Value::String(latest.to_string()),
            );
        }

        self.execute_tool("get_channel_history", Some(args)).await
    }

    /// Update a message
    pub async fn update_message(
        &mut self,
        channel: &str,
        ts: &str,
        text: &str,
    ) -> Result<CallToolResult, WorkflowError> {
        let mut args = HashMap::new();
        args.insert(
            "channel".to_string(),
            serde_json::Value::String(channel.to_string()),
        );
        args.insert("ts".to_string(), serde_json::Value::String(ts.to_string()));
        args.insert(
            "text".to_string(),
            serde_json::Value::String(text.to_string()),
        );

        self.execute_tool("update_message", Some(args)).await
    }

    /// Delete a message
    pub async fn delete_message(
        &mut self,
        channel: &str,
        ts: &str,
    ) -> Result<CallToolResult, WorkflowError> {
        let mut args = HashMap::new();
        args.insert(
            "channel".to_string(),
            serde_json::Value::String(channel.to_string()),
        );
        args.insert("ts".to_string(), serde_json::Value::String(ts.to_string()));

        self.execute_tool("delete_message", Some(args)).await
    }

    /// Add a reaction to a message
    pub async fn add_reaction(
        &mut self,
        channel: &str,
        timestamp: &str,
        name: &str,
    ) -> Result<CallToolResult, WorkflowError> {
        let mut args = HashMap::new();
        args.insert(
            "channel".to_string(),
            serde_json::Value::String(channel.to_string()),
        );
        args.insert(
            "timestamp".to_string(),
            serde_json::Value::String(timestamp.to_string()),
        );
        args.insert(
            "name".to_string(),
            serde_json::Value::String(name.to_string()),
        );

        self.execute_tool("add_reaction", Some(args)).await
    }

    /// Remove a reaction from a message
    pub async fn remove_reaction(
        &mut self,
        channel: &str,
        timestamp: &str,
        name: &str,
    ) -> Result<CallToolResult, WorkflowError> {
        let mut args = HashMap::new();
        args.insert(
            "channel".to_string(),
            serde_json::Value::String(channel.to_string()),
        );
        args.insert(
            "timestamp".to_string(),
            serde_json::Value::String(timestamp.to_string()),
        );
        args.insert(
            "name".to_string(),
            serde_json::Value::String(name.to_string()),
        );

        self.execute_tool("remove_reaction", Some(args)).await
    }

    /// Search for messages
    pub async fn search_messages(
        &mut self,
        query: &str,
        sort: Option<&str>,
        sort_dir: Option<&str>,
        count: Option<u32>,
    ) -> Result<CallToolResult, WorkflowError> {
        let mut args = HashMap::new();
        args.insert(
            "query".to_string(),
            serde_json::Value::String(query.to_string()),
        );

        if let Some(sort) = sort {
            args.insert(
                "sort".to_string(),
                serde_json::Value::String(sort.to_string()),
            );
        }

        if let Some(sort_dir) = sort_dir {
            args.insert(
                "sort_dir".to_string(),
                serde_json::Value::String(sort_dir.to_string()),
            );
        }

        if let Some(count) = count {
            args.insert("count".to_string(), serde_json::Value::Number(count.into()));
        }

        self.execute_tool("search_messages", Some(args)).await
    }

    /// Create a new channel
    pub async fn create_channel(
        &mut self,
        name: &str,
        is_private: Option<bool>,
    ) -> Result<CallToolResult, WorkflowError> {
        let mut args = HashMap::new();
        args.insert(
            "name".to_string(),
            serde_json::Value::String(name.to_string()),
        );

        if let Some(is_private) = is_private {
            args.insert(
                "is_private".to_string(),
                serde_json::Value::Bool(is_private),
            );
        }

        self.execute_tool("create_channel", Some(args)).await
    }

    /// Invite users to a channel
    pub async fn invite_to_channel(
        &mut self,
        channel: &str,
        users: Vec<String>,
    ) -> Result<CallToolResult, WorkflowError> {
        let mut args = HashMap::new();
        args.insert(
            "channel".to_string(),
            serde_json::Value::String(channel.to_string()),
        );
        args.insert(
            "users".to_string(),
            serde_json::Value::Array(
                users
                    .into_iter()
                    .map(|u| serde_json::Value::String(u))
                    .collect(),
            ),
        );

        self.execute_tool("invite_to_channel", Some(args)).await
    }

    /// Get Slack configuration
    pub fn get_slack_config(&self) -> &SlackClientConfig {
        &self.slack_config
    }
}

#[async_trait]
impl ExternalMCPClientNode for SlackClientNode {
    fn get_config(&self) -> &ExternalMCPConfig {
        self.base_client.get_config()
    }

    async fn connect(&mut self) -> Result<(), WorkflowError> {
        self.base_client.connect().await
    }

    async fn execute_tool(
        &mut self,
        tool_name: &str,
        arguments: Option<HashMap<String, serde_json::Value>>,
    ) -> Result<CallToolResult, WorkflowError> {
        self.base_client.execute_tool(tool_name, arguments).await
    }

    async fn list_tools(&mut self) -> Result<Vec<ToolDefinition>, WorkflowError> {
        self.base_client.list_tools().await
    }

    async fn disconnect(&mut self) -> Result<(), WorkflowError> {
        self.base_client.disconnect().await
    }

    fn is_connected(&self) -> bool {
        self.base_client.is_connected()
    }
}

#[async_trait]
impl Node for SlackClientNode {
    async fn execute(&self, input: Value, context: &TaskContext) -> Result<Value, WorkflowError> {
        // Pass through to base client
        self.base_client.execute(input, context).await
    }

    fn name(&self) -> &str {
        "SlackClientNode"
    }
}

// #[cfg(test)]
// mod tests;