Merge pull request 'feat: add node-runner base image for npm-based MCP servers' (#11) from feat/node-runner-base-image into main
This commit was merged in pull request #11.
This commit is contained in:
12
deploy/Dockerfile.node-runner
Normal file
12
deploy/Dockerfile.node-runner
Normal file
@@ -0,0 +1,12 @@
|
||||
# Base container for npm-based MCP servers (STDIO transport).
|
||||
# mcpd uses this image to run `npx -y <packageName>` when a server
|
||||
# has packageName but no dockerImage.
|
||||
FROM node:20-alpine
|
||||
|
||||
WORKDIR /mcp
|
||||
|
||||
# Pre-warm npx cache directory
|
||||
RUN mkdir -p /root/.npm
|
||||
|
||||
# Default entrypoint — overridden by mcpd via container command
|
||||
ENTRYPOINT ["npx", "-y"]
|
||||
@@ -30,6 +30,8 @@ services:
|
||||
MCPD_PORT: "3100"
|
||||
MCPD_HOST: "0.0.0.0"
|
||||
MCPD_LOG_LEVEL: info
|
||||
MCPD_NODE_RUNNER_IMAGE: mcpctl-node-runner:latest
|
||||
MCPD_MCP_NETWORK: mcp-servers
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
@@ -48,6 +50,16 @@ services:
|
||||
retries: 3
|
||||
start_period: 10s
|
||||
|
||||
# Base image for npm-based MCP servers (built once, used by mcpd)
|
||||
node-runner:
|
||||
build:
|
||||
context: ..
|
||||
dockerfile: deploy/Dockerfile.node-runner
|
||||
image: mcpctl-node-runner:latest
|
||||
profiles:
|
||||
- build
|
||||
entrypoint: ["echo", "Image built successfully"]
|
||||
|
||||
postgres-test:
|
||||
image: postgres:16-alpine
|
||||
container_name: mcpctl-postgres-test
|
||||
@@ -71,8 +83,11 @@ networks:
|
||||
mcpctl:
|
||||
driver: bridge
|
||||
mcp-servers:
|
||||
name: mcp-servers
|
||||
driver: bridge
|
||||
internal: true
|
||||
# Not internal — MCP servers need outbound access to reach external APIs
|
||||
# (e.g., Grafana, Home Assistant). Isolation is enforced by not binding
|
||||
# host ports on MCP server containers; only mcpd can reach them.
|
||||
|
||||
volumes:
|
||||
mcpctl-pgdata:
|
||||
|
||||
@@ -5,7 +5,7 @@ import type {
|
||||
ContainerInfo,
|
||||
ContainerLogs,
|
||||
} from '../orchestrator.js';
|
||||
import { DEFAULT_MEMORY_LIMIT, DEFAULT_NANO_CPUS } from '../orchestrator.js';
|
||||
import { DEFAULT_MEMORY_LIMIT } from '../orchestrator.js';
|
||||
|
||||
const MCPCTL_LABEL = 'mcpctl.managed';
|
||||
|
||||
@@ -54,7 +54,7 @@ export class DockerContainerManager implements McpOrchestrator {
|
||||
|
||||
async createContainer(spec: ContainerSpec): Promise<ContainerInfo> {
|
||||
const memoryLimit = spec.memoryLimit ?? DEFAULT_MEMORY_LIMIT;
|
||||
const nanoCpus = spec.nanoCpus ?? DEFAULT_NANO_CPUS;
|
||||
const nanoCpus = spec.nanoCpus;
|
||||
|
||||
const portBindings: Record<string, Array<{ HostPort: string }>> = {};
|
||||
const exposedPorts: Record<string, Record<string, never>> = {};
|
||||
@@ -83,7 +83,7 @@ export class DockerContainerManager implements McpOrchestrator {
|
||||
HostConfig: {
|
||||
PortBindings: portBindings,
|
||||
Memory: memoryLimit,
|
||||
NanoCpus: nanoCpus,
|
||||
...(nanoCpus ? { NanoCpus: nanoCpus } : {}),
|
||||
NetworkMode: spec.network ?? 'bridge',
|
||||
},
|
||||
};
|
||||
|
||||
@@ -4,6 +4,12 @@ import type { McpOrchestrator, ContainerSpec, ContainerInfo } from './orchestrat
|
||||
import { NotFoundError } from './mcp-server.service.js';
|
||||
import { resolveServerEnv } from './env-resolver.js';
|
||||
|
||||
/** Default image for npm-based MCP servers (STDIO with packageName, no dockerImage). */
|
||||
const DEFAULT_NODE_RUNNER_IMAGE = process.env['MCPD_NODE_RUNNER_IMAGE'] ?? 'mcpctl-node-runner:latest';
|
||||
|
||||
/** Network for MCP server containers (matches docker-compose mcp-servers network). */
|
||||
const MCP_SERVERS_NETWORK = process.env['MCPD_MCP_NETWORK'] ?? 'mcp-servers';
|
||||
|
||||
export class InvalidStateError extends Error {
|
||||
readonly statusCode = 409;
|
||||
constructor(message: string) {
|
||||
@@ -139,7 +145,23 @@ export class InstanceService {
|
||||
});
|
||||
}
|
||||
|
||||
const image = server.dockerImage ?? server.packageName ?? server.name;
|
||||
// Determine image + command based on server config:
|
||||
// 1. Explicit dockerImage → use as-is
|
||||
// 2. packageName (npm) → use node-runner image + npx command
|
||||
// 3. Fallback → server name (legacy)
|
||||
let image: string;
|
||||
let npmCommand: string[] | undefined;
|
||||
|
||||
if (server.dockerImage) {
|
||||
image = server.dockerImage;
|
||||
} else if (server.packageName) {
|
||||
image = DEFAULT_NODE_RUNNER_IMAGE;
|
||||
// Build npx command: entrypoint is ["npx", "-y"], so CMD = [packageName, ...args]
|
||||
const serverCommand = server.command as string[] | null;
|
||||
npmCommand = [server.packageName, ...(serverCommand ?? [])];
|
||||
} else {
|
||||
image = server.name;
|
||||
}
|
||||
|
||||
let instance = await this.instanceRepo.create({
|
||||
serverId,
|
||||
@@ -151,6 +173,7 @@ export class InstanceService {
|
||||
image,
|
||||
name: `mcpctl-${server.name}-${instance.id}`,
|
||||
hostPort: null,
|
||||
network: MCP_SERVERS_NETWORK,
|
||||
labels: {
|
||||
'mcpctl.server-id': serverId,
|
||||
'mcpctl.instance-id': instance.id,
|
||||
@@ -159,10 +182,16 @@ export class InstanceService {
|
||||
if (server.transport === 'SSE' || server.transport === 'STREAMABLE_HTTP') {
|
||||
spec.containerPort = server.containerPort ?? 3000;
|
||||
}
|
||||
// npm-based servers: command = [packageName, ...args] (entrypoint handles npx -y)
|
||||
// Docker-image servers: use explicit command if provided
|
||||
if (npmCommand) {
|
||||
spec.command = npmCommand;
|
||||
} else {
|
||||
const command = server.command as string[] | null;
|
||||
if (command) {
|
||||
spec.command = command;
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve env vars from inline values and secret refs
|
||||
if (this.secretRepo) {
|
||||
|
||||
Reference in New Issue
Block a user