Files
dc605b911e feat(mcp): expand CRUD server to near-complete CLI parity (#1440)
* feat(mcp): expand CRUD MCP server to near-complete CLI parity

Closes the CLI-vs-MCP tool coverage gap identified by auditing every
`goclaw` CLI command against the existing goclaw_* MCP tool set. Adds
skill grant/revoke, agent skill pin/unpin, and full CRUD/inspection
surfaces for memory, knowledge graph (including dedup/merge/prune),
tenants, providers, LLM traces, channel contacts, pending messages,
audit activity, system config, tenant storage (list/size/delete/move),
scoped agent config export/import, secure-CLI binary registry, and a
DB-backed health check.

Deliberately out of scope, documented inline where relevant:
- `goclaw credentials`: confirmed CLI-local (~/.goclaw/config.yaml +
  keychain), no server resource to wrap. goclaw_secure_cli_binaries_*
  covers the closest real, previously-uncovered server resource instead.
- Full tar-archive agent export/import (KG + workspace files): the CLI's
  version streams a multi-section archive with progress events, a shape
  that doesn't map to a single MCP tool call. Config + context files
  (the portable "brain") is covered.
- `kg extract` (LLM-driven text extraction): goclaw_kg_ingest accepts
  the same Entity/Relation shapes the extractor produces, so a caller
  can run extraction itself and hand off the result.

Wires 8 new store dependencies (Memory, KnowledgeGraph, Tracing,
Contacts, PendingMessages, Activity, SystemConfigs, SecureCLI) through
gateway.Server setters -> cmd/gateway.go -> CRUDDeps, following the
existing Providers/Tenants pattern. Storage and secure-CLI-binary
handlers duplicate internal/http's path-escape/symlink-hiding
validation logic (documented inline) since internal/http already
imports internal/mcp and the reverse would cycle.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(mcp): return sessionKey in goclaw_chat_send response for persistent conversations

The goclaw_chat_send tool creates a new session internally when sessionKey is
empty, but never returned the key to the caller. This prevented using
goclaw_chat_history to fetch previous messages in the session.

Add SessionKey field to ChatSendResult so callers can:
1. Start a new agent chat without providing sessionKey
2. Receive the sessionKey back in the response
3. Use that sessionKey for follow-up messages and history queries

Fixes the training loop pattern: start chat → get sessionKey → call
goclaw_chat_history with that key → iterate skill based on actual failures.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* feat(mcp): add timing diagnostics to goclaw_chat_send for timeout troubleshooting

Log request arrival, processing duration, and errors with millisecond precision.
Helps identify whether timeouts occur at MCP client→goclaw, goclaw→ollama, or
during agent execution. Critical for production debugging.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>

* fix(gateway): add http.Server timeouts for defensive timeout handling

Set explicit timeouts in http.Server:
- ReadTimeout: 1h (allow large uploads, long-running agent operations)
- WriteTimeout: 1h (allow streaming responses to slow clients)
- IdleTimeout: 30s (close idle keep-alive connections quickly)

Provides defense-in-depth when Nginx/Traefik timeouts are misconfigured.
Matches Nginx timeout (3600s) to prevent race conditions.

Timeout chain: traefik 3600s = nginx 3600s = goclaw 3600s

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Bruno Clermont <bruno.clermont@gmail.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-16 01:56:03 +07:00

158 lines
6.1 KiB
Go

package mcp
import (
"context"
mcpgo "github.com/mark3labs/mcp-go/mcp"
mcpserver "github.com/mark3labs/mcp-go/server"
"github.com/nextlevelbuilder/goclaw/internal/store"
)
// registerMemoryCRUDTools registers the goclaw_memory_* MCP tools backed by
// store.MemoryStore — closes a CLI-vs-MCP coverage gap (the `goclaw memory
// get/list/search/store/delete` commands had no MCP equivalent).
func registerMemoryCRUDTools(srv *mcpserver.MCPServer, memory store.MemoryStore) {
srv.AddTool(mcpgo.NewTool("goclaw_memory_list",
mcpgo.WithDescription("List memory documents for an agent/user scope."),
mcpgo.WithString("agent_id", mcpgo.Required(), mcpgo.Description("Agent ID scope.")),
mcpgo.WithString("user_id", mcpgo.Description("User ID scope; empty for agent-global documents.")),
mcpgo.WithReadOnlyHintAnnotation(true),
), handleMemoryList(memory))
srv.AddTool(mcpgo.NewTool("goclaw_memory_get",
mcpgo.WithDescription("Get a single memory document's content."),
mcpgo.WithString("agent_id", mcpgo.Required(), mcpgo.Description("Agent ID scope.")),
mcpgo.WithString("user_id", mcpgo.Description("User ID scope; empty for agent-global documents.")),
mcpgo.WithString("path", mcpgo.Required(), mcpgo.Description("Document path.")),
mcpgo.WithReadOnlyHintAnnotation(true),
), handleMemoryGet(memory))
srv.AddTool(mcpgo.NewTool("goclaw_memory_search",
mcpgo.WithDescription("Search memory documents (hybrid vector+text search) for an agent/user scope."),
mcpgo.WithString("query", mcpgo.Required(), mcpgo.Description("Search query.")),
mcpgo.WithString("agent_id", mcpgo.Required(), mcpgo.Description("Agent ID scope.")),
mcpgo.WithString("user_id", mcpgo.Description("User ID scope; empty for agent-global documents.")),
mcpgo.WithNumber("max_results", mcpgo.Description("Maximum results to return; server default if omitted.")),
mcpgo.WithReadOnlyHintAnnotation(true),
), handleMemorySearch(memory))
srv.AddTool(mcpgo.NewTool("goclaw_memory_store",
mcpgo.WithDescription("Create or overwrite a memory document's content, then re-index it."),
mcpgo.WithString("agent_id", mcpgo.Required(), mcpgo.Description("Agent ID scope.")),
mcpgo.WithString("user_id", mcpgo.Description("User ID scope; empty for agent-global documents.")),
mcpgo.WithString("path", mcpgo.Required(), mcpgo.Description("Document path.")),
mcpgo.WithString("content", mcpgo.Required(), mcpgo.Description("Document content.")),
), handleMemoryStore(memory))
srv.AddTool(mcpgo.NewTool("goclaw_memory_delete",
mcpgo.WithDescription("Delete a memory document."),
mcpgo.WithString("agent_id", mcpgo.Required(), mcpgo.Description("Agent ID scope.")),
mcpgo.WithString("user_id", mcpgo.Description("User ID scope; empty for agent-global documents.")),
mcpgo.WithString("path", mcpgo.Required(), mcpgo.Description("Document path.")),
mcpgo.WithDestructiveHintAnnotation(true),
), handleMemoryDelete(memory))
}
func handleMemoryList(memory store.MemoryStore) mcpserver.ToolHandlerFunc {
return func(ctx context.Context, req mcpgo.CallToolRequest) (*mcpgo.CallToolResult, error) {
agentID, err := req.RequireString("agent_id")
if err != nil {
return toolError("memory.list", err)
}
userID := req.GetString("user_id", "")
docs, err := memory.ListDocuments(ctx, agentID, userID)
if err != nil {
return toolError("memory.list", err)
}
return jsonToolResult(docs)
}
}
func handleMemoryGet(memory store.MemoryStore) mcpserver.ToolHandlerFunc {
return func(ctx context.Context, req mcpgo.CallToolRequest) (*mcpgo.CallToolResult, error) {
agentID, err := req.RequireString("agent_id")
if err != nil {
return toolError("memory.get", err)
}
path, err := req.RequireString("path")
if err != nil {
return toolError("memory.get", err)
}
userID := req.GetString("user_id", "")
content, err := memory.GetDocument(ctx, agentID, userID, path)
if err != nil {
return toolError("memory.get", err)
}
return jsonToolResult(map[string]string{"path": path, "content": content})
}
}
func handleMemorySearch(memory store.MemoryStore) mcpserver.ToolHandlerFunc {
return func(ctx context.Context, req mcpgo.CallToolRequest) (*mcpgo.CallToolResult, error) {
query, err := req.RequireString("query")
if err != nil {
return toolError("memory.search", err)
}
agentID, err := req.RequireString("agent_id")
if err != nil {
return toolError("memory.search", err)
}
userID := req.GetString("user_id", "")
opts := store.MemorySearchOptions{}
if v, ok := req.GetArguments()["max_results"]; ok {
if f, ok := v.(float64); ok {
opts.MaxResults = int(f)
}
}
results, err := memory.Search(ctx, query, agentID, userID, opts)
if err != nil {
return toolError("memory.search", err)
}
return jsonToolResult(results)
}
}
func handleMemoryStore(memory store.MemoryStore) mcpserver.ToolHandlerFunc {
return func(ctx context.Context, req mcpgo.CallToolRequest) (*mcpgo.CallToolResult, error) {
agentID, err := req.RequireString("agent_id")
if err != nil {
return toolError("memory.store", err)
}
path, err := req.RequireString("path")
if err != nil {
return toolError("memory.store", err)
}
content, err := req.RequireString("content")
if err != nil {
return toolError("memory.store", err)
}
userID := req.GetString("user_id", "")
if err := memory.PutDocument(ctx, agentID, userID, path, content); err != nil {
return toolError("memory.store", err)
}
if err := memory.IndexDocument(ctx, agentID, userID, path); err != nil {
return toolError("memory.store", err)
}
return jsonToolResult(map[string]string{"ok": "true", "path": path})
}
}
func handleMemoryDelete(memory store.MemoryStore) mcpserver.ToolHandlerFunc {
return func(ctx context.Context, req mcpgo.CallToolRequest) (*mcpgo.CallToolResult, error) {
agentID, err := req.RequireString("agent_id")
if err != nil {
return toolError("memory.delete", err)
}
path, err := req.RequireString("path")
if err != nil {
return toolError("memory.delete", err)
}
userID := req.GetString("user_id", "")
if err := memory.DeleteDocument(ctx, agentID, userID, path); err != nil {
return toolError("memory.delete", err)
}
return jsonToolResult(map[string]string{"ok": "true"})
}
}