Skip to content

feat(everything): add trigger-agentic-sampling tool#3163

Draft
olaservo wants to merge 12 commits intomodelcontextprotocol:mainfrom
olaservo:add-sampling-with-tools
Draft

feat(everything): add trigger-agentic-sampling tool#3163
olaservo wants to merge 12 commits intomodelcontextprotocol:mainfrom
olaservo:add-sampling-with-tools

Conversation

@olaservo
Copy link
Copy Markdown
Member

Description

Adds support for SEP-1577 (Sampling With Tools) to the Everything server by introducing a new trigger-agentic-sampling tool that demonstrates the agentic sampling loop pattern.

This tool:

  • Sends a prompt to the client's LLM with tools available
  • Handles tool_use responses and executes tools locally
  • Loops until a final text response is received or max iterations reached
  • Only registers if the client supports sampling.tools capability

Server Details

  • Server: everything
  • Changes to: tools

Motivation and Context

Issue: #3040

SEP-1577 adds tool calling support to the MCP sampling API, enabling servers to provide tools that the LLM can use during sampling requests. This creates an "agentic loop" where:

  1. Server sends sampling/createMessage with a tools array
  2. If the LLM returns stopReason: "toolUse", the server executes the tools
  3. Server sends tool results back and continues until final response

The Everything server is a reference implementation that demonstrates all MCP features, so it should include this capability.

How Has This Been Tested?

(We can wait to merge until I've had a chance to add a screengrab if that's preferred.)

Breaking Changes

No

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Protocol Documentation
  • My changes follows MCP security best practices
  • I have updated the server's README accordingly
  • I have tested this with an LLM client
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have documented all environment variables and configuration options

Additional context

References

Implementation Notes

The tool includes simple local implementations of echo and add tools for demonstration purposes. In a production scenario, tools could likely be reused via:

  • Internal server.server.callTool() calls
  • Connecting to other MCP servers as a client
  • Shared business logic functions

I thought about trying to re-use existing tools already defined in the everything server, but then decided to keep it self-contained instead.

olaservo and others added 2 commits December 28, 2025 21:29
Adds a new tool that demonstrates sampling with tools capability
(MCP 2025-11-25). The tool sends prompts to the LLM with tools
available, handles tool_use responses in an agentic loop, and
executes tools locally until a final response is received.

🦉 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Update features.md and structure.md to document the new
trigger-agentic-sampling tool. Also adds missing documentation
for trigger-elicitation-request.

🦉 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
domdomegg
domdomegg previously approved these changes Jan 5, 2026
Copy link
Copy Markdown
Member

@domdomegg domdomegg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Copy Markdown
Member

@cliffhall cliffhall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't test with the Inspector yet (saw your mention of an issue with the SDK on that), but review did turn up a few minor things.

Comment thread src/everything/tools/trigger-agentic-sampling.ts Outdated
Comment thread src/everything/tools/trigger-agentic-sampling.ts Outdated
Comment thread src/everything/tools/trigger-agentic-sampling.ts Outdated
Comment thread src/everything/tools/trigger-agentic-sampling.ts Outdated
Comment thread src/everything/tools/trigger-agentic-sampling.ts Outdated
Comment thread src/everything/tools/trigger-agentic-sampling.ts Outdated
Address PR review feedback: remove console.log statements that would
interfere with STDIO JSON-RPC output. Registration now silently skips
when client doesn't support sampling.tools (matching other conditional
tools). Remaining debug logs changed to console.error.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Match the pattern used by other conditional tools: wrap registerTool
in `if (clientSupports...) { }` instead of early return.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@olaservo olaservo requested a review from cliffhall January 9, 2026 12:32
@cliffhall
Copy link
Copy Markdown
Member

Hi @olaservo. If you could resolve the conflicts and include some screenshots, it would be helpful. I'm still not fully up to speed on what this looks like for the Inspector side of things, so I need to do some reseach.

olaservo and others added 2 commits January 24, 2026 12:08
Resolve conflicts in tools/index.ts and docs/features.md by including
both the new trigger-agentic-sampling tool (SEP-1577) and the task-based
tools (SEP-1686) from main.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@olaservo
Copy link
Copy Markdown
Member Author

Fixed the conflicts, working on some screenshots from my custom client to illustrate the flow (I think I might update the example here too in the process).

…history

Retain complete LLM response (text + tool_use blocks) in message history
instead of filtering to only tool_use blocks. This preserves chain-of-thought
reasoning context for subsequent turns in multi-step agentic loops.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add MCP-spec compliant annotations to echo and add tools indicating
they are read-only, non-destructive, idempotent, and closed-world.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@olaservo olaservo marked this pull request as draft January 25, 2026 02:03
@cliffhall cliffhall added enhancement New feature or request server-everything Reference implementation for the Everything MCP server - src/everything labels Apr 20, 2026
@cliffhall
Copy link
Copy Markdown
Member

@claude review

@claude
Copy link
Copy Markdown
Contributor

claude Bot commented Apr 21, 2026

Claude finished @cliffhall's task in 3m 49s —— View job


PR Review: feat(everything): add trigger-agentic-sampling tool

  • Gather context and read changed files
  • Review documentation changes
  • Review tool registration
  • Review main implementation file
  • Post findings

Overall this is a well-structured implementation of the SEP-1577 agentic sampling loop. The build passes cleanly and the core logic is sound. A few issues to address before merging:

Issues

1. Inconsistent indentation in the handler body (style)

[trigger-agentic-sampling.ts:167-317] The server.registerTool(...) callback body starts at 4-space indent (lines 168–312) but then the return statement on line 314 jumps to 6 spaces before the closing }); on line 317. Compare with trigger-sampling-request.ts where the handler body is consistently at 6 spaces (2 for the function body + 2 for the if block + 2 for the callback). The entire handler body from line 168 to 316 should be at 6-space indent.

Fix this →

2. Variable shadowing (code smell)

[trigger-agentic-sampling.ts:173-174] The outer name constant (line 134, value "trigger-agentic-sampling") is shadowed by the callback parameters in the filter/map chain:

const tools: Tool[] = availableTools
  .filter((name) => name in AVAILABLE_TOOL_DEFINITIONS)  // shadows outer `name`
  .map((name) => AVAILABLE_TOOL_DEFINITIONS[name]);      // shadows outer `name`

Rename to toolName to avoid the shadowing and improve clarity. Fix this →

3. README.md not updated

The checklist marks "I have updated the server's README accordingly" but src/everything/README.md has no mention of trigger-agentic-sampling. Only the docs/features.md and docs/structure.md files were updated. The README should at minimum reference the new tool. Fix this →

4. Decorative comment banners don't match repo style (minor)

The file uses heavy // ====...==== section dividers that don't appear in any other tool file in the repo. These add noise without benefit — the section names (INPUT SCHEMA, TOOL DEFINITIONS, etc.) are self-evident from the code. Suggest removing them to match the lean style of the rest of the codebase.

Looks Good

  • Conditional registration: Correctly gates on "tools" in samplingCapability, matching the pattern in trigger-sampling-request.ts and trigger-elicitation-request.ts. No console.log on skip (previous review feedback addressed).
  • console.error for debug output: Correct — using stderr avoids corrupting STDIO JSON-RPC output.
  • toolChoice: none on the final iteration: Smart safety valve to force a text response when the iteration limit is about to be hit.
  • Batched tool results: Multiple tool_use blocks in a single response are all executed and returned as a single role: "user" message with an array of tool_result items — correct per MCP spec.
  • Post-loop guard: The if (iteration >= maxIterations && !finalResponse) check correctly handles the edge case where the LLM returns stopReason: "toolUse" even when toolChoice: none was sent.
  • SDK types: CreateMessageResultWithToolsSchema, ToolUseContent, ToolResultContent, toolChoice are all present in the installed SDK v1.25.2 — build passes cleanly.
  • Type cast: toolUse.input as Record<string, unknown> on line 266 is expected; ToolUseContent.input is typed as object in the SDK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request server-everything Reference implementation for the Everything MCP server - src/everything

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants