pub enum MiddlewareError {
Unauthenticated(String),
Unauthorized(String),
RateLimitExceeded {
message: String,
retry_after: Option<u64>,
},
InvalidRequest(String),
Internal(String),
Custom {
code: String,
message: String,
},
HttpChallenge {
status: u16,
www_authenticate: String,
body: Option<String>,
},
}Expand description
Errors that can occur during middleware execution
These errors are converted to McpError by the framework and then to
JSON-RPC error responses. Middleware should use semantic error types
rather than creating JSON-RPC errors directly.
§Conversion Chain
MiddlewareError → McpError → JsonRpcError → HTTP/Lambda response§JSON-RPC Error Codes
Each error variant maps to a specific JSON-RPC error code (see error_codes):
Unauthenticated→-32001“Authentication required”Unauthorized→-32002“Permission denied”RateLimitExceeded→-32003“Rate limit exceeded”InvalidRequest→-32600(standard Invalid Request)Internal→-32603(standard Internal error)Custom{code, msg}→ custom code from variant
§Examples
use turul_http_mcp_server::middleware::{MiddlewareError, McpMiddleware, RequestContext, SessionInjection};
use turul_mcp_session_storage::SessionView;
use async_trait::async_trait;
struct ApiKeyAuth {
valid_key: String,
}
#[async_trait]
impl McpMiddleware for ApiKeyAuth {
async fn before_dispatch(
&self,
ctx: &mut RequestContext<'_>,
_session: Option<&dyn SessionView>,
_injection: &mut SessionInjection,
) -> Result<(), MiddlewareError> {
let key = ctx.metadata()
.get("api-key")
.and_then(|v| v.as_str())
.ok_or_else(|| MiddlewareError::Unauthorized("Missing API key".into()))?;
if key != self.valid_key {
return Err(MiddlewareError::Unauthorized("Invalid API key".into()));
}
Ok(())
}
}Variants§
Unauthenticated(String)
Authentication required but not provided
Authentication provided but insufficient permissions
RateLimitExceeded
Rate limit exceeded
InvalidRequest(String)
Request validation failed
Internal(String)
Internal middleware error (should not expose to client)
Custom
Custom error with code and message
Fields
HttpChallenge
HTTP-level challenge response (401/403 with WWW-Authenticate header)
Used for OAuth 2.1 Bearer token challenges. This variant is handled
exclusively at the transport level (pre-session phase) and produces
a raw HTTP response — it NEVER reaches map_middleware_error_to_jsonrpc().
An unreachable!() guard in that function catches programming errors.
Implementations§
Source§impl MiddlewareError
impl MiddlewareError
Sourcepub fn unauthenticated(msg: impl Into<String>) -> Self
pub fn unauthenticated(msg: impl Into<String>) -> Self
Create an unauthenticated error
Create an unauthorized error
Sourcepub fn rate_limit(msg: impl Into<String>, retry_after: Option<u64>) -> Self
pub fn rate_limit(msg: impl Into<String>, retry_after: Option<u64>) -> Self
Create a rate limit error
Sourcepub fn invalid_request(msg: impl Into<String>) -> Self
pub fn invalid_request(msg: impl Into<String>) -> Self
Create an invalid request error
Sourcepub fn custom(code: impl Into<String>, message: impl Into<String>) -> Self
pub fn custom(code: impl Into<String>, message: impl Into<String>) -> Self
Create a custom error
Sourcepub fn http_challenge(status: u16, www_authenticate: impl Into<String>) -> Self
pub fn http_challenge(status: u16, www_authenticate: impl Into<String>) -> Self
Create an HTTP challenge error (401/403 with WWW-Authenticate header)
Used for OAuth 2.1 Bearer token challenges. Handled at transport level only.
Trait Implementations§
Source§impl Clone for MiddlewareError
impl Clone for MiddlewareError
Source§fn clone(&self) -> MiddlewareError
fn clone(&self) -> MiddlewareError
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more