Files
ds_mcp_template/build/lib/mcp_template/tools/system_tools.py
T

158 lines
5.8 KiB
Python
Raw Normal View History

2025-09-11 23:13:58 +01:00
"""
System Tools for MCP
"""
import os
import platform
import psutil
from datetime import datetime
from typing import Dict, Any, List
from ..core.types import MCPTool
class SystemTools:
"""Collection of system-related tools"""
@staticmethod
def get_tools() -> List[MCPTool]:
"""Get all system tools"""
return [
SystemTools._create_get_system_info_tool(),
SystemTools._create_get_current_time_tool(),
SystemTools._create_list_directory_tool(),
SystemTools._create_get_file_info_tool(),
SystemTools._create_get_environment_variable_tool(),
]
@staticmethod
def _create_get_system_info_tool() -> MCPTool:
"""Create system info tool"""
async def get_system_info() -> Dict[str, Any]:
"""Get basic system information"""
try:
return {
"platform": platform.system(),
"platform_version": platform.version(),
"architecture": platform.machine(),
"processor": platform.processor(),
"python_version": platform.python_version(),
"cpu_count": os.cpu_count(),
"memory_total": psutil.virtual_memory().total if psutil else "psutil not available",
"memory_available": psutil.virtual_memory().available if psutil else "psutil not available",
}
except Exception as e:
return {"error": f"Could not retrieve system info: {str(e)}"}
return MCPTool(
name="get_system_info",
description="Get basic system information including OS, CPU, and memory details",
input_schema={"type": "object", "properties": {}},
handler=get_system_info,
)
@staticmethod
def _create_get_current_time_tool() -> MCPTool:
"""Create current time tool"""
async def get_current_time() -> str:
"""Get the current date and time"""
now = datetime.now()
return now.strftime("%Y-%m-%d %H:%M:%S")
return MCPTool(
name="get_current_time",
description="Get the current date and time in YYYY-MM-DD HH:MM:SS format",
input_schema={"type": "object", "properties": {}},
handler=get_current_time,
)
@staticmethod
def _create_list_directory_tool() -> MCPTool:
"""Create directory listing tool"""
async def list_directory(path: str = ".") -> List[str]:
"""List contents of a directory"""
try:
if not os.path.exists(path):
raise ValueError(f"Path does not exist: {path}")
if not os.path.isdir(path):
raise ValueError(f"Path is not a directory: {path}")
return os.listdir(path)
except Exception as e:
raise ValueError(f"Could not list directory: {str(e)}")
return MCPTool(
name="list_directory",
description="List the contents of a directory",
input_schema={
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Directory path to list",
"default": "."
},
},
},
handler=list_directory,
)
@staticmethod
def _create_get_file_info_tool() -> MCPTool:
"""Create file info tool"""
async def get_file_info(file_path: str) -> Dict[str, Any]:
"""Get information about a file"""
try:
if not os.path.exists(file_path):
raise ValueError(f"File does not exist: {file_path}")
stat = os.stat(file_path)
return {
"name": os.path.basename(file_path),
"path": os.path.abspath(file_path),
"size": stat.st_size,
"is_file": os.path.isfile(file_path),
"is_directory": os.path.isdir(file_path),
"modified_time": datetime.fromtimestamp(stat.st_mtime).isoformat(),
"created_time": datetime.fromtimestamp(stat.st_ctime).isoformat(),
}
except Exception as e:
raise ValueError(f"Could not get file info: {str(e)}")
return MCPTool(
name="get_file_info",
description="Get detailed information about a file or directory",
input_schema={
"type": "object",
"properties": {
"file_path": {"type": "string", "description": "Path to the file or directory"},
},
"required": ["file_path"],
},
handler=get_file_info,
)
@staticmethod
def _create_get_environment_variable_tool() -> MCPTool:
"""Create environment variable tool"""
async def get_environment_variable(name: str, default_value: str = "") -> str:
"""Get the value of an environment variable"""
return os.getenv(name, default_value)
return MCPTool(
name="get_environment_variable",
description="Get the value of an environment variable",
input_schema={
"type": "object",
"properties": {
"name": {"type": "string", "description": "Name of the environment variable"},
"default_value": {
"type": "string",
"description": "Default value if variable is not set",
"default": ""
},
},
"required": ["name"],
},
handler=get_environment_variable,
)