Add `transportContextProvider` to `McpClient.AsyncSpec`
@Kehrlann đang làm issue này rồi.
Từ ngày 7/8/2026.
Đánh giá
Issue này chưa được đánh giá.
Mô tả
Expected Behavior
AsyncSpec should accept a context provider the same way SyncSpec does, so a context can be attached to every client operation in one place:
McpClient.async(transport)
.transportContextProvider(() -> McpTransportContext.create(Map.of("tenant", currentTenant())))
.build();
The setter would mirror the sync one:
public AsyncSpec transportContextProvider(Supplier<McpTransportContext> contextProvider) {
this.contextProvider = contextProvider;
return this;
}
and McpAsyncClient would apply the same contextWrite that the sync client already applies. Defaulting to () -> McpTransportContext.EMPTY keeps existing behavior unchanged for anyone who doesn't set it.
One caveat worth documenting either way: the supplier is evaluated at subscribe time, so for an async client, it runs on whichever thread subscribes, which may not be the thread the application thinks of as the caller.
Current Behavior
transportContextProvider only exists on SyncSpec. On main at fd00498:
// McpClient
class SyncSpec { ... } // 163
private Supplier<McpTransportContext> contextProvider = () -> McpTransportContext.EMPTY; // 197
public SyncSpec transportContextProvider(Supplier<McpTransportContext> contextProvider) // 503
class AsyncSpec { ... } // 584
AsyncSpec has no equivalent field and no equivalent method. The javadoc is upfront about it, at line 497:
There is no direct equivalent in
AsyncSpec. To achieve the same result, appendcontextWrite(McpTransportContext.KEY, context)to anyMcpAsyncClientcall.
That instruction works. It just puts a cross-cutting concern at every call site.
Context
For a library that needs something attached to all requests, auth or tenancy or tracing, the documented workaround isn't usable, because the library doesn't own the call sites. The application does, and one missed call is a request that goes out without the context.
Nothing errors when that happens. The request just goes out without whatever the context was carrying, and you find out from the server's response, which typically won't say "your context was empty".
The change looks small. What SyncSpec does is mechanical:
// McpSyncClient:452
private <T> Mono<T> withProvidedContext(Mono<T> action) {
return action.contextWrite(ctx -> ctx.put(McpTransportContext.KEY, this.contextProvider.get()));
}
Every public operation on McpSyncClient routes through it, 23 call sites, from initialize() at line 190 through completeCompletion(...) at 442. McpAsyncClient would need the same contextWrite in the same places.
Alternatives considered:
- Follow the javadoc and
contextWriteat every call site. Works for an application that owns all its call sites. Doesn't work for a library and doesn't survive the addition of a new call site later. - Wrap
McpAsyncClientin a decorator that appliescontextWriteto each method. Possible, but it has to be kept in sync with the interface by hand, and it doesn't help anyone who obtains the raw client from a framework. - Use the sync client instead. This is what we do today. That's a real functional restriction, not just an inconvenience.
Is the asymmetry deliberate, or can it be reconsidered?
- Ngôn ngữ chính
- Java
- Star
- 3.7k
- Fork
- 1.1k
- Merge trung bình
- 1 ngày 15 giờ
- Pull request đã merge (30 ngày)
- 9
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của modelcontextprotocol/java-sdk
-
area/transport bug P2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
modelcontextprotocol/java-sdk#1136 ·
-
area/client bug P2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
modelcontextprotocol/java-sdk#1124 · 1 bình luận ·
-
ServerCapabilities.logging is added unconditionally, overriding the caller's explicit capabilities Đang mởbug P2 ready for work
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
modelcontextprotocol/java-sdk#1086 · 1 bình luận ·
-
enhancement good first issue P3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
modelcontextprotocol/java-sdk#1067 ·
-
bug P2 ready for work
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
modelcontextprotocol/java-sdk#898 · 1 bình luận ·
Tất cả issue của modelcontextprotocol/java-sdk
Issue tương tự
-
Bug Java Platform: Java
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
getsentry/sentry-java#6138 · 1 bình luận ·
-
bug needs triage p2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
GoogleCloudPlatform/DataflowTemplates#4273 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
bug needs triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100