Skip to content

Spring MCP Client security not working when controller exposes streaming HTTP endpoint #19

@robynico

Description

@robynico

When the MCP client is exposed through a Flux-based streaming HTTP endpoint, the thread-bound request context does not appear to be propagated correctly.

In this scenario, calls to RequestContextHolder.getRequestAttributes() within AuthenticationMcpTransportContextProvider.java consistently return null.

It seems that the thread architecture used in reactive endpoints (WebFlux) does not maintain the same request context as in traditional WebMVC controllers.

@GetMapping("/stream-chat")
public Flux<String> index(@RequestParam(required = false) String query) {
    if (StringUtils.hasText(query)) {
        String prompt = String.format("""
            What is the weather in %s right now?
            Compare to historical data over the past 5 years.
            Concisely tell me if it is within the usual range.
            Format the output in plain HTML, no CSS.
            """, query);

        return RequestContextReactorHelper.withRequestContext(
                chatClient.prompt(prompt)
                        .toolCallbacks(mcpToolCallbacks)
                        .stream()
                        .call()
                        .content()
        );
    }
    return Flux.just("no query");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions