turul_mcp_json_rpc_server/lib.rs
1//! # JSON-RPC 2.0 Server Implementation
2//!
3//! A pure, transport-agnostic JSON-RPC 2.0 server implementation with clean domain/protocol separation.
4//! This crate provides the core types and dispatch logic for JSON-RPC without any transport-specific code.
5//!
6//! ## Features
7//! - **JSON-RPC 2.0 Compliance**: Full specification support with proper error handling
8//! - **Type-Safe Error Handling**: Domain errors with automatic protocol conversion
9//! - **Clean Architecture**: Handlers return domain errors, dispatcher owns protocol
10//! - **Transport Agnostic**: Works with HTTP, WebSocket, TCP, etc.
11//! - **Async/Await Support**: Full async support with session context
12//! - **Zero Double-Wrapping**: Clean error handling without intermediate wrappers
13//!
14//! ## Architecture
15//!
16//! ```rust,no_run
17//! # use async_trait::async_trait;
18//! # use turul_mcp_json_rpc_server::{JsonRpcHandler, JsonRpcDispatcher, request::RequestParams};
19//! # use serde_json::Value;
20//! # use std::fmt;
21//! #
22//! # #[derive(Debug)]
23//! # enum MyDomainError {
24//! # InvalidInput(String),
25//! # }
26//! #
27//! # impl fmt::Display for MyDomainError {
28//! # fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29//! # match self {
30//! # MyDomainError::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
31//! # }
32//! # }
33//! # }
34//! #
35//! # impl std::error::Error for MyDomainError {}
36//! #
37//! #
38//! # struct MyHandler;
39//! // Handlers return domain errors only
40//! #[async_trait]
41//! impl JsonRpcHandler for MyHandler {
42//! type Error = MyDomainError; // Not JsonRpcError!
43//!
44//! async fn handle(&self, _method: &str, _params: Option<RequestParams>, _session: Option<turul_mcp_json_rpc_server::SessionContext>) -> Result<Value, MyDomainError> {
45//! Err(MyDomainError::InvalidInput("bad data".to_string()))
46//! }
47//! }
48//!
49//! // Dispatcher converts domain → protocol automatically (example concept)
50//! // let dispatcher = JsonRpcDispatcher::new(); // Actual usage requires ToJsonRpcError trait
51//! ```
52
53pub mod dispatch;
54pub mod error;
55pub mod notification;
56pub mod prelude;
57pub mod request;
58pub mod response;
59pub mod types;
60
61#[cfg(feature = "async")]
62pub mod r#async;
63
64// Re-export main types
65/// JSON-RPC 2.0 error types and standard error codes
66pub use error::{JsonRpcError, JsonRpcErrorCode};
67/// JSON-RPC notification message structure for fire-and-forget communications
68pub use notification::JsonRpcNotification;
69/// JSON-RPC request structure with method and parameters
70pub use request::{JsonRpcRequest, RequestParams};
71/// JSON-RPC response types including success and error variants
72pub use response::{JsonRpcMessage, JsonRpcResponse, ResponseResult};
73/// Core JSON-RPC types for version and request identification
74pub use types::{JsonRpcVersion, RequestId};
75
76#[cfg(feature = "async")]
77pub use r#async::{JsonRpcDispatcher, JsonRpcHandler, SessionContext};
78
79#[cfg(feature = "streams")]
80pub use r#async::streaming::{JsonRpcFrame, StreamingJsonRpcDispatcher, StreamingJsonRpcHandler};
81
82/// JSON-RPC 2.0 version constant
83pub const JSONRPC_VERSION: &str = "2.0";
84
85/// Standard JSON-RPC 2.0 error codes
86pub mod error_codes {
87 pub const PARSE_ERROR: i64 = -32700;
88 pub const INVALID_REQUEST: i64 = -32600;
89 pub const METHOD_NOT_FOUND: i64 = -32601;
90 pub const INVALID_PARAMS: i64 = -32602;
91 pub const INTERNAL_ERROR: i64 = -32603;
92
93 // Server error range: -32099 to -32000
94 pub const SERVER_ERROR_START: i64 = -32099;
95 pub const SERVER_ERROR_END: i64 = -32000;
96}