-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinux-mcp.py
More file actions
396 lines (335 loc) · 14.2 KB
/
linux-mcp.py
File metadata and controls
396 lines (335 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env python3
"""
Linux System Diagnostics MCP Server
This MCP server provides tools to diagnose Linux system issues,
check configurations, and understand system behavior.
"""
import asyncio
import subprocess
import json
import os
import re
from typing import Any, Sequence
from mcp.server.models import InitializationOptions
import mcp.types as types
from mcp.server import NotificationOptions, Server
import mcp.server.stdio
# Initialize the MCP server
server = Server("linux-diagnostics")
@server.list_tools()
async def handle_list_tools() -> list[types.Tool]:
"""List available diagnostic tools"""
return [
types.Tool(
name="check_service_status",
description="Check the status of a systemd service and related logs",
inputSchema={
"type": "object",
"properties": {
"service_name": {
"type": "string",
"description": "Name of the systemd service to check"
},
"include_logs": {
"type": "boolean",
"description": "Include recent journal logs",
"default": True
}
},
"required": ["service_name"]
}
),
types.Tool(
name="check_port_usage",
description="Check what's using a specific port and related network info",
inputSchema={
"type": "object",
"properties": {
"port": {
"type": "integer",
"description": "Port number to check"
},
"protocol": {
"type": "string",
"enum": ["tcp", "udp", "both"],
"description": "Protocol to check",
"default": "both"
}
},
"required": ["port"]
}
),
types.Tool(
name="check_disk_space",
description="Check disk space usage and identify large files/directories",
inputSchema={
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Path to check (default: /)",
"default": "/"
},
"find_large_files": {
"type": "boolean",
"description": "Find largest files in the path",
"default": True
}
}
}
),
types.Tool(
name="check_process_info",
description="Get detailed information about running processes",
inputSchema={
"type": "object",
"properties": {
"process_name": {
"type": "string",
"description": "Process name or pattern to search for"
},
"include_children": {
"type": "boolean",
"description": "Include child processes",
"default": True
}
},
"required": ["process_name"]
}
),
types.Tool(
name="check_config_file",
description="Validate and analyze configuration files",
inputSchema={
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "Path to configuration file"
},
"config_type": {
"type": "string",
"enum": ["nginx", "apache", "ssh", "generic"],
"description": "Type of config file for specialized validation",
"default": "generic"
}
},
"required": ["file_path"]
}
),
types.Tool(
name="check_system_resources",
description="Check overall system resource usage (CPU, memory, I/O)",
inputSchema={
"type": "object",
"properties": {
"duration": {
"type": "integer",
"description": "How long to monitor in seconds",
"default": 5
}
}
}
),
types.Tool(
name="check_network_connectivity",
description="Test network connectivity and DNS resolution",
inputSchema={
"type": "object",
"properties": {
"target": {
"type": "string",
"description": "Target hostname or IP to test"
},
"test_type": {
"type": "string",
"enum": ["ping", "dns", "port", "trace"],
"description": "Type of network test",
"default": "ping"
},
"port": {
"type": "integer",
"description": "Port number (for port test)",
"default": 80
}
},
"required": ["target"]
}
)
]
async def run_command(cmd: str, timeout: int = 30) -> dict:
"""Execute a shell command safely and return results"""
try:
process = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await asyncio.wait_for(
process.communicate(),
timeout=timeout
)
return {
"returncode": process.returncode,
"stdout": stdout.decode('utf-8', errors='ignore'),
"stderr": stderr.decode('utf-8', errors='ignore')
}
except asyncio.TimeoutError:
return {
"returncode": -1,
"stdout": "",
"stderr": f"Command timed out after {timeout} seconds"
}
except Exception as e:
return {
"returncode": -1,
"stdout": "",
"stderr": f"Error executing command: {str(e)}"
}
@server.call_tool()
async def handle_call_tool(
name: str, arguments: dict | None
) -> list[types.TextContent]:
"""Handle tool calls"""
if name == "check_service_status":
service_name = arguments.get("service_name")
include_logs = arguments.get("include_logs", True)
# Check service status
status_result = await run_command(f"systemctl status {service_name}")
result = f"=== Service Status for {service_name} ===\n"
result += status_result["stdout"]
if status_result["stderr"]:
result += f"\nErrors:\n{status_result['stderr']}"
# Include recent logs if requested
if include_logs:
logs_result = await run_command(
f"journalctl -u {service_name} --no-pager -n 20"
)
result += f"\n\n=== Recent Logs ===\n{logs_result['stdout']}"
return [types.TextContent(type="text", text=result)]
elif name == "check_port_usage":
port = arguments.get("port")
protocol = arguments.get("protocol", "both")
result = f"=== Port {port} Usage ===\n"
if protocol in ["tcp", "both"]:
tcp_result = await run_command(f"netstat -tlnp | grep :{port}")
if tcp_result["stdout"]:
result += f"TCP connections:\n{tcp_result['stdout']}\n"
if protocol in ["udp", "both"]:
udp_result = await run_command(f"netstat -ulnp | grep :{port}")
if udp_result["stdout"]:
result += f"UDP connections:\n{udp_result['stdout']}\n"
# Also check with ss (modern alternative)
ss_result = await run_command(f"ss -tulpn | grep :{port}")
if ss_result["stdout"]:
result += f"\nDetailed socket info:\n{ss_result['stdout']}"
return [types.TextContent(type="text", text=result)]
elif name == "check_disk_space":
path = arguments.get("path", "/")
find_large_files = arguments.get("find_large_files", True)
result = f"=== Disk Space Analysis for {path} ===\n"
# Basic disk usage
df_result = await run_command(f"df -h {path}")
result += f"Filesystem usage:\n{df_result['stdout']}\n"
# Directory sizes
du_result = await run_command(f"du -h -d 1 {path} | sort -hr")
result += f"\nDirectory sizes:\n{du_result['stdout']}\n"
if find_large_files:
# Find largest files
large_files = await run_command(
f"find {path} -type f -size +100M -exec ls -lh {{}} \\; 2>/dev/null | head -10"
)
if large_files["stdout"]:
result += f"\nLargest files (>100MB):\n{large_files['stdout']}"
return [types.TextContent(type="text", text=result)]
elif name == "check_process_info":
process_name = arguments.get("process_name")
include_children = arguments.get("include_children", True)
result = f"=== Process Information for {process_name} ===\n"
# Find processes
ps_result = await run_command(f"ps aux | grep {process_name} | grep -v grep")
result += f"Running processes:\n{ps_result['stdout']}\n"
if include_children:
# Get process tree
pstree_result = await run_command(f"pstree -p | grep {process_name}")
if pstree_result["stdout"]:
result += f"\nProcess tree:\n{pstree_result['stdout']}\n"
# Memory and CPU usage
top_result = await run_command(f"top -b -n 1 | grep {process_name}")
if top_result["stdout"]:
result += f"\nResource usage:\n{top_result['stdout']}"
return [types.TextContent(type="text", text=result)]
elif name == "check_config_file":
file_path = arguments.get("file_path")
config_type = arguments.get("config_type", "generic")
result = f"=== Configuration Analysis: {file_path} ===\n"
# Check if file exists and permissions
stat_result = await run_command(f"ls -la {file_path}")
result += f"File info:\n{stat_result['stdout']}\n"
# Basic syntax check based on config type
if config_type == "nginx":
syntax_result = await run_command("nginx -t")
result += f"Nginx syntax check:\n{syntax_result['stdout']}\n{syntax_result['stderr']}\n"
elif config_type == "apache":
syntax_result = await run_command("apache2ctl configtest")
result += f"Apache syntax check:\n{syntax_result['stdout']}\n{syntax_result['stderr']}\n"
# Show file contents (first 50 lines)
content_result = await run_command(f"head -n 50 {file_path}")
result += f"\nFile contents (first 50 lines):\n{content_result['stdout']}"
return [types.TextContent(type="text", text=result)]
elif name == "check_system_resources":
duration = arguments.get("duration", 5)
result = "=== System Resource Check ===\n"
# CPU info
cpu_result = await run_command("top -b -n 1 | head -n 5")
result += f"CPU usage:\n{cpu_result['stdout']}\n"
# Memory info
mem_result = await run_command("free -h")
result += f"Memory usage:\n{mem_result['stdout']}\n"
# Load average
load_result = await run_command("uptime")
result += f"Load average:\n{load_result['stdout']}\n"
# I/O stats
iostat_result = await run_command("iostat -x 1 1")
result += f"I/O statistics:\n{iostat_result['stdout']}"
return [types.TextContent(type="text", text=result)]
elif name == "check_network_connectivity":
target = arguments.get("target")
test_type = arguments.get("test_type", "ping")
port = arguments.get("port", 80)
result = f"=== Network Connectivity Test: {target} ===\n"
if test_type == "ping":
ping_result = await run_command(f"ping -c 4 {target}")
result += f"Ping test:\n{ping_result['stdout']}\n{ping_result['stderr']}"
elif test_type == "dns":
dns_result = await run_command(f"nslookup {target}")
result += f"DNS lookup:\n{dns_result['stdout']}"
# Also try dig
dig_result = await run_command(f"dig {target}")
result += f"\nDetailed DNS info:\n{dig_result['stdout']}"
elif test_type == "port":
nc_result = await run_command(f"nc -zv {target} {port}", timeout=10)
result += f"Port {port} test:\n{nc_result['stdout']}\n{nc_result['stderr']}"
elif test_type == "trace":
trace_result = await run_command(f"traceroute {target}")
result += f"Traceroute:\n{trace_result['stdout']}"
return [types.TextContent(type="text", text=result)]
else:
raise ValueError(f"Unknown tool: {name}")
async def main():
# Run the server using stdin/stdout streams
async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):
await server.run(
read_stream,
write_stream,
InitializationOptions(
server_name="linux-diagnostics",
server_version="0.1.0",
capabilities=server.get_capabilities(
notification_options=NotificationOptions(),
experimental_capabilities={},
),
),
)
if __name__ == "__main__":
asyncio.run(main())