feat(examples): Add func call support (#168)
Some works about simple-chat. 1. fix some bugs. 2. add func call optional. Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn>
This commit is contained in:
parent
787cc015b7
commit
6de444c62f
1 changed files with 160 additions and 108 deletions
|
|
@ -3,7 +3,7 @@ use std::{collections::HashMap, sync::Arc};
|
|||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use rmcp::{
|
||||
model::{CallToolRequestParam, Tool as McpTool},
|
||||
model::{CallToolRequestParam, CallToolResult, Tool as McpTool},
|
||||
service::ServerSink,
|
||||
};
|
||||
use serde_json::Value;
|
||||
|
|
@ -18,7 +18,7 @@ pub trait Tool: Send + Sync {
|
|||
fn name(&self) -> String;
|
||||
fn description(&self) -> String;
|
||||
fn parameters(&self) -> Value;
|
||||
async fn call(&self, args: Value) -> Result<String>;
|
||||
async fn call(&self, args: Value) -> Result<CallToolResult>;
|
||||
}
|
||||
|
||||
pub struct McpToolAdapter {
|
||||
|
|
@ -50,12 +50,12 @@ impl Tool for McpToolAdapter {
|
|||
serde_json::to_value(&self.tool.input_schema).unwrap_or(serde_json::json!({}))
|
||||
}
|
||||
|
||||
async fn call(&self, args: Value) -> Result<String> {
|
||||
async fn call(&self, args: Value) -> Result<CallToolResult> {
|
||||
let arguments = match args {
|
||||
Value::Object(map) => Some(map),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
println!("arguments: {:?}", arguments);
|
||||
let call_result = self
|
||||
.server
|
||||
.call_tool(CallToolRequestParam {
|
||||
|
|
@ -63,9 +63,8 @@ impl Tool for McpToolAdapter {
|
|||
arguments,
|
||||
})
|
||||
.await?;
|
||||
let result = serde_json::to_string(&call_result).unwrap();
|
||||
|
||||
Ok(result)
|
||||
Ok(call_result)
|
||||
}
|
||||
}
|
||||
#[derive(Default)]
|
||||
|
|
|
|||
Loading…
Reference in a new issue