pulseengine_mcp_protocol/lib.rs
1//! Core Model Context Protocol types and validation
2//!
3//! This crate provides the fundamental types, traits, and validation logic
4//! for the Model Context Protocol. It serves as the foundation for building
5//! MCP servers and clients with strong type safety and validation.
6//!
7//! # Quick Start
8//!
9//! ```rust
10//! use pulseengine_mcp_protocol::{Tool, Content, CallToolResult};
11//! use serde_json::json;
12//!
13//! // Define a tool with proper schema and optional output schema
14//! let tool = Tool {
15//! name: "get_weather".to_string(),
16//! description: "Get current weather for a location".to_string(),
17//! input_schema: json!({
18//! "type": "object",
19//! "properties": {
20//! "location": {
21//! "type": "string",
22//! "description": "City name or coordinates"
23//! }
24//! },
25//! "required": ["location"]
26//! }),
27//! output_schema: Some(json!({
28//! "type": "object",
29//! "properties": {
30//! "temperature": {"type": "string"},
31//! "condition": {"type": "string"}
32//! }
33//! })),
34//! title: None,
35//! annotations: None,
36//! icons: None,
37//! _meta: None,
38//! };
39//!
40//! // Create a tool response with optional structured content
41//! let result = CallToolResult {
42//! content: vec![Content::text("Current weather: 22°C, sunny".to_string())],
43//! is_error: Some(false),
44//! structured_content: Some(json!({
45//! "temperature": "22°C",
46//! "condition": "sunny"
47//! })),
48//! _meta: None,
49//! };
50//! ```
51//!
52//! This crate is currently used in production by the Loxone MCP Server
53//! for home automation with 30+ tools.
54
55pub mod error;
56pub mod errors;
57pub mod model;
58pub mod ui;
59pub mod validation;
60
61#[cfg(test)]
62mod error_tests;
63#[cfg(test)]
64mod lib_tests;
65#[cfg(test)]
66mod model_tests;
67#[cfg(test)]
68mod ui_tests;
69#[cfg(test)]
70mod validation_tests;
71
72// Re-export core types for easy access
73pub use error::{Error, ErrorCode, McpResult, Result};
74pub use errors::{CommonError, CommonResult};
75pub use model::*;
76pub use ui::*;
77pub use validation::Validator;
78
79/// Protocol version constants
80pub const MCP_VERSION: &str = "2025-06-18";
81pub const SUPPORTED_PROTOCOL_VERSIONS: &[&str] = &["2025-06-18", "2025-03-26", "2024-11-05"];
82
83/// Check if a protocol version is supported
84pub fn is_protocol_version_supported(version: &str) -> bool {
85 SUPPORTED_PROTOCOL_VERSIONS.contains(&version)
86}
87
88/// Validate MCP protocol version compatibility
89///
90/// # Errors
91///
92/// Returns an error if the client version is not supported by this server
93pub fn validate_protocol_version(client_version: &str) -> Result<()> {
94 if is_protocol_version_supported(client_version) {
95 Ok(())
96 } else {
97 Err(Error::protocol_version_mismatch(
98 client_version,
99 MCP_VERSION,
100 ))
101 }
102}