zap-schema 1.0.0

ZAP Schema Compiler - Zero-Copy Application Protocol with whitespace-significant syntax
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
//! ZAP server implementation
//!
//! Cap'n Proto RPC server for AI agent communication.
//!
//! # Example
//!
//! ```rust,ignore
//! use zap::{Server, Config};
//! use zap::server::{ToolHandler, ResourceHandler, PromptHandler, ToolDef};
//! use std::collections::HashMap;
//! use std::sync::Arc;
//!
//! struct MyToolHandler;
//!
//! impl ToolHandler for MyToolHandler {
//!     fn list(&self) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<ToolDef>> + Send + '_>> {
//!         Box::pin(async {
//!             vec![ToolDef {
//!                 name: "echo".into(),
//!                 description: "Echo input".into(),
//!                 schema: b"{}".to_vec(),
//!                 annotations: HashMap::new(),
//!             }]
//!         })
//!     }
//!
//!     fn call(
//!         &self,
//!         _name: &str,
//!         args: &[u8],
//!         _metadata: HashMap<String, String>,
//!     ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Vec<u8>, String>> + Send + '_>> {
//!         let args = args.to_vec();
//!         Box::pin(async move { Ok(args) })
//!     }
//! }
//!
//! #[tokio::main]
//! async fn main() -> zap::Result<()> {
//!     let mut server = Server::new(Config::default());
//!     server.set_tool_handler(Arc::new(MyToolHandler));
//!     server.run().await
//! }
//! ```

use crate::zap_capnp::{
    prompt_message, resource_stream, zap,
};
use crate::{Config, Error, Result};
use capnp::capability::Promise;
use capnp_rpc::{rpc_twoparty_capnp, twoparty, RpcSystem};
use futures::AsyncReadExt;
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use tokio::net::TcpListener;
use tokio::task::LocalSet;

/// Tool definition
#[derive(Debug, Clone)]
pub struct ToolDef {
    pub name: String,
    pub description: String,
    pub schema: Vec<u8>,
    pub annotations: HashMap<String, String>,
}

/// Resource definition
#[derive(Debug, Clone)]
pub struct ResourceDef {
    pub uri: String,
    pub name: String,
    pub description: String,
    pub mime_type: String,
    pub annotations: HashMap<String, String>,
}

/// Resource content
#[derive(Debug, Clone)]
pub enum ResourceContentData {
    Text(String),
    Blob(Vec<u8>),
}

/// Resource content with metadata
#[derive(Debug, Clone)]
pub struct ResourceContentDef {
    pub uri: String,
    pub mime_type: String,
    pub content: ResourceContentData,
}

/// Prompt definition
#[derive(Debug, Clone)]
pub struct PromptDef {
    pub name: String,
    pub description: String,
    pub arguments: Vec<PromptArgument>,
}

/// Prompt argument
#[derive(Debug, Clone)]
pub struct PromptArgument {
    pub name: String,
    pub description: String,
    pub required: bool,
}

/// Prompt message
#[derive(Debug, Clone)]
pub struct PromptMessage {
    pub role: PromptRole,
    pub content: PromptContent,
}

/// Prompt role
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PromptRole {
    User,
    Assistant,
    System,
}

/// Prompt content
#[derive(Debug, Clone)]
pub enum PromptContent {
    Text(String),
    Image { data: Vec<u8>, mime_type: String },
    Resource(ResourceContentDef),
}

/// Tool handler trait
///
/// Implement this trait to handle tool operations.
pub trait ToolHandler: Send + Sync + 'static {
    /// List available tools
    fn list(&self) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<ToolDef>> + Send + '_>>;

    /// Call a tool
    fn call(
        &self,
        name: &str,
        args: &[u8],
        metadata: HashMap<String, String>,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = std::result::Result<Vec<u8>, String>> + Send + '_>>;
}

/// Resource handler trait
///
/// Implement this trait to handle resource operations.
pub trait ResourceHandler: Send + Sync + 'static {
    /// List available resources
    fn list(&self) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<ResourceDef>> + Send + '_>>;

    /// Read a resource
    fn read(
        &self,
        uri: &str,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = std::result::Result<ResourceContentDef, String>> + Send + '_>>;

    /// Subscribe to resource updates (returns a stream receiver)
    fn subscribe(
        &self,
        uri: &str,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = std::result::Result<tokio::sync::mpsc::Receiver<ResourceContentDef>, String>> + Send + '_>>;
}

/// Prompt handler trait
///
/// Implement this trait to handle prompt operations.
pub trait PromptHandler: Send + Sync + 'static {
    /// List available prompts
    fn list(&self) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<PromptDef>> + Send + '_>>;

    /// Get a prompt with arguments
    fn get(
        &self,
        name: &str,
        args: HashMap<String, String>,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = std::result::Result<Vec<PromptMessage>, String>> + Send + '_>>;
}

/// Log handler trait
pub trait LogHandler: Send + Sync + 'static {
    fn log(&self, level: LogLevel, message: &str, data: &[u8]);
}

/// Log level
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogLevel {
    Debug,
    Info,
    Warn,
    Error,
}

/// Default no-op tool handler
pub struct NoopToolHandler;

impl ToolHandler for NoopToolHandler {
    fn list(&self) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<ToolDef>> + Send + '_>> {
        Box::pin(async { Vec::new() })
    }

    fn call(
        &self,
        _name: &str,
        _args: &[u8],
        _metadata: HashMap<String, String>,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = std::result::Result<Vec<u8>, String>> + Send + '_>> {
        Box::pin(async { Err("no tool handler registered".to_string()) })
    }
}

/// Default no-op resource handler
pub struct NoopResourceHandler;

impl ResourceHandler for NoopResourceHandler {
    fn list(&self) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<ResourceDef>> + Send + '_>> {
        Box::pin(async { Vec::new() })
    }

    fn read(
        &self,
        _uri: &str,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = std::result::Result<ResourceContentDef, String>> + Send + '_>> {
        Box::pin(async { Err("no resource handler registered".to_string()) })
    }

    fn subscribe(
        &self,
        _uri: &str,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = std::result::Result<tokio::sync::mpsc::Receiver<ResourceContentDef>, String>> + Send + '_>> {
        Box::pin(async { Err("no resource handler registered".to_string()) })
    }
}

/// Default no-op prompt handler
pub struct NoopPromptHandler;

impl PromptHandler for NoopPromptHandler {
    fn list(&self) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<PromptDef>> + Send + '_>> {
        Box::pin(async { Vec::new() })
    }

    fn get(
        &self,
        _name: &str,
        _args: HashMap<String, String>,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = std::result::Result<Vec<PromptMessage>, String>> + Send + '_>> {
        Box::pin(async { Err("no prompt handler registered".to_string()) })
    }
}

/// Default log handler (uses tracing)
pub struct TracingLogHandler;

impl LogHandler for TracingLogHandler {
    fn log(&self, level: LogLevel, message: &str, _data: &[u8]) {
        match level {
            LogLevel::Debug => tracing::debug!("{}", message),
            LogLevel::Info => tracing::info!("{}", message),
            LogLevel::Warn => tracing::warn!("{}", message),
            LogLevel::Error => tracing::error!("{}", message),
        }
    }
}

/// Server info
#[derive(Debug, Clone)]
pub struct ServerInfoDef {
    pub name: String,
    pub version: String,
    pub tools: bool,
    pub resources: bool,
    pub prompts: bool,
    pub logging: bool,
}

impl Default for ServerInfoDef {
    fn default() -> Self {
        Self {
            name: "zap".to_string(),
            version: crate::VERSION.to_string(),
            tools: true,
            resources: true,
            prompts: true,
            logging: true,
        }
    }
}

/// ZAP Server
///
/// A Cap'n Proto RPC server that implements the Zap interface.
pub struct Server {
    config: Config,
    tool_handler: Arc<dyn ToolHandler>,
    resource_handler: Arc<dyn ResourceHandler>,
    prompt_handler: Arc<dyn PromptHandler>,
    log_handler: Arc<dyn LogHandler>,
    server_info: ServerInfoDef,
}

impl Server {
    /// Create a new server with the given config
    pub fn new(config: Config) -> Self {
        Self {
            config,
            tool_handler: Arc::new(NoopToolHandler),
            resource_handler: Arc::new(NoopResourceHandler),
            prompt_handler: Arc::new(NoopPromptHandler),
            log_handler: Arc::new(TracingLogHandler),
            server_info: ServerInfoDef::default(),
        }
    }

    /// Set the tool handler
    pub fn set_tool_handler(&mut self, handler: Arc<dyn ToolHandler>) {
        self.tool_handler = handler;
    }

    /// Set the resource handler
    pub fn set_resource_handler(&mut self, handler: Arc<dyn ResourceHandler>) {
        self.resource_handler = handler;
    }

    /// Set the prompt handler
    pub fn set_prompt_handler(&mut self, handler: Arc<dyn PromptHandler>) {
        self.prompt_handler = handler;
    }

    /// Set the log handler
    pub fn set_log_handler(&mut self, handler: Arc<dyn LogHandler>) {
        self.log_handler = handler;
    }

    /// Set server info
    pub fn set_server_info(&mut self, info: ServerInfoDef) {
        self.server_info = info;
    }

    /// Run the server
    ///
    /// This runs the Cap'n Proto RPC server on a LocalSet since the RPC system
    /// uses Rc internally and is not Send.
    pub async fn run(&self) -> Result<()> {
        let addr = format!("{}:{}", self.config.listen, self.config.port);
        tracing::info!("ZAP server listening on {}", addr);

        let listener = TcpListener::bind(&addr).await?;

        // Create shared state for all connections
        let state = Arc::new(ServerState {
            tool_handler: self.tool_handler.clone(),
            resource_handler: self.resource_handler.clone(),
            prompt_handler: self.prompt_handler.clone(),
            log_handler: self.log_handler.clone(),
            server_info: self.server_info.clone(),
            client_count: AtomicU64::new(0),
        });

        // Use LocalSet for non-Send RPC futures
        let local = LocalSet::new();

        local.run_until(async move {
            loop {
                tokio::select! {
                    result = listener.accept() => {
                        match result {
                            Ok((stream, addr)) => {
                                let client_id = state.client_count.fetch_add(1, Ordering::SeqCst);
                                tracing::debug!("client {} connected from {}", client_id, addr);

                                let state = state.clone();
                                // Use spawn_local for non-Send futures
                                tokio::task::spawn_local(async move {
                                    if let Err(e) = handle_connection(stream, state, client_id).await {
                                        tracing::error!("client {} error: {}", client_id, e);
                                    }
                                    tracing::debug!("client {} disconnected", client_id);
                                });
                            }
                            Err(e) => {
                                tracing::error!("accept error: {}", e);
                            }
                        }
                    }
                    _ = tokio::signal::ctrl_c() => {
                        tracing::info!("shutting down");
                        break;
                    }
                }
            }
            Ok::<(), Error>(())
        }).await?;

        Ok(())
    }

    /// Run on an existing TCP listener (useful for tests)
    pub async fn run_on_listener(&self, listener: TcpListener) -> Result<()> {
        let state = Arc::new(ServerState {
            tool_handler: self.tool_handler.clone(),
            resource_handler: self.resource_handler.clone(),
            prompt_handler: self.prompt_handler.clone(),
            log_handler: self.log_handler.clone(),
            server_info: self.server_info.clone(),
            client_count: AtomicU64::new(0),
        });

        let local = LocalSet::new();

        local.run_until(async move {
            loop {
                tokio::select! {
                    result = listener.accept() => {
                        match result {
                            Ok((stream, addr)) => {
                                let client_id = state.client_count.fetch_add(1, Ordering::SeqCst);
                                tracing::debug!("client {} connected from {}", client_id, addr);

                                let state = state.clone();
                                tokio::task::spawn_local(async move {
                                    if let Err(e) = handle_connection(stream, state, client_id).await {
                                        tracing::error!("client {} error: {}", client_id, e);
                                    }
                                });
                            }
                            Err(e) => {
                                tracing::error!("accept error: {}", e);
                                break;
                            }
                        }
                    }
                    _ = tokio::signal::ctrl_c() => {
                        break;
                    }
                }
            }
            Ok::<(), Error>(())
        }).await?;

        Ok(())
    }
}

/// Shared server state
struct ServerState {
    tool_handler: Arc<dyn ToolHandler>,
    resource_handler: Arc<dyn ResourceHandler>,
    prompt_handler: Arc<dyn PromptHandler>,
    log_handler: Arc<dyn LogHandler>,
    server_info: ServerInfoDef,
    client_count: AtomicU64,
}

/// Handle a single client connection
async fn handle_connection(
    stream: tokio::net::TcpStream,
    state: Arc<ServerState>,
    _client_id: u64,
) -> Result<()> {
    stream.set_nodelay(true)?;

    // Convert tokio TcpStream to futures-compatible stream
    let stream = tokio_util::compat::TokioAsyncReadCompatExt::compat(stream);
    let (reader, writer) = stream.split();

    // Create the Cap'n Proto RPC network
    let network = twoparty::VatNetwork::new(
        reader,
        writer,
        rpc_twoparty_capnp::Side::Server,
        Default::default(),
    );

    // Create the Zap implementation
    let zap_impl = ZapImpl::new(state);
    let zap_client: zap::Client = capnp_rpc::new_client(zap_impl);

    // Run the RPC system
    let rpc_system = RpcSystem::new(Box::new(network), Some(zap_client.client));

    rpc_system.await.map_err(Error::Capnp)
}

/// Implementation of the Zap interface
struct ZapImpl {
    state: Arc<ServerState>,
}

impl ZapImpl {
    fn new(state: Arc<ServerState>) -> Self {
        Self { state }
    }
}

impl zap::Server for ZapImpl {
    /// Initialize connection
    fn init(
        &mut self,
        params: zap::InitParams,
        mut results: zap::InitResults,
    ) -> Promise<(), capnp::Error> {
        let state = self.state.clone();

        Promise::from_future(async move {
            // Read client info
            let client = params.get()?.get_client()?;
            let client_name = client.get_name()?.to_str()?;
            let client_version = client.get_version()?.to_str()?;

            tracing::info!("client connected: {} v{}", client_name, client_version);

            // Build server info response
            let mut server = results.get().init_server();
            server.set_name(&state.server_info.name);
            server.set_version(&state.server_info.version);

            let mut caps = server.init_capabilities();
            caps.set_tools(state.server_info.tools);
            caps.set_resources(state.server_info.resources);
            caps.set_prompts(state.server_info.prompts);
            caps.set_logging(state.server_info.logging);

            Ok(())
        })
    }

    /// List available tools
    fn list_tools(
        &mut self,
        _params: zap::ListToolsParams,
        mut results: zap::ListToolsResults,
    ) -> Promise<(), capnp::Error> {
        let handler = self.state.tool_handler.clone();

        Promise::from_future(async move {
            let tools = handler.list().await;

            let tool_list = results.get().init_tools();
            let mut builder = tool_list.init_tools(tools.len() as u32);

            for (i, t) in tools.iter().enumerate() {
                let mut tool = builder.reborrow().get(i as u32);
                tool.set_name(&t.name);
                tool.set_description(&t.description);
                tool.set_schema(&t.schema);

                // Set annotations
                if !t.annotations.is_empty() {
                    let annotations = tool.init_annotations();
                    let mut entries = annotations.init_entries(t.annotations.len() as u32);
                    for (j, (k, v)) in t.annotations.iter().enumerate() {
                        let mut entry = entries.reborrow().get(j as u32);
                        entry.set_key(k);
                        entry.set_value(v);
                    }
                }
            }

            Ok(())
        })
    }

    /// Call a tool
    fn call_tool(
        &mut self,
        params: zap::CallToolParams,
        mut results: zap::CallToolResults,
    ) -> Promise<(), capnp::Error> {
        let handler = self.state.tool_handler.clone();

        Promise::from_future(async move {
            let call = params.get()?.get_call()?;
            let id = call.get_id()?.to_str()?;
            let name = call.get_name()?.to_str()?;
            let args = call.get_args()?;

            // Extract metadata
            let mut metadata = HashMap::new();
            if call.has_metadata() {
                let md = call.get_metadata()?;
                if md.has_entries() {
                    for entry in md.get_entries()? {
                        let key = entry.get_key()?.to_str()?;
                        let value = entry.get_value()?.to_str()?;
                        metadata.insert(key.to_string(), value.to_string());
                    }
                }
            }

            // Call the handler
            let result = handler.call(name, args, metadata).await;

            // Build response
            let mut tool_result = results.get().init_result();
            tool_result.set_id(id);

            match result {
                Ok(content) => {
                    tool_result.set_content(&content);
                }
                Err(e) => {
                    tool_result.set_error(&e);
                }
            }

            Ok(())
        })
    }

    /// List available resources
    fn list_resources(
        &mut self,
        _params: zap::ListResourcesParams,
        mut results: zap::ListResourcesResults,
    ) -> Promise<(), capnp::Error> {
        let handler = self.state.resource_handler.clone();

        Promise::from_future(async move {
            let resources = handler.list().await;

            let resource_list = results.get().init_resources();
            let mut builder = resource_list.init_resources(resources.len() as u32);

            for (i, r) in resources.iter().enumerate() {
                let mut resource = builder.reborrow().get(i as u32);
                resource.set_uri(&r.uri);
                resource.set_name(&r.name);
                resource.set_description(&r.description);
                resource.set_mime_type(&r.mime_type);

                if !r.annotations.is_empty() {
                    let annotations = resource.init_annotations();
                    let mut entries = annotations.init_entries(r.annotations.len() as u32);
                    for (j, (k, v)) in r.annotations.iter().enumerate() {
                        let mut entry = entries.reborrow().get(j as u32);
                        entry.set_key(k);
                        entry.set_value(v);
                    }
                }
            }

            Ok(())
        })
    }

    /// Read a resource
    fn read_resource(
        &mut self,
        params: zap::ReadResourceParams,
        mut results: zap::ReadResourceResults,
    ) -> Promise<(), capnp::Error> {
        let handler = self.state.resource_handler.clone();

        Promise::from_future(async move {
            let uri = params.get()?.get_uri()?.to_str()?;

            let result = handler.read(uri).await;

            let mut content = results.get().init_content();

            match result {
                Ok(data) => {
                    content.set_uri(&data.uri);
                    content.set_mime_type(&data.mime_type);

                    match data.content {
                        ResourceContentData::Text(text) => {
                            content.init_content().set_text(&text);
                        }
                        ResourceContentData::Blob(blob) => {
                            content.init_content().set_blob(&blob);
                        }
                    }
                }
                Err(e) => {
                    // Set error as text content
                    content.set_uri(uri);
                    content.set_mime_type("text/plain");
                    content.init_content().set_text(&format!("error: {}", e));
                }
            }

            Ok(())
        })
    }

    /// Subscribe to resource updates
    fn subscribe(
        &mut self,
        params: zap::SubscribeParams,
        mut results: zap::SubscribeResults,
    ) -> Promise<(), capnp::Error> {
        let handler = self.state.resource_handler.clone();

        Promise::from_future(async move {
            let uri = params.get()?.get_uri()?.to_str()?.to_string();

            let result = handler.subscribe(&uri).await;

            match result {
                Ok(receiver) => {
                    let stream_impl = ResourceStreamImpl::new(uri, receiver);
                    let stream_client: resource_stream::Client =
                        capnp_rpc::new_client(stream_impl);
                    results.get().set_stream(stream_client);
                }
                Err(_e) => {
                    // Return an empty stream that immediately completes
                    let (_, receiver) = tokio::sync::mpsc::channel(1);
                    let stream_impl = ResourceStreamImpl::new(uri, receiver);
                    let stream_client: resource_stream::Client =
                        capnp_rpc::new_client(stream_impl);
                    results.get().set_stream(stream_client);
                }
            }

            Ok(())
        })
    }

    /// List available prompts
    fn list_prompts(
        &mut self,
        _params: zap::ListPromptsParams,
        mut results: zap::ListPromptsResults,
    ) -> Promise<(), capnp::Error> {
        let handler = self.state.prompt_handler.clone();

        Promise::from_future(async move {
            let prompts = handler.list().await;

            let prompt_list = results.get().init_prompts();
            let mut builder = prompt_list.init_prompts(prompts.len() as u32);

            for (i, p) in prompts.iter().enumerate() {
                let mut prompt = builder.reborrow().get(i as u32);
                prompt.set_name(&p.name);
                prompt.set_description(&p.description);

                let mut args = prompt.init_arguments(p.arguments.len() as u32);
                for (j, arg) in p.arguments.iter().enumerate() {
                    let mut a = args.reborrow().get(j as u32);
                    a.set_name(&arg.name);
                    a.set_description(&arg.description);
                    a.set_required(arg.required);
                }
            }

            Ok(())
        })
    }

    /// Get a prompt
    fn get_prompt(
        &mut self,
        params: zap::GetPromptParams,
        mut results: zap::GetPromptResults,
    ) -> Promise<(), capnp::Error> {
        let handler = self.state.prompt_handler.clone();

        Promise::from_future(async move {
            let params_reader = params.get()?;
            let name = params_reader.get_name()?.to_str()?;

            // Extract args
            let mut args = HashMap::new();
            if params_reader.has_args() {
                let md = params_reader.get_args()?;
                if md.has_entries() {
                    for entry in md.get_entries()? {
                        let key = entry.get_key()?.to_str()?;
                        let value = entry.get_value()?.to_str()?;
                        args.insert(key.to_string(), value.to_string());
                    }
                }
            }

            let result = handler.get(name, args).await;

            match result {
                Ok(messages) => {
                    let mut builder = results.get().init_messages(messages.len() as u32);

                    for (i, msg) in messages.iter().enumerate() {
                        let mut m = builder.reborrow().get(i as u32);

                        // Set role
                        match msg.role {
                            PromptRole::User => m.set_role(prompt_message::Role::User),
                            PromptRole::Assistant => m.set_role(prompt_message::Role::Assistant),
                            PromptRole::System => m.set_role(prompt_message::Role::System),
                        }

                        // Set content
                        let mut content = m.init_content();
                        match &msg.content {
                            PromptContent::Text(text) => {
                                content.set_text(text);
                            }
                            PromptContent::Image { data, mime_type } => {
                                let mut img = content.init_image();
                                img.set_data(data);
                                img.set_mime_type(mime_type);
                            }
                            PromptContent::Resource(r) => {
                                let mut res = content.init_resource();
                                res.set_uri(&r.uri);
                                res.set_mime_type(&r.mime_type);
                                match &r.content {
                                    ResourceContentData::Text(t) => {
                                        res.init_content().set_text(t);
                                    }
                                    ResourceContentData::Blob(b) => {
                                        res.init_content().set_blob(b);
                                    }
                                }
                            }
                        }
                    }
                }
                Err(_e) => {
                    // Return empty list on error
                    results.get().init_messages(0);
                }
            }

            Ok(())
        })
    }

    /// Log a message
    fn log(
        &mut self,
        params: zap::LogParams,
        _results: zap::LogResults,
    ) -> Promise<(), capnp::Error> {
        let handler = self.state.log_handler.clone();

        Promise::from_future(async move {
            let params_reader = params.get()?;
            let level = match params_reader.get_level()? {
                zap::LogLevel::Debug => LogLevel::Debug,
                zap::LogLevel::Info => LogLevel::Info,
                zap::LogLevel::Warn => LogLevel::Warn,
                zap::LogLevel::Error => LogLevel::Error,
            };
            let message = params_reader.get_message()?.to_str()?;
            let data = params_reader.get_data()?;

            handler.log(level, message, data);

            Ok(())
        })
    }
}

/// Implementation of ResourceStream interface
struct ResourceStreamImpl {
    uri: String,
    receiver: std::cell::RefCell<tokio::sync::mpsc::Receiver<ResourceContentDef>>,
    done: std::cell::Cell<bool>,
}

impl ResourceStreamImpl {
    fn new(uri: String, receiver: tokio::sync::mpsc::Receiver<ResourceContentDef>) -> Self {
        Self {
            uri,
            receiver: std::cell::RefCell::new(receiver),
            done: std::cell::Cell::new(false),
        }
    }
}

impl resource_stream::Server for ResourceStreamImpl {
    fn next(
        &mut self,
        _params: resource_stream::NextParams,
        mut results: resource_stream::NextResults,
    ) -> Promise<(), capnp::Error> {
        if self.done.get() {
            return Promise::from_future(async move {
                results.get().set_done(true);
                Ok(())
            });
        }

        // Since we're running on LocalSet, we don't need Send
        let receiver = &self.receiver;
        let done_flag = &self.done;

        // Use a simple approach: try to receive now
        let mut guard = receiver.borrow_mut();
        match guard.try_recv() {
            Ok(data) => {
                drop(guard);
                Promise::from_future(async move {
                    let mut content = results.get().init_content();
                    content.set_uri(&data.uri);
                    content.set_mime_type(&data.mime_type);

                    match data.content {
                        ResourceContentData::Text(text) => {
                            content.init_content().set_text(&text);
                        }
                        ResourceContentData::Blob(blob) => {
                            content.init_content().set_blob(&blob);
                        }
                    }

                    results.get().set_done(false);
                    Ok(())
                })
            }
            Err(tokio::sync::mpsc::error::TryRecvError::Empty) => {
                // Channel is empty but not closed - return not done
                drop(guard);
                Promise::from_future(async move {
                    results.get().set_done(false);
                    Ok(())
                })
            }
            Err(tokio::sync::mpsc::error::TryRecvError::Disconnected) => {
                done_flag.set(true);
                drop(guard);
                Promise::from_future(async move {
                    results.get().set_done(true);
                    Ok(())
                })
            }
        }
    }

    fn cancel(
        &mut self,
        _params: resource_stream::CancelParams,
        _results: resource_stream::CancelResults,
    ) -> Promise<(), capnp::Error> {
        self.done.set(true);
        Promise::ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_server_new() {
        let config = Config::default();
        let server = Server::new(config);
        assert_eq!(server.server_info.name, "zap");
    }

    #[test]
    fn test_tool_def() {
        let tool = ToolDef {
            name: "test".into(),
            description: "A test tool".into(),
            schema: b"{}".to_vec(),
            annotations: HashMap::new(),
        };
        assert_eq!(tool.name, "test");
    }

    #[test]
    fn test_resource_content_data() {
        let text = ResourceContentData::Text("hello".into());
        assert!(matches!(text, ResourceContentData::Text(_)));

        let blob = ResourceContentData::Blob(vec![1, 2, 3]);
        assert!(matches!(blob, ResourceContentData::Blob(_)));
    }

    #[test]
    fn test_log_levels() {
        assert_ne!(LogLevel::Debug, LogLevel::Error);
        assert_eq!(LogLevel::Info, LogLevel::Info);
    }

    #[test]
    fn test_server_info_default() {
        let info = ServerInfoDef::default();
        assert_eq!(info.name, "zap");
        assert!(info.tools);
        assert!(info.resources);
        assert!(info.prompts);
        assert!(info.logging);
    }
}