1use std::fmt;
4use std::future::Future;
5use std::sync::Arc;
6
7use async_trait::async_trait;
8use futures::FutureExt;
9use futures::future::BoxFuture;
10use rmcp::model::{ElicitRequestParams, ElicitResult};
11use serde::{Deserialize, Serialize};
12
13use super::mcp_tool::McpTool;
14use crate::cancel::CancelSignal;
15use crate::error::BoxError;
16use crate::types::JsonValue;
17
18#[derive(Debug, Clone, PartialEq, serde::Serialize)]
20#[non_exhaustive]
21pub struct McpProgress {
22 pub progress: f64,
24 #[serde(skip_serializing_if = "Option::is_none")]
26 pub total: Option<f64>,
27 #[serde(skip_serializing_if = "Option::is_none")]
29 pub message: Option<String>,
30}
31
32impl McpProgress {
33 #[must_use]
35 pub fn new(progress: f64, total: Option<f64>, message: Option<String>) -> Self {
36 Self {
37 progress,
38 total,
39 message,
40 }
41 }
42}
43
44type ProgressFn = dyn Fn(McpProgress) -> BoxFuture<'static, Result<(), BoxError>> + Send + Sync;
45
46#[derive(Clone)]
52pub struct McpProgressCallback(Arc<ProgressFn>);
53
54impl McpProgressCallback {
55 pub fn new<F, Fut>(callback: F) -> Self
57 where
58 F: Fn(McpProgress) -> Fut + Send + Sync + 'static,
59 Fut: Future<Output = Result<(), BoxError>> + Send + 'static,
60 {
61 Self(Arc::new(move |progress| callback(progress).boxed()))
62 }
63
64 pub(crate) async fn call(&self, progress: McpProgress) -> Result<(), BoxError> {
65 (self.0)(progress).await
66 }
67}
68
69impl<F: Fn(McpProgress) + Send + Sync + 'static> From<F> for McpProgressCallback {
70 fn from(callback: F) -> Self {
71 Self(Arc::new(move |progress| {
72 callback(progress);
73 futures::future::ready(Ok(())).boxed()
74 }))
75 }
76}
77
78impl fmt::Debug for McpProgressCallback {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80 f.write_str("McpProgressCallback")
81 }
82}
83
84#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
86#[serde(rename_all = "lowercase")]
87#[non_exhaustive]
88pub enum McpLogLevel {
89 Debug,
91 Info,
93 Notice,
95 Warning,
97 Error,
99 Critical,
101 Alert,
103 Emergency,
105}
106
107#[derive(Debug, Clone, PartialEq, Serialize)]
109#[non_exhaustive]
110pub struct McpLogMessage {
111 pub level: McpLogLevel,
113 #[serde(skip_serializing_if = "Option::is_none")]
115 pub logger: Option<String>,
116 pub data: JsonValue,
118}
119
120impl McpLogMessage {
121 #[must_use]
123 pub fn new(level: McpLogLevel, data: JsonValue) -> Self {
124 Self {
125 level,
126 logger: None,
127 data,
128 }
129 }
130
131 #[must_use]
133 pub fn with_logger(mut self, logger: impl Into<String>) -> Self {
134 self.logger = Some(logger.into());
135 self
136 }
137}
138
139crate::callback::callback! {
140 pub struct McpLogHandler(Fn(&McpLogMessage));
150}
151
152crate::callback::callback! {
153 pub struct ToolsChangedCallback(Fn(Vec<String>, Vec<McpTool>));
156}
157
158#[derive(Debug, Clone)]
160#[non_exhaustive]
161pub struct ElicitationContext {
162 request_id: JsonValue,
163 cancel_signal: CancelSignal,
164}
165
166impl ElicitationContext {
167 pub(crate) fn new(request_id: JsonValue, cancel_signal: CancelSignal) -> Self {
168 Self {
169 request_id,
170 cancel_signal,
171 }
172 }
173
174 #[must_use]
176 pub fn request_id(&self) -> &JsonValue {
177 &self.request_id
178 }
179
180 #[must_use]
182 pub fn cancel_signal(&self) -> &CancelSignal {
183 &self.cancel_signal
184 }
185}
186
187#[async_trait]
204pub trait ElicitationCallback: Send + Sync + 'static {
205 async fn elicit(&self, context: ElicitationContext, params: ElicitRequestParams) -> Result<ElicitResult, BoxError>;
207}
208
209#[async_trait]
210impl<F, Fut> ElicitationCallback for F
211where
212 F: Fn(ElicitationContext, ElicitRequestParams) -> Fut + Send + Sync + 'static,
213 Fut: Future<Output = Result<ElicitResult, BoxError>> + Send + 'static,
214{
215 async fn elicit(&self, context: ElicitationContext, params: ElicitRequestParams) -> Result<ElicitResult, BoxError> {
216 self(context, params).await
217 }
218}