Skip to content

Commit

Permalink
✨ add enhanced logging configuration flags
Browse files Browse the repository at this point in the history
  • Loading branch information
wesen committed Jan 21, 2025
1 parent f7da7d4 commit e416046
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 58 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ package main
import (
"io"

"github.com/go-go-golems/go-mcp/pkg/prompts"
"github.com/go-go-golems/go-mcp/pkg/protocol"
"github.com/go-go-golems/go-mcp/pkg/resources"
"github.com/go-go-golems/go-mcp/pkg/server"
"github.com/go-go-golems/go-mcp/pkg/tools"
"github.com/go-go-golems/go-go-mcp/pkg/prompts"
"github.com/go-go-golems/go-go-mcp/pkg/protocol"
"github.com/go-go-golems/go-go-mcp/pkg/resources"
"github.com/go-go-golems/go-go-mcp/pkg/server"
"github.com/go-go-golems/go-go-mcp/pkg/tools"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -157,14 +157,14 @@ The client supports two transport types:
The command transport is the default way to interact with an MCP server:

```bash
# List available prompts
./mcp-client prompts list
# List available prompts (uses default command: mcp-server start --transport stdio)
./mcp-client --command ./mcp-server prompts list

# List available tools
./mcp-client tools list
./mcp-client --command ./mcp-server tools list

# Execute a prompt with arguments
./mcp-client prompts execute hello --args '{"name":"World"}'
./mcp-client --command ./mcp-server prompts execute hello --args '{"name":"World"}'

# Call a tool with arguments
./mcp-client tools call echo --args '{"message":"Hello, MCP!"}'
Expand All @@ -173,11 +173,11 @@ The command transport is the default way to interact with an MCP server:
You can customize the server command and arguments if needed:

```bash
# Use a different server binary
./mcp-client --command ./custom-server prompts list
# Use a different server binary with custom arguments
./mcp-client --command custom-server,start,--debug,--port,8001 prompts list

# Pass custom arguments to the server
./mcp-client --args start,--debug,--port,8001 prompts list
# Use a server with a specific configuration
./mcp-client -c mcp-server,start,--config,config.yaml prompts list
```

#### Using SSE Transport
Expand Down
44 changes: 33 additions & 11 deletions cmd/mcp-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"
"time"

"github.com/go-go-golems/go-mcp/pkg/client"
"github.com/go-go-golems/go-mcp/pkg/protocol"
"github.com/go-go-golems/go-go-mcp/pkg/client"
"github.com/go-go-golems/go-go-mcp/pkg/protocol"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
)
Expand All @@ -19,11 +19,12 @@ var (
GitCommit = "none"

// Command flags
transport string
serverURL string
debug bool
command string
cmdArgs []string
transport string
serverURL string
debug bool
cmdArgs []string
logLevel string
withCaller bool

// Operation flags
promptArgs string
Expand All @@ -37,19 +38,37 @@ func main() {
Long: `A Model Context Protocol (MCP) client CLI implementation.
Supports both stdio and SSE transports for client-server communication.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
level, err := zerolog.ParseLevel(logLevel)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid log level %s, defaulting to info\n", logLevel)
level = zerolog.InfoLevel
}
zerolog.SetGlobalLevel(level)
if debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
if withCaller {
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
return fmt.Sprintf("%s:%d", short, line)
}
}
},
}

// Add persistent flags
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "Enable debug logging")
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "info", "Log level (trace, debug, info, warn, error, fatal, panic)")
rootCmd.PersistentFlags().BoolVar(&withCaller, "with-caller", true, "Show caller information in logs")
rootCmd.PersistentFlags().StringVarP(&transport, "transport", "t", "command", "Transport type (command or sse)")
rootCmd.PersistentFlags().StringVarP(&serverURL, "server", "s", "http://localhost:8000", "Server URL for SSE transport")
rootCmd.PersistentFlags().StringVarP(&command, "command", "c", "mcp-server", "Command to run for command transport")
rootCmd.PersistentFlags().StringSliceVarP(&cmdArgs, "args", "a", []string{"start", "--transport", "stdio"}, "Command arguments for command transport")
rootCmd.PersistentFlags().StringSliceVarP(&cmdArgs, "command", "c", []string{"mcp-server", "start", "--transport", "stdio"}, "Command and arguments for command transport (first argument is the command)")

// Prompts command group
promptsCmd := &cobra.Command{
Expand Down Expand Up @@ -308,7 +327,10 @@ func createClient() (*client.Client, error) {

switch transport {
case "command":
t, err = client.NewCommandStdioTransport(command, cmdArgs...)
if len(cmdArgs) == 0 {
return nil, fmt.Errorf("command is required for command transport")
}
t, err = client.NewCommandStdioTransport(cmdArgs[0], cmdArgs[1:]...)
if err != nil {
return nil, fmt.Errorf("failed to create command transport: %w", err)
}
Expand Down
40 changes: 30 additions & 10 deletions cmd/mcp-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"os"
"time"

"github.com/go-go-golems/go-mcp/pkg/prompts"
"github.com/go-go-golems/go-mcp/pkg/protocol"
"github.com/go-go-golems/go-mcp/pkg/resources"
"github.com/go-go-golems/go-mcp/pkg/server"
"github.com/go-go-golems/go-mcp/pkg/tools"
"github.com/go-go-golems/go-go-mcp/pkg/prompts"
"github.com/go-go-golems/go-go-mcp/pkg/protocol"
"github.com/go-go-golems/go-go-mcp/pkg/resources"
"github.com/go-go-golems/go-go-mcp/pkg/server"
"github.com/go-go-golems/go-go-mcp/pkg/tools"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
)
Expand All @@ -22,9 +22,11 @@ var (
GitCommit = "none"

// Command flags
transport string
port int
debug bool
transport string
port int
debug bool
logLevel string
withCaller bool
)

func main() {
Expand All @@ -37,16 +39,34 @@ Supports both stdio and SSE transports for client-server communication.
The server implements the Model Context Protocol (MCP) specification,
providing a framework for building MCP servers and clients.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Configure logging
zerolog.SetGlobalLevel(zerolog.InfoLevel)
level, err := zerolog.ParseLevel(logLevel)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid log level %s, defaulting to info\n", logLevel)
level = zerolog.InfoLevel
}
zerolog.SetGlobalLevel(level)
if debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
if withCaller {
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
return fmt.Sprintf("%s:%d", short, line)
}
}
},
}

// Add persistent flags available to all commands
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "Enable debug logging")
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "info", "Log level (trace, debug, info, warn, error, fatal, panic)")
rootCmd.PersistentFlags().BoolVar(&withCaller, "with-caller", true, "Show caller information in logs")

// Start command
startCmd := &cobra.Command{
Expand Down
11 changes: 7 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/go-go-golems/go-mcp
module github.com/go-go-golems/go-go-mcp

go 1.22.3

Expand All @@ -10,15 +10,18 @@ require (
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.0.0-20191116160921-f9c825593386 // indirect
github.com/stretchr/testify v1.9.0 // indirect
golang.org/x/net v0.33.0 // indirect
gopkg.in/cenkalti/backoff.v1 v1.1.0 // indirect
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/spf13/cobra v1.8.1
golang.org/x/sys v0.12.0 // indirect
golang.org/x/sys v0.28.0 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
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=
Expand All @@ -12,11 +12,11 @@ github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLf
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/r3labs/sse/v2 v2.10.0 h1:hFEkLLFY4LDifoHdiCN/LlGBAdVJYsANaLqNYa1l/v0=
github.com/r3labs/sse/v2 v2.10.0/go.mod h1:Igau6Whc+F17QUgML1fYe1VPZzTV6EMCnYktEmkNJ7I=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
Expand All @@ -28,16 +28,16 @@ github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3k
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20191116160921-f9c825593386 h1:ktbWvQrW08Txdxno1PiDpSxPXG6ndGsfnJjRRtkM0LQ=
golang.org/x/net v0.0.0-20191116160921-f9c825593386/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/cenkalti/backoff.v1 v1.1.0 h1:Arh75ttbsvlpVA7WtVpH4u9h6Zl46xuptxqLxPiSo4Y=
gopkg.in/cenkalti/backoff.v1 v1.1.0/go.mod h1:J6Vskwqd+OMVJl8C33mmtxTBs2gyzfv7UDAkHu8BrjI=
Expand Down
3 changes: 2 additions & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"sync"

"github.com/go-go-golems/go-mcp/pkg/protocol"
"github.com/go-go-golems/go-go-mcp/pkg/protocol"
"github.com/rs/zerolog"
)

Expand Down Expand Up @@ -398,6 +398,7 @@ func (c *Client) Ping() error {

// Close closes the client connection
func (c *Client) Close() error {
c.logger.Debug().Msg("closing client")
return c.transport.Close()
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/client/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"
"sync"

"github.com/go-go-golems/go-mcp/pkg/protocol"
"github.com/go-go-golems/go-go-mcp/pkg/protocol"
"github.com/r3labs/sse/v2"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/client/stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os/exec"
"sync"

"github.com/go-go-golems/go-mcp/pkg/protocol"
"github.com/go-go-golems/go-go-mcp/pkg/protocol"
)

// StdioTransport implements Transport using standard input/output
Expand Down
4 changes: 2 additions & 2 deletions pkg/prompts/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"sort"
"sync"

"github.com/go-go-golems/go-mcp/pkg"
"github.com/go-go-golems/go-mcp/pkg/protocol"
"github.com/go-go-golems/go-go-mcp/pkg"
"github.com/go-go-golems/go-go-mcp/pkg/protocol"
)

// Registry provides a simple way to register individual prompts
Expand Down
2 changes: 1 addition & 1 deletion pkg/providers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pkg

import "github.com/go-go-golems/go-mcp/pkg/protocol"
import "github.com/go-go-golems/go-go-mcp/pkg/protocol"

// PromptProvider defines the interface for serving prompts
type PromptProvider interface {
Expand Down
4 changes: 2 additions & 2 deletions pkg/resources/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"sort"
"sync"

"github.com/go-go-golems/go-mcp/pkg"
"github.com/go-go-golems/go-mcp/pkg/protocol"
"github.com/go-go-golems/go-go-mcp/pkg"
"github.com/go-go-golems/go-go-mcp/pkg/protocol"
)

// Registry provides a simple way to register individual resources
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"os"
"sync"

"github.com/go-go-golems/go-mcp/pkg"
"github.com/go-go-golems/go-mcp/pkg/protocol"
"github.com/go-go-golems/go-go-mcp/pkg"
"github.com/go-go-golems/go-go-mcp/pkg/protocol"
"github.com/rs/zerolog"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/server/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"net/http"
"sync"

"github.com/go-go-golems/go-mcp/pkg"
"github.com/go-go-golems/go-mcp/pkg/protocol"
"github.com/go-go-golems/go-go-mcp/pkg"
"github.com/go-go-golems/go-go-mcp/pkg/protocol"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/rs/zerolog"
Expand Down
4 changes: 2 additions & 2 deletions pkg/tools/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"sort"
"sync"

"github.com/go-go-golems/go-mcp/pkg"
"github.com/go-go-golems/go-mcp/pkg/protocol"
"github.com/go-go-golems/go-go-mcp/pkg"
"github.com/go-go-golems/go-go-mcp/pkg/protocol"
)

// Registry provides a simple way to register individual tools
Expand Down

0 comments on commit e416046

Please sign in to comment.