Class HttpServletStreamableServerTransportProvider

java.lang.Object
jakarta.servlet.GenericServlet
jakarta.servlet.http.HttpServlet
io.modelcontextprotocol.server.transport.HttpServletStreamableServerTransportProvider
All Implemented Interfaces:
McpServerTransportProviderBase, McpStreamableServerTransportProvider, jakarta.servlet.Servlet, jakarta.servlet.ServletConfig, Serializable

@WebServlet(asyncSupported=true) public class HttpServletStreamableServerTransportProvider extends jakarta.servlet.http.HttpServlet implements McpStreamableServerTransportProvider
Server-side implementation of the Model Context Protocol (MCP) streamable transport layer using HTTP with Server-Sent Events (SSE) through HttpServlet. This implementation provides a bridge between synchronous HttpServlet operations and reactive programming patterns to maintain compatibility with the reactive transport interface.

This is the HttpServlet equivalent of io.modelcontextprotocol.server.transport.WebMvcStreamableServerTransportProvider for the core MCP module, providing streamable HTTP transport functionality without Spring dependencies.

Author:
Zachary German, Christian Tzolov, Dariusz Jędrzejczyk
See Also:
  • Field Details

  • Method Details

    • protocolVersions

      public List<String> protocolVersions()
      Description copied from interface: McpServerTransportProviderBase
      Returns the protocol version supported by this transport provider.
      Specified by:
      protocolVersions in interface McpServerTransportProviderBase
      Returns:
      the protocol version as a string
    • setSessionFactory

      public void setSessionFactory(McpStreamableServerSession.Factory sessionFactory)
      Description copied from interface: McpStreamableServerTransportProvider
      Sets the session factory that will be used to create sessions for new clients. An implementation of the MCP server MUST call this method before any MCP interactions take place.
      Specified by:
      setSessionFactory in interface McpStreamableServerTransportProvider
      Parameters:
      sessionFactory - the session factory to be used for initiating client sessions
    • notifyClients

      public reactor.core.publisher.Mono<Void> notifyClients(String method, Object params)
      Broadcasts a notification to all connected clients through their SSE connections. If any errors occur during sending to a particular client, they are logged but don't prevent sending to other clients.
      Specified by:
      notifyClients in interface McpServerTransportProviderBase
      Specified by:
      notifyClients in interface McpStreamableServerTransportProvider
      Parameters:
      method - The method name for the notification
      params - The parameters for the notification
      Returns:
      A Mono that completes when the broadcast attempt is finished
      See Also:
    • closeGracefully

      public reactor.core.publisher.Mono<Void> closeGracefully()
      Initiates a graceful shutdown of the transport.
      Specified by:
      closeGracefully in interface McpServerTransportProviderBase
      Specified by:
      closeGracefully in interface McpStreamableServerTransportProvider
      Returns:
      A Mono that completes when all cleanup operations are finished
    • doGet

      protected void doGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException
      Handles GET requests to establish SSE connections and message replay.
      Overrides:
      doGet in class jakarta.servlet.http.HttpServlet
      Parameters:
      request - The HTTP servlet request
      response - The HTTP servlet response
      Throws:
      jakarta.servlet.ServletException - If a servlet-specific error occurs
      IOException - If an I/O error occurs
    • doPost

      protected void doPost(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException
      Handles POST requests for incoming JSON-RPC messages from clients.
      Overrides:
      doPost in class jakarta.servlet.http.HttpServlet
      Parameters:
      request - The HTTP servlet request containing the JSON-RPC message
      response - The HTTP servlet response
      Throws:
      jakarta.servlet.ServletException - If a servlet-specific error occurs
      IOException - If an I/O error occurs
    • doDelete

      protected void doDelete(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException
      Handles DELETE requests for session deletion.
      Overrides:
      doDelete in class jakarta.servlet.http.HttpServlet
      Parameters:
      request - The HTTP servlet request
      response - The HTTP servlet response
      Throws:
      jakarta.servlet.ServletException - If a servlet-specific error occurs
      IOException - If an I/O error occurs
    • responseError

      public void responseError(jakarta.servlet.http.HttpServletResponse response, int httpCode, McpError mcpError) throws IOException
      Throws:
      IOException
    • destroy

      public void destroy()
      Cleans up resources when the servlet is being destroyed.

      This method ensures a graceful shutdown by closing all client connections before calling the parent's destroy method.

      Specified by:
      destroy in interface jakarta.servlet.Servlet
      Overrides:
      destroy in class jakarta.servlet.GenericServlet
    • builder