此工具简单易用,可获取并展示macOS系统的磁盘使用情况信息。它基于MCP协议运行,且已集成到Claude Desktop中。
此工具专为macOS系统设计,以下是启动它的步骤:
pip install -r requirements.txt
python disk_usage_server.py
注意:此服务器专门针对Mac的/System/Volumes/Data分区(即disk3s5)设计,无法在其他操作系统上运行。
若要将其集成到Claude Desktop,可按以下步骤操作:
claude-config.txt的文件。{
"mcpServers": {
"disk-usage": {
"command": "python3",
"args": [
"<你的实际路径>/disk_usage_server.py"
]
}
}
}
<你的实际路径>替换为实际保存服务器文件的位置。/System/Volumes/Data分区设计。pip install -r requirements.txt
python disk_usage_server.py
claude-config.txt的文件。{
"mcpServers": {
"disk-usage": {
"command": "python3",
"args": [
"<你的实际路径>/disk_usage_server.py"
]
}
}
}
<你的实际路径>替换为实际保存服务器文件的位置。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)
disk_usage_server.py:主要的MCP服务器实现文件。requirements.txt:Python依赖管理文件。claude-config.txt:示例Claude Desktop配置文件,用于指定MCP服务器的位置和运行参数。通过此项目,可以学习到以下内容:
此项目借助Python实现了一个简单的MCP服务器,通过subprocess模块调用系统的df命令来获取磁盘使用情况。然后解析df命令的输出,提取所需的信息,并将其以JSON格式返回。服务器运行在本地的8080端口,通过HTTP协议提供服务。同时,该项目针对macOS系统的/System/Volumes/Data分区进行了特定的设计,确保能准确获取该分区的磁盘使用信息。在集成到Claude Desktop时,通过配置claude-config.txt文件,指定服务器的启动命令和参数,实现了与Claude Desktop的无缝对接。