|
| 1 | +# MCP Server Example |
| 2 | + |
| 3 | +A simple Model Context Protocol (MCP) server implementation using RustAPI. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- ✅ **Tool Listing** - Expose available tools to AI assistants |
| 8 | +- ✅ **Tool Execution** - Execute tools with type-safe arguments |
| 9 | +- ✅ **Resource Listing** - Provide access to documents and resources |
| 10 | +- ✅ **TOON Format** - 50-58% token savings for LLM communication |
| 11 | +- ✅ **Token Counting** - Automatic token usage headers |
| 12 | +- ✅ **Content Negotiation** - Automatic JSON/TOON format selection |
| 13 | + |
| 14 | +## Running |
| 15 | + |
| 16 | +```bash |
| 17 | +cargo run --example mcp-server |
| 18 | +``` |
| 19 | + |
| 20 | +The server will start on `http://localhost:8080` |
| 21 | + |
| 22 | +## Endpoints |
| 23 | + |
| 24 | +### MCP Server Info |
| 25 | +```bash |
| 26 | +curl http://localhost:8080/mcp |
| 27 | +``` |
| 28 | + |
| 29 | +### List Tools (JSON) |
| 30 | +```bash |
| 31 | +curl http://localhost:8080/mcp/tools |
| 32 | +``` |
| 33 | + |
| 34 | +### List Tools (TOON - for LLMs) |
| 35 | +```bash |
| 36 | +curl -H "Accept: application/toon" http://localhost:8080/mcp/tools |
| 37 | +``` |
| 38 | + |
| 39 | +### Execute Tool |
| 40 | +```bash |
| 41 | +curl -X POST http://localhost:8080/mcp/tools/execute \ |
| 42 | + -H "Content-Type: application/json" \ |
| 43 | + -d '{ |
| 44 | + "tool": "calculate", |
| 45 | + "arguments": { |
| 46 | + "operation": "add", |
| 47 | + "a": 5, |
| 48 | + "b": 3 |
| 49 | + } |
| 50 | + }' |
| 51 | +``` |
| 52 | + |
| 53 | +### List Resources |
| 54 | +```bash |
| 55 | +curl http://localhost:8080/mcp/resources |
| 56 | +``` |
| 57 | + |
| 58 | +## Available Tools |
| 59 | + |
| 60 | +### calculate |
| 61 | +Performs basic arithmetic operations (add, subtract, multiply, divide). |
| 62 | + |
| 63 | +**Arguments:** |
| 64 | +- `operation` (string, required): One of "add", "subtract", "multiply", "divide" |
| 65 | +- `a` (number, required): First operand |
| 66 | +- `b` (number, required): Second operand |
| 67 | + |
| 68 | +**Example:** |
| 69 | +```bash |
| 70 | +curl -X POST http://localhost:8080/mcp/tools/execute \ |
| 71 | + -H "Content-Type: application/json" \ |
| 72 | + -d '{ |
| 73 | + "tool": "calculate", |
| 74 | + "arguments": { |
| 75 | + "operation": "multiply", |
| 76 | + "a": 7, |
| 77 | + "b": 6 |
| 78 | + } |
| 79 | + }' |
| 80 | +``` |
| 81 | + |
| 82 | +### get_weather |
| 83 | +Gets current weather for a location (mock implementation). |
| 84 | + |
| 85 | +**Arguments:** |
| 86 | +- `location` (string, required): City name or coordinates |
| 87 | +- `units` (string, optional): "celsius" or "fahrenheit" (default: celsius) |
| 88 | + |
| 89 | +**Example:** |
| 90 | +```bash |
| 91 | +curl -X POST http://localhost:8080/mcp/tools/execute \ |
| 92 | + -H "Content-Type: application/json" \ |
| 93 | + -d '{ |
| 94 | + "tool": "get_weather", |
| 95 | + "arguments": { |
| 96 | + "location": "Istanbul", |
| 97 | + "units": "celsius" |
| 98 | + } |
| 99 | + }' |
| 100 | +``` |
| 101 | + |
| 102 | +## Token Savings Example |
| 103 | + |
| 104 | +**JSON Response (148 bytes, ~40 tokens):** |
| 105 | +```json |
| 106 | +{ |
| 107 | + "tools": [ |
| 108 | + { |
| 109 | + "name": "calculate", |
| 110 | + "description": "Perform basic arithmetic operations" |
| 111 | + } |
| 112 | + ] |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +**TOON Response (87 bytes, ~25 tokens) - 37% savings:** |
| 117 | +``` |
| 118 | +tools[1]{name,description}: |
| 119 | + calculate,Perform basic arithmetic operations |
| 120 | +``` |
| 121 | + |
| 122 | +## Headers |
| 123 | + |
| 124 | +All MCP responses include token counting headers: |
| 125 | + |
| 126 | +- `X-Token-Count-JSON` - Token count if JSON format |
| 127 | +- `X-Token-Count-TOON` - Token count if TOON format |
| 128 | +- `X-Token-Savings` - Percentage saved with TOON |
| 129 | +- `X-Format-Used` - Format actually used ("json" or "toon") |
| 130 | + |
| 131 | +## Integration with AI Assistants |
| 132 | + |
| 133 | +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. |
| 134 | + |
| 135 | +## Extending |
| 136 | + |
| 137 | +To add more tools: |
| 138 | + |
| 139 | +1. Add tool definition to `list_tools()` function |
| 140 | +2. Add execution logic to `execute_tool()` function |
| 141 | +3. Update the match statement with your tool name |
| 142 | + |
| 143 | +Example: |
| 144 | +```rust |
| 145 | +Tool { |
| 146 | + name: "my_new_tool".to_string(), |
| 147 | + description: "Does something useful".to_string(), |
| 148 | + input_schema: ToolSchema { |
| 149 | + // Define schema here |
| 150 | + }, |
| 151 | +} |
| 152 | +``` |
0 commit comments