Class AbstractMcpRestServlet
- All Implemented Interfaces:
jakarta.servlet.Servlet,jakarta.servlet.ServletConfig,Serializable,BasicUniversalConfig,DefaultConfig,DefaultHtmlConfig,ReadinessStateAware
- Direct Known Subclasses:
McpRestServlet,McpRestServlet
BasicRestServlet subclass that exposes a single MCP JSON-RPC endpoint at POST /.
Subclasses provide their McpServerConfig by overriding createMcpConfig() (called once at
init time), or by registering an McpServerConfig bean in their RestContext
bean store.
Example:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.juneau.rest.server.servlet.RestServlet
RestServlet.Builder<R extends RestServlet,SELF extends RestServlet.Builder<R, SELF>>, RestServlet.DefaultBuilder<R extends RestServlet> -
Field Summary
Fields inherited from class jakarta.servlet.http.HttpServlet
LEGACY_DO_HEAD -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidCloses this servlet's open subscription streams as soon as the embedded server begins shutting down.protected abstract McpServerConfigFactory method for theMcpServerConfigbacking this servlet.Returns theMcpServerConfigbacking this servlet.protected McpSubscriptionBrokerOptional subscription broker backing this servlet'ssubscriptions/listensupport.handleMcp(JsonRpcRequest req, RestRequest restReq) MCP JSON-RPC endpoint.protected abstract McpRevisionrevision()The MCP protocol revision this servlet speaks.Methods inherited from class org.apache.juneau.rest.server.servlet.RestServlet
builder, createAuthenticator, destroy, doLog, getContext, getPath, getPaths, getRequest, getResponse, getRestBuilder, init, log, log, service, setContextMethods inherited from class jakarta.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPatch, doPost, doPut, doTrace, getLastModified, isSensitiveHeader, serviceMethods inherited from class jakarta.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
-
Constructor Details
-
AbstractMcpRestServlet
public AbstractMcpRestServlet()
-
-
Method Details
-
getMcpConfig
Returns theMcpServerConfigbacking this servlet.The default implementation lazily caches the value of
createMcpConfig(); subclasses needing per-request reconfiguration may override.This is invoked on every request, so the cache is published via a lock-free
AtomicReference.compareAndSet(V, V)rather than asynchronizedblock, avoiding monitor contention on the hot path. Under a first-access race,createMcpConfig()may be invoked by more than one thread, but only one resulting instance is ever published toconfigand returned to any caller; the others are discarded. This is safe as long ascreateMcpConfig()is side-effect-free and idempotent (i.e. simply constructs and returns a new config), which is the documented contract of that method.- Returns:
- The config. Never
null.
-
createMcpConfig
Factory method for theMcpServerConfigbacking this servlet.Subclasses must override this method to declare their tools, prompts, and resources.
- Returns:
- A non-
nullserver config.
-
revision
The MCP protocol revision this servlet speaks.Supplied by the revision-specific subclass a consumer extends (for example
org.apache.juneau.rest.server.mcp.v20250618.McpRestServlet); consumers do not normally implement this themselves.- Returns:
- The bound revision. Never
null.
-
getSubscriptionBroker
Optional subscription broker backing this servlet'ssubscriptions/listensupport.Returning
null (the default) means this servlet does not support subscriptions: noMcpSubscriptionBrokerorMcpSubscriptionsbean is added to the request-scopedBeanStorethathandleMcp(org.apache.juneau.bean.jsonrpc.JsonRpcRequest, org.apache.juneau.rest.server.RestRequest)builds, so any handler or dispatch branch that looks one up viactx.getBean(McpSubscriptionBroker.class)seesOptional.empty().A revision binding that implements
subscriptions/listen(for exampleorg.apache.juneau.rest.server.mcp.v20260728.McpRestServlet) overrides this to return a memoized broker instance so every request sees the same live subscription registry.- Returns:
- The subscription broker, or
null if this servlet does not support subscriptions.
-
acceptReadinessState
Closes this servlet's open subscription streams as soon as the embedded server begins shutting down.A
subscriptions/listenstream never finishes on its own, so without this the server's graceful drain would wait out its entire stop timeout (30s by default) for every connected subscriber before it could stop. Closing the subscriptions makes each stream send its terminal frame and complete, letting the drain finish immediately.- Specified by:
acceptReadinessStatein interfaceReadinessStateAware- Parameters:
state- The per-service readiness state published by the embedded-server lifecycle component.
-
handleMcp
MCP JSON-RPC endpoint.- Parameters:
req- The parsed JSON-RPC request envelope.restReq- The HTTP request (used to surface the bean store to handlers).- Returns:
- The JSON-RPC response, or
nullfor notifications (which the framework renders as an empty body).
-