spring-projects / spring-projects/spring-ai
Spring AI MCP Server does not properly work when using spring.main.lazy-initialization=true
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 9.5k
- Forks
- 2.9k
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 5
Description
Bug description
I'm using spring-ai-starter-mcp-server-webmvc in my project to expose tools. I recognized that it gives me a 500 error when I set the property spring.main.lazy-initialization=false and I try to access the /sse endpoint.
Error
Caused by: jakarta.servlet.ServletException: java.lang.NullPointerException: Cannot invoke "io.modelcontextprotocol.spec.McpServerSession$Factory.create(io.modelcontextprotocol.spec.McpServerTransport)" because "this.sessionFactory" is null
at org.springframework.web.servlet.function.ErrorHandlingServerResponse.handleError(ErrorHandlingServerResponse.java:70)
at org.springframework.web.servlet.function.AbstractServerResponse.writeTo(AbstractServerResponse.java:105)
at org.springframework.web.servlet.function.support.HandlerFunctionAdapter.handle(HandlerFunctionAdapter.java:112)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1089)
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(FrameworkServlet.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)
at org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:108)
at org.springframework.security.web.FilterChainProxy.lambda$doFilterInternal$3(FilterChainProxy.java:231)
at org.springframework.security.web.ObservationFilterChainDecorator$FilterObservation$SimpleFilterObservation.lambda$wrap$1(ObservationFilterChainDecorator.java:479)
at org.springframework.security.web.ObservationFilterChainDecorator$AroundFilterObservation$SimpleAroundFilterObservation.lambda$wrap$1(ObservationFilterChainDecorator.java:340)
at org.springframework.security.web.ObservationFilterChainDecorator.lambda$wrapSecured$0(ObservationFilterChainDecorator.java:82)
at org.springframework.security.web.ObservationFilterChainDecorator$VirtualFilterChain.doFilter(ObservationFilterChainDecorator.java:128)
at org.springframework.security.web.access.intercept.AuthorizationFilter.doFilter(AuthorizationFilter.java:101)
at org.springframework.security.web.ObservationFilterChainDecorator$ObservationFilter.wrapFilter(ObservationFilterChainDecorator.java:240)
at org.springframework.security.web.ObservationFilterChainDecorator$ObservationFilter.doFilter(ObservationFilterChainDecorator.java:227)
at org.springframework.security.web.ObservationFilterChainDecorator$VirtualFilterChain.doFilter(ObservationFilterChainDecorator.java:137)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:126)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:120)
at org.springframework.security.web.ObservationFilterChainDecorator$ObservationFilter.wrapFilter(ObservationFilterChainDecorator.java:240)
at org.springframework.security.web.ObservationFilterChainDecorator$ObservationFilter.doFilter(ObservationFilterChainDecorator.java:227)
at org.springframework.security.web.ObservationFilterChainDecorator$VirtualFilterChain.doFilter(ObservationFilterChainDecorator.java:137)
at com.cumulocity.microservice.security.filter.PostAuthenticateServletFilter$1.run(PostAuthenticateServletFilter.java:44)
... 94 common frames omitted
Caused by: java.lang.NullPointerException: Cannot invoke "io.modelcontextprotocol.spec.McpServerSession$Factory.create(io.modelcontextprotocol.spec.McpServerTransport)" because "this.sessionFactory" is null
at io.modelcontextprotocol.server.transport.WebMvcSseServerTransportProvider.lambda$handleSseConnection$6(WebMvcSseServerTransportProvider.java:265)
at org.springframework.web.servlet.function.SseServerResponse.writeToInternal(SseServerResponse.java:95)
at org.springframework.web.servlet.function.AbstractServerResponse.writeTo(AbstractServerResponse.java:101)
... 123 common frames omitted
Steps to reproduce
- Clone the example repo: https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/weather/starter-webmvc-server
- Add
spring.main.lazy-initialization=trueto application.properties - Start the service
- Try to access
/sseendpoint
Expected behavior
The MCP server should be started and be accessible even when lazy Loading it set to true.
Minimal Complete Reproducible example
see above
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the starter-webmvc-server example linked in the reproduction steps and its application.properties, then inspect the WebMvcSseServerTransportProvider stack-trace entry. Reproduce the failure with lazy initialization enabled and verify that accessing /sse no longer produces the sessionFactory NullPointerException.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring-boot
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100