Files
archie/internal/tools/mcp/transport_http.go
T
sam 1586fef2b9 feat(mcp): answer server-initiated sampling from the chat model
MCP servers could not ask the client for a completion: the transports
routed only responses by id and dropped every server-initiated request,
so a server calling sampling/createMessage hung waiting for a reply.

Route server->client requests through the transport readers to a handler
on the client. The client answers sampling/createMessage from a
configured SamplingHandler and maps a missing handler, malformed params
and handler failure to JSON-RPC errors for that request alone -- never a
silent drop or a failed session. stdio and SSE implement the routing;
the stateless HTTP transport cannot receive the request at all and says
so in its doc.

The daemon wires the handler to its own chat runtime and active model
role, so a sample is answered by the model a chat turn uses, capped by
the request's maxTokens and the model's output ceiling, and recorded on
/status like every other model call. No model configured is a refused
request with a clear error.
2026-10-02 09:33:32 +10:00

121 lines
3.9 KiB
Go

package mcp
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"time"
)
// HTTPTransportConfig configures the HTTP/StreamableHTTP MCP transport.
type HTTPTransportConfig struct {
// Endpoint is the URL of the MCP server (e.g. "https://mcp.example.com/mcp").
// Required.
Endpoint string
// Headers are additional HTTP headers sent with every request (e.g.
// Authorization, custom API keys).
Headers map[string]string
// Timeout is the per-request timeout. Default: 30s.
Timeout time.Duration
}
func (c HTTPTransportConfig) effectiveTimeout() time.Duration {
if c.Timeout <= 0 {
return 30 * time.Second
}
return c.Timeout
}
// HTTPTransport implements [Transport] over HTTP/StreamableHTTP. Each
// [Send] makes an HTTP POST with the JSON-RPC request body and returns the
// JSON-RPC response. [Notify] fires an HTTP POST and discards the response
// body — notifications don't expect a JSON-RPC reply.
//
// This transport is stateless (no persistent connection), so Start/Stop
// are not needed. It satisfies the [Transport] interface directly. Being
// stateless, it has no server→client channel and therefore does not
// implement serverRequestRouter: a server-initiated request (MCP sampling)
// cannot arrive over it at all, rather than arriving and being dropped.
// A deployment that needs sampling must use stdio or SSE.
type HTTPTransport struct {
config HTTPTransportConfig
client *http.Client
}
// NewHTTPTransport creates a new HTTP transport with the given config.
func NewHTTPTransport(config HTTPTransportConfig) *HTTPTransport {
return &HTTPTransport{
config: config,
client: &http.Client{Timeout: config.effectiveTimeout()},
}
}
// Send posts a JSON-RPC request body to the server endpoint and returns the
// response body. The response body must be a valid JSON-RPC response.
func (t *HTTPTransport) Send(ctx context.Context, body []byte) ([]byte, error) {
resp, err := t.do(ctx, body)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
// Read up to 4 KB of the error body for diagnostics.
discard := io.LimitReader(resp.Body, 4096)
msg, _ := io.ReadAll(discard)
return nil, fmt.Errorf("mcp: HTTP %d: %s", resp.StatusCode, string(msg))
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("mcp: read response body: %w", err)
}
return data, nil
}
// Notify posts a JSON-RPC notification body to the server and discards the
// response. Notifications carry no "id" and never receive a reply.
func (t *HTTPTransport) Notify(ctx context.Context, body []byte) error {
resp, err := t.do(ctx, body)
if err != nil {
return err
}
// Drain and close the response body to return the connection to the pool.
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
return nil
}
// do sends a POST request with the given body and returns the HTTP response.
func (t *HTTPTransport) do(ctx context.Context, body []byte) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, t.config.Endpoint, bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("mcp: build request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json, text/event-stream")
for k, v := range t.config.Headers {
req.Header.Set(k, v)
}
resp, err := t.client.Do(req)
if err != nil {
return nil, fmt.Errorf("mcp: %w", err)
}
return resp, nil
}
// Start is a no-op: HTTP transport is always ready.
func (t *HTTPTransport) Start(_ context.Context) error { return nil }
// Stop is a no-op: HTTP transport has no persistent resources to release.
func (t *HTTPTransport) Stop(_ context.Context) error { return nil }
// State always returns StateRunning: HTTP is stateless, so the transport is
// always considered ready.
func (t *HTTPTransport) State() TransportState { return StateRunning }
// Compile-time interface check.
var _ Transport = (*HTTPTransport)(nil)