#[tool] async method fails to compile (FnOnce not general enough) when its call tree uses |x: &T| async move {} closures (e.g. stream::iter(..).map(|x| async move{..}))
维护者通常 3 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 src/main.rs 中的最小复现开始,并运行 cargo check 以确认 #[tool] 错误。然后检查 issue 中描述的生成 dispatch 行为,以及工具宏如何施加其高阶 bound。完成的标准是:async stream 模式能够通过 #[tool] 编译,或者在用户会遇到该限制的地方对其进行文档说明。
由索引模型根据 Issue 内容生成。
描述
Summary
An async #[tool] method fails to compile with a higher-ranked-trait-bound error if the transitive call tree of the tool method contains a closure |x: &T| async move { .. } — most commonly futures::stream::iter(coll.iter()).map(|x| async move { .. }).buffer_unordered(..). The same code compiles fine outside the #[tool] macro.
Environment
- rmcp 3.0.1 (crates.io)
- rustc 1.94.0 stable
- macOS (reproduced locally)
Minimal reproduction
Cargo.toml
[package]
name = "rmcp-async-repro"
version = "0.0.0"
edition = "2021"
[dependencies]
rmcp = { version = "3", features = ["server", "macros"] }
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
futures = "0.3"
src/main.rs
use rmcp::{
handler::server::{router::tool::ToolRouter, wrapper::Parameters},
model::{CallToolResult, ServerCapabilities, ServerInfo},
schemars, tool, tool_handler, tool_router, ServerHandler,
};
use serde_json::Value;
#[derive(serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
struct Req {
msg: String,
}
#[derive(Clone, Default)]
struct S {
tool_router: ToolRouter<S>,
}
impl S {
async fn execute(&self, _name: &str, _args: &Value) -> CallToolResult {
use futures::stream::{self, StreamExt};
let ips = vec!["1.1.1.1".to_string()];
let _out: Vec<String> = stream::iter(ips.iter())
.map(|ip| async move { ip.to_string() }) // <-- trigger
.buffer_unordered(4)
.collect()
.await;
CallToolResult::default()
}
}
#[tool_router]
impl S {
#[tool(name = "echo_tool", description = "echo")]
async fn echo(&self, Parameters(req): Parameters<Req>) -> CallToolResult {
let args = serde_json::to_value(&req).unwrap_or_default();
self.execute("echo", &args).await
}
}
#[tool_handler]
impl ServerHandler for S {
fn get_info(&self) -> ServerInfo {
let mut info = ServerInfo::default();
info.capabilities = ServerCapabilities::builder().enable_tools().build();
info
}
}
fn main() {}
Error
error: implementation of `FnOnce` is not general enough
--> src/main.rs (originates in the `#[tool]` attribute macro)
= note: closure with signature `fn(&'0 String) -> {async block@src/main.rs:26:23: 26:33}`
must implement `FnOnce<(&'1 String,)>`, for any two lifetimes `'0` and `'1`...
= note: ...but it actually implements `FnOnce<(&String,)>`
Analysis
The #[tool] macro generates dispatch code carrying a higher-ranked trait bound. A |x: &T| async move { .. } closure (e.g. produced by futures::stream::iter(..).map(..)) cannot satisfy a for<'a> Fn(&'a T) -> Future<..>-style bound — the long-standing rustc "async closure + HRTB" limitation. The pattern compiles in ordinary code because it is never placed under such a bound; the macro forces it into one.
Removing the stream::iter(..).map(|x| async move{..}) from the tool's call tree (or replacing it with an async fn helper + .then(..), or owning the items before the stream) makes the #[tool] macro compile. Implementing ServerHandler manually (without #[tool]) also avoids it.
Workaround
- Refactor
|x: &T| async move { .. }in the tool's call tree (use anasync fnhelper +.then(..), or own items before the stream), or - Implement
ServerHandlermanually instead of using#[tool].
Ask
If #[tool] could avoid forcing the user's call tree into an HRTB that async-move closures can't satisfy (e.g. boxing the future / restructuring the generated handler), that would unblock a fairly common futures::stream pattern. At minimum, documenting the limitation would save significant diagnosis time — the error is very hard to map back to the trigger.
- 主要语言
- Rust
- 星标
- 4k
- 派生
- 662
- 平均合并
- 3 天 23 小时
- 30 天内合并 PR
- 34
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
modelcontextprotocol/rust-sdk 的其他 Issue
-
Streamable HTTP client: a 401 or 403 with a JSON-RPC error body and no WWW-Authenticate loses its HTTP status可能已有人在做 @aton-of-data 于 4 天前认领。 未关闭bug P2 ready for work T-security T-transport
难度 2/5 1-3 小时 新手友好度 68/100
modelcontextprotocol/rust-sdk#1339 ·
维护者通常 3 天内回复
-
auth: refresh_token() requests offline_access that was never granted, so refreshes fail with invalid_scope可能已有人在做 @jstar0 于 6 天前认领。 未关闭bug P1 ready for work T-security T-transport
难度 2/5 1-3 小时 新手友好度 65/100
modelcontextprotocol/rust-sdk#1330 ·
维护者通常 3 天内回复
-
P3 question Stale T-documentation T-enhancement
难度 1/5 1 小时以内 新手友好度 86/100
modelcontextprotocol/rust-sdk#1155 · 1 条评论 ·
维护者通常 3 天内回复
-
Two rmcp tests fail on Windows: connection-refused text check and `npm` spawn in test_with_js可能已有人在做 @bolinfest 今天认领。 未关闭bug P2 ready for work T-test
modelcontextprotocol/rust-sdk#1364 · 1 条评论 · 已指派 1 人 ·
维护者通常 3 天内回复
-
Client never answers a server `ping` sent during the `initialize` handshake可能已有人在做 @bolinfest 今天认领。 未关闭bug P1 ready for work T-service
modelcontextprotocol/rust-sdk#1362 · 3 条评论 · 已指派 1 人 ·
维护者通常 3 天内回复
查看 modelcontextprotocol/rust-sdk 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 66/100
ActivityWatch/aw-notify-rs#47 · 1 条评论 ·
-
`inverse()` of a controlled `CUGate` drops the phase parameter γ可能已有人在做 @rupasri118 今天认领。 未关闭bug mod: circuit
难度 2/5 1-3 小时 新手友好度 66/100
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 84/100
维护者通常 2 天内回复
-
agent:triaged bug bughunt pm:bundler priority:p1
难度 2/5 1-3 小时 新手友好度 70/100
SocketDev/socket-patch#1573 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
bytecodealliance/rustix#1706 · 1 条评论 ·
维护者通常 11 天内回复