refactor: refactor tool macros and router implementation (#261)
* refactor: refactor tool macros and router implementation - Updated the `#[tool(tool_box)]` macro to `#[tool_router]` across various modules for consistency. - Enhanced the `Calculator`, `Counter`, and `GenericService` structs to utilize `ToolRouter` for handling tool calls. - Introduced `Parameters` struct for better parameter handling in tool functions. - Added new methods for listing tools and calling tools in server handlers. - Improved test cases to reflect changes in tool routing and parameter handling. - Updated documentation and examples to align with the new router structure. * fix: fix fmt and build error * fix: fix test failure * docs: documents for macros, fix ci * fix: fix ci * fix: fix wrongly replaced documents * fix: remove useless file * fix: change the parameter format for tool_router * fix: update extract_doc_line to handle existing documentation and clean up unused code in server handler * doc: update document for macro and examples * doc: update readme and add contribute guide * fix: fix type
This commit is contained in:
parent
6d3190504c
commit
1f7f4d3055
35 changed files with 1498 additions and 1147 deletions
|
|
@ -124,5 +124,10 @@ See [oauth_support](docs/OAUTH_SUPPORT.md) for details.
|
||||||
## Related Projects
|
## Related Projects
|
||||||
- [containerd-mcp-server](https://github.com/jokemanfire/mcp-containerd) - A containerd-based MCP server implementation
|
- [containerd-mcp-server](https://github.com/jokemanfire/mcp-containerd) - A containerd-based MCP server implementation
|
||||||
|
|
||||||
## Development with Dev Container
|
## Development
|
||||||
See [docs/DEVCONTAINER.md](docs/DEVCONTAINER.md) for instructions on using Dev Container for development.
|
|
||||||
|
### Tips for Contributors
|
||||||
|
See [docs/CONTRIBUTE.MD](docs/CONTRIBUTE.MD) to get some tips for contributing.
|
||||||
|
|
||||||
|
### Using Dev Container
|
||||||
|
If you want to use dev container, see [docs/DEVCONTAINER.md](docs/DEVCONTAINER.md) for instructions on using Dev Container for development.
|
||||||
|
|
@ -19,7 +19,7 @@ syn = {version = "2", features = ["full"]}
|
||||||
quote = "1"
|
quote = "1"
|
||||||
proc-macro2 = "1"
|
proc-macro2 = "1"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
darling = { version = "0.20" }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
@ -10,41 +10,149 @@ This library primarily provides the following macros:
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
### Tool Macro
|
### tool
|
||||||
|
|
||||||
Mark a function as a tool:
|
This macro is used to mark a function as a tool handler.
|
||||||
|
|
||||||
```rust ignore
|
This will generate a function that return the attribute of this tool, with type `rmcp::model::Tool`.
|
||||||
#[tool]
|
|
||||||
fn calculator(&self, #[tool(param)] a: i32, #[tool(param)] b: i32) -> Result<CallToolResult, Error> {
|
#### Usage
|
||||||
// Implement tool functionality
|
|
||||||
Ok(CallToolResult::success(vec![Content::text((a + b).to_string())]))
|
| feied | type | usage |
|
||||||
|
| :- | :- | :- |
|
||||||
|
| `name` | `String` | The name of the tool. If not provided, it defaults to the function name. |
|
||||||
|
| `description` | `String` | A description of the tool. The document of this function will be used. |
|
||||||
|
| `input_schema` | `Expr` | A JSON Schema object defining the expected parameters for the tool. If not provide, if will use the json schema of its argument with type `Parameters<T>` |
|
||||||
|
| `annotations` | `ToolAnnotationsAttribute` | Additional tool information. Defaults to `None`. |
|
||||||
|
|
||||||
|
#### Example
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[tool(name = "my_tool", description = "This is my tool", annotations(title = "我的工具", read_only_hint = true))]
|
||||||
|
pub async fn my_tool(param: Parameters<MyToolParam>) {
|
||||||
|
// handling tool request
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Use on an impl block to automatically register multiple tools:
|
### tool_router
|
||||||
|
|
||||||
```rust ignore
|
This macro is used to generate a tool router based on functions marked with `#[rmcp::tool]` in an implementation block.
|
||||||
#[tool(tool_box)]
|
|
||||||
impl MyHandler {
|
It creates a function that returns a `ToolRouter` instance.
|
||||||
|
|
||||||
|
In most case, you need to add a field for handler to store the router information and initialize it when creating handler, or store it with a static variable.
|
||||||
|
|
||||||
|
#### Usage
|
||||||
|
|
||||||
|
| feied | type | usage |
|
||||||
|
| :- | :- | :- |
|
||||||
|
| `router` | `Ident` | The name of the router function to be generated. Defaults to `tool_router`. |
|
||||||
|
| `vis` | `Visibility` | The visibility of the generated router function. Defaults to empty. |
|
||||||
|
|
||||||
|
#### Example
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[tool_router]
|
||||||
|
impl MyToolHandler {
|
||||||
#[tool]
|
#[tool]
|
||||||
fn tool1(&self) -> Result<CallToolResult, Error> {
|
pub fn my_tool() {
|
||||||
// Tool 1 implementation
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tool]
|
pub fn new() -> Self {
|
||||||
fn tool2(&self) -> Result<CallToolResult, Error> {
|
Self {
|
||||||
// Tool 2 implementation
|
// the default name of tool router will be `tool_router`
|
||||||
|
tool_router: Self::tool_router(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Or specify the visibility and router name, which would be helpful when you want to combine multiple routers into one:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
mod a {
|
||||||
|
#[tool_router(router = tool_router_a, vis = pub)]
|
||||||
|
impl MyToolHandler {
|
||||||
|
#[tool]
|
||||||
|
fn my_tool_a() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod b {
|
||||||
|
#[tool_router(router = tool_router_b, vis = pub)]
|
||||||
|
impl MyToolHandler {
|
||||||
|
#[tool]
|
||||||
|
fn my_tool_b() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MyToolHandler {
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
tool_router: self::tool_router_a() + self::tool_router_b(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
### tool_handler
|
||||||
|
|
||||||
|
This macro will generate the handler for `tool_call` and `list_tools` methods in the implementation block, by using an existing `ToolRouter` instance.
|
||||||
|
|
||||||
|
#### Usage
|
||||||
|
|
||||||
|
| field | type | usage |
|
||||||
|
| :- | :- | :- |
|
||||||
|
| `router` | `Expr` | The expression to access the `ToolRouter` instance. Defaults to `self.tool_router`. |
|
||||||
|
|
||||||
|
#### Example
|
||||||
|
```rust
|
||||||
|
#[tool_handler]
|
||||||
|
impl ServerHandler for MyToolHandler {
|
||||||
|
// ...implement other handler
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
or using a custom router expression:
|
||||||
|
```rust
|
||||||
|
#[tool_handler(router = self.get_router().await)]
|
||||||
|
impl ServerHandler for MyToolHandler {
|
||||||
|
// ...implement other handler
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Explained
|
||||||
|
This macro will be expended to something like this:
|
||||||
|
```rust
|
||||||
|
impl ServerHandler for MyToolHandler {
|
||||||
|
async fn call_tool(
|
||||||
|
&self,
|
||||||
|
request: CallToolRequestParam,
|
||||||
|
context: RequestContext<RoleServer>,
|
||||||
|
) -> Result<CallToolResult, rmcp::Error> {
|
||||||
|
let tcc = ToolCallContext::new(self, request, context);
|
||||||
|
self.tool_router.call(tcc).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn list_tools(
|
||||||
|
&self,
|
||||||
|
_request: Option<PaginatedRequestParam>,
|
||||||
|
_context: RequestContext<RoleServer>,
|
||||||
|
) -> Result<ListToolsResult, rmcp::Error> {
|
||||||
|
let items = self.tool_router.list_all();
|
||||||
|
Ok(ListToolsResult::with_all_items(items))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## Advanced Features
|
## Advanced Features
|
||||||
|
|
||||||
- Support for parameter aggregation (`#[tool(aggr)]`)
|
|
||||||
- Support for custom tool names and descriptions
|
- Support for custom tool names and descriptions
|
||||||
- Automatic generation of tool descriptions from documentation comments
|
- Automatic generation of tool descriptions from documentation comments
|
||||||
- JSON Schema generation for tool parameters
|
- JSON Schema generation for tool parameters
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,161 @@
|
||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
|
|
||||||
mod tool;
|
mod tool;
|
||||||
|
mod tool_handler;
|
||||||
|
mod tool_router;
|
||||||
|
/// # tool
|
||||||
|
///
|
||||||
|
/// This macro is used to mark a function as a tool handler.
|
||||||
|
///
|
||||||
|
/// This will generate a function that return the attribute of this tool, with type `rmcp::model::Tool`.
|
||||||
|
///
|
||||||
|
/// ## Usage
|
||||||
|
///
|
||||||
|
/// | feied | type | usage |
|
||||||
|
/// | :- | :- | :- |
|
||||||
|
/// | `name` | `String` | The name of the tool. If not provided, it defaults to the function name. |
|
||||||
|
/// | `description` | `String` | A description of the tool. The document of this function will be used. |
|
||||||
|
/// | `input_schema` | `Expr` | A JSON Schema object defining the expected parameters for the tool. If not provide, if will use the json schema of its argument with type `Parameters<T>` |
|
||||||
|
/// | `annotations` | `ToolAnnotationsAttribute` | Additional tool information. Defaults to `None`. |
|
||||||
|
///
|
||||||
|
/// ## Example
|
||||||
|
///
|
||||||
|
/// ```rust,ignore
|
||||||
|
/// #[tool(name = "my_tool", description = "This is my tool", annotations(title = "我的工具", read_only_hint = true))]
|
||||||
|
/// pub async fn my_tool(param: Parameters<MyToolParam>) {
|
||||||
|
/// // handling tool request
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn tool(attr: TokenStream, input: TokenStream) -> TokenStream {
|
pub fn tool(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
tool::tool(attr.into(), input.into())
|
tool::tool(attr.into(), input.into())
|
||||||
.unwrap_or_else(|err| err.to_compile_error())
|
.unwrap_or_else(|err| err.to_compile_error())
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// # tool_router
|
||||||
|
///
|
||||||
|
/// This macro is used to generate a tool router based on functions marked with `#[rmcp::tool]` in an implementation block.
|
||||||
|
///
|
||||||
|
/// It creates a function that returns a `ToolRouter` instance.
|
||||||
|
///
|
||||||
|
/// In most case, you need to add a field for handler to store the router information and initialize it when creating handler, or store it with a static variable.
|
||||||
|
/// ## Usage
|
||||||
|
///
|
||||||
|
/// | feied | type | usage |
|
||||||
|
/// | :- | :- | :- |
|
||||||
|
/// | `router` | `Ident` | The name of the router function to be generated. Defaults to `tool_router`. |
|
||||||
|
/// | `vis` | `Visibility` | The visibility of the generated router function. Defaults to empty. |
|
||||||
|
///
|
||||||
|
/// ## Example
|
||||||
|
///
|
||||||
|
/// ```rust,ignore
|
||||||
|
/// #[tool_router]
|
||||||
|
/// impl MyToolHandler {
|
||||||
|
/// #[tool]
|
||||||
|
/// pub fn my_tool() {
|
||||||
|
///
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// pub fn new() -> Self {
|
||||||
|
/// Self {
|
||||||
|
/// // the default name of tool router will be `tool_router`
|
||||||
|
/// tool_router: Self::tool_router(),
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// Or specify the visibility and router name, which would be helpful when you want to combine multiple routers into one:
|
||||||
|
///
|
||||||
|
/// ```rust,ignore
|
||||||
|
/// mod a {
|
||||||
|
/// #[tool_router(router = tool_router_a, vis = pub)]
|
||||||
|
/// impl MyToolHandler {
|
||||||
|
/// #[tool]
|
||||||
|
/// fn my_tool_a() {
|
||||||
|
///
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// mod b {
|
||||||
|
/// #[tool_router(router = tool_router_b, vis = pub)]
|
||||||
|
/// impl MyToolHandler {
|
||||||
|
/// #[tool]
|
||||||
|
/// fn my_tool_b() {
|
||||||
|
///
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// impl MyToolHandler {
|
||||||
|
/// fn new() -> Self {
|
||||||
|
/// Self {
|
||||||
|
/// tool_router: self::tool_router_a() + self::tool_router_b(),
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
#[proc_macro_attribute]
|
||||||
|
pub fn tool_router(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
|
tool_router::tool_router(attr.into(), input.into())
|
||||||
|
.unwrap_or_else(|err| err.to_compile_error())
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// # tool_handler
|
||||||
|
///
|
||||||
|
/// This macro will generate the handler for `tool_call` and `list_tools` methods in the implementation block, by using an existing `ToolRouter` instance.
|
||||||
|
///
|
||||||
|
/// ## Usage
|
||||||
|
///
|
||||||
|
/// | field | type | usage |
|
||||||
|
/// | :- | :- | :- |
|
||||||
|
/// | `router` | `Expr` | The expression to access the `ToolRouter` instance. Defaults to `self.tool_router`. |
|
||||||
|
/// ## Example
|
||||||
|
/// ```rust,ignore
|
||||||
|
/// #[tool_handler]
|
||||||
|
/// impl ServerHandler for MyToolHandler {
|
||||||
|
/// // ...implement other handler
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// or using a custom router expression:
|
||||||
|
/// ```rust,ignore
|
||||||
|
/// #[tool_handler(router = self.get_router().await)]
|
||||||
|
/// impl ServerHandler for MyToolHandler {
|
||||||
|
/// // ...implement other handler
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// ## Explain
|
||||||
|
///
|
||||||
|
/// This macro will be expended to something like this:
|
||||||
|
/// ```rust,ignore
|
||||||
|
/// impl ServerHandler for MyToolHandler {
|
||||||
|
/// async fn call_tool(
|
||||||
|
/// &self,
|
||||||
|
/// request: CallToolRequestParam,
|
||||||
|
/// context: RequestContext<RoleServer>,
|
||||||
|
/// ) -> Result<CallToolResult, rmcp::Error> {
|
||||||
|
/// let tcc = ToolCallContext::new(self, request, context);
|
||||||
|
/// self.tool_router.call(tcc).await
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// async fn list_tools(
|
||||||
|
/// &self,
|
||||||
|
/// _request: Option<PaginatedRequestParam>,
|
||||||
|
/// _context: RequestContext<RoleServer>,
|
||||||
|
/// ) -> Result<ListToolsResult, rmcp::Error> {
|
||||||
|
/// let items = self.tool_router.list_all();
|
||||||
|
/// Ok(ListToolsResult::with_all_items(items))
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
#[proc_macro_attribute]
|
||||||
|
pub fn tool_handler(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
|
tool_handler::tool_handler(attr.into(), input.into())
|
||||||
|
.unwrap_or_else(|err| err.to_compile_error())
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,348 +1,96 @@
|
||||||
use std::collections::HashSet;
|
use darling::{FromMeta, ast::NestedMeta};
|
||||||
|
|
||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::{ToTokens, quote};
|
use quote::{ToTokens, format_ident, quote};
|
||||||
use serde_json::json;
|
use syn::{Expr, Ident, ImplItemFn, ReturnType};
|
||||||
use syn::{
|
#[derive(FromMeta, Default, Debug)]
|
||||||
Expr, FnArg, Ident, ItemFn, ItemImpl, Lit, MetaList, PatType, Token, Type, Visibility,
|
#[darling(default)]
|
||||||
parse::Parse, parse_quote, spanned::Spanned,
|
pub struct ToolAttribute {
|
||||||
};
|
/// The name of the tool
|
||||||
|
pub name: Option<String>,
|
||||||
/// Stores tool annotation attributes
|
pub description: Option<String>,
|
||||||
#[derive(Default, Clone)]
|
/// A JSON Schema object defining the expected parameters for the tool
|
||||||
struct ToolAnnotationAttrs(pub serde_json::Map<String, serde_json::Value>);
|
pub input_schema: Option<Expr>,
|
||||||
|
/// Optional additional tool information.
|
||||||
impl Parse for ToolAnnotationAttrs {
|
pub annotations: Option<ToolAnnotationsAttribute>,
|
||||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
|
||||||
let mut attrs = serde_json::Map::new();
|
|
||||||
|
|
||||||
while !input.is_empty() {
|
|
||||||
let key: Ident = input.parse()?;
|
|
||||||
input.parse::<Token![:]>()?;
|
|
||||||
let value: Lit = input.parse()?;
|
|
||||||
let value = match value {
|
|
||||||
Lit::Str(s) => json!(s.value()),
|
|
||||||
Lit::Bool(b) => json!(b.value),
|
|
||||||
_ => {
|
|
||||||
return Err(syn::Error::new(
|
|
||||||
key.span(),
|
|
||||||
"annotations must be string or boolean literals",
|
|
||||||
));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
attrs.insert(key.to_string(), value);
|
|
||||||
if input.is_empty() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
input.parse::<Token![,]>()?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(ToolAnnotationAttrs(attrs))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
pub struct ResolvedToolAttribute {
|
||||||
struct ToolImplItemAttrs {
|
pub name: String,
|
||||||
tool_box: Option<Option<Ident>>,
|
pub description: Option<String>,
|
||||||
|
pub input_schema: Expr,
|
||||||
|
pub annotations: Expr,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse for ToolImplItemAttrs {
|
impl ResolvedToolAttribute {
|
||||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
pub fn into_fn(self, fn_ident: Ident) -> syn::Result<ImplItemFn> {
|
||||||
let mut tool_box = None;
|
let Self {
|
||||||
while !input.is_empty() {
|
|
||||||
let key: Ident = input.parse()?;
|
|
||||||
match key.to_string().as_str() {
|
|
||||||
"tool_box" => {
|
|
||||||
tool_box = Some(None);
|
|
||||||
if input.lookahead1().peek(Token![=]) {
|
|
||||||
input.parse::<Token![=]>()?;
|
|
||||||
let value: Ident = input.parse()?;
|
|
||||||
tool_box = Some(Some(value));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
return Err(syn::Error::new(key.span(), "unknown attribute"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if input.is_empty() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
input.parse::<Token![,]>()?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(ToolImplItemAttrs { tool_box })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct ToolFnItemAttrs {
|
|
||||||
name: Option<Expr>,
|
|
||||||
description: Option<Expr>,
|
|
||||||
vis: Option<Visibility>,
|
|
||||||
annotations: Option<ToolAnnotationAttrs>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parse for ToolFnItemAttrs {
|
|
||||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
|
||||||
let mut name = None;
|
|
||||||
let mut description = None;
|
|
||||||
let mut vis = None;
|
|
||||||
let mut annotations = None;
|
|
||||||
|
|
||||||
while !input.is_empty() {
|
|
||||||
let key: Ident = input.parse()?;
|
|
||||||
input.parse::<Token![=]>()?;
|
|
||||||
match key.to_string().as_str() {
|
|
||||||
"name" => {
|
|
||||||
let value: Expr = input.parse()?;
|
|
||||||
name = Some(value);
|
|
||||||
}
|
|
||||||
"description" => {
|
|
||||||
let value: Expr = input.parse()?;
|
|
||||||
description = Some(value);
|
|
||||||
}
|
|
||||||
"vis" => {
|
|
||||||
let value: Visibility = input.parse()?;
|
|
||||||
vis = Some(value);
|
|
||||||
}
|
|
||||||
"annotations" => {
|
|
||||||
// Parse the annotations as a nested structure
|
|
||||||
let content;
|
|
||||||
syn::braced!(content in input);
|
|
||||||
let value = content.parse()?;
|
|
||||||
annotations = Some(value);
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
return Err(syn::Error::new(key.span(), "unknown attribute"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if input.is_empty() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
input.parse::<Token![,]>()?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(ToolFnItemAttrs {
|
|
||||||
name,
|
name,
|
||||||
description,
|
description,
|
||||||
vis,
|
input_schema,
|
||||||
annotations,
|
annotations,
|
||||||
})
|
} = self;
|
||||||
}
|
let description = if let Some(description) = description {
|
||||||
}
|
quote! { Some(#description.into()) }
|
||||||
|
|
||||||
struct ToolFnParamAttrs {
|
|
||||||
serde_meta: Vec<MetaList>,
|
|
||||||
schemars_meta: Vec<MetaList>,
|
|
||||||
ident: Ident,
|
|
||||||
rust_type: Box<Type>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ToTokens for ToolFnParamAttrs {
|
|
||||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
|
||||||
let ident = &self.ident;
|
|
||||||
let rust_type = &self.rust_type;
|
|
||||||
let serde_meta = &self.serde_meta;
|
|
||||||
let schemars_meta = &self.schemars_meta;
|
|
||||||
tokens.extend(quote! {
|
|
||||||
#(#[#serde_meta])*
|
|
||||||
#(#[#schemars_meta])*
|
|
||||||
pub #ident: #rust_type,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
|
|
||||||
enum ToolParams {
|
|
||||||
Aggregated {
|
|
||||||
rust_type: PatType,
|
|
||||||
},
|
|
||||||
Params {
|
|
||||||
attrs: Vec<ToolFnParamAttrs>,
|
|
||||||
},
|
|
||||||
#[default]
|
|
||||||
NoParam,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct ToolAttrs {
|
|
||||||
fn_item: ToolFnItemAttrs,
|
|
||||||
params: ToolParams,
|
|
||||||
}
|
|
||||||
const TOOL_IDENT: &str = "tool";
|
|
||||||
const SERDE_IDENT: &str = "serde";
|
|
||||||
const SCHEMARS_IDENT: &str = "schemars";
|
|
||||||
const PARAM_IDENT: &str = "param";
|
|
||||||
const AGGREGATED_IDENT: &str = "aggr";
|
|
||||||
const REQ_IDENT: &str = "req";
|
|
||||||
|
|
||||||
pub enum ParamMarker {
|
|
||||||
Param,
|
|
||||||
Aggregated,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parse for ParamMarker {
|
|
||||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
|
||||||
let ident: Ident = input.parse()?;
|
|
||||||
match ident.to_string().as_str() {
|
|
||||||
PARAM_IDENT => Ok(ParamMarker::Param),
|
|
||||||
AGGREGATED_IDENT | REQ_IDENT => Ok(ParamMarker::Aggregated),
|
|
||||||
_ => Err(syn::Error::new(ident.span(), "unknown attribute")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum ToolItem {
|
|
||||||
Fn(ItemFn),
|
|
||||||
Impl(ItemImpl),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parse for ToolItem {
|
|
||||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
|
||||||
let lookahead = input.lookahead1();
|
|
||||||
if lookahead.peek(Token![impl]) {
|
|
||||||
let item = input.parse::<ItemImpl>()?;
|
|
||||||
Ok(ToolItem::Impl(item))
|
|
||||||
} else {
|
} else {
|
||||||
let item = input.parse::<ItemFn>()?;
|
quote! { None }
|
||||||
Ok(ToolItem::Fn(item))
|
};
|
||||||
}
|
let tokens = quote! {
|
||||||
}
|
pub fn #fn_ident() -> rmcp::model::Tool {
|
||||||
}
|
rmcp::model::Tool {
|
||||||
|
name: #name.into(),
|
||||||
// dispatch impl function item and impl block item
|
description: #description,
|
||||||
pub(crate) fn tool(attr: TokenStream, input: TokenStream) -> syn::Result<TokenStream> {
|
input_schema: #input_schema,
|
||||||
let tool_item = syn::parse2::<ToolItem>(input)?;
|
annotations: #annotations,
|
||||||
match tool_item {
|
|
||||||
ToolItem::Fn(item) => tool_fn_item(attr, item),
|
|
||||||
ToolItem::Impl(item) => tool_impl_item(attr, item),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn tool_impl_item(attr: TokenStream, mut input: ItemImpl) -> syn::Result<TokenStream> {
|
|
||||||
let tool_impl_attr: ToolImplItemAttrs = syn::parse2(attr)?;
|
|
||||||
let tool_box_ident = tool_impl_attr.tool_box;
|
|
||||||
|
|
||||||
// get all tool function ident
|
|
||||||
let mut tool_fn_idents = Vec::new();
|
|
||||||
for item in &input.items {
|
|
||||||
if let syn::ImplItem::Fn(method) = item {
|
|
||||||
for attr in &method.attrs {
|
|
||||||
if attr.path().is_ident(TOOL_IDENT) {
|
|
||||||
tool_fn_idents.push(method.sig.ident.clone());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
syn::parse2::<ImplItemFn>(tokens)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// handle different cases
|
#[derive(FromMeta, Debug, Default)]
|
||||||
if input.trait_.is_some() {
|
#[darling(default)]
|
||||||
if let Some(ident) = tool_box_ident {
|
pub struct ToolAnnotationsAttribute {
|
||||||
// check if there are generic parameters
|
/// A human-readable title for the tool.
|
||||||
if !input.generics.params.is_empty() {
|
pub title: Option<String>,
|
||||||
// for trait implementation with generic parameters, directly use the already generated *_inner method
|
|
||||||
|
|
||||||
// generate call_tool method
|
/// If true, the tool does not modify its environment.
|
||||||
input.items.push(parse_quote! {
|
///
|
||||||
async fn call_tool(
|
/// Default: false
|
||||||
&self,
|
pub read_only_hint: Option<bool>,
|
||||||
request: rmcp::model::CallToolRequestParam,
|
|
||||||
context: rmcp::service::RequestContext<rmcp::RoleServer>,
|
|
||||||
) -> Result<rmcp::model::CallToolResult, rmcp::Error> {
|
|
||||||
self.call_tool_inner(request, context).await
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// generate list_tools method
|
/// If true, the tool may perform destructive updates to its environment.
|
||||||
input.items.push(parse_quote! {
|
/// If false, the tool performs only additive updates.
|
||||||
async fn list_tools(
|
///
|
||||||
&self,
|
/// (This property is meaningful only when `readOnlyHint == false`)
|
||||||
request: Option<rmcp::model::PaginatedRequestParam>,
|
///
|
||||||
context: rmcp::service::RequestContext<rmcp::RoleServer>,
|
/// Default: true
|
||||||
) -> Result<rmcp::model::ListToolsResult, rmcp::Error> {
|
/// A human-readable description of the tool's purpose.
|
||||||
self.list_tools_inner(request, context).await
|
pub destructive_hint: Option<bool>,
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// if there are no generic parameters, add tool box derive
|
|
||||||
input.items.push(parse_quote!(
|
|
||||||
rmcp::tool_box!(@derive #ident);
|
|
||||||
));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Err(syn::Error::new(
|
|
||||||
proc_macro2::Span::call_site(),
|
|
||||||
"tool_box attribute is required for trait implementation",
|
|
||||||
));
|
|
||||||
}
|
|
||||||
} else if let Some(ident) = tool_box_ident {
|
|
||||||
// if it is a normal impl block
|
|
||||||
if !input.generics.params.is_empty() {
|
|
||||||
// if there are generic parameters, not use tool_box! macro, but generate code directly
|
|
||||||
|
|
||||||
// create call code for each tool function
|
/// If true, calling the tool repeatedly with the same arguments
|
||||||
let match_arms = tool_fn_idents.iter().map(|ident| {
|
/// will have no additional effect on the its environment.
|
||||||
let attr_fn = Ident::new(&format!("{}_tool_attr", ident), ident.span());
|
///
|
||||||
let call_fn = Ident::new(&format!("{}_tool_call", ident), ident.span());
|
/// (This property is meaningful only when `readOnlyHint == false`)
|
||||||
quote! {
|
///
|
||||||
name if name == Self::#attr_fn().name => {
|
/// Default: false.
|
||||||
Self::#call_fn(tcc).await
|
pub idempotent_hint: Option<bool>,
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let tool_attrs = tool_fn_idents.iter().map(|ident| {
|
/// If true, this tool may interact with an "open world" of external
|
||||||
let attr_fn = Ident::new(&format!("{}_tool_attr", ident), ident.span());
|
/// entities. If false, the tool's domain of interaction is closed.
|
||||||
quote! { Self::#attr_fn() }
|
/// For example, the world of a web search tool is open, whereas that
|
||||||
});
|
/// of a memory tool is not.
|
||||||
|
///
|
||||||
|
/// Default: true
|
||||||
|
pub open_world_hint: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
// implement call_tool method
|
fn none_expr() -> Expr {
|
||||||
input.items.push(parse_quote! {
|
syn::parse2::<Expr>(quote! { None }).unwrap()
|
||||||
async fn call_tool_inner(
|
|
||||||
&self,
|
|
||||||
request: rmcp::model::CallToolRequestParam,
|
|
||||||
context: rmcp::service::RequestContext<rmcp::RoleServer>,
|
|
||||||
) -> Result<rmcp::model::CallToolResult, rmcp::Error> {
|
|
||||||
let tcc = rmcp::handler::server::tool::ToolCallContext::new(self, request, context);
|
|
||||||
match tcc.name() {
|
|
||||||
#(#match_arms,)*
|
|
||||||
_ => Err(rmcp::Error::invalid_params("tool not found", None)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// implement list_tools method
|
|
||||||
input.items.push(parse_quote! {
|
|
||||||
async fn list_tools_inner(
|
|
||||||
&self,
|
|
||||||
_: Option<rmcp::model::PaginatedRequestParam>,
|
|
||||||
_: rmcp::service::RequestContext<rmcp::RoleServer>,
|
|
||||||
) -> Result<rmcp::model::ListToolsResult, rmcp::Error> {
|
|
||||||
Ok(rmcp::model::ListToolsResult {
|
|
||||||
next_cursor: None,
|
|
||||||
tools: vec![#(#tool_attrs),*],
|
|
||||||
})
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// if there are no generic parameters, use the original tool_box! macro
|
|
||||||
let this_type_ident = &input.self_ty;
|
|
||||||
input.items.push(parse_quote!(
|
|
||||||
rmcp::tool_box!(#this_type_ident {
|
|
||||||
#(#tool_fn_idents),*
|
|
||||||
} #ident);
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(quote! {
|
|
||||||
#input
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// extract doc line from attribute
|
// extract doc line from attribute
|
||||||
fn extract_doc_line(attr: &syn::Attribute) -> Option<String> {
|
fn extract_doc_line(existing_docs: Option<String>, attr: &syn::Attribute) -> Option<String> {
|
||||||
if !attr.path().is_ident("doc") {
|
if !attr.path().is_ident("doc") {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
@ -360,391 +108,147 @@ fn extract_doc_line(attr: &syn::Attribute) -> Option<String> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let content = lit_str.value().trim().to_string();
|
let content = lit_str.value().trim().to_string();
|
||||||
|
match (existing_docs, content) {
|
||||||
(!content.is_empty()).then_some(content)
|
(Some(mut existing_docs), content) if !content.is_empty() => {
|
||||||
|
existing_docs.push('\n');
|
||||||
|
existing_docs.push_str(&content);
|
||||||
|
Some(existing_docs)
|
||||||
|
}
|
||||||
|
(Some(existing_docs), _) => Some(existing_docs),
|
||||||
|
(None, content) if !content.is_empty() => Some(content),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn tool_fn_item(attr: TokenStream, mut input_fn: ItemFn) -> syn::Result<TokenStream> {
|
pub fn tool(attr: TokenStream, input: TokenStream) -> syn::Result<TokenStream> {
|
||||||
let mut tool_macro_attrs = ToolAttrs::default();
|
let attribute = if attr.is_empty() {
|
||||||
let args: ToolFnItemAttrs = syn::parse2(attr)?;
|
Default::default()
|
||||||
tool_macro_attrs.fn_item = args;
|
|
||||||
// let mut fommated_fn_args: Punctuated<FnArg, Comma> = Punctuated::new();
|
|
||||||
let mut unextractable_args_indexes = HashSet::new();
|
|
||||||
for (index, mut fn_arg) in input_fn.sig.inputs.iter_mut().enumerate() {
|
|
||||||
enum Caught {
|
|
||||||
Param(ToolFnParamAttrs),
|
|
||||||
Aggregated(PatType),
|
|
||||||
}
|
|
||||||
let mut caught = None;
|
|
||||||
match &mut fn_arg {
|
|
||||||
FnArg::Receiver(_) => {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
FnArg::Typed(pat_type) => {
|
|
||||||
let mut serde_metas = Vec::new();
|
|
||||||
let mut schemars_metas = Vec::new();
|
|
||||||
let mut arg_ident = match pat_type.pat.as_ref() {
|
|
||||||
syn::Pat::Ident(pat_ident) => Some(pat_ident.ident.clone()),
|
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
let raw_attrs: Vec<_> = pat_type.attrs.drain(..).collect();
|
|
||||||
for attr in raw_attrs {
|
|
||||||
match &attr.meta {
|
|
||||||
syn::Meta::List(meta_list) => {
|
|
||||||
if meta_list.path.is_ident(TOOL_IDENT) {
|
|
||||||
let pat_type = pat_type.clone();
|
|
||||||
let marker = meta_list.parse_args::<ParamMarker>()?;
|
|
||||||
match marker {
|
|
||||||
ParamMarker::Param => {
|
|
||||||
let Some(arg_ident) = arg_ident.take() else {
|
|
||||||
return Err(syn::Error::new(
|
|
||||||
proc_macro2::Span::call_site(),
|
|
||||||
"input param must have an ident as name",
|
|
||||||
));
|
|
||||||
};
|
|
||||||
caught.replace(Caught::Param(ToolFnParamAttrs {
|
|
||||||
serde_meta: Vec::new(),
|
|
||||||
schemars_meta: Vec::new(),
|
|
||||||
ident: arg_ident,
|
|
||||||
rust_type: pat_type.ty.clone(),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
ParamMarker::Aggregated => {
|
|
||||||
caught.replace(Caught::Aggregated(pat_type.clone()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if meta_list.path.is_ident(SERDE_IDENT) {
|
|
||||||
serde_metas.push(meta_list.clone());
|
|
||||||
} else if meta_list.path.is_ident(SCHEMARS_IDENT) {
|
|
||||||
schemars_metas.push(meta_list.clone());
|
|
||||||
} else {
|
|
||||||
pat_type.attrs.push(attr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
pat_type.attrs.push(attr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
match caught {
|
|
||||||
Some(Caught::Param(mut param)) => {
|
|
||||||
param.serde_meta = serde_metas;
|
|
||||||
param.schemars_meta = schemars_metas;
|
|
||||||
match &mut tool_macro_attrs.params {
|
|
||||||
ToolParams::Params { attrs } => {
|
|
||||||
attrs.push(param);
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
tool_macro_attrs.params = ToolParams::Params { attrs: vec![param] };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unextractable_args_indexes.insert(index);
|
|
||||||
}
|
|
||||||
Some(Caught::Aggregated(rust_type)) => {
|
|
||||||
if let ToolParams::Params { .. } = tool_macro_attrs.params {
|
|
||||||
return Err(syn::Error::new(
|
|
||||||
rust_type.span(),
|
|
||||||
"cannot mix aggregated and individual parameters",
|
|
||||||
));
|
|
||||||
}
|
|
||||||
tool_macro_attrs.params = ToolParams::Aggregated { rust_type };
|
|
||||||
unextractable_args_indexes.insert(index);
|
|
||||||
}
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// input_fn.sig.inputs = fommated_fn_args;
|
|
||||||
let name = if let Some(expr) = tool_macro_attrs.fn_item.name {
|
|
||||||
expr
|
|
||||||
} else {
|
} else {
|
||||||
let fn_name = &input_fn.sig.ident;
|
let attr_args = NestedMeta::parse_meta_list(attr)?;
|
||||||
parse_quote! {
|
ToolAttribute::from_list(&attr_args)?
|
||||||
stringify!(#fn_name)
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
let tool_attr_fn_ident = Ident::new(
|
let mut fn_item = syn::parse2::<ImplItemFn>(input.clone())?;
|
||||||
&format!("{}_tool_attr", input_fn.sig.ident),
|
let fn_ident = &fn_item.sig.ident;
|
||||||
proc_macro2::Span::call_site(),
|
|
||||||
);
|
|
||||||
|
|
||||||
// generate get tool attr function
|
let tool_attr_fn_ident = format_ident!("{}_tool_attr", fn_ident);
|
||||||
let tool_attr_fn = {
|
let input_schema_expr = if let Some(input_schema) = attribute.input_schema {
|
||||||
let description = if let Some(expr) = tool_macro_attrs.fn_item.description {
|
input_schema
|
||||||
// Use explicitly provided description if available
|
} else {
|
||||||
expr
|
// try to find some parameters wrapper in the function
|
||||||
} else {
|
let params_ty = fn_item.sig.inputs.iter().find_map(|input| {
|
||||||
// Try to extract documentation comments
|
if let syn::FnArg::Typed(pat_type) = input {
|
||||||
let doc_content = input_fn
|
if let syn::Type::Path(type_path) = &*pat_type.ty {
|
||||||
.attrs
|
if type_path
|
||||||
.iter()
|
.path
|
||||||
.filter_map(extract_doc_line)
|
.segments
|
||||||
.collect::<Vec<_>>()
|
.last()
|
||||||
.join("\n");
|
.is_some_and(|type_name| type_name.ident == "Parameters")
|
||||||
|
|
||||||
parse_quote! {
|
|
||||||
#doc_content.trim().to_string()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let schema = match &tool_macro_attrs.params {
|
|
||||||
ToolParams::Aggregated { rust_type } => {
|
|
||||||
let ty = &rust_type.ty;
|
|
||||||
let schema = quote! {
|
|
||||||
rmcp::handler::server::tool::cached_schema_for_type::<#ty>()
|
|
||||||
};
|
|
||||||
schema
|
|
||||||
}
|
|
||||||
ToolParams::Params { attrs, .. } => {
|
|
||||||
let (param_type, temp_param_type_name) =
|
|
||||||
create_request_type(attrs, input_fn.sig.ident.to_string());
|
|
||||||
let schema = quote! {
|
|
||||||
{
|
{
|
||||||
#param_type
|
return Some(pat_type.ty.clone());
|
||||||
rmcp::handler::server::tool::cached_schema_for_type::<#temp_param_type_name>()
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
schema
|
|
||||||
}
|
|
||||||
ToolParams::NoParam => {
|
|
||||||
quote! {
|
|
||||||
rmcp::handler::server::tool::cached_schema_for_type::<rmcp::model::EmptyObject>()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
None
|
||||||
let input_fn_attrs = &input_fn.attrs;
|
});
|
||||||
let input_fn_vis = &input_fn.vis;
|
if let Some(params_ty) = params_ty {
|
||||||
|
// if found, use the Parameters schema
|
||||||
let annotations_code = if let Some(annotations) = &tool_macro_attrs.fn_item.annotations {
|
syn::parse2::<Expr>(quote! {
|
||||||
let annotations =
|
rmcp::handler::server::tool::cached_schema_for_type::<#params_ty>()
|
||||||
serde_json::to_string(&annotations.0).expect("failed to serialize annotations");
|
})?
|
||||||
quote! {
|
|
||||||
Some(serde_json::from_str::<rmcp::model::ToolAnnotations>(&#annotations).expect("Could not parse tool annotations"))
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
quote! { None }
|
// if not found, use the default EmptyObject schema
|
||||||
};
|
syn::parse2::<Expr>(quote! {
|
||||||
|
rmcp::handler::server::tool::cached_schema_for_type::<rmcp::model::EmptyObject>()
|
||||||
quote! {
|
})?
|
||||||
#(#input_fn_attrs)*
|
|
||||||
#input_fn_vis fn #tool_attr_fn_ident() -> rmcp::model::Tool {
|
|
||||||
rmcp::model::Tool {
|
|
||||||
name: #name.into(),
|
|
||||||
description: Some(#description.into()),
|
|
||||||
input_schema: #schema.into(),
|
|
||||||
annotations: #annotations_code,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
let annotations_expr = if let Some(annotations) = attribute.annotations {
|
||||||
// generate wrapped tool function
|
let ToolAnnotationsAttribute {
|
||||||
let tool_call_fn = {
|
title,
|
||||||
// wrapper function have the same sig:
|
read_only_hint,
|
||||||
// async fn #tool_tool_call(context: rmcp::handler::server::tool::ToolCallContext<'_, Self>)
|
destructive_hint,
|
||||||
// -> std::result::Result<rmcp::model::CallToolResult, rmcp::Error>
|
idempotent_hint,
|
||||||
//
|
open_world_hint,
|
||||||
// and the block part should be like:
|
} = annotations;
|
||||||
// {
|
fn wrap_option<T: ToTokens>(x: Option<T>) -> TokenStream {
|
||||||
// use rmcp::handler::server::tool::*;
|
x.map(|x| quote! {Some(#x.into())})
|
||||||
// let (t0, context) = <T0>::from_tool_call_context_part(context)?;
|
.unwrap_or(quote! { None })
|
||||||
// let (t1, context) = <T1>::from_tool_call_context_part(context)?;
|
}
|
||||||
// ...
|
let title = wrap_option(title);
|
||||||
// let (tn, context) = <Tn>::from_tool_call_context_part(context)?;
|
let read_only_hint = wrap_option(read_only_hint);
|
||||||
// // for params
|
let destructive_hint = wrap_option(destructive_hint);
|
||||||
// ... expand helper types here
|
let idempotent_hint = wrap_option(idempotent_hint);
|
||||||
// let (__rmcp_tool_req, context) = rmcp::model::JsonObject::from_tool_call_context_part(context)?;
|
let open_world_hint = wrap_option(open_world_hint);
|
||||||
// let __#TOOL_ToolCallParam { param_0, param_1, param_2, .. } = parse_json_object(__rmcp_tool_req)?;
|
let token_stream = quote! {
|
||||||
// // for aggr
|
Some(rmcp::model::ToolAnnotations {
|
||||||
// let (Parameters(aggr), context) = <Parameters<AggrType>>::from_tool_call_context_part(context)?;
|
title: #title,
|
||||||
// Self::#tool_ident(to, param_0, t1, param_1, ..., param_2, tn, aggr).await.into_call_tool_result()
|
read_only_hint: #read_only_hint,
|
||||||
//
|
destructive_hint: #destructive_hint,
|
||||||
// }
|
idempotent_hint: #idempotent_hint,
|
||||||
//
|
open_world_hint: #open_world_hint,
|
||||||
//
|
|
||||||
//
|
|
||||||
|
|
||||||
// for receiver type, name it as __rmcp_tool_receiver
|
|
||||||
let is_async = input_fn.sig.asyncness.is_some();
|
|
||||||
let receiver_ident = || Ident::new("__rmcp_tool_receiver", proc_macro2::Span::call_site());
|
|
||||||
// generate the extraction part for trivial args
|
|
||||||
let trivial_args = input_fn
|
|
||||||
.sig
|
|
||||||
.inputs
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.filter_map(|(index, arg)| {
|
|
||||||
if unextractable_args_indexes.contains(&index) {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
// get ident/type pair
|
|
||||||
let line = match arg {
|
|
||||||
FnArg::Typed(pat_type) => {
|
|
||||||
let pat = &pat_type.pat;
|
|
||||||
let ty = &pat_type.ty;
|
|
||||||
quote! {
|
|
||||||
let (#pat, context) = <#ty>::from_tool_call_context_part(context)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
FnArg::Receiver(r) => {
|
|
||||||
let ty = r.ty.clone();
|
|
||||||
let pat = receiver_ident();
|
|
||||||
quote! {
|
|
||||||
let (#pat, context) = <#ty>::from_tool_call_context_part(context)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Some(line)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
let trivial_arg_extraction_part = quote! {
|
|
||||||
#(#trivial_args)*
|
|
||||||
};
|
|
||||||
let processed_arg_extraction_part = match &mut tool_macro_attrs.params {
|
|
||||||
ToolParams::Aggregated { rust_type } => {
|
|
||||||
let PatType { pat, ty, .. } = rust_type;
|
|
||||||
quote! {
|
|
||||||
let (Parameters(#pat), context) = <Parameters<#ty>>::from_tool_call_context_part(context)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ToolParams::Params { attrs } => {
|
|
||||||
let (param_type, temp_param_type_name) =
|
|
||||||
create_request_type(attrs, input_fn.sig.ident.to_string());
|
|
||||||
|
|
||||||
let params_ident = attrs.iter().map(|attr| &attr.ident).collect::<Vec<_>>();
|
|
||||||
quote! {
|
|
||||||
#param_type
|
|
||||||
let (__rmcp_tool_req, context) = rmcp::model::JsonObject::from_tool_call_context_part(context)?;
|
|
||||||
let #temp_param_type_name {
|
|
||||||
#(#params_ident,)*
|
|
||||||
} = parse_json_object(__rmcp_tool_req)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ToolParams::NoParam => {
|
|
||||||
quote! {}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// generate the execution part
|
|
||||||
// has receiver?
|
|
||||||
let params = &input_fn
|
|
||||||
.sig
|
|
||||||
.inputs
|
|
||||||
.iter()
|
|
||||||
.map(|fn_arg| match fn_arg {
|
|
||||||
FnArg::Receiver(_) => {
|
|
||||||
let pat = receiver_ident();
|
|
||||||
quote! { #pat }
|
|
||||||
}
|
|
||||||
FnArg::Typed(pat_type) => {
|
|
||||||
let pat = &pat_type.pat.clone();
|
|
||||||
quote! { #pat }
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
|
||||||
let raw_fn_ident = &input_fn.sig.ident;
|
|
||||||
let call = if is_async {
|
|
||||||
quote! {
|
|
||||||
Self::#raw_fn_ident(#(#params),*).await.into_call_tool_result()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
quote! {
|
|
||||||
Self::#raw_fn_ident(#(#params),*).into_call_tool_result()
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
// assemble the whole function
|
syn::parse2::<Expr>(token_stream)?
|
||||||
let tool_call_fn_ident = Ident::new(
|
} else {
|
||||||
&format!("{}_tool_call", input_fn.sig.ident),
|
none_expr()
|
||||||
proc_macro2::Span::call_site(),
|
|
||||||
);
|
|
||||||
let raw_fn_vis = tool_macro_attrs
|
|
||||||
.fn_item
|
|
||||||
.vis
|
|
||||||
.as_ref()
|
|
||||||
.unwrap_or(&input_fn.vis);
|
|
||||||
let raw_fn_attr = &input_fn
|
|
||||||
.attrs
|
|
||||||
.iter()
|
|
||||||
.filter(|attr| !attr.path().is_ident(TOOL_IDENT))
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
quote! {
|
|
||||||
#(#raw_fn_attr)*
|
|
||||||
#raw_fn_vis async fn #tool_call_fn_ident(context: rmcp::handler::server::tool::ToolCallContext<'_, Self>)
|
|
||||||
-> std::result::Result<rmcp::model::CallToolResult, rmcp::Error> {
|
|
||||||
use rmcp::handler::server::tool::*;
|
|
||||||
#trivial_arg_extraction_part
|
|
||||||
#processed_arg_extraction_part
|
|
||||||
#call
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
let resolved_tool_attr = ResolvedToolAttribute {
|
||||||
|
name: attribute.name.unwrap_or_else(|| fn_ident.to_string()),
|
||||||
|
description: attribute
|
||||||
|
.description
|
||||||
|
.or_else(|| fn_item.attrs.iter().fold(None, extract_doc_line)),
|
||||||
|
input_schema: input_schema_expr,
|
||||||
|
annotations: annotations_expr,
|
||||||
|
};
|
||||||
|
let tool_attr_fn = resolved_tool_attr.into_fn(tool_attr_fn_ident)?;
|
||||||
|
// modify the the input function
|
||||||
|
if fn_item.sig.asyncness.is_some() {
|
||||||
|
// 1. remove asyncness from sig
|
||||||
|
// 2. make return type: `std::pin::Pin<Box<dyn Future<Output = #ReturnType> + Send + '_>>`
|
||||||
|
// 3. make body: { Box::pin(async move { #body }) }
|
||||||
|
let new_output = syn::parse2::<ReturnType>({
|
||||||
|
match &fn_item.sig.output {
|
||||||
|
syn::ReturnType::Default => {
|
||||||
|
quote! { -> std::pin::Pin<Box<dyn Future<Output = ()> + Send + '_>> }
|
||||||
|
}
|
||||||
|
syn::ReturnType::Type(_, ty) => {
|
||||||
|
quote! { -> std::pin::Pin<Box<dyn Future<Output = #ty> + Send + '_>> }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})?;
|
||||||
|
let prev_block = &fn_item.block;
|
||||||
|
let new_block = syn::parse2::<syn::Block>(quote! {
|
||||||
|
{ Box::pin(async move #prev_block ) }
|
||||||
|
})?;
|
||||||
|
fn_item.sig.asyncness = None;
|
||||||
|
fn_item.sig.output = new_output;
|
||||||
|
fn_item.block = new_block;
|
||||||
|
}
|
||||||
Ok(quote! {
|
Ok(quote! {
|
||||||
#tool_attr_fn
|
#tool_attr_fn
|
||||||
#tool_call_fn
|
#fn_item
|
||||||
#input_fn
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_request_type(attrs: &[ToolFnParamAttrs], tool_name: String) -> (TokenStream, Ident) {
|
|
||||||
let pascal_case_tool_name = tool_name.to_ascii_uppercase();
|
|
||||||
let temp_param_type_name = Ident::new(
|
|
||||||
&format!("__{pascal_case_tool_name}ToolCallParam",),
|
|
||||||
proc_macro2::Span::call_site(),
|
|
||||||
);
|
|
||||||
(
|
|
||||||
quote! {
|
|
||||||
use rmcp::{serde, schemars};
|
|
||||||
#[derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
|
|
||||||
pub struct #temp_param_type_name {
|
|
||||||
#(#attrs)*
|
|
||||||
}
|
|
||||||
},
|
|
||||||
temp_param_type_name,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
#[test]
|
|
||||||
fn test_tool_sync_macro() -> syn::Result<()> {
|
|
||||||
let attr = quote! {
|
|
||||||
name = "test_tool",
|
|
||||||
description = "test tool",
|
|
||||||
vis =
|
|
||||||
};
|
|
||||||
let input = quote! {
|
|
||||||
fn sum(&self, #[tool(aggr)] req: StructRequest) -> Result<CallToolResult, McpError> {
|
|
||||||
Ok(CallToolResult::success(vec![Content::text((req.a + req.b).to_string())]))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let input = tool(attr, input)?;
|
|
||||||
|
|
||||||
println!("input: {:#}", input);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_trait_tool_macro() -> syn::Result<()> {
|
fn test_trait_tool_macro() -> syn::Result<()> {
|
||||||
let attr = quote! {
|
let attr = quote! {
|
||||||
tool_box = Calculator
|
name = "direct-annotated-tool",
|
||||||
|
annotations(title = "Annotated Tool", read_only_hint = true)
|
||||||
};
|
};
|
||||||
let input = quote! {
|
let input = quote! {
|
||||||
impl ServerHandler for Calculator {
|
async fn async_method(&self, Parameters(Request { fields }): Parameters<Request>) {
|
||||||
#[tool]
|
drop(fields)
|
||||||
fn get_info(&self) -> ServerInfo {
|
|
||||||
ServerInfo {
|
|
||||||
instructions: Some("A simple calculator".into()),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let input = tool(attr, input)?;
|
let _input = tool(attr, input)?;
|
||||||
|
|
||||||
println!("input: {:#}", input);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_doc_comment_description() -> syn::Result<()> {
|
fn test_doc_comment_description() -> syn::Result<()> {
|
||||||
let attr = quote! {}; // No explicit description
|
let attr = quote! {}; // No explicit description
|
||||||
|
|
@ -764,6 +268,7 @@ mod test {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_explicit_description_priority() -> syn::Result<()> {
|
fn test_explicit_description_priority() -> syn::Result<()> {
|
||||||
let attr = quote! {
|
let attr = quote! {
|
||||||
|
|
|
||||||
51
crates/rmcp-macros/src/tool_handler.rs
Normal file
51
crates/rmcp-macros/src/tool_handler.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
use darling::{FromMeta, ast::NestedMeta};
|
||||||
|
use proc_macro2::TokenStream;
|
||||||
|
use quote::{ToTokens, quote};
|
||||||
|
use syn::{Expr, ImplItem, ItemImpl};
|
||||||
|
|
||||||
|
#[derive(FromMeta)]
|
||||||
|
#[darling(default)]
|
||||||
|
pub struct ToolHandlerAttribute {
|
||||||
|
pub router: Expr,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ToolHandlerAttribute {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
router: syn::parse2(quote! {
|
||||||
|
self.tool_router
|
||||||
|
})
|
||||||
|
.unwrap(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tool_handler(attr: TokenStream, input: TokenStream) -> syn::Result<TokenStream> {
|
||||||
|
let attr_args = NestedMeta::parse_meta_list(attr)?;
|
||||||
|
let ToolHandlerAttribute { router } = ToolHandlerAttribute::from_list(&attr_args)?;
|
||||||
|
let mut item_impl = syn::parse2::<ItemImpl>(input.clone())?;
|
||||||
|
let tool_call_fn = quote! {
|
||||||
|
async fn call_tool(
|
||||||
|
&self,
|
||||||
|
request: rmcp::model::CallToolRequestParam,
|
||||||
|
context: rmcp::service::RequestContext<rmcp::RoleServer>,
|
||||||
|
) -> Result<rmcp::model::CallToolResult, rmcp::Error> {
|
||||||
|
let tcc = rmcp::handler::server::tool::ToolCallContext::new(self, request, context);
|
||||||
|
#router.call(tcc).await
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let tool_list_fn = quote! {
|
||||||
|
async fn list_tools(
|
||||||
|
&self,
|
||||||
|
_request: Option<rmcp::model::PaginatedRequestParam>,
|
||||||
|
_context: rmcp::service::RequestContext<rmcp::RoleServer>,
|
||||||
|
) -> Result<rmcp::model::ListToolsResult, rmcp::Error> {
|
||||||
|
Ok(rmcp::model::ListToolsResult::with_all_items(#router.list_all()))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let tool_call_fn = syn::parse2::<ImplItem>(tool_call_fn)?;
|
||||||
|
let tool_list_fn = syn::parse2::<ImplItem>(tool_list_fn)?;
|
||||||
|
item_impl.items.push(tool_call_fn);
|
||||||
|
item_impl.items.push(tool_list_fn);
|
||||||
|
Ok(item_impl.into_token_stream())
|
||||||
|
}
|
||||||
90
crates/rmcp-macros/src/tool_router.rs
Normal file
90
crates/rmcp-macros/src/tool_router.rs
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
//! ```ignore
|
||||||
|
//! #[rmcp::tool_router(router)]
|
||||||
|
//! impl Handler {
|
||||||
|
//!
|
||||||
|
//! }
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
|
||||||
|
use darling::{FromMeta, ast::NestedMeta};
|
||||||
|
use proc_macro2::TokenStream;
|
||||||
|
use quote::{ToTokens, format_ident, quote};
|
||||||
|
use syn::{Ident, ImplItem, ItemImpl, Visibility};
|
||||||
|
|
||||||
|
#[derive(FromMeta)]
|
||||||
|
#[darling(default)]
|
||||||
|
pub struct ToolRouterAttribute {
|
||||||
|
pub router: Ident,
|
||||||
|
pub vis: Option<Visibility>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ToolRouterAttribute {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
router: format_ident!("tool_router"),
|
||||||
|
vis: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tool_router(attr: TokenStream, input: TokenStream) -> syn::Result<TokenStream> {
|
||||||
|
let attr_args = NestedMeta::parse_meta_list(attr)?;
|
||||||
|
let ToolRouterAttribute { router, vis } = ToolRouterAttribute::from_list(&attr_args)?;
|
||||||
|
let mut item_impl = syn::parse2::<ItemImpl>(input.clone())?;
|
||||||
|
// find all function marked with `#[rmcp::tool]`
|
||||||
|
let tool_attr_fns: Vec<_> = item_impl
|
||||||
|
.items
|
||||||
|
.iter()
|
||||||
|
.filter_map(|item| {
|
||||||
|
if let syn::ImplItem::Fn(fn_item) = item {
|
||||||
|
fn_item
|
||||||
|
.attrs
|
||||||
|
.iter()
|
||||||
|
.any(|attr| {
|
||||||
|
attr.path()
|
||||||
|
.segments
|
||||||
|
.last()
|
||||||
|
.is_some_and(|seg| seg.ident == "tool")
|
||||||
|
})
|
||||||
|
.then_some(&fn_item.sig.ident)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let mut routers = vec![];
|
||||||
|
for handler in tool_attr_fns {
|
||||||
|
let tool_attr_fn_ident = format_ident!("{handler}_tool_attr");
|
||||||
|
routers.push(quote! {
|
||||||
|
.with_route((Self::#tool_attr_fn_ident(), Self::#handler))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
let router_fn = syn::parse2::<ImplItem>(quote! {
|
||||||
|
#vis fn #router() -> rmcp::handler::server::router::tool::ToolRouter<Self> {
|
||||||
|
rmcp::handler::server::router::tool::ToolRouter::<Self>::new()
|
||||||
|
#(#routers)*
|
||||||
|
}
|
||||||
|
})?;
|
||||||
|
item_impl.items.push(router_fn);
|
||||||
|
Ok(item_impl.into_token_stream())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn test_router_attr() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let attr = quote! {
|
||||||
|
router = test_router,
|
||||||
|
};
|
||||||
|
let attr_args = NestedMeta::parse_meta_list(attr)?;
|
||||||
|
let ToolRouterAttribute { router, vis } = ToolRouterAttribute::from_list(&attr_args)?;
|
||||||
|
println!("router: {}", router);
|
||||||
|
if let Some(vis) = vis {
|
||||||
|
println!("visibility: {}", vis.to_token_stream());
|
||||||
|
} else {
|
||||||
|
println!("visibility: None");
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -63,7 +63,6 @@ http-body-util = { version = "0.1", optional = true }
|
||||||
bytes = { version = "1", optional = true }
|
bytes = { version = "1", optional = true }
|
||||||
# macro
|
# macro
|
||||||
rmcp-macros = { version = "0.1", workspace = true, optional = true }
|
rmcp-macros = { version = "0.1", workspace = true, optional = true }
|
||||||
|
|
||||||
[target.'cfg(not(all(target_family = "wasm", target_os = "unknown")))'.dependencies]
|
[target.'cfg(not(all(target_family = "wasm", target_os = "unknown")))'.dependencies]
|
||||||
chrono = { version = "0.4.38", features = ["serde"] }
|
chrono = { version = "0.4.38", features = ["serde"] }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,20 +15,22 @@ wait for the first release.
|
||||||
Creating a server with tools is simple using the `#[tool]` macro:
|
Creating a server with tools is simple using the `#[tool]` macro:
|
||||||
|
|
||||||
```rust, ignore
|
```rust, ignore
|
||||||
use rmcp::{Error as McpError, ServiceExt, model::*, tool, transport::stdio};
|
use rmcp::{Error as McpError, ServiceExt, model::*, tool, tool_router, transport::stdio, handler::server::tool::ToolCallContext, handler::server::router::tool::ToolRouter};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Counter {
|
pub struct Counter {
|
||||||
counter: Arc<Mutex<i32>>,
|
counter: Arc<Mutex<i32>>,
|
||||||
|
tool_router: ToolRouter<Self>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tool(tool_box)]
|
#[tool_router]
|
||||||
impl Counter {
|
impl Counter {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
counter: Arc::new(Mutex::new(0)),
|
counter: Arc::new(Mutex::new(0)),
|
||||||
|
tool_router: Self::tool_router(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -51,7 +53,7 @@ impl Counter {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implement the server handler
|
// Implement the server handler
|
||||||
#[tool(tool_box)]
|
#[tool_handler]
|
||||||
impl rmcp::ServerHandler for Counter {
|
impl rmcp::ServerHandler for Counter {
|
||||||
fn get_info(&self) -> ServerInfo {
|
fn get_info(&self) -> ServerInfo {
|
||||||
ServerInfo {
|
ServerInfo {
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
mod resource;
|
mod resource;
|
||||||
|
pub mod router;
|
||||||
pub mod tool;
|
pub mod tool;
|
||||||
pub mod wrapper;
|
pub mod wrapper;
|
||||||
impl<H: ServerHandler> Service<RoleServer> for H {
|
impl<H: ServerHandler> Service<RoleServer> for H {
|
||||||
|
|
|
||||||
96
crates/rmcp/src/handler/server/router.rs
Normal file
96
crates/rmcp/src/handler/server/router.rs
Normal file
|
|
@ -0,0 +1,96 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use tool::{IntoToolRoute, ToolRoute};
|
||||||
|
|
||||||
|
use super::ServerHandler;
|
||||||
|
use crate::{
|
||||||
|
RoleServer, Service,
|
||||||
|
model::{ClientRequest, ListToolsResult, ServerResult},
|
||||||
|
service::NotificationContext,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub mod tool;
|
||||||
|
|
||||||
|
pub struct Router<S> {
|
||||||
|
pub tool_router: tool::ToolRouter<S>,
|
||||||
|
pub service: Arc<S>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> Router<S>
|
||||||
|
where
|
||||||
|
S: ServerHandler,
|
||||||
|
{
|
||||||
|
pub fn new(service: S) -> Self {
|
||||||
|
Self {
|
||||||
|
tool_router: tool::ToolRouter::new(),
|
||||||
|
service: Arc::new(service),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_tool<R, A>(mut self, route: R) -> Self
|
||||||
|
where
|
||||||
|
R: IntoToolRoute<S, A>,
|
||||||
|
{
|
||||||
|
self.tool_router.add_route(route.into_tool_route());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_tools(mut self, routes: impl IntoIterator<Item = ToolRoute<S>>) -> Self {
|
||||||
|
for route in routes {
|
||||||
|
self.tool_router.add_route(route);
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> Service<RoleServer> for Router<S>
|
||||||
|
where
|
||||||
|
S: ServerHandler,
|
||||||
|
{
|
||||||
|
async fn handle_notification(
|
||||||
|
&self,
|
||||||
|
notification: <RoleServer as crate::service::ServiceRole>::PeerNot,
|
||||||
|
context: NotificationContext<RoleServer>,
|
||||||
|
) -> Result<(), crate::Error> {
|
||||||
|
self.service
|
||||||
|
.handle_notification(notification, context)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
async fn handle_request(
|
||||||
|
&self,
|
||||||
|
request: <RoleServer as crate::service::ServiceRole>::PeerReq,
|
||||||
|
context: crate::service::RequestContext<RoleServer>,
|
||||||
|
) -> Result<<RoleServer as crate::service::ServiceRole>::Resp, crate::Error> {
|
||||||
|
match request {
|
||||||
|
ClientRequest::CallToolRequest(request) => {
|
||||||
|
if self.tool_router.has_route(request.params.name.as_ref())
|
||||||
|
|| !self.tool_router.transparent_when_not_found
|
||||||
|
{
|
||||||
|
let tool_call_context = crate::handler::server::tool::ToolCallContext::new(
|
||||||
|
self.service.as_ref(),
|
||||||
|
request.params,
|
||||||
|
context,
|
||||||
|
);
|
||||||
|
let result = self.tool_router.call(tool_call_context).await?;
|
||||||
|
Ok(ServerResult::CallToolResult(result))
|
||||||
|
} else {
|
||||||
|
self.service
|
||||||
|
.handle_request(ClientRequest::CallToolRequest(request), context)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ClientRequest::ListToolsRequest(_) => {
|
||||||
|
let tools = self.tool_router.list_all();
|
||||||
|
Ok(ServerResult::ListToolsResult(ListToolsResult {
|
||||||
|
tools,
|
||||||
|
next_cursor: None,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
rest => self.service.handle_request(rest, context).await,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_info(&self) -> <RoleServer as crate::service::ServiceRole>::Info {
|
||||||
|
self.service.get_info()
|
||||||
|
}
|
||||||
|
}
|
||||||
0
crates/rmcp/src/handler/server/router/prompt.rs
Normal file
0
crates/rmcp/src/handler/server/router/prompt.rs
Normal file
272
crates/rmcp/src/handler/server/router/tool.rs
Normal file
272
crates/rmcp/src/handler/server/router/tool.rs
Normal file
|
|
@ -0,0 +1,272 @@
|
||||||
|
use std::{borrow::Cow, sync::Arc};
|
||||||
|
|
||||||
|
use futures::{FutureExt, future::BoxFuture};
|
||||||
|
use schemars::JsonSchema;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
handler::server::tool::{
|
||||||
|
CallToolHandler, DynCallToolHandler, ToolCallContext, schema_for_type,
|
||||||
|
},
|
||||||
|
model::{CallToolResult, Tool, ToolAnnotations},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct ToolRoute<S> {
|
||||||
|
#[allow(clippy::type_complexity)]
|
||||||
|
pub call: Arc<DynCallToolHandler<S>>,
|
||||||
|
pub attr: crate::model::Tool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> std::fmt::Debug for ToolRoute<S> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
f.debug_struct("ToolRoute")
|
||||||
|
.field("name", &self.attr.name)
|
||||||
|
.field("description", &self.attr.description)
|
||||||
|
.field("input_schema", &self.attr.input_schema)
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> Clone for ToolRoute<S> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
call: self.call.clone(),
|
||||||
|
attr: self.attr.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S: Send + Sync + 'static> ToolRoute<S> {
|
||||||
|
pub fn new<C, A>(attr: impl Into<Tool>, call: C) -> Self
|
||||||
|
where
|
||||||
|
C: CallToolHandler<S, A> + Send + Sync + Clone + 'static,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
call: Arc::new(move |context: ToolCallContext<S>| {
|
||||||
|
let call = call.clone();
|
||||||
|
context.invoke(call).boxed()
|
||||||
|
}),
|
||||||
|
attr: attr.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn new_dyn<C>(attr: impl Into<Tool>, call: C) -> Self
|
||||||
|
where
|
||||||
|
C: for<'a> Fn(
|
||||||
|
ToolCallContext<'a, S>,
|
||||||
|
) -> BoxFuture<'a, Result<CallToolResult, crate::Error>>
|
||||||
|
+ Send
|
||||||
|
+ Sync
|
||||||
|
+ 'static,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
call: Arc::new(call),
|
||||||
|
attr: attr.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn name(&self) -> &str {
|
||||||
|
&self.attr.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait IntoToolRoute<S, A> {
|
||||||
|
fn into_tool_route(self) -> ToolRoute<S>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S, C, A, T> IntoToolRoute<S, A> for (T, C)
|
||||||
|
where
|
||||||
|
S: Send + Sync + 'static,
|
||||||
|
C: CallToolHandler<S, A> + Send + Sync + Clone + 'static,
|
||||||
|
T: Into<Tool>,
|
||||||
|
{
|
||||||
|
fn into_tool_route(self) -> ToolRoute<S> {
|
||||||
|
ToolRoute::new(self.0.into(), self.1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> IntoToolRoute<S, ()> for ToolRoute<S>
|
||||||
|
where
|
||||||
|
S: Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
fn into_tool_route(self) -> ToolRoute<S> {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ToolAttrGenerateFunctionAdapter;
|
||||||
|
impl<S, F> IntoToolRoute<S, ToolAttrGenerateFunctionAdapter> for F
|
||||||
|
where
|
||||||
|
S: Send + Sync + 'static,
|
||||||
|
F: Fn() -> ToolRoute<S>,
|
||||||
|
{
|
||||||
|
fn into_tool_route(self) -> ToolRoute<S> {
|
||||||
|
(self)()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait CallToolHandlerExt<S, A>: Sized
|
||||||
|
where
|
||||||
|
Self: CallToolHandler<S, A> + Send + Sync + 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,
|
||||||
|
{
|
||||||
|
fn name(self, name: impl Into<Cow<'static, str>>) -> WithToolAttr<Self, S, A> {
|
||||||
|
WithToolAttr {
|
||||||
|
attr: Tool::new(
|
||||||
|
name.into(),
|
||||||
|
"",
|
||||||
|
schema_for_type::<crate::model::JsonObject>(),
|
||||||
|
),
|
||||||
|
call: self,
|
||||||
|
_marker: std::marker::PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct WithToolAttr<C, S, A>
|
||||||
|
where
|
||||||
|
C: CallToolHandler<S, A> + Send + Sync + Clone + 'static,
|
||||||
|
{
|
||||||
|
pub attr: crate::model::Tool,
|
||||||
|
pub call: C,
|
||||||
|
pub _marker: std::marker::PhantomData<fn(S, A)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
{
|
||||||
|
fn into_tool_route(self) -> ToolRoute<S> {
|
||||||
|
ToolRoute::new(self.attr, self.call)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<C, S, A> WithToolAttr<C, S, A>
|
||||||
|
where
|
||||||
|
C: CallToolHandler<S, A> + Send + Sync + Clone + 'static,
|
||||||
|
{
|
||||||
|
pub fn description(mut self, description: impl Into<Cow<'static, str>>) -> Self {
|
||||||
|
self.attr.description = Some(description.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
pub fn parameters<T: JsonSchema>(mut self) -> Self {
|
||||||
|
self.attr.input_schema = schema_for_type::<T>().into();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
pub fn parameters_value(mut self, schema: serde_json::Value) -> Self {
|
||||||
|
self.attr.input_schema = crate::model::object(schema).into();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
pub fn annotation(mut self, annotation: impl Into<ToolAnnotations>) -> Self {
|
||||||
|
self.attr.annotations = Some(annotation.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ToolRouter<S> {
|
||||||
|
#[allow(clippy::type_complexity)]
|
||||||
|
pub map: std::collections::HashMap<Cow<'static, str>, ToolRoute<S>>,
|
||||||
|
|
||||||
|
pub transparent_when_not_found: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> Default for ToolRouter<S> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
map: std::collections::HashMap::new(),
|
||||||
|
transparent_when_not_found: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<S> Clone for ToolRouter<S> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
map: self.map.clone(),
|
||||||
|
transparent_when_not_found: self.transparent_when_not_found,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> IntoIterator for ToolRouter<S> {
|
||||||
|
type Item = ToolRoute<S>;
|
||||||
|
type IntoIter = std::collections::hash_map::IntoValues<Cow<'static, str>, ToolRoute<S>>;
|
||||||
|
|
||||||
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
self.map.into_values()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> ToolRouter<S>
|
||||||
|
where
|
||||||
|
S: Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
map: std::collections::HashMap::new(),
|
||||||
|
transparent_when_not_found: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn with_route<R, A>(mut self, route: R) -> Self
|
||||||
|
where
|
||||||
|
R: IntoToolRoute<S, A>,
|
||||||
|
{
|
||||||
|
self.add_route(route.into_tool_route());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_route(&mut self, item: ToolRoute<S>) {
|
||||||
|
self.map.insert(item.attr.name.clone(), item);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn merge(&mut self, other: ToolRouter<S>) {
|
||||||
|
for item in other.map.into_values() {
|
||||||
|
self.add_route(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove_route<H, A>(&mut self, name: &str) {
|
||||||
|
self.map.remove(name);
|
||||||
|
}
|
||||||
|
pub fn has_route(&self, name: &str) -> bool {
|
||||||
|
self.map.contains_key(name)
|
||||||
|
}
|
||||||
|
pub async fn call(
|
||||||
|
&self,
|
||||||
|
context: ToolCallContext<'_, S>,
|
||||||
|
) -> Result<CallToolResult, crate::Error> {
|
||||||
|
let item = self
|
||||||
|
.map
|
||||||
|
.get(context.name())
|
||||||
|
.ok_or_else(|| crate::Error::invalid_params("tool not found", None))?;
|
||||||
|
(item.call)(context).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn list_all(&self) -> Vec<crate::model::Tool> {
|
||||||
|
self.map.values().map(|item| item.attr.clone()).collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> std::ops::Add<ToolRouter<S>> for ToolRouter<S>
|
||||||
|
where
|
||||||
|
S: Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn add(mut self, other: ToolRouter<S>) -> Self::Output {
|
||||||
|
self.merge(other);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> std::ops::AddAssign<ToolRouter<S>> for ToolRouter<S>
|
||||||
|
where
|
||||||
|
S: Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
fn add_assign(&mut self, other: ToolRouter<S>) {
|
||||||
|
self.merge(other);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,16 +2,18 @@ use std::{
|
||||||
any::TypeId, borrow::Cow, collections::HashMap, future::Ready, marker::PhantomData, sync::Arc,
|
any::TypeId, borrow::Cow, collections::HashMap, future::Ready, marker::PhantomData, sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
use futures::future::BoxFuture;
|
use futures::future::{BoxFuture, FutureExt};
|
||||||
use schemars::JsonSchema;
|
use schemars::JsonSchema;
|
||||||
use serde::{Deserialize, Serialize, de::DeserializeOwned};
|
use serde::{Deserialize, Serialize, de::DeserializeOwned};
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
|
|
||||||
|
pub use super::router::tool::{ToolRoute, ToolRouter};
|
||||||
use crate::{
|
use crate::{
|
||||||
RoleServer,
|
RoleServer,
|
||||||
model::{CallToolRequestParam, CallToolResult, ConstString, IntoContents, JsonObject},
|
model::{CallToolRequestParam, CallToolResult, IntoContents, JsonObject},
|
||||||
service::RequestContext,
|
service::RequestContext,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A shortcut for generating a JSON schema for a type.
|
/// A shortcut for generating a JSON schema for a type.
|
||||||
pub fn schema_for_type<T: JsonSchema>() -> JsonObject {
|
pub fn schema_for_type<T: JsonSchema>() -> JsonObject {
|
||||||
// explicitly to align json schema version to official specifications.
|
// explicitly to align json schema version to official specifications.
|
||||||
|
|
@ -62,16 +64,16 @@ pub fn parse_json_object<T: DeserializeOwned>(input: JsonObject) -> Result<T, cr
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub struct ToolCallContext<'service, S> {
|
pub struct ToolCallContext<'s, S> {
|
||||||
request_context: RequestContext<RoleServer>,
|
pub request_context: RequestContext<RoleServer>,
|
||||||
service: &'service S,
|
pub service: &'s S,
|
||||||
name: Cow<'static, str>,
|
pub name: Cow<'static, str>,
|
||||||
arguments: Option<JsonObject>,
|
pub arguments: Option<JsonObject>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'service, S> ToolCallContext<'service, S> {
|
impl<'s, S> ToolCallContext<'s, S> {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
service: &'service S,
|
service: &'s S,
|
||||||
CallToolRequestParam { name, arguments }: CallToolRequestParam,
|
CallToolRequestParam { name, arguments }: CallToolRequestParam,
|
||||||
request_context: RequestContext<RoleServer>,
|
request_context: RequestContext<RoleServer>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
|
@ -90,10 +92,8 @@ impl<'service, S> ToolCallContext<'service, S> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait FromToolCallContextPart<'a, S>: Sized {
|
pub trait FromToolCallContextPart<S>: Sized {
|
||||||
fn from_tool_call_context_part(
|
fn from_tool_call_context_part(context: &mut ToolCallContext<S>) -> Result<Self, crate::Error>;
|
||||||
context: ToolCallContext<'a, S>,
|
|
||||||
) -> Result<(Self, ToolCallContext<'a, S>), crate::Error>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait IntoCallToolResult {
|
pub trait IntoCallToolResult {
|
||||||
|
|
@ -161,16 +161,16 @@ impl IntoCallToolResult for Result<CallToolResult, crate::Error> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait CallToolHandler<'a, S, A> {
|
pub trait CallToolHandler<S, A> {
|
||||||
type Fut: Future<Output = Result<CallToolResult, crate::Error>> + Send + 'a;
|
fn call(
|
||||||
fn call(self, context: ToolCallContext<'a, S>) -> Self::Fut;
|
self,
|
||||||
|
context: ToolCallContext<'_, S>,
|
||||||
|
) -> BoxFuture<'_, Result<CallToolResult, crate::Error>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type DynCallToolHandler<S> = dyn Fn(ToolCallContext<'_, S>) -> BoxFuture<'_, Result<CallToolResult, crate::Error>>
|
pub type DynCallToolHandler<S> = dyn for<'s> Fn(ToolCallContext<'s, S>) -> BoxFuture<'s, Result<CallToolResult, crate::Error>>
|
||||||
+ Send
|
+ Send
|
||||||
+ Sync;
|
+ Sync;
|
||||||
/// Parameter Extractor
|
|
||||||
pub struct Parameter<K: ConstString, V>(pub K, pub V);
|
|
||||||
|
|
||||||
/// Parameter Extractor
|
/// Parameter Extractor
|
||||||
///
|
///
|
||||||
|
|
@ -188,83 +188,25 @@ impl<P: JsonSchema> JsonSchema for Parameters<P> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Callee Extractor
|
impl<S> FromToolCallContextPart<S> for CancellationToken {
|
||||||
pub struct Callee<'a, S>(pub &'a S);
|
fn from_tool_call_context_part(context: &mut ToolCallContext<S>) -> Result<Self, crate::Error> {
|
||||||
|
Ok(context.request_context.ct.clone())
|
||||||
impl<'a, S> FromToolCallContextPart<'a, S> for CancellationToken {
|
|
||||||
fn from_tool_call_context_part(
|
|
||||||
context: ToolCallContext<'a, S>,
|
|
||||||
) -> Result<(Self, ToolCallContext<'a, S>), crate::Error> {
|
|
||||||
Ok((context.request_context.ct.clone(), context))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, S> FromToolCallContextPart<'a, S> for Callee<'a, S> {
|
|
||||||
fn from_tool_call_context_part(
|
|
||||||
context: ToolCallContext<'a, S>,
|
|
||||||
) -> Result<(Self, ToolCallContext<'a, S>), crate::Error> {
|
|
||||||
Ok((Callee(context.service), context))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ToolName(pub Cow<'static, str>);
|
pub struct ToolName(pub Cow<'static, str>);
|
||||||
|
|
||||||
impl<'a, S> FromToolCallContextPart<'a, S> for ToolName {
|
impl<S> FromToolCallContextPart<S> for ToolName {
|
||||||
fn from_tool_call_context_part(
|
fn from_tool_call_context_part(context: &mut ToolCallContext<S>) -> Result<Self, crate::Error> {
|
||||||
context: ToolCallContext<'a, S>,
|
Ok(Self(context.name.clone()))
|
||||||
) -> Result<(Self, ToolCallContext<'a, S>), crate::Error> {
|
|
||||||
Ok((Self(context.name.clone()), context))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, S> FromToolCallContextPart<'a, S> for &'a S {
|
impl<S, P> FromToolCallContextPart<S> for Parameters<P>
|
||||||
fn from_tool_call_context_part(
|
|
||||||
context: ToolCallContext<'a, S>,
|
|
||||||
) -> Result<(Self, ToolCallContext<'a, S>), crate::Error> {
|
|
||||||
Ok((context.service, context))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, S, K, V> FromToolCallContextPart<'a, S> for Parameter<K, V>
|
|
||||||
where
|
|
||||||
K: ConstString,
|
|
||||||
V: DeserializeOwned,
|
|
||||||
{
|
|
||||||
fn from_tool_call_context_part(
|
|
||||||
context: ToolCallContext<'a, S>,
|
|
||||||
) -> Result<(Self, ToolCallContext<'a, S>), crate::Error> {
|
|
||||||
let arguments = context
|
|
||||||
.arguments
|
|
||||||
.as_ref()
|
|
||||||
.ok_or(crate::Error::invalid_params(
|
|
||||||
format!("missing parameter {field}", field = K::VALUE),
|
|
||||||
None,
|
|
||||||
))?;
|
|
||||||
let value = arguments.get(K::VALUE).ok_or(crate::Error::invalid_params(
|
|
||||||
format!("missing parameter {field}", field = K::VALUE),
|
|
||||||
None,
|
|
||||||
))?;
|
|
||||||
let value: V = serde_json::from_value(value.clone()).map_err(|e| {
|
|
||||||
crate::Error::invalid_params(
|
|
||||||
format!(
|
|
||||||
"failed to deserialize parameter {field}: {error}",
|
|
||||||
field = K::VALUE,
|
|
||||||
error = e
|
|
||||||
),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
Ok((Parameter(K::default(), value), context))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, S, P> FromToolCallContextPart<'a, S> for Parameters<P>
|
|
||||||
where
|
where
|
||||||
P: DeserializeOwned,
|
P: DeserializeOwned,
|
||||||
{
|
{
|
||||||
fn from_tool_call_context_part(
|
fn from_tool_call_context_part(context: &mut ToolCallContext<S>) -> Result<Self, crate::Error> {
|
||||||
mut context: ToolCallContext<'a, S>,
|
|
||||||
) -> Result<(Self, ToolCallContext<'a, S>), crate::Error> {
|
|
||||||
let arguments = context.arguments.take().unwrap_or_default();
|
let arguments = context.arguments.take().unwrap_or_default();
|
||||||
let value: P =
|
let value: P =
|
||||||
serde_json::from_value(serde_json::Value::Object(arguments)).map_err(|e| {
|
serde_json::from_value(serde_json::Value::Object(arguments)).map_err(|e| {
|
||||||
|
|
@ -273,37 +215,31 @@ where
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
Ok((Parameters(value), context))
|
Ok(Parameters(value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, S> FromToolCallContextPart<'a, S> for JsonObject {
|
impl<S> FromToolCallContextPart<S> for JsonObject {
|
||||||
fn from_tool_call_context_part(
|
fn from_tool_call_context_part(context: &mut ToolCallContext<S>) -> Result<Self, crate::Error> {
|
||||||
mut context: ToolCallContext<'a, S>,
|
|
||||||
) -> Result<(Self, ToolCallContext<'a, S>), crate::Error> {
|
|
||||||
let object = context.arguments.take().unwrap_or_default();
|
let object = context.arguments.take().unwrap_or_default();
|
||||||
Ok((object, context))
|
Ok(object)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, S> FromToolCallContextPart<'a, S> for crate::model::Extensions {
|
impl<S> FromToolCallContextPart<S> for crate::model::Extensions {
|
||||||
fn from_tool_call_context_part(
|
fn from_tool_call_context_part(context: &mut ToolCallContext<S>) -> Result<Self, crate::Error> {
|
||||||
context: ToolCallContext<'a, S>,
|
|
||||||
) -> Result<(Self, ToolCallContext<'a, S>), crate::Error> {
|
|
||||||
let extensions = context.request_context.extensions.clone();
|
let extensions = context.request_context.extensions.clone();
|
||||||
Ok((extensions, context))
|
Ok(extensions)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Extension<T>(pub T);
|
pub struct Extension<T>(pub T);
|
||||||
|
|
||||||
impl<'a, S, T> FromToolCallContextPart<'a, S> for Extension<T>
|
impl<S, T> FromToolCallContextPart<S> for Extension<T>
|
||||||
where
|
where
|
||||||
T: Send + Sync + 'static + Clone,
|
T: Send + Sync + 'static + Clone,
|
||||||
{
|
{
|
||||||
fn from_tool_call_context_part(
|
fn from_tool_call_context_part(context: &mut ToolCallContext<S>) -> Result<Self, crate::Error> {
|
||||||
context: ToolCallContext<'a, S>,
|
|
||||||
) -> Result<(Self, ToolCallContext<'a, S>), crate::Error> {
|
|
||||||
let extension = context
|
let extension = context
|
||||||
.request_context
|
.request_context
|
||||||
.extensions
|
.extensions
|
||||||
|
|
@ -315,58 +251,52 @@ where
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
Ok((Extension(extension), context))
|
Ok(Extension(extension))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, S> FromToolCallContextPart<'a, S> for crate::Peer<RoleServer> {
|
impl<S> FromToolCallContextPart<S> for crate::Peer<RoleServer> {
|
||||||
fn from_tool_call_context_part(
|
fn from_tool_call_context_part(context: &mut ToolCallContext<S>) -> Result<Self, crate::Error> {
|
||||||
context: ToolCallContext<'a, S>,
|
|
||||||
) -> Result<(Self, ToolCallContext<'a, S>), crate::Error> {
|
|
||||||
let peer = context.request_context.peer.clone();
|
let peer = context.request_context.peer.clone();
|
||||||
Ok((peer, context))
|
Ok(peer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, S> FromToolCallContextPart<'a, S> for crate::model::Meta {
|
impl<S> FromToolCallContextPart<S> for crate::model::Meta {
|
||||||
fn from_tool_call_context_part(
|
fn from_tool_call_context_part(context: &mut ToolCallContext<S>) -> Result<Self, crate::Error> {
|
||||||
mut context: ToolCallContext<'a, S>,
|
|
||||||
) -> Result<(Self, ToolCallContext<'a, S>), crate::Error> {
|
|
||||||
let mut meta = crate::model::Meta::default();
|
let mut meta = crate::model::Meta::default();
|
||||||
std::mem::swap(&mut meta, &mut context.request_context.meta);
|
std::mem::swap(&mut meta, &mut context.request_context.meta);
|
||||||
Ok((meta, context))
|
Ok(meta)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RequestId(pub crate::model::RequestId);
|
pub struct RequestId(pub crate::model::RequestId);
|
||||||
impl<'a, S> FromToolCallContextPart<'a, S> for RequestId {
|
impl<S> FromToolCallContextPart<S> for RequestId {
|
||||||
fn from_tool_call_context_part(
|
fn from_tool_call_context_part(context: &mut ToolCallContext<S>) -> Result<Self, crate::Error> {
|
||||||
context: ToolCallContext<'a, S>,
|
Ok(RequestId(context.request_context.id.clone()))
|
||||||
) -> Result<(Self, ToolCallContext<'a, S>), crate::Error> {
|
|
||||||
Ok((RequestId(context.request_context.id.clone()), context))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, S> FromToolCallContextPart<'a, S> for RequestContext<RoleServer> {
|
impl<S> FromToolCallContextPart<S> for RequestContext<RoleServer> {
|
||||||
fn from_tool_call_context_part(
|
fn from_tool_call_context_part(context: &mut ToolCallContext<S>) -> Result<Self, crate::Error> {
|
||||||
context: ToolCallContext<'a, S>,
|
Ok(context.request_context.clone())
|
||||||
) -> Result<(Self, ToolCallContext<'a, S>), crate::Error> {
|
|
||||||
Ok((context.request_context.clone(), context))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'s, S> ToolCallContext<'s, S> {
|
impl<'s, S> ToolCallContext<'s, S> {
|
||||||
pub fn invoke<H, A>(self, h: H) -> H::Fut
|
pub fn invoke<H, A>(self, h: H) -> BoxFuture<'s, Result<CallToolResult, crate::Error>>
|
||||||
where
|
where
|
||||||
H: CallToolHandler<'s, S, A>,
|
H: CallToolHandler<S, A>,
|
||||||
{
|
{
|
||||||
h.call(self)
|
h.call(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::type_complexity)]
|
#[allow(clippy::type_complexity)]
|
||||||
pub struct AsyncAdapter<P, Fut, R>(PhantomData<(fn(P) -> Fut, fn(Fut) -> R)>);
|
pub struct AsyncAdapter<P, Fut, R>(PhantomData<fn(P) -> fn(Fut) -> R>);
|
||||||
pub struct SyncAdapter<P, R>(PhantomData<fn(P) -> R>);
|
pub struct SyncAdapter<P, R>(PhantomData<fn(P) -> R>);
|
||||||
|
// #[allow(clippy::type_complexity)]
|
||||||
|
pub struct AsyncMethodAdapter<P, R>(PhantomData<fn(P) -> R>);
|
||||||
|
pub struct SyncMethodAdapter<P, R>(PhantomData<fn(P) -> R>);
|
||||||
|
|
||||||
macro_rules! impl_for {
|
macro_rules! impl_for {
|
||||||
($($T: ident)*) => {
|
($($T: ident)*) => {
|
||||||
|
|
@ -381,174 +311,118 @@ macro_rules! impl_for {
|
||||||
impl_for!([$($Tn)* $Tn_1] [$($Rest)*]);
|
impl_for!([$($Tn)* $Tn_1] [$($Rest)*]);
|
||||||
};
|
};
|
||||||
(@impl $($Tn: ident)*) => {
|
(@impl $($Tn: ident)*) => {
|
||||||
impl<'s, $($Tn,)* S, F, Fut, R> CallToolHandler<'s, S, AsyncAdapter<($($Tn,)*), Fut, R>> for F
|
impl<$($Tn,)* S, F, R> CallToolHandler<S, AsyncMethodAdapter<($($Tn,)*), R>> for F
|
||||||
where
|
where
|
||||||
$(
|
$(
|
||||||
$Tn: FromToolCallContextPart<'s, S> + 's,
|
$Tn: FromToolCallContextPart<S> ,
|
||||||
)*
|
)*
|
||||||
F: FnOnce($($Tn,)*) -> Fut + Send + 's,
|
F: FnOnce(&S, $($Tn,)*) -> BoxFuture<'_, R>,
|
||||||
Fut: Future<Output = R> + Send + 's,
|
|
||||||
R: IntoCallToolResult + Send + 's,
|
// Need RTN support here(I guess), https://github.com/rust-lang/rust/pull/138424
|
||||||
S: Send + Sync,
|
// Fut: Future<Output = R> + Send + 'a,
|
||||||
|
R: IntoCallToolResult + Send + 'static,
|
||||||
|
S: Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
type Fut = IntoCallToolResultFut<Fut, R>;
|
#[allow(unused_variables, non_snake_case, unused_mut)]
|
||||||
#[allow(unused_variables, non_snake_case)]
|
|
||||||
fn call(
|
fn call(
|
||||||
self,
|
self,
|
||||||
context: ToolCallContext<'s, S>,
|
mut context: ToolCallContext<'_, S>,
|
||||||
) -> Self::Fut {
|
) -> BoxFuture<'_, Result<CallToolResult, crate::Error>>{
|
||||||
$(
|
$(
|
||||||
let result = $Tn::from_tool_call_context_part(context);
|
let result = $Tn::from_tool_call_context_part(&mut context);
|
||||||
let ($Tn, context) = match result {
|
let $Tn = match result {
|
||||||
Ok((value, context)) => (value, context),
|
Ok(value) => value,
|
||||||
Err(e) => return IntoCallToolResultFut::Ready {
|
Err(e) => return std::future::ready(Err(e)).boxed(),
|
||||||
result: std::future::ready(Err(e)),
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
)*
|
)*
|
||||||
IntoCallToolResultFut::Pending {
|
let service = context.service;
|
||||||
fut: self($($Tn,)*),
|
let fut = self(service, $($Tn,)*);
|
||||||
_marker: PhantomData
|
async move {
|
||||||
}
|
let result = fut.await;
|
||||||
|
result.into_call_tool_result()
|
||||||
|
}.boxed()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'s, $($Tn,)* S, F, R> CallToolHandler<'s, S, SyncAdapter<($($Tn,)*), R>> for F
|
impl<$($Tn,)* S, F, Fut, R> CallToolHandler<S, AsyncAdapter<($($Tn,)*), Fut, R>> for F
|
||||||
where
|
where
|
||||||
$(
|
$(
|
||||||
$Tn: FromToolCallContextPart<'s, S> + 's,
|
$Tn: FromToolCallContextPart<S> ,
|
||||||
)*
|
)*
|
||||||
F: FnOnce($($Tn,)*) -> R + Send + 's,
|
F: FnOnce($($Tn,)*) -> Fut + Send + ,
|
||||||
R: IntoCallToolResult + Send + 's,
|
Fut: Future<Output = R> + Send + 'static,
|
||||||
|
R: IntoCallToolResult + Send + 'static,
|
||||||
S: Send + Sync,
|
S: Send + Sync,
|
||||||
{
|
{
|
||||||
type Fut = Ready<Result<CallToolResult, crate::Error>>;
|
#[allow(unused_variables, non_snake_case, unused_mut)]
|
||||||
#[allow(unused_variables, non_snake_case)]
|
|
||||||
fn call(
|
fn call(
|
||||||
self,
|
self,
|
||||||
context: ToolCallContext<'s, S>,
|
mut context: ToolCallContext<S>,
|
||||||
) -> Self::Fut {
|
) -> BoxFuture<'static, Result<CallToolResult, crate::Error>>{
|
||||||
$(
|
$(
|
||||||
let result = $Tn::from_tool_call_context_part(context);
|
let result = $Tn::from_tool_call_context_part(&mut context);
|
||||||
let ($Tn, context) = match result {
|
let $Tn = match result {
|
||||||
Ok((value, context)) => (value, context),
|
Ok(value) => value,
|
||||||
Err(e) => return std::future::ready(Err(e)),
|
Err(e) => return std::future::ready(Err(e)).boxed(),
|
||||||
};
|
};
|
||||||
)*
|
)*
|
||||||
std::future::ready(self($($Tn,)*).into_call_tool_result())
|
let fut = self($($Tn,)*);
|
||||||
|
async move {
|
||||||
|
let result = fut.await;
|
||||||
|
result.into_call_tool_result()
|
||||||
|
}.boxed()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<$($Tn,)* S, F, R> CallToolHandler<S, SyncMethodAdapter<($($Tn,)*), R>> for F
|
||||||
|
where
|
||||||
|
$(
|
||||||
|
$Tn: FromToolCallContextPart<S> + ,
|
||||||
|
)*
|
||||||
|
F: FnOnce(&S, $($Tn,)*) -> R + Send + ,
|
||||||
|
R: IntoCallToolResult + Send + ,
|
||||||
|
S: Send + Sync,
|
||||||
|
{
|
||||||
|
#[allow(unused_variables, non_snake_case, unused_mut)]
|
||||||
|
fn call(
|
||||||
|
self,
|
||||||
|
mut context: ToolCallContext<S>,
|
||||||
|
) -> BoxFuture<'static, Result<CallToolResult, crate::Error>> {
|
||||||
|
$(
|
||||||
|
let result = $Tn::from_tool_call_context_part(&mut context);
|
||||||
|
let $Tn = match result {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(e) => return std::future::ready(Err(e)).boxed(),
|
||||||
|
};
|
||||||
|
)*
|
||||||
|
std::future::ready(self(context.service, $($Tn,)*).into_call_tool_result()).boxed()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<$($Tn,)* S, F, R> CallToolHandler<S, SyncAdapter<($($Tn,)*), R>> for F
|
||||||
|
where
|
||||||
|
$(
|
||||||
|
$Tn: FromToolCallContextPart<S> + ,
|
||||||
|
)*
|
||||||
|
F: FnOnce($($Tn,)*) -> R + Send + ,
|
||||||
|
R: IntoCallToolResult + Send + ,
|
||||||
|
S: Send + Sync,
|
||||||
|
{
|
||||||
|
#[allow(unused_variables, non_snake_case, unused_mut)]
|
||||||
|
fn call(
|
||||||
|
self,
|
||||||
|
mut context: ToolCallContext<S>,
|
||||||
|
) -> BoxFuture<'static, Result<CallToolResult, crate::Error>> {
|
||||||
|
$(
|
||||||
|
let result = $Tn::from_tool_call_context_part(&mut context);
|
||||||
|
let $Tn = match result {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(e) => return std::future::ready(Err(e)).boxed(),
|
||||||
|
};
|
||||||
|
)*
|
||||||
|
std::future::ready(self($($Tn,)*).into_call_tool_result()).boxed()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
impl_for!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15);
|
impl_for!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15);
|
||||||
pub struct ToolBoxItem<S> {
|
|
||||||
#[allow(clippy::type_complexity)]
|
|
||||||
pub call: Box<DynCallToolHandler<S>>,
|
|
||||||
pub attr: crate::model::Tool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S: Send + Sync + 'static + Clone> ToolBoxItem<S> {
|
|
||||||
pub fn new<C>(attr: crate::model::Tool, call: C) -> Self
|
|
||||||
where
|
|
||||||
C: Fn(ToolCallContext<'_, S>) -> BoxFuture<'_, Result<CallToolResult, crate::Error>>
|
|
||||||
+ Send
|
|
||||||
+ Sync
|
|
||||||
+ 'static,
|
|
||||||
{
|
|
||||||
Self {
|
|
||||||
call: Box::new(call),
|
|
||||||
attr,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn name(&self) -> &str {
|
|
||||||
&self.attr.name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct ToolBox<S> {
|
|
||||||
#[allow(clippy::type_complexity)]
|
|
||||||
pub map: std::collections::HashMap<Cow<'static, str>, ToolBoxItem<S>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> ToolBox<S> {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
map: std::collections::HashMap::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn add(&mut self, item: ToolBoxItem<S>) {
|
|
||||||
self.map.insert(item.attr.name.clone(), item);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remove<H, A>(&mut self, name: &str) {
|
|
||||||
self.map.remove(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn call(
|
|
||||||
&self,
|
|
||||||
context: ToolCallContext<'_, S>,
|
|
||||||
) -> Result<CallToolResult, crate::Error> {
|
|
||||||
let item = self
|
|
||||||
.map
|
|
||||||
.get(context.name())
|
|
||||||
.ok_or_else(|| crate::Error::invalid_params("tool not found", None))?;
|
|
||||||
(item.call)(context).await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn list(&self) -> Vec<crate::model::Tool> {
|
|
||||||
self.map.values().map(|item| item.attr.clone()).collect()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "macros")]
|
|
||||||
#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! tool_box {
|
|
||||||
(@pin_add $callee: ident, $attr: expr, $f: expr) => {
|
|
||||||
$callee.add(ToolBoxItem::new($attr, |context| Box::pin($f(context))));
|
|
||||||
};
|
|
||||||
($server: ident { $($tool: ident),* $(,)?} ) => {
|
|
||||||
$crate::tool_box!($server { $($tool),* } tool_box);
|
|
||||||
};
|
|
||||||
($server: ident { $($tool: ident),* $(,)?} $tool_box: ident) => {
|
|
||||||
fn $tool_box() -> &'static $crate::handler::server::tool::ToolBox<$server> {
|
|
||||||
use $crate::handler::server::tool::{ToolBox, ToolBoxItem};
|
|
||||||
static TOOL_BOX: std::sync::OnceLock<ToolBox<$server>> = std::sync::OnceLock::new();
|
|
||||||
TOOL_BOX.get_or_init(|| {
|
|
||||||
let mut tool_box = ToolBox::new();
|
|
||||||
$crate::paste!{
|
|
||||||
$(
|
|
||||||
$crate::tool_box!(@pin_add tool_box, $server::[< $tool _tool_attr>](), $server::[<$tool _tool_call>]);
|
|
||||||
)*
|
|
||||||
}
|
|
||||||
tool_box
|
|
||||||
})
|
|
||||||
}
|
|
||||||
};
|
|
||||||
(@derive) => {
|
|
||||||
$crate::tool_box!(@derive tool_box);
|
|
||||||
};
|
|
||||||
|
|
||||||
(@derive $tool_box:ident) => {
|
|
||||||
async fn list_tools(
|
|
||||||
&self,
|
|
||||||
_: Option<$crate::model::PaginatedRequestParam>,
|
|
||||||
_: $crate::service::RequestContext<$crate::service::RoleServer>,
|
|
||||||
) -> Result<$crate::model::ListToolsResult, $crate::Error> {
|
|
||||||
Ok($crate::model::ListToolsResult {
|
|
||||||
next_cursor: None,
|
|
||||||
tools: Self::tool_box().list(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn call_tool(
|
|
||||||
&self,
|
|
||||||
call_tool_request_param: $crate::model::CallToolRequestParam,
|
|
||||||
context: $crate::service::RequestContext<$crate::service::RoleServer>,
|
|
||||||
) -> Result<$crate::model::CallToolResult, $crate::Error> {
|
|
||||||
let context = $crate::handler::server::tool::ToolCallContext::new(self, call_tool_request_param, context);
|
|
||||||
Self::$tool_box().call(context).await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -15,23 +15,25 @@
|
||||||
//! as Claude Desktop or the Cursor IDE.
|
//! as Claude Desktop or the Cursor IDE.
|
||||||
//!
|
//!
|
||||||
//! For example, to implement a server that has a tool that can count, you would
|
//! For example, to implement a server that has a tool that can count, you would
|
||||||
//! make an object for that tool and add an implementation with the `#[tool(tool_box)]` macro:
|
//! make an object for that tool and add an implementation with the `#[tool_router]` macro:
|
||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use std::sync::Arc;
|
//! use std::sync::Arc;
|
||||||
//! use rmcp::{Error as McpError, model::*, tool};
|
//! use rmcp::{Error as McpError, model::*, tool, tool_router, handler::server::tool::ToolRouter};
|
||||||
//! use tokio::sync::Mutex;
|
//! use tokio::sync::Mutex;
|
||||||
//!
|
//!
|
||||||
//! #[derive(Clone)]
|
//! #[derive(Clone)]
|
||||||
//! pub struct Counter {
|
//! pub struct Counter {
|
||||||
//! counter: Arc<Mutex<i32>>,
|
//! counter: Arc<Mutex<i32>>,
|
||||||
|
//! tool_router: ToolRouter<Self>,
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! #[tool(tool_box)]
|
//! #[tool_router]
|
||||||
//! impl Counter {
|
//! impl Counter {
|
||||||
//! fn new() -> Self {
|
//! fn new() -> Self {
|
||||||
//! Self {
|
//! Self {
|
||||||
//! counter: Arc::new(Mutex::new(0)),
|
//! counter: Arc::new(Mutex::new(0)),
|
||||||
|
//! tool_router: Self::tool_router(),
|
||||||
//! }
|
//! }
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
|
|
@ -120,7 +122,7 @@ pub mod transport;
|
||||||
pub use paste::paste;
|
pub use paste::paste;
|
||||||
#[cfg(all(feature = "macros", feature = "server"))]
|
#[cfg(all(feature = "macros", feature = "server"))]
|
||||||
#[cfg_attr(docsrs, doc(cfg(all(feature = "macros", feature = "server"))))]
|
#[cfg_attr(docsrs, doc(cfg(all(feature = "macros", feature = "server"))))]
|
||||||
pub use rmcp_macros::tool;
|
pub use rmcp_macros::*;
|
||||||
#[cfg(all(feature = "macros", feature = "server"))]
|
#[cfg(all(feature = "macros", feature = "server"))]
|
||||||
#[cfg_attr(docsrs, doc(cfg(all(feature = "macros", feature = "server"))))]
|
#[cfg_attr(docsrs, doc(cfg(all(feature = "macros", feature = "server"))))]
|
||||||
pub use schemars;
|
pub use schemars;
|
||||||
|
|
|
||||||
|
|
@ -679,6 +679,17 @@ macro_rules! paginated_result {
|
||||||
pub next_cursor: Option<Cursor>,
|
pub next_cursor: Option<Cursor>,
|
||||||
pub $i_item: $t_item,
|
pub $i_item: $t_item,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl $t {
|
||||||
|
pub fn with_all_items(
|
||||||
|
items: $t_item,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
next_cursor: None,
|
||||||
|
$i_item: items,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
|
#![allow(dead_code)]
|
||||||
use rmcp::{
|
use rmcp::{
|
||||||
ServerHandler,
|
ServerHandler,
|
||||||
|
handler::server::{router::tool::ToolRouter, tool::Parameters},
|
||||||
model::{ServerCapabilities, ServerInfo},
|
model::{ServerCapabilities, ServerInfo},
|
||||||
schemars, tool,
|
schemars, tool, tool_router,
|
||||||
};
|
};
|
||||||
#[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
|
#[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
|
||||||
pub struct SumRequest {
|
pub struct SumRequest {
|
||||||
|
|
@ -9,30 +11,46 @@ pub struct SumRequest {
|
||||||
pub a: i32,
|
pub a: i32,
|
||||||
pub b: i32,
|
pub b: i32,
|
||||||
}
|
}
|
||||||
#[derive(Debug, Clone, Default)]
|
|
||||||
pub struct Calculator;
|
#[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
|
||||||
#[tool(tool_box)]
|
pub struct SubRequest {
|
||||||
|
#[schemars(description = "the left hand side number")]
|
||||||
|
pub a: i32,
|
||||||
|
#[schemars(description = "the right hand side number")]
|
||||||
|
pub b: i32,
|
||||||
|
}
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Calculator {
|
||||||
|
tool_router: ToolRouter<Self>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Calculator {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
tool_router: Self::tool_router(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Calculator {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tool_router]
|
||||||
impl Calculator {
|
impl Calculator {
|
||||||
#[tool(description = "Calculate the sum of two numbers")]
|
#[tool(description = "Calculate the sum of two numbers")]
|
||||||
fn sum(&self, #[tool(aggr)] SumRequest { a, b }: SumRequest) -> String {
|
fn sum(&self, Parameters(SumRequest { a, b }): Parameters<SumRequest>) -> String {
|
||||||
(a + b).to_string()
|
(a + b).to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tool(description = "Calculate the sub of two numbers")]
|
#[tool(description = "Calculate the sub of two numbers")]
|
||||||
fn sub(
|
fn sub(&self, Parameters(SubRequest { a, b }): Parameters<SubRequest>) -> String {
|
||||||
&self,
|
|
||||||
#[tool(param)]
|
|
||||||
#[schemars(description = "the left hand side number")]
|
|
||||||
a: i32,
|
|
||||||
#[tool(param)]
|
|
||||||
#[schemars(description = "the right hand side number")]
|
|
||||||
b: i32,
|
|
||||||
) -> String {
|
|
||||||
(a - b).to_string()
|
(a - b).to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tool(tool_box)]
|
|
||||||
impl ServerHandler for Calculator {
|
impl ServerHandler for Calculator {
|
||||||
fn get_info(&self) -> ServerInfo {
|
fn get_info(&self) -> ServerInfo {
|
||||||
ServerInfo {
|
ServerInfo {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
use rmcp::{Error as McpError, model::*, schemars, tool};
|
use rmcp::{
|
||||||
|
Error as McpError, handler::server::tool::Parameters, model::*, schemars, tool, tool_router,
|
||||||
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
|
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
|
||||||
|
|
@ -24,7 +26,7 @@ pub struct ChatRequest {
|
||||||
#[derive(Clone, Default)]
|
#[derive(Clone, Default)]
|
||||||
pub struct Demo;
|
pub struct Demo;
|
||||||
|
|
||||||
#[tool(tool_box)]
|
#[tool_router]
|
||||||
impl Demo {
|
impl Demo {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self
|
Self
|
||||||
|
|
@ -33,9 +35,9 @@ impl Demo {
|
||||||
#[tool(description = "LLM")]
|
#[tool(description = "LLM")]
|
||||||
async fn chat(
|
async fn chat(
|
||||||
&self,
|
&self,
|
||||||
#[tool(aggr)] chat_request: ChatRequest,
|
chat_request: Parameters<ChatRequest>,
|
||||||
) -> Result<CallToolResult, McpError> {
|
) -> Result<CallToolResult, McpError> {
|
||||||
let content = Content::json(chat_request)?;
|
let content = Content::json(chat_request.0)?;
|
||||||
Ok(CallToolResult::success(vec![content]))
|
Ok(CallToolResult::success(vec![content]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use rmcp::{ServerHandler, tool};
|
use rmcp::{ServerHandler, handler::server::router::tool::ToolRouter, tool, tool_handler};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct AnnotatedServer {}
|
pub struct AnnotatedServer {
|
||||||
|
tool_router: ToolRouter<AnnotatedServer>,
|
||||||
|
}
|
||||||
|
|
||||||
impl AnnotatedServer {
|
impl AnnotatedServer {
|
||||||
// Tool with inline comments for documentation
|
// Tool with inline comments for documentation
|
||||||
|
|
@ -11,29 +13,14 @@ mod tests {
|
||||||
/// This is used to test tool annotations
|
/// This is used to test tool annotations
|
||||||
#[tool(
|
#[tool(
|
||||||
name = "direct-annotated-tool",
|
name = "direct-annotated-tool",
|
||||||
annotations = {
|
annotations(title = "Annotated Tool", read_only_hint = true)
|
||||||
title: "Annotated Tool",
|
|
||||||
readOnlyHint: true
|
|
||||||
}
|
|
||||||
)]
|
)]
|
||||||
pub async fn direct_annotated_tool(&self, #[tool(param)] input: String) -> String {
|
pub async fn direct_annotated_tool(&self, input: String) -> String {
|
||||||
format!("Direct: {}", input)
|
format!("Direct: {}", input)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#[tool_handler]
|
||||||
impl ServerHandler for AnnotatedServer {
|
impl ServerHandler for AnnotatedServer {}
|
||||||
async fn call_tool(
|
|
||||||
&self,
|
|
||||||
request: rmcp::model::CallToolRequestParam,
|
|
||||||
context: rmcp::service::RequestContext<rmcp::RoleServer>,
|
|
||||||
) -> Result<rmcp::model::CallToolResult, rmcp::Error> {
|
|
||||||
let tcc = rmcp::handler::server::tool::ToolCallContext::new(self, request, context);
|
|
||||||
match tcc.name() {
|
|
||||||
"direct-annotated-tool" => Self::direct_annotated_tool_tool_call(tcc).await,
|
|
||||||
_ => Err(rmcp::Error::invalid_params("method not found", None)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_direct_tool_attributes() {
|
fn test_direct_tool_attributes() {
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
//cargo test --test test_tool_macros --features "client server"
|
//cargo test --test test_tool_macros --features "client server"
|
||||||
|
#![allow(dead_code)]
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use rmcp::{
|
use rmcp::{
|
||||||
ClientHandler, ServerHandler, ServiceExt,
|
ClientHandler, ServerHandler, ServiceExt,
|
||||||
handler::server::tool::ToolCallContext,
|
handler::server::{router::tool::ToolRouter, tool::Parameters},
|
||||||
model::{CallToolRequestParam, ClientInfo},
|
model::{CallToolRequestParam, ClientInfo},
|
||||||
tool,
|
tool, tool_handler, tool_router,
|
||||||
};
|
};
|
||||||
use schemars::JsonSchema;
|
use schemars::JsonSchema;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
@ -17,37 +17,38 @@ pub struct GetWeatherRequest {
|
||||||
pub date: String,
|
pub date: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServerHandler for Server {
|
#[tool_handler(router = self.tool_router)]
|
||||||
async fn call_tool(
|
impl ServerHandler for Server {}
|
||||||
&self,
|
|
||||||
request: rmcp::model::CallToolRequestParam,
|
#[derive(Debug, Clone)]
|
||||||
context: rmcp::service::RequestContext<rmcp::RoleServer>,
|
#[allow(dead_code)]
|
||||||
) -> Result<rmcp::model::CallToolResult, rmcp::Error> {
|
pub struct Server {
|
||||||
let tcc = ToolCallContext::new(self, request, context);
|
tool_router: ToolRouter<Self>,
|
||||||
match tcc.name() {
|
}
|
||||||
"get-weather" => Self::get_weather_tool_call(tcc).await,
|
|
||||||
_ => Err(rmcp::Error::invalid_params("method not found", None)),
|
impl Default for Server {
|
||||||
}
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[tool_router(router = tool_router)]
|
||||||
pub struct Server {}
|
|
||||||
|
|
||||||
impl Server {
|
impl Server {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
tool_router: Self::tool_router(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// This tool is used to get the weather of a city.
|
/// This tool is used to get the weather of a city.
|
||||||
#[tool(name = "get-weather", description = "Get the weather of a city.", vis = )]
|
#[tool(name = "get-weather", description = "Get the weather of a city.")]
|
||||||
pub async fn get_weather(&self, #[tool(param)] city: String) -> String {
|
pub async fn get_weather(&self, city: Parameters<GetWeatherRequest>) -> String {
|
||||||
drop(city);
|
drop(city);
|
||||||
"rain".to_string()
|
"rain".to_string()
|
||||||
}
|
}
|
||||||
#[tool(description = "Empty Parameter")]
|
|
||||||
async fn empty_param(&self) {}
|
|
||||||
|
|
||||||
#[tool(description = "Optional Parameter")]
|
#[tool]
|
||||||
async fn optional_param(&self, #[tool(param)] city: Option<String>) -> String {
|
async fn empty_param(&self) {}
|
||||||
city.unwrap_or_default()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// define generic service trait
|
// define generic service trait
|
||||||
|
|
@ -68,13 +69,15 @@ impl DataService for MockDataService {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct GenericServer<DS: DataService> {
|
pub struct GenericServer<DS: DataService> {
|
||||||
data_service: Arc<DS>,
|
data_service: Arc<DS>,
|
||||||
|
tool_router: ToolRouter<Self>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tool(tool_box)]
|
#[tool_router]
|
||||||
impl<DS: DataService> GenericServer<DS> {
|
impl<DS: DataService> GenericServer<DS> {
|
||||||
pub fn new(data_service: DS) -> Self {
|
pub fn new(data_service: DS) -> Self {
|
||||||
Self {
|
Self {
|
||||||
data_service: Arc::new(data_service),
|
data_service: Arc::new(data_service),
|
||||||
|
tool_router: Self::tool_router(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -83,16 +86,22 @@ impl<DS: DataService> GenericServer<DS> {
|
||||||
self.data_service.get_data()
|
self.data_service.get_data()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[tool(tool_box)]
|
|
||||||
|
#[tool_handler]
|
||||||
impl<DS: DataService> ServerHandler for GenericServer<DS> {}
|
impl<DS: DataService> ServerHandler for GenericServer<DS> {}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_tool_macros() {
|
async fn test_tool_macros() {
|
||||||
let server = Server::default();
|
let server = Server::new();
|
||||||
let _attr = Server::get_weather_tool_attr();
|
let _attr = Server::get_weather_tool_attr();
|
||||||
let _get_weather_call_fn = Server::get_weather_tool_call;
|
let _get_weather_tool_attr_fn = Server::get_weather_tool_attr;
|
||||||
let _get_weather_fn = Server::get_weather;
|
let _get_weather_fn = Server::get_weather;
|
||||||
server.get_weather("harbin".into()).await;
|
server
|
||||||
|
.get_weather(Parameters(GetWeatherRequest {
|
||||||
|
city: "Harbin".into(),
|
||||||
|
date: "Yesterday".into(),
|
||||||
|
}))
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|
@ -108,14 +117,14 @@ async fn test_tool_macros_with_generics() {
|
||||||
let mock_service = MockDataService;
|
let mock_service = MockDataService;
|
||||||
let server = GenericServer::new(mock_service);
|
let server = GenericServer::new(mock_service);
|
||||||
let _attr = GenericServer::<MockDataService>::get_data_tool_attr();
|
let _attr = GenericServer::<MockDataService>::get_data_tool_attr();
|
||||||
let _get_data_call_fn = GenericServer::<MockDataService>::get_data_tool_call;
|
let _get_data_call_fn = GenericServer::<MockDataService>::get_data;
|
||||||
let _get_data_fn = GenericServer::<MockDataService>::get_data;
|
let _get_data_fn = GenericServer::<MockDataService>::get_data;
|
||||||
assert_eq!(server.get_data().await, "mock data");
|
assert_eq!(server.get_data().await, "mock data");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_tool_macros_with_optional_param() {
|
async fn test_tool_macros_with_optional_param() {
|
||||||
let _attr = Server::optional_param_tool_attr();
|
let _attr = Server::get_weather_tool_attr();
|
||||||
// println!("{_attr:?}");
|
// println!("{_attr:?}");
|
||||||
let attr_type = _attr
|
let attr_type = _attr
|
||||||
.input_schema
|
.input_schema
|
||||||
|
|
@ -147,49 +156,56 @@ pub struct OptionalI64TestSchema {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dummy struct to host the test tool method
|
// Dummy struct to host the test tool method
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct OptionalSchemaTester {}
|
pub struct OptionalSchemaTester {
|
||||||
|
tool_router: ToolRouter<Self>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for OptionalSchemaTester {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OptionalSchemaTester {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
tool_router: Self::tool_router(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tool_router]
|
||||||
impl OptionalSchemaTester {
|
impl OptionalSchemaTester {
|
||||||
// Dummy tool function using the test schema as an aggregated parameter
|
// Dummy tool function using the test schema as an aggregated parameter
|
||||||
#[tool(description = "A tool to test optional schema generation")]
|
#[tool(description = "A tool to test optional schema generation")]
|
||||||
async fn test_optional_aggr(&self, #[tool(aggr)] _req: OptionalFieldTestSchema) {
|
async fn test_optional(&self, _req: Parameters<OptionalFieldTestSchema>) {
|
||||||
// Implementation doesn't matter for schema testing
|
// Implementation doesn't matter for schema testing
|
||||||
// Return type changed to () to satisfy IntoCallToolResult
|
// Return type changed to () to satisfy IntoCallToolResult
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tool function to test optional i64 handling
|
// Tool function to test optional i64 handling
|
||||||
#[tool(description = "A tool to test optional i64 schema generation")]
|
#[tool(description = "A tool to test optional i64 schema generation")]
|
||||||
async fn test_optional_i64_aggr(&self, #[tool(aggr)] req: OptionalI64TestSchema) -> String {
|
async fn test_optional_i64(
|
||||||
|
&self,
|
||||||
|
Parameters(req): Parameters<OptionalI64TestSchema>,
|
||||||
|
) -> String {
|
||||||
match req.count {
|
match req.count {
|
||||||
Some(c) => format!("Received count: {}", c),
|
Some(c) => format!("Received count: {}", c),
|
||||||
None => "Received null count".to_string(),
|
None => "Received null count".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#[tool_handler]
|
||||||
// Implement ServerHandler to route tool calls for OptionalSchemaTester
|
// Implement ServerHandler to route tool calls for OptionalSchemaTester
|
||||||
impl ServerHandler for OptionalSchemaTester {
|
impl ServerHandler for OptionalSchemaTester {}
|
||||||
async fn call_tool(
|
|
||||||
&self,
|
|
||||||
request: rmcp::model::CallToolRequestParam,
|
|
||||||
context: rmcp::service::RequestContext<rmcp::RoleServer>,
|
|
||||||
) -> Result<rmcp::model::CallToolResult, rmcp::Error> {
|
|
||||||
let tcc = ToolCallContext::new(self, request, context);
|
|
||||||
match tcc.name() {
|
|
||||||
"test_optional_aggr" => Self::test_optional_aggr_tool_call(tcc).await,
|
|
||||||
"test_optional_i64_aggr" => Self::test_optional_i64_aggr_tool_call(tcc).await,
|
|
||||||
_ => Err(rmcp::Error::invalid_params("method not found", None)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_optional_field_schema_generation_via_macro() {
|
fn test_optional_field_schema_generation_via_macro() {
|
||||||
// tests https://github.com/modelcontextprotocol/rust-sdk/issues/135
|
// tests https://github.com/modelcontextprotocol/rust-sdk/issues/135
|
||||||
|
|
||||||
// Get the attributes generated by the #[tool] macro helper
|
// Get the attributes generated by the #[tool] macro helper
|
||||||
let tool_attr = OptionalSchemaTester::test_optional_aggr_tool_attr();
|
let tool_attr = OptionalSchemaTester::test_optional_tool_attr();
|
||||||
|
|
||||||
// Print the actual generated schema for debugging
|
// Print the actual generated schema for debugging
|
||||||
println!(
|
println!(
|
||||||
|
|
@ -257,7 +273,7 @@ async fn test_optional_i64_field_with_null_input() -> anyhow::Result<()> {
|
||||||
let (server_transport, client_transport) = tokio::io::duplex(4096);
|
let (server_transport, client_transport) = tokio::io::duplex(4096);
|
||||||
|
|
||||||
// Server setup
|
// Server setup
|
||||||
let server = OptionalSchemaTester::default();
|
let server = OptionalSchemaTester::new();
|
||||||
let server_handle = tokio::spawn(async move {
|
let server_handle = tokio::spawn(async move {
|
||||||
server.serve(server_transport).await?.waiting().await?;
|
server.serve(server_transport).await?.waiting().await?;
|
||||||
anyhow::Ok(())
|
anyhow::Ok(())
|
||||||
|
|
@ -270,7 +286,7 @@ async fn test_optional_i64_field_with_null_input() -> anyhow::Result<()> {
|
||||||
// Test null case
|
// Test null case
|
||||||
let result = client
|
let result = client
|
||||||
.call_tool(CallToolRequestParam {
|
.call_tool(CallToolRequestParam {
|
||||||
name: "test_optional_i64_aggr".into(),
|
name: "test_optional_i64".into(),
|
||||||
arguments: Some(
|
arguments: Some(
|
||||||
serde_json::json!({
|
serde_json::json!({
|
||||||
"count": null,
|
"count": null,
|
||||||
|
|
@ -298,7 +314,7 @@ async fn test_optional_i64_field_with_null_input() -> anyhow::Result<()> {
|
||||||
// Test Some case
|
// Test Some case
|
||||||
let some_result = client
|
let some_result = client
|
||||||
.call_tool(CallToolRequestParam {
|
.call_tool(CallToolRequestParam {
|
||||||
name: "test_optional_i64_aggr".into(),
|
name: "test_optional_i64".into(),
|
||||||
arguments: Some(
|
arguments: Some(
|
||||||
serde_json::json!({
|
serde_json::json!({
|
||||||
"count": 42,
|
"count": 42,
|
||||||
|
|
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue