-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add filtering for MCP tool list #1108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kehrlann
wants to merge
2
commits into
main
Choose a base branch
from
dgarnier/tool-filters
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
81 changes: 81 additions & 0 deletions
81
mcp-core/src/main/java/io/modelcontextprotocol/server/McpAsyncListFilter.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Copyright 2026-2026 the original author or authors. | ||
| */ | ||
|
|
||
| package io.modelcontextprotocol.server; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import io.modelcontextprotocol.common.McpTransportContext; | ||
| import io.modelcontextprotocol.spec.McpSchema.Tool; | ||
| import io.modelcontextprotocol.util.Assert; | ||
| import reactor.core.publisher.Flux; | ||
| import reactor.core.publisher.Mono; | ||
| import reactor.core.scheduler.Schedulers; | ||
|
|
||
| /** | ||
| * Decide per request whether a primitive is advertised in the corresponding listing, such | ||
| * as {@code tools/list}. | ||
| * <p> | ||
| * A primitive hidden by this filter is omitted from listings ONLY. It remains reachable | ||
| * through its own endpoint: a hidden tool called by name still executes. Permissions MUST | ||
| * be enforced in the primitive's handler. | ||
| * | ||
| * @author Daniel Garnier-Moiroux | ||
| * @see McpSyncListFilter | ||
| * @see McpTransportContextExtractor | ||
| */ | ||
| @FunctionalInterface | ||
| public interface McpAsyncListFilter<T> { | ||
|
|
||
| /** | ||
| * Whether the given primitive is visible to the caller of the current request. | ||
| * @param transportContext transport context containing, for example, HTTP headers or | ||
| * a resolved principal. Should never be {@code null}, but may | ||
| * {@link McpTransportContext#EMPTY} for transports that carry no per-request | ||
| * metadata, such as STDIO. | ||
| * @param primitive the primitive that is a candidate for inclusion in the listing, | ||
| * such as {@link Tool}. | ||
| * @return a publisher emitting {@code true} to include the primitive in the listing, | ||
| * {@code false} to omit it. Completing empty omits the primitive; erroring fails the | ||
| * listing request. | ||
| */ | ||
| Mono<Boolean> isVisible(McpTransportContext transportContext, T primitive); | ||
|
|
||
| /** | ||
| * Convert a potentially blocking, synchronous filter into an asynchronous one, | ||
| * offloading it to prevent accidental blocking of a non-blocking transport. | ||
| * @param filter the synchronous filter. MUST NOT be null. | ||
| * @param immediateExecution When true, do not offload work asynchronously. Do NOT set | ||
| * to true when the filter performs blocking I/O. | ||
| */ | ||
| static <T> McpAsyncListFilter<T> fromSync(McpSyncListFilter<T> filter, boolean immediateExecution) { | ||
| Assert.notNull(filter, "filter must not be null"); | ||
| return (transportContext, primitive) -> { | ||
| var visible = Mono.fromCallable(() -> filter.isVisible(transportContext, primitive)); | ||
| return immediateExecution ? visible : visible.subscribeOn(Schedulers.boundedElastic()); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Combine multiple filters in a single AND-filter. An empty or {@code null} list | ||
| * makes everything visible, keeping listing on a single code path when nothing is | ||
| * configured. | ||
| * @param filters the filters to combine. May be {@code null} or empty, but MUST NOT | ||
| * contain {@code null} elements. | ||
| */ | ||
| static <T> McpAsyncListFilter<T> and(List<McpAsyncListFilter<T>> filters) { | ||
| if (filters == null || filters.isEmpty()) { | ||
| return (transportContext, primitive) -> Mono.just(Boolean.TRUE); | ||
| } | ||
| Assert.noNullElements(filters, "filters must not contain null elements"); | ||
| if (filters.size() == 1) { | ||
| return filters.get(0); | ||
| } | ||
| List<McpAsyncListFilter<T>> snapshot = List.copyOf(filters); | ||
| return (transportContext, primitive) -> Flux.fromIterable(snapshot) | ||
| .concatMap(filter -> filter.isVisible(transportContext, primitive).defaultIfEmpty(Boolean.FALSE)) | ||
| .all(Boolean.TRUE::equals); | ||
| } | ||
|
|
||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With 1 filter, Mono.empty() stays empty; with 2+, empty is coerced to false: (e.g..defaultIfEmpty(Boolean.FALSE)). Not sure what downstream implications this might have. It will be cleaner to either remove the if (filters.size() == 1) optimization or use the same defaultIfEmpty strategy