| Crates.io | fastmcp-rs |
| lib.rs | fastmcp-rs |
| version | 0.2.0 |
| created_at | 2025-10-13 17:29:17.842381+00 |
| updated_at | 2025-10-13 17:29:17.842381+00 |
| description | Rust prototype for the FastMCP server |
| homepage | https://gitee.com/luzhihaoTestingLab/fastmcp-rust |
| repository | https://gitee.com/luzhihaoTestingLab/fastmcp-rust.git |
| max_upload_size | |
| id | 1880857 |
| size | 167,759 |
This crate hosts an experimental Rust reimplementation of the FastMCP server. The goal is to provide feature parity with the core server ergonomics-tools, resources, prompts, and transports-using idiomatic Rust.
src/
lib.rs # re-exports and FastMcpServer entry point
command.rs # shared command envelope + dispatch logic
server.rs # FastMcpServer, configuration, metadata
tool.rs # ToolDefinition, ToolRegistry, ToolInvocation
resource.rs # ResourceDefinition, ResourceManager
prompt.rs # PromptDefinition, PromptManager, templating
error.rs # FastMcpError hierarchy using thiserror
stdio.rs # STDIO transport runtime
http/
mod.rs # HTTP/SSE/streamable HTTP server builder and handlers
cargo run --example simple_add
cargo run --example fetch_url
simple_add exposes an add tool over STDIO and demonstrates a basic prompt.fetch_url spins up the HTTP stack and streams responses from a fetch_url tool.This crate now provides macros to reduce boilerplate when defining and registering tools, resources, and prompts. Import them via use fastmcp_rs::macros::*;.
Example — define and register a tool (JSON schema via parameters_json):
use fastmcp_rs::{FastMcpServer, macros::*, tool::ToolResponse};
use serde_json::{Value, json};
let server = FastMcpServer::builder().name("Demo").build().into_shared();
mcp_register_tools!(server, [
mcp_tool! {
name: "echo",
handler: |_, payload: Value| async move {
Ok(mcp_response!([ mcp_text!(payload.to_string()) ]))
},
description: "Echoes the payload",
parameters_json: { "type": "object" },
annotations: { "category": json!("demo") },
}
]);
Attribute-style tool definition:
use serde_json::{json, Value};
use fastmcp_rs::{FastMcpServer, mcp_register_tools, mcp_response, mcp_text, tool::ToolResponse};
// Path-qualified attribute avoids name clash with `mcp_tool!`
#[fastmcp_rs_macros::mcp_tool(
name = "add",
description = "Adds two numbers",
parameters_json(
"type": "object",
"required": ["a", "b"],
"properties": { "a": { "type": "number" }, "b": { "type": "number" } }
),
annotations(category = json!("math"))
)]
async fn add(_: fastmcp_rs::InvocationContext, payload: Value) -> fastmcp_rs::Result<ToolResponse> {
let a = payload.get("a").and_then(Value::as_f64).unwrap_or(0.0);
let b = payload.get("b").and_then(Value::as_f64).unwrap_or(0.0);
Ok(mcp_response!([ mcp_text!(format!("{} + {} = {}", a, b, a + b)) ]))
}
let server = FastMcpServer::builder().name("AttrDemo").build().into_shared();
// Register via generated factory function
mcp_register_tools!(server, [ add_tool() ]);
// Or call the generated helper directly: add_register(&server)?;
Supported attribute keys: name, description, summary, parameters (expr of type serde_json::Value), parameters_json(...), and annotations(key = expr, ...). Two helpers are generated: <fn>_tool() and <fn>_register(server).
Example — single-message text prompt:
mcp_register_prompts!(server, [
mcp_prompt_text! {
name: "welcome",
role: "system",
text: "Hello {{ user }}",
description: "Greets a user",
parameters: json!({
"type": "object",
"required": ["user"],
"properties": { "user": { "type": "string" } }
}),
}
]);
Resources (static and dynamic):
use fastmcp_rs::resource::ResourceContent;
mcp_register_resources!(server, [
mcp_resource_static! {
uri: "resource://hello",
content: ResourceContent::text("hello world"),
description: "Simple text resource",
annotations: { "kind": json!("demo") },
}
,
mcp_resource_dynamic! {
uri: "resource://time",
resolver: MyResolver {},
description: "Current server time"
}
]);
Server builder shortcut:
let server = mcp_server!{
name: "Demo",
instructions: "Use the provided tools",
tool_duplicates: fastmcp_rs::tool::DuplicateBehavior::Warn,
};
FastMcpServerhttps://gitee.com/luzhihaoTestingLab/fastmcp-rust.git0.2.0fastmcp-rs-macros 版本 0.2.0https://docs.rs/fastmcp-rs、https://docs.rs/fastmcp-rs-macrosauto-register:启用后,使用 #[mcp_tool] 的函数会自动注册到 FastMcpServer,避免显式 mcp_register_tools!。0.2.x:新增属性宏 #[mcp_tool] 与 auto-register 可选特性,示例与文档更新。0.1.x:核心服务、工具/资源/提示词注册与 HTTP/STDIO 传输原型。Cargo.toml:主 crate 与子 crate 的版本、仓库、主页、文档字段完整。simple_add、fetch_url、resources 可独立构建;在 Windows 环境下,避免一次性 --examples 链接锁问题。auto-register 已文档化并在示例中兼容处理(避免重复注册)。