MCPcopy Index your code
hub / github.com/docker/docker-agent / Initialize

Method Initialize

pkg/tools/mcp/remote.go:80–135  ·  view source on GitHub ↗
(ctx context.Context, _ *gomcp.InitializeRequest)

Source from the content-addressed store, hash-verified

78}
79
80func (c *remoteMCPClient) Initialize(ctx context.Context, _ *gomcp.InitializeRequest) (*gomcp.InitializeResult, error) {
81 // Create HTTP client with OAuth support. We keep a reference to the
82 // oauthTransport so we can enrich Connect errors with the server's own
83 // explanation — without this, a plain `Bad Request` bubbles up and the
84 // user has no idea that, say, the Slack app hasn't been enabled for MCP.
85 httpClient, oauthT, err := c.createHTTPClient()
86 if err != nil {
87 return nil, fmt.Errorf("creating HTTP client: %w", err)
88 }
89
90 var transport gomcp.Transport
91
92 switch c.transportType {
93 case "sse":
94 transport = &gomcp.SSEClientTransport{
95 Endpoint: c.url,
96 HTTPClient: httpClient,
97 }
98 case "streamable", "streamable-http":
99 transport = &gomcp.StreamableClientTransport{
100 Endpoint: c.url,
101 HTTPClient: httpClient,
102 DisableStandaloneSSE: true,
103 }
104 default:
105 return nil, fmt.Errorf("unsupported transport type: %s", c.transportType)
106 }
107
108 // Create an MCP client with elicitation support
109 impl := &gomcp.Implementation{
110 Name: "docker agent",
111 Version: "1.0.0",
112 }
113
114 toolChanged, promptChanged := c.notificationHandlers()
115
116 opts := &gomcp.ClientOptions{
117 ElicitationHandler: c.handleElicitationRequest,
118 CreateMessageHandler: c.handleSamplingRequest,
119 ToolListChangedHandler: toolChanged,
120 PromptListChangedHandler: promptChanged,
121 }
122
123 client := gomcp.NewClient(impl, opts)
124
125 // Connect to the MCP server
126 session, err := client.Connect(ctx, transport, nil)
127 if err != nil {
128 return nil, enrichConnectError(err, oauthT)
129 }
130
131 c.setSession(session)
132
133 slog.DebugContext(ctx, "Remote MCP client connected successfully")
134 return session.InitializeResult(), nil
135}
136
137// enrichConnectError wraps the error returned by client.Connect with any

Callers

nothing calls this directly

Calls 5

createHTTPClientMethod · 0.95
enrichConnectErrorFunction · 0.85
notificationHandlersMethod · 0.80
setSessionMethod · 0.80
ConnectMethod · 0.65

Tested by

no test coverage detected