# MCP Datadog Server (SDK Version)
MCP Server untuk Datadog menggunakan `@modelcontextprotocol/sdk` - implementasi resmi dari Model Context Protocol.
## Perbedaan dengan Versi Manual
### `mcp-server.js` (Manual Implementation)
- Implementasi JSON-RPC manual
- Handle request/response secara manual
- Error handling custom
### `mcp-server-sdk.js` (SDK Implementation) ✨ **Recommended**
- Menggunakan `@modelcontextprotocol/sdk` official
- API yang lebih clean dan maintainable
- Error handling otomatis dari SDK
- Compatible dengan semua MCP clients
## Prerequisites
```bash
npm install
```
Dependencies:
- `@modelcontextprotocol/sdk`: ^1.0.1
- `axios`: ^1.6.0
- `dotenv`: ^16.0.3
## Environment Variables
Buat file `.env` di root folder:
```env
DATADOG_API_KEY=your_api_key_here
DATADOG_APP_KEY=your_app_key_here
DATADOG_SITE=datadoghq.com
```
## Running the Server
### Development
```bash
npm run mcp-server-sdk
```
### Integration dengan Claude Desktop
Edit file konfigurasi Claude Desktop:
- **MacOS**: `~/Library/Application Support/Claude/claude_desktop_config.json`
- **Windows**: `%APPDATA%\Claude\claude_desktop_config.json`
Tambahkan konfigurasi berikut:
```json
{
"mcpServers": {
"datadog": {
"command": "node",
"args": [
"D:\\repo\\mcp-datadog\\datadog-mcp\\mcp-server-sdk.js"
],
"env": {
"DATADOG_API_KEY": "your_api_key",
"DATADOG_APP_KEY": "your_app_key",
"DATADOG_SITE": "datadoghq.com"
}
}
}
}
```
## Available Tools
### 1. `list_services`
List semua tools yang tersedia
### 2. `query_metrics`
Query general purpose untuk metric Datadog apapun
**Parameters:**
- `query` (string): Datadog metric query
- `from` (integer): Unix timestamp start (seconds)
- `to` (integer): Unix timestamp end (seconds)
**Example:**
```json
{
"query": "avg:system.cpu.user{*}",
"from": 1700000000,
"to": 1700003600
}
```
### 3. `query_kubernetes_metrics`
Query Kubernetes metrics
**Parameters:**
- `query` (string): Datadog K8s metric query
- `from` (integer): Unix timestamp start
- `to` (integer): Unix timestamp end
- `cluster` (string, optional): Cluster name
### 4. `query_deployment_pods`
Query pod count dan metrics untuk deployment spesifik
**Parameters:**
- `namespace` (string): K8s namespace
- `deployment` (string): Deployment name
- `from` (integer): Unix timestamp start
- `to` (integer): Unix timestamp end
- `cluster` (string, optional): Cluster name
- `apm_service_name` (string, optional): APM service name
**Returns:**
- pods count
- CPU usage & limits
- Memory usage & limits
- Network in/out
- Response time (APM)
### 5. `query_apm_metrics`
Query APM metrics untuk service
**Parameters:**
- `service` (string): Service name
- `from` (integer): Unix timestamp start
- `to` (integer): Unix timestamp end
- `metric` (string, optional): Metric name
- `aggregator` (string, optional): avg|max|min|sum
### 6. `query_host_metrics`
Query host/infrastructure metrics
**Parameters:**
- `hostname` (string): Hostname
- `from` (integer): Unix timestamp start
- `to` (integer): Unix timestamp end
- `metric` (string, optional): Metric name
- `aggregator` (string, optional): avg|max|min|sum
### 7. `query_database_metrics`
Query database monitoring metrics
**Parameters:**
- `db_host` (string): Database host
- `from` (integer): Unix timestamp start
- `to` (integer): Unix timestamp end
- `db_type` (string, optional): postgresql|mysql
- `metric` (string, optional): Metric name
- `aggregator` (string, optional): avg|max|min|sum
### 8. `list_hosts`
List semua monitored hosts
**Parameters:**
- `from` (integer): Unix timestamp start
- `to` (integer): Unix timestamp end
### 9. `list_databases`
List semua monitored databases
**Parameters:**
- `from` (integer): Unix timestamp start
- `to` (integer): Unix timestamp end
- `db_type` (string, optional): postgresql|mysql
### 10. `list_all_hosts`
List semua hosts dengan informasi lengkap (CPU, memory, OS, kernel, dll)
**Parameters:** None
### 11. `get_host_detail`
Get detail lengkap untuk host tertentu
**Parameters:**
- `hostname` (string): Hostname
### 12. `get_namespace_metrics`
Get total resource usage untuk K8s namespace
**Parameters:**
- `namespace` (string): K8s namespace
- `from` (integer): Unix timestamp start
- `to` (integer): Unix timestamp end
- `cluster` (string, optional): Cluster name
## Testing
### Manual Test
```bash
# Initialize
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}' | node mcp-server-sdk.js
# List tools
echo '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}' | node mcp-server-sdk.js
# Call tool
echo '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"list_all_hosts","arguments":{}}}' | node mcp-server-sdk.js
```
## Architecture
```
mcp-server-sdk.js
├── @modelcontextprotocol/sdk
│ ├── Server
│ ├── StdioServerTransport
│ └── Request Schemas
├── Datadog API Client (axios)
├── CLI Query Executor (npm run query)
└── Tool Handlers
├── listServices()
├── queryMetrics()
├── queryKubernetesMetrics()
├── queryDeploymentPods()
├── queryAPMMetrics()
├── queryHostMetrics()
├── queryDatabaseMetrics()
├── listHosts()
├── listDatabases()
├── listAllHosts()
├── getHostDetail()
└── getNamespaceMetrics()
```
## Migration dari Manual ke SDK
Jika saat ini menggunakan `mcp-server.js`, untuk migrasi ke SDK version:
1. Update Claude Desktop config untuk menggunakan `mcp-server-sdk.js`
2. Restart Claude Desktop
3. Test dengan query sederhana
Tidak ada perubahan di API atau parameter - 100% backward compatible!
## Advantages of SDK Version
✅ **Maintainable**: Kode lebih clean dan terstruktur
✅ **Reliable**: Error handling otomatis dari SDK
✅ **Future-proof**: Update SDK untuk fitur baru MCP
✅ **Standard**: Mengikuti best practices MCP
✅ **Type-safe**: Better TypeScript support (jika migrasi ke TS)
## Next Steps
Untuk menambahkan tool baru:
1. Tambahkan tool definition di `ListToolsRequestSchema` handler
2. Tambahkan case di `CallToolRequestSchema` handler
3. Implementasikan function handler
4. Test dengan MCP Inspector atau Claude Desktop
## Troubleshooting
### Error: Cannot find module '@modelcontextprotocol/sdk'
```bash
npm install
```
### Error: DATADOG_API_KEY not set
Pastikan file `.env` sudah dibuat dengan API keys yang valid
### Server tidak respond
Check console error dengan:
```bash
node mcp-server-sdk.js 2>&1 | tee debug.log
```
## Support
Untuk pertanyaan atau issue, check:
- [MCP Documentation](https://modelcontextprotocol.io)
- [Datadog API Docs](https://docs.datadoghq.com/api/)