feat(mcp): add sampling/createMessage support#2815
Merged
Conversation
rumpl
approved these changes
May 18, 2026
|
❌ PR Review Failed — The review agent encountered an error and could not complete the review. View logs. |
Requirements compliance (against #2809)
The two open items are human-in-the-loop (no approval UI) and model preferences (hints dropped rather than considered). Both are acknowledged as intentional in the design notes — flagging here for follow-up tracking. |
|
Thanks @dgageot for getting a jump on this, I was the OP. Would you mind if I filed a follow-up issue to implement tool callbacks to close the remaining functional gap? The "tools" aspect allows the LLM to invoke tool callbacks while processing the sampling message. Thanks again! sequenceDiagram
participant H as cagent
participant S as MCP Server
participant L as LLM
activate H
H->>+S: tools/call {name, arguments}
note over S: needs LLM inference
S->>+H: sampling/createMessage<br/>{messages, tools: [...]}
H->>+L: chat completion
L-->>-H: ToolUseContent<br/>stopReason: "toolUse"
H-->>-S: CreateMessageResult<br/>{tool_use, stopReason: "toolUse"}
note over S: executes tool locally
S->>+H: sampling/createMessage<br/>{messages + tool_use + tool_result, tools: [...]}
H->>+L: chat completion
L-->>-H: TextContent<br/>stopReason: "endTurn"
H-->>-S: CreateMessageResult<br/>{text, stopReason: "endTurn"}
S-->>-H: tool result
deactivate H
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2809.
Implements MCP sampling (
sampling/createMessage) so MCP servers attached to a docker-agent can ask the host to drive its own LLM, mirroring the existing elicitation plumbing.What
tools.SamplingHandlercapability (pkg/tools/sampling.go,pkg/tools/capabilities.go).SamplinginClientCapabilitiesand wiresCreateMessageHandlerfor both stdio and remote transports (pkg/tools/mcp/{mcp.go,session_client.go,stdio.go,remote.go}).pkg/runtime/sampling.go: converts inboundCreateMessageParamsto[]chat.Message(system prompt, text/image/audio, role mapping), clones the current agent's model with the requestedMaxTokens, drains the resulting stream and returns anmcp.CreateMessageResultwith the assistant text, model id and a translatedstopReason.pkg/runtime/loop.gopasses the sampling handler throughtools.ConfigureHandlers.Design notes
ModelPreferencesare advisory only; we use the agent's currently-selected model. Tool use is intentionally disabled (tools=nil) so sampling cannot recurse into the agent loop.Info; completion logs model + finish reason + byte count atDebug.Tests
pkg/runtime/sampling_test.gocovers the message conversion (text, system prompt, image data URL, audio fallback, default role, unsupported role), every rejection path (nil request, system-prompt-only, nil entry, oversize text/image/system-prompt, too-many-messages), the finish-reason → stopReason mapping, and the data-URL helper. Existing MCP client mocks (pkg/tools/mcp/{mcp_test.go,reconnect_test.go}) were updated for the new interface method.Validation
task lint— 0 issues (golangci-lint + custom cops +go mod tidy --diff)task test— all packages pass