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
//! Port of `lib/ruby_llm/mcp/error.rb`.
use serde_json::Value;
use crate::error::ErrorResponse;
/// `RubyLLM::MCP::Error`: an MCP server answered with a JSON-RPC error, an unexpected HTTP
/// status, or not at all. Surfaces as `rust_llm::Error::Mcp`.
#[derive(Debug, Clone, Default)]
pub struct McpError {
pub message: String,
/// The JSON-RPC error code, or `None` when the failure had none.
pub code: Option<i64>,
/// Additional information the server attached to the error.
pub data: Option<Value>,
pub response: Option<ErrorResponse>,
/// `MCP::SessionExpired` (`mcp/session_expired.rb`): the session of a server that predates
/// 2026-07-28 has ended (it answered 404, or its stdio process exited), so the client starts
/// a new one.
pub session_expired: bool,
}
impl McpError {
pub fn new(message: impl Into<String>) -> McpError {
McpError {
message: message.into(),
..Default::default()
}
}
/// `SessionExpired.new(message)`.
pub fn session_expired(message: impl Into<String>) -> McpError {
McpError {
message: message.into(),
session_expired: true,
..Default::default()
}
}
}
impl std::fmt::Display for McpError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.message)
}
}
impl std::error::Error for McpError {}
impl From<McpError> for crate::Error {
fn from(error: McpError) -> Self {
crate::Error::Mcp(Box::new(error))
}
}