spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

Is it supported to expose MCP tools by adding spring-ai-starter-mcp-server-webflux to a legacy WebMVC app? No endpoint GET /sse error

Open openai0229 opened this issue 1 year ago • 11 comments

Bug description When adding the spring-ai-starter-mcp-server-webflux starter to an existing Spring WebMVC application to expose MCP tools, accessing the /see or /sse endpoints results in an error. The actual exception is:

org.springframework.web.servlet.NoHandlerFoundException: No endpoint GET /sse.
    at org.springframework.web.servlet.DispatcherServlet.noHandlerFound(DispatcherServlet.java:1305)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564)
    at org.springframework.web.servlet.FrameworkServlet.service(HttpServlet.java:885)
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:195)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:140)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:164)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:140)

I am not sure if this is a bug or a misconfiguration on my side. Any help or guidance would be appreciated. Is this kind of integration supported? Or is there any official recommendation for integrating MCP into an existing WebMVC application?

Environment

  • spring-ai-starter-mcp-server-webflux version: 1.0.0
  • Spring Boot version: 3.4.5
  • Java version: 17

Steps to reproduce

  1. Add the spring-ai-starter-mcp-server-webflux dependency to an existing Spring WebMVC application.
  2. Start the project.
  3. Visit /see or /sse in a browser.
  4. The NoHandlerFoundException error above appears.

openai0229 avatar Jun 10 '25 14:06 openai0229

Hi @openai0229

I tried to write a normal web-mvc project https://github.com/sunyuhan1998/spring-ai-repro-demo/tree/main/mcp/webmvc-mcp-server which contains a Controller for testing, then I transformed it into an mcp server, In this demo I use the following dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>

But that didn't reproduce your problem, the test controller and /sse both work fine. Can you provide a minimal project that reproduces the issue?

sunyuhan1998 avatar Jun 11 '25 06:06 sunyuhan1998

你好

我尝试编写一个普通的 web-mvc 项目 https://github.com/sunyuhan1998/spring-ai-repro-demo/tree/main/mcp/webmvc-mcp-server 其中包含一个用于测试的内容,然后将其转换为 mcp 服务器,在这个演示中,我使用了以下依赖项:Controller

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>

但这并没有重现您的问题,测试控制器和两者都工作正常。您能否提供一个重现该问题的最小项目?/sse

I think what he meant was that after switching webMVC to webflux mode, MCP Server cannot detect the endpoint, even if the client has been configured properly. Because I have encountered the same problem before

fanxt0218 avatar Jun 11 '25 06:06 fanxt0218

Hi @openai0229

I tried to write a normal web-mvc project https://github.com/sunyuhan1998/spring-ai-repro-demo/tree/main/mcp/webmvc-mcp-server which contains a Controller for testing, then I transformed it into an mcp server, In this demo I use the following dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>

But that didn't reproduce your problem, the test controller and /sse both work fine. Can you provide a minimal project that reproduces the issue?

Thank you @sunyuhan1998 , I'll check out your code example first, then I'll give my minimal example

openai0229 avatar Jun 11 '25 07:06 openai0229

Thank you @sunyuhan1998 , I'll check out your code example first, then I'll give my minimal example

Ok, since others in this issue have mentioned experiencing the same problem, I'm interested in this issue and look forward to your minimal example and further explanation of what's going on!

sunyuhan1998 avatar Jun 11 '25 07:06 sunyuhan1998

Hi @sunyuhan1998

Image Image

I looked at your code, and then I found that yours is the mcp-server dependency of webmvc. Then I tried to use cline to link your mcp-server, and it was linked normally. Then I changed it to webflux and found that the link failed.

I noticed that you are using version 1.1.0. Is this version that fixes some issues? I'm not sure if it's not possible to use the mcp-server dependency of webflux. My demo code is almost ready. I will give you the code repository address later.

openai0229 avatar Jun 11 '25 08:06 openai0229

I noticed that you are using version 1.1.0. Is this version that fixes some issues? I'm not sure if it's not possible to use the mcp-server dependency of webflux. My demo code is almost ready. I will give you the code repository address later.

I'm not quite sure what adjustments were made to MCP Server in version 1.1.0, after all, it's still a development release.

In the MCP Server Boot Starter provided by Spring AI, there are three options depending on the protocol and request method you choose:

  1. stdio
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-mcp-server</artifactId>
</dependency>
  1. webmvc + sse
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>
  1. flux + sse
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-mcp-server-webflux</artifactId>
</dependency>

You can view more detailed instructions here: mcp-server-boot-starter-docs

In fact, the Server support for web-mvc and flux is implemented as part of the java-sdk for MCP: mcp-spring, and the Spring AI starter just automatically configures them.

Looking forward to your demo!

sunyuhan1998 avatar Jun 11 '25 09:06 sunyuhan1998

Looking forward to your demo!

Thank you @sunyuhan1998, This is My Demo https://github.com/openai0229/spring-ai-mcp-server-demo. I feel very strange, now my demo mcp server is not available at all, I did not write mcp-client myself, I used cline to link my mcp-server, I don't know if it's because I wrote the demo too hastily and missed something.

openai0229 avatar Jun 11 '25 09:06 openai0229

I reproduced your issue, and the current problem is that if both spring-ai-starter-mcp-server-webflux and spring-boot-starter-web are used of MCP Server will make /sse inaccessible, but I found a way to make it usable: add the following configuration on your MCP Server side:

spring:
  main:
    web-application-type: reactive

But I think this will bring some side effects, and I recommend you to prioritize the use of the spring-ai-starter-mcp-server-webmvc. I'm still analyzing the cause of the problem and if it's an issue with Spring AI itself I'll try to push a PR to fix it, thanks for the report!

sunyuhan1998 avatar Jun 11 '25 10:06 sunyuhan1998

Thank you @sunyuhan1998 , I now found a more strange problem. After I switched webflux back to webmvc, the mcpSyncServer bean in the automatic configuration class McpServerAutoConfiguration was not loaded normally.

Image I checked my spring startup log and found that there was no such log line.

Then I tried to getBean manually after spring started, and this line of log was output normally. After that, I tested again, and my mcp service could work normally

openai0229 avatar Jun 11 '25 11:06 openai0229

I reproduced your issue, and the current problem is that if both spring-ai-starter-mcp-server-webflux and spring-boot-starter-web are used of MCP Server will make /sse inaccessible, but I found a way to make it usable: add the following configuration on your MCP Server side:

spring:
  main:
    web-application-type: reactive

I checked the source code, and the issue is actually SpringBoot's default behavior: if you introduce both WebMvc and WebFlux in your project, SpringBoot will prioritize WebMvc, which is reflected in SpringBoot's code:

https://github.com/spring-projects/spring-boot/blob/0daf3f5897a9bad169ad7ebad6d79e4260a24822/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/WebApplicationType.java#L60-L71

This causes endpoints such as /sse registered via spring-ai-starter-mcp-server-webflux registered endpoints such as /sse are invalidated, I think this is a usage issue and not a problem with Spring AI itself, just that we need to improve the Spring AI documentation a bit more.

sunyuhan1998 avatar Jun 11 '25 11:06 sunyuhan1998

This causes endpoints such as /sse registered via spring-ai-starter-mcp-server-webflux registered endpoints such as /sse are invalidated, I think this is a usage issue and not a problem with Spring AI itself, just that we need to improve the Spring AI documentation a bit more.

Thank you for your answer!

openai0229 avatar Jun 11 '25 11:06 openai0229