Skip to content

Sample MCP Server - Go (system-monitor-server) #898

@crivetimihai

Description

@crivetimihai

Overview

Create a sample MCP Server in Go that provides comprehensive system monitoring capabilities including process management, resource usage, and system health metrics.

Server Specifications

Server Details

  • Name: system-monitor-server
  • Language: Go
  • Location: mcp-servers/go/system-monitor-server/
  • Purpose: Demonstrate system monitoring and process management via MCP

Core Features

  • Real-time system metrics (CPU, memory, disk, network)
  • Process monitoring and management
  • Service health checks
  • Log file monitoring and analysis
  • System resource alerts
  • Cross-platform support (Linux, macOS, Windows)

Tools Provided

1. get_system_metrics

Retrieve current system resource usage

type SystemMetrics struct {
    CPU       CPUMetrics    `json:"cpu"`
    Memory    MemoryMetrics `json:"memory"`
    Disk      []DiskMetrics `json:"disk"`
    Network   NetworkMetrics `json:"network"`
    Timestamp time.Time     `json:"timestamp"`
}

2. list_processes

List running processes with filtering options

type ProcessListRequest struct {
    FilterBy   string `json:"filter_by,omitempty"`   // name, user, pid
    FilterValue string `json:"filter_value,omitempty"`
    SortBy     string `json:"sort_by,omitempty"`     // cpu, memory, name
    Limit      int    `json:"limit,omitempty"`
    IncludeThreads bool `json:"include_threads,omitempty"`
}

3. monitor_process

Monitor specific process health and resource usage

type ProcessMonitorRequest struct {
    PID          int           `json:"pid,omitempty"`
    ProcessName  string        `json:"process_name,omitempty"`
    Duration     time.Duration `json:"duration"`
    Interval     time.Duration `json:"interval"`
    AlertThresholds Thresholds `json:"alert_thresholds,omitempty"`
}

4. check_service_health

Check health of system services and applications

type HealthCheckRequest struct {
    Services []ServiceCheck `json:"services"`
    Timeout  time.Duration  `json:"timeout,omitempty"`
}

type ServiceCheck struct {
    Name     string            `json:"name"`
    Type     string            `json:"type"`     // port, http, command, file
    Target   string            `json:"target"`
    Expected map[string]string `json:"expected,omitempty"`
}

5. tail_logs

Stream log file contents with filtering

type LogTailRequest struct {
    FilePath   string `json:"file_path"`
    Lines      int    `json:"lines,omitempty"`      // number of lines to tail
    Follow     bool   `json:"follow,omitempty"`     // continuous monitoring
    Filter     string `json:"filter,omitempty"`     // regex filter
    MaxSize    int64  `json:"max_size,omitempty"`   // max file size to process
}

6. get_disk_usage

Analyze disk usage with detailed breakdowns

type DiskUsageRequest struct {
    Path      string   `json:"path"`
    MaxDepth  int      `json:"max_depth,omitempty"`
    MinSize   int64    `json:"min_size,omitempty"`
    SortBy    string   `json:"sort_by,omitempty"`
    FileTypes []string `json:"file_types,omitempty"`
}

Implementation Requirements

Directory Structure

mcp-servers/go/system-monitor-server/
├── cmd/
│   └── server/
│       └── main.go
├── internal/
│   ├── metrics/
│   │   ├── system.go
│   │   ├── process.go
│   │   └── network.go
│   ├── monitor/
│   │   ├── process_monitor.go
│   │   ├── log_monitor.go
│   │   └── health_checker.go
│   ├── alerts/
│   │   └── threshold.go
│   └── config/
│       └── config.go
├── pkg/
│   ├── types/
│   │   └── metrics.go
│   └── utils/
│       └── platform.go
├── go.mod
├── go.sum
├── README.md
└── Makefile

Dependencies

// go.mod
module github.com/IBM/mcp-context-forge/mcp-servers/go/system-monitor-server

go 1.21

require (
    github.com/shirou/gopsutil/v3 v3.23.12
    github.com/hpcloud/tail v1.0.0
    github.com/IBM/mcp-context-forge/mcp-servers/go/mcp v0.1.0
    golang.org/x/sys v0.15.0
)

Configuration

# config.yaml
monitoring:
  update_interval: "5s"
  history_retention: "24h"
  
alerts:
  cpu_threshold: 80.0      # percent
  memory_threshold: 85.0   # percent  
  disk_threshold: 90.0     # percent
  
health_checks:
  - name: "web_server"
    type: "http"
    target: "http://localhost:8080/health"
    interval: "30s"
    
  - name: "database"
    type: "port"
    target: "localhost:5432"
    interval: "60s"

log_monitoring:
  max_file_size: "100MB"
  max_tail_lines: 1000
  allowed_paths: ["/var/log", "/tmp", "./logs"]

Usage Examples

System Metrics

# Get current system metrics
echo '{
  "method": "tools/call",
  "params": {
    "name": "get_system_metrics",
    "arguments": {}
  }
}' | mcp-system-monitor-server

Process Monitoring

# Monitor high CPU processes
echo '{
  "method": "tools/call",
  "params": {
    "name": "list_processes", 
    "arguments": {
      "sort_by": "cpu",
      "limit": 10
    }
  }
}' | mcp-system-monitor-server

Health Checks

# Check service health
echo '{
  "method": "tools/call",
  "params": {
    "name": "check_service_health",
    "arguments": {
      "services": [
        {"name": "web", "type": "http", "target": "http://localhost:8080/health"},
        {"name": "db", "type": "port", "target": "localhost:5432"}
      ],
      "timeout": "10s"
    }
  }
}' | mcp-system-monitor-server

Advanced Features

  • Real-time Streaming: Live metrics via WebSocket
  • Historical Data: Time-series data collection
  • Alert System: Configurable threshold-based alerts
  • Performance Profiling: Process performance analysis
  • Container Support: Docker and Kubernetes monitoring
  • Remote Monitoring: Monitor remote systems via SSH

Security Features

  • Path traversal protection for log access
  • Process access controls
  • Configurable allowed directories
  • Rate limiting for resource-intensive operations
  • Audit logging for administrative actions

Testing Requirements

  • Unit tests for all metric collection functions
  • Integration tests on multiple platforms
  • Performance tests for high-load scenarios
  • Security tests for path traversal prevention
  • Mock tests for system dependencies

Acceptance Criteria

  • Go MCP server with 6+ system monitoring tools
  • Cross-platform support (Linux, macOS, Windows)
  • Real-time system metrics collection
  • Process monitoring and health checking
  • Log file monitoring with filtering
  • Configurable alerts and thresholds
  • Security controls for file system access
  • Comprehensive test suite (>90% coverage)
  • Performance optimized for minimal system impact
  • Complete documentation with examples

Priority

Medium - Demonstrates system administration and monitoring patterns

Use Cases

  • Development environment monitoring
  • Application performance debugging
  • System health dashboards
  • Automated system administration
  • Container and service monitoring
  • Log analysis and troubleshooting

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestgoGo programmingmcp-serversMCP Server SamplesoicOpen Innovation Community Contributions

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions