Skip to content

fix: enforce per-tool auth checks in sampling tool wrapper#3494

Merged
jlowin merged 2 commits intomainfrom
fix/sampling-tool-auth-bypass
Mar 14, 2026
Merged

fix: enforce per-tool auth checks in sampling tool wrapper#3494
jlowin merged 2 commits intomainfrom
fix/sampling-tool-auth-bypass

Conversation

@jlowin
Copy link
Copy Markdown
Member

@jlowin jlowin commented Mar 14, 2026

When SamplingTool.from_callable_tool() wraps a FunctionTool or TransformedTool for use in sampling contexts (ctx.sample / ctx.sample_step), the generated wrapper calls tool.run() directly without running the tool's auth checks. The server's HTTP dispatcher applies run_auth_checks before every tool call, but the sampling wrapper bypassed this entirely -- creating a privilege escalation path where an LLM could invoke auth-protected tools through sampling without proper authorization.

The fix adds the same auth enforcement logic inside the from_callable_tool wrapper that the server dispatcher uses: check tool.auth, resolve the current transport and access token, and run run_auth_checks before delegating to tool.run(). STDIO transport skips auth checks as it does everywhere else. A tool without auth set is unaffected.

# Before: auth-protected tool could be invoked via sampling without checks
protected = FunctionTool.from_function(secret_fn, auth=require_scopes("admin"))
sampling_tool = SamplingTool.from_callable_tool(protected)
await sampling_tool.run({})  # succeeded without any token

# After: raises AuthorizationError when token lacks required scopes
await sampling_tool.run({})  # AuthorizationError: insufficient permissions

Co-authored-by: Claude [email protected]

@marvin-context-protocol marvin-context-protocol Bot added bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. high-priority auth Related to authentication (Bearer, JWT, OAuth, WorkOS) for client or server. server Related to FastMCP server implementation or server-side functionality. labels Mar 14, 2026
@jlowin jlowin merged commit ca8069c into main Mar 14, 2026
7 checks passed
@jlowin jlowin deleted the fix/sampling-tool-auth-bypass branch March 14, 2026 20:14
@jlowin jlowin added the security Security fixes: input validation, SSRF/LFI prevention, auth hardening, injection defenses. label Mar 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auth Related to authentication (Bearer, JWT, OAuth, WorkOS) for client or server. bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. high-priority security Security fixes: input validation, SSRF/LFI prevention, auth hardening, injection defenses. server Related to FastMCP server implementation or server-side functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant