Skip to content

Fix #1557: close MCP SSE connections before Tomcat graceful shutdown… - #1766

Merged
alexheifetz merged 1 commit into
mainfrom
graceful-shutdown-with-mcp
Jul 7, 2026
Merged

alexheifetz merged 1 commit into
mainfrom
graceful-shutdown-with-mcp

Conversation

@igordayen

@igordayen igordayen commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds McpSseShutdownConfiguration to embabel-agent-mcpserver to fix a 30-second shutdown stall when using the MCP
server starter.

Closes: #1557

WebMvcSseServerTransportProvider keeps SSE connections open after MCP calls complete — by design, as SSE is a
persistent connection. On shutdown, Tomcat counts these as active requests and waits up to 30 seconds before
force-closing them. This fix listens for ContextClosedEvent (which fires before Tomcat's graceful shutdown phase) and
calls closeGracefully() to close all active sessions within a 5-second timeout.

Root cause

WebMvcSseServerTransportProvider.closeGracefully() exists in the MCP Java SDK but is not wired into the Spring
lifecycle by either the MCP Java SDK or Spring AI's autoconfiguration. The following upstream issues have been open
since late 2025 with no fix shipped:

Versions affected: spring-ai-starter-mcp-server-webmvc 1.1.7 / MCP Java SDK 0.18.2.

Design notes

  • @Autowired(required = false) is used instead of @ConditionalOnBean — the conditional evaluates before Spring AI
    registers the transport bean and always resolves to false. Optional injection defers the check until after all beans
    are created.
  • closeGracefully() — safe to keep when upstream ships a fix, as a second call on an already-closed
    provider iterates an empty session map.
  • 5-second timeout guards against the pipeline stalling (MCP Java SDK Introduce AgentInvocation #635).

@sonarqubecloud

sonarqubecloud Bot commented Jul 7, 2026

Copy link
Copy Markdown

@igordayen

Copy link
Copy Markdown
Contributor Author

@fctg-stevendoolan confirmed the fix is working properly.

@alexheifetz
alexheifetz merged commit 6cb9722 into main Jul 7, 2026
16 of 17 checks passed
@alexheifetz
alexheifetz deleted the graceful-shutdown-with-mcp branch July 7, 2026 22:44
@igordayen

Copy link
Copy Markdown
Contributor Author

https://github.com/fctg-stevendoolan - please be aware of PR: #2071 , Mifration to Streamable HTTP.
Spring is removing a class used by McpSseShutdownConfiguration. Please consider switching from SSE to Streamable HTTP.
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Server does not shutdown gracefully with embabel-agent-starter-mcpserver

2 participants