Skip to content

Commit 24b4972

Browse files
remove most of the Client interface
* the Client interface is a duplicate of ClientSession
1 parent fffea27 commit 24b4972

File tree

14 files changed

+49
-222
lines changed

14 files changed

+49
-222
lines changed

cmd/docker-mcp/internal/gateway/capabilitites.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (g *Gateway) listCapabilities(ctx context.Context, configuration Configurat
6767

6868
var capabilities Capabilities
6969

70-
tools, err := client.ListTools(ctx, &mcp.ListToolsParams{})
70+
tools, err := client.Session().ListTools(ctx, &mcp.ListToolsParams{})
7171
if err != nil {
7272
logf(" > Can't list tools %s: %s", serverConfig.Name, err)
7373
} else {
@@ -82,7 +82,7 @@ func (g *Gateway) listCapabilities(ctx context.Context, configuration Configurat
8282
}
8383
}
8484

85-
prompts, err := client.ListPrompts(ctx, &mcp.ListPromptsParams{})
85+
prompts, err := client.Session().ListPrompts(ctx, &mcp.ListPromptsParams{})
8686
if err == nil {
8787
for _, prompt := range prompts.Prompts {
8888
capabilities.Prompts = append(capabilities.Prompts, PromptRegistration{
@@ -92,7 +92,7 @@ func (g *Gateway) listCapabilities(ctx context.Context, configuration Configurat
9292
}
9393
}
9494

95-
resources, err := client.ListResources(ctx, &mcp.ListResourcesParams{})
95+
resources, err := client.Session().ListResources(ctx, &mcp.ListResourcesParams{})
9696
if err == nil {
9797
for _, resource := range resources.Resources {
9898
capabilities.Resources = append(capabilities.Resources, ResourceRegistration{
@@ -102,7 +102,7 @@ func (g *Gateway) listCapabilities(ctx context.Context, configuration Configurat
102102
}
103103
}
104104

105-
resourceTemplates, err := client.ListResourceTemplates(ctx, &mcp.ListResourceTemplatesParams{})
105+
resourceTemplates, err := client.Session().ListResourceTemplates(ctx, &mcp.ListResourceTemplatesParams{})
106106
if err == nil {
107107
for _, resourceTemplate := range resourceTemplates.ResourceTemplates {
108108
capabilities.ResourceTemplates = append(capabilities.ResourceTemplates, ResourceTemplateRegistration{

cmd/docker-mcp/internal/gateway/clientpool.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (cp *clientPool) ReleaseClient(client mcpclient.Client) {
107107

108108
// Client was not kept, close it
109109
if !foundKept {
110-
client.Close()
110+
client.Session().Close()
111111
return
112112
}
113113

@@ -124,7 +124,7 @@ func (cp *clientPool) Close() {
124124
for _, keptClient := range existingMap {
125125
client, err := keptClient.Getter.GetClient(context.TODO()) // should be cached
126126
if err == nil {
127-
client.Close()
127+
client.Session().Close()
128128
}
129129
}
130130
}
@@ -388,7 +388,7 @@ func (cg *clientGetter) GetClient(ctx context.Context) (mcpclient.Client, error)
388388
} else {
389389
ss = nil
390390
}
391-
if _, err := client.Initialize(ctx, initParams, cg.cp.Verbose, ss); err != nil {
391+
if err := client.Initialize(ctx, initParams, cg.cp.Verbose, ss); err != nil {
392392
return nil, err
393393
}
394394

cmd/docker-mcp/internal/gateway/clientpool_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func TestStdioClientInitialization(t *testing.T) {
223223
defer clientPool.ReleaseClient(client)
224224

225225
// Test ListTools to verify the client is working
226-
tools, err := client.ListTools(ctx, &mcp.ListToolsParams{})
226+
tools, err := client.Session().ListTools(ctx, &mcp.ListToolsParams{})
227227
if err != nil {
228228
t.Fatalf("Failed to list tools: %v", err)
229229
}

cmd/docker-mcp/internal/gateway/handlers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (g *Gateway) mcpServerToolHandler(serverConfig catalog.ServerConfig, annota
4444
}
4545
defer g.clientPool.ReleaseClient(client)
4646

47-
return client.CallTool(ctx, genericParams)
47+
return client.Session().CallTool(ctx, genericParams)
4848
}
4949
}
5050

@@ -56,7 +56,7 @@ func (g *Gateway) mcpServerPromptHandler(serverConfig catalog.ServerConfig) mcp.
5656
}
5757
defer g.clientPool.ReleaseClient(client)
5858

59-
return client.GetPrompt(ctx, params)
59+
return client.Session().GetPrompt(ctx, params)
6060
}
6161
}
6262

@@ -68,7 +68,7 @@ func (g *Gateway) mcpServerResourceHandler(serverConfig catalog.ServerConfig) mc
6868
}
6969
defer g.clientPool.ReleaseClient(client)
7070

71-
return client.ReadResource(ctx, params)
71+
return client.Session().ReadResource(ctx, params)
7272
}
7373
}
7474

@@ -80,6 +80,6 @@ func (g *Gateway) mcpServerResourceTemplateHandler(serverConfig catalog.ServerCo
8080
}
8181
defer g.clientPool.ReleaseClient(client)
8282

83-
return client.ReadResource(ctx, params)
83+
return client.Session().ReadResource(ctx, params)
8484
}
8585
}

cmd/docker-mcp/internal/gateway/proxies.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func newClientWithCleanup(client mcp.Client, cleanup func(context.Context) error
3030
}
3131

3232
func (c *clientWithCleanup) Close() error {
33-
return errors.Join(c.Client.Close(), c.cleanup(context.TODO()))
33+
return errors.Join(c.Session().Close(), c.cleanup(context.TODO()))
3434
}
3535

3636
type clientWithCleanup struct {

cmd/docker-mcp/internal/gateway/run.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,13 @@ func (g *Gateway) reloadConfiguration(ctx context.Context, configuration Configu
222222
// Resource templates are handled as regular resources in the new SDK
223223
for _, template := range capabilities.ResourceTemplates {
224224
// Convert ResourceTemplate to Resource
225-
resource := &mcp.Resource{
226-
URI: template.ResourceTemplate.URITemplate,
225+
resource := &mcp.ResourceTemplate{
226+
URITemplate: template.ResourceTemplate.URITemplate,
227227
Name: template.ResourceTemplate.Name,
228228
Description: template.ResourceTemplate.Description,
229229
MIMEType: template.ResourceTemplate.MIMEType,
230230
}
231-
g.mcpServer.AddResource(resource, template.Handler)
231+
g.mcpServer.AddResourceTemplate(resource, template.Handler)
232232
}
233233

234234
g.health.SetHealthy()

cmd/docker-mcp/internal/gateway/transport.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ import (
1515

1616
func (g *Gateway) startStdioServer(ctx context.Context, _ io.Reader, _ io.Writer) error {
1717
transport := mcp.NewStdioTransport()
18-
_, err := g.mcpServer.Connect(ctx, transport)
19-
if err != nil {
20-
return err
21-
}
2218
return g.mcpServer.Run(ctx, transport)
2319
}
2420

cmd/docker-mcp/internal/mcp/mcp_client.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,8 @@ import (
99

1010
// Client interface wraps the official MCP SDK client with our legacy interface
1111
type Client interface {
12-
Initialize(ctx context.Context, params *mcp.InitializeParams, debug bool, serverSession *mcp.ServerSession) (*mcp.InitializeResult, error)
13-
ListTools(ctx context.Context, params *mcp.ListToolsParams) (*mcp.ListToolsResult, error)
14-
ListPrompts(ctx context.Context, params *mcp.ListPromptsParams) (*mcp.ListPromptsResult, error)
15-
ListResources(ctx context.Context, params *mcp.ListResourcesParams) (*mcp.ListResourcesResult, error)
16-
ListResourceTemplates(ctx context.Context, params *mcp.ListResourceTemplatesParams) (*mcp.ListResourceTemplatesResult, error)
17-
CallTool(ctx context.Context, params *mcp.CallToolParams) (*mcp.CallToolResult, error)
18-
GetPrompt(ctx context.Context, params *mcp.GetPromptParams) (*mcp.GetPromptResult, error)
19-
ReadResource(ctx context.Context, params *mcp.ReadResourceParams) (*mcp.ReadResourceResult, error)
20-
Close() error
12+
Initialize(ctx context.Context, params *mcp.InitializeParams, debug bool, serverSession *mcp.ServerSession) error
13+
Session() *mcp.ClientSession
2114
}
2215

2316
func stdioNotifications(serverSession *mcp.ServerSession) *mcp.ClientOptions {

cmd/docker-mcp/internal/mcp/remote.go

Lines changed: 6 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ func NewRemoteMCPClient(config catalog.ServerConfig) Client {
2525
}
2626
}
2727

28-
func (c *remoteMCPClient) Initialize(ctx context.Context, _ *mcp.InitializeParams, _ bool, _ *mcp.ServerSession) (*mcp.InitializeResult, error) {
28+
func (c *remoteMCPClient) Initialize(ctx context.Context, _ *mcp.InitializeParams, _ bool, _ *mcp.ServerSession) error {
2929
if c.initialized.Load() {
30-
return nil, fmt.Errorf("client already initialized")
30+
return fmt.Errorf("client already initialized")
3131
}
3232

3333
// Read configuration.
@@ -67,7 +67,7 @@ func (c *remoteMCPClient) Initialize(ctx context.Context, _ *mcp.InitializeParam
6767
// TODO: Need to implement custom HTTP client with headers for streaming
6868
mcpTransport = mcp.NewStreamableClientTransport(url, &mcp.StreamableClientTransportOptions{})
6969
default:
70-
return nil, fmt.Errorf("unsupported remote transport: %s", transport)
70+
return fmt.Errorf("unsupported remote transport: %s", transport)
7171
}
7272

7373
c.client = mcp.NewClient(&mcp.Implementation{
@@ -77,79 +77,17 @@ func (c *remoteMCPClient) Initialize(ctx context.Context, _ *mcp.InitializeParam
7777

7878
session, err := c.client.Connect(ctx, mcpTransport)
7979
if err != nil {
80-
return nil, fmt.Errorf("failed to connect: %w", err)
80+
return fmt.Errorf("failed to connect: %w", err)
8181
}
8282

8383
c.session = session
8484
c.initialized.Store(true)
8585

86-
// The Connect method handles initialization automatically in the new SDK
87-
return &mcp.InitializeResult{
88-
ProtocolVersion: "2024-11-05",
89-
ServerInfo: &mcp.Implementation{
90-
Name: "docker-mcp-gateway",
91-
Version: "1.0.0",
92-
},
93-
// Capabilities field is private and will be set by the SDK
94-
}, nil
95-
}
96-
97-
func (c *remoteMCPClient) ListTools(ctx context.Context, params *mcp.ListToolsParams) (*mcp.ListToolsResult, error) {
98-
if !c.initialized.Load() {
99-
return nil, fmt.Errorf("client not initialized")
100-
}
101-
return c.session.ListTools(ctx, params)
102-
}
103-
104-
func (c *remoteMCPClient) ListPrompts(ctx context.Context, params *mcp.ListPromptsParams) (*mcp.ListPromptsResult, error) {
105-
if !c.initialized.Load() {
106-
return nil, fmt.Errorf("client not initialized")
107-
}
108-
return c.session.ListPrompts(ctx, params)
109-
}
110-
111-
func (c *remoteMCPClient) ListResources(ctx context.Context, params *mcp.ListResourcesParams) (*mcp.ListResourcesResult, error) {
112-
if !c.initialized.Load() {
113-
return nil, fmt.Errorf("client not initialized")
114-
}
115-
return c.session.ListResources(ctx, params)
116-
}
117-
118-
func (c *remoteMCPClient) ListResourceTemplates(ctx context.Context, params *mcp.ListResourceTemplatesParams) (*mcp.ListResourceTemplatesResult, error) {
119-
if !c.initialized.Load() {
120-
return nil, fmt.Errorf("client not initialized")
121-
}
122-
return c.session.ListResourceTemplates(ctx, params)
123-
}
124-
125-
func (c *remoteMCPClient) CallTool(ctx context.Context, params *mcp.CallToolParams) (*mcp.CallToolResult, error) {
126-
if !c.initialized.Load() {
127-
return nil, fmt.Errorf("client not initialized")
128-
}
129-
return c.session.CallTool(ctx, params)
130-
}
131-
132-
func (c *remoteMCPClient) GetPrompt(ctx context.Context, params *mcp.GetPromptParams) (*mcp.GetPromptResult, error) {
133-
if !c.initialized.Load() {
134-
return nil, fmt.Errorf("client not initialized")
135-
}
136-
return c.session.GetPrompt(ctx, params)
137-
}
138-
139-
func (c *remoteMCPClient) ReadResource(ctx context.Context, params *mcp.ReadResourceParams) (*mcp.ReadResourceResult, error) {
140-
if !c.initialized.Load() {
141-
return nil, fmt.Errorf("client not initialized")
142-
}
143-
return c.session.ReadResource(ctx, params)
144-
}
145-
146-
func (c *remoteMCPClient) Close() error {
147-
if c.session != nil {
148-
return c.session.Close()
149-
}
15086
return nil
15187
}
15288

89+
func (c *remoteMCPClient) Session() *mcp.ClientSession { return c.session }
90+
15391
func expandEnv(value string, secrets map[string]string) string {
15492
return os.Expand(value, func(name string) string {
15593
return secrets[name]

cmd/docker-mcp/internal/mcp/stdio.go

Lines changed: 7 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ func NewStdioCmdClient(name string, command string, env []string, args ...string
3131
}
3232
}
3333

34-
func (c *stdioMCPClient) Initialize(ctx context.Context, _ *mcp.InitializeParams, debug bool, s *mcp.ServerSession) (*mcp.InitializeResult, error) {
34+
func (c *stdioMCPClient) Initialize(ctx context.Context, _ *mcp.InitializeParams, debug bool, s *mcp.ServerSession) error {
3535
if c.initialized.Load() {
36-
return nil, fmt.Errorf("client already initialized")
36+
return fmt.Errorf("client already initialized")
3737
}
3838

3939
cmd := exec.CommandContext(ctx, c.command, c.args...)
@@ -51,76 +51,18 @@ func (c *stdioMCPClient) Initialize(ctx context.Context, _ *mcp.InitializeParams
5151

5252
session, err := c.client.Connect(ctx, transport)
5353
if err != nil {
54-
return nil, fmt.Errorf("failed to connect: %w", err)
54+
return fmt.Errorf("failed to connect: %w", err)
5555
}
5656

5757
c.session = session
5858
c.initialized.Store(true)
5959

60-
// The Connect method handles initialization automatically in the new SDK
61-
// We just return a basic result structure
62-
return &mcp.InitializeResult{
63-
ProtocolVersion: "2024-11-05",
64-
ServerInfo: &mcp.Implementation{
65-
Name: "docker-mcp-gateway",
66-
Version: "1.0.0",
67-
},
68-
// Capabilities field is private and will be set by the SDK
69-
}, nil
70-
}
71-
72-
func (c *stdioMCPClient) ListTools(ctx context.Context, params *mcp.ListToolsParams) (*mcp.ListToolsResult, error) {
73-
if !c.initialized.Load() {
74-
return nil, fmt.Errorf("client not initialized")
75-
}
76-
return c.session.ListTools(ctx, params)
77-
}
78-
79-
func (c *stdioMCPClient) ListPrompts(ctx context.Context, params *mcp.ListPromptsParams) (*mcp.ListPromptsResult, error) {
80-
if !c.initialized.Load() {
81-
return nil, fmt.Errorf("client not initialized")
82-
}
83-
return c.session.ListPrompts(ctx, params)
84-
}
85-
86-
func (c *stdioMCPClient) ListResources(ctx context.Context, params *mcp.ListResourcesParams) (*mcp.ListResourcesResult, error) {
87-
if !c.initialized.Load() {
88-
return nil, fmt.Errorf("client not initialized")
89-
}
90-
return c.session.ListResources(ctx, params)
91-
}
92-
93-
func (c *stdioMCPClient) ListResourceTemplates(ctx context.Context, params *mcp.ListResourceTemplatesParams) (*mcp.ListResourceTemplatesResult, error) {
94-
if !c.initialized.Load() {
95-
return nil, fmt.Errorf("client not initialized")
96-
}
97-
return c.session.ListResourceTemplates(ctx, params)
98-
}
99-
100-
func (c *stdioMCPClient) CallTool(ctx context.Context, params *mcp.CallToolParams) (*mcp.CallToolResult, error) {
101-
if !c.initialized.Load() {
102-
return nil, fmt.Errorf("client not initialized")
103-
}
104-
return c.session.CallTool(ctx, params)
105-
}
106-
107-
func (c *stdioMCPClient) GetPrompt(ctx context.Context, params *mcp.GetPromptParams) (*mcp.GetPromptResult, error) {
108-
if !c.initialized.Load() {
109-
return nil, fmt.Errorf("client not initialized")
110-
}
111-
return c.session.GetPrompt(ctx, params)
60+
return nil
11261
}
11362

114-
func (c *stdioMCPClient) ReadResource(ctx context.Context, params *mcp.ReadResourceParams) (*mcp.ReadResourceResult, error) {
63+
func (c *stdioMCPClient) Session() *mcp.ClientSession {
11564
if !c.initialized.Load() {
116-
return nil, fmt.Errorf("client not initialized")
65+
panic("client not initialize")
11766
}
118-
return c.session.ReadResource(ctx, params)
119-
}
120-
121-
func (c *stdioMCPClient) Close() error {
122-
if c.session != nil {
123-
return c.session.Close()
124-
}
125-
return nil
67+
return c.session
12668
}

0 commit comments

Comments
 (0)