bazel start + mcp start
This commit is contained in:
1 parent
a90653260a
commit
4c082aca7c
33 files changed
+1728
-79
No files matched your search
@@ -0,0 +1,137 @@
|
||||
// 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
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSendFileTool(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
os.WriteFile(filepath.Join(home, "chart.png"), []byte("png"), 0o644)
|
||||
os.Mkdir(filepath.Join(home, "dir"), 0o755)
|
||||
os.Symlink("/etc/passwd", filepath.Join(home, "escape"))
|
||||
s := &Server{Home: home, MaxMB: 1}
|
||||
|
||||
call := func(path string) string {
|
||||
return `{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"send_file","arguments":{"path":"` + path + `"}}}`
|
||||
}
|
||||
in := strings.Join([]string{
|
||||
`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18"}}`,
|
||||
`{"jsonrpc":"2.0","method":"notifications/initialized"}`,
|
||||
call("chart.png"), call(home + "/dir"), call(home + "/escape"), call("missing"),
|
||||
}, "\n")
|
||||
var out bytes.Buffer
|
||||
if err := s.Serve(strings.NewReader(in), &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
lines := strings.Split(strings.TrimSpace(out.String()), "\n")
|
||||
if len(lines) != 5 || !strings.Contains(lines[0], `"protocolVersion":"2025-06-18"`) {
|
||||
t.Fatalf("replies: %v", lines)
|
||||
}
|
||||
want := []struct {
|
||||
text string
|
||||
isErr bool
|
||||
}{{"queued", false}, {"regular file", true}, {"isn't under", true}, {"can't find", true}}
|
||||
for i, w := range want {
|
||||
var r struct {
|
||||
Result struct {
|
||||
Content []struct{ Text string }
|
||||
IsError bool
|
||||
}
|
||||
}
|
||||
_ = json.Unmarshal([]byte(lines[i+1]), &r)
|
||||
if r.Result.IsError != w.isErr || !strings.Contains(r.Result.Content[0].Text, w.text) {
|
||||
t.Fatalf("call %d: %s", i, lines[i+1])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"slackerbot/internal/acp"
|
||||
)
|
||||
|
||||
// Installed by nix/container.nix.
|
||||
const Command = "/run/current-system/sw/bin/slackerbot-tools"
|
||||
|
||||
// Main runs the server, for `slackerbot tools`.
|
||||
func Main(args []string) error {
|
||||
fs := flag.NewFlagSet("tools", flag.ExitOnError)
|
||||
home := fs.String("home", "/root", "the folder files may be sent from")
|
||||
maxMB := fs.Int64("max-file-mb", 100, "the largest file send_file accepts")
|
||||
_ = fs.Parse(args)
|
||||
s := &Server{Home: *home, MaxMB: *maxMB}
|
||||
return s.Serve(os.Stdin, os.Stdout)
|
||||
}
|
||||
|
||||
// Spec is how goose starts the server, passed in session/new and session/load.
|
||||
func Spec(home string, maxMB int64) acp.MCPServer {
|
||||
return acp.MCPServer{
|
||||
Name: ServerName,
|
||||
Command: Command,
|
||||
Args: []string{"tools", "-home", home, "-max-file-mb", strconv.FormatInt(maxMB, 10)},
|
||||
Env: []any{},
|
||||
}
|
||||
}
|
||||
|
||||
type FileRequest struct {
|
||||
Path string `json:"path"`
|
||||
Comment string `json:"comment"`
|
||||
}
|
||||
|
||||
// Calls watches a turn's ACP updates and collects send_file calls that completed.
|
||||
type Calls struct {
|
||||
mu sync.Mutex
|
||||
pending map[string]FileRequest
|
||||
done []FileRequest
|
||||
}
|
||||
|
||||
func isSendFile(u acp.Update) bool {
|
||||
return u.ToolName == ServerName+"__"+SendFile ||
|
||||
strings.HasPrefix(u.Title, ServerName+": "+strings.ReplaceAll(SendFile, "_", " "))
|
||||
}
|
||||
|
||||
func (c *Calls) Update(u acp.Update) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
switch u.Kind {
|
||||
case "tool_call":
|
||||
var req FileRequest
|
||||
if u.ToolID == "" || !isSendFile(u) || json.Unmarshal(u.RawInput, &req) != nil || req.Path == "" {
|
||||
return
|
||||
}
|
||||
if c.pending == nil {
|
||||
c.pending = map[string]FileRequest{}
|
||||
}
|
||||
c.pending[u.ToolID] = req
|
||||
case "tool_call_update":
|
||||
req, ok := c.pending[u.ToolID]
|
||||
if !ok || u.Status != "completed" && u.Status != "failed" {
|
||||
return
|
||||
}
|
||||
delete(c.pending, u.ToolID)
|
||||
if u.Status == "completed" {
|
||||
c.done = append(c.done, req)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Calls) Files() []FileRequest {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
return c.done
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"slackerbot/internal/acp"
|
||||
)
|
||||
|
||||
func TestCallsCollectsCompletedSends(t *testing.T) {
|
||||
var c Calls
|
||||
in := json.RawMessage(`{"path":"/root/a.png","comment":"here"}`)
|
||||
c.Update(acp.Update{Kind: "tool_call", ToolID: "1", ToolName: "slackerbot__send_file", RawInput: in})
|
||||
c.Update(acp.Update{Kind: "tool_call", ToolID: "2", Title: "slackerbot: send file · /root/b.zip", RawInput: json.RawMessage(`{"path":"/root/b.zip"}`)})
|
||||
c.Update(acp.Update{Kind: "tool_call", ToolID: "3", ToolName: "developer__shell", RawInput: json.RawMessage(`{"path":"/root/x"}`)})
|
||||
c.Update(acp.Update{Kind: "tool_call_update", ToolID: "1", Status: "completed"})
|
||||
c.Update(acp.Update{Kind: "tool_call_update", ToolID: "2", Status: "failed"})
|
||||
c.Update(acp.Update{Kind: "tool_call_update", ToolID: "3", Status: "completed"})
|
||||
|
||||
got := c.Files()
|
||||
if len(got) != 1 || got[0].Path != "/root/a.png" || got[0].Comment != "here" {
|
||||
t.Fatalf("got %+v", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user