Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit 0c89680

Browse files
committed
refactor: Add HttpServletSseServerTransport for generic Servlet integration
Introduces a new Server-Sent Events (SSE) Server transport implementation based on the Servlet API that enables MCP integration with any Java HTTP server application supporting Servlets. This implementation complements the existing WebFlux and WebMvc transports by providing a generic Servlet-based option that works with any Java web server, not just Spring applications. Key changes: - Add HttpServletSseServerTransport with async Servlet 6.0 support - Add integration tests with embedded Tomcat - Add servlet dependencies with externalized versions: - jakarta.servlet-api 6.1.0 (provided) - tomcat-embed-core 11.0.2 (test) - tomcat-embed-websocket 11.0.2 (test) Resolves #61
1 parent 42b5931 commit 0c89680

File tree

11 files changed

+273
-206
lines changed

11 files changed

+273
-206
lines changed

mcp-docs/src/main/antora/modules/ROOT/images/class-diagrams.puml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ class StdioServerTransport implements ServerMcpTransport {
153153
}
154154

155155

156+
class HttpServletSseServerTransport implements ServerMcpTransport {
157+
}
158+
159+
156160
class HttpClientSseClientTransport implements ClientMcpTransport {
157161
}
158162

mcp-docs/src/main/antora/modules/ROOT/images/spring-ai-mcp-uml-classdiagram.svg

Lines changed: 1 addition & 1 deletion
Loading

mcp-docs/src/main/antora/modules/ROOT/pages/mcp.adoc

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Java SDK implementation of the link:https://modelcontextprotocol.io/introduction
1515
* Multiple transport implementations:
1616
** Core transports:
1717
*** Stdio-based transport for process-based communication
18-
*** Java HttpClient-based SSE transport for HTTP streaming
18+
*** Java HttpClient-based SSE client transport for HTTP SSE Client-side streaming
19+
*** Servlet-based SSE server transport for HTTP SSE Server streaming
1920
** Spring-based transports:
2021
*** WebFlux SSE transport for reactive HTTP streaming
2122
*** WebMVC SSE transport for servlet-based HTTP streaming
@@ -515,6 +516,41 @@ Implements the MCP HTTP with SSE transport specification, providing:
515516
* Message routing and session management
516517
* Graceful shutdown capabilities
517518
519+
SSE (Servlet)::
520+
+
521+
Creates a Servlet-based SSE server transport.
522+
Included in the core `mcp` module.
523+
The `HttpServletSseServerTransport` can be used with any Servlet container.
524+
To using it with a Spring Web application, you can register it as a Servlet bean:
525+
+
526+
[source,java]
527+
----
528+
@Configuration
529+
@EnableWebMvc
530+
public class McpServerConfig implements WebMvcConfigurer {
531+
532+
@Bean
533+
public HttpServletSseServerTransport servletSseServerTransport() {
534+
return new HttpServletSseServerTransport(new ObjectMapper(), "/mcp/message");
535+
}
536+
537+
@Bean
538+
public ServletRegistrationBean customServletBean(HttpServletSseServerTransport servlet) {
539+
return new ServletRegistrationBean(servlet);
540+
}
541+
}
542+
----
543+
+
544+
Implements the MCP HTTP with SSE transport specification using the traditional Servlet API, providing:
545+
+
546+
* Asynchronous message handling using Servlet 6.0 async support
547+
* Session management for multiple client connections
548+
* Two types of endpoints:
549+
** SSE endpoint (/sse) for server-to-client events
550+
** Message endpoint (configurable) for client-to-server requests
551+
* Error handling and response formatting
552+
* Graceful shutdown support
553+
518554
======
519555

520556
=== Server Capabilities

mcp-docs/src/main/antora/modules/ROOT/pages/overview.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ Core implementation of the Model Context Protocol specification, providing:
2020

2121
* *Core Transports*
2222
** Stdio-based (`StdioClientTransport`, `StdioServerTransport`) for process-based communication
23-
** Java HttpClient-based SSE client (`HttpClientSseClientTransport`) for HTTP streaming
23+
** Java HttpClient-based SSE client (`HttpClientSseClientTransport`) for HTTP SSE Client-side streaming
24+
** Servlet-based SSE server (`HttpServletSseServerTransport`) for HTTP SSE Server streaming using traditional Servlet API
2425

2526
* *Optional SSE Transports*
2627
** link:https://github.com/spring-projects-experimental/spring-ai-mcp/tree/main/mcp-transport/mcp-webflux-sse-transport[WebFlux SSE Transport] - Reactive HTTP streaming with Spring WebFlux (Client & Server)
@@ -114,6 +115,7 @@ Reffer to the xref:dependency-management.adoc[Dependency Management] page for mo
114115
* link:https://github.com/habuma/spring-ai-examples/tree/main/spring-ai-mcp[Theme Park API] - MCP server/client with Theme Park API tools
115116
* link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/mcp-webflux-server[WebFlux SSE] - WebFlux server/client implementation
116117
* link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/mcp-webmvc-server[WebMvc SSE] - WebMvc server with HttpClient implementation
118+
* link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/mcp-servlet-server[Servlet SSE] - SSE Servlet server with HttpClient implementation
117119

118120
== Documentation
119121

mcp-test/pom.xml

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,29 +72,14 @@
7272
<dependency>
7373
<groupId>org.awaitility</groupId>
7474
<artifactId>awaitility</artifactId>
75-
<version>4.2.0</version>
75+
<version>${awaitility.version}</version>
7676
</dependency>
7777

7878
<dependency>
7979
<groupId>ch.qos.logback</groupId>
8080
<artifactId>logback-classic</artifactId>
8181
<version>${logback.version}</version>
8282
</dependency>
83-
84-
<!-- <dependency>
85-
<groupId>jakarta.servlet</groupId>
86-
<artifactId>jakarta.servlet-api</artifactId>
87-
<version>5.0.0</version>
88-
<scope>provided</scope>
89-
</dependency>
90-
91-
<dependency>
92-
<groupId>org.apache.tomcat.embed</groupId>
93-
<artifactId>tomcat-embed-core</artifactId>
94-
<version>10.1.18</version>
95-
<scope>test</scope>
96-
</dependency> -->
97-
9883
</dependencies>
9984

10085

mcp-transport/mcp-webflux-sse-transport/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
<dependency>
9898
<groupId>org.awaitility</groupId>
9999
<artifactId>awaitility</artifactId>
100-
<version>4.2.0</version>
100+
<version>${awaitility.version}</version>
101101
<scope>test</scope>
102102
</dependency>
103103

mcp-transport/mcp-webmvc-sse-transport/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
<dependency>
8888
<groupId>org.awaitility</groupId>
8989
<artifactId>awaitility</artifactId>
90-
<version>4.2.0</version>
90+
<version>${awaitility.version}</version>
9191
<scope>test</scope>
9292
</dependency>
9393

@@ -111,14 +111,14 @@
111111
<dependency>
112112
<groupId>jakarta.servlet</groupId>
113113
<artifactId>jakarta.servlet-api</artifactId>
114-
<version>5.0.0</version>
114+
<version>${jakarta.servlet.version}</version>
115115
<scope>provided</scope>
116116
</dependency>
117117

118118
<dependency>
119119
<groupId>org.apache.tomcat.embed</groupId>
120120
<artifactId>tomcat-embed-core</artifactId>
121-
<version>11.0.2</version>
121+
<version>${tomcat.version}</version>
122122
<scope>test</scope>
123123
</dependency>
124124

mcp/pom.xml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
<dependency>
104104
<groupId>org.awaitility</groupId>
105105
<artifactId>awaitility</artifactId>
106-
<version>4.2.0</version>
106+
<version>${awaitility.version}</version>
107107
<scope>test</scope>
108108
</dependency>
109109

@@ -114,7 +114,29 @@
114114
<scope>test</scope>
115115
</dependency>
116116

117+
<!-- Used by the HttpServletSseServerTransport -->
118+
<dependency>
119+
<groupId>jakarta.servlet</groupId>
120+
<artifactId>jakarta.servlet-api</artifactId>
121+
<version>${jakarta.servlet.version}</version>
122+
<scope>provided</scope>
123+
</dependency>
124+
125+
<!-- Tomcat dependencies for testing -->
126+
<dependency>
127+
<groupId>org.apache.tomcat.embed</groupId>
128+
<artifactId>tomcat-embed-core</artifactId>
129+
<version>${tomcat.version}</version>
130+
<scope>test</scope>
131+
</dependency>
132+
<dependency>
133+
<groupId>org.apache.tomcat.embed</groupId>
134+
<artifactId>tomcat-embed-websocket</artifactId>
135+
<version>${tomcat.version}</version>
136+
<scope>test</scope>
137+
</dependency>
138+
117139
</dependencies>
118140

119141

120-
</project>
142+
</project>

0 commit comments

Comments
 (0)