|
| 1 | +--- |
| 2 | +title: Debugging MCP servers |
| 3 | +description: A comprehensive guide to debugging Model Context Protocol (MCP) servers and integrations |
| 4 | +--- |
| 5 | + |
| 6 | +Effective debugging is essential when developing MCP servers or integrating them with applications. This guide covers the debugging tools and approaches available in the MCP ecosystem. |
| 7 | + |
| 8 | +## Debugging tools overview |
| 9 | + |
| 10 | +MCP provides several tools for debugging at different levels: |
| 11 | + |
| 12 | +1. **MCP Inspector** |
| 13 | + - Interactive debugging interface |
| 14 | + - Direct server testing |
| 15 | + - See the [Inspector Guide](docs/tools/inspector) for details |
| 16 | + |
| 17 | +2. **Claude.app Developer Tools** |
| 18 | + - Integration testing |
| 19 | + - Log collection |
| 20 | + - Chrome DevTools integration |
| 21 | + |
| 22 | +3. **Server Logging** |
| 23 | + - Custom logging implementations |
| 24 | + - Error tracking |
| 25 | + - Performance monitoring |
| 26 | + |
| 27 | +## Debugging in Claude.app |
| 28 | + |
| 29 | +### Checking server status |
| 30 | + |
| 31 | +The Claude.app interface provides basic server status information: |
| 32 | + |
| 33 | +1. Click the 🔌 icon to view: |
| 34 | + - Connected servers |
| 35 | + - Available prompts |
| 36 | + - Accessible resources |
| 37 | + - Supported tools |
| 38 | + |
| 39 | +### Enabling logging |
| 40 | + |
| 41 | +Get detailed logs from Claude.app: |
| 42 | + |
| 43 | +```bash |
| 44 | +# Launch with logging enabled |
| 45 | +open /Applications/Claude.app --stdout ~/Claude.log --stderr ~/Claude.log |
| 46 | + |
| 47 | +# Follow logs in real-time |
| 48 | +tail -n 100 -f ~/Claude.log |
| 49 | +``` |
| 50 | + |
| 51 | +The logs capture: |
| 52 | +- Server connection events |
| 53 | +- Configuration issues |
| 54 | +- Runtime errors |
| 55 | +- Message exchanges |
| 56 | + |
| 57 | +### Using Chrome DevTools |
| 58 | + |
| 59 | +Access Chrome's developer tools for deeper inspection: |
| 60 | + |
| 61 | +1. Enable DevTools: |
| 62 | +```bash |
| 63 | +jq '.allowDevTools = true' ~/Library/Application\ Support/Claude/developer_settings.json > tmp.json \ |
| 64 | + && mv tmp.json ~/Library/Application\ Support/Claude/developer_settings.json |
| 65 | +``` |
| 66 | + |
| 67 | +2. Open DevTools: `Command-Option-Shift-i` |
| 68 | + |
| 69 | +Note: You'll see two DevTools windows: |
| 70 | +- Main content window |
| 71 | +- App title bar window |
| 72 | + |
| 73 | +Use the Network panel to inspect: |
| 74 | +- WebSocket connections |
| 75 | +- Server-sent events |
| 76 | +- Message payloads |
| 77 | +- Connection timing |
| 78 | + |
| 79 | +## Common issues |
| 80 | + |
| 81 | +### Environment variables |
| 82 | + |
| 83 | +MCP servers don't inherit environment variables automatically. Configure them in `claude_desktop_config.json`: |
| 84 | + |
| 85 | +```json |
| 86 | +{ |
| 87 | + "myserver": { |
| 88 | + "command": "mcp-server-myapp", |
| 89 | + "env": { |
| 90 | + "HOME": "/Users/username", |
| 91 | + "PATH": "/usr/local/bin:/usr/bin:/bin" |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### Server initialization |
| 98 | + |
| 99 | +Common initialization problems: |
| 100 | + |
| 101 | +1. **Path Issues** |
| 102 | + - Incorrect server executable path |
| 103 | + - Missing required files |
| 104 | + - Permission problems |
| 105 | + |
| 106 | +2. **Configuration Errors** |
| 107 | + - Invalid JSON syntax |
| 108 | + - Missing required fields |
| 109 | + - Type mismatches |
| 110 | + |
| 111 | +3. **Environment Problems** |
| 112 | + - Missing environment variables |
| 113 | + - Incorrect variable values |
| 114 | + - Permission restrictions |
| 115 | + |
| 116 | +### Connection problems |
| 117 | + |
| 118 | +When servers fail to connect: |
| 119 | + |
| 120 | +1. Check Claude.app logs |
| 121 | +2. Verify server process is running |
| 122 | +3. Test standalone with Inspector |
| 123 | +4. Check network connectivity |
| 124 | +5. Verify protocol compatibility |
| 125 | + |
| 126 | +## Implementing logging |
| 127 | + |
| 128 | +### Server-side logging |
| 129 | + |
| 130 | +Implement custom logging in your server: |
| 131 | + |
| 132 | +```typescript |
| 133 | +server.setNotificationHandler(LoggingMessageNotificationSchema, (notification) => { |
| 134 | + console.log(`[${notification.params.level}] ${notification.params.data}`); |
| 135 | +}); |
| 136 | +``` |
| 137 | + |
| 138 | +Important events to log: |
| 139 | +- Initialization steps |
| 140 | +- Resource access |
| 141 | +- Tool execution |
| 142 | +- Error conditions |
| 143 | +- Performance metrics |
| 144 | + |
| 145 | +### Client-side logging |
| 146 | + |
| 147 | +In client applications: |
| 148 | + |
| 149 | +1. Enable debug logging |
| 150 | +2. Monitor network traffic |
| 151 | +3. Track message exchanges |
| 152 | +4. Record error states |
| 153 | + |
| 154 | +## Debugging workflow |
| 155 | + |
| 156 | +### Development cycle |
| 157 | + |
| 158 | +1. Initial Development |
| 159 | + - Use Inspector for basic testing |
| 160 | + - Implement core functionality |
| 161 | + - Add logging points |
| 162 | + |
| 163 | +2. Integration Testing |
| 164 | + - Test in Claude.app |
| 165 | + - Monitor logs |
| 166 | + - Check error handling |
| 167 | + |
| 168 | +3. Production Deployment |
| 169 | + - Verify logging |
| 170 | + - Monitor performance |
| 171 | + - Track errors |
| 172 | + |
| 173 | +### Testing changes |
| 174 | + |
| 175 | +To test changes efficiently: |
| 176 | + |
| 177 | +- **Configuration changes**: Restart Claude.app |
| 178 | +- **Server code changes**: Use Command-R to reload |
| 179 | +- **Quick iteration**: Use Inspector during development |
| 180 | + |
| 181 | +## Best practices |
| 182 | + |
| 183 | +### Logging strategy |
| 184 | + |
| 185 | +1. **Structured Logging** |
| 186 | + - Use consistent formats |
| 187 | + - Include context |
| 188 | + - Add timestamps |
| 189 | + - Track request IDs |
| 190 | + |
| 191 | +2. **Error Handling** |
| 192 | + - Log stack traces |
| 193 | + - Include error context |
| 194 | + - Track error patterns |
| 195 | + - Monitor recovery |
| 196 | + |
| 197 | +3. **Performance Tracking** |
| 198 | + - Log operation timing |
| 199 | + - Monitor resource usage |
| 200 | + - Track message sizes |
| 201 | + - Measure latency |
| 202 | + |
| 203 | +### Security considerations |
| 204 | + |
| 205 | +When debugging: |
| 206 | + |
| 207 | +1. **Sensitive Data** |
| 208 | + - Sanitize logs |
| 209 | + - Protect credentials |
| 210 | + - Mask personal information |
| 211 | + |
| 212 | +2. **Access Control** |
| 213 | + - Verify permissions |
| 214 | + - Check authentication |
| 215 | + - Monitor access patterns |
| 216 | + |
| 217 | +## Getting help |
| 218 | + |
| 219 | +When encountering issues: |
| 220 | + |
| 221 | +1. **First Steps** |
| 222 | + - Check server logs |
| 223 | + - Test with Inspector |
| 224 | + - Review configuration |
| 225 | + - Verify environment |
| 226 | + |
| 227 | +2. **Support Channels** |
| 228 | + - #mcp-sig channel |
| 229 | + - GitHub issues |
| 230 | + - MCP Asana project |
| 231 | + |
| 232 | +3. **Providing Information** |
| 233 | + - Log excerpts |
| 234 | + - Configuration files |
| 235 | + - Steps to reproduce |
| 236 | + - Environment details |
| 237 | + |
| 238 | +## Next steps |
| 239 | + |
| 240 | +- Learn to use the [MCP Inspector](/docs/tools/inspector) |
| 241 | +- Review [Protocol Overview](/api-reference/protocol-overview) |
| 242 | +- Study [Error Handling](/api-reference/error-handling) |
0 commit comments