GitHubスター
0
ユーザー評価
未評価
お気に入り
0
閲覧数
4
フォーク
0
イシュー
0
MCP Server Rust Template
A production-ready template for building MCP (Model Context Protocol) servers in Rust, deployable to Cloudflare Workers.
🚀 Quick Start
Using cargo-generate (Recommended)
# Install cargo-generate if you haven't already
cargo install cargo-generate
# Generate a new project from this template
cargo generate --git git@github.com:sunnyhealthai/mcp-server-rust-template.git --name my-mcp-server
# Navigate to your new project
cd my-mcp-server
# Install dependencies and start developing
cargo make init
cargo make dev
Manual Setup
Clone this template
git clone git@github.com:sunnyhealthai/mcp-server-rust-template.git my-mcp-server cd my-mcp-server
Install cargo-make
cargo install cargo-make
Install dependencies
cargo make install-deps
Run locally
cargo make dev
Test your server
cargo make test cargo make test-integration
🛠️ Customization Guide
1. Update Server Information
Edit src/lib.rs
and update these constants:
const SERVER_NAME: &str = "Your Server Name";
const SERVER_VERSION: &str = "1.0.0";
const PROTOCOL_VERSION: &str = "2024-11-05";
2. Update Package Information
Edit Cargo.toml
:
[package]
name = "your-mcp-server"
authors = ["Your Name <your.email@example.com>"]
repository = "https://github.com/yourusername/your-mcp-server"
Edit package.json
:
{
"name": "your-mcp-server",
"description": "Your MCP server description"
}
3. Implement Your Tools
The template includes example calculator tools. To add your own:
Create a new tool module in
src/tools/
:// src/tools/your_tool.rs use crate::{ContentItem, ToolsCallResult}; pub fn handle_your_tool(arguments: Option<serde_json::Value>) -> Result<ToolsCallResult, String> { // Your implementation }
Register your tool in
src/tools/mod.rs
:pub fn register_default_tools(registry: &mut ToolRegistry) { registry.register_tool(Tool { name: "your_tool".to_string(), description: Some("Description of your tool".to_string()), input_schema: json!({ "type": "object", "properties": { // Define your parameters }, "required": [] }), }); }
Add the tool handler in
src/tools/mod.rs
:pub fn call_tool(&self, name: &str, arguments: Option<serde_json::Value>) -> Result<ToolsCallResult, String> { match name { "your_tool" => your_tool::handle_your_tool(arguments), // ... other tools } }
4. Remove Example Tools (Optional)
To remove the example calculator tools:
- Delete
src/tools/example_calculator.rs
- Remove the calculator tool registrations from
src/tools/mod.rs
- Remove the calculator handlers from the
call_tool
method
📁 Project Structure
├── src/
│ ├── lib.rs # Main server implementation
│ ├── tools/ # Tool implementations
│ │ ├── mod.rs # Tool registry and management
│ │ └── example_calculator.rs # Example calculator tools
│ └── utils.rs # Utility functions
├── tests/ # Unit and integration tests
├── examples/ # Example scripts and usage
├── Cargo.toml # Rust dependencies
├── package.json # NPM scripts
├── wrangler.toml # Cloudflare Workers config
└── README.md # This file
🔧 Development
Available Commands
cargo make dev
- Start development servercargo make build
- Build for productioncargo make deploy
- Deploy to Cloudflare Workerscargo make test
- Run Rust testscargo make test-integration
- Run integration testscargo make lint
- Run clippy lintercargo make format
- Format code with rustfmtcargo make ci
- Run full CI pipelinecargo make clean
- Clean build artifactscargo make --list-all-steps
- Show all available tasks
Task Aliases
cargo make t
- Alias for testcargo make b
- Alias for buildcargo make d
- Alias for deploycargo make c
- Alias for check
Testing
The template includes comprehensive tests:
- Unit tests for tools in
tests/integration_test.rs
- Transport tests in
tests/transport_test.rs
- Integration test script in
examples/test_mcp_client.sh
Adding Tests
Add tests for your tools in tests/integration_test.rs
:
#[test]
fn test_your_tool() {
// Your test implementation
}
🌐 API Reference
MCP Protocol Endpoints
POST /mcp
- Streamable HTTP Transport
The main MCP endpoint supporting JSON-RPC 2.0 requests.
Supported Methods:
initialize
- Initialize MCP sessiontools/list
- List available toolstools/call
- Execute a tool
Headers:
Content-Type: application/json
Accept: application/json
orAccept: text/event-stream
GET /sse
- Legacy SSE Transport
Returns SSE stream with endpoint information (for backward compatibility).
POST /messages
- Legacy SSE Messages
Handles JSON-RPC messages for legacy SSE transport.
Example Usage
# Initialize session
curl -X POST http://localhost:8787/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "initialize",
"params": {
"protocol_version": "2024-11-05",
"capabilities": {},
"client_info": {
"name": "example-client",
"version": "1.0.0"
}
},
"id": 1
}'
# List available tools
curl -X POST http://localhost:8787/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "tools/list",
"id": 2
}'
# Call a tool
curl -X POST http://localhost:8787/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "your_tool",
"arguments": {
// Your tool arguments
}
},
"id": 3
}'
🚀 Deployment
Deploy to Cloudflare Workers
Login to Cloudflare
wrangler login
Configure your worker
Editwrangler.toml
:name = "your-mcp-server"
Deploy
cargo make deploy
Environment Variables
Add any required environment variables to your Cloudflare Worker:
wrangler secret put YOUR_SECRET_NAME
Access in your code:
let secret = ctx.env.secret("YOUR_SECRET_NAME")?;
📚 Resources
🤝 Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Based on the MCP (Model Context Protocol) specification
- Built for deployment on Cloudflare Workers
- Inspired by the TypeScript MCP implementation