Skip to content

Support setting Spring authentication object in McpServerSession #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion mcp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${springframework.version}</version>
</dependency>


<dependency>
<groupId>io.projectreactor.netty</groupId>
Expand Down Expand Up @@ -205,4 +211,4 @@
</dependencies>


</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public McpAsyncServerExchange(McpServerSession session, McpSchema.ClientCapabili
this.clientInfo = clientInfo;
}

public McpServerSession getSession() {
return session;
}

/**
* Get the client capabilities that define the supported features and functionality.
* @return The client capabilities
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.modelcontextprotocol.spec.McpSchema;
import io.modelcontextprotocol.spec.McpSchema.LoggingLevel;
import io.modelcontextprotocol.spec.McpSchema.LoggingMessageNotification;
import io.modelcontextprotocol.spec.McpServerSession;

/**
* Represents a synchronous exchange with a Model Context Protocol (MCP) client. The
Expand Down Expand Up @@ -90,4 +91,7 @@ public void loggingNotification(LoggingMessageNotification loggingMessageNotific
this.exchange.loggingNotification(loggingMessageNotification).block();
}

public McpServerSession getSession() {
return exchange.getSession();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import io.modelcontextprotocol.server.McpAsyncServerExchange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import reactor.core.publisher.Mono;
import reactor.core.publisher.MonoSink;
import reactor.core.publisher.Sinks;
Expand Down Expand Up @@ -48,6 +50,8 @@ public class McpServerSession implements McpSession {

private final AtomicReference<McpSchema.Implementation> clientInfo = new AtomicReference<>();

private Authentication authentication;

private static final int STATE_UNINITIALIZED = 0;

private static final int STATE_INITIALIZING = 1;
Expand Down Expand Up @@ -79,6 +83,7 @@ public McpServerSession(String id, Duration requestTimeout, McpServerTransport t
this.initNotificationHandler = initNotificationHandler;
this.requestHandlers = requestHandlers;
this.notificationHandlers = notificationHandlers;
this.authentication = SecurityContextHolder.getContext().getAuthentication();
}

/**
Expand All @@ -89,6 +94,15 @@ public String getId() {
return this.id;
}

/**
* Retrieve authentication object set by Spring security filters as per your project
* security config
* @return Authentication
*/
public Authentication getAuthentication() {
return authentication;
}

/**
* Called upon successful initialization sequence between the client and the server
* with the client capabilities and information.
Expand Down