@wunderio/wdrmcp 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/LICENSE +185 -0
  2. package/README.md +141 -0
  3. package/dist/config.d.ts +6 -0
  4. package/dist/config.d.ts.map +1 -0
  5. package/dist/config.js +59 -0
  6. package/dist/config.js.map +1 -0
  7. package/dist/docker.d.ts +34 -0
  8. package/dist/docker.d.ts.map +1 -0
  9. package/dist/docker.js +115 -0
  10. package/dist/docker.js.map +1 -0
  11. package/dist/executors/base.d.ts +2 -0
  12. package/dist/executors/base.d.ts.map +1 -0
  13. package/dist/executors/base.js +4 -0
  14. package/dist/executors/base.js.map +1 -0
  15. package/dist/executors/command.d.ts +37 -0
  16. package/dist/executors/command.d.ts.map +1 -0
  17. package/dist/executors/command.js +107 -0
  18. package/dist/executors/command.js.map +1 -0
  19. package/dist/executors/mcp-proxy.d.ts +58 -0
  20. package/dist/executors/mcp-proxy.d.ts.map +1 -0
  21. package/dist/executors/mcp-proxy.js +152 -0
  22. package/dist/executors/mcp-proxy.js.map +1 -0
  23. package/dist/index.d.ts +14 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +46 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/logger.d.ts +18 -0
  28. package/dist/logger.d.ts.map +1 -0
  29. package/dist/logger.js +70 -0
  30. package/dist/logger.js.map +1 -0
  31. package/dist/registry.d.ts +42 -0
  32. package/dist/registry.d.ts.map +1 -0
  33. package/dist/registry.js +240 -0
  34. package/dist/registry.js.map +1 -0
  35. package/dist/server.d.ts +12 -0
  36. package/dist/server.d.ts.map +1 -0
  37. package/dist/server.js +54 -0
  38. package/dist/server.js.map +1 -0
  39. package/dist/types.d.ts +98 -0
  40. package/dist/types.d.ts.map +1 -0
  41. package/dist/types.js +5 -0
  42. package/dist/types.js.map +1 -0
  43. package/package.json +51 -0
package/LICENSE ADDED
@@ -0,0 +1,185 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined in Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity exercising
24
+ permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or Object
36
+ form, made available under the License, as indicated by a copyright
37
+ notice that is included in or attached to the work (an example is
38
+ provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original Work and any Derivative Works thereof, and any modifications
50
+ thereto or additions to the Work or Derivative Works thereof, and any
51
+ larger work that includes the Work or such Derivative Works, submitted
52
+ to, or arising from contributions by, You to Licensor, including but
53
+ not limited to any communication that is distinctly identified as such
54
+ (including without limitation source code, object code, patches, and
55
+ documentation created or produced by You or based on the Work).
56
+
57
+ "Contributor" shall mean Licensor and any Legal Entity on behalf of
58
+ whom a Contribution has been received by Licensor and subsequently
59
+ incorporated within the Work.
60
+
61
+ "Patent Claims" of a Contributor shall mean the set of patent claims
62
+ owned or controlled by such Contributor, whether already acquired or
63
+ hereafter acquired, that would be infringed by some manner, permitted
64
+ by this License, of making, using, or selling its Contributions.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those Patent Claims of such
79
+ Contributor that are necessarily infringed by Contribution(s) that
80
+ would not have infringed the Patent Claims but for the combination of
81
+ such Contribution(s) with the Work to which such Contribution(s) were
82
+ submitted.
83
+
84
+ 4. Redistribution. You may reproduce and distribute copies of the
85
+ Work or Derivative Works thereof in any medium, with or without
86
+ modifications, and in Source or Object form, provided that You
87
+ meet the following conditions:
88
+
89
+ (a) You must give any other recipients of the Work or
90
+ Derivative Works a copy of this License; and
91
+
92
+ (b) You must cause any modified files to carry prominent notices
93
+ stating that You changed the files; and
94
+
95
+ (c) You must retain, in the Source form of any Derivative Works
96
+ that You distribute, all copyright, patent, trademark, and
97
+ attribution notices from the Source form of the Work,
98
+ excluding those notices that do not pertain to any part of
99
+ the Derivative Works; and
100
+
101
+ (d) If the Work includes a "NOTICE" text file, then any
102
+ Derivative Works that You distribute must include a readable
103
+ copy of the attribution notices contained
104
+ within such NOTICE file, excluding those notices that do not
105
+ pertain to any part of the Derivative Works, in at least one
106
+ of the following places: within a NOTICE text file distributed
107
+ as part of the Derivative Works; within the Source form or
108
+ documentation, if provided along with the Derivative Works; or,
109
+ within a display generated by the Derivative Works, if and
110
+ wherever such third-party notices normally appear. The contents
111
+ of the NOTICE file are for informational purposes only and
112
+ do not modify the License. You may add Your own attribution
113
+ notices within Derivative Works that You distribute, alongside
114
+ or as an addendum to the NOTICE from the Work, provided that
115
+ such additional attribution notices cannot be construed as
116
+ modifying the License.
117
+
118
+ You may add Your own copyright statement to Your modifications and
119
+ may provide additional or different license terms and conditions
120
+ for use, reproduction, or distribution of Your modifications, or
121
+ for any such Derivative Works as a whole, provided Your use,
122
+ reproduction, and distribution of the Work otherwise complies with
123
+ the conditions of this License.
124
+
125
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
126
+ any Contribution intentionally submitted for inclusion in the Work
127
+ by You to Licensor shall be under the terms and conditions of
128
+ this License, without any additional terms or conditions.
129
+ Notwithstanding the above, nothing herein shall supersede or modify
130
+ the terms of any separate license agreement you may have executed
131
+ with Licensor regarding such Contribution.
132
+
133
+ 6. Trademarks. This License does not grant permission to use the trade
134
+ names, trademarks, service marks, or product names of the Licensor,
135
+ except as required for reasonable and customary use in describing the
136
+ origin of the Work and reproducing the content of the NOTICE file.
137
+
138
+ 7. Disclaimer of Warranty. Unless required by applicable law or
139
+ agreed to in writing, Licensor provides the Work (and each
140
+ Contributor provides its Contributions) on an "AS IS" BASIS,
141
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
142
+ implied, including, without limitation, any warranties or conditions
143
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
144
+ PARTICULAR PURPOSE. You are solely responsible for determining the
145
+ appropriateness of using or redistributing the Work and assume any
146
+ risks associated with Your exercise of permissions under this License.
147
+
148
+ 8. Limitation of Liability. In no event and under no legal theory,
149
+ whether in tort (including negligence), contract, or otherwise,
150
+ unless required by applicable law (such as deliberate and grossly
151
+ negligent acts) or agreed to in writing, shall any Contributor be
152
+ liable to You for damages, including any direct, indirect, special,
153
+ incidental, or consequential damages of any character arising as a
154
+ result of this License or out of the use or inability to use the
155
+ Work (including but not limited to damages for loss of goodwill,
156
+ work stoppage, computer failure or malfunction, or any and all
157
+ other commercial damages or losses), even if such Contributor
158
+ has been advised of the possibility of such damages.
159
+
160
+ 9. Accepting Warranty or Additional Liability. While redistributing
161
+ the Work or Derivative Works thereof, You may choose to offer,
162
+ and charge a fee for, acceptance of support, warranty, indemnity,
163
+ or other liability obligations and/or rights consistent with this
164
+ License. However, in accepting such obligations, You may act only
165
+ on Your own behalf and on Your sole responsibility, not on behalf
166
+ of any other Contributor, and only if You agree to indemnify,
167
+ defend, and hold each Contributor harmless for any liability
168
+ incurred by, or claims asserted against, such Contributor by reason
169
+ of your accepting any such warranty or additional liability.
170
+
171
+ END OF TERMS AND CONDITIONS
172
+
173
+ Copyright 2024 Wunder
174
+
175
+ Licensed under the Apache License, Version 2.0 (the "License");
176
+ you may not use this file except in compliance with the License.
177
+ You may obtain a copy of the License at
178
+
179
+ http://www.apache.org/licenses/LICENSE-2.0
180
+
181
+ Unless required by applicable law or agreed to in writing, software
182
+ distributed under the License is distributed on an "AS IS" BASIS,
183
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
184
+ See the License for the specific language governing permissions and
185
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,141 @@
1
+ # @wunderio/wdrmcp
2
+
3
+ A generic MCP (Model Context Protocol) server that dynamically loads tool definitions from YAML configuration files and executes them in Docker containers or proxies them to remote MCP servers.
4
+
5
+ Built with the [official MCP TypeScript SDK](https://github.com/modelcontextprotocol/typescript-sdk).
6
+
7
+ ## Features
8
+
9
+ - **YAML-driven tool definitions** — define tools declaratively, no code changes needed
10
+ - **Docker container execution** — run commands in Docker containers with security validation
11
+ - **MCP server proxying** — proxy tool calls to remote MCP servers via HTTP JSON-RPC
12
+ - **Dynamic tool discovery** — automatically fetch and expose tools from remote MCP servers
13
+ - **Security** — container ownership validation, command injection prevention, argument validation
14
+ - **DDEV integration** — built-in support for DDEV container naming and project scoping
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ npm install -g @wunderio/wdrmcp
20
+ ```
21
+
22
+ Or use directly with npx:
23
+
24
+ ```bash
25
+ npx @wunderio/wdrmcp --tools-config /path/to/tools-config
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ### CLI
31
+
32
+ ```bash
33
+ wdrmcp --tools-config /path/to/tools-config [options]
34
+ ```
35
+
36
+ **Options:**
37
+
38
+ | Flag | Description | Default |
39
+ |------|-------------|---------|
40
+ | `--tools-config <path>` | Path to YAML tool config directory | *(required)* |
41
+ | `--log-level <level>` | Log level (debug, info, warn, error) | `info` |
42
+ | `--log-file <path>` | Log file path | `/tmp/wdrmcp.log` |
43
+
44
+ **Environment variables:**
45
+
46
+ | Variable | Description | Default |
47
+ |----------|-------------|---------|
48
+ | `DDEV_PROJECT` | DDEV project name | `default-project` |
49
+ | `HOST_PROJECT_ROOT` | Host filesystem project root | `/workspace` |
50
+ | `CONTAINER_PROJECT_ROOT` | Container filesystem project root | `/var/www/html` |
51
+
52
+ ### VS Code / GitHub Copilot Configuration
53
+
54
+ ```json
55
+ {
56
+ "servers": {
57
+ "wdrmcp": {
58
+ "command": "npx",
59
+ "args": ["-y", "@wunderio/wdrmcp", "--tools-config", "/path/to/tools-config"],
60
+ "env": {
61
+ "DDEV_PROJECT": "my-project"
62
+ }
63
+ }
64
+ }
65
+ }
66
+ ```
67
+
68
+ ## Tool Configuration Format
69
+
70
+ Tools are defined in YAML files within the tools-config directory. Each file must contain a `tools` array.
71
+
72
+ ### Command Tool
73
+
74
+ Executes shell commands in Docker containers:
75
+
76
+ ```yaml
77
+ tools:
78
+ - name: my_tool
79
+ enabled: true
80
+ description: "What this tool does"
81
+ type: command
82
+ command_template: "my-command {arg1} {arg2}"
83
+ container: "ddev-{DDEV_PROJECT}-web"
84
+ user: "www-data"
85
+ shell: "/bin/bash"
86
+ default_args:
87
+ arg2: "default-value"
88
+ disallowed_commands:
89
+ - "rm"
90
+ - "shutdown"
91
+ validation_rules:
92
+ - pattern: "dangerous-pattern"
93
+ message: "This pattern is not allowed"
94
+ input_schema:
95
+ type: object
96
+ properties:
97
+ arg1:
98
+ type: string
99
+ description: "First argument"
100
+ required:
101
+ - arg1
102
+ ```
103
+
104
+ ### MCP Server Proxy Tool
105
+
106
+ Proxies tool calls to a remote MCP server:
107
+
108
+ ```yaml
109
+ tools:
110
+ - name: remote_tools
111
+ enabled: true
112
+ description: "Remote MCP server"
113
+ type: mcp_server
114
+ server_url: "http://localhost:8080"
115
+ expose_remote_tools: true
116
+ tool_prefix: "remote_"
117
+ timeout: 30
118
+ auth_token: "my-token"
119
+ verify_ssl: true
120
+ ```
121
+
122
+ ## Architecture
123
+
124
+ ```
125
+ src/
126
+ ├── index.ts # CLI entry point
127
+ ├── server.ts # MCP server setup (tool registration with Zod schemas)
128
+ ├── registry.ts # Tool registry (YAML loading, executor creation)
129
+ ├── executors/
130
+ │ ├── base.ts # Base executor interface
131
+ │ ├── command.ts # Docker container command executor
132
+ │ └── mcp-proxy.ts # Remote MCP server proxy executor
133
+ ├── docker.ts # Docker exec & container validation
134
+ ├── config.ts # CLI argument parsing
135
+ ├── logger.ts # Stderr-only logger
136
+ └── types.ts # TypeScript type definitions
137
+ ```
138
+
139
+ ## License
140
+
141
+ MIT
@@ -0,0 +1,6 @@
1
+ /**
2
+ * CLI argument parsing and configuration loading.
3
+ */
4
+ import type { BridgeConfig } from "./types.js";
5
+ export declare function parseArgs(argv: string[]): BridgeConfig;
6
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAmB/C,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,YAAY,CA2CtD"}
package/dist/config.js ADDED
@@ -0,0 +1,59 @@
1
+ /**
2
+ * CLI argument parsing and configuration loading.
3
+ */
4
+ function printUsage() {
5
+ console.error(`
6
+ Usage: wdrmcp --tools-config <path> [options]
7
+
8
+ Options:
9
+ --tools-config <path> Path to directory containing YAML tool configuration files (required)
10
+ --log-level <level> Log level: debug, info, warn, error (default: info)
11
+ --log-file <path> Path to log file (default: /tmp/wdrmcp.log)
12
+ --help Show this help message
13
+
14
+ Environment variables:
15
+ DDEV_PROJECT DDEV project name (default: "default-project")
16
+ HOST_PROJECT_ROOT Host filesystem project root (default: /workspace)
17
+ CONTAINER_PROJECT_ROOT Container filesystem project root (default: /var/www/html)
18
+ `);
19
+ }
20
+ export function parseArgs(argv) {
21
+ const args = argv.slice(2); // Skip node and script path.
22
+ let toolsConfigPath;
23
+ let logLevel = "info";
24
+ let logFile = "/tmp/wdrmcp.log";
25
+ for (let i = 0; i < args.length; i++) {
26
+ switch (args[i]) {
27
+ case "--tools-config":
28
+ toolsConfigPath = args[++i];
29
+ break;
30
+ case "--log-level":
31
+ logLevel = args[++i];
32
+ break;
33
+ case "--log-file":
34
+ logFile = args[++i];
35
+ break;
36
+ case "--help":
37
+ printUsage();
38
+ process.exit(0);
39
+ default:
40
+ console.error(`Unknown argument: ${args[i]}`);
41
+ printUsage();
42
+ process.exit(1);
43
+ }
44
+ }
45
+ if (!toolsConfigPath) {
46
+ console.error("Error: --tools-config is required");
47
+ printUsage();
48
+ process.exit(1);
49
+ }
50
+ return {
51
+ toolsConfigPath,
52
+ ddevProject: process.env.DDEV_PROJECT ?? "default-project",
53
+ logLevel,
54
+ logFile,
55
+ hostProjectRoot: process.env.HOST_PROJECT_ROOT ?? "/workspace",
56
+ containerProjectRoot: process.env.CONTAINER_PROJECT_ROOT ?? "/var/www/html",
57
+ };
58
+ }
59
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/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AW,EAAE,OAAO,CAAC,GAAG,CAAC,YAAY,IAAI,iBAAiB;QAC1D,QAAQ;QACR,OAAO;QACP,eAAe,EAAE,OAAO,CAAC,GAAG,CAAC,iBAAiB,IAAI,YAAY;QAC9D,oBAAoB,EAClB,OAAO,CAAC,GAAG,CAAC,sBAAsB,IAAI,eAAe;KACxD,CAAC;AACJ,CAAC"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Docker executor — runs commands in Docker containers with security validation.
3
+ *
4
+ * Container ownership is validated once and cached. UID resolution is cached.
5
+ * Static safety checks (shell name, etc.) happen at construction time.
6
+ */
7
+ export interface DockerExecOptions {
8
+ container: string;
9
+ command: string[];
10
+ user?: string;
11
+ shell?: string;
12
+ }
13
+ /**
14
+ * Validate that a container belongs to the expected DDEV project.
15
+ * Result is cached — subsequent calls for the same container return immediately.
16
+ */
17
+ export declare function validateContainer(container: string, ddevProject: string): Promise<void>;
18
+ /**
19
+ * Validate that static values don't contain dangerous shell characters.
20
+ * Call once at construction time, not per-execution.
21
+ */
22
+ export declare function validateStaticSafety(values: string[]): void;
23
+ /**
24
+ * Resolve `auto:uid-from-path` user syntax to an actual UID.
25
+ * Cached per container+path combination.
26
+ *
27
+ * Format: "auto:uid-from-path" or "auto:uid-from-path:/var/www/html"
28
+ */
29
+ export declare function resolveContainerUser(user: string, container: string): Promise<string>;
30
+ /**
31
+ * Execute a command inside a Docker container.
32
+ */
33
+ export declare function dockerExec(options: DockerExecOptions): Promise<string>;
34
+ //# sourceMappingURL=docker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"docker.d.ts","sourceRoot":"","sources":["../src/docker.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAcH,MAAM,WAAW,iBAAiB;IAChC,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;GAGG;AACH,wBAAsB,iBAAiB,CACrC,SAAS,EAAE,MAAM,EACjB,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,IAAI,CAAC,CA0Bf;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAU3D;AAED;;;;;GAKG;AACH,wBAAsB,oBAAoB,CACxC,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,MAAM,CAAC,CA8BjB;AAED;;GAEG;AACH,wBAAsB,UAAU,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,MAAM,CAAC,CAQ5E"}
package/dist/docker.js ADDED
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Docker executor — runs commands in Docker containers with security validation.
3
+ *
4
+ * Container ownership is validated once and cached. UID resolution is cached.
5
+ * Static safety checks (shell name, etc.) happen at construction time.
6
+ */
7
+ import { execFile } from "node:child_process";
8
+ import { getLogger } from "./logger.js";
9
+ /** Characters considered dangerous in command arguments. */
10
+ const DANGEROUS_CHARS = [";", "|", "&", ">", "<", "$", "`", "\n", "\r"];
11
+ /** Cache of validated containers: "container:project" → true. */
12
+ const validatedContainers = new Map();
13
+ /** Cache of resolved UIDs: "container:path" → uid string. */
14
+ const uidCache = new Map();
15
+ /**
16
+ * Validate that a container belongs to the expected DDEV project.
17
+ * Result is cached — subsequent calls for the same container return immediately.
18
+ */
19
+ export async function validateContainer(container, ddevProject) {
20
+ const cacheKey = `${container}:${ddevProject}`;
21
+ if (validatedContainers.has(cacheKey))
22
+ return;
23
+ const log = getLogger();
24
+ log.debug(`Validating container "${container}" for project "${ddevProject}"`);
25
+ try {
26
+ const format = `{{index .Config.Labels "com.ddev.site-name"}}`;
27
+ const labelValue = (await execCommand("docker", ["inspect", "--format", format, container])).trim();
28
+ if (ddevProject !== "default-project" && labelValue !== ddevProject) {
29
+ throw new Error(`Container "${container}" belongs to "${labelValue}", not "${ddevProject}"`);
30
+ }
31
+ }
32
+ catch (e) {
33
+ if (e.message.includes("belongs to"))
34
+ throw e;
35
+ // Container doesn't exist yet or docker inspect failed — warn but allow.
36
+ log.warn(`Container validation warning: ${e.message}`);
37
+ }
38
+ validatedContainers.set(cacheKey, true);
39
+ log.debug(`Container "${container}" validated (cached)`);
40
+ }
41
+ /**
42
+ * Validate that static values don't contain dangerous shell characters.
43
+ * Call once at construction time, not per-execution.
44
+ */
45
+ export function validateStaticSafety(values) {
46
+ for (const val of values) {
47
+ for (const char of DANGEROUS_CHARS) {
48
+ if (val.includes(char)) {
49
+ throw new Error(`Dangerous character "${char}" in static value: "${val}"`);
50
+ }
51
+ }
52
+ }
53
+ }
54
+ /**
55
+ * Resolve `auto:uid-from-path` user syntax to an actual UID.
56
+ * Cached per container+path combination.
57
+ *
58
+ * Format: "auto:uid-from-path" or "auto:uid-from-path:/var/www/html"
59
+ */
60
+ export async function resolveContainerUser(user, container) {
61
+ if (!user.startsWith("auto:uid-from-path"))
62
+ return user;
63
+ const parts = user.split(":");
64
+ const targetPath = parts.length > 2 ? parts[2] : "/var/www/html";
65
+ const cacheKey = `${container}:${targetPath}`;
66
+ const cached = uidCache.get(cacheKey);
67
+ if (cached)
68
+ return cached;
69
+ const log = getLogger();
70
+ try {
71
+ const result = await dockerExec({
72
+ container,
73
+ command: [`stat -c %u ${targetPath}`],
74
+ user: "root",
75
+ shell: "/bin/sh",
76
+ });
77
+ const uid = result.trim();
78
+ if (uid) {
79
+ log.info(`Resolved auto:uid-from-path:${targetPath} → UID ${uid} (cached)`);
80
+ uidCache.set(cacheKey, uid);
81
+ return uid;
82
+ }
83
+ }
84
+ catch (e) {
85
+ log.warn(`Error resolving UID: ${e}. Falling back to www-data`);
86
+ }
87
+ uidCache.set(cacheKey, "www-data");
88
+ return "www-data";
89
+ }
90
+ /**
91
+ * Execute a command inside a Docker container.
92
+ */
93
+ export async function dockerExec(options) {
94
+ const { container, command, user, shell = "/bin/bash" } = options;
95
+ const dockerArgs = ["exec"];
96
+ if (user)
97
+ dockerArgs.push("-u", user);
98
+ dockerArgs.push(container, shell, "-c", command.join(" "));
99
+ return execCommand("docker", dockerArgs);
100
+ }
101
+ /**
102
+ * Async wrapper around child_process.execFile.
103
+ */
104
+ function execCommand(cmd, args) {
105
+ return new Promise((resolve, reject) => {
106
+ execFile(cmd, args, { maxBuffer: 10 * 1024 * 1024, timeout: 120_000 }, (error, stdout, stderr) => {
107
+ if (error) {
108
+ reject(new Error(`Command failed: ${cmd} ${args.join(" ")}\n${stderr?.trim() || error.message}`));
109
+ return;
110
+ }
111
+ resolve(stdout);
112
+ });
113
+ });
114
+ }
115
+ //# sourceMappingURL=docker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"docker.js","sourceRoot":"","sources":["../src/docker.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,4DAA4D;AAC5D,MAAM,eAAe,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AAExE,iEAAiE;AACjE,MAAM,mBAAmB,GAAG,IAAI,GAAG,EAAmB,CAAC;AAEvD,6DAA6D;AAC7D,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAC;AAS3C;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,SAAiB,EACjB,WAAmB;IAEnB,MAAM,QAAQ,GAAG,GAAG,SAAS,IAAI,WAAW,EAAE,CAAC;IAC/C,IAAI,mBAAmB,CAAC,GAAG,CAAC,QAAQ,CAAC;QAAE,OAAO;IAE9C,MAAM,GAAG,GAAG,SAAS,EAAE,CAAC;IACxB,GAAG,CAAC,KAAK,CAAC,yBAAyB,SAAS,kBAAkB,WAAW,GAAG,CAAC,C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E,MAAM,UAAU,GAAG,CAAC,MAAM,CAAC,CAAC;IAC5B,IAAI,IAAI;QAAE,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IACtC,UAAU,CAAC,IAAI,CAAC,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IAE3D,OAAO,WAAW,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;AAC3C,CAAC;AAED;;GAEG;AACH,SAAS,WAAW,CAAC,GAAW,EAAE,IAAc;IAC9C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,QAAQ,CACN,GAAG,EACH,IAAI,EACJ,EAAE,SAAS,EAAE,EAAE,GAAG,IAAI,GAAG,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,EACjD,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE;YACxB,IAAI,KAAK,EAAE,CAAC;gBACV,MAAM,CACJ,IAAI,KAAK,CACP,mBAAmB,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,MAAM,EAAE,IAAI,EAAE,IAAI,KAAK,CAAC,OAAO,EAAE,CAC/E,CACF,CAAC;gBACF,OAAO;YACT,CAAC;YACD,OAAO,CAAC,MAAM,CAAC,CAAC;QAClB,CAAC,CACF,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=base.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/executors/base.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ export {};
2
+ // This file is intentionally empty — the abstract base class has been removed.
3
+ // Executors now implement the ToolExecutor interface directly from types.ts.
4
+ //# sourceMappingURL=base.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/executors/base.ts"],"names":[],"mappings":";AAAA,+EAA+E;AAC/E,6EAA6E"}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * CommandToolExecutor — executes shell commands in Docker containers
3
+ * with argument substitution ({placeholder} syntax).
4
+ *
5
+ * Refactored from the Python version with these improvements:
6
+ * - Static safety checks happen at construction time (not per-call)
7
+ * - Container validation and UID resolution are cached in docker.ts
8
+ * - Path normalization is an external preprocessor (not baked in)
9
+ * - No abstract base class; implements ToolExecutor interface directly
10
+ */
11
+ import type { ToolExecutionResult, ToolExecutor, ValidationRule } from "../types.js";
12
+ export interface CommandExecutorOptions {
13
+ commandTemplate: string;
14
+ container: string;
15
+ ddevProject: string;
16
+ user?: string;
17
+ shell?: string;
18
+ defaultArgs?: Record<string, string>;
19
+ disallowedCommands?: string[];
20
+ validationRules?: ValidationRule[];
21
+ }
22
+ export declare class CommandToolExecutor implements ToolExecutor {
23
+ private readonly commandTemplate;
24
+ private readonly container;
25
+ private readonly ddevProject;
26
+ private readonly user;
27
+ private readonly shell;
28
+ private readonly defaultArgs;
29
+ private readonly disallowedCommands;
30
+ private readonly validationRules;
31
+ constructor(options: CommandExecutorOptions);
32
+ execute(args: Record<string, unknown>): Promise<ToolExecutionResult>;
33
+ validateArguments(args: Record<string, unknown>): void;
34
+ /** Check validation rules against a value. Returns error message or null. */
35
+ private checkRules;
36
+ }
37
+ //# sourceMappingURL=command.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command.d.ts","sourceRoot":"","sources":["../../src/executors/command.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AASH,OAAO,KAAK,EAAE,mBAAmB,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAErF,MAAM,WAAW,sBAAsB;IACrC,eAAe,EAAE,MAAM,CAAC;IACxB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrC,kBAAkB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC9B,eAAe,CAAC,EAAE,cAAc,EAAE,CAAC;CACpC;AAED,qBAAa,mBAAoB,YAAW,YAAY;IACtD,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAS;IACzC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAC/B,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAyB;IACrD,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAc;IACjD,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAmB;gBAEvC,OAAO,EAAE,sBAAsB;IAcrC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,mBAAmB,CAAC;IAsD1E,iBAAiB,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI;IAiBtD,6EAA6E;IAC7E,OAAO,CAAC,UAAU;CAQnB"}