Tiny MCP Server (Rust)

这是一个用Rust实现的MCP(Machine Conversation Protocol)服务器。
功能
- 支持MCP协议的生命周期管理
- 支持多种传输方式(stdio、SSE)
- 提供工具注册和调用机制
运行测试
运行所有测试:
cargo test
运行SSE测试
SSE测试需要启动实际的服务器进程,可能会导致端口冲突和进程残留问题。这些测试默认被标记为#[ignore]
,需要手动运行。
要运行SSE测试,请按照以下步骤操作:
- 首先清理可能残留的进程:
./scripts/cleanup_sse.sh
- 然后手动运行SSE示例:
cargo run --example sse
- 在另一个终端窗口中,使用curl测试SSE服务器:
curl http://localhost:3000/schema
- 如果需要关闭SSE服务器,可以使用以下命令:
lsof -i :3000 -t | xargs kill -9
清理残留进程
如果测试过程中出现问题,可能会有SSE服务器进程残留。使用以下脚本清理:
./scripts/cleanup_sse.sh
示例
运行stdio服务器
cargo run --example stdio
运行SSE服务器
cargo run --example sse
默认监听127.0.0.1:3000
。可以指定不同的地址:
cargo run --example sse -- 127.0.0.1:8080
Features
- Multiple Transport Layers : Supports both stdio and Server-Sent Events (SSE) transports
- Extensible Tool System : Easily register custom tools with the server
- Type-Safe API : Leverages Rust's type system for safe and reliable communication
- Asynchronous Design : Built on tokio for high-performance async I/O
- Simple Integration : Just add a single dependency to your project
- Convenient Imports : Use the prelude module to import all necessary types and traits
Installation
Add this to your Cargo.toml
:
[dependencies]
tiny_mcp_server = "0.1.0"
tokio = { version = "1.0", features = ["full"] }
serde_json = "1.0"
Quick Start
For convenience, you can use the prelude module to import all necessary types and traits:
use serde_json::{Value, json};
use std::error::Error;
use tiny_mcp_server::prelude::*;
#[mcp_tool(
description = "A simple greeting tool",
version = "1.0.0",
category = "examples"
)]
pub async fn say_hello(name: String) -> MCPResult<Value> {
Ok(json!({
"greeting": format!("Hello {name}")
}))
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Start with stdio transport
server::start_stdio().await
}
Documentation
For more detailed documentation, see the API documentation.
Examples
Check out the examples directory for more usage examples:
stdio.rs
: Example using stdio transport
sse.rs
: Example using SSE transport
License
This project is licensed under either of:
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.