-
Notifications
You must be signed in to change notification settings - Fork 701
test: Add additional MCP transport context integration tests #529
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
Merged
tzolov
merged 2 commits into
modelcontextprotocol:main
from
tzolov:mcp-transport-context-its
Sep 4, 2025
Merged
Changes from 1 commit
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
381 changes: 381 additions & 0 deletions
381
...t/java/io/modelcontextprotocol/common/AsyncServerMcpTransportContextIntegrationTests.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,381 @@ | ||||||
/* | ||||||
* Copyright 2024-2025 the original author or authors. | ||||||
*/ | ||||||
|
||||||
package io.modelcontextprotocol.common; | ||||||
|
||||||
import java.util.Map; | ||||||
import java.util.function.BiFunction; | ||||||
import java.util.function.Supplier; | ||||||
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper; | ||||||
import io.modelcontextprotocol.client.McpAsyncClient; | ||||||
import io.modelcontextprotocol.client.McpClient; | ||||||
import io.modelcontextprotocol.client.McpSyncClient; | ||||||
import io.modelcontextprotocol.client.transport.WebClientStreamableHttpTransport; | ||||||
import io.modelcontextprotocol.client.transport.WebFluxSseClientTransport; | ||||||
import io.modelcontextprotocol.server.McpAsyncServerExchange; | ||||||
import io.modelcontextprotocol.server.McpServer; | ||||||
import io.modelcontextprotocol.server.McpServerFeatures; | ||||||
import io.modelcontextprotocol.server.McpStatelessServerFeatures; | ||||||
import io.modelcontextprotocol.server.McpTransportContextExtractor; | ||||||
import io.modelcontextprotocol.server.TestUtil; | ||||||
import io.modelcontextprotocol.server.transport.WebFluxSseServerTransportProvider; | ||||||
import io.modelcontextprotocol.server.transport.WebFluxStatelessServerTransport; | ||||||
import io.modelcontextprotocol.server.transport.WebFluxStreamableServerTransportProvider; | ||||||
import io.modelcontextprotocol.spec.McpSchema; | ||||||
import org.junit.jupiter.api.AfterEach; | ||||||
import org.junit.jupiter.api.Test; | ||||||
import org.junit.jupiter.api.Timeout; | ||||||
import reactor.core.publisher.Mono; | ||||||
import reactor.netty.DisposableServer; | ||||||
import reactor.netty.http.server.HttpServer; | ||||||
import reactor.test.StepVerifier; | ||||||
|
||||||
import org.springframework.http.server.reactive.HttpHandler; | ||||||
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter; | ||||||
import org.springframework.web.reactive.function.client.ClientRequest; | ||||||
import org.springframework.web.reactive.function.client.ExchangeFilterFunction; | ||||||
import org.springframework.web.reactive.function.client.WebClient; | ||||||
import org.springframework.web.reactive.function.server.RouterFunction; | ||||||
import org.springframework.web.reactive.function.server.RouterFunctions; | ||||||
import org.springframework.web.reactive.function.server.ServerRequest; | ||||||
|
||||||
import static org.assertj.core.api.Assertions.assertThat; | ||||||
|
||||||
/** | ||||||
* Integration tests for {@link McpTransportContext} propagation between MCP clients and | ||||||
* async servers using Spring WebFlux infrastructure. | ||||||
* | ||||||
* <p> | ||||||
* This test class validates the end-to-end flow of transport context propagation in MCP | ||||||
* communication for asynchronous server implementations. It tests various combinations of | ||||||
* client types (sync/async) and server transport mechanisms (stateless, streamable, SSE) | ||||||
* to ensure proper context handling across different configurations. | ||||||
* | ||||||
* <h2>Context Propagation Flow</h2> | ||||||
* <ol> | ||||||
* <li>Client sets a value in its transport context (either via thread-local for sync or | ||||||
* Reactor context for async)</li> | ||||||
* <li>Client-side context provider extracts the value and adds it as an HTTP header to | ||||||
* the request</li> | ||||||
* <li>Server-side context extractor reads the header from the incoming request</li> | ||||||
* <li>Server handler receives the extracted context and returns the value as the tool | ||||||
* call result</li> | ||||||
* <li>Test verifies the round-trip context propagation was successful</li> | ||||||
* </ol> | ||||||
* | ||||||
* @author Daniel Garnier-Moiroux | ||||||
* @author Christian Tzolov | ||||||
* @see McpTransportContext | ||||||
* @see McpTransportContextExtractor | ||||||
* @see WebFluxStatelessServerTransport | ||||||
* @see WebFluxStreamableServerTransportProvider | ||||||
* @see WebFluxSseServerTransportProvider | ||||||
*/ | ||||||
@Timeout(15) | ||||||
public class AsyncServerMcpTransportContextIntegrationTests { | ||||||
|
||||||
private static final int PORT = TestUtil.findAvailablePort(); | ||||||
|
||||||
private static final ThreadLocal<String> SYNC_CLIENT_SIDE_HEADER_VALUE_HOLDER = new ThreadLocal<>(); | ||||||
|
||||||
private static final String HEADER_NAME = "x-test"; | ||||||
|
||||||
// Sync client context provider | ||||||
private final Supplier<McpTransportContext> syncClientContextProvider = () -> { | ||||||
var headerValue = SYNC_CLIENT_SIDE_HEADER_VALUE_HOLDER.get(); | ||||||
return headerValue != null ? McpTransportContext.create(Map.of("client-side-header-value", headerValue)) | ||||||
: McpTransportContext.EMPTY; | ||||||
}; | ||||||
|
||||||
// Async client context provider | ||||||
ExchangeFilterFunction asyncClientContextProvider = (request, next) -> Mono.deferContextual(ctx -> { | ||||||
var context = ctx.getOrDefault(McpTransportContext.KEY, McpTransportContext.EMPTY); | ||||||
|
var context = ctx.getOrDefault(McpTransportContext.KEY, McpTransportContext.EMPTY); | |
var transportContext = ctx.getOrDefault(McpTransportContext.KEY, McpTransportContext.EMPTY); |
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.
Same comment as in
AsyncServerMcpTransportContextIntegrationTests
: consider using only async clients here.