|
| 1 | +# MCP Protocol Implementation |
| 2 | + |
| 3 | +This server now implements the **Model Context Protocol (MCP)** v2024-11-05. |
| 4 | + |
| 5 | +## What is MCP? |
| 6 | + |
| 7 | +MCP is a standardized protocol that allows AI assistants (like Claude, ChatGPT) to: |
| 8 | +- Discover what resources are available |
| 9 | +- Read resources (device data, locations, groups) |
| 10 | +- List and call tools (functions to interact with devices) |
| 11 | + |
| 12 | +## Endpoints |
| 13 | + |
| 14 | +### 1. Initialize (No Auth Required) |
| 15 | + |
| 16 | +```bash |
| 17 | +POST /api/mcp/initialize |
| 18 | +Content-Type: application/json |
| 19 | + |
| 20 | +{ |
| 21 | + "jsonrpc": "2.0", |
| 22 | + "method": "initialize", |
| 23 | + "id": 1 |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +**Response:** |
| 28 | +```json |
| 29 | +{ |
| 30 | + "jsonrpc": "2.0", |
| 31 | + "id": 1, |
| 32 | + "result": { |
| 33 | + "protocolVersion": "2024-11-05", |
| 34 | + "capabilities": { |
| 35 | + "logging": {}, |
| 36 | + "resources": { "listChanged": true }, |
| 37 | + "tools": { "listChanged": true }, |
| 38 | + "sampling": {} |
| 39 | + }, |
| 40 | + "serverInfo": { |
| 41 | + "name": "IoT Cloud MCP Bridge", |
| 42 | + "version": "1.0.0" |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +### 2. List Tools (No Auth Required) |
| 49 | + |
| 50 | +```bash |
| 51 | +POST /api/mcp |
| 52 | +Content-Type: application/json |
| 53 | + |
| 54 | +{ |
| 55 | + "jsonrpc": "2.0", |
| 56 | + "method": "tools/list", |
| 57 | + "id": 2 |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +**Response:** |
| 62 | +```json |
| 63 | +{ |
| 64 | + "jsonrpc": "2.0", |
| 65 | + "id": 2, |
| 66 | + "result": { |
| 67 | + "tools": [ |
| 68 | + { |
| 69 | + "name": "get_devices", |
| 70 | + "description": "Get all IoT devices for the authenticated user", |
| 71 | + "inputSchema": { |
| 72 | + "type": "object", |
| 73 | + "properties": { |
| 74 | + "locationId": { "type": "string", "description": "Optional: Filter devices by location ID" }, |
| 75 | + "groupId": { "type": "string", "description": "Optional: Filter devices by group ID" } |
| 76 | + } |
| 77 | + } |
| 78 | + }, |
| 79 | + ... |
| 80 | + ] |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### 3. List Resources (Auth Required) |
| 86 | + |
| 87 | +```bash |
| 88 | +POST /api/mcp/call |
| 89 | +Content-Type: application/json |
| 90 | +Authorization: Bearer {FIREBASE_TOKEN} |
| 91 | + |
| 92 | +{ |
| 93 | + "jsonrpc": "2.0", |
| 94 | + "method": "resources/list", |
| 95 | + "id": 3 |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +**Response:** |
| 100 | +```json |
| 101 | +{ |
| 102 | + "jsonrpc": "2.0", |
| 103 | + "id": 3, |
| 104 | + "result": { |
| 105 | + "resources": [ |
| 106 | + { |
| 107 | + "uri": "iot://devices", |
| 108 | + "name": "Devices", |
| 109 | + "description": "All IoT devices accessible to the user", |
| 110 | + "mimeType": "application/json" |
| 111 | + }, |
| 112 | + { |
| 113 | + "uri": "iot://device/{uuid}", |
| 114 | + "name": "Device: Light 01", |
| 115 | + "description": "Device: SmartLight", |
| 116 | + "mimeType": "application/json" |
| 117 | + }, |
| 118 | + ... |
| 119 | + ] |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +### 4. Read Resource (Auth Required) |
| 125 | + |
| 126 | +```bash |
| 127 | +POST /api/mcp/call |
| 128 | +Content-Type: application/json |
| 129 | +Authorization: Bearer {FIREBASE_TOKEN} |
| 130 | + |
| 131 | +{ |
| 132 | + "jsonrpc": "2.0", |
| 133 | + "method": "resources/read", |
| 134 | + "params": { |
| 135 | + "uri": "iot://devices" |
| 136 | + }, |
| 137 | + "id": 4 |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +**Response:** |
| 142 | +```json |
| 143 | +{ |
| 144 | + "jsonrpc": "2.0", |
| 145 | + "id": 4, |
| 146 | + "result": { |
| 147 | + "contents": [ |
| 148 | + { |
| 149 | + "uri": "iot://devices", |
| 150 | + "mimeType": "application/json", |
| 151 | + "text": "[{\"uuid\": \"...\", \"name\": \"Light 01\", ...}]" |
| 152 | + } |
| 153 | + ] |
| 154 | + } |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +### 5. Call Tool (Auth Required) |
| 159 | + |
| 160 | +```bash |
| 161 | +POST /api/mcp/call |
| 162 | +Content-Type: application/json |
| 163 | +Authorization: Bearer {FIREBASE_TOKEN} |
| 164 | + |
| 165 | +{ |
| 166 | + "jsonrpc": "2.0", |
| 167 | + "method": "tools/call", |
| 168 | + "params": { |
| 169 | + "name": "get_devices", |
| 170 | + "args": { |
| 171 | + "locationId": "location-123" |
| 172 | + } |
| 173 | + }, |
| 174 | + "id": 5 |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | +**Response:** |
| 179 | +```json |
| 180 | +{ |
| 181 | + "jsonrpc": "2.0", |
| 182 | + "id": 5, |
| 183 | + "result": { |
| 184 | + "content": [ |
| 185 | + { |
| 186 | + "type": "text", |
| 187 | + "text": "[{\"uuid\": \"...\", \"name\": \"Light 01\", ...}]" |
| 188 | + } |
| 189 | + ] |
| 190 | + } |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +## Available Tools |
| 195 | + |
| 196 | +### `get_devices` |
| 197 | +Get all IoT devices for the user |
| 198 | + |
| 199 | +**Parameters:** |
| 200 | +- `locationId` (optional): Filter by location |
| 201 | +- `groupId` (optional): Filter by group |
| 202 | + |
| 203 | +### `get_device` |
| 204 | +Get details of a specific device |
| 205 | + |
| 206 | +**Parameters:** |
| 207 | +- `deviceId` (required): Device UUID |
| 208 | + |
| 209 | +### `get_device_state` |
| 210 | +Get current state and properties of a device |
| 211 | + |
| 212 | +**Parameters:** |
| 213 | +- `deviceId` (required): Device UUID |
| 214 | + |
| 215 | +### `get_locations` |
| 216 | +Get all location groups |
| 217 | + |
| 218 | +**Parameters:** None |
| 219 | + |
| 220 | +### `get_groups` |
| 221 | +Get all device groups |
| 222 | + |
| 223 | +**Parameters:** |
| 224 | +- `locationId` (optional): Filter by location |
| 225 | + |
| 226 | +### `get_definitions` |
| 227 | +Get entity definitions and workflow examples |
| 228 | + |
| 229 | +**Parameters:** |
| 230 | +- `type` (optional): "entities" or "workflows" |
| 231 | + |
| 232 | +## Available Resources |
| 233 | + |
| 234 | +### `iot://devices` |
| 235 | +List of all user devices |
| 236 | + |
| 237 | +### `iot://locations` |
| 238 | +List of all user locations |
| 239 | + |
| 240 | +### `iot://groups` |
| 241 | +List of all device groups |
| 242 | + |
| 243 | +### `iot://device/{uuid}` |
| 244 | +Details of a specific device |
| 245 | + |
| 246 | +## Authentication Flow |
| 247 | + |
| 248 | +1. User logs in to your app: `POST /api/auth/login` |
| 249 | +2. Gets Firebase token |
| 250 | +3. Sends token in MCP requests: `Authorization: Bearer {token}` |
| 251 | +4. Server validates token and returns user-scoped data |
| 252 | + |
| 253 | +## Example: Complete MCP Flow |
| 254 | + |
| 255 | +```bash |
| 256 | +# Step 1: Initialize |
| 257 | +curl -X POST http://localhost:3001/api/mcp/initialize \ |
| 258 | + -H "Content-Type: application/json" \ |
| 259 | + -d '{ |
| 260 | + "jsonrpc": "2.0", |
| 261 | + "method": "initialize", |
| 262 | + "id": 1 |
| 263 | + }' |
| 264 | + |
| 265 | +# Step 2: List tools (no auth needed) |
| 266 | +curl -X POST http://localhost:3001/api/mcp \ |
| 267 | + -H "Content-Type: application/json" \ |
| 268 | + -d '{ |
| 269 | + "jsonrpc": "2.0", |
| 270 | + "method": "tools/list", |
| 271 | + "id": 2 |
| 272 | + }' |
| 273 | + |
| 274 | +# Step 3: Login to get token |
| 275 | +TOKEN=$(curl -X POST http://localhost:3001/api/auth/login \ |
| 276 | + -H "Content-Type: application/json" \ |
| 277 | + -d '{"email":"user@example.com","password":"pass"}' | jq -r '.access_token') |
| 278 | + |
| 279 | +# Step 4: List resources (with auth) |
| 280 | +curl -X POST http://localhost:3001/api/mcp/call \ |
| 281 | + -H "Content-Type: application/json" \ |
| 282 | + -H "Authorization: Bearer $TOKEN" \ |
| 283 | + -d '{ |
| 284 | + "jsonrpc": "2.0", |
| 285 | + "method": "resources/list", |
| 286 | + "id": 3 |
| 287 | + }' |
| 288 | + |
| 289 | +# Step 5: Call a tool (with auth) |
| 290 | +curl -X POST http://localhost:3001/api/mcp/call \ |
| 291 | + -H "Content-Type: application/json" \ |
| 292 | + -H "Authorization: Bearer $TOKEN" \ |
| 293 | + -d '{ |
| 294 | + "jsonrpc": "2.0", |
| 295 | + "method": "tools/call", |
| 296 | + "params": { |
| 297 | + "name": "get_devices" |
| 298 | + }, |
| 299 | + "id": 4 |
| 300 | + }' |
| 301 | +``` |
| 302 | + |
| 303 | +## Error Handling |
| 304 | + |
| 305 | +All errors follow JSON-RPC 2.0 error format: |
| 306 | + |
| 307 | +```json |
| 308 | +{ |
| 309 | + "jsonrpc": "2.0", |
| 310 | + "id": 1, |
| 311 | + "error": { |
| 312 | + "code": -32603, |
| 313 | + "message": "Internal server error", |
| 314 | + "data": {} |
| 315 | + } |
| 316 | +} |
| 317 | +``` |
| 318 | + |
| 319 | +**Error codes:** |
| 320 | +- `-32700`: Parse error |
| 321 | +- `-32600`: Invalid request |
| 322 | +- `-32601`: Method not found |
| 323 | +- `-32602`: Invalid params |
| 324 | +- `-32603`: Internal error |
| 325 | +- `-32001`: Resource not found |
| 326 | +- `-32002`: Invalid request ID |
| 327 | + |
| 328 | +## Integration with MCP Clients |
| 329 | + |
| 330 | +Your server is now compatible with: |
| 331 | + |
| 332 | +- **Claude Desktop App** - Via MCP client configuration |
| 333 | +- **ChatGPT** - Via API/tool endpoints |
| 334 | +- **Custom MCP Clients** - Any JSON-RPC 2.0 client |
| 335 | + |
| 336 | +## MCP Specification |
| 337 | + |
| 338 | +Full specification: https://spec.modelcontextprotocol.io/ |
| 339 | + |
| 340 | +Key points: |
| 341 | +- ✅ JSON-RPC 2.0 protocol |
| 342 | +- ✅ Resource listing and reading |
| 343 | +- ✅ Tool definition and invocation |
| 344 | +- ✅ Error handling |
| 345 | +- ✅ Both authenticated and unauthenticated endpoints |
| 346 | + |
| 347 | +## Testing Your MCP Setup |
| 348 | + |
| 349 | +Use any JSON-RPC client or curl to test endpoints. Recommended tools: |
| 350 | + |
| 351 | +- **Postman** - GUI testing with JSON-RPC support |
| 352 | +- **Thunder Client** - VSCode extension |
| 353 | +- **curl** - Command line (see examples above) |
| 354 | +- **MCP Inspector** - Official debugging tool from Anthropic |
| 355 | + |
| 356 | +## Next Steps |
| 357 | + |
| 358 | +1. Deploy to production (Render recommended) |
| 359 | +2. Get your server URL |
| 360 | +3. Configure MCP client (Claude Desktop, ChatGPT, etc.) |
| 361 | +4. Test tool execution |
| 362 | +5. Monitor logs for issues |
| 363 | + |
| 364 | +See [CHATGPT_APPS.md](./CHATGPT_APPS.md) for ChatGPT-specific setup. |
0 commit comments