diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 6b0a85cd3..6b39c5d80 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -1370,7 +1370,7 @@ func (b *boot) registerTools(ctx context.Context) error { log.Info("workspace tools disabled (chat.workspace is unset)") } for _, srv := range cfg.Tools.MCPServers { - provider, err := configuredMCPProvider(srv, cfg.WorkDir) + provider, err := configuredMCPProvider(srv, cfg.WorkDir, b.mcpSamplingHandler()) if err != nil { log.Warn("mcp tool provider skipped", "name", srv.Name, "err", err) continue diff --git a/internal/app/archied/main.go b/internal/app/archied/main.go index 1443e11a6..109aba70c 100644 --- a/internal/app/archied/main.go +++ b/internal/app/archied/main.go @@ -198,7 +198,7 @@ func npmCacheServerEnv(command, workDir string) []string { return mcp.NpmCacheEnv(command, filepath.Join(workDir, "mcp-npm-cache")) } -func configuredMCPProvider(server config.MCPServer, workDir string) (toolprovider.Engine, error) { +func configuredMCPProvider(server config.MCPServer, workDir string, sampling mcp.SamplingHandler) (toolprovider.Engine, error) { name := strings.TrimSpace(server.Name) if name == "" { return nil, fmt.Errorf("MCP server name is required") @@ -208,6 +208,8 @@ func configuredMCPProvider(server config.MCPServer, workDir string) (toolprovide transportType = "stdio" } + samplingOption := mcptoolprovider.WithSamplingHandler(sampling) + switch transportType { case "stdio": command := strings.TrimSpace(server.Command) @@ -220,7 +222,7 @@ func configuredMCPProvider(server config.MCPServer, workDir string) (toolprovide Dir: server.WorkDir, Env: npmCacheServerEnv(command, workDir), }) - return mcptoolprovider.New(name, transport, server.ParallelToolCalls), nil + return mcptoolprovider.New(name, transport, server.ParallelToolCalls, samplingOption), nil case "http", "streamablehttp": url := strings.TrimSpace(server.URL) @@ -231,7 +233,7 @@ func configuredMCPProvider(server config.MCPServer, workDir string) (toolprovide Endpoint: url, Headers: server.Headers, }) - return mcptoolprovider.New(name, transport, server.ParallelToolCalls), nil + return mcptoolprovider.New(name, transport, server.ParallelToolCalls, samplingOption), nil case "sse": sseEndpoint := strings.TrimSpace(server.SSEEndpoint) @@ -243,7 +245,7 @@ func configuredMCPProvider(server config.MCPServer, workDir string) (toolprovide MessageEndpoint: strings.TrimSpace(server.MessageEndpoint), Headers: server.Headers, }) - return mcptoolprovider.New(name, transport, server.ParallelToolCalls), nil + return mcptoolprovider.New(name, transport, server.ParallelToolCalls, samplingOption), nil default: return nil, fmt.Errorf("MCP transport %q is not supported", transportType) diff --git a/internal/app/archied/main_test.go b/internal/app/archied/main_test.go index b267ce800..801628929 100644 --- a/internal/app/archied/main_test.go +++ b/internal/app/archied/main_test.go @@ -434,7 +434,7 @@ func TestConfiguredMCPProviderSupportsAllTransportTypes(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - provider, err := configuredMCPProvider(tt.server, t.TempDir()) + provider, err := configuredMCPProvider(tt.server, t.TempDir(), nil) if (err != nil) != tt.wantErr { t.Fatalf("configuredMCPProvider() error = %v, wantErr %t", err, tt.wantErr) } diff --git a/internal/app/archied/mcp_sampling.go b/internal/app/archied/mcp_sampling.go new file mode 100644 index 000000000..52ba499f3 --- /dev/null +++ b/internal/app/archied/mcp_sampling.go @@ -0,0 +1,108 @@ +package archied + +import ( + "context" + "errors" + "fmt" + + "github.com/samcharles93/ai-sdk/chat" + "github.com/samcharles93/ai-sdk/core" + + "github.com/samcharles93/archie-core/internal/tools/mcp" +) + +// samplingDefaultMaxTokens bounds a server-requested completion when the +// request does not set maxTokens. A sampling request is a delegated +// sub-completion, not a full turn; this keeps one MCP server from spending a +// turn-sized output allowance on a single request. +const samplingDefaultMaxTokens = 4096 + +// mcpSamplingHandler answers an MCP server's sampling/createMessage request +// from the daemon's own chat model, so a server can ask for a completion +// without archie opening a second provider path. A missing runtime or model +// is a refused request, never a dropped one: the client maps the returned +// error to a JSON-RPC error response for that request alone, leaving the +// server session intact. +func (b *boot) mcpSamplingHandler() mcp.SamplingHandler { + return func(ctx context.Context, req mcp.SamplingRequest) (mcp.SamplingResult, error) { + llm := b.chatLLM() + if llm == nil { + return mcp.SamplingResult{}, errors.New("mcp sampling: no model runtime is configured") + } + if b.chatModels == nil { + return mcp.SamplingResult{}, errors.New("mcp sampling: no chat model is configured") + } + model := b.chatModels.ActiveModel() + if model == "" { + return mcp.SamplingResult{}, errors.New("mcp sampling: no chat model is configured") + } + messages, err := samplingMessages(req) + if err != nil { + return mcp.SamplingResult{}, err + } + options := core.GenerateOptions{ + Messages: messages, + System: req.SystemPrompt, + MaxSteps: 1, + MaxTokens: samplingMaxTokens(req, b.chatModels, model), + } + if req.Temperature != nil { + options.Temperature = float32(*req.Temperature) + } + result, err := llm.Chat(ctx, model, options) + // A sampling call is a model call this process made, so it is + // recorded for /status like every other one; a nil recorder records + // nothing. + b.providerOutcomes.record(model, err) + if err != nil { + return mcp.SamplingResult{}, fmt.Errorf("mcp sampling: %w", err) + } + return mcp.SamplingResult{ + Model: model, + Role: "assistant", + Content: mcp.SamplingContent{Type: "text", Text: result.Text}, + StopReason: string(result.FinishReason), + }, nil + } +} + +// samplingMessages maps the server's messages onto chat messages. A +// non-text content block or an unknown role is rejected rather than silently +// dropped: answering from a partial prompt would be a wrong answer, not a +// degraded one. +func samplingMessages(req mcp.SamplingRequest) ([]chat.Message, error) { + if len(req.Messages) == 0 { + return nil, errors.New("mcp sampling: the request has no messages") + } + messages := make([]chat.Message, 0, len(req.Messages)) + for i, message := range req.Messages { + if message.Content.Type != "" && message.Content.Type != "text" { + return nil, fmt.Errorf("mcp sampling: message %d has unsupported content type %q", i, message.Content.Type) + } + var role chat.Role + switch message.Role { + case "user": + role = chat.RoleUser + case "assistant": + role = chat.RoleAssistant + default: + return nil, fmt.Errorf("mcp sampling: message %d has unsupported role %q", i, message.Role) + } + messages = append(messages, chat.Message{Role: role, Content: message.Content.Text}) + } + return messages, nil +} + +// samplingMaxTokens bounds the completion to the request's own maxTokens, +// falling back to a conservative default, and never past the model's own +// output ceiling. +func samplingMaxTokens(req mcp.SamplingRequest, models *chatModelManager, model string) int { + maxTokens := req.MaxTokens + if maxTokens <= 0 { + maxTokens = samplingDefaultMaxTokens + } + if details, ok := models.ModelDetails(model); ok && details.MaxOutputTokens > 0 && maxTokens > details.MaxOutputTokens { + return details.MaxOutputTokens + } + return maxTokens +} diff --git a/internal/app/archied/mcp_sampling_test.go b/internal/app/archied/mcp_sampling_test.go new file mode 100644 index 000000000..9740e2f5a --- /dev/null +++ b/internal/app/archied/mcp_sampling_test.go @@ -0,0 +1,110 @@ +package archied + +import ( + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/samcharles93/archie-core/internal/agentexec" + "github.com/samcharles93/archie-core/internal/tools/mcp" +) + +func newSamplingTestRuntime(t *testing.T) *httptest.Server { + t.Helper() + api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = io.WriteString(w, `{"id":"chatcmpl-1","object":"chat.completion","created":1,"model":"gpt-5.6",`+ + `"choices":[{"index":0,"message":{"role":"assistant","content":"four"},"finish_reason":"stop"}],`+ + `"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`) + })) + t.Cleanup(api.Close) + return api +} + +// TestMCPSamplingHandlerAnswersFromConfiguredModel proves a server-initiated +// sampling request is answered by the daemon's own chat model and recorded on +// /status like every other model call this process makes. +func TestMCPSamplingHandlerAnswersFromConfiguredModel(t *testing.T) { + api := newSamplingTestRuntime(t) + + b := newBootstrap() + b.setLLM(agentexec.NewRuntime(map[string]agentexec.Provider{ + "openai": {Class: "openai", BaseURL: api.URL}, + })) + b.chatModels = newChatModelManager(map[string]string{"chat": "openai/gpt-5.6"}) + b.providerOutcomes = newProviderOutcomeRecorder() + + result, err := b.mcpSamplingHandler()(t.Context(), mcp.SamplingRequest{ + Messages: []mcp.SamplingMessage{{ + Role: "user", + Content: mcp.SamplingContent{Type: "text", Text: "2+2?"}, + }}, + MaxTokens: 16, + }) + if err != nil { + t.Fatalf("sampling handler = %v, want a completion", err) + } + if result.Model != "openai/gpt-5.6" { + t.Errorf("result.Model = %q, want the configured chat model", result.Model) + } + if result.Role != "assistant" || result.Content.Type != "text" || result.Content.Text != "four" { + t.Errorf("result = %+v, want an assistant text completion", result) + } + outcome, attempted := b.providerOutcomes.LastChatModelOutcome() + if !attempted || outcome.Model != "openai/gpt-5.6" || outcome.Err != "" { + t.Errorf("recorded outcome = %+v (attempted %v), want the sampling call recorded as successful", outcome, attempted) + } +} + +// TestMCPSamplingHandlerRefusesCleanlyWithNoModel proves a daemon with no +// usable model answers the request with an error instead of panicking or +// dropping it: the server sees a failed request, not a dead session. +func TestMCPSamplingHandlerRefusesCleanlyWithNoModel(t *testing.T) { + tests := []struct { + name string + wired bool + want string + }{ + {name: "no model runtime", wired: false, want: "no model runtime"}, + {name: "no chat model configured", wired: true, want: "no chat model"}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + b := newBootstrap() + if tc.wired { + b.setLLM(agentexec.NewRuntime(map[string]agentexec.Provider{ + "openai": {Class: "openai"}, + })) + } + b.chatModels = newChatModelManager(nil) + + _, err := b.mcpSamplingHandler()(t.Context(), mcp.SamplingRequest{ + Messages: []mcp.SamplingMessage{{ + Role: "user", + Content: mcp.SamplingContent{Type: "text", Text: "hi"}, + }}, + }) + if err == nil { + t.Fatal("sampling handler returned no error with no usable model") + } + if !strings.Contains(err.Error(), tc.want) { + t.Errorf("error = %q, want it to mention %q", err, tc.want) + } + }) + } +} + +func TestSamplingMessagesRejectsUnsupportedContent(t *testing.T) { + _, err := samplingMessages(mcp.SamplingRequest{ + Messages: []mcp.SamplingMessage{{ + Role: "user", + Content: mcp.SamplingContent{Type: "image"}, + }}, + }) + if err == nil || !strings.Contains(err.Error(), "unsupported content type") { + t.Fatalf("samplingMessages error = %v, want an unsupported-content refusal", err) + } +} diff --git a/internal/tools/mcp/client.go b/internal/tools/mcp/client.go index 01be934fa..1e6488136 100644 --- a/internal/tools/mcp/client.go +++ b/internal/tools/mcp/client.go @@ -37,6 +37,21 @@ type Client struct { // whose config declares it handles concurrent requests gets no callMu: // nil means this server's calls are never held apart. callMu *sync.Mutex + // samplingHandler answers server-initiated sampling/createMessage + // requests. Set once at construction, so reading it from a transport + // reader goroutine needs no lock. Nil means the method is refused. + samplingHandler SamplingHandler +} + +// ClientOption adjusts a Client at construction. +type ClientOption func(*Client) + +// WithSamplingHandler answers server-initiated sampling/createMessage +// requests from the supplied handler. Without it the client answers the +// method with a JSON-RPC method-not-found error, per the MCP spec -- never +// a silent drop. +func WithSamplingHandler(handler SamplingHandler) ClientOption { + return func(c *Client) { c.samplingHandler = handler } } // NewClient builds a Client over transport. serverName identifies this @@ -44,14 +59,54 @@ type Client struct { // self-reported name. parallelToolCalls drops the per-server serialization // of tools/call: false (the default) keeps one call in flight at a time, // true lets the caller's own concurrency through. -func NewClient(transport Transport, serverName string, parallelToolCalls bool) *Client { +// +// A transport that can receive server-initiated requests gets this client +// registered as their handler; one that cannot (the stateless HTTP +// transport) has no server→client channel for them to arrive on. +func NewClient(transport Transport, serverName string, parallelToolCalls bool, opts ...ClientOption) *Client { c := &Client{transport: transport, serverName: serverName} if !parallelToolCalls { c.callMu = &sync.Mutex{} } + for _, opt := range opts { + opt(c) + } + if router, ok := transport.(serverRequestRouter); ok { + router.SetServerRequestHandler(c.handleServerRequest) + } return c } +// handleServerRequest dispatches one server-initiated request. The MCP spec +// defines sampling/createMessage as the request a server sends to a client; +// everything else is method-not-found. A request this client cannot answer +// always gets an explicit JSON-RPC error response -- dropping it would leave +// the server blocked on a reply that never comes. +func (c *Client) handleServerRequest(ctx context.Context, method string, params json.RawMessage) (json.RawMessage, *ErrorData) { + if method != "sampling/createMessage" { + return nil, &ErrorData{Code: ErrCodeMethodNotFound, Message: "method not found: " + method} + } + if c.samplingHandler == nil { + return nil, &ErrorData{ + Code: ErrCodeMethodNotFound, + Message: "sampling/createMessage is not supported: no sampling handler is configured", + } + } + var req SamplingRequest + if err := json.Unmarshal(params, &req); err != nil { + return nil, &ErrorData{Code: ErrCodeInvalidParams, Message: "invalid sampling/createMessage params: " + err.Error()} + } + result, err := c.samplingHandler(ctx, req) + if err != nil { + return nil, &ErrorData{Code: ErrCodeInternal, Message: err.Error()} + } + body, err := json.Marshal(result) + if err != nil { + return nil, &ErrorData{Code: ErrCodeInternal, Message: "marshal sampling result: " + err.Error()} + } + return body, nil +} + // InitializeResult is the server's response to initialize. type InitializeResult struct { ProtocolVersion string `json:"protocolVersion"` diff --git a/internal/tools/mcp/sampling.go b/internal/tools/mcp/sampling.go new file mode 100644 index 000000000..c11320d14 --- /dev/null +++ b/internal/tools/mcp/sampling.go @@ -0,0 +1,76 @@ +package mcp + +import ( + "context" + "encoding/json" +) + +// JSON-RPC 2.0 error codes used when answering a server-initiated request. +// These are spec-defined and part of the MCP wire contract: the server +// distinguishes "this client cannot do that" from "the request was +// malformed" from "the handler failed" by the code, not the message. +const ( + // ErrCodeMethodNotFound is returned when this client has no idea how + // to answer the requested method, including a sampling request when no + // sampling handler is configured. It is never a silent drop. + ErrCodeMethodNotFound = -32601 + // ErrCodeInvalidParams is returned when the request's params cannot be + // decoded into the method's shape. + ErrCodeInvalidParams = -32602 + // ErrCodeInternal is returned when the configured handler ran but + // failed. The message carries the handler's own error text. + ErrCodeInternal = -32603 +) + +// SamplingContent is one content block in a sampling message. The MCP +// sampling method sends text today; the type field is carried so an +// unsupported block is rejected explicitly rather than silently emptied. +type SamplingContent struct { + Type string `json:"type"` + Text string `json:"text,omitempty"` +} + +// SamplingMessage is one conversation message supplied by the MCP server. +type SamplingMessage struct { + Role string `json:"role"` + Content SamplingContent `json:"content"` +} + +// SamplingRequest is the params of a sampling/createMessage request: the +// server asks this client to run a completion on its behalf. +type SamplingRequest struct { + Messages []SamplingMessage `json:"messages"` + SystemPrompt string `json:"systemPrompt,omitempty"` + MaxTokens int `json:"maxTokens,omitempty"` + Temperature *float64 `json:"temperature,omitempty"` + StopSequences []string `json:"stopSequences,omitempty"` +} + +// SamplingResult is the result of a sampling/createMessage request. +type SamplingResult struct { + Model string `json:"model"` + Role string `json:"role"` + Content SamplingContent `json:"content"` + StopReason string `json:"stopReason,omitempty"` +} + +// SamplingHandler answers one server-initiated sampling request from a +// configured model. It returns a Go error for a refused or failed +// completion; the client maps that to a JSON-RPC internal error so the +// server sees the failure for its own request only, never a dropped +// session. +type SamplingHandler func(ctx context.Context, req SamplingRequest) (SamplingResult, error) + +// ServerRequestHandler answers one request the MCP server sent to this +// client. A non-nil *ErrorData becomes a JSON-RPC error response; +// otherwise result becomes the success response. The MCP wire contract +// permits both, and a client must never drop a request it cannot answer. +type ServerRequestHandler func(ctx context.Context, method string, params json.RawMessage) (json.RawMessage, *ErrorData) + +// serverRequestRouter is implemented by transports that can receive +// server-initiated requests over their server→client channel. The +// stateless HTTP transport cannot (it has no persistent stream) and +// therefore does not implement it. +type serverRequestRouter interface { + SetServerRequestHandler(handler ServerRequestHandler) +} diff --git a/internal/tools/mcp/sampling_test.go b/internal/tools/mcp/sampling_test.go new file mode 100644 index 000000000..00e14e9b9 --- /dev/null +++ b/internal/tools/mcp/sampling_test.go @@ -0,0 +1,201 @@ +package mcp + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "io" + "strings" + "sync" + "testing" + "time" +) + +// samplingRouterTransport is a Transport double that also receives +// server-initiated requests, so the client's dispatch can be tested without +// a subprocess. It captures the handler the client registers. +type samplingRouterTransport struct { + mu sync.Mutex + handler ServerRequestHandler +} + +func (t *samplingRouterTransport) Send(context.Context, []byte) ([]byte, error) { + return nil, errors.New("samplingRouterTransport: Send is not used by these tests") +} + +func (t *samplingRouterTransport) Notify(context.Context, []byte) error { return nil } + +func (t *samplingRouterTransport) SetServerRequestHandler(handler ServerRequestHandler) { + t.mu.Lock() + defer t.mu.Unlock() + t.handler = handler +} + +func (t *samplingRouterTransport) registered() ServerRequestHandler { + t.mu.Lock() + defer t.mu.Unlock() + return t.handler +} + +func samplingParams(t *testing.T) json.RawMessage { + t.Helper() + return json.RawMessage(`{"messages":[{"role":"user","content":{"type":"text","text":"2+2?"}}],"maxTokens":16}`) +} + +func TestServerSamplingRequestIsAnswered(t *testing.T) { + transport := &samplingRouterTransport{} + var got SamplingRequest + NewClient(transport, "sampling-server", false, WithSamplingHandler( + func(_ context.Context, req SamplingRequest) (SamplingResult, error) { + got = req + return SamplingResult{ + Model: "openai/gpt-5.6", + Role: "assistant", + Content: SamplingContent{Type: "text", Text: "four"}, + StopReason: "stop", + }, nil + }, + )) + + handler := transport.registered() + if handler == nil { + t.Fatal("client did not register a server request handler with the transport") + } + + result, rpcErr := handler(context.Background(), "sampling/createMessage", samplingParams(t)) + if rpcErr != nil { + t.Fatalf("sampling request answered with RPC error %+v, want a result", rpcErr) + } + if len(got.Messages) != 1 || got.Messages[0].Content.Text != "2+2?" { + t.Errorf("handler received %+v, want the request's message text", got) + } + if got.MaxTokens != 16 { + t.Errorf("handler received maxTokens %d, want 16", got.MaxTokens) + } + + var decoded SamplingResult + if err := json.Unmarshal(result, &decoded); err != nil { + t.Fatalf("unmarshal sampling result: %v", err) + } + if decoded.Content.Text != "four" || decoded.Model != "openai/gpt-5.6" { + t.Errorf("sampling result = %+v, want the handler's completion", decoded) + } +} + +func TestServerSamplingRequestWithoutHandlerIsMethodNotFound(t *testing.T) { + transport := &samplingRouterTransport{} + NewClient(transport, "sampling-server", false) + + handler := transport.registered() + if handler == nil { + t.Fatal("client did not register a server request handler with the transport") + } + + _, rpcErr := handler(context.Background(), "sampling/createMessage", samplingParams(t)) + if rpcErr == nil { + t.Fatal("sampling request with no configured handler returned a result, want a JSON-RPC error") + } + if rpcErr.Code != ErrCodeMethodNotFound { + t.Errorf("error code = %d, want %d (method not found)", rpcErr.Code, ErrCodeMethodNotFound) + } +} + +func TestServerSamplingHandlerFailureIsInternalError(t *testing.T) { + transport := &samplingRouterTransport{} + NewClient(transport, "sampling-server", false, WithSamplingHandler( + func(context.Context, SamplingRequest) (SamplingResult, error) { + return SamplingResult{}, errors.New("model unavailable") + }, + )) + + _, rpcErr := transport.registered()(context.Background(), "sampling/createMessage", samplingParams(t)) + if rpcErr == nil { + t.Fatal("handler failure returned a result, want a JSON-RPC error") + } + if rpcErr.Code != ErrCodeInternal { + t.Errorf("error code = %d, want %d (internal)", rpcErr.Code, ErrCodeInternal) + } + if !strings.Contains(rpcErr.Message, "model unavailable") { + t.Errorf("error message = %q, want the handler's own error text", rpcErr.Message) + } +} + +func TestServerUnknownRequestIsMethodNotFound(t *testing.T) { + transport := &samplingRouterTransport{} + NewClient(transport, "sampling-server", false, WithSamplingHandler( + func(context.Context, SamplingRequest) (SamplingResult, error) { + t.Error("sampling handler must not run for an unknown method") + return SamplingResult{}, nil + }, + )) + + _, rpcErr := transport.registered()(context.Background(), "roots/list", json.RawMessage(`{}`)) + if rpcErr == nil || rpcErr.Code != ErrCodeMethodNotFound { + t.Fatalf("unknown method error = %+v, want method not found", rpcErr) + } +} + +type nopWriteCloser struct{ io.Writer } + +func (nopWriteCloser) Close() error { return nil } + +// TestStdioTransportAnswersServerSamplingRequest drives the transport's +// reader directly: a server-initiated frame read off stdout must reach the +// handler and its response must be written back to stdin with the server's +// own request id, or the server hangs waiting for a completion it never +// gets. +func TestStdioTransportAnswersServerSamplingRequest(t *testing.T) { + var out bytes.Buffer + transport := NewStdioTransport(StdioTransportConfig{Command: "unused"}) + transport.stdin = nopWriteCloser{&out} + // No subprocess is spawned here: the reader path under test only writes a + // response when the transport considers itself live. + transport.mu.Lock() + transport.state = StateRunning + transport.mu.Unlock() + + handled := make(chan SamplingRequest, 1) + transport.SetServerRequestHandler( + func(_ context.Context, method string, params json.RawMessage) (json.RawMessage, *ErrorData) { + if method != "sampling/createMessage" { + return nil, &ErrorData{Code: ErrCodeMethodNotFound, Message: method} + } + var req SamplingRequest + if err := json.Unmarshal(params, &req); err != nil { + return nil, &ErrorData{Code: ErrCodeInvalidParams, Message: err.Error()} + } + handled <- req + body, err := json.Marshal(SamplingResult{ + Model: "openai/gpt-5.6", + Role: "assistant", + Content: SamplingContent{Type: "text", Text: "four"}, + }) + if err != nil { + return nil, &ErrorData{Code: ErrCodeInternal, Message: err.Error()} + } + return body, nil + }, + ) + + transport.deliverResponse(t.Context(), []byte( + `{"jsonrpc":"2.0","id":42,"method":"sampling/createMessage",`+ + `"params":{"messages":[{"role":"user","content":{"type":"text","text":"ping"}}]}}`, + )) + + select { + case req := <-handled: + if len(req.Messages) != 1 || req.Messages[0].Content.Text != "ping" { + t.Errorf("handler received %+v, want the request's message text", req) + } + case <-time.After(2 * time.Second): + t.Fatal("server sampling request was not routed to the handler") + } + + // The handler goroutine writes the response after it returns; wait for it + // before reading the shared buffer. + transport.serverRequestWg.Wait() + if !strings.Contains(out.String(), `"id":42`) { + t.Fatalf("stdio transport did not write a response for request id 42; wrote %q", out.String()) + } +} diff --git a/internal/tools/mcp/transport.go b/internal/tools/mcp/transport.go index ec4a87bb7..6ce5b08e4 100644 --- a/internal/tools/mcp/transport.go +++ b/internal/tools/mcp/transport.go @@ -119,6 +119,26 @@ type StdioTransport struct { // permissions, etc.). Reset to 0 on a successful spawn. When MaxRetries // is set and this exceeds it, the transport enters StateError. startupFailures int + + // serverRequestHandler answers requests the server sends to this client + // (sampling/createMessage). Nil until a Client registers itself. + serverRequestHandler ServerRequestHandler + // handlerCancel ends the lifecycle context handed to the server-request + // handler, so an in-flight sampling completion is not left running after + // the transport is gone. The handler context itself is carried through + // the reader call chain rather than stored. + handlerCancel context.CancelFunc + // serverRequestWg tracks in-flight handler goroutines so Stop can wait + // for their responses to be written before the transport goes away. + serverRequestWg sync.WaitGroup +} + +// SetServerRequestHandler registers the handler for server-initiated +// requests. It implements serverRequestRouter. +func (t *StdioTransport) SetServerRequestHandler(handler ServerRequestHandler) { + t.mu.Lock() + defer t.mu.Unlock() + t.serverRequestHandler = handler } // NewStdioTransport creates a new transport with the given config. @@ -173,12 +193,18 @@ func (t *StdioTransport) Start(ctx context.Context) error { } } t.stopCh = make(chan struct{}) + // The reader (and thus every server-request handler it dispatches) runs + // on a cancelable context derived from the caller's, decoupled from the + // caller's own cancellation the same way the subprocess is. Stop cancels + // it so an in-flight sampling completion ends with the transport. + handlerCtx, cancel := context.WithCancel(context.WithoutCancel(ctx)) + t.handlerCancel = cancel // Reset crash and failure counters on explicit user start. t.crashCount = 0 t.startupFailures = 0 t.mu.Unlock() - return t.startSubprocess(context.WithoutCancel(ctx)) + return t.startSubprocess(handlerCtx) } // stopSubprocess kills the current subprocess and waits for it to exit. @@ -235,13 +261,21 @@ func (t *StdioTransport) Stop(_ context.Context) error { close(t.stopCh) } } + cancel := t.handlerCancel t.mu.Unlock() + if cancel != nil { + cancel() + } + // Kill the subprocess. t.stopSubprocess() - // Wait for the reader goroutine to finish. + // Wait for the reader goroutine to finish, then for any in-flight + // server-request handler it dispatched. Waiting for the reader first + // guarantees no further handler goroutines can be added. t.readerWg.Wait() + t.serverRequestWg.Wait() // Fail all pending requests. t.mu.Lock() @@ -456,21 +490,28 @@ func (t *StdioTransport) runReader(ctx context.Context, reader *bufio.Reader) { return } - t.deliverResponse(body) + t.deliverResponse(ctx, body) } } -// deliverResponse routes a response body to the caller waiting for it. -func (t *StdioTransport) deliverResponse(body []byte) { - msgID, err := extractMessageID(body) - if err != nil { - // Cannot route a message without an ID. This could be a server - // notification (no ID) -- we silently drop it rather than blocking. - // In a full MCP client implementation, notifications would be - // handled via a callback. +// deliverResponse routes one message read from the subprocess's stdout. A +// response goes to the caller waiting on its id; a server-initiated request +// goes to the registered handler; a notification has no response to route. +func (t *StdioTransport) deliverResponse(ctx context.Context, body []byte) { + var msg Message + if err := json.Unmarshal(body, &msg); err != nil { + return + } + if msg.IsRequest() { + t.dispatchServerRequest(ctx, msg) + return + } + if len(msg.ID) == 0 { + // A server notification carries no response to route. return } + msgID := string(msg.ID) t.mu.Lock() ch, ok := t.pending[msgID] if ok { @@ -490,6 +531,55 @@ func (t *StdioTransport) deliverResponse(body []byte) { } } +// dispatchServerRequest runs the registered handler for a server-initiated +// request without blocking the reader, and writes its response back. A +// missing handler is answered method-not-found, never dropped: the server is +// blocked on a reply for its own request id. +func (t *StdioTransport) dispatchServerRequest(ctx context.Context, msg Message) { + t.mu.Lock() + handler := t.serverRequestHandler + t.mu.Unlock() + + t.serverRequestWg.Add(1) + go func() { + defer t.serverRequestWg.Done() + var ( + result json.RawMessage + rpcErr *ErrorData + ) + if handler == nil { + rpcErr = &ErrorData{Code: ErrCodeMethodNotFound, Message: "method not found: " + msg.Method} + } else { + result, rpcErr = handler(ctx, msg.Method, msg.Params) + } + t.respondToServerRequest(msg.ID, result, rpcErr) + }() +} + +// respondToServerRequest writes a JSON-RPC response for a server-initiated +// request back to the subprocess's stdin. +func (t *StdioTransport) respondToServerRequest(id, result json.RawMessage, rpcErr *ErrorData) { + data, err := json.Marshal(Message{JSONRPC: "2.0", ID: id, Result: result, Error: rpcErr}) + if err != nil { + return + } + + t.mu.Lock() + stdin := t.stdin + running := t.state == StateRunning + t.mu.Unlock() + if !running || stdin == nil { + return + } + + t.writeMu.Lock() + defer t.writeMu.Unlock() + // The error is unreportable here: the transport's only job is to answer + // the server's own request, and a failed write is exactly a subprocess + // that has already gone away (its reader handles the restart). + _ = writeMessageWithTimeout(stdin, data, t.config.SendTimeout) +} + // handleProcessDeath is called when the reader detects the subprocess // has died. It increments crashCount for backoff, transitions to Starting, // closes pending channels, and initiates the auto-restart sequence. diff --git a/internal/tools/mcp/transport_http.go b/internal/tools/mcp/transport_http.go index 5aa3d58e0..880ffa432 100644 --- a/internal/tools/mcp/transport_http.go +++ b/internal/tools/mcp/transport_http.go @@ -34,7 +34,11 @@ func (c HTTPTransportConfig) effectiveTimeout() time.Duration { // 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. +// 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 diff --git a/internal/tools/mcp/transport_sse.go b/internal/tools/mcp/transport_sse.go index 0d1f2879b..819bf027a 100644 --- a/internal/tools/mcp/transport_sse.go +++ b/internal/tools/mcp/transport_sse.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "context" + "encoding/json" "fmt" "io" "net/http" @@ -85,6 +86,23 @@ type SSETransport struct { readerWg sync.WaitGroup // Track HTTP response body so Stop can close it. respBody io.Closer + + // serverRequestHandler answers requests the server sends to this client + // (sampling/createMessage). Nil until a Client registers itself. + serverRequestHandler ServerRequestHandler + // serverRequestWg tracks in-flight handler goroutines so Stop waits for + // their responses to be posted before the connection is closed. The + // handler context is the SSE lifecycle context, carried through the + // reader call chain and cancelled by Stop. + serverRequestWg sync.WaitGroup +} + +// SetServerRequestHandler registers the handler for server-initiated +// requests. It implements serverRequestRouter. +func (t *SSETransport) SetServerRequestHandler(handler ServerRequestHandler) { + t.mu.Lock() + defer t.mu.Unlock() + t.serverRequestHandler = handler } // NewSSETransport creates a new SSE transport with the given config. @@ -142,6 +160,10 @@ func (t *SSETransport) Stop(_ context.Context) error { t.readerWg.Wait() + // The reader is done, so no further handler goroutines can be added; + // wait for the responses of any already dispatched. + t.serverRequestWg.Wait() + t.mu.Lock() for id, ch := range t.pending { close(ch) @@ -281,7 +303,7 @@ func (t *SSETransport) runReader(ctx context.Context, body io.Closer, reader *bu } switch event { case "message": - t.deliverMessage(data) + t.deliverMessage(ctx, data) case "endpoint": // Server sent a new endpoint URL (session refresh). // Already handled on connect; accept mid-stream updates too. @@ -379,14 +401,23 @@ func nextBackoff(d, ceiling time.Duration) time.Duration { // ── Delivery ───────────────────────────────────────────────────────────── -// deliverMessage routes an incoming SSE message event to the caller waiting -// for it, keyed by JSON-RPC ID. -func (t *SSETransport) deliverMessage(data string) { - msgID, err := extractMessageID([]byte(data)) - if err != nil { +// deliverMessage routes an incoming SSE message event. A response goes to +// the caller waiting on its id; a server-initiated request goes to the +// registered handler; a notification has no response to route. +func (t *SSETransport) deliverMessage(ctx context.Context, data string) { + var msg Message + if err := json.Unmarshal([]byte(data), &msg); err != nil { + return + } + if msg.IsRequest() { + t.dispatchServerRequest(ctx, msg) + return + } + if len(msg.ID) == 0 { return } + msgID := string(msg.ID) t.mu.Lock() ch, ok := t.pending[msgID] if ok { @@ -402,6 +433,37 @@ func (t *SSETransport) deliverMessage(data string) { } } +// dispatchServerRequest runs the registered handler for a server-initiated +// request without blocking the reader, and posts its response back to the +// message endpoint. +func (t *SSETransport) dispatchServerRequest(ctx context.Context, msg Message) { + t.mu.Lock() + handler := t.serverRequestHandler + t.mu.Unlock() + + t.serverRequestWg.Add(1) + go func() { + defer t.serverRequestWg.Done() + var ( + result json.RawMessage + rpcErr *ErrorData + ) + if handler == nil { + rpcErr = &ErrorData{Code: ErrCodeMethodNotFound, Message: "method not found: " + msg.Method} + } else { + result, rpcErr = handler(ctx, msg.Method, msg.Params) + } + body, err := json.Marshal(Message{JSONRPC: "2.0", ID: msg.ID, Result: result, Error: rpcErr}) + if err != nil { + return + } + // Unreportable here: the server's request has no other answer path, + // and a failed POST is a connection the reader is already + // reconnecting. + _ = t.postJSON(ctx, body) + }() +} + // ── POST ───────────────────────────────────────────────────────────────── // postJSON sends a JSON-RPC body to the message endpoint via POST. diff --git a/internal/tools/provider/contract_test.go b/internal/tools/provider/contract_test.go index ca9f33dd2..e797e641e 100644 --- a/internal/tools/provider/contract_test.go +++ b/internal/tools/provider/contract_test.go @@ -56,7 +56,7 @@ func TestArchiedWiresTypedProvidersAndExecutableConsumers(t *testing.T) { // root was decomposed; the receiver makes the arg expressions // carry the b. prefix there. "toolprovider.NewRegistry(b.toolReg)", - "configuredMCPProvider(srv, cfg.WorkDir)", + "configuredMCPProvider(srv, cfg.WorkDir, b.mcpSamplingHandler())", "capabilityHost.Register(b.providerRegistry)", // The chat turn builds its toolset from the registry before the // system prompt is rendered, so the prompt can advertise exactly diff --git a/internal/tools/provider/mcp/provider.go b/internal/tools/provider/mcp/provider.go index f81f1a7ab..e6bf3b1a4 100644 --- a/internal/tools/provider/mcp/provider.go +++ b/internal/tools/provider/mcp/provider.go @@ -52,18 +52,38 @@ type Provider struct { // mediaCallSeq disambiguates concurrent calls to the same tool so // their written media files never share a directory. mediaCallSeq atomic.Int64 + + // samplingHandler answers server-initiated sampling/createMessage + // requests. Nil leaves the method refused, which is the clean failure + // for a server that asks without a sampling handler configured. + samplingHandler protocol.SamplingHandler +} + +// Option adjusts a Provider at construction. +type Option func(*Provider) + +// WithSamplingHandler answers server-initiated sampling/createMessage +// requests from the supplied handler. It is the daemon's configured model +// path, so an MCP server's sampling request is answered by the same model +// a chat turn uses rather than a second provider client. +func WithSamplingHandler(handler protocol.SamplingHandler) Option { + return func(p *Provider) { p.samplingHandler = handler } } // New creates an MCP tool provider. parallelToolCalls mirrors the // configured server's own flag: false (the default) has the provider's // client serialize tools/call, true lets them overlap. -func New(name string, transport LifecycleTransport, parallelToolCalls bool) *Provider { - return &Provider{ +func New(name string, transport LifecycleTransport, parallelToolCalls bool, opts ...Option) *Provider { + p := &Provider{ name: strings.TrimSpace(name), segment: sanitizeToolSegment(name), transport: transport, parallelToolCalls: parallelToolCalls, } + for _, opt := range opts { + opt(p) + } + return p } // Manifest declares the MCP provider's tool capability. @@ -103,7 +123,8 @@ func (p *Provider) Start(ctx context.Context) error { if err := p.transport.Start(ctx); err != nil { return fmt.Errorf("start MCP server %q: %w", p.name, err) } - client := protocol.NewClient(p.transport, p.name, p.parallelToolCalls) + client := protocol.NewClient(p.transport, p.name, p.parallelToolCalls, + protocol.WithSamplingHandler(p.samplingHandler)) if _, err := client.Initialize(ctx); err != nil { cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), cleanupTimeout) stopErr := p.transport.Stop(cleanupCtx) diff --git a/internal/tools/provider/mcp/provider_test.go b/internal/tools/provider/mcp/provider_test.go index 5b653688c..9ddf84b1e 100644 --- a/internal/tools/provider/mcp/provider_test.go +++ b/internal/tools/provider/mcp/provider_test.go @@ -378,6 +378,16 @@ type fakeTransport struct { // overlap. callEntered chan struct{} callRelease chan struct{} + + // serverRequestHandler is captured from the client at construction; a + // test invokes it to stand in for a server-initiated request. + serverRequestHandler protocol.ServerRequestHandler +} + +func (t *fakeTransport) SetServerRequestHandler(handler protocol.ServerRequestHandler) { + t.mu.Lock() + defer t.mu.Unlock() + t.serverRequestHandler = handler } func newFakeTransport() *fakeTransport { @@ -477,6 +487,43 @@ func (t *fakeTransport) Send(_ context.Context, body []byte) ([]byte, error) { }) } +// TestProviderWiresSamplingHandlerToClient proves the handler a daemon +// supplies reaches the client the provider builds, so a server-initiated +// sampling request is answered from the daemon's model path rather than +// refused (or silently dropped). +func TestProviderWiresSamplingHandlerToClient(t *testing.T) { + transport := newFakeTransport() + provider := New("sampling-server", transport, false, WithSamplingHandler( + func(context.Context, protocol.SamplingRequest) (protocol.SamplingResult, error) { + return protocol.SamplingResult{ + Model: "openai/gpt-5.6", + Role: "assistant", + Content: protocol.SamplingContent{Type: "text", Text: "four"}, + }, nil + }, + )) + if err := provider.Start(context.Background()); err != nil { + t.Fatalf("Start: %v", err) + } + + handler := transport.serverRequestHandler + if handler == nil { + t.Fatal("provider did not pass the sampling handler to its client") + } + result, rpcErr := handler(context.Background(), "sampling/createMessage", + json.RawMessage(`{"messages":[{"role":"user","content":{"type":"text","text":"2+2?"}}]}`)) + if rpcErr != nil { + t.Fatalf("sampling answered with RPC error %+v, want a result", rpcErr) + } + var decoded protocol.SamplingResult + if err := json.Unmarshal(result, &decoded); err != nil { + t.Fatalf("unmarshal result: %v", err) + } + if decoded.Content.Text != "four" { + t.Errorf("sampling result = %+v, want the handler's completion", decoded) + } +} + func (t *fakeTransport) Notify(_ context.Context, body []byte) error { t.mu.Lock() defer t.mu.Unlock()