feat: add local feature for !Send tool handler support (#740)
* feat: add local feature for !Send tool handler support * fix: gate streamable HTTP transport on not(local) feature
This commit is contained in:
parent
8700e5c920
commit
1a4a52a173
7 changed files with 362 additions and 222 deletions
|
|
@ -4,7 +4,9 @@ use std::sync::Arc;
|
|||
use crate::{
|
||||
error::ErrorData as McpError,
|
||||
model::*,
|
||||
service::{NotificationContext, RequestContext, RoleClient, Service, ServiceRole},
|
||||
service::{
|
||||
MaybeSendFuture, NotificationContext, RequestContext, RoleClient, Service, ServiceRole,
|
||||
},
|
||||
};
|
||||
|
||||
impl<H: ClientHandler> Service<RoleClient> for H {
|
||||
|
|
@ -83,7 +85,7 @@ pub trait ClientHandler: Sized + Send + Sync + 'static {
|
|||
fn ping(
|
||||
&self,
|
||||
context: RequestContext<RoleClient>,
|
||||
) -> impl Future<Output = Result<(), McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<(), McpError>> + MaybeSendFuture + '_ {
|
||||
std::future::ready(Ok(()))
|
||||
}
|
||||
|
||||
|
|
@ -91,7 +93,7 @@ pub trait ClientHandler: Sized + Send + Sync + 'static {
|
|||
&self,
|
||||
params: CreateMessageRequestParams,
|
||||
context: RequestContext<RoleClient>,
|
||||
) -> impl Future<Output = Result<CreateMessageResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<CreateMessageResult, McpError>> + MaybeSendFuture + '_ {
|
||||
std::future::ready(Err(
|
||||
McpError::method_not_found::<CreateMessageRequestMethod>(),
|
||||
))
|
||||
|
|
@ -100,7 +102,7 @@ pub trait ClientHandler: Sized + Send + Sync + 'static {
|
|||
fn list_roots(
|
||||
&self,
|
||||
context: RequestContext<RoleClient>,
|
||||
) -> impl Future<Output = Result<ListRootsResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<ListRootsResult, McpError>> + MaybeSendFuture + '_ {
|
||||
std::future::ready(Ok(ListRootsResult::default()))
|
||||
}
|
||||
|
||||
|
|
@ -162,7 +164,8 @@ pub trait ClientHandler: Sized + Send + Sync + 'static {
|
|||
&self,
|
||||
request: CreateElicitationRequestParams,
|
||||
context: RequestContext<RoleClient>,
|
||||
) -> impl Future<Output = Result<CreateElicitationResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<CreateElicitationResult, McpError>> + MaybeSendFuture + '_
|
||||
{
|
||||
// Default implementation declines all requests - real clients should override this
|
||||
let _ = (request, context);
|
||||
std::future::ready(Ok(CreateElicitationResult {
|
||||
|
|
@ -175,7 +178,7 @@ pub trait ClientHandler: Sized + Send + Sync + 'static {
|
|||
&self,
|
||||
request: CustomRequest,
|
||||
context: RequestContext<RoleClient>,
|
||||
) -> impl Future<Output = Result<CustomResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<CustomResult, McpError>> + MaybeSendFuture + '_ {
|
||||
let CustomRequest { method, .. } = request;
|
||||
let _ = context;
|
||||
std::future::ready(Err(McpError::new(
|
||||
|
|
@ -189,46 +192,46 @@ pub trait ClientHandler: Sized + Send + Sync + 'static {
|
|||
&self,
|
||||
params: CancelledNotificationParam,
|
||||
context: NotificationContext<RoleClient>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
std::future::ready(())
|
||||
}
|
||||
fn on_progress(
|
||||
&self,
|
||||
params: ProgressNotificationParam,
|
||||
context: NotificationContext<RoleClient>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
std::future::ready(())
|
||||
}
|
||||
fn on_logging_message(
|
||||
&self,
|
||||
params: LoggingMessageNotificationParam,
|
||||
context: NotificationContext<RoleClient>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
std::future::ready(())
|
||||
}
|
||||
fn on_resource_updated(
|
||||
&self,
|
||||
params: ResourceUpdatedNotificationParam,
|
||||
context: NotificationContext<RoleClient>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
std::future::ready(())
|
||||
}
|
||||
fn on_resource_list_changed(
|
||||
&self,
|
||||
context: NotificationContext<RoleClient>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
std::future::ready(())
|
||||
}
|
||||
fn on_tool_list_changed(
|
||||
&self,
|
||||
context: NotificationContext<RoleClient>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
std::future::ready(())
|
||||
}
|
||||
fn on_prompt_list_changed(
|
||||
&self,
|
||||
context: NotificationContext<RoleClient>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
std::future::ready(())
|
||||
}
|
||||
|
||||
|
|
@ -236,14 +239,14 @@ pub trait ClientHandler: Sized + Send + Sync + 'static {
|
|||
&self,
|
||||
params: ElicitationResponseNotificationParam,
|
||||
context: NotificationContext<RoleClient>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
std::future::ready(())
|
||||
}
|
||||
fn on_custom_notification(
|
||||
&self,
|
||||
notification: CustomNotification,
|
||||
context: NotificationContext<RoleClient>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
let _ = (notification, context);
|
||||
std::future::ready(())
|
||||
}
|
||||
|
|
@ -269,7 +272,7 @@ macro_rules! impl_client_handler_for_wrapper {
|
|||
fn ping(
|
||||
&self,
|
||||
context: RequestContext<RoleClient>,
|
||||
) -> impl Future<Output = Result<(), McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<(), McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).ping(context)
|
||||
}
|
||||
|
||||
|
|
@ -277,14 +280,14 @@ macro_rules! impl_client_handler_for_wrapper {
|
|||
&self,
|
||||
params: CreateMessageRequestParams,
|
||||
context: RequestContext<RoleClient>,
|
||||
) -> impl Future<Output = Result<CreateMessageResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<CreateMessageResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).create_message(params, context)
|
||||
}
|
||||
|
||||
fn list_roots(
|
||||
&self,
|
||||
context: RequestContext<RoleClient>,
|
||||
) -> impl Future<Output = Result<ListRootsResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<ListRootsResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).list_roots(context)
|
||||
}
|
||||
|
||||
|
|
@ -292,7 +295,7 @@ macro_rules! impl_client_handler_for_wrapper {
|
|||
&self,
|
||||
request: CreateElicitationRequestParams,
|
||||
context: RequestContext<RoleClient>,
|
||||
) -> impl Future<Output = Result<CreateElicitationResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<CreateElicitationResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).create_elicitation(request, context)
|
||||
}
|
||||
|
||||
|
|
@ -300,7 +303,7 @@ macro_rules! impl_client_handler_for_wrapper {
|
|||
&self,
|
||||
request: CustomRequest,
|
||||
context: RequestContext<RoleClient>,
|
||||
) -> impl Future<Output = Result<CustomResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<CustomResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).on_custom_request(request, context)
|
||||
}
|
||||
|
||||
|
|
@ -308,7 +311,7 @@ macro_rules! impl_client_handler_for_wrapper {
|
|||
&self,
|
||||
params: CancelledNotificationParam,
|
||||
context: NotificationContext<RoleClient>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
(**self).on_cancelled(params, context)
|
||||
}
|
||||
|
||||
|
|
@ -316,7 +319,7 @@ macro_rules! impl_client_handler_for_wrapper {
|
|||
&self,
|
||||
params: ProgressNotificationParam,
|
||||
context: NotificationContext<RoleClient>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
(**self).on_progress(params, context)
|
||||
}
|
||||
|
||||
|
|
@ -324,7 +327,7 @@ macro_rules! impl_client_handler_for_wrapper {
|
|||
&self,
|
||||
params: LoggingMessageNotificationParam,
|
||||
context: NotificationContext<RoleClient>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
(**self).on_logging_message(params, context)
|
||||
}
|
||||
|
||||
|
|
@ -332,28 +335,28 @@ macro_rules! impl_client_handler_for_wrapper {
|
|||
&self,
|
||||
params: ResourceUpdatedNotificationParam,
|
||||
context: NotificationContext<RoleClient>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
(**self).on_resource_updated(params, context)
|
||||
}
|
||||
|
||||
fn on_resource_list_changed(
|
||||
&self,
|
||||
context: NotificationContext<RoleClient>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
(**self).on_resource_list_changed(context)
|
||||
}
|
||||
|
||||
fn on_tool_list_changed(
|
||||
&self,
|
||||
context: NotificationContext<RoleClient>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
(**self).on_tool_list_changed(context)
|
||||
}
|
||||
|
||||
fn on_prompt_list_changed(
|
||||
&self,
|
||||
context: NotificationContext<RoleClient>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
(**self).on_prompt_list_changed(context)
|
||||
}
|
||||
|
||||
|
|
@ -361,7 +364,7 @@ macro_rules! impl_client_handler_for_wrapper {
|
|||
&self,
|
||||
notification: CustomNotification,
|
||||
context: NotificationContext<RoleClient>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
(**self).on_custom_notification(notification, context)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,10 @@ use std::sync::Arc;
|
|||
use crate::{
|
||||
error::ErrorData as McpError,
|
||||
model::{TaskSupport, *},
|
||||
service::{NotificationContext, RequestContext, RoleServer, Service, ServiceRole},
|
||||
service::{
|
||||
MaybeSend, MaybeSendFuture, NotificationContext, RequestContext, RoleServer, Service,
|
||||
ServiceRole,
|
||||
},
|
||||
};
|
||||
|
||||
pub mod common;
|
||||
|
|
@ -159,12 +162,16 @@ impl<H: ServerHandler> Service<RoleServer> for H {
|
|||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub trait ServerHandler: Sized + Send + Sync + 'static {
|
||||
#[allow(
|
||||
private_bounds,
|
||||
reason = "MaybeSend is a sealed conditional Send + Sync alias"
|
||||
)]
|
||||
pub trait ServerHandler: Sized + MaybeSend + 'static {
|
||||
fn enqueue_task(
|
||||
&self,
|
||||
_request: CallToolRequestParams,
|
||||
_context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<CreateTaskResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<CreateTaskResult, McpError>> + MaybeSendFuture + '_ {
|
||||
std::future::ready(Err(McpError::internal_error(
|
||||
"Task processing not implemented".to_string(),
|
||||
None,
|
||||
|
|
@ -173,7 +180,7 @@ pub trait ServerHandler: Sized + Send + Sync + 'static {
|
|||
fn ping(
|
||||
&self,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<(), McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<(), McpError>> + MaybeSendFuture + '_ {
|
||||
std::future::ready(Ok(()))
|
||||
}
|
||||
// handle requests
|
||||
|
|
@ -181,7 +188,7 @@ pub trait ServerHandler: Sized + Send + Sync + 'static {
|
|||
&self,
|
||||
request: InitializeRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<InitializeResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<InitializeResult, McpError>> + MaybeSendFuture + '_ {
|
||||
if context.peer.peer_info().is_none() {
|
||||
context.peer.set_peer_info(request);
|
||||
}
|
||||
|
|
@ -191,49 +198,50 @@ pub trait ServerHandler: Sized + Send + Sync + 'static {
|
|||
&self,
|
||||
request: CompleteRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<CompleteResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<CompleteResult, McpError>> + MaybeSendFuture + '_ {
|
||||
std::future::ready(Ok(CompleteResult::default()))
|
||||
}
|
||||
fn set_level(
|
||||
&self,
|
||||
request: SetLevelRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<(), McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<(), McpError>> + MaybeSendFuture + '_ {
|
||||
std::future::ready(Err(McpError::method_not_found::<SetLevelRequestMethod>()))
|
||||
}
|
||||
fn get_prompt(
|
||||
&self,
|
||||
request: GetPromptRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<GetPromptResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<GetPromptResult, McpError>> + MaybeSendFuture + '_ {
|
||||
std::future::ready(Err(McpError::method_not_found::<GetPromptRequestMethod>()))
|
||||
}
|
||||
fn list_prompts(
|
||||
&self,
|
||||
request: Option<PaginatedRequestParams>,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<ListPromptsResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<ListPromptsResult, McpError>> + MaybeSendFuture + '_ {
|
||||
std::future::ready(Ok(ListPromptsResult::default()))
|
||||
}
|
||||
fn list_resources(
|
||||
&self,
|
||||
request: Option<PaginatedRequestParams>,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<ListResourcesResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<ListResourcesResult, McpError>> + MaybeSendFuture + '_ {
|
||||
std::future::ready(Ok(ListResourcesResult::default()))
|
||||
}
|
||||
fn list_resource_templates(
|
||||
&self,
|
||||
request: Option<PaginatedRequestParams>,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<ListResourceTemplatesResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<ListResourceTemplatesResult, McpError>> + MaybeSendFuture + '_
|
||||
{
|
||||
std::future::ready(Ok(ListResourceTemplatesResult::default()))
|
||||
}
|
||||
fn read_resource(
|
||||
&self,
|
||||
request: ReadResourceRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<ReadResourceResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<ReadResourceResult, McpError>> + MaybeSendFuture + '_ {
|
||||
std::future::ready(Err(
|
||||
McpError::method_not_found::<ReadResourceRequestMethod>(),
|
||||
))
|
||||
|
|
@ -242,28 +250,28 @@ pub trait ServerHandler: Sized + Send + Sync + 'static {
|
|||
&self,
|
||||
request: SubscribeRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<(), McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<(), McpError>> + MaybeSendFuture + '_ {
|
||||
std::future::ready(Err(McpError::method_not_found::<SubscribeRequestMethod>()))
|
||||
}
|
||||
fn unsubscribe(
|
||||
&self,
|
||||
request: UnsubscribeRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<(), McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<(), McpError>> + MaybeSendFuture + '_ {
|
||||
std::future::ready(Err(McpError::method_not_found::<UnsubscribeRequestMethod>()))
|
||||
}
|
||||
fn call_tool(
|
||||
&self,
|
||||
request: CallToolRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<CallToolResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<CallToolResult, McpError>> + MaybeSendFuture + '_ {
|
||||
std::future::ready(Err(McpError::method_not_found::<CallToolRequestMethod>()))
|
||||
}
|
||||
fn list_tools(
|
||||
&self,
|
||||
request: Option<PaginatedRequestParams>,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<ListToolsResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<ListToolsResult, McpError>> + MaybeSendFuture + '_ {
|
||||
std::future::ready(Ok(ListToolsResult::default()))
|
||||
}
|
||||
/// Get a tool definition by name.
|
||||
|
|
@ -277,7 +285,7 @@ pub trait ServerHandler: Sized + Send + Sync + 'static {
|
|||
&self,
|
||||
request: CustomRequest,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<CustomResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<CustomResult, McpError>> + MaybeSendFuture + '_ {
|
||||
let CustomRequest { method, .. } = request;
|
||||
let _ = context;
|
||||
std::future::ready(Err(McpError::new(
|
||||
|
|
@ -291,34 +299,34 @@ pub trait ServerHandler: Sized + Send + Sync + 'static {
|
|||
&self,
|
||||
notification: CancelledNotificationParam,
|
||||
context: NotificationContext<RoleServer>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
std::future::ready(())
|
||||
}
|
||||
fn on_progress(
|
||||
&self,
|
||||
notification: ProgressNotificationParam,
|
||||
context: NotificationContext<RoleServer>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
std::future::ready(())
|
||||
}
|
||||
fn on_initialized(
|
||||
&self,
|
||||
context: NotificationContext<RoleServer>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
tracing::info!("client initialized");
|
||||
std::future::ready(())
|
||||
}
|
||||
fn on_roots_list_changed(
|
||||
&self,
|
||||
context: NotificationContext<RoleServer>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
std::future::ready(())
|
||||
}
|
||||
fn on_custom_notification(
|
||||
&self,
|
||||
notification: CustomNotification,
|
||||
context: NotificationContext<RoleServer>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
let _ = (notification, context);
|
||||
std::future::ready(())
|
||||
}
|
||||
|
|
@ -331,7 +339,7 @@ pub trait ServerHandler: Sized + Send + Sync + 'static {
|
|||
&self,
|
||||
request: Option<PaginatedRequestParams>,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<ListTasksResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<ListTasksResult, McpError>> + MaybeSendFuture + '_ {
|
||||
std::future::ready(Err(McpError::method_not_found::<ListTasksMethod>()))
|
||||
}
|
||||
|
||||
|
|
@ -339,7 +347,7 @@ pub trait ServerHandler: Sized + Send + Sync + 'static {
|
|||
&self,
|
||||
request: GetTaskInfoParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<GetTaskResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<GetTaskResult, McpError>> + MaybeSendFuture + '_ {
|
||||
let _ = (request, context);
|
||||
std::future::ready(Err(McpError::method_not_found::<GetTaskInfoMethod>()))
|
||||
}
|
||||
|
|
@ -348,7 +356,7 @@ pub trait ServerHandler: Sized + Send + Sync + 'static {
|
|||
&self,
|
||||
request: GetTaskResultParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<GetTaskPayloadResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<GetTaskPayloadResult, McpError>> + MaybeSendFuture + '_ {
|
||||
let _ = (request, context);
|
||||
std::future::ready(Err(McpError::method_not_found::<GetTaskResultMethod>()))
|
||||
}
|
||||
|
|
@ -357,7 +365,7 @@ pub trait ServerHandler: Sized + Send + Sync + 'static {
|
|||
&self,
|
||||
request: CancelTaskParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<CancelTaskResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<CancelTaskResult, McpError>> + MaybeSendFuture + '_ {
|
||||
let _ = (request, context);
|
||||
std::future::ready(Err(McpError::method_not_found::<CancelTaskMethod>()))
|
||||
}
|
||||
|
|
@ -370,14 +378,14 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: CallToolRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<CreateTaskResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<CreateTaskResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).enqueue_task(request, context)
|
||||
}
|
||||
|
||||
fn ping(
|
||||
&self,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<(), McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<(), McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).ping(context)
|
||||
}
|
||||
|
||||
|
|
@ -385,7 +393,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: InitializeRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<InitializeResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<InitializeResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).initialize(request, context)
|
||||
}
|
||||
|
||||
|
|
@ -393,7 +401,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: CompleteRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<CompleteResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<CompleteResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).complete(request, context)
|
||||
}
|
||||
|
||||
|
|
@ -401,7 +409,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: SetLevelRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<(), McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<(), McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).set_level(request, context)
|
||||
}
|
||||
|
||||
|
|
@ -409,7 +417,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: GetPromptRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<GetPromptResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<GetPromptResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).get_prompt(request, context)
|
||||
}
|
||||
|
||||
|
|
@ -417,7 +425,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: Option<PaginatedRequestParams>,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<ListPromptsResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<ListPromptsResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).list_prompts(request, context)
|
||||
}
|
||||
|
||||
|
|
@ -425,7 +433,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: Option<PaginatedRequestParams>,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<ListResourcesResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<ListResourcesResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).list_resources(request, context)
|
||||
}
|
||||
|
||||
|
|
@ -433,7 +441,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: Option<PaginatedRequestParams>,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<ListResourceTemplatesResult, McpError>> + Send + '_
|
||||
) -> impl Future<Output = Result<ListResourceTemplatesResult, McpError>> + MaybeSendFuture + '_
|
||||
{
|
||||
(**self).list_resource_templates(request, context)
|
||||
}
|
||||
|
|
@ -442,7 +450,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: ReadResourceRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<ReadResourceResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<ReadResourceResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).read_resource(request, context)
|
||||
}
|
||||
|
||||
|
|
@ -450,7 +458,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: SubscribeRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<(), McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<(), McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).subscribe(request, context)
|
||||
}
|
||||
|
||||
|
|
@ -458,7 +466,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: UnsubscribeRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<(), McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<(), McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).unsubscribe(request, context)
|
||||
}
|
||||
|
||||
|
|
@ -466,7 +474,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: CallToolRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<CallToolResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<CallToolResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).call_tool(request, context)
|
||||
}
|
||||
|
||||
|
|
@ -474,7 +482,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: Option<PaginatedRequestParams>,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<ListToolsResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<ListToolsResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).list_tools(request, context)
|
||||
}
|
||||
|
||||
|
|
@ -486,7 +494,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: CustomRequest,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<CustomResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<CustomResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).on_custom_request(request, context)
|
||||
}
|
||||
|
||||
|
|
@ -494,7 +502,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
notification: CancelledNotificationParam,
|
||||
context: NotificationContext<RoleServer>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
(**self).on_cancelled(notification, context)
|
||||
}
|
||||
|
||||
|
|
@ -502,21 +510,21 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
notification: ProgressNotificationParam,
|
||||
context: NotificationContext<RoleServer>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
(**self).on_progress(notification, context)
|
||||
}
|
||||
|
||||
fn on_initialized(
|
||||
&self,
|
||||
context: NotificationContext<RoleServer>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
(**self).on_initialized(context)
|
||||
}
|
||||
|
||||
fn on_roots_list_changed(
|
||||
&self,
|
||||
context: NotificationContext<RoleServer>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
(**self).on_roots_list_changed(context)
|
||||
}
|
||||
|
||||
|
|
@ -524,7 +532,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
notification: CustomNotification,
|
||||
context: NotificationContext<RoleServer>,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
|
||||
(**self).on_custom_notification(notification, context)
|
||||
}
|
||||
|
||||
|
|
@ -536,7 +544,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: Option<PaginatedRequestParams>,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<ListTasksResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<ListTasksResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).list_tasks(request, context)
|
||||
}
|
||||
|
||||
|
|
@ -544,7 +552,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: GetTaskInfoParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<GetTaskResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<GetTaskResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).get_task_info(request, context)
|
||||
}
|
||||
|
||||
|
|
@ -552,7 +560,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: GetTaskResultParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<GetTaskPayloadResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<GetTaskPayloadResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).get_task_result(request, context)
|
||||
}
|
||||
|
||||
|
|
@ -560,7 +568,7 @@ macro_rules! impl_server_handler_for_wrapper {
|
|||
&self,
|
||||
request: CancelTaskParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<CancelTaskResult, McpError>> + Send + '_ {
|
||||
) -> impl Future<Output = Result<CancelTaskResult, McpError>> + MaybeSendFuture + '_ {
|
||||
(**self).cancel_task(request, context)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@
|
|||
|
||||
use std::{future::Future, marker::PhantomData};
|
||||
|
||||
use futures::future::{BoxFuture, FutureExt};
|
||||
#[cfg(not(feature = "local"))]
|
||||
use futures::future::BoxFuture;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use super::common::{AsRequestContext, FromContextPart};
|
||||
|
|
@ -15,7 +16,7 @@ use crate::{
|
|||
RoleServer,
|
||||
handler::server::wrapper::Parameters,
|
||||
model::{GetPromptResult, PromptMessage},
|
||||
service::RequestContext,
|
||||
service::{MaybeBoxFuture, MaybeSend, MaybeSendFuture, RequestContext},
|
||||
};
|
||||
|
||||
/// Context for prompt retrieval operations
|
||||
|
|
@ -57,14 +58,23 @@ pub trait GetPromptHandler<S, A> {
|
|||
fn handle(
|
||||
self,
|
||||
context: PromptContext<'_, S>,
|
||||
) -> BoxFuture<'_, Result<GetPromptResult, crate::ErrorData>>;
|
||||
) -> MaybeBoxFuture<'_, Result<GetPromptResult, crate::ErrorData>>;
|
||||
}
|
||||
|
||||
/// Type alias for dynamic prompt handlers
|
||||
#[cfg(not(feature = "local"))]
|
||||
pub type DynGetPromptHandler<S> = dyn for<'a> Fn(PromptContext<'a, S>) -> BoxFuture<'a, Result<GetPromptResult, crate::ErrorData>>
|
||||
+ Send
|
||||
+ Sync;
|
||||
|
||||
#[cfg(feature = "local")]
|
||||
pub type DynGetPromptHandler<S> = dyn for<'a> Fn(
|
||||
PromptContext<'a, S>,
|
||||
) -> futures::future::LocalBoxFuture<
|
||||
'a,
|
||||
Result<GetPromptResult, crate::ErrorData>,
|
||||
>;
|
||||
|
||||
/// Adapter type for async methods that return `Vec<PromptMessage>`
|
||||
pub struct AsyncMethodAdapter<T>(PhantomData<T>);
|
||||
|
||||
|
|
@ -191,31 +201,31 @@ macro_rules! impl_prompt_handler_for {
|
|||
impl<$($Tn,)* S, F, R> GetPromptHandler<S, ($($Tn,)*)> for F
|
||||
where
|
||||
$(
|
||||
$Tn: for<'a> FromContextPart<PromptContext<'a, S>> + Send,
|
||||
$Tn: for<'a> FromContextPart<PromptContext<'a, S>> + MaybeSendFuture,
|
||||
)*
|
||||
F: FnOnce(&S, $($Tn,)*) -> BoxFuture<'_, R> + Send,
|
||||
R: IntoGetPromptResult + Send + 'static,
|
||||
S: Send + Sync + 'static,
|
||||
F: FnOnce(&S, $($Tn,)*) -> MaybeBoxFuture<'_, R> + MaybeSendFuture,
|
||||
R: IntoGetPromptResult + MaybeSendFuture + 'static,
|
||||
S: MaybeSend + 'static,
|
||||
{
|
||||
#[allow(unused_variables, non_snake_case, unused_mut)]
|
||||
fn handle(
|
||||
self,
|
||||
mut context: PromptContext<'_, S>,
|
||||
) -> BoxFuture<'_, Result<GetPromptResult, crate::ErrorData>>
|
||||
) -> MaybeBoxFuture<'_, Result<GetPromptResult, crate::ErrorData>>
|
||||
{
|
||||
$(
|
||||
let result = $Tn::from_context_part(&mut context);
|
||||
let $Tn = match result {
|
||||
Ok(value) => value,
|
||||
Err(e) => return std::future::ready(Err(e)).boxed(),
|
||||
Err(e) => return Box::pin(std::future::ready(Err(e))),
|
||||
};
|
||||
)*
|
||||
let service = context.server;
|
||||
let fut = self(service, $($Tn,)*);
|
||||
async move {
|
||||
Box::pin(async move {
|
||||
let result = fut.await;
|
||||
result.into_get_prompt_result()
|
||||
}.boxed()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -224,28 +234,28 @@ macro_rules! impl_prompt_handler_for {
|
|||
impl<$($Tn,)* S, F, R> GetPromptHandler<S, SyncPromptMethodAdapter<($($Tn,)*), R>> for F
|
||||
where
|
||||
$(
|
||||
$Tn: for<'a> FromContextPart<PromptContext<'a, S>> + Send,
|
||||
$Tn: for<'a> FromContextPart<PromptContext<'a, S>> + MaybeSendFuture,
|
||||
)*
|
||||
F: FnOnce(&S, $($Tn,)*) -> R + Send,
|
||||
R: IntoGetPromptResult + Send,
|
||||
S: Send + Sync,
|
||||
F: FnOnce(&S, $($Tn,)*) -> R + MaybeSendFuture,
|
||||
R: IntoGetPromptResult + MaybeSendFuture,
|
||||
S: MaybeSend,
|
||||
{
|
||||
#[allow(unused_variables, non_snake_case, unused_mut)]
|
||||
fn handle(
|
||||
self,
|
||||
mut context: PromptContext<'_, S>,
|
||||
) -> BoxFuture<'_, Result<GetPromptResult, crate::ErrorData>>
|
||||
) -> MaybeBoxFuture<'_, Result<GetPromptResult, crate::ErrorData>>
|
||||
{
|
||||
$(
|
||||
let result = $Tn::from_context_part(&mut context);
|
||||
let $Tn = match result {
|
||||
Ok(value) => value,
|
||||
Err(e) => return std::future::ready(Err(e)).boxed(),
|
||||
Err(e) => return Box::pin(std::future::ready(Err(e))),
|
||||
};
|
||||
)*
|
||||
let service = context.server;
|
||||
let result = self(service, $($Tn,)*);
|
||||
std::future::ready(result.into_get_prompt_result()).boxed()
|
||||
Box::pin(std::future::ready(result.into_get_prompt_result()))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -254,25 +264,25 @@ macro_rules! impl_prompt_handler_for {
|
|||
impl<$($Tn,)* S, F, Fut, R> GetPromptHandler<S, AsyncPromptAdapter<($($Tn,)*), Fut, R>> for F
|
||||
where
|
||||
$(
|
||||
$Tn: for<'a> FromContextPart<PromptContext<'a, S>> + Send + 'static,
|
||||
$Tn: for<'a> FromContextPart<PromptContext<'a, S>> + MaybeSendFuture + 'static,
|
||||
)*
|
||||
F: FnOnce($($Tn,)*) -> Fut + Send + 'static,
|
||||
Fut: Future<Output = Result<R, crate::ErrorData>> + Send + 'static,
|
||||
R: IntoGetPromptResult + Send + 'static,
|
||||
S: Send + Sync + 'static,
|
||||
F: FnOnce($($Tn,)*) -> Fut + MaybeSendFuture + 'static,
|
||||
Fut: Future<Output = Result<R, crate::ErrorData>> + MaybeSendFuture + 'static,
|
||||
R: IntoGetPromptResult + MaybeSendFuture + 'static,
|
||||
S: MaybeSend + 'static,
|
||||
{
|
||||
#[allow(unused_variables, non_snake_case, unused_mut)]
|
||||
fn handle(
|
||||
self,
|
||||
mut context: PromptContext<'_, S>,
|
||||
) -> BoxFuture<'_, Result<GetPromptResult, crate::ErrorData>>
|
||||
) -> MaybeBoxFuture<'_, Result<GetPromptResult, crate::ErrorData>>
|
||||
{
|
||||
// Extract all parameters before moving into the async block
|
||||
$(
|
||||
let result = $Tn::from_context_part(&mut context);
|
||||
let $Tn = match result {
|
||||
Ok(value) => value,
|
||||
Err(e) => return std::future::ready(Err(e)).boxed(),
|
||||
Err(e) => return Box::pin(std::future::ready(Err(e))),
|
||||
};
|
||||
)*
|
||||
|
||||
|
|
@ -290,27 +300,27 @@ macro_rules! impl_prompt_handler_for {
|
|||
impl<$($Tn,)* S, F, R> GetPromptHandler<S, SyncPromptAdapter<($($Tn,)*), R>> for F
|
||||
where
|
||||
$(
|
||||
$Tn: for<'a> FromContextPart<PromptContext<'a, S>> + Send + 'static,
|
||||
$Tn: for<'a> FromContextPart<PromptContext<'a, S>> + MaybeSendFuture + 'static,
|
||||
)*
|
||||
F: FnOnce($($Tn,)*) -> Result<R, crate::ErrorData> + Send + 'static,
|
||||
R: IntoGetPromptResult + Send + 'static,
|
||||
S: Send + Sync,
|
||||
F: FnOnce($($Tn,)*) -> Result<R, crate::ErrorData> + MaybeSendFuture + 'static,
|
||||
R: IntoGetPromptResult + MaybeSendFuture + 'static,
|
||||
S: MaybeSend,
|
||||
{
|
||||
#[allow(unused_variables, non_snake_case, unused_mut)]
|
||||
fn handle(
|
||||
self,
|
||||
mut context: PromptContext<'_, S>,
|
||||
) -> BoxFuture<'_, Result<GetPromptResult, crate::ErrorData>>
|
||||
) -> MaybeBoxFuture<'_, Result<GetPromptResult, crate::ErrorData>>
|
||||
{
|
||||
$(
|
||||
let result = $Tn::from_context_part(&mut context);
|
||||
let $Tn = match result {
|
||||
Ok(value) => value,
|
||||
Err(e) => return std::future::ready(Err(e)).boxed(),
|
||||
Err(e) => return Box::pin(std::future::ready(Err(e))),
|
||||
};
|
||||
)*
|
||||
let result = self($($Tn,)*);
|
||||
std::future::ready(result.and_then(|r| r.into_get_prompt_result())).boxed()
|
||||
Box::pin(std::future::ready(result.and_then(|r| r.into_get_prompt_result())))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
use std::{borrow::Cow, sync::Arc};
|
||||
|
||||
use futures::future::BoxFuture;
|
||||
|
||||
use crate::{
|
||||
handler::server::prompt::{DynGetPromptHandler, GetPromptHandler, PromptContext},
|
||||
model::{GetPromptResult, Prompt},
|
||||
service::{MaybeBoxFuture, MaybeSend},
|
||||
};
|
||||
|
||||
pub struct PromptRoute<S> {
|
||||
|
|
@ -32,10 +31,10 @@ impl<S> Clone for PromptRoute<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: Send + Sync + 'static> PromptRoute<S> {
|
||||
impl<S: MaybeSend + 'static> PromptRoute<S> {
|
||||
pub fn new<H, A: 'static>(attr: impl Into<Prompt>, handler: H) -> Self
|
||||
where
|
||||
H: GetPromptHandler<S, A> + Send + Sync + Clone + 'static,
|
||||
H: GetPromptHandler<S, A> + MaybeSend + Clone + 'static,
|
||||
{
|
||||
Self {
|
||||
get: Arc::new(move |context: PromptContext<S>| {
|
||||
|
|
@ -50,9 +49,8 @@ impl<S: Send + Sync + 'static> PromptRoute<S> {
|
|||
where
|
||||
H: for<'a> Fn(
|
||||
PromptContext<'a, S>,
|
||||
) -> BoxFuture<'a, Result<GetPromptResult, crate::ErrorData>>
|
||||
+ Send
|
||||
+ Sync
|
||||
) -> MaybeBoxFuture<'a, Result<GetPromptResult, crate::ErrorData>>
|
||||
+ MaybeSend
|
||||
+ 'static,
|
||||
{
|
||||
Self {
|
||||
|
|
@ -72,9 +70,9 @@ pub trait IntoPromptRoute<S, A> {
|
|||
|
||||
impl<S, H, A, P> IntoPromptRoute<S, A> for (P, H)
|
||||
where
|
||||
S: Send + Sync + 'static,
|
||||
S: MaybeSend + 'static,
|
||||
A: 'static,
|
||||
H: GetPromptHandler<S, A> + Send + Sync + Clone + 'static,
|
||||
H: GetPromptHandler<S, A> + MaybeSend + Clone + 'static,
|
||||
P: Into<Prompt>,
|
||||
{
|
||||
fn into_prompt_route(self) -> PromptRoute<S> {
|
||||
|
|
@ -84,7 +82,7 @@ where
|
|||
|
||||
impl<S> IntoPromptRoute<S, ()> for PromptRoute<S>
|
||||
where
|
||||
S: Send + Sync + 'static,
|
||||
S: MaybeSend + 'static,
|
||||
{
|
||||
fn into_prompt_route(self) -> PromptRoute<S> {
|
||||
self
|
||||
|
|
@ -96,7 +94,7 @@ pub struct PromptAttrGenerateFunctionAdapter;
|
|||
|
||||
impl<S, F> IntoPromptRoute<S, PromptAttrGenerateFunctionAdapter> for F
|
||||
where
|
||||
S: Send + Sync + 'static,
|
||||
S: MaybeSend + 'static,
|
||||
F: Fn() -> PromptRoute<S>,
|
||||
{
|
||||
fn into_prompt_route(self) -> PromptRoute<S> {
|
||||
|
|
@ -137,7 +135,7 @@ impl<S> IntoIterator for PromptRouter<S> {
|
|||
|
||||
impl<S> PromptRouter<S>
|
||||
where
|
||||
S: Send + Sync + 'static,
|
||||
S: MaybeSend + 'static,
|
||||
{
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
|
|
@ -195,7 +193,7 @@ where
|
|||
|
||||
impl<S> std::ops::Add<PromptRouter<S>> for PromptRouter<S>
|
||||
where
|
||||
S: Send + Sync + 'static,
|
||||
S: MaybeSend + 'static,
|
||||
{
|
||||
type Output = Self;
|
||||
|
||||
|
|
@ -207,7 +205,7 @@ where
|
|||
|
||||
impl<S> std::ops::AddAssign<PromptRouter<S>> for PromptRouter<S>
|
||||
where
|
||||
S: Send + Sync + 'static,
|
||||
S: MaybeSend + 'static,
|
||||
{
|
||||
fn add_assign(&mut self, other: PromptRouter<S>) {
|
||||
self.merge(other);
|
||||
|
|
|
|||
|
|
@ -124,7 +124,6 @@ mod tool_traits;
|
|||
|
||||
use std::{borrow::Cow, sync::Arc};
|
||||
|
||||
use futures::{FutureExt, future::BoxFuture};
|
||||
use schemars::JsonSchema;
|
||||
pub use tool_traits::{AsyncTool, SyncTool, ToolBase};
|
||||
|
||||
|
|
@ -134,6 +133,7 @@ use crate::{
|
|||
tool_name_validation::validate_and_warn_tool_name,
|
||||
},
|
||||
model::{CallToolResult, Tool, ToolAnnotations},
|
||||
service::{MaybeBoxFuture, MaybeSend},
|
||||
};
|
||||
|
||||
pub struct ToolRoute<S> {
|
||||
|
|
@ -161,15 +161,15 @@ impl<S> Clone for ToolRoute<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: Send + Sync + 'static> ToolRoute<S> {
|
||||
impl<S: MaybeSend + 'static> ToolRoute<S> {
|
||||
pub fn new<C, A>(attr: impl Into<Tool>, call: C) -> Self
|
||||
where
|
||||
C: CallToolHandler<S, A> + Send + Sync + Clone + 'static,
|
||||
C: CallToolHandler<S, A> + MaybeSend + Clone + 'static,
|
||||
{
|
||||
Self {
|
||||
call: Arc::new(move |context: ToolCallContext<S>| {
|
||||
let call = call.clone();
|
||||
context.invoke(call).boxed()
|
||||
context.invoke(call)
|
||||
}),
|
||||
attr: attr.into(),
|
||||
}
|
||||
|
|
@ -178,9 +178,8 @@ impl<S: Send + Sync + 'static> ToolRoute<S> {
|
|||
where
|
||||
C: for<'a> Fn(
|
||||
ToolCallContext<'a, S>,
|
||||
) -> BoxFuture<'a, Result<CallToolResult, crate::ErrorData>>
|
||||
+ Send
|
||||
+ Sync
|
||||
) -> MaybeBoxFuture<'a, Result<CallToolResult, crate::ErrorData>>
|
||||
+ MaybeSend
|
||||
+ 'static,
|
||||
{
|
||||
Self {
|
||||
|
|
@ -199,8 +198,8 @@ pub trait IntoToolRoute<S, A> {
|
|||
|
||||
impl<S, C, A, T> IntoToolRoute<S, A> for (T, C)
|
||||
where
|
||||
S: Send + Sync + 'static,
|
||||
C: CallToolHandler<S, A> + Send + Sync + Clone + 'static,
|
||||
S: MaybeSend + 'static,
|
||||
C: CallToolHandler<S, A> + MaybeSend + Clone + 'static,
|
||||
T: Into<Tool>,
|
||||
{
|
||||
fn into_tool_route(self) -> ToolRoute<S> {
|
||||
|
|
@ -210,7 +209,7 @@ where
|
|||
|
||||
impl<S> IntoToolRoute<S, ()> for ToolRoute<S>
|
||||
where
|
||||
S: Send + Sync + 'static,
|
||||
S: MaybeSend + 'static,
|
||||
{
|
||||
fn into_tool_route(self) -> ToolRoute<S> {
|
||||
self
|
||||
|
|
@ -220,7 +219,7 @@ where
|
|||
pub struct ToolAttrGenerateFunctionAdapter;
|
||||
impl<S, F> IntoToolRoute<S, ToolAttrGenerateFunctionAdapter> for F
|
||||
where
|
||||
S: Send + Sync + 'static,
|
||||
S: MaybeSend + 'static,
|
||||
F: Fn() -> ToolRoute<S>,
|
||||
{
|
||||
fn into_tool_route(self) -> ToolRoute<S> {
|
||||
|
|
@ -230,14 +229,14 @@ where
|
|||
|
||||
pub trait CallToolHandlerExt<S, A>: Sized
|
||||
where
|
||||
Self: CallToolHandler<S, A> + Send + Sync + Clone + 'static,
|
||||
Self: CallToolHandler<S, A> + MaybeSend + Clone + 'static,
|
||||
{
|
||||
fn name(self, name: impl Into<Cow<'static, str>>) -> WithToolAttr<Self, S, A>;
|
||||
}
|
||||
|
||||
impl<C, S, A> CallToolHandlerExt<S, A> for C
|
||||
where
|
||||
C: CallToolHandler<S, A> + Send + Sync + Clone + 'static,
|
||||
C: CallToolHandler<S, A> + MaybeSend + Clone + 'static,
|
||||
{
|
||||
fn name(self, name: impl Into<Cow<'static, str>>) -> WithToolAttr<Self, S, A> {
|
||||
WithToolAttr {
|
||||
|
|
@ -254,7 +253,7 @@ where
|
|||
|
||||
pub struct WithToolAttr<C, S, A>
|
||||
where
|
||||
C: CallToolHandler<S, A> + Send + Sync + Clone + 'static,
|
||||
C: CallToolHandler<S, A> + MaybeSend + Clone + 'static,
|
||||
{
|
||||
pub attr: crate::model::Tool,
|
||||
pub call: C,
|
||||
|
|
@ -263,8 +262,8 @@ where
|
|||
|
||||
impl<C, S, A> IntoToolRoute<S, A> for WithToolAttr<C, S, A>
|
||||
where
|
||||
C: CallToolHandler<S, A> + Send + Sync + Clone + 'static,
|
||||
S: Send + Sync + 'static,
|
||||
C: CallToolHandler<S, A> + MaybeSend + Clone + 'static,
|
||||
S: MaybeSend + 'static,
|
||||
{
|
||||
fn into_tool_route(self) -> ToolRoute<S> {
|
||||
ToolRoute::new(self.attr, self.call)
|
||||
|
|
@ -273,7 +272,7 @@ where
|
|||
|
||||
impl<C, S, A> WithToolAttr<C, S, A>
|
||||
where
|
||||
C: CallToolHandler<S, A> + Send + Sync + Clone + 'static,
|
||||
C: CallToolHandler<S, A> + MaybeSend + Clone + 'static,
|
||||
{
|
||||
pub fn description(mut self, description: impl Into<Cow<'static, str>>) -> Self {
|
||||
self.attr.description = Some(description.into());
|
||||
|
|
@ -328,7 +327,7 @@ impl<S> IntoIterator for ToolRouter<S> {
|
|||
|
||||
impl<S> ToolRouter<S>
|
||||
where
|
||||
S: Send + Sync + 'static,
|
||||
S: MaybeSend + 'static,
|
||||
{
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
|
|
@ -428,7 +427,7 @@ where
|
|||
|
||||
impl<S> std::ops::Add<ToolRouter<S>> for ToolRouter<S>
|
||||
where
|
||||
S: Send + Sync + 'static,
|
||||
S: MaybeSend + 'static,
|
||||
{
|
||||
type Output = Self;
|
||||
|
||||
|
|
@ -440,7 +439,7 @@ where
|
|||
|
||||
impl<S> std::ops::AddAssign<ToolRouter<S>> for ToolRouter<S>
|
||||
where
|
||||
S: Send + Sync + 'static,
|
||||
S: MaybeSend + 'static,
|
||||
{
|
||||
fn add_assign(&mut self, other: ToolRouter<S>) {
|
||||
self.merge(other);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use std::{borrow::Cow, pin::Pin, sync::Arc};
|
||||
use std::{borrow::Cow, future::Future, sync::Arc};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
|
@ -11,6 +11,7 @@ use crate::{
|
|||
},
|
||||
model::{Icon, JsonObject, Meta, ToolAnnotations, ToolExecution},
|
||||
schemars::JsonSchema,
|
||||
service::{MaybeSend, MaybeSendFuture},
|
||||
};
|
||||
|
||||
/// Base trait to define attributes of a tool.
|
||||
|
|
@ -84,7 +85,8 @@ pub trait ToolBase {
|
|||
///
|
||||
/// Consider using [`AsyncTool`] if your workflow involves asynchronous operations.
|
||||
/// Examples are shown in [the module-level documentation][crate::handler::server::router::tool].
|
||||
pub trait SyncTool<S: Sync + Send + 'static>: ToolBase {
|
||||
#[allow(private_bounds)]
|
||||
pub trait SyncTool<S: MaybeSend + 'static>: ToolBase {
|
||||
fn invoke(service: &S, param: Self::Parameter) -> Result<Self::Output, Self::Error>;
|
||||
}
|
||||
|
||||
|
|
@ -92,11 +94,12 @@ pub trait SyncTool<S: Sync + Send + 'static>: ToolBase {
|
|||
///
|
||||
/// Consider using [`SyncTool`] if your workflow does not involve asynchronous operations.
|
||||
/// Examples are shown in [the module-level documentation][crate::handler::server::router::tool].
|
||||
pub trait AsyncTool<S: Sync + Send + 'static>: ToolBase {
|
||||
#[allow(private_bounds)]
|
||||
pub trait AsyncTool<S: MaybeSend + 'static>: ToolBase {
|
||||
fn invoke(
|
||||
service: &S,
|
||||
param: Self::Parameter,
|
||||
) -> impl Future<Output = Result<Self::Output, Self::Error>> + Send;
|
||||
) -> impl Future<Output = Result<Self::Output, Self::Error>> + MaybeSendFuture;
|
||||
}
|
||||
|
||||
pub(crate) fn tool_attribute<T: ToolBase>() -> crate::model::Tool {
|
||||
|
|
@ -113,14 +116,14 @@ pub(crate) fn tool_attribute<T: ToolBase>() -> crate::model::Tool {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn sync_tool_wrapper<S: Sync + Send + 'static, T: SyncTool<S>>(
|
||||
pub(crate) fn sync_tool_wrapper<S: MaybeSend + 'static, T: SyncTool<S>>(
|
||||
service: &S,
|
||||
Parameters(params): Parameters<T::Parameter>,
|
||||
) -> Result<Json<T::Output>, ErrorData> {
|
||||
T::invoke(service, params).map(Json).map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn sync_tool_wrapper_with_empty_params<S: Sync + Send + 'static, T: SyncTool<S>>(
|
||||
pub(crate) fn sync_tool_wrapper_with_empty_params<S: MaybeSend + 'static, T: SyncTool<S>>(
|
||||
service: &S,
|
||||
) -> Result<Json<T::Output>, ErrorData> {
|
||||
T::invoke(service, T::Parameter::default())
|
||||
|
|
@ -128,11 +131,10 @@ pub(crate) fn sync_tool_wrapper_with_empty_params<S: Sync + Send + 'static, T: S
|
|||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
#[expect(clippy::type_complexity)]
|
||||
pub(crate) fn async_tool_wrapper<S: Sync + Send + 'static, T: AsyncTool<S>>(
|
||||
pub(crate) fn async_tool_wrapper<S: MaybeSend + 'static, T: AsyncTool<S>>(
|
||||
service: &S,
|
||||
Parameters(params): Parameters<T::Parameter>,
|
||||
) -> Pin<Box<dyn Future<Output = Result<Json<T::Output>, ErrorData>> + Send + '_>> {
|
||||
) -> crate::service::MaybeBoxFuture<'_, Result<Json<T::Output>, ErrorData>> {
|
||||
Box::pin(async move {
|
||||
T::invoke(service, params)
|
||||
.await
|
||||
|
|
@ -141,10 +143,9 @@ pub(crate) fn async_tool_wrapper<S: Sync + Send + 'static, T: AsyncTool<S>>(
|
|||
})
|
||||
}
|
||||
|
||||
#[expect(clippy::type_complexity)]
|
||||
pub(crate) fn async_tool_wrapper_with_empty_params<S: Sync + Send + 'static, T: AsyncTool<S>>(
|
||||
pub(crate) fn async_tool_wrapper_with_empty_params<S: MaybeSend + 'static, T: AsyncTool<S>>(
|
||||
service: &S,
|
||||
) -> Pin<Box<dyn Future<Output = Result<Json<T::Output>, ErrorData>> + Send + '_>> {
|
||||
) -> crate::service::MaybeBoxFuture<'_, Result<Json<T::Output>, ErrorData>> {
|
||||
Box::pin(async move {
|
||||
T::invoke(service, T::Parameter::default())
|
||||
.await
|
||||
|
|
|
|||
|
|
@ -4,7 +4,8 @@ use std::{
|
|||
marker::PhantomData,
|
||||
};
|
||||
|
||||
use futures::future::{BoxFuture, FutureExt};
|
||||
#[cfg(not(feature = "local"))]
|
||||
use futures::future::BoxFuture;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use super::common::{AsRequestContext, FromContextPart};
|
||||
|
|
@ -16,7 +17,7 @@ use crate::{
|
|||
RoleServer,
|
||||
handler::server::wrapper::Parameters,
|
||||
model::{CallToolRequestParams, CallToolResult, IntoContents, JsonObject},
|
||||
service::RequestContext,
|
||||
service::{MaybeBoxFuture, MaybeSend, MaybeSendFuture, RequestContext},
|
||||
};
|
||||
|
||||
/// Deserialize a JSON object into a type
|
||||
|
|
@ -146,13 +147,21 @@ pub trait CallToolHandler<S, A> {
|
|||
fn call(
|
||||
self,
|
||||
context: ToolCallContext<'_, S>,
|
||||
) -> BoxFuture<'_, Result<CallToolResult, crate::ErrorData>>;
|
||||
) -> MaybeBoxFuture<'_, Result<CallToolResult, crate::ErrorData>>;
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "local"))]
|
||||
pub type DynCallToolHandler<S> = dyn for<'s> Fn(ToolCallContext<'s, S>) -> BoxFuture<'s, Result<CallToolResult, crate::ErrorData>>
|
||||
+ Send
|
||||
+ Sync;
|
||||
|
||||
#[cfg(feature = "local")]
|
||||
pub type DynCallToolHandler<S> =
|
||||
dyn for<'s> Fn(
|
||||
ToolCallContext<'s, S>,
|
||||
)
|
||||
-> futures::future::LocalBoxFuture<'s, Result<CallToolResult, crate::ErrorData>>;
|
||||
|
||||
// Tool-specific extractor for tool name
|
||||
pub struct ToolName(pub Cow<'static, str>);
|
||||
|
||||
|
|
@ -189,7 +198,7 @@ impl<S> FromContextPart<ToolCallContext<'_, S>> for JsonObject {
|
|||
}
|
||||
|
||||
impl<'s, S> ToolCallContext<'s, S> {
|
||||
pub fn invoke<H, A>(self, h: H) -> BoxFuture<'s, Result<CallToolResult, crate::ErrorData>>
|
||||
pub fn invoke<H, A>(self, h: H) -> MaybeBoxFuture<'s, Result<CallToolResult, crate::ErrorData>>
|
||||
where
|
||||
H: CallToolHandler<S, A>,
|
||||
{
|
||||
|
|
@ -221,31 +230,31 @@ macro_rules! impl_for {
|
|||
$(
|
||||
$Tn: for<'a> FromContextPart<ToolCallContext<'a, S>> ,
|
||||
)*
|
||||
F: FnOnce(&S, $($Tn,)*) -> BoxFuture<'_, R>,
|
||||
F: FnOnce(&S, $($Tn,)*) -> MaybeBoxFuture<'_, R>,
|
||||
|
||||
// Need RTN support here(I guess), https://github.com/rust-lang/rust/pull/138424
|
||||
// Fut: Future<Output = R> + Send + 'a,
|
||||
R: IntoCallToolResult + Send + 'static,
|
||||
S: Send + Sync + 'static,
|
||||
R: IntoCallToolResult + MaybeSendFuture + 'static,
|
||||
S: MaybeSend + 'static,
|
||||
{
|
||||
#[allow(unused_variables, non_snake_case, unused_mut)]
|
||||
fn call(
|
||||
self,
|
||||
mut context: ToolCallContext<'_, S>,
|
||||
) -> BoxFuture<'_, Result<CallToolResult, crate::ErrorData>>{
|
||||
) -> MaybeBoxFuture<'_, Result<CallToolResult, crate::ErrorData>>{
|
||||
$(
|
||||
let result = $Tn::from_context_part(&mut context);
|
||||
let $Tn = match result {
|
||||
Ok(value) => value,
|
||||
Err(e) => return std::future::ready(Err(e)).boxed(),
|
||||
Err(e) => return Box::pin(std::future::ready(Err(e))),
|
||||
};
|
||||
)*
|
||||
let service = context.service;
|
||||
let fut = self(service, $($Tn,)*);
|
||||
async move {
|
||||
Box::pin(async move {
|
||||
let result = fut.await;
|
||||
result.into_call_tool_result()
|
||||
}.boxed()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -254,28 +263,28 @@ macro_rules! impl_for {
|
|||
$(
|
||||
$Tn: for<'a> FromContextPart<ToolCallContext<'a, S>> ,
|
||||
)*
|
||||
F: FnOnce($($Tn,)*) -> Fut + Send + ,
|
||||
Fut: Future<Output = R> + Send + 'static,
|
||||
R: IntoCallToolResult + Send + 'static,
|
||||
S: Send + Sync,
|
||||
F: FnOnce($($Tn,)*) -> Fut + MaybeSendFuture,
|
||||
Fut: Future<Output = R> + MaybeSendFuture + 'static,
|
||||
R: IntoCallToolResult + MaybeSendFuture + 'static,
|
||||
S: MaybeSend,
|
||||
{
|
||||
#[allow(unused_variables, non_snake_case, unused_mut)]
|
||||
fn call(
|
||||
self,
|
||||
mut context: ToolCallContext<S>,
|
||||
) -> BoxFuture<'static, Result<CallToolResult, crate::ErrorData>>{
|
||||
) -> MaybeBoxFuture<'static, Result<CallToolResult, crate::ErrorData>>{
|
||||
$(
|
||||
let result = $Tn::from_context_part(&mut context);
|
||||
let $Tn = match result {
|
||||
Ok(value) => value,
|
||||
Err(e) => return std::future::ready(Err(e)).boxed(),
|
||||
Err(e) => return Box::pin(std::future::ready(Err(e))),
|
||||
};
|
||||
)*
|
||||
let fut = self($($Tn,)*);
|
||||
async move {
|
||||
Box::pin(async move {
|
||||
let result = fut.await;
|
||||
result.into_call_tool_result()
|
||||
}.boxed()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -284,23 +293,23 @@ macro_rules! impl_for {
|
|||
$(
|
||||
$Tn: for<'a> FromContextPart<ToolCallContext<'a, S>> + ,
|
||||
)*
|
||||
F: FnOnce(&S, $($Tn,)*) -> R + Send + ,
|
||||
R: IntoCallToolResult + Send + ,
|
||||
S: Send + Sync,
|
||||
F: FnOnce(&S, $($Tn,)*) -> R + MaybeSendFuture,
|
||||
R: IntoCallToolResult + MaybeSendFuture,
|
||||
S: MaybeSend,
|
||||
{
|
||||
#[allow(unused_variables, non_snake_case, unused_mut)]
|
||||
fn call(
|
||||
self,
|
||||
mut context: ToolCallContext<S>,
|
||||
) -> BoxFuture<'static, Result<CallToolResult, crate::ErrorData>> {
|
||||
) -> MaybeBoxFuture<'static, Result<CallToolResult, crate::ErrorData>> {
|
||||
$(
|
||||
let result = $Tn::from_context_part(&mut context);
|
||||
let $Tn = match result {
|
||||
Ok(value) => value,
|
||||
Err(e) => return std::future::ready(Err(e)).boxed(),
|
||||
Err(e) => return Box::pin(std::future::ready(Err(e))),
|
||||
};
|
||||
)*
|
||||
std::future::ready(self(context.service, $($Tn,)*).into_call_tool_result()).boxed()
|
||||
Box::pin(std::future::ready(self(context.service, $($Tn,)*).into_call_tool_result()))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -309,23 +318,23 @@ macro_rules! impl_for {
|
|||
$(
|
||||
$Tn: for<'a> FromContextPart<ToolCallContext<'a, S>> + ,
|
||||
)*
|
||||
F: FnOnce($($Tn,)*) -> R + Send + ,
|
||||
R: IntoCallToolResult + Send + ,
|
||||
S: Send + Sync,
|
||||
F: FnOnce($($Tn,)*) -> R + MaybeSendFuture,
|
||||
R: IntoCallToolResult + MaybeSendFuture,
|
||||
S: MaybeSend,
|
||||
{
|
||||
#[allow(unused_variables, non_snake_case, unused_mut)]
|
||||
fn call(
|
||||
self,
|
||||
mut context: ToolCallContext<S>,
|
||||
) -> BoxFuture<'static, Result<CallToolResult, crate::ErrorData>> {
|
||||
) -> MaybeBoxFuture<'static, Result<CallToolResult, crate::ErrorData>> {
|
||||
$(
|
||||
let result = $Tn::from_context_part(&mut context);
|
||||
let $Tn = match result {
|
||||
Ok(value) => value,
|
||||
Err(e) => return std::future::ready(Err(e)).boxed(),
|
||||
Err(e) => return Box::pin(std::future::ready(Err(e))),
|
||||
};
|
||||
)*
|
||||
std::future::ready(self($($Tn,)*).into_call_tool_result()).boxed()
|
||||
Box::pin(std::future::ready(self($($Tn,)*).into_call_tool_result()))
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue