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
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

use crate::mcp::clients::services::NotionApiService;
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, ToolDefinition};

/// Notion-specific configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NotionConfig {
    /// Base configuration for external MCP connection
    pub base_config: ExternalMCPConfig,

    /// Notion workspace ID (optional)
    pub workspace_id: Option<String>,

    /// Default database ID for operations (optional)
    pub default_database_id: Option<String>,
}

impl NotionConfig {
    /// Create a new Notion configuration with HTTP transport
    pub fn new_http(base_url: String, api_key: Option<String>) -> Self {
        let auth = api_key.map(|key| AuthConfig {
            api_key: Some(key.clone()),
            token: Some(key),
            headers: None,
        });

        Self {
            base_config: ExternalMCPConfig {
                service_name: "notion".to_string(),
                transport: crate::mcp::transport::TransportType::Http {
                    base_url,
                    pool_config: crate::mcp::transport::HttpPoolConfig::default(),
                },
                auth,
                retry_config: RetryConfig::default(),
            },
            workspace_id: None,
            default_database_id: None,
        }
    }

    /// Create a new Notion configuration with WebSocket transport
    pub fn new_websocket(url: String) -> Self {
        Self {
            base_config: ExternalMCPConfig {
                service_name: "notion".to_string(),
                transport: crate::mcp::transport::TransportType::WebSocket {
                    url,
                    heartbeat_interval: Some(std::time::Duration::from_secs(30)),
                    reconnect_config: crate::mcp::transport::ReconnectConfig::default(),
                },
                auth: None,
                retry_config: RetryConfig::default(),
            },
            workspace_id: None,
            default_database_id: None,
        }
    }

    /// Create a new Notion configuration with stdio transport
    pub fn new_stdio(command: String, args: Vec<String>) -> Self {
        Self {
            base_config: ExternalMCPConfig {
                service_name: "notion".to_string(),
                transport: crate::mcp::transport::TransportType::Stdio {
                    command,
                    args,
                    auto_restart: true,
                    max_restarts: 3,
                },
                auth: None,
                retry_config: RetryConfig::default(),
            },
            workspace_id: None,
            default_database_id: None,
        }
    }
}

/// Notion client node for interacting with Notion via MCP
#[derive(Debug)]
pub struct NotionClientNode {
    pub config: NotionConfig,
    base_client: BaseExternalMCPClient,
    api_service: Option<NotionApiService>,
}

impl NotionClientNode {
    pub fn new(config: NotionConfig) -> Self {
        let base_client = BaseExternalMCPClient::new(config.base_config.clone());

        // Extract API key from auth config
        let api_service = config
            .base_config
            .auth
            .as_ref()
            .and_then(|auth| auth.api_key.clone().or(auth.token.clone()))
            .map(|key| NotionApiService::new(key));

        Self {
            config,
            base_client,
            api_service,
        }
    }

    /// Search for pages in Notion
    pub async fn search_pages(
        &mut self,
        query: &str,
        limit: Option<u32>,
    ) -> Result<Value, WorkflowError> {
        // Use real API if available
        if let Some(ref api_service) = self.api_service {
            let search_result = api_service
                .search(query, Some("page"), None, None, limit)
                .await?;

            return Ok(serde_json::to_value(search_result)?);
        }

        // Fall back to MCP server
        let mut args = HashMap::new();
        args.insert("query".to_string(), Value::String(query.to_string()));
        if let Some(limit) = limit {
            args.insert("limit".to_string(), Value::Number(limit.into()));
        }

        let result = self
            .base_client
            .execute_tool("search_pages", Some(args))
            .await?;
        Ok(serde_json::to_value(result.content)?)
    }

    /// Create a new page in Notion
    pub async fn create_page(
        &mut self,
        title: &str,
        content: &str,
        parent_id: Option<&str>,
    ) -> Result<Value, WorkflowError> {
        let mut args = HashMap::new();
        args.insert("title".to_string(), Value::String(title.to_string()));
        args.insert("content".to_string(), Value::String(content.to_string()));

        if let Some(parent) = parent_id {
            args.insert("parent_id".to_string(), Value::String(parent.to_string()));
        } else if let Some(ref db_id) = self.config.default_database_id {
            args.insert("parent_id".to_string(), Value::String(db_id.clone()));
        }

        let result = self
            .base_client
            .execute_tool("create_page", Some(args))
            .await?;
        Ok(serde_json::to_value(result.content)?)
    }

    /// Create a research documentation page with structured content
    pub async fn create_research_page(
        &mut self,
        title: &str,
        summary: &str,
        key_points: &[String],
        sources: &[Value],
        parent_id: Option<&str>,
        properties: Option<HashMap<String, Value>>,
    ) -> Result<Value, WorkflowError> {
        // Build structured content for research documentation
        let mut content_parts = Vec::new();

        // Add title
        content_parts.push(format!("# {}\n", title));

        // Add summary section
        content_parts.push("## Summary\n".to_string());
        content_parts.push(format!("{}\n\n", summary));

        // Add key insights
        if !key_points.is_empty() {
            content_parts.push("## Key Insights\n".to_string());
            for point in key_points {
                content_parts.push(format!("- {}\n", point));
            }
            content_parts.push("\n".to_string());
        }

        // Add sources section
        if !sources.is_empty() {
            content_parts.push("## Sources\n".to_string());
            for (i, source) in sources.iter().enumerate() {
                match source {
                    Value::String(url) => {
                        content_parts.push(format!("{}. {}\n", i + 1, url));
                    }
                    Value::Object(obj) => {
                        let Some(title) = obj.get("title") else {
                            continue;
                        };
                        let Some(url) = obj.get("url") else {
                            continue;
                        };

                        content_parts.push(format!(
                            "{}. [{}]({})\n",
                            i + 1,
                            title.as_str().unwrap_or("Unknown Title"),
                            url.as_str().unwrap_or("No URL")
                        ));
                    }
                    _ => {
                        content_parts.push(format!(
                            "{}. {}\n",
                            i + 1,
                            serde_json::to_string(source).unwrap_or_default()
                        ));
                    }
                }
            }
            content_parts.push("\n".to_string());
        }

        // Add timestamp
        content_parts.push(format!(
            "---\n*Generated on: {}*\n",
            chrono::Utc::now().format("%Y-%m-%d %H:%M:%S UTC")
        ));

        let content = content_parts.join("");

        // Create the page with enhanced arguments
        let mut args = HashMap::new();
        args.insert("title".to_string(), Value::String(title.to_string()));
        args.insert("content".to_string(), Value::String(content));

        if let Some(parent) = parent_id {
            args.insert("parent_id".to_string(), Value::String(parent.to_string()));
        } else if let Some(ref db_id) = self.config.default_database_id {
            args.insert("parent_id".to_string(), Value::String(db_id.clone()));
        }

        // Add properties if provided
        if let Some(props) = properties {
            args.insert(
                "properties".to_string(),
                Value::Object(props.into_iter().collect()),
            );
        }

        let result = self
            .base_client
            .execute_tool("create_page", Some(args))
            .await?;
        Ok(serde_json::to_value(result.content)?)
    }

    /// Update an existing page in Notion
    pub async fn update_page(
        &mut self,
        page_id: &str,
        updates: HashMap<String, Value>,
    ) -> Result<Value, WorkflowError> {
        let mut args = HashMap::new();
        args.insert("page_id".to_string(), Value::String(page_id.to_string()));
        args.insert(
            "updates".to_string(),
            Value::Object(updates.into_iter().collect()),
        );

        let result = self
            .base_client
            .execute_tool("update_page", Some(args))
            .await?;
        Ok(serde_json::to_value(result.content)?)
    }

    /// Get a page by ID
    pub async fn get_page(&mut self, page_id: &str) -> Result<Value, WorkflowError> {
        let mut args = HashMap::new();
        args.insert("page_id".to_string(), Value::String(page_id.to_string()));

        let result = self
            .base_client
            .execute_tool("get_page", Some(args))
            .await?;
        Ok(serde_json::to_value(result.content)?)
    }

    /// List databases in the workspace
    pub async fn list_databases(&mut self) -> Result<Value, WorkflowError> {
        let result = self
            .base_client
            .execute_tool("list_databases", None)
            .await?;
        Ok(serde_json::to_value(result.content)?)
    }

    /// Query a database
    pub async fn query_database(
        &mut self,
        database_id: &str,
        filter: Option<Value>,
        sorts: Option<Value>,
        limit: Option<u32>,
    ) -> Result<Value, WorkflowError> {
        let mut args = HashMap::new();
        args.insert(
            "database_id".to_string(),
            Value::String(database_id.to_string()),
        );

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

        let result = self
            .base_client
            .execute_tool("query_database", Some(args))
            .await?;
        Ok(serde_json::to_value(result.content)?)
    }

    /// Helper method to parse Notion-specific errors
    pub fn parse_notion_error(&self, error: &WorkflowError) -> WorkflowError {
        match error {
            WorkflowError::MCPError { message } => {
                // Check for common Notion API errors
                if message.contains("unauthorized") || message.contains("401") {
                    WorkflowError::MCPError {
                        message: format!("Notion authentication failed: {}", message),
                    }
                } else if message.contains("not_found") || message.contains("404") {
                    WorkflowError::MCPError {
                        message: format!("Notion resource not found: {}", message),
                    }
                } else if message.contains("rate_limit") || message.contains("429") {
                    WorkflowError::MCPError {
                        message: format!("Notion rate limit exceeded: {}", message),
                    }
                } else {
                    WorkflowError::MCPError {
                        message: message.clone(),
                    }
                }
            }
            _ => WorkflowError::MCPError {
                message: format!("Notion error: {:?}", error),
            },
        }
    }
}

#[async_trait]
impl Node for NotionClientNode {
    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 {
        "NotionClientNode"
    }
}

#[async_trait]
impl ExternalMCPClientNode for NotionClientNode {
    fn get_config(&self) -> &ExternalMCPConfig {
        &self.config.base_config
    }

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

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

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

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

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

/// Builder for NotionClientNode
pub struct NotionClientBuilder {
    config: NotionConfig,
}

impl NotionClientBuilder {
    pub fn new_http(base_url: String) -> Self {
        Self {
            config: NotionConfig::new_http(base_url, None),
        }
    }

    pub fn new_websocket(url: String) -> Self {
        Self {
            config: NotionConfig::new_websocket(url),
        }
    }

    pub fn new_stdio(command: String, args: Vec<String>) -> Self {
        Self {
            config: NotionConfig::new_stdio(command, args),
        }
    }

    pub fn with_api_key(mut self, api_key: String) -> Self {
        if self.config.base_config.auth.is_none() {
            self.config.base_config.auth = Some(AuthConfig {
                api_key: Some(api_key.clone()),
                token: Some(api_key),
                headers: None,
            });
        } else if let Some(ref mut auth) = self.config.base_config.auth {
            auth.token = Some(api_key);
        }
        self
    }

    pub fn with_workspace_id(mut self, workspace_id: String) -> Self {
        self.config.workspace_id = Some(workspace_id);
        self
    }

    pub fn with_default_database_id(mut self, database_id: String) -> Self {
        self.config.default_database_id = Some(database_id);
        self
    }

    pub fn with_retry_config(mut self, retry_config: RetryConfig) -> Self {
        self.config.base_config.retry_config = retry_config;
        self
    }

    pub fn build(self) -> NotionClientNode {
        NotionClientNode::new(self.config)
    }
}

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