Skip to content

Update example with current Spring WebFlux server API #220

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 1 commit into
base: main
Choose a base branch
from
Open
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
83 changes: 43 additions & 40 deletions mcp-spring/mcp-spring-webflux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,51 @@
```

```java
String MESSAGE_ENDPOINT = "/mcp/message";
import io.modelcontextprotocol.server.McpAsyncServer;
import io.modelcontextprotocol.server.McpServer;
import io.modelcontextprotocol.server.transport.WebFluxSseServerTransportProvider;
import io.modelcontextprotocol.spec.McpSchema;
import io.modelcontextprotocol.spec.McpServerTransportProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;

@Configuration
static class MyConfig {

public class MyConfig {
// SSE transport
@Bean
public WebFluxSseServerTransport sseServerTransport() {
return new WebFluxSseServerTransport(new ObjectMapper(), "/mcp/message");
}

// Router function for SSE transport used by Spring WebFlux to start an HTTP
// server.
@Bean
public RouterFunction<?> mcpRouterFunction(WebFluxSseServerTransport transport) {
return transport.getRouterFunction();
}

@Bean
public McpAsyncServer mcpServer(ServerMcpTransport transport, OpenLibrary openLibrary) {

// Configure server capabilities with resource support
var capabilities = McpSchema.ServerCapabilities.builder()
.resources(false, true) // No subscribe support, but list changes notifications
.tools(true) // Tool support with list changes notifications
.prompts(true) // Prompt support with list changes notifications
.logging() // Logging support
.build();

// Create the server with both tool and resource capabilities
var server = McpServer.using(transport)
.serverInfo("MCP Demo Server", "1.0.0")
.capabilities(capabilities)
.resources(systemInfoResourceRegistration())
.prompts(greetingPromptRegistration())
.tools(openLibraryToolRegistrations(openLibrary))
.async();

return server;
}

// ...

@Bean
public WebFluxSseServerTransportProvider sseServerTransportProvider() {
return WebFluxSseServerTransportProvider.builder()
.messageEndpoint("/mcp/message")
.sseEndpoint("/mcp")
.build();
}

// Router function for SSE transport used by Spring WebFlux to start an HTTP
// server.
@Bean
public RouterFunction<?> mcpRouterFunction(WebFluxSseServerTransportProvider transport) {
return transport.getRouterFunction();
}

@Bean
public McpAsyncServer mcpServer(McpServerTransportProvider transport, OpenLibrary openLibrary) {
// Configure server capabilities with resource support
var capabilities = McpSchema.ServerCapabilities.builder()
.resources(false, true) // No subscribe support, but list changes notifications
.tools(true) // Tool support with list changes notifications
.prompts(true) // Prompt support with list changes notifications
.logging() // Logging support
.build();

// Create the server with both tool and resource capabilities
return McpServer.async(transport)
.serverInfo("MCP Demo Server", "1.0.0")
.capabilities(capabilities)
.resources(systemInfoResourceRegistration())
.prompts(greetingPromptRegistration())
.tools(openLibraryToolRegistrations(openLibrary))
.build();
}
}
```