Skip to content

AIML-2969 Feature/internal/mcp transport context for http servlet sse server transport provider #6

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sproutMultiModuleBuild {
moduleToBuild = 'mcp-parent'
nodeLabel = 'ephemeral'
tribes = ['global']
notifySlackGroupsOnFailure = ['@kevin']
runECRLogin = 'true'
jdk = 17
deployRCBranches = true
}
16 changes: 8 additions & 8 deletions mcp-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.modelcontextprotocol.sdk</groupId>
<groupId>com.sproutsocial.io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp-parent</artifactId>
<version>0.12.0-SNAPSHOT</version>
<version>0.12.0-sprout</version>
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: why not SNAPSHOT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@david-huber actually i wasn't sure if we even can or should push SNAPSHOT into nexus. I am thinking this should just be 0.12.0 or 0.12.0-rc1 or something?

</parent>

<artifactId>mcp-bom</artifactId>
Expand All @@ -28,28 +28,28 @@
<dependencies>
<!-- Core MCP -->
<dependency>
<groupId>io.modelcontextprotocol.sdk</groupId>
<groupId>com.sproutsocial.io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp</artifactId>
<version>${project.version}</version>
<version>0.12.0-sprout</version>
</dependency>

<!-- MCP Test -->
<dependency>
<groupId>io.modelcontextprotocol.sdk</groupId>
<groupId>com.sproutsocial.io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp-test</artifactId>
<version>${project.version}</version>
</dependency>

<!-- MCP Transport - WebFlux SSE -->
<dependency>
<groupId>io.modelcontextprotocol.sdk</groupId>
<groupId>com.sproutsocial.io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp-spring-webflux</artifactId>
<version>${project.version}</version>
</dependency>

<!-- MCP Transport - WebMVC SSE -->
<dependency>
<groupId>io.modelcontextprotocol.sdk</groupId>
<groupId>com.sproutsocial.io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp-spring-webmvc</artifactId>
<version>${project.version}</version>
</dependency>
Expand Down Expand Up @@ -95,4 +95,4 @@
</plugins>
</build>

</project>
</project>
12 changes: 6 additions & 6 deletions mcp-spring/mcp-spring-webflux/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.modelcontextprotocol.sdk</groupId>
<groupId>com.sproutsocial.io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp-parent</artifactId>
<version>0.12.0-SNAPSHOT</version>
<version>0.12.0-sprout</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>mcp-spring-webflux</artifactId>
Expand All @@ -23,15 +23,15 @@

<dependencies>
<dependency>
<groupId>io.modelcontextprotocol.sdk</groupId>
<groupId>com.sproutsocial.io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp</artifactId>
<version>0.12.0-SNAPSHOT</version>
<version>0.12.0-sprout</version>
</dependency>

<dependency>
<groupId>io.modelcontextprotocol.sdk</groupId>
<groupId>com.sproutsocial.io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp-test</artifactId>
<version>0.12.0-SNAPSHOT</version>
<version>0.12.0-sprout</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class WebClientStreamableHttpAsyncClientTests extends AbstractMcpAsyncCli

// Uses the https://github.com/tzolov/mcp-everything-server-docker-image
@SuppressWarnings("resource")
GenericContainer<?> container = new GenericContainer<>("docker.io/tzolov/mcp-everything-server:v2")
GenericContainer<?> container = new GenericContainer<>(
"412335208158.dkr.ecr.us-east-1.amazonaws.com/docker-hub/tzolov/mcp-everything-server:v2")
.withCommand("node dist/index.js streamableHttp")
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String()))
.withExposedPorts(3001)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class WebClientStreamableHttpSyncClientTests extends AbstractMcpSyncClien

// Uses the https://github.com/tzolov/mcp-everything-server-docker-image
@SuppressWarnings("resource")
GenericContainer<?> container = new GenericContainer<>("docker.io/tzolov/mcp-everything-server:v2")
GenericContainer<?> container = new GenericContainer<>(
"412335208158.dkr.ecr.us-east-1.amazonaws.com/docker-hub/tzolov/mcp-everything-server:v2")
.withCommand("node dist/index.js streamableHttp")
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String()))
.withExposedPorts(3001)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class WebFluxSseMcpAsyncClientTests extends AbstractMcpAsyncClientTests {

// Uses the https://github.com/tzolov/mcp-everything-server-docker-image
@SuppressWarnings("resource")
GenericContainer<?> container = new GenericContainer<>("docker.io/tzolov/mcp-everything-server:v2")
GenericContainer<?> container = new GenericContainer<>(
"412335208158.dkr.ecr.us-east-1.amazonaws.com/docker-hub/tzolov/mcp-everything-server:v2")
.withCommand("node dist/index.js sse")
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String()))
.withExposedPorts(3001)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class WebFluxSseMcpSyncClientTests extends AbstractMcpSyncClientTests {

// Uses the https://github.com/tzolov/mcp-everything-server-docker-image
@SuppressWarnings("resource")
GenericContainer<?> container = new GenericContainer<>("docker.io/tzolov/mcp-everything-server:v2")
GenericContainer<?> container = new GenericContainer<>(
"412335208158.dkr.ecr.us-east-1.amazonaws.com/docker-hub/tzolov/mcp-everything-server:v2")
.withCommand("node dist/index.js sse")
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String()))
.withExposedPorts(3001)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class WebFluxSseClientTransportTests {
static String host = "http://localhost:3001";

@SuppressWarnings("resource")
GenericContainer<?> container = new GenericContainer<>("docker.io/tzolov/mcp-everything-server:v2")
GenericContainer<?> container = new GenericContainer<>(
"412335208158.dkr.ecr.us-east-1.amazonaws.com/docker-hub/tzolov/mcp-everything-server:v2")
.withCommand("node dist/index.js sse")
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String()))
.withExposedPorts(3001)
Expand Down
16 changes: 8 additions & 8 deletions mcp-spring/mcp-spring-webmvc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.modelcontextprotocol.sdk</groupId>
<groupId>com.sproutsocial.io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp-parent</artifactId>
<version>0.12.0-SNAPSHOT</version>
<version>0.12.0-sprout</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>mcp-spring-webmvc</artifactId>
Expand All @@ -23,9 +23,9 @@

<dependencies>
<dependency>
<groupId>io.modelcontextprotocol.sdk</groupId>
<groupId>com.sproutsocial.io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp</artifactId>
<version>0.12.0-SNAPSHOT</version>
<version>0.12.0-sprout</version>
</dependency>

<dependency>
Expand All @@ -35,16 +35,16 @@
</dependency>

<dependency>
<groupId>io.modelcontextprotocol.sdk</groupId>
<groupId>com.sproutsocial.io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp-test</artifactId>
<version>0.12.0-SNAPSHOT</version>
<version>0.12.0-sprout</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.modelcontextprotocol.sdk</groupId>
<groupId>com.sproutsocial.io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp-spring-webflux</artifactId>
<version>0.12.0-SNAPSHOT</version>
<version>0.12.0-sprout</version>
<scope>test</scope>
</dependency>

Expand Down
10 changes: 5 additions & 5 deletions mcp-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.modelcontextprotocol.sdk</groupId>
<groupId>com.sproutsocial.io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp-parent</artifactId>
<version>0.12.0-SNAPSHOT</version>
<version>0.12.0-sprout</version>
</parent>
<artifactId>mcp-test</artifactId>
<packaging>jar</packaging>
Expand All @@ -22,9 +22,9 @@

<dependencies>
<dependency>
<groupId>io.modelcontextprotocol.sdk</groupId>
<groupId>com.sproutsocial.io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp</artifactId>
<version>0.12.0-SNAPSHOT</version>
<version>0.12.0-sprout</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -101,4 +101,4 @@
</dependencies>


</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public abstract class AbstractMcpAsyncClientResiliencyTests {

// Uses the https://github.com/tzolov/mcp-everything-server-docker-image
@SuppressWarnings("resource")
static GenericContainer<?> container = new GenericContainer<>("docker.io/tzolov/mcp-everything-server:v2")
static GenericContainer<?> container = new GenericContainer<>(
"412335208158.dkr.ecr.us-east-1.amazonaws.com/docker-hub/tzolov/mcp-everything-server:v2")
.withCommand("node dist/index.js streamableHttp")
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String()))
.withNetwork(network)
Expand Down
6 changes: 3 additions & 3 deletions mcp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.modelcontextprotocol.sdk</groupId>
<groupId>com.sproutsocial.io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp-parent</artifactId>
<version>0.12.0-SNAPSHOT</version>
<version>0.12.0-sprout</version>
</parent>
<artifactId>mcp</artifactId>
<packaging>jar</packaging>
Expand Down Expand Up @@ -220,4 +220,4 @@
</dependencies>


</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.modelcontextprotocol.server.DefaultMcpTransportContext;
import io.modelcontextprotocol.server.McpTransportContext;
import io.modelcontextprotocol.server.McpTransportContextExtractor;
import io.modelcontextprotocol.spec.McpError;
import io.modelcontextprotocol.spec.McpSchema;
import io.modelcontextprotocol.spec.McpServerSession;
Expand Down Expand Up @@ -102,6 +105,8 @@ public class HttpServletSseServerTransportProvider extends HttpServlet implement
/** Map of active client sessions, keyed by session ID */
private final Map<String, McpServerSession> sessions = new ConcurrentHashMap<>();

private McpTransportContextExtractor<HttpServletRequest> contextExtractor;

/** Flag indicating if the transport is in the process of shutting down */
private final AtomicBoolean isClosing = new AtomicBoolean(false);

Expand Down Expand Up @@ -144,7 +149,7 @@ public HttpServletSseServerTransportProvider(ObjectMapper objectMapper, String m
@Deprecated
public HttpServletSseServerTransportProvider(ObjectMapper objectMapper, String baseUrl, String messageEndpoint,
String sseEndpoint) {
this(objectMapper, baseUrl, messageEndpoint, sseEndpoint, null);
this(objectMapper, baseUrl, messageEndpoint, sseEndpoint, null, null);
}

/**
Expand All @@ -163,11 +168,33 @@ public HttpServletSseServerTransportProvider(ObjectMapper objectMapper, String b
@Deprecated
public HttpServletSseServerTransportProvider(ObjectMapper objectMapper, String baseUrl, String messageEndpoint,
String sseEndpoint, Duration keepAliveInterval) {
this(objectMapper, baseUrl, messageEndpoint, sseEndpoint, keepAliveInterval, null);
}

/**
* Creates a new HttpServletSseServerTransportProvider instance with a custom SSE
* endpoint.
* @param objectMapper The JSON object mapper to use for message
* serialization/deserialization
* @param baseUrl The base URL for the server transport
* @param messageEndpoint The endpoint path where clients will send their messages
* @param sseEndpoint The endpoint path where clients will establish SSE connections
* @param keepAliveInterval The interval for keep-alive pings, or null to disable
* keep-alive functionality
* @param contextExtractor The extractor for transport context from the request.
* @deprecated Use the builder {@link #builder()} instead for better configuration
* options.
*/
@Deprecated
public HttpServletSseServerTransportProvider(ObjectMapper objectMapper, String baseUrl, String messageEndpoint,
String sseEndpoint, Duration keepAliveInterval,
McpTransportContextExtractor<HttpServletRequest> contextExtractor) {

this.objectMapper = objectMapper;
this.baseUrl = baseUrl;
this.messageEndpoint = messageEndpoint;
this.sseEndpoint = sseEndpoint;
this.contextExtractor = contextExtractor;

if (keepAliveInterval != null) {

Expand Down Expand Up @@ -339,10 +366,13 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
body.append(line);
}

final McpTransportContext transportContext = contextExtractor.extract(request,
new DefaultMcpTransportContext());
McpSchema.JSONRPCMessage message = McpSchema.deserializeJsonRpcMessage(objectMapper, body.toString());

// Process the message through the session's handle method
session.handle(message).block(); // Block for Servlet compatibility
// Block for Servlet compatibility
session.handle(message).contextWrite(ctx -> ctx.put(McpTransportContext.KEY, transportContext)).block();

response.setStatus(HttpServletResponse.SC_OK);
}
Expand Down Expand Up @@ -534,6 +564,8 @@ public static class Builder {

private String sseEndpoint = DEFAULT_SSE_ENDPOINT;

private McpTransportContextExtractor<HttpServletRequest> contextExtractor = (serverRequest, context) -> context;

private Duration keepAliveInterval;

/**
Expand Down Expand Up @@ -583,6 +615,19 @@ public Builder sseEndpoint(String sseEndpoint) {
return this;
}

/**
* Sets the context extractor for extracting transport context from the request.
* @param contextExtractor The context extractor to use. Must not be null.
* @return this builder instance
* @throws IllegalArgumentException if contextExtractor is null
*/
public HttpServletSseServerTransportProvider.Builder contextExtractor(
McpTransportContextExtractor<HttpServletRequest> contextExtractor) {
Assert.notNull(contextExtractor, "Context extractor must not be null");
this.contextExtractor = contextExtractor;
return this;
}

/**
* Sets the interval for keep-alive pings.
* <p>
Expand All @@ -609,7 +654,7 @@ public HttpServletSseServerTransportProvider build() {
throw new IllegalStateException("MessageEndpoint must be set");
}
return new HttpServletSseServerTransportProvider(objectMapper, baseUrl, messageEndpoint, sseEndpoint,
keepAliveInterval);
keepAliveInterval, contextExtractor);
}

}
Expand Down
Loading