fix: mcp containerd fifo

This commit is contained in:
Ran
2026-02-07 22:14:38 +08:00
parent 344b617423
commit 4e661bae76
4 changed files with 18 additions and 46 deletions
+2 -2
View File
@@ -13,5 +13,5 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH:-amd64} \
FROM alpine:latest
RUN apk add --no-cache grep
WORKDIR /app
COPY --from=build /out/mcp /app/mcp
ENTRYPOINT ["/app/mcp"]
COPY --from=build /out/mcp /opt/mcp
ENTRYPOINT ["/bin/sh","-lc","bootstrap(){ [ -e /app/mcp ] || { mkdir -p /app; [ -f /opt/mcp ] && cp -a /opt/mcp /app/mcp 2>/dev/null || true; }; }; bootstrap; if [ -x /app/mcp ]; then exec /app/mcp \"$@\"; fi; exec /opt/mcp \"$@\"","--"]
+12 -10
View File
@@ -415,17 +415,19 @@ func (s *DefaultService) StartTask(ctx context.Context, containerID string, opts
return nil, err
}
var cioOpts []cio.Opt
if opts == nil || opts.UseStdio {
cioOpts = append(cioOpts, cio.WithStdio)
var ioCreator cio.Creator
if opts == nil || !opts.UseStdio {
ioCreator = cio.NullIO
} else {
cioOpts := []cio.Opt{cio.WithStdio}
if opts.Terminal {
cioOpts = append(cioOpts, cio.WithTerminal)
}
if opts.FIFODir != "" {
cioOpts = append(cioOpts, cio.WithFIFODir(opts.FIFODir))
}
ioCreator = cio.NewCreator(cioOpts...)
}
if opts != nil && opts.Terminal {
cioOpts = append(cioOpts, cio.WithTerminal)
}
if opts != nil && opts.FIFODir != "" {
cioOpts = append(cioOpts, cio.WithFIFODir(opts.FIFODir))
}
ioCreator := cio.NewCreator(cioOpts...)
task, err := container.NewTask(ctx, ioCreator)
if err != nil {
+2 -32
View File
@@ -189,7 +189,7 @@ func (h *ContainerdHandler) CreateContainer(c echo.Context) error {
Options: []string{"rbind", "rw"},
},
}),
oci.WithProcessArgs("/bin/sh", "-lc", "sleep 2147483647"),
oci.WithProcessArgs("/bin/sh", "-lc", "bootstrap(){ [ -e /app/mcp ] || { mkdir -p /app; [ -f /opt/mcp ] && cp -a /opt/mcp /app/mcp 2>/dev/null || true; }; }; bootstrap; exec /app/mcp"),
}
_, err = h.service.CreateContainer(ctx, ctr.CreateContainerRequest{
@@ -227,13 +227,8 @@ func (h *ContainerdHandler) CreateContainer(c echo.Context) error {
}
started := false
fifoDir, err := h.taskFIFODir()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
if _, err := h.service.StartTask(ctx, containerID, &ctr.StartTaskOptions{
UseStdio: false,
FIFODir: fifoDir,
}); err == nil {
started = true
if h.queries != nil {
@@ -256,21 +251,6 @@ func (h *ContainerdHandler) CreateContainer(c echo.Context) error {
})
}
func (h *ContainerdHandler) taskFIFODir() (string, error) {
if homeDir, err := os.UserHomeDir(); err == nil && homeDir != "" {
fifoDir := filepath.Join(homeDir, ".memoh", "containerd-fifo")
if err := os.MkdirAll(fifoDir, 0o755); err != nil {
return "", err
}
return fifoDir, nil
}
fifoDir := "/tmp/memoh-containerd-fifo"
if err := os.MkdirAll(fifoDir, 0o755); err != nil {
return "", err
}
return fifoDir, nil
}
func (h *ContainerdHandler) ensureTaskRunning(ctx context.Context, containerID string) error {
tasks, err := h.service.ListTasks(ctx, &ctr.ListTasksOptions{
Filter: "container.id==" + containerID,
@@ -285,13 +265,8 @@ func (h *ContainerdHandler) ensureTaskRunning(ctx context.Context, containerID s
_ = h.service.DeleteTask(ctx, containerID, &ctr.DeleteTaskOptions{Force: true})
}
fifoDir, err := h.taskFIFODir()
if err != nil {
return err
}
_, err = h.service.StartTask(ctx, containerID, &ctr.StartTaskOptions{
UseStdio: false,
FIFODir: fifoDir,
})
return err
}
@@ -687,7 +662,7 @@ func (h *ContainerdHandler) SetupBotContainer(ctx context.Context, botID string)
Options: []string{"rbind", "rw"},
},
}),
oci.WithProcessArgs("/bin/sh", "-lc", "sleep 2147483647"),
oci.WithProcessArgs("/bin/sh", "-lc", "bootstrap(){ [ -e /app/mcp ] || { mkdir -p /app; [ -f /opt/mcp ] && cp -a /opt/mcp /app/mcp 2>/dev/null || true; }; }; bootstrap; exec /app/mcp"),
}
_, err := h.service.CreateContainer(ctx, ctr.CreateContainerRequest{
@@ -724,13 +699,8 @@ func (h *ContainerdHandler) SetupBotContainer(ctx context.Context, botID string)
}
}
fifoDir, err := h.taskFIFODir()
if err != nil {
return err
}
if _, err := h.service.StartTask(ctx, containerID, &ctr.StartTaskOptions{
UseStdio: false,
FIFODir: fifoDir,
}); err == nil {
if h.queries != nil {
if pgBotID, parseErr := parsePgUUID(botID); parseErr == nil {
+2 -2
View File
@@ -192,7 +192,7 @@ func (h *ContainerdHandler) getMCPSession(ctx context.Context, containerID strin
func (h *ContainerdHandler) startContainerdMCPSession(ctx context.Context, containerID string) (*mcpSession, error) {
execSession, err := h.service.ExecTaskStreaming(ctx, containerID, ctr.ExecTaskRequest{
Args: []string{"/mcp"},
Args: []string{"/app/mcp"},
})
if err != nil {
return nil, err
@@ -239,7 +239,7 @@ func (h *ContainerdHandler) startLimaMCPSession(containerID string) (*mcpSession
"--exec-id",
execID,
containerID,
"/mcp",
"/app/mcp",
)
stdin, err := cmd.StdinPipe()