Skip to content

Commit 4cca605

Browse files
committed
refactor(sacp): add tool_fn!() macro for MCP tool registration
Adds a convenience macro that expands to the to_future_hack closure `|t, args, cx| Box::pin(t(args, cx))`. This is needed until return-type-notation is stabilized in Rust. Updated all usages in docs and tests to use the new macro.
1 parent a35d1ee commit 4cca605

5 files changed

Lines changed: 19 additions & 6 deletions

File tree

src/sacp-conductor/tests/mcp_integration/proxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl Component for ProxyComponent {
3434
result: format!("Echo: {}", params.message),
3535
})
3636
},
37-
|f, args, cx| Box::pin(f(args, cx)),
37+
sacp::tool_fn!(),
3838
)
3939
.build();
4040

src/sacp-conductor/tests/mcp_server_handler_chain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl Component for ProxyWithMcpAndHandler {
8282
result: format!("Echo: {}", params.message),
8383
})
8484
},
85-
|f, args, cx| Box::pin(f(args, cx)),
85+
sacp::tool_fn!(),
8686
)
8787
.build();
8888

src/sacp-conductor/tests/test_session_id_in_mcp_tools.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn create_echo_proxy() -> Result<sacp::DynComponent, sacp::Error> {
4040
acp_url: context.acp_url(),
4141
})
4242
},
43-
|f, args, cx| Box::pin(f(args, cx)),
43+
sacp::tool_fn!(),
4444
)
4545
.build();
4646

src/sacp/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,16 @@ pub use sacp_derive::{JrNotification, JrRequest, JrResponsePayload};
228228

229229
mod session;
230230
pub use session::*;
231+
232+
/// This macro is used for the value of the `to_future_hack` parameter of [`mcp_server::McpServerBuilder::tool_fn`].
233+
///
234+
/// It expands to `|t, args, cx| Box::pin(t(args, cx))`.
235+
///
236+
/// This is needed until [return-type-notation](https://github.com/rust-lang/rust/issues/109417)
237+
/// is stabilized.
238+
#[macro_export]
239+
macro_rules! tool_fn {
240+
() => {
241+
|t, args, cx| Box::pin(t(args, cx))
242+
};
243+
}

src/sacp/src/mcp_server/builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::{
3434
/// "greet",
3535
/// "Greet someone by name",
3636
/// async |input: GreetInput, _cx| Ok(format!("Hello, {}!", input.name)),
37-
/// |t, args, cx| Box::pin(t(args, cx)),
37+
/// sacp::tool_fn!(),
3838
/// )
3939
/// .build();
4040
/// ```
@@ -87,7 +87,7 @@ where
8787
/// * `description`: The description of the tool.
8888
/// * `func`: The function that implements the tool. Use an async closure like `async |args, cx| { .. }`.
8989
/// * `to_future_hack`: A function that converts the tool function into a future.
90-
/// You should always write `|t, args, cx| Box::pin(t(args, cx))`.
90+
/// You should always use the [`sacp::tool_fn!()`](crate::tool_fn) macro here.
9191
/// This is needed to sidestep current Rust language limitations.
9292
///
9393
/// # Examples
@@ -98,7 +98,7 @@ where
9898
/// "greet",
9999
/// "Greet someone by name",
100100
/// async |input: GreetInput, _cx| Ok(format!("Hello, {}!", input.name)),
101-
/// |t, args, cx| Box::pin(t(args, cx)),
101+
/// sacp::tool_fn!(),
102102
/// )
103103
/// ```
104104
pub fn tool_fn<P, R, F, H>(

0 commit comments

Comments
 (0)