Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GOEXPERIMENT=jsonv2
3 changes: 3 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
version: '3'

dotenv:
- .env

tasks:
default:
desc: 'Default task is to "build"'
Expand Down
81 changes: 53 additions & 28 deletions cmd/buttplug-mcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
package main

import (
"context"
"fmt"
"log"
"log/slog"
"os"
"os/signal"
"sync"

"github.com/ConAcademy/buttplug-mcp/internal/bp"
"github.com/ConAcademy/buttplug-mcp/internal/mcp"
Expand All @@ -18,7 +22,7 @@ const (
mcpServerVersion = "0.0.1"

defaultSSEHostPort = ":8889"
defaultLogDest = "buttplug-mcp.log"
defaultLogDest = "butplug-mcp.log"
)

type Config struct {
Expand All @@ -33,24 +37,23 @@ type Config struct {

func main() {
var config Config
var showHelp bool
var logFilename string

pflag.StringVarP(&logFilename, "log-file", "l", "", "Log file destination (or MCP_LOG_FILE envvar). Default is stderr")
pflag.BoolVarP(&config.LogJSON, "log-json", "j", false, "Log in JSON (default is plaintext)")
pflag.StringVarP(&config.MCPConfig.SSEHostPort, "sse-host", "", "", "host:port to listen to SSE connections")
pflag.BoolVarP(&config.MCPConfig.UseSSE, "sse", "", false, "Use SSE Transport (default is STDIO transport)")
pflag.IntVarP(&config.BPConfig.WsPort, "ws-port", "", 0, "port to connect to the Buttplug Websocket server")
pflag.DurationVarP(&config.BPConfig.DebounceDuration, "debounce", "d", bp.DefaultDebounceDuration, "duration for debounce (default is 20Hz = '50ms')")
pflag.BoolVarP(&config.MCPConfig.UseSSE, "sse", "", false, "Use SSE Transport (default is stdio transport)")
pflag.StringVarP(&config.MCPConfig.ToolDescriptionsFile, "tool-descriptions", "", "", "Path to YAML file with tool descriptions to override defaults (run `tool-descriptions' to see current descriptions in YAML)")
pflag.StringVarP(&config.BPConfig.WebsocketHost, "ws-host", "", "localhost", "host to connect to the Buttplug Websocket server")
pflag.IntVarP(&config.BPConfig.WebsocketPort, "ws-port", "", 12345, "port to connect to the Buttplug Websocket server")
pflag.BoolVarP(&config.Verbose, "verbose", "v", false, "Verbose logging")
pflag.BoolVarP(&showHelp, "help", "h", false, "Show help")
pflag.Parse()

if showHelp {
fmt.Fprintf(os.Stdout, "usage: %s [opts]\n\n", os.Args[0])
pflag.Usage = func() {
o := pflag.CommandLine.Output()
fmt.Fprintf(o, "usage: %s [|start|tool-descriptions] [flags]\n", os.Args[0])
fmt.Fprintf(o, "flags:\n")
pflag.PrintDefaults()
os.Exit(0)
}
pflag.Parse()

if config.MCPConfig.SSEHostPort == "" {
config.MCPConfig.SSEHostPort = defaultSSEHostPort
Expand All @@ -67,8 +70,7 @@ func main() {
if logFilename != "" {
logFile, err := os.OpenFile(logFilename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to open log file: %s\n", err.Error())
os.Exit(1)
log.Fatal("failed to open log file:", err)
}
logWriter = logFile
defer logFile.Close()
Expand All @@ -86,26 +88,49 @@ func main() {
logger = slog.New(slog.NewTextHandler(logWriter, &slog.HandlerOptions{Level: logLevel}))
}

// Run our Buttplug manager
var err error
bpManager, err := bp.NewManager(config.BPConfig, logger)
bpm, err := bp.NewManager(config.BPConfig, logger)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to start Buttplug manager: %s\n", err.Error())
os.Exit(1)
log.Fatal("failed to create Buttplug manager:", err)
}
go func() {
err := bpManager.Run()

mcps, err := mcp.New(config.MCPConfig, bpm, logger)
if err != nil {
log.Fatal("failed to create MCP server:", err)
}

switch pflag.Arg(0) {
case "tool-descriptions":
yaml, err := mcp.FormatToolDescriptionsAsYAML(mcps.ToolDescriptions())
if err != nil {
fmt.Fprintf(os.Stderr, "bpm failed: %s\n", err.Error())
os.Exit(1)
log.Fatal("failed to format tool descriptions as YAML:", err)
}
}()
fmt.Println(string(yaml))
return

// Run our MCP server
if err := mcp.RunRouter(config.MCPConfig, bpManager, logger); err != nil {
logger.Error("mcp router error", "error", err.Error())
os.Exit(1)
case "start", "":
// continue to starting the server

default:
log.Fatalf("unknown command: %s", pflag.Arg(0))
}

// TODO: I guess we should clean up the buttplug?
var wg sync.WaitGroup
defer wg.Wait()

ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

wg.Go(func() {
if err := bpm.Run(ctx); err != nil {
logger.Error(
"buttplug manager error, program is useless now",
"error", err.Error())
}
})

if err := mcps.Start(); err != nil {
logger.Error(
"failed to start MCP server:",
"error", err.Error())
}
}
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
module github.com/ConAcademy/buttplug-mcp

go 1.24.1
go 1.25.0

require (
github.com/diamondburned/go-buttplug v0.0.3
github.com/goccy/go-yaml v1.19.0
github.com/mark3labs/mcp-go v0.31.0
github.com/puzpuzpuz/xsync/v4 v4.2.0
github.com/spf13/pflag v1.0.6
libdb.so/go-buttplug v0.0.8
)

require (
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
github.com/coder/websocket v1.8.14 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
)
31 changes: 18 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/alecthomas/assert/v2 v2.8.1 h1:YCxnYR6jjpfnEK5AK5SysALKdUEBPGH4Y7As6tBnDw0=
github.com/alecthomas/assert/v2 v2.8.1/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM=
github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
github.com/coder/websocket v1.8.14 h1:9L0p0iKiNOibykf283eHkKUHHrpG7f65OE3BhhO7v9g=
github.com/coder/websocket v1.8.14/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/diamondburned/go-buttplug v0.0.3 h1:tBRkOMm2GXKWVUOvwnszAGUM4tCF9CUsJyE/PKzlQFY=
github.com/diamondburned/go-buttplug v0.0.3/go.mod h1:2WnC+qL7dg3vB6Hiv9HuITG/eYFsCQ1CETh1qmLUq1s=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/goccy/go-yaml v1.19.0 h1:EmkZ9RIsX+Uq4DYFowegAuJo8+xdX3T/2dwNPXbxEYE=
github.com/goccy/go-yaml v1.19.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mark3labs/mcp-go v0.31.0 h1:4UxSV8aM770OPmTvaVe/b1rA2oZAjBMhGBfUgOGut+4=
github.com/mark3labs/mcp-go v0.31.0/go.mod h1:rXqOudj/djTORU/ThxYx8fqEVj/5pvTuuebQ2RC7uk4=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/neilotoole/slogt v1.1.0 h1:c7qE92sq+V0yvCuaxph+RQ2jOKL61c4hqS1Bv9W7FZE=
github.com/neilotoole/slogt v1.1.0/go.mod h1:RCrGXkPc/hYybNulqQrMHRtvlQ7F6NktNVLuLwk6V+w=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/qri-io/jsonpointer v0.1.1/go.mod h1:DnJPaYgiKu56EuDp8TU5wFLdZIcAnb/uH9v37ZaMV64=
github.com/qri-io/jsonschema v0.2.1/go.mod h1:g7DPkiOsK1xv6T/Ao5scXRkd+yTFygcANPBaaqW+VrI=
github.com/puzpuzpuz/xsync/v4 v4.2.0 h1:dlxm77dZj2c3rxq0/XNvvUKISAmovoXF4a4qM6Wvkr0=
github.com/puzpuzpuz/xsync/v4 v4.2.0/go.mod h1:VJDmTCJMBt8igNxnkQd86r+8KUeN1quSfNKu5bLYFQo=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
libdb.so/go-buttplug v0.0.8 h1:icQrfxEShgQ9l6NouOULq2C1WF/AY/1hKujOSVb7uiY=
libdb.so/go-buttplug v0.0.8/go.mod h1:IxRlJOOshEEHMky0lL+f1kSB94Ccqu7PhvGu6VqUXSo=
Loading