Move whole rmcp crate to offcial rust sdk (#44)
* get rid of async-trait * move rmcp to official-rust-sdk * replace master with main * update readme, change license * fix typo * Update README.md Co-authored-by: Jefry Dewangga <jefrydco@gmail.com> * fix typos --------- Co-authored-by: = <=> Co-authored-by: Jefry Dewangga <jefrydco@gmail.com>
This commit is contained in:
parent
31493cfbcc
commit
0be6067326
1 changed files with 8676 additions and 4466 deletions
230
crates/rmcp/src/handler/server.rs
Normal file
230
crates/rmcp/src/handler/server.rs
Normal file
|
|
@ -0,0 +1,230 @@
|
|||
use crate::error::Error as McpError;
|
||||
use crate::model::*;
|
||||
use crate::service::{Peer, RequestContext, RoleServer, Service, ServiceRole};
|
||||
|
||||
mod resource;
|
||||
pub mod tool;
|
||||
|
||||
impl<H: ServerHandler> Service<RoleServer> for H {
|
||||
async fn handle_request(
|
||||
&self,
|
||||
request: <RoleServer as ServiceRole>::PeerReq,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> Result<<RoleServer as ServiceRole>::Resp, McpError> {
|
||||
match request {
|
||||
ClientRequest::InitializeRequest(request) => self
|
||||
.initialize(request.params, context)
|
||||
.await
|
||||
.map(ServerResult::InitializeResult),
|
||||
ClientRequest::PingRequest(_request) => {
|
||||
self.ping(context).await.map(ServerResult::empty)
|
||||
}
|
||||
ClientRequest::CompleteRequest(request) => self
|
||||
.complete(request.params, context)
|
||||
.await
|
||||
.map(ServerResult::CompleteResult),
|
||||
ClientRequest::SetLevelRequest(request) => self
|
||||
.set_level(request.params, context)
|
||||
.await
|
||||
.map(ServerResult::empty),
|
||||
ClientRequest::GetPromptRequest(request) => self
|
||||
.get_prompt(request.params, context)
|
||||
.await
|
||||
.map(ServerResult::GetPromptResult),
|
||||
ClientRequest::ListPromptsRequest(request) => self
|
||||
.list_prompts(request.params, context)
|
||||
.await
|
||||
.map(ServerResult::ListPromptsResult),
|
||||
ClientRequest::ListResourcesRequest(request) => self
|
||||
.list_resources(request.params, context)
|
||||
.await
|
||||
.map(ServerResult::ListResourcesResult),
|
||||
ClientRequest::ListResourceTemplatesRequest(request) => self
|
||||
.list_resource_templates(request.params, context)
|
||||
.await
|
||||
.map(ServerResult::ListResourceTemplatesResult),
|
||||
ClientRequest::ReadResourceRequest(request) => self
|
||||
.read_resource(request.params, context)
|
||||
.await
|
||||
.map(ServerResult::ReadResourceResult),
|
||||
ClientRequest::SubscribeRequest(request) => self
|
||||
.subscribe(request.params, context)
|
||||
.await
|
||||
.map(ServerResult::empty),
|
||||
ClientRequest::UnsubscribeRequest(request) => self
|
||||
.unsubscribe(request.params, context)
|
||||
.await
|
||||
.map(ServerResult::empty),
|
||||
ClientRequest::CallToolRequest(request) => self
|
||||
.call_tool(request.params, context)
|
||||
.await
|
||||
.map(ServerResult::CallToolResult),
|
||||
ClientRequest::ListToolsRequest(request) => self
|
||||
.list_tools(request.params, context)
|
||||
.await
|
||||
.map(ServerResult::ListToolsResult),
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_notification(
|
||||
&self,
|
||||
notification: <RoleServer as ServiceRole>::PeerNot,
|
||||
) -> Result<(), McpError> {
|
||||
match notification {
|
||||
ClientNotification::CancelledNotification(notification) => {
|
||||
self.on_cancelled(notification.params).await
|
||||
}
|
||||
ClientNotification::ProgressNotification(notification) => {
|
||||
self.on_progress(notification.params).await
|
||||
}
|
||||
ClientNotification::InitializedNotification(_notification) => {
|
||||
self.on_initialized().await
|
||||
}
|
||||
ClientNotification::RootsListChangedNotification(_notification) => {
|
||||
self.on_roots_list_changed().await
|
||||
}
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_peer(&self) -> Option<Peer<RoleServer>> {
|
||||
self.get_peer()
|
||||
}
|
||||
|
||||
fn set_peer(&mut self, peer: Peer<RoleServer>) {
|
||||
self.set_peer(peer);
|
||||
}
|
||||
|
||||
fn get_info(&self) -> <RoleServer as ServiceRole>::Info {
|
||||
self.get_info()
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub trait ServerHandler: Sized + Clone + Send + Sync + 'static {
|
||||
fn ping(
|
||||
&self,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<(), McpError>> + Send + '_ {
|
||||
std::future::ready(Ok(()))
|
||||
}
|
||||
// handle requests
|
||||
fn initialize(
|
||||
&self,
|
||||
request: InitializeRequestParam,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<InitializeResult, McpError>> + Send + '_ {
|
||||
std::future::ready(Ok(self.get_info()))
|
||||
}
|
||||
fn complete(
|
||||
&self,
|
||||
request: CompleteRequestParam,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<CompleteResult, McpError>> + Send + '_ {
|
||||
std::future::ready(Err(McpError::method_not_found::<CompleteRequestMethod>()))
|
||||
}
|
||||
fn set_level(
|
||||
&self,
|
||||
request: SetLevelRequestParam,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<(), McpError>> + Send + '_ {
|
||||
std::future::ready(Err(McpError::method_not_found::<SetLevelRequestMethod>()))
|
||||
}
|
||||
fn get_prompt(
|
||||
&self,
|
||||
request: GetPromptRequestParam,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<GetPromptResult, McpError>> + Send + '_ {
|
||||
std::future::ready(Err(McpError::method_not_found::<GetPromptRequestMethod>()))
|
||||
}
|
||||
fn list_prompts(
|
||||
&self,
|
||||
request: PaginatedRequestParam,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<ListPromptsResult, McpError>> + Send + '_ {
|
||||
std::future::ready(Ok(ListPromptsResult::default()))
|
||||
}
|
||||
fn list_resources(
|
||||
&self,
|
||||
request: PaginatedRequestParam,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<ListResourcesResult, McpError>> + Send + '_ {
|
||||
std::future::ready(Ok(ListResourcesResult::default()))
|
||||
}
|
||||
fn list_resource_templates(
|
||||
&self,
|
||||
request: PaginatedRequestParam,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<ListResourceTemplatesResult, McpError>> + Send + '_ {
|
||||
std::future::ready(Ok(ListResourceTemplatesResult::default()))
|
||||
}
|
||||
fn read_resource(
|
||||
&self,
|
||||
request: ReadResourceRequestParam,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<ReadResourceResult, McpError>> + Send + '_ {
|
||||
std::future::ready(Err(
|
||||
McpError::method_not_found::<ReadResourceRequestMethod>(),
|
||||
))
|
||||
}
|
||||
fn subscribe(
|
||||
&self,
|
||||
request: SubscribeRequestParam,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<(), McpError>> + Send + '_ {
|
||||
std::future::ready(Err(McpError::method_not_found::<SubscribeRequestMethod>()))
|
||||
}
|
||||
fn unsubscribe(
|
||||
&self,
|
||||
request: UnsubscribeRequestParam,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<(), McpError>> + Send + '_ {
|
||||
std::future::ready(Err(McpError::method_not_found::<UnsubscribeRequestMethod>()))
|
||||
}
|
||||
fn call_tool(
|
||||
&self,
|
||||
request: CallToolRequestParam,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<CallToolResult, McpError>> + Send + '_ {
|
||||
std::future::ready(Err(McpError::method_not_found::<CallToolRequestMethod>()))
|
||||
}
|
||||
fn list_tools(
|
||||
&self,
|
||||
request: PaginatedRequestParam,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> impl Future<Output = Result<ListToolsResult, McpError>> + Send + '_ {
|
||||
std::future::ready(Ok(ListToolsResult::default()))
|
||||
}
|
||||
|
||||
fn on_cancelled(
|
||||
&self,
|
||||
notification: CancelledNotificationParam,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
std::future::ready(())
|
||||
}
|
||||
fn on_progress(
|
||||
&self,
|
||||
notification: ProgressNotificationParam,
|
||||
) -> impl Future<Output = ()> + Send + '_ {
|
||||
std::future::ready(())
|
||||
}
|
||||
fn on_initialized(&self) -> impl Future<Output = ()> + Send + '_ {
|
||||
tracing::info!("client initialized");
|
||||
std::future::ready(())
|
||||
}
|
||||
fn on_roots_list_changed(&self) -> impl Future<Output = ()> + Send + '_ {
|
||||
std::future::ready(())
|
||||
}
|
||||
|
||||
fn get_peer(&self) -> Option<Peer<RoleServer>> {
|
||||
None
|
||||
}
|
||||
|
||||
fn set_peer(&mut self, peer: Peer<RoleServer>) {
|
||||
drop(peer);
|
||||
}
|
||||
|
||||
fn get_info(&self) -> ServerInfo {
|
||||
ServerInfo::default()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue