mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
9ceabf68c4
Replace the host bind-mount + containerd exec approach with a per-bot
in-container gRPC server (ContainerService, port 9090). All file I/O,
exec, and MCP stdio sessions now go through gRPC instead of running
shell commands or reading host-mounted directories.
Architecture changes:
- cmd/mcp: rewritten as a gRPC server (ContainerService) with full
file and exec API (ReadFile, WriteFile, ListDir, ReadRaw, WriteRaw,
Exec, Stat, Mkdir, Rename, DeleteFile)
- internal/mcp/mcpcontainer: protobuf definitions and generated stubs
- internal/mcp/mcpclient: gRPC client wrapper with connection pool
(Pool) and Provider interface for dependency injection
- mcp.Manager: add per-bot IP cache, gRPC connection pool, and
SetContainerIP/MCPClient methods; remove DataDir/Exec helpers
- containerd.Service: remove ExecTask/ExecTaskStreaming; network setup
now returns NetworkResult{IP} for pool routing
- internal/fs/service.go: deleted (replaced by mcpclient)
- handlers/fs.go: deleted; MCP stdio session logic moved to mcp_stdio.go
- container provider Executor: all tools (read/write/list/edit/exec)
now call gRPC client instead of running shell via exec
- storefs, containerfs, media, skills, memory: all I/O ported to
mcpclient.Provider
Database:
- migration 0022: drop host_path column from containers table
One-time data migration:
- migrateBindMountData: on first Start() after upgrade, copies old
bind-mount data into the container via gRPC, then renames src dir
to prevent re-migration; runs in background goroutine
Bug fixes:
- mcp_stdio: callRaw now returns full JSON-RPC envelope
{"jsonrpc","id","result"|"error"} matching protocol spec;
explicit "initialize" call now advances session init state to
prevent duplicate handshake on next non-initialize call
- mcpclient Pool: properly evict stale gRPC connection after snapshot
replace (container process recreated); use SetContainerIP instead
of direct map write so IP changes always evict pool entry
- migrateBindMountData: walkErr on directories now counted as failure
so partially-walked trees don't get incorrectly marked as migrated
- cmd/mcp/Dockerfile: removed dead file (docker/Dockerfile.mcp is the
canonical production build)
Tests:
- provider_test.go: restored with bufconn in-process gRPC mock
(fakeContainerService + staticProvider), 14 cases covering all 5
tools plus edge cases
- mcp_session_test.go: new, covers JSON-RPC envelope, init state
machine, pending cleanup on cancel/close, readLoop cancel
- storefs/service_test.go: restored (pure function roundtrip tests)
119 lines
2.1 KiB
Protocol Buffer
119 lines
2.1 KiB
Protocol Buffer
syntax = "proto3";
|
|
|
|
package mcpcontainer;
|
|
|
|
option go_package = "github.com/memohai/memoh/internal/mcp/mcpcontainer";
|
|
|
|
service ContainerService {
|
|
rpc ReadFile(ReadFileRequest) returns (ReadFileResponse);
|
|
rpc WriteFile(WriteFileRequest) returns (WriteFileResponse);
|
|
rpc ListDir(ListDirRequest) returns (ListDirResponse);
|
|
rpc Stat(StatRequest) returns (StatResponse);
|
|
rpc Mkdir(MkdirRequest) returns (MkdirResponse);
|
|
rpc Rename(RenameRequest) returns (RenameResponse);
|
|
rpc Exec(stream ExecInput) returns (stream ExecOutput);
|
|
rpc ReadRaw(ReadRawRequest) returns (stream DataChunk);
|
|
rpc WriteRaw(stream WriteRawChunk) returns (WriteRawResponse);
|
|
rpc DeleteFile(DeleteFileRequest) returns (DeleteFileResponse);
|
|
}
|
|
|
|
message ReadFileRequest {
|
|
string path = 1;
|
|
int32 line_offset = 2;
|
|
int32 n_lines = 3;
|
|
}
|
|
|
|
message ReadFileResponse {
|
|
string content = 1;
|
|
int32 total_lines = 2;
|
|
bool binary = 3;
|
|
}
|
|
|
|
message WriteFileRequest {
|
|
string path = 1;
|
|
bytes content = 2;
|
|
}
|
|
|
|
message WriteFileResponse {}
|
|
|
|
message ListDirRequest {
|
|
string path = 1;
|
|
bool recursive = 2;
|
|
}
|
|
|
|
message FileEntry {
|
|
string path = 1;
|
|
bool is_dir = 2;
|
|
int64 size = 3;
|
|
string mode = 4;
|
|
string mod_time = 5;
|
|
}
|
|
|
|
message ListDirResponse {
|
|
repeated FileEntry entries = 1;
|
|
}
|
|
|
|
message ExecInput {
|
|
string command = 1;
|
|
string work_dir = 2;
|
|
repeated string env = 3;
|
|
int32 timeout_seconds = 4;
|
|
bytes stdin_data = 5;
|
|
}
|
|
|
|
message ExecOutput {
|
|
enum Stream {
|
|
STDOUT = 0;
|
|
STDERR = 1;
|
|
EXIT = 2;
|
|
}
|
|
Stream stream = 1;
|
|
bytes data = 2;
|
|
int32 exit_code = 3;
|
|
}
|
|
|
|
message ReadRawRequest {
|
|
string path = 1;
|
|
}
|
|
|
|
message DataChunk {
|
|
bytes data = 1;
|
|
}
|
|
|
|
message WriteRawChunk {
|
|
string path = 1;
|
|
bytes data = 2;
|
|
}
|
|
|
|
message WriteRawResponse {
|
|
int64 bytes_written = 1;
|
|
}
|
|
|
|
message DeleteFileRequest {
|
|
string path = 1;
|
|
bool recursive = 2;
|
|
}
|
|
|
|
message DeleteFileResponse {}
|
|
|
|
message StatRequest {
|
|
string path = 1;
|
|
}
|
|
|
|
message StatResponse {
|
|
FileEntry entry = 1;
|
|
}
|
|
|
|
message MkdirRequest {
|
|
string path = 1;
|
|
}
|
|
|
|
message MkdirResponse {}
|
|
|
|
message RenameRequest {
|
|
string old_path = 1;
|
|
string new_path = 2;
|
|
}
|
|
|
|
message RenameResponse {}
|