mirror of
https://github.com/coder/coder.git
synced 2026-11-01 12:53:26 +08:00
## Stack Context PR 5 of 6 in a stack that migrates every Coder MCP surface from the archived `github.com/mark3labs/mcp-go` library to the official `github.com/modelcontextprotocol/go-sdk` v1.7.0. Stack: #28056 -> #28057 -> #28058 -> #28059 -> #28060 -> #28061 ## Why The aibridge injected-MCP proxy now owns an official `*mcp.Client`, `*mcp.StreamableClientTransport`, and `*mcp.ClientSession`. - The proxy constructor accepts an optional `*http.Client` instead of mark3labs options; the header-injecting wrapper shallow-copies a supplied client so its Timeout, Jar, and redirect policy survive. - Manual protocol version negotiation and the mark3labs five-second close workaround are removed; the SDK negotiates during `Connect` and fails when no mutually supported version exists. - Repeated `Init` closes the previous session, and a failed tool fetch closes the just-created session so transports do not leak. - Tool and intercept types use the official pointer content types; embedded resource blobs are re-encoded to base64 for model-facing text because the SDK decodes them into raw bytes. - `aibridge/mcpmock` is regenerated, and its stale `go:generate` source path is corrected. > Mux created this PR on Mike's behalf.
59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package mcp
|
|
|
|
import (
|
|
"flag"
|
|
"net/http"
|
|
)
|
|
|
|
// withHeaders shallow-copies base so client-level settings such as
|
|
// Timeout and Jar survive the transport wrap.
|
|
func withHeaders(base *http.Client, headers map[string]string) *http.Client {
|
|
client := &http.Client{}
|
|
if base != nil {
|
|
clone := *base
|
|
client = &clone
|
|
}
|
|
if client.Transport == nil {
|
|
client.Transport = http.DefaultTransport
|
|
}
|
|
if len(headers) > 0 {
|
|
client.Transport = &headerRoundTripper{
|
|
base: client.Transport,
|
|
headers: headers,
|
|
}
|
|
}
|
|
return client
|
|
}
|
|
|
|
type headerRoundTripper struct {
|
|
base http.RoundTripper
|
|
headers map[string]string
|
|
}
|
|
|
|
func (h *headerRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
clone := req.Clone(req.Context())
|
|
for k, v := range h.headers {
|
|
clone.Header.Set(k, v)
|
|
}
|
|
return h.base.RoundTrip(clone)
|
|
}
|
|
|
|
// mcpHTTPClient returns an isolated *http.Client when running
|
|
// inside tests, or nil for production. During tests,
|
|
// httptest.Server.Close() calls
|
|
// http.DefaultTransport.CloseIdleConnections(), which disrupts
|
|
// any MCP client sharing that transport. When DefaultTransport
|
|
// is a *http.Transport it is cloned; otherwise a minimal
|
|
// transport with ProxyFromEnvironment is created as a fallback.
|
|
func mcpHTTPClient() *http.Client {
|
|
if flag.Lookup("test.v") == nil {
|
|
return nil
|
|
}
|
|
if dt, ok := http.DefaultTransport.(*http.Transport); ok {
|
|
return &http.Client{Transport: dt.Clone()}
|
|
}
|
|
return &http.Client{Transport: &http.Transport{
|
|
Proxy: http.ProxyFromEnvironment,
|
|
}}
|
|
}
|