docker_mcp 0.2.5 → 0.3.0

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.
@@ -1,49 +1,52 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DockerMCP
4
- # MCP tool for creating Docker containers without starting them.
4
+ # MCP tool for creating Docker containers.
5
5
  #
6
- # This tool provides the ability to create Docker containers from images with
7
- # comprehensive configuration options. Unlike RunContainer, this tool only
8
- # creates the container but does not start it, allowing for additional
9
- # configuration or manual startup control.
6
+ # This tool creates a new Docker container from a specified image without
7
+ # starting it. The container is created in a "created" state and can be
8
+ # started later using the start_container tool. This two-step process
9
+ # allows for container configuration before execution.
10
10
  #
11
11
  # == Features
12
12
  #
13
- # - Create containers from any available Docker image
14
- # - Full container configuration support
15
- # - Port exposure and binding configuration
16
- # - Volume mounting capabilities
17
- # - Environment variable configuration
18
- # - Custom command specification
19
- # - Comprehensive error handling with specific error types
13
+ # - Creates containers from any available Docker image
14
+ # - Supports custom container naming
15
+ # - Configures command execution and environment variables
16
+ # - Sets up port exposure and network configuration
17
+ # - Applies advanced host configurations
18
+ # - Handles container labeling and metadata
20
19
  #
21
20
  # == Security Considerations
22
21
  #
23
- # - Containers inherit Docker daemon security context
24
- # - Host configuration can expose host resources
25
- # - Volume mounts provide filesystem access
26
- # - Port bindings expose services to networks
27
- # - Environment variables may contain sensitive data
22
+ # Container creation is a powerful operation that can:
23
+ # - **Resource Allocation**: Consume system resources and storage
24
+ # - **Network Access**: Create network endpoints and bindings
25
+ # - **File System Access**: Mount host directories and volumes
26
+ # - **Security Context**: Run with elevated privileges if configured
28
27
  #
29
- # Use appropriate security measures:
30
- # - Validate image sources and integrity
31
- # - Limit host resource exposure
32
- # - Use read-only volumes where appropriate
33
- # - Restrict network access through host configuration
34
- # - Sanitize environment variables
28
+ # Implement proper access controls and resource limits.
29
+ #
30
+ # == Parameters
31
+ #
32
+ # - **image**: Docker image to use (required)
33
+ # - **name**: Custom container name (optional)
34
+ # - **cmd**: Command to execute as space-separated string (optional)
35
+ # - **env**: Environment variables as comma-separated KEY=VALUE pairs (optional)
36
+ # - **exposed_ports**: Port exposure configuration as JSON object (optional)
37
+ # - **host_config**: Advanced host configuration as JSON object (optional)
35
38
  #
36
39
  # == Example Usage
37
40
  #
38
41
  # # Simple container creation
39
- # CreateContainer.call(
42
+ # response = CreateContainer.call(
40
43
  # server_context: context,
41
44
  # image: "nginx:latest",
42
45
  # name: "web-server"
43
46
  # )
44
47
  #
45
48
  # # Advanced container with configuration
46
- # CreateContainer.call(
49
+ # response = CreateContainer.call(
47
50
  # server_context: context,
48
51
  # image: "postgres:13",
49
52
  # name: "database",
@@ -55,86 +58,41 @@ module DockerMCP
55
58
  # }
56
59
  # )
57
60
  #
58
- # @see RunContainer
59
- # @see StartContainer
60
61
  # @see Docker::Container.create
61
62
  # @since 0.1.0
62
- class CreateContainer < MCP::Tool
63
+ CREATE_CONTAINER_DEFINITION = ToolForge.define(:create_container) do
63
64
  description 'Create a Docker container'
64
65
 
65
- input_schema(
66
- properties: {
67
- image: {
68
- type: 'string',
66
+ param :image,
67
+ type: :string,
69
68
  description: 'Image name to use (e.g., "ubuntu:22.04")'
70
- },
71
- name: {
72
- type: 'string',
73
- description: 'Container name (optional)'
74
- },
75
- cmd: {
76
- type: 'string',
77
- description: 'Command to run as space-separated string (optional, e.g., "npm start" or "python app.py")'
78
- },
79
- env: {
80
- type: 'string',
81
- description: 'Environment variables as comma-separated KEY=VALUE pairs (optional)'
82
- },
83
- exposed_ports: {
84
- type: 'object',
85
- description: 'Exposed ports as {"port/protocol": {}} (optional)'
86
- },
87
- host_config: {
88
- type: 'object',
89
- description: 'Host configuration including port bindings, volumes, etc. (optional)'
90
- }
91
- },
92
- required: ['image']
93
- )
94
69
 
95
- # Create a new Docker container from an image.
96
- #
97
- # This method creates a container with the specified configuration but does
98
- # not start it. The container can be started later using StartContainer or
99
- # other Docker commands. All configuration parameters are optional except
100
- # for the base image.
101
- #
102
- # @param image [String] Docker image name with optional tag (e.g., "nginx:latest")
103
- # @param server_context [Object] MCP server context (unused but required)
104
- # @param name [String, nil] custom name for the container
105
- # @param cmd [String, nil] command to execute as space-separated string
106
- # @param env [String, nil] environment variables as comma-separated KEY=VALUE pairs
107
- # @param exposed_ports [Hash, nil] ports to expose in {"port/protocol" => {}} format
108
- # @param host_config [Hash, nil] Docker host configuration including bindings and volumes
109
- #
110
- # @return [MCP::Tool::Response] creation results with container ID and name
111
- #
112
- # @raise [Docker::Error::NotFoundError] if the specified image doesn't exist
113
- # @raise [Docker::Error::ConflictError] if container name already exists
114
- # @raise [StandardError] for other creation failures
115
- #
116
- # @example Create simple container
117
- # response = CreateContainer.call(
118
- # server_context: context,
119
- # image: "alpine:latest",
120
- # name: "test-container"
121
- # )
122
- #
123
- # @example Create container with full configuration
124
- # response = CreateContainer.call(
125
- # server_context: context,
126
- # image: "redis:7-alpine",
127
- # name: "redis-cache",
128
- # env: "REDIS_PASSWORD=secret,REDIS_PORT=6379",
129
- # exposed_ports: {"6379/tcp" => {}},
130
- # host_config: {
131
- # "PortBindings" => {"6379/tcp" => [{"HostPort" => "6379"}]},
132
- # "RestartPolicy" => {"Name" => "unless-stopped"}
133
- # }
134
- # )
135
- #
136
- # @see Docker::Container.create
137
- def self.call(image:, server_context:, name: nil, cmd: nil, env: nil, exposed_ports: nil, host_config: nil)
70
+ param :name,
71
+ type: :string,
72
+ description: 'Container name (optional)',
73
+ required: false
74
+
75
+ param :cmd,
76
+ type: :string,
77
+ description: 'Command to run as space-separated string (optional, e.g., "npm start" or "python app.py")',
78
+ required: false
79
+
80
+ param :env,
81
+ type: :string,
82
+ description: 'Environment variables as comma-separated KEY=VALUE pairs (optional)',
83
+ required: false
84
+
85
+ param :exposed_ports,
86
+ type: :object,
87
+ description: 'Exposed ports as {"port/protocol": {}} (optional)',
88
+ required: false
89
+
90
+ param :host_config,
91
+ type: :object,
92
+ description: 'Host configuration including port bindings, volumes, etc. (optional)',
93
+ required: false
94
+
95
+ execute do |image:, name: nil, cmd: nil, env: nil, exposed_ports: nil, host_config: nil|
138
96
  config = { 'Image' => image }
139
97
  config['name'] = name if name
140
98
 
@@ -150,25 +108,15 @@ module DockerMCP
150
108
  container = Docker::Container.create(config)
151
109
  container_name = container.info['Names']&.first&.delete_prefix('/')
152
110
 
153
- MCP::Tool::Response.new([{
154
- type: 'text',
155
- text: "Container created successfully. ID: #{container.id}, Name: #{container_name}"
156
- }])
111
+ "Container created successfully. ID: #{container.id}, Name: #{container_name}"
157
112
  rescue Docker::Error::NotFoundError
158
- MCP::Tool::Response.new([{
159
- type: 'text',
160
- text: "Image #{image} not found"
161
- }])
113
+ "Image #{image} not found"
162
114
  rescue Docker::Error::ConflictError
163
- MCP::Tool::Response.new([{
164
- type: 'text',
165
- text: "Container with name #{name} already exists"
166
- }])
115
+ "Container with name #{name} already exists"
167
116
  rescue StandardError => e
168
- MCP::Tool::Response.new([{
169
- type: 'text',
170
- text: "Error creating container: #{e.message}"
171
- }])
117
+ "Error creating container: #{e.message}"
172
118
  end
173
119
  end
120
+
121
+ CreateContainer = CREATE_CONTAINER_DEFINITION.to_mcp_tool
174
122
  end
@@ -3,128 +3,96 @@
3
3
  module DockerMCP
4
4
  # MCP tool for creating Docker networks.
5
5
  #
6
- # This tool provides the ability to create custom Docker networks for
7
- # container communication and isolation. Networks enable containers to
8
- # communicate securely while providing isolation from other network
9
- # segments.
6
+ # This tool provides the ability to create custom Docker networks
7
+ # for container communication and isolation. Networks enable secure
8
+ # and controlled communication between containers and external systems.
10
9
  #
11
10
  # == Features
12
11
  #
13
12
  # - Create custom Docker networks
14
- # - Support for different network drivers
15
- # - Duplicate name detection
13
+ # - Support for multiple network drivers (bridge, overlay, host, etc.)
14
+ # - Duplicate name checking and validation
15
+ # - Network configuration and options
16
16
  # - Comprehensive error handling
17
- # - Flexible network configuration
18
- #
19
- # == Network Drivers
20
- #
21
- # Docker supports various network drivers:
22
- # - **bridge**: Default isolated network for single-host networking
23
- # - **host**: Remove network isolation, use host networking directly
24
- # - **overlay**: Multi-host networking for swarm services
25
- # - **macvlan**: Assign MAC addresses to containers
26
- # - **none**: Disable networking completely
27
- # - **Custom**: Third-party network drivers
17
+ # - Network isolation and security controls
28
18
  #
29
19
  # == Security Considerations
30
20
  #
31
- # Network creation affects system security:
32
- # - **Network Isolation**: Networks provide security boundaries
33
- # - **Traffic Control**: Custom networks enable traffic filtering
34
- # - **Access Control**: Networks control container communication
35
- # - **DNS Resolution**: Networks provide internal DNS services
21
+ # Network creation involves important security considerations:
22
+ # - **Network Isolation**: Improper networks can compromise container isolation
23
+ # - **Traffic Control**: Networks affect inter-container communication
24
+ # - **External Access**: Bridge networks may expose containers externally
25
+ # - **Resource Usage**: Networks consume system resources
26
+ # - **DNS Resolution**: Custom networks affect service discovery
27
+ # - **Firewall Bypass**: Networks can bypass host firewall rules
36
28
  #
37
- # Security implications:
38
- # - Bridge networks isolate containers from host network
39
- # - Host networks expose containers to all host traffic
40
- # - Custom networks should follow least-privilege principles
41
- # - Network names may reveal infrastructure details
42
- #
43
- # Best practices:
44
- # - Use descriptive but not sensitive network names
29
+ # **Security Recommendations**:
30
+ # - Use appropriate network drivers for use case
45
31
  # - Implement network segmentation strategies
46
- # - Limit container access to necessary networks only
47
- # - Monitor network traffic and connections
32
+ # - Monitor network traffic and usage
33
+ # - Avoid exposing internal networks externally
34
+ # - Use network policies for access control
48
35
  # - Regular audit of network configurations
49
36
  #
37
+ # == Parameters
38
+ #
39
+ # - **name**: Name of the network (required)
40
+ # - **driver**: Driver to use (optional, default: "bridge")
41
+ # - **check_duplicate**: Check for networks with duplicate names (optional, default: true)
42
+ #
43
+ # == Network Drivers
44
+ #
45
+ # - **bridge**: Default driver for single-host networking
46
+ # - **overlay**: Multi-host networking for Docker Swarm
47
+ # - **host**: Uses host's network stack directly
48
+ # - **none**: Disables networking for containers
49
+ # - **macvlan**: Assigns MAC addresses to containers
50
+ #
50
51
  # == Example Usage
51
52
  #
52
53
  # # Create basic bridge network
53
- # CreateNetwork.call(
54
+ # response = CreateNetwork.call(
54
55
  # server_context: context,
55
56
  # name: "app-network"
56
57
  # )
57
58
  #
58
- # # Create network with specific driver
59
- # CreateNetwork.call(
59
+ # # Create overlay network for swarm
60
+ # response = CreateNetwork.call(
60
61
  # server_context: context,
61
- # name: "frontend-net",
62
- # driver: "bridge"
62
+ # name: "swarm-network",
63
+ # driver: "overlay"
63
64
  # )
64
65
  #
65
- # # Create without duplicate checking
66
- # CreateNetwork.call(
66
+ # # Create network allowing duplicates
67
+ # response = CreateNetwork.call(
67
68
  # server_context: context,
68
- # name: "temp-network",
69
+ # name: "test-network",
70
+ # driver: "bridge",
69
71
  # check_duplicate: false
70
72
  # )
71
73
  #
72
- # @see ListNetworks
73
- # @see RemoveNetwork
74
74
  # @see Docker::Network.create
75
75
  # @since 0.1.0
76
- class CreateNetwork < MCP::Tool
76
+ CREATE_NETWORK_DEFINITION = ToolForge.define(:create_network) do
77
77
  description 'Create a Docker network'
78
78
 
79
- input_schema(
80
- properties: {
81
- name: {
82
- type: 'string',
79
+ param :name,
80
+ type: :string,
83
81
  description: 'Name of the network'
84
- },
85
- driver: {
86
- type: 'string',
87
- description: 'Driver to use (default: bridge)'
88
- },
89
- check_duplicate: {
90
- type: 'boolean',
91
- description: 'Check for networks with duplicate names (default: true)'
92
- }
93
- },
94
- required: ['name']
95
- )
96
82
 
97
- # Create a new Docker network.
98
- #
99
- # This method creates a custom Docker network with the specified name
100
- # and driver. The network can then be used by containers for isolated
101
- # communication.
102
- #
103
- # @param name [String] name for the new network
104
- # @param server_context [Object] MCP server context (unused but required)
105
- # @param driver [String] network driver to use (default: "bridge")
106
- # @param check_duplicate [Boolean] whether to check for duplicate names (default: true)
107
- #
108
- # @return [MCP::Tool::Response] network creation results with network ID
109
- #
110
- # @raise [Docker::Error::ConflictError] if network name already exists
111
- # @raise [StandardError] for other network creation failures
112
- #
113
- # @example Create application network
114
- # response = CreateNetwork.call(
115
- # server_context: context,
116
- # name: "webapp-network"
117
- # )
118
- #
119
- # @example Create host network
120
- # response = CreateNetwork.call(
121
- # server_context: context,
122
- # name: "high-performance-net",
123
- # driver: "host"
124
- # )
125
- #
126
- # @see Docker::Network.create
127
- def self.call(name:, server_context:, driver: 'bridge', check_duplicate: true)
83
+ param :driver,
84
+ type: :string,
85
+ description: 'Driver to use (default: bridge)',
86
+ required: false,
87
+ default: 'bridge'
88
+
89
+ param :check_duplicate,
90
+ type: :boolean,
91
+ description: 'Check for networks with duplicate names (default: true)',
92
+ required: false,
93
+ default: true
94
+
95
+ execute do |name:, driver: 'bridge', check_duplicate: true|
128
96
  options = {
129
97
  'Name' => name,
130
98
  'Driver' => driver,
@@ -133,20 +101,13 @@ module DockerMCP
133
101
 
134
102
  network = Docker::Network.create(name, options)
135
103
 
136
- MCP::Tool::Response.new([{
137
- type: 'text',
138
- text: "Network #{name} created successfully. ID: #{network.id}"
139
- }])
104
+ "Network #{name} created successfully. ID: #{network.id}"
140
105
  rescue Docker::Error::ConflictError
141
- MCP::Tool::Response.new([{
142
- type: 'text',
143
- text: "Network #{name} already exists"
144
- }])
106
+ "Network #{name} already exists"
145
107
  rescue StandardError => e
146
- MCP::Tool::Response.new([{
147
- type: 'text',
148
- text: "Error creating network: #{e.message}"
149
- }])
108
+ "Error creating network: #{e.message}"
150
109
  end
151
110
  end
111
+
112
+ CreateNetwork = CREATE_NETWORK_DEFINITION.to_mcp_tool
152
113
  end
@@ -3,125 +3,89 @@
3
3
  module DockerMCP
4
4
  # MCP tool for creating Docker volumes.
5
5
  #
6
- # This tool provides the ability to create persistent Docker volumes for
7
- # data storage that survives container lifecycle events. Volumes are the
8
- # preferred mechanism for persisting data generated and used by Docker
9
- # containers.
6
+ # This tool provides the ability to create Docker volumes for persistent
7
+ # data storage. Volumes are essential for maintaining data across container
8
+ # lifecycles and enabling data sharing between containers.
10
9
  #
11
10
  # == Features
12
11
  #
13
- # - Create named persistent volumes
14
- # - Support for different volume drivers
12
+ # - Create named Docker volumes
13
+ # - Support for multiple volume drivers
14
+ # - Persistent data storage management
15
+ # - Volume driver configuration
15
16
  # - Comprehensive error handling
16
- # - Duplicate name detection
17
- # - Flexible storage configuration
17
+ # - Volume lifecycle management
18
18
  #
19
- # == Volume Drivers
20
- #
21
- # Docker supports various volume drivers:
22
- # - **local**: Default driver using host filesystem
23
- # - **nfs**: Network File System for shared storage
24
- # - **cifs**: Common Internet File System
25
- # - **rexray**: External storage orchestration
26
- # - **Custom**: Third-party volume drivers
19
+ # == Security Considerations
27
20
  #
28
- # == Persistence Benefits
21
+ # Volume creation involves important security considerations:
22
+ # - **Data Persistence**: Volumes store data beyond container lifecycle
23
+ # - **Access Control**: Volume permissions affect data security
24
+ # - **Data Isolation**: Improper volumes can compromise data separation
25
+ # - **Storage Security**: Volume drivers may have security implications
26
+ # - **Resource Usage**: Volumes consume disk space and system resources
27
+ # - **Data Leakage**: Shared volumes can expose sensitive data
29
28
  #
30
- # Docker volumes provide several advantages:
31
- # - **Data Persistence**: Survive container deletion and recreation
32
- # - **Performance**: Better performance than bind mounts on Docker Desktop
33
- # - **Portability**: Can be moved between containers and hosts
34
- # - **Backup**: Easier to backup and restore than container filesystems
35
- # - **Sharing**: Can be shared between multiple containers
36
- # - **Management**: Managed by Docker daemon with proper lifecycle
29
+ # **Security Recommendations**:
30
+ # - Use appropriate volume drivers for security requirements
31
+ # - Implement proper access controls and permissions
32
+ # - Monitor volume usage and capacity
33
+ # - Regular backup of critical volume data
34
+ # - Audit volume access patterns
35
+ # - Use encryption for sensitive data volumes
36
+ # - Implement volume lifecycle policies
37
37
  #
38
- # == Security Considerations
38
+ # == Parameters
39
39
  #
40
- # Volume creation affects data security:
41
- # - **Data Isolation**: Volumes provide isolation between containers
42
- # - **Access Control**: Volume permissions affect data access
43
- # - **Storage Location**: Local volumes stored on host filesystem
44
- # - **Shared Access**: Multiple containers can access the same volume
40
+ # - **name**: Name of the volume (required)
41
+ # - **driver**: Driver to use (optional, default: "local")
45
42
  #
46
- # Security implications:
47
- # - Volumes persist data beyond container lifecycle
48
- # - Volume names may reveal application details
49
- # - Shared volumes can leak data between containers
50
- # - Storage driver choice affects security properties
43
+ # == Volume Drivers
51
44
  #
52
- # Best practices:
53
- # - Use descriptive but not sensitive volume names
54
- # - Implement proper volume access controls
55
- # - Regular backup of critical volume data
56
- # - Monitor volume usage and access patterns
57
- # - Choose appropriate drivers for security requirements
45
+ # - **local**: Default driver for local filesystem storage
46
+ # - **nfs**: Network File System driver for shared storage
47
+ # - **cifs**: Common Internet File System driver
48
+ # - **rexray**: REX-Ray driver for cloud storage integration
49
+ # - **convoy**: Convoy driver for snapshot management
58
50
  #
59
51
  # == Example Usage
60
52
  #
61
53
  # # Create basic local volume
62
- # CreateVolume.call(
54
+ # response = CreateVolume.call(
63
55
  # server_context: context,
64
56
  # name: "app-data"
65
57
  # )
66
58
  #
67
59
  # # Create volume with specific driver
68
- # CreateVolume.call(
60
+ # response = CreateVolume.call(
69
61
  # server_context: context,
70
62
  # name: "shared-storage",
71
63
  # driver: "nfs"
72
64
  # )
73
65
  #
74
- # @see ListVolumes
75
- # @see RemoveVolume
66
+ # # Create database volume
67
+ # response = CreateVolume.call(
68
+ # server_context: context,
69
+ # name: "postgres-data",
70
+ # driver: "local"
71
+ # )
72
+ #
76
73
  # @see Docker::Volume.create
77
74
  # @since 0.1.0
78
- class CreateVolume < MCP::Tool
75
+ CREATE_VOLUME_DEFINITION = ToolForge.define(:create_volume) do
79
76
  description 'Create a Docker volume'
80
77
 
81
- input_schema(
82
- properties: {
83
- name: {
84
- type: 'string',
78
+ param :name,
79
+ type: :string,
85
80
  description: 'Name of the volume'
86
- },
87
- driver: {
88
- type: 'string',
89
- description: 'Driver to use (default: local)'
90
- }
91
- },
92
- required: ['name']
93
- )
94
81
 
95
- # Create a new Docker volume for persistent data storage.
96
- #
97
- # This method creates a named Docker volume that can be used by containers
98
- # for persistent data storage. The volume will survive container deletion
99
- # and can be shared between multiple containers.
100
- #
101
- # @param name [String] name for the new volume
102
- # @param server_context [Object] MCP server context (unused but required)
103
- # @param driver [String] volume driver to use (default: "local")
104
- #
105
- # @return [MCP::Tool::Response] volume creation results
106
- #
107
- # @raise [Docker::Error::ConflictError] if volume name already exists
108
- # @raise [StandardError] for other volume creation failures
109
- #
110
- # @example Create application data volume
111
- # response = CreateVolume.call(
112
- # server_context: context,
113
- # name: "webapp-data"
114
- # )
115
- #
116
- # @example Create NFS volume
117
- # response = CreateVolume.call(
118
- # server_context: context,
119
- # name: "shared-files",
120
- # driver: "nfs"
121
- # )
122
- #
123
- # @see Docker::Volume.create
124
- def self.call(name:, server_context:, driver: 'local')
82
+ param :driver,
83
+ type: :string,
84
+ description: 'Driver to use (default: local)',
85
+ required: false,
86
+ default: 'local'
87
+
88
+ execute do |name:, driver: 'local'|
125
89
  options = {
126
90
  'Name' => name,
127
91
  'Driver' => driver
@@ -129,20 +93,13 @@ module DockerMCP
129
93
 
130
94
  Docker::Volume.create(name, options)
131
95
 
132
- MCP::Tool::Response.new([{
133
- type: 'text',
134
- text: "Volume #{name} created successfully"
135
- }])
96
+ "Volume #{name} created successfully"
136
97
  rescue Docker::Error::ConflictError
137
- MCP::Tool::Response.new([{
138
- type: 'text',
139
- text: "Volume #{name} already exists"
140
- }])
98
+ "Volume #{name} already exists"
141
99
  rescue StandardError => e
142
- MCP::Tool::Response.new([{
143
- type: 'text',
144
- text: "Error creating volume: #{e.message}"
145
- }])
100
+ "Error creating volume: #{e.message}"
146
101
  end
147
102
  end
103
+
104
+ CreateVolume = CREATE_VOLUME_DEFINITION.to_mcp_tool
148
105
  end