-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.ts
More file actions
68 lines (60 loc) · 1.62 KB
/
exec.ts
File metadata and controls
68 lines (60 loc) · 1.62 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
import { z } from "zod";
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { execDocker, errorResponse } from "../utils/docker-api.js";
const inputSchema = {
containerId: z
.string()
.min(1)
.describe("Container ID or name to execute the command in"),
command: z
.array(z.string())
.min(1)
.describe("Command and arguments to execute (e.g. ['ls', '-la', '/app'])"),
workdir: z
.string()
.optional()
.describe("Working directory inside the container"),
user: z
.string()
.optional()
.describe("Username or UID to run the command as (e.g. 'root', '1000')"),
env: z
.array(z.string())
.optional()
.describe("Environment variables as 'KEY=VALUE' for the exec session"),
};
export function register(server: McpServer): void {
server.tool(
"docker_exec",
"Execute a command in a running container and return its output",
inputSchema,
async (args) => {
try {
const cmdArgs = ["exec"];
if (args.workdir) {
cmdArgs.push("-w", args.workdir);
}
if (args.user) {
cmdArgs.push("-u", args.user);
}
if (args.env) {
for (const e of args.env) {
cmdArgs.push("-e", e);
}
}
cmdArgs.push(args.containerId, ...args.command);
const output = await execDocker(cmdArgs);
return {
content: [
{
type: "text" as const,
text: output || "(no output)",
},
],
};
} catch (error) {
return errorResponse(error);
}
},
);
}