Mcp Server Disk Usage

Mcp Server Disk Usage

🚀 磁盘使用情况工具

此工具简单易用,可获取并展示macOS系统的磁盘使用情况信息。它基于MCP协议运行,且已集成到Claude Desktop中。

🚀 快速开始

此工具专为macOS系统设计,以下是启动它的步骤:

  1. 克隆此仓库到本地。
  2. 在终端中运行以下命令安装所需的Python包:
    pip install -r requirements.txt
    
  3. 启动服务器:
    python disk_usage_server.py
    

注意:此服务器专门针对Mac的/System/Volumes/Data分区(即disk3s5)设计,无法在其他操作系统上运行。

若要将其集成到Claude Desktop,可按以下步骤操作:

  1. 在Claude Desktop的配置目录下创建一个名为claude-config.txt的文件。
  2. 将以下内容粘贴到该文件中:
    {
    "mcpServers": {
    "disk-usage": {
    "command": "python3",
    "args": [
    "<你的实际路径>/disk_usage_server.py"
    ]
    }
    }
    }
    
  3. <你的实际路径>替换为实际保存服务器文件的位置。
  4. 重启Claude Desktop以应用配置。

✨ 主要特性

  • 实时数据:监控特定分区的实时使用情况。
  • 详细信息:提供详细的磁盘使用信息,包括已用空间、可用空间、保留空间和使用百分比等。
  • 特定于macOS:特别针对Mac系统的/System/Volumes/Data分区设计。

📦 安装指南

环境要求

  • 操作系统:macOS(该工具专为Mac设计)。
  • Python版本:3.11+
  • 依赖管理工具:pip

安装步骤

  1. 克隆此仓库到本地。
  2. 在终端中运行以下命令安装所需的Python包:
    pip install -r requirements.txt
    
  3. 启动服务器:
    python disk_usage_server.py
    

集成到Claude Desktop

  1. 在Claude Desktop的配置目录下创建一个名为claude-config.txt的文件。
  2. 将以下内容粘贴到该文件中:
    {
    "mcpServers": {
    "disk-usage": {
    "command": "python3",
    "args": [
    "<你的实际路径>/disk_usage_server.py"
    ]
    }
    }
    }
    
  3. <你的实际路径>替换为实际保存服务器文件的位置。
  4. 重启Claude Desktop以应用配置。

💻 使用示例

基础用法

import subprocess
import json
from datetime import datetime, timezone
from http.server import BaseHTTPRequestHandler, HTTPServer

class DiskUsageRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
# 确定要查询的分区(此处为Mac的/System/Volumes/Data)
target_partition = '/System/Volumes/Data'

try:
# 使用df命令获取磁盘使用情况
result = subprocess.run(['df', '-h'], capture_output=True, text=True, check=True)

# 解析df命令的输出
output_lines = result.stdout.split('\n')
for line in output_lines:
if target_partition in line:
break

# 提取所需信息
parts = line.strip().split()
total_space = parts[1]
used_space = parts[2]
available_space = parts[3]
use_percentage = parts[4]
mounted_on = parts[5]

# 返回结构化数据
response = {
"device": target_partition,
"total_gb": total_space,
"used_gb": used_space,
"available_gb": available_space,
"use_percentage": use_percentage,
"mounted_on": mounted_on,
"timestamp": datetime.now(timezone.utc).isoformat()
}

self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(response).encode())

except subprocess.CalledProcessError as e:
# 处理df命令执行失败的情况
error = {
"error": str(e),
"timestamp": datetime.now(timezone.utc).isoformat()
}
self.send_response(500)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(error).encode())

if __name__ == "__main__":
server_address = ('', 8080)

📚 详细文档

项目结构

  1. disk_usage_server.py:主要的MCP服务器实现文件。
  2. requirements.txt:Python依赖管理文件。
  3. claude-config.txt:示例Claude Desktop配置文件,用于指定MCP服务器的位置和运行参数。

学习成果

通过此项目,可以学习到以下内容:

  1. 如何使用Python创建一个简单的MCP服务器。
  2. 如何将系统命令集成到MCP工具中。
  3. 如何为AI设计结构化的数据输出格式。
  4. 如何处理特定于操作系统的细节(如分区信息)。
  5. 如何提供清晰易懂的人类可读的摘要信息。

🔧 技术细节

此项目借助Python实现了一个简单的MCP服务器,通过subprocess模块调用系统的df命令来获取磁盘使用情况。然后解析df命令的输出,提取所需的信息,并将其以JSON格式返回。服务器运行在本地的8080端口,通过HTTP协议提供服务。同时,该项目针对macOS系统的/System/Volumes/Data分区进行了特定的设计,确保能准确获取该分区的磁盘使用信息。在集成到Claude Desktop时,通过配置claude-config.txt文件,指定服务器的启动命令和参数,实现了与Claude Desktop的无缝对接。

  • 0 关注
  • 0 收藏,31 浏览
  • system 提出于 2025-09-25 05:21

相似服务问题

相关AI产品