Skip to content

Commit ab99b99

Browse files
committed
Implement the functional options pattern
1 parent e7b5afd commit ab99b99

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

cmd/mcptools/commands/test_helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func setupMockClient(executeFunc func(method string, _ any) (map[string]any, err
3030
mockClient := client.NewWithTransport(mockTransport)
3131

3232
// Override the function that creates clients
33-
CreateClientFunc = func(_ []string) (*client.Client, error) {
33+
CreateClientFunc = func(_ []string, _ ...client.Option) (*client.Client, error) {
3434
return mockClient, nil
3535
}
3636

cmd/mcptools/commands/utils.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var (
1717

1818
// CreateClientFunc is the function used to create MCP clients.
1919
// This can be replaced in tests to use a mock transport.
20-
var CreateClientFunc = func(args []string) (*client.Client, error) {
20+
var CreateClientFunc = func(args []string, opts ...client.Option) (*client.Client, error) {
2121
if len(args) == 0 {
2222
return nil, ErrCommandRequired
2323
}
@@ -42,7 +42,13 @@ var CreateClientFunc = func(args []string) (*client.Client, error) {
4242
return client.NewHTTP(args[0]), nil
4343
}
4444

45-
return client.NewStdio(args), nil
45+
c := client.NewStdio(args)
46+
47+
for _, opt := range opts {
48+
opt(c)
49+
}
50+
51+
return c, nil
4652
}
4753

4854
// ProcessFlags processes command line flags, sets the format option, and returns the remaining

pkg/client/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ type Client struct {
1818
transport transport.Transport
1919
}
2020

21+
// Option provides a way for passing options to the Client to change its
22+
// configuration.
23+
type Option func(*Client)
24+
2125
// NewWithTransport creates a new MCP client using the provided transport.
2226
// This allows callers to provide a custom transport implementation.
2327
func NewWithTransport(t transport.Transport) *Client {

0 commit comments

Comments
 (0)