关于 Disk Usage MCP Server (Demo)
Disk Usage MCP Server (Demo) 是供 Claude Desktop 用户查询系统磁盘用量的 Python 示例服务。其 `get_disk_usage` 工具返回 macOS `/System/Volumes/Data` 分区的结构化容量信息,不支持其他操作系统。
主要功能
- 01报告总量、已用、可用和预留空间
- 02返回用量百分比和挂载信息
- 03提供易读的用量摘要
- 04针对 macOS 数据分区
适用场景
- 01通过 Claude Desktop 查询当前磁盘容量
- 02查看可用空间和预留空间
- 03检查用量百分比和挂载信息
MCP 相关
它向 MCP 客户端提供 `get_disk_usage` 工具,返回目标 macOS 分区的结构化磁盘容量、设备和挂载信息。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
磁盘使用情况 MCP 服务(演示)
什么是 MCP?
模型上下文协议(MCP)是一种协议,可让 Claude 等 AI 模型与外部工具和服务交互。它允许 Claude 实时访问系统信息,并代表你执行操作。
了解更多关于 MCP 的信息:
你将获得什么
与 Claude Desktop 集成后,这个演示 MCP 服务可让 Claude 实时检查你的 Mac 磁盘使用情况。下面是一个交互示例:

用户:“我目前的磁盘使用情况如何?”
Claude:“我来帮你检查当前的磁盘使用情况。”
[申请使用 disk-usage 工具的权限,用户同意]
查看 ```disk-usage```(本地)中的 ```get_disk_usage``` 返回结果
{
"device": "/dev/disk3s5",
"total_gb": "228.0GB",
"used_gb": "177.0GB",
"available_gb": "27.0GB",
"reserved_gb": "24.0GB",
"percent_used": "87%",
"mount": "/System/Volumes/Data",
"summary": "Total: 228.0GB | Used: 177.0GB | Available: 27.0GB | Reserved: 24.0GB | Usage: 87%"
}
Claude:“以下是你当前的磁盘使用情况:
磁盘总空间:228.0GB
已用空间:177.0GB
可用空间:27.0GB
预留空间:24.0GB
使用率:87%
挂载点:/System/Volumes/Data
设备:/dev/disk3s5
你的磁盘目前已使用 87%,还剩 27.0GB 可用空间。如果你担心使用率过高,可以考虑清理一些文件来释放更多空间。”
工作原理
服务功能
服务启动时,会通过 MCP 协议向 Claude 声明其功能。以下是 Claude 看到的内容:
@mcp.tool()
def get_disk_usage() -> Dict[str, str]:
"""Get current disk usage information for the system disk
Returns:
A dictionary containing disk usage information with fields:
- device: Device name
- total_gb: Total size in GB
- used_gb: Used space in GB
- available_gb: Available space in GB
- reserved_gb: Space reserved by system in GB
- percent_used: Usage percentage
- mount: Mount point
- summary: Human-readable summary of disk usage
"""
这告诉 Claude:
- 服务提供了一个名为
get_disk_usage的工具 - 该工具返回包含详细磁盘信息的字典
- 文档字符串解释了每个字段的含义
Claude-MCP 完整交互流程
- 发送给 Claude 的初始系统提示词:
You are Claude, an AI assistant. You have access to these tools:
get_disk_usage() -> Dict[str, str]
"Get current disk usage information for the system disk"
To use this tool, respond with a <tool_call> XML block:
<tool_call>
<name>get_disk_usage</name>
<reason>Explain why you need disk usage information</reason>
</tool_call>
- 用户查询:
User: What is my current disk usage?
- Claude 的工具选择响应:
I'll check your disk usage information.
<tool_call>
<name>get_disk_usage</name>
<reason>User has requested current disk usage information. This tool will provide detailed statistics about disk space utilization.</reason>
</tool_call>
- Claude Desktop 应用 → MCP 服务:
// HTTP POST to MCP Server
{
"jsonrpc": "2.0",
"method": "invoke",
"params": {
"tool": "get_disk_usage",
"args": {}
},
"id": 1
}
- MCP 服务 → Claude Desktop 应用:
{
"jsonrpc": "2.0",
"result": {
"device": "/dev/disk3s5",
"total_gb": "228.0GB",
"used_gb": "177.0GB",
"available_gb": "27.0GB",
"reserved_gb": "24.0GB",
"percent_used": "87%",
"mount": "/System/Volumes/Data",
"summary": "Total: 228.0GB | Used: 177.0GB | Available: 27.0GB | Reserved: 24.0GB | Usage: 87%"
},
"id": 1
}
- 发送给 Claude 的新请求(将工具输出作为上下文):
User: What is my current disk usage?
Tool Response:
{
"device": "/dev/disk3s5",
"total_gb": "228.0GB",
"used_gb": "177.0GB",
"available_gb": "27.0GB",
"reserved_gb": "24.0GB",
"percent_used": "87%",
"mount": "/System/Volumes/Data",
"summary": "Total: 228.0GB | Used: 177.0GB | Available: 27.0GB | Reserved: 24.0GB | Usage: 87%"
}
Provide a helpful response to the user based on this disk usage information.
- Claude 的最终响应:
Here's your current disk usage information:
Total Disk Space: 228.0GB
Used Space: 177.0GB
Available Space: 27.0GB
Reserved Space: 24.0GB
Usage Percentage: 87%
Mount Point: /System/Volumes/Data
Device: /dev/disk3s5
Your disk is currently at 87% capacity, with 27.0GB of space still available. If you're concerned about the high usage, you might want to consider cleaning up some files to free up more space.
这展示了完整流程:
- Claude 一开始就知道有哪些可用工具
- 用户提出问题
- Claude 识别出需要磁盘信息,并提出使用该工具
- 获得批准后,工具响应会注入对话
- Claude 将原始数据处理成有帮助的响应
技术实现
# Tool advertisement from server
{
"tools": [
{
"name": "get_disk_usage",
"description": "Get current disk usage information for the system disk",
"parameters": {}, # No parameters needed
"returns": {
"type": "object",
"properties": {
"device": {"type": "string"},
"total_gb": {"type": "string"},
"used_gb": {"type": "string"},
"available_gb": {"type": "string"},
"reserved_gb": {"type": "string"},
"percent_used": {"type": "string"},
"mount": {"type": "string"},
"summary": {"type": "string"}
}
}
}
]
}
-
用户查询:
User: "What is my current disk usage?" -
Claude 的工具选择: Claude 识别出此查询需要磁盘信息,并决定使用
get_disk_usage工具。Claude: "Let me check your current disk usage for you." [approval to use disk-usage tool, user gives consent] -
工具调用和响应:
// Raw tool response { "device": "/dev/disk3s5", "total_gb": "228.0GB", "used_gb": "177.0GB", "available_gb": "27.0GB", "reserved_gb": "24.0GB", "percent_used": "87%", "mount": "/System/Volumes/Data", "summary": "Total: 228.0GB | Used: 177.0GB | Available: 27.0GB | Reserved: 24.0GB | Usage: 87%" } -
Claude 的自然语言响应: Claude 处理原始数据,并提供易于理解的响应:
Here's your current disk usage information: Total Disk Space: 228.0GB Used Space: 177.0GB Available Space: 27.0GB Reserved Space: 24.0GB Usage Percentage: 87% Mount Point: /System/Volumes/Data Device: /dev/disk3s5 Your disk is currently at 87% capacity, with 27.0GB of space still available. If you're concerned about the high usage, you might want to consider cleaning up some files to free up more space."
通信协议
在底层,MCP 使用基于 WebSocket 的双向 JSON-RPC 协议。其工作方式如下:
-
初始化:
// Claude -> Server { "jsonrpc": "2.0", "method": "initialize", "params": { "protocolVersion": "2024-11-05", "capabilities": {}, "clientInfo": { "name": "claude-ai", "version": "0.1.0" } }, "id": 0 } -
工具声明: 服务会返回其功能,包括
get_disk_usage工具及其架构。 -
工具调用:
// Claude -> Server { "jsonrpc": "2.0", "method": "invoke", "params": { "tool": "get_disk_usage", "args": {} }, "id": 1 } -
服务响应:
// Server -> Claude { "jsonrpc": "2.0", "result": { "device": "/dev/disk3s5", "total_gb": "228.0GB", "used_gb": "177.0GB", ... }, "id": 1 }
这种基于 WebSocket 的协议支持:
- 持久连接
- 双向通信
- 结构化类型信息
- 工具发现和文档
实现
- MCP 服务(
disk_usage_server.py)使用 Python 的subprocess模块运行df命令 - 它专门查看
/System/Volumes/Data分区(macOS 上的 disk3s5) - 服务解析输出并返回结构化数据,包括:
- 磁盘总空间
- 已用空间
- 可用空间
- 预留空间(系统预留的空间)
- 使用率
- 挂载点和设备信息
要求
- macOS(此工具专为 Mac 的磁盘结构设计)
- Python 3.11+
- pip(Python 包管理器)
运行服务
-
克隆此仓库
-
安装依赖项:
pip install -r requirements.txt
- 运行服务:
python disk_usage_server.py
注意:此服务专门查找 macOS 上的 /System/Volumes/Data 分区(disk3s5)。它无法在其他操作系统上运行。
Claude Desktop 集成
- 在 Claude Desktop 配置目录中创建
claude-config.txt文件(macOS 上通常为~/.config/claude-desktop/),并写入:
{
"mcpServers": {
"disk-usage": {
"command": "python3",
"args": [
"<path-to-your-directory>/disk_usage_server.py"
]
}
}
}
-
将
<path-to-your-directory>替换为保存服务文件的实际路径 -
重启 Claude Desktop
学习成果
此项目展示了:
- 如何使用 Python 创建简单的 MCP 服务
- 如何将系统命令集成到 MCP 工具中
- 如何为 AI 使用整理数据
- 如何处理系统特定的细节(例如磁盘分区)
- 如何提供清晰易懂的技术数据摘要
文件
disk_usage_server.py:MCP 服务的主要实现requirements.txt:Python 依赖项claude-config.txt:Claude Desktop 配置示例
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。