pulseengine-mcp-server 0.17.1

[DEPRECATED] Use rmcp instead. MCP server framework.
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
//! Tests for backend trait and error handling

use crate::backend::{BackendError, McpBackend, SimpleBackend};
use async_trait::async_trait;
use pulseengine_mcp_protocol::error::ErrorCode;
use pulseengine_mcp_protocol::*;
use std::error::Error as StdError;
use std::fmt;

#[test]
fn test_backend_error_creation() {
    let config_err = BackendError::configuration("Config test");
    assert!(
        config_err
            .to_string()
            .contains("Configuration error: Config test")
    );

    let connection_err = BackendError::connection("Connection test");
    assert!(
        connection_err
            .to_string()
            .contains("Connection error: Connection test")
    );

    let not_supported_err = BackendError::not_supported("Not supported test");
    assert!(
        not_supported_err
            .to_string()
            .contains("Operation not supported: Not supported test")
    );

    let internal_err = BackendError::internal("Internal test");
    assert!(
        internal_err
            .to_string()
            .contains("Internal backend error: Internal test")
    );
}

#[test]
fn test_backend_error_custom() {
    #[derive(Debug)]
    struct CustomError(String);

    impl fmt::Display for CustomError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "Custom: {}", self.0)
        }
    }

    impl StdError for CustomError {}

    let custom_err = BackendError::custom(CustomError("test".to_string()));
    assert!(custom_err.to_string().contains("Custom error:"));
}

#[test]
fn test_backend_error_to_protocol_error() {
    let backend_err = BackendError::NotInitialized;
    let protocol_err: Error = backend_err.into();
    assert_eq!(protocol_err.code, ErrorCode::InternalError);

    let config_err = BackendError::configuration("test");
    let protocol_err: Error = config_err.into();
    assert_eq!(protocol_err.code, ErrorCode::InvalidParams);

    let connection_err = BackendError::connection("test");
    let protocol_err: Error = connection_err.into();
    assert_eq!(protocol_err.code, ErrorCode::InternalError);

    let not_supported_err = BackendError::not_supported("test");
    let protocol_err: Error = not_supported_err.into();
    assert_eq!(protocol_err.code, ErrorCode::MethodNotFound);

    let internal_err = BackendError::internal("test");
    let protocol_err: Error = internal_err.into();
    assert_eq!(protocol_err.code, ErrorCode::InternalError);
}

// Mock backend for testing
#[derive(Clone)]
struct MockBackend {
    should_fail: bool,
    server_name: String,
}

#[derive(Debug)]
struct MockError(String);

impl fmt::Display for MockError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Mock error: {}", self.0)
    }
}

impl StdError for MockError {}

impl From<BackendError> for MockError {
    fn from(err: BackendError) -> Self {
        MockError(err.to_string())
    }
}

impl From<MockError> for Error {
    fn from(err: MockError) -> Self {
        Error::internal_error(err.to_string())
    }
}

#[async_trait]
impl McpBackend for MockBackend {
    type Error = MockError;
    type Config = bool;

    async fn initialize(config: Self::Config) -> std::result::Result<Self, Self::Error> {
        Ok(Self {
            should_fail: config,
            server_name: "Mock Server".to_string(),
        })
    }

    fn get_server_info(&self) -> ServerInfo {
        ServerInfo {
            protocol_version: ProtocolVersion::default(),
            capabilities: ServerCapabilities::default(),
            server_info: Implementation::new(self.server_name.clone(), "1.0.0"),
            instructions: Some("Mock backend for testing".to_string()),
        }
    }

    async fn health_check(&self) -> std::result::Result<(), Self::Error> {
        if self.should_fail {
            Err(MockError("Health check failed".to_string()))
        } else {
            Ok(())
        }
    }

    async fn list_tools(
        &self,
        _request: PaginatedRequestParam,
    ) -> std::result::Result<ListToolsResult, Self::Error> {
        if self.should_fail {
            return Err(MockError("Failed to list tools".to_string()));
        }

        Ok(ListToolsResult {
            tools: vec![Tool {
                name: "mock_tool".to_string(),
                description: "A mock tool for testing".to_string(),
                input_schema: serde_json::json!({
                    "type": "object",
                    "properties": {},
                    "required": []
                }),
                output_schema: None,
                title: None,
                annotations: None,
                icons: None,
                execution: None,
                _meta: None,
            }],
            next_cursor: None,
        })
    }

    async fn call_tool(
        &self,
        request: CallToolRequestParam,
    ) -> std::result::Result<CallToolResult, Self::Error> {
        if self.should_fail {
            return Err(MockError("Failed to call tool".to_string()));
        }

        if request.name == "mock_tool" {
            Ok(CallToolResult {
                content: vec![Content::Text {
                    text: "Mock tool executed successfully".to_string(),
                    _meta: None,
                }],
                is_error: Some(false),
                structured_content: None,
                _meta: None,
            })
        } else {
            Err(BackendError::not_supported(format!("Tool not found: {}", request.name)).into())
        }
    }

    async fn list_resources(
        &self,
        _request: PaginatedRequestParam,
    ) -> std::result::Result<ListResourcesResult, Self::Error> {
        Ok(ListResourcesResult {
            resources: vec![],
            next_cursor: None,
        })
    }

    async fn read_resource(
        &self,
        request: ReadResourceRequestParam,
    ) -> std::result::Result<ReadResourceResult, Self::Error> {
        Err(BackendError::not_supported(format!("Resource not found: {}", request.uri)).into())
    }

    async fn list_prompts(
        &self,
        _request: PaginatedRequestParam,
    ) -> std::result::Result<ListPromptsResult, Self::Error> {
        Ok(ListPromptsResult {
            prompts: vec![],
            next_cursor: None,
        })
    }

    async fn get_prompt(
        &self,
        request: GetPromptRequestParam,
    ) -> std::result::Result<GetPromptResult, Self::Error> {
        Err(BackendError::not_supported(format!("Prompt not found: {}", request.name)).into())
    }
}

#[tokio::test]
async fn test_mock_backend_initialization() {
    let backend = MockBackend::initialize(false).await.unwrap();
    assert!(!backend.should_fail);
    assert_eq!(backend.server_name, "Mock Server");
}

#[tokio::test]
async fn test_mock_backend_server_info() {
    let backend = MockBackend::initialize(false).await.unwrap();
    let server_info = backend.get_server_info();

    assert_eq!(server_info.server_info.name, "Mock Server");
    assert_eq!(server_info.server_info.version, "1.0.0");
    assert!(server_info.instructions.is_some());
}

#[tokio::test]
async fn test_mock_backend_health_check() {
    let healthy_backend = MockBackend::initialize(false).await.unwrap();
    assert!(healthy_backend.health_check().await.is_ok());

    let unhealthy_backend = MockBackend::initialize(true).await.unwrap();
    assert!(unhealthy_backend.health_check().await.is_err());
}

#[tokio::test]
async fn test_mock_backend_tools() {
    let backend = MockBackend::initialize(false).await.unwrap();

    // Test list tools
    let tools_result = backend
        .list_tools(PaginatedRequestParam { cursor: None })
        .await
        .unwrap();
    assert_eq!(tools_result.tools.len(), 1);
    assert_eq!(tools_result.tools[0].name, "mock_tool");

    // Test call tool success
    let call_result = backend
        .call_tool(CallToolRequestParam {
            name: "mock_tool".to_string(),
            arguments: Some(serde_json::Value::Object(Default::default())),
        })
        .await
        .unwrap();
    assert_eq!(call_result.is_error, Some(false));
    assert_eq!(call_result.content.len(), 1);

    // Test call tool failure
    let call_result = backend
        .call_tool(CallToolRequestParam {
            name: "nonexistent_tool".to_string(),
            arguments: Some(serde_json::Value::Object(Default::default())),
        })
        .await;
    assert!(call_result.is_err());
}

#[tokio::test]
async fn test_mock_backend_resources() {
    let backend = MockBackend::initialize(false).await.unwrap();

    // Test list resources (empty)
    let resources_result = backend
        .list_resources(PaginatedRequestParam { cursor: None })
        .await
        .unwrap();
    assert!(resources_result.resources.is_empty());

    // Test read resource (not supported)
    let read_result = backend
        .read_resource(ReadResourceRequestParam {
            uri: "test://resource".to_string(),
        })
        .await;
    assert!(read_result.is_err());
}

#[tokio::test]
async fn test_mock_backend_prompts() {
    let backend = MockBackend::initialize(false).await.unwrap();

    // Test list prompts (empty)
    let prompts_result = backend
        .list_prompts(PaginatedRequestParam { cursor: None })
        .await
        .unwrap();
    assert!(prompts_result.prompts.is_empty());

    // Test get prompt (not supported)
    let prompt_result = backend
        .get_prompt(GetPromptRequestParam {
            name: "test_prompt".to_string(),
            arguments: Some(std::collections::HashMap::new()),
        })
        .await;
    assert!(prompt_result.is_err());
}

#[tokio::test]
async fn test_mock_backend_optional_methods() {
    let backend = MockBackend::initialize(false).await.unwrap();

    // Test list resource templates (default implementation)
    let templates_result = backend
        .list_resource_templates(PaginatedRequestParam { cursor: None })
        .await
        .unwrap();
    assert!(templates_result.resource_templates.is_empty());

    // Test subscribe (not supported)
    let subscribe_result = backend
        .subscribe(SubscribeRequestParam {
            uri: "test://resource".to_string(),
        })
        .await;
    // Default implementation now accepts subscriptions (Phase 1 of subscription support)
    assert!(subscribe_result.is_ok());

    // Test unsubscribe (default implementation accepts)
    let unsubscribe_result = backend
        .unsubscribe(UnsubscribeRequestParam {
            uri: "test://resource".to_string(),
        })
        .await;
    // Default implementation now accepts unsubscriptions (Phase 1 of subscription support)
    assert!(unsubscribe_result.is_ok());

    // Test complete (default implementation)
    let complete_result = backend
        .complete(CompleteRequestParam {
            ref_: pulseengine_mcp_protocol::CompletionRef::Resource {
                uri: "test://resource".to_string(),
            },
            argument: pulseengine_mcp_protocol::CompletionArgument {
                name: "test".to_string(),
                value: "test".to_string(),
            },
            context: None,
        })
        .await
        .unwrap();
    assert!(complete_result.completion.values.is_empty());

    // Test set level (default accepts any level)
    let set_level_result = backend
        .set_level(SetLevelRequestParam {
            level: LogLevel::Info,
        })
        .await;
    assert!(set_level_result.is_ok());

    // Test custom method (not supported)
    let custom_result = backend
        .handle_custom_method(
            "custom_method",
            serde_json::Value::Object(Default::default()),
        )
        .await;
    assert!(custom_result.is_err());
}

#[tokio::test]
async fn test_mock_backend_lifecycle_hooks() {
    let backend = MockBackend::initialize(false).await.unwrap();

    // Test lifecycle hooks (default implementations)
    assert!(backend.on_startup().await.is_ok());
    assert!(backend.on_shutdown().await.is_ok());

    let client_info = Implementation::new("test_client", "1.0.0");

    assert!(backend.on_client_connect(&client_info).await.is_ok());
    assert!(backend.on_client_disconnect(&client_info).await.is_ok());
}

// Mock SimpleBackend for testing the blanket implementation
#[derive(Clone)]
struct MockSimpleBackend;

#[async_trait]
impl SimpleBackend for MockSimpleBackend {
    type Error = MockError;
    type Config = ();

    async fn initialize(_config: Self::Config) -> std::result::Result<Self, Self::Error> {
        Ok(MockSimpleBackend)
    }

    fn get_server_info(&self) -> ServerInfo {
        ServerInfo {
            protocol_version: ProtocolVersion::default(),
            capabilities: ServerCapabilities::default(),
            server_info: Implementation::new("Simple Mock Server", "1.0.0"),
            instructions: None,
        }
    }

    async fn health_check(&self) -> std::result::Result<(), Self::Error> {
        Ok(())
    }

    async fn list_tools(
        &self,
        _request: PaginatedRequestParam,
    ) -> std::result::Result<ListToolsResult, Self::Error> {
        Ok(ListToolsResult {
            tools: vec![],
            next_cursor: None,
        })
    }

    async fn call_tool(
        &self,
        _request: CallToolRequestParam,
    ) -> std::result::Result<CallToolResult, Self::Error> {
        Ok(CallToolResult {
            content: vec![],
            is_error: Some(false),
            structured_content: None,
            _meta: None,
        })
    }
}

#[tokio::test]
async fn test_simple_backend_to_mcp_backend() {
    let backend = <MockSimpleBackend as SimpleBackend>::initialize(())
        .await
        .unwrap();

    // Test that SimpleBackend can be used as McpBackend
    let server_info = SimpleBackend::get_server_info(&backend);
    assert_eq!(server_info.server_info.name, "Simple Mock Server");

    // Test default implementations for optional methods
    let resources = backend
        .list_resources(PaginatedRequestParam { cursor: None })
        .await
        .unwrap();
    assert!(resources.resources.is_empty());

    let prompts = backend
        .list_prompts(PaginatedRequestParam { cursor: None })
        .await
        .unwrap();
    assert!(prompts.prompts.is_empty());

    // Test that unimplemented methods return appropriate errors
    let read_result = backend
        .read_resource(ReadResourceRequestParam {
            uri: "test://resource".to_string(),
        })
        .await;
    assert!(read_result.is_err());

    let prompt_result = backend
        .get_prompt(GetPromptRequestParam {
            name: "test".to_string(),
            arguments: None,
        })
        .await;
    assert!(prompt_result.is_err());
}

// Test thread safety
#[test]
fn test_backend_types_send_sync() {
    fn assert_send<T: Send>() {}
    fn assert_sync<T: Sync>() {}

    assert_send::<BackendError>();
    assert_sync::<BackendError>();
    assert_send::<MockBackend>();
    assert_sync::<MockBackend>();
    assert_send::<MockSimpleBackend>();
    assert_sync::<MockSimpleBackend>();
}

#[test]
fn test_backend_error_debug() {
    let err = BackendError::configuration("test");
    let debug_str = format!("{err:?}");
    assert!(debug_str.contains("Configuration"));
    assert!(debug_str.contains("test"));
}