model_context_protocol/
lib.rs

1//! # Model Context Protocol
2//!
3//! A Rust implementation of the Model Context Protocol (MCP).
4//!
5//! This library provides types and traits for building MCP servers and clients.
6//!
7//! ## Example
8//!
9//! ```rust
10//! use model_context_protocol::Protocol;
11//!
12//! // Example usage will be added as the library develops
13//! ```
14
15use serde::{Deserialize, Serialize};
16
17/// The current version of the MCP protocol
18pub const PROTOCOL_VERSION: &str = "0.1.0";
19
20/// Represents the Model Context Protocol interface
21pub trait Protocol {
22    /// Initialize the protocol connection
23    fn initialize(&mut self) -> Result<(), Error>;
24    
25    /// Send a message through the protocol
26    fn send_message(&self, message: Message) -> Result<(), Error>;
27    
28    /// Receive a message from the protocol
29    fn receive_message(&self) -> Result<Message, Error>;
30}
31
32/// A message in the Model Context Protocol
33#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct Message {
35    /// Message ID
36    pub id: String,
37    /// Message type
38    pub message_type: MessageType,
39    /// Message payload
40    pub payload: serde_json::Value,
41}
42
43/// Types of messages in the protocol
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub enum MessageType {
46    /// Request message
47    Request,
48    /// Response message
49    Response,
50    /// Notification message
51    Notification,
52    /// Error message
53    Error,
54}
55
56/// Error types for the protocol
57#[derive(Debug, Clone)]
58pub enum Error {
59    /// Connection error
60    ConnectionError(String),
61    /// Serialization error
62    SerializationError(String),
63    /// Protocol error
64    ProtocolError(String),
65    /// Unknown error
66    Unknown(String),
67}
68
69impl std::fmt::Display for Error {
70    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71        match self {
72            Error::ConnectionError(msg) => write!(f, "Connection error: {}", msg),
73            Error::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
74            Error::ProtocolError(msg) => write!(f, "Protocol error: {}", msg),
75            Error::Unknown(msg) => write!(f, "Unknown error: {}", msg),
76        }
77    }
78}
79
80impl std::error::Error for Error {}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85
86    #[test]
87    fn test_protocol_version() {
88        assert_eq!(PROTOCOL_VERSION, "0.1.0");
89    }
90
91    #[test]
92    fn test_message_serialization() {
93        let msg = Message {
94            id: "test-123".to_string(),
95            message_type: MessageType::Request,
96            payload: serde_json::json!({"test": "data"}),
97        };
98        
99        let serialized = serde_json::to_string(&msg).unwrap();
100        let deserialized: Message = serde_json::from_str(&serialized).unwrap();
101        
102        assert_eq!(msg.id, deserialized.id);
103    }
104}