// Package tools is the stdio MCP server that runs inside each container. // Its tools only check their input; the bot sees the calls over ACP and does the work. package tools import ( "bufio" "encoding/json" "fmt" "io" "os" "path/filepath" "strings" ) const ( ServerName = "slackerbot" SendFile = "send_file" ) const sendFileHelp = "Send a file to the user in Slack: images, videos, PDFs, archives, reports or anything else " + "they should see or download. Prefer it to pasting long output. The file must be under %s and at most %d MB. " + "It's uploaded into the conversation when your turn ends, so mention it in your reply. " + "At most a few files per turn." type Server struct { Home string MaxMB int64 } type msg struct { JSONRPC string `json:"jsonrpc"` ID json.RawMessage `json:"id,omitempty"` Method string `json:"method,omitempty"` Params json.RawMessage `json:"params,omitempty"` Result any `json:"result,omitempty"` Error *rpcError `json:"error,omitempty"` } type rpcError struct { Code int `json:"code"` Message string `json:"message"` } func (s *Server) Serve(in io.Reader, out io.Writer) error { sc := bufio.NewScanner(in) sc.Buffer(make([]byte, 1<<20), 16<<20) enc := json.NewEncoder(out) for sc.Scan() { var m msg if json.Unmarshal(sc.Bytes(), &m) != nil || len(m.ID) == 0 { continue } reply := msg{JSONRPC: "2.0", ID: m.ID} reply.Result, reply.Error = s.handle(m) if err := enc.Encode(reply); err != nil { return err } } return sc.Err() } func (s *Server) handle(m msg) (any, *rpcError) { switch m.Method { case "initialize": var p struct { ProtocolVersion string `json:"protocolVersion"` } _ = json.Unmarshal(m.Params, &p) return map[string]any{ "protocolVersion": p.ProtocolVersion, "capabilities": map[string]any{"tools": map[string]any{}}, "serverInfo": map[string]string{"name": ServerName, "version": "1"}, }, nil case "ping": return map[string]any{}, nil case "tools/list": return map[string]any{"tools": []any{map[string]any{ "name": SendFile, "description": fmt.Sprintf(sendFileHelp, s.Home, s.MaxMB), "inputSchema": map[string]any{ "type": "object", "properties": map[string]any{ "path": map[string]string{"type": "string", "description": "Path of the file, e.g. " + s.Home + "/out/chart.png"}, "comment": map[string]string{"type": "string", "description": "Optional message posted with the file"}, }, "required": []string{"path"}, }, }}}, nil case "tools/call": var p struct { Name string `json:"name"` Arguments struct { Path string `json:"path"` } `json:"arguments"` } _ = json.Unmarshal(m.Params, &p) if p.Name != SendFile { return nil, &rpcError{-32602, "unknown tool " + p.Name} } text, isErr := "queued", false if err := s.Check(p.Arguments.Path); err != nil { text, isErr = err.Error(), true } return map[string]any{"content": []any{map[string]string{"type": "text", "text": text}}, "isError": isErr}, nil } return nil, &rpcError{-32601, "method not found: " + m.Method} } func (s *Server) Check(path string) error { if path == "" { return fmt.Errorf("path is required") } if !filepath.IsAbs(path) { path = filepath.Join(s.Home, path) } real, err := filepath.EvalSymlinks(path) if err != nil { return fmt.Errorf("can't find %s", path) } if !strings.HasPrefix(real, filepath.Clean(s.Home)+"/") { return fmt.Errorf("%s isn't under %s; copy it there first", path, s.Home) } st, err := os.Stat(real) if err != nil { return err } if !st.Mode().IsRegular() { return fmt.Errorf("%s isn't a regular file (zip a folder first)", path) } if st.Size() == 0 { return fmt.Errorf("%s is empty", path) } if st.Size() > s.MaxMB<<20 { return fmt.Errorf("%s is %d MB, over the %d MB limit", path, st.Size()>>20, s.MaxMB) } return nil }