A Model Context Protocol (MCP) server implementation for AI agent integration with RustAPI.
📖 Cookbook: Crates → rustapi-toon · MCP Specification
- Rust 1.70+
- Understanding of toon-api example
- Interest in AI/LLM development
- ✅ Tool Listing - Expose available tools to AI assistants
- ✅ Tool Execution - Execute tools with type-safe arguments
- ✅ Resource Listing - Provide access to documents and resources
- ✅ TOON Format - 50-58% token savings for LLM communication
- ✅ Token Counting - Automatic token usage headers
- ✅ Content Negotiation - Automatic JSON/TOON format selection
cargo run --example mcp-serverThe server will start on http://localhost:8080
curl http://localhost:8080/mcpcurl http://localhost:8080/mcp/toolscurl -H "Accept: application/toon" http://localhost:8080/mcp/toolscurl -X POST http://localhost:8080/mcp/tools/execute \
-H "Content-Type: application/json" \
-d '{
"tool": "calculate",
"arguments": {
"operation": "add",
"a": 5,
"b": 3
}
}'curl http://localhost:8080/mcp/resourcesPerforms basic arithmetic operations (add, subtract, multiply, divide).
Arguments:
operation(string, required): One of "add", "subtract", "multiply", "divide"a(number, required): First operandb(number, required): Second operand
Example:
curl -X POST http://localhost:8080/mcp/tools/execute \
-H "Content-Type: application/json" \
-d '{
"tool": "calculate",
"arguments": {
"operation": "multiply",
"a": 7,
"b": 6
}
}'Gets current weather for a location (mock implementation).
Arguments:
location(string, required): City name or coordinatesunits(string, optional): "celsius" or "fahrenheit" (default: celsius)
Example:
curl -X POST http://localhost:8080/mcp/tools/execute \
-H "Content-Type: application/json" \
-d '{
"tool": "get_weather",
"arguments": {
"location": "Istanbul",
"units": "celsius"
}
}'JSON Response (148 bytes, ~40 tokens):
{
"tools": [
{
"name": "calculate",
"description": "Perform basic arithmetic operations"
}
]
}TOON Response (87 bytes, ~25 tokens) - 37% savings:
tools[1]{name,description}:
calculate,Perform basic arithmetic operations
All MCP responses include token counting headers:
X-Token-Count-JSON- Token count if JSON formatX-Token-Count-TOON- Token count if TOON formatX-Token-Savings- Percentage saved with TOONX-Format-Used- Format actually used ("json" or "toon")
This MCP server can be used with AI assistants like Claude, GPT-4, or any MCP-compatible client. Simply point the client to http://localhost:8080/mcp and it will discover available tools and resources.
To add more tools:
- Add tool definition to
list_tools()function - Add execution logic to
execute_tool()function - Update the match statement with your tool name
Example:
Tool {
name: "my_new_tool".to_string(),
description: "Does something useful".to_string(),
input_schema: ToolSchema {
// Define schema here
},
}