fix(macro): add generics marco types support (#98)

add generic marco support

Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn>
This commit is contained in:
jokemanfire 2025-04-07 10:01:28 +08:00 • committed by GitHub
commit 36834f37d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 245 additions and 17 deletions

View file

@ -39,4 +39,8 @@ path = "src/axum.rs"
[[example]]
name = "servers_axum_router"
path = "src/axum_router.rs"
path = "src/axum_router.rs"
[[example]]
name = "servers_generic_server"
path = "src/generic_service.rs"

View file

@ -19,6 +19,7 @@ pub struct Counter {
}
#[tool(tool_box)]
impl Counter {
#[allow(dead_code)]
pub fn new() -> Self {
Self {
counter: Arc::new(Mutex::new(0)),

View file

@ -0,0 +1,73 @@
use std::sync::Arc;
use rmcp::{
ServerHandler,
model::{ServerCapabilities, ServerInfo},
schemars, tool,
};
#[allow(dead_code)]
pub trait DataService: Send + Sync + 'static {
fn get_data(&self) -> String;
fn set_data(&mut self, data: String);
}
#[derive(Debug, Clone)]
pub struct MemoryDataService {
data: String,
}
impl MemoryDataService {
#[allow(dead_code)]
pub fn new(initial_data: impl Into<String>) -> Self {
Self {
data: initial_data.into(),
}
}
}
impl DataService for MemoryDataService {
fn get_data(&self) -> String {
self.data.clone()
}
fn set_data(&mut self, data: String) {
self.data = data;
}
}
#[derive(Debug, Clone)]
pub struct GenericService<DS: DataService> {
#[allow(dead_code)]
data_service: Arc<DS>,
}
#[tool(tool_box)]
impl<DS: DataService> GenericService<DS> {
pub fn new(data_service: DS) -> Self {
Self {
data_service: Arc::new(data_service),
}
}
#[tool(description = "get memory from service")]
pub async fn get_data(&self) -> String {
self.data_service.get_data()
}
#[tool(description = "set memory to service")]
pub async fn set_data(&self, #[tool(param)] data: String) -> String {
let new_data = data.clone();
format!("Current memory: {}", new_data)
}
}
impl<DS: DataService> ServerHandler for GenericService<DS> {
fn get_info(&self) -> ServerInfo {
ServerInfo {
instructions: Some("generic data service".into()),
capabilities: ServerCapabilities::builder().enable_tools().build(),
..Default::default()
}
}
}

View file

@ -1,2 +1,3 @@
pub mod calculator;
pub mod counter;
pub mod generic_service;

View file

@ -0,0 +1,18 @@
use std::error::Error;
mod common;
use common::generic_service::{GenericService, MemoryDataService};
use rmcp::serve_server;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let memory_service = MemoryDataService::new("initial data");
let generic_service = GenericService::new(memory_service);
println!("start server, connect to standard input/output");
let io = (tokio::io::stdin(), tokio::io::stdout());
serve_server(generic_service, io).await?;
Ok(())
}