Package com.onec.mcp
Class McpPrincipalContext
java.lang.Object
com.onec.mcp.McpPrincipalContext
- All Implemented Interfaces:
io.modelcontextprotocol.server.McpTransportContextExtractor<jakarta.servlet.http.HttpServletRequest>
public class McpPrincipalContext
extends Object
implements io.modelcontextprotocol.server.McpTransportContextExtractor<jakarta.servlet.http.HttpServletRequest>
Bridges the authenticated Spring Security principal into the MCP tool-call context.
The MCP servlet transport runs extract(jakarta.servlet.http.HttpServletRequest) on the servlet request thread,
after the Spring Security filter chain has populated
SecurityContextHolder for that request. We capture the Authentication
there and stash it in the McpTransportContext, which the SDK propagates into
the (possibly reactive) tool-call processing. Tool handlers then read it back via
principal(McpSyncServerExchange) — never from the thread-local, which may not
survive the hop to a Reactor scheduler thread.
This makes every tool execute as the connecting user, so the existing
UiAccessService deny-by-default role checks apply unchanged.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionio.modelcontextprotocol.common.McpTransportContextextract(jakarta.servlet.http.HttpServletRequest request) static Principalprincipal(io.modelcontextprotocol.server.McpSyncServerExchange exchange) Reads the authenticated principal captured for the current tool call, ornullwhen the request was anonymous.
-
Field Details
-
PRINCIPAL_KEY
Key under which the capturedPrincipalis stored in the transport context.- See Also:
-
-
Constructor Details
-
McpPrincipalContext
public McpPrincipalContext()
-
-
Method Details
-
extract
public io.modelcontextprotocol.common.McpTransportContext extract(jakarta.servlet.http.HttpServletRequest request) - Specified by:
extractin interfaceio.modelcontextprotocol.server.McpTransportContextExtractor<jakarta.servlet.http.HttpServletRequest>
-
principal
Reads the authenticated principal captured for the current tool call, ornullwhen the request was anonymous. Anullprincipal is denied everything byUiAccessService(deny by default), which is the desired behavior.
-