Update the mcp demos to latest 0.6.0 version

This commit is contained in:
Christian Tzolov
2025-01-24 19:10:15 +01:00
parent 9f8cb92aa9
commit 0ddbb686ad
23 changed files with 225 additions and 66 deletions

View File

@@ -43,7 +43,7 @@
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-ai-mcp</artifactId>
<version>0.5.1</version>
<version>0.6.0</version>
</dependency>
</dependencies>

View File

@@ -50,7 +50,7 @@ public class Application {
.addEnvVar("BRAVE_API_KEY", System.getenv("BRAVE_API_KEY"))
.build();
var mcpClient = McpClient.using(new StdioClientTransport(stdioParams)).sync();
var mcpClient = McpClient.sync(new StdioClientTransport(stdioParams)).build();
var init = mcpClient.initialize();
System.out.println("MCP Initialized: " + init);
return mcpClient;

View File

@@ -43,7 +43,7 @@
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-ai-mcp</artifactId>
<version>0.5.1</version>
<version>0.6.0</version>
</dependency>
</dependencies>

View File

@@ -70,8 +70,8 @@ public class Application {
.args("-y", "@modelcontextprotocol/server-filesystem", getDbPath())
.build();
var mcpClient = McpClient.using(new StdioClientTransport(stdioParams))
.requestTimeout(Duration.ofSeconds(10)).sync();
var mcpClient = McpClient.sync(new StdioClientTransport(stdioParams))
.requestTimeout(Duration.ofSeconds(10)).build();
var init = mcpClient.initialize();

View File

@@ -18,7 +18,7 @@
<description>Sample application demonstrating MCP Servlet server usage</description>
<properties>
<spring-ai-mcp.version>0.5.1</spring-ai-mcp.version>
<spring-ai-mcp.version>0.6.0</spring-ai-mcp.version>
</properties>
<dependencyManagement>

View File

@@ -27,7 +27,7 @@ public class ClientStdio {
public static void main(String[] args) {
var stdioParams = ServerParameters.builder("java")
.args("-Dtransport.mode=stdio", "-jar",
.args("-Dtransport.mode=stdio", "-Dspring.main.web-application-type=none", "-jar",
"model-context-protocol/mcp-servlet-server/target/mcp-servlet-server-0.0.1-SNAPSHOT.jar")
.build();

View File

@@ -40,7 +40,7 @@ public class SampleClient {
public void run() {
var client = McpClient.using(this.transport).sync();
var client = McpClient.sync(this.transport).build();
client.initialize();

View File

@@ -8,11 +8,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.mcp.server.McpAsyncServer;
import org.springframework.ai.mcp.server.McpServer;
import org.springframework.ai.mcp.server.McpServer.PromptRegistration;
import org.springframework.ai.mcp.server.McpServer.ResourceRegistration;
import org.springframework.ai.mcp.server.McpServer.ToolRegistration;
import org.springframework.ai.mcp.server.McpServerFeatures.SyncPromptRegistration;
import org.springframework.ai.mcp.server.McpServerFeatures.SyncResourceRegistration;
import org.springframework.ai.mcp.server.McpServerFeatures.SyncToolRegistration;
import org.springframework.ai.mcp.server.McpSyncServer;
import org.springframework.ai.mcp.server.transport.HttpServletSseServerTransport;
import org.springframework.ai.mcp.server.transport.StdioServerTransport;
import org.springframework.ai.mcp.spec.McpSchema;
@@ -60,7 +60,7 @@ public class McpServerConfig implements WebMvcConfigurer {
}
@Bean
public McpAsyncServer mcpServer(ServerMcpTransport transport, OpenLibrary openLibrary) { // @formatter:off
public McpSyncServer mcpServer(ServerMcpTransport transport, OpenLibrary openLibrary) { // @formatter:off
// Configure server capabilities with resource support
var capabilities = McpSchema.ServerCapabilities.builder()
@@ -71,24 +71,24 @@ public class McpServerConfig implements WebMvcConfigurer {
.build();
// Create the server with both tool and resource capabilities
var server = McpServer.using(transport)
var server = McpServer.sync(transport)
.serverInfo("MCP Demo Servlet Server", "1.0.0")
.capabilities(capabilities)
.resources(systemInfoResourceRegistration())
.prompts(greetingPromptRegistration())
.tools(
ToolHelper.toToolRegistration(
ToolHelper.toSyncToolRegistration(
FunctionCallback.builder()
.function("toUpperCase", (Function<ToUpperCaseInput, String>) s -> s.input().toUpperCase())
.description("To upper case")
.inputType(ToUpperCaseInput.class)
.build()))
.tools(openLibraryToolRegistrations(openLibrary))
.async();
.build();
return server; // @formatter:on
} // @formatter:on
public static List<ToolRegistration> openLibraryToolRegistrations(OpenLibrary openLibrary) {
public static List<SyncToolRegistration> openLibraryToolRegistrations(OpenLibrary openLibrary) {
var books = FunctionCallback.builder()
.method("getBooks", String.class)
@@ -102,10 +102,10 @@ public class McpServerConfig implements WebMvcConfigurer {
.targetObject(openLibrary)
.build();
return ToolHelper.toToolRegistration(books, bookTitlesByAuthor);
return ToolHelper.toSyncToolRegistration(books, bookTitlesByAuthor);
}
private static ResourceRegistration systemInfoResourceRegistration() {
private static SyncResourceRegistration systemInfoResourceRegistration() {
// Create a resource registration for system information
var systemInfoResource = new McpSchema.Resource( // @formatter:off
@@ -115,7 +115,7 @@ public class McpServerConfig implements WebMvcConfigurer {
"application/json", null
);
var resourceRegistration = new ResourceRegistration(systemInfoResource, (request) -> {
var resourceRegistration = new SyncResourceRegistration(systemInfoResource, (request) -> {
try {
var systemInfo = Map.of(
"javaVersion", System.getProperty("java.version"),
@@ -138,12 +138,12 @@ public class McpServerConfig implements WebMvcConfigurer {
return resourceRegistration;
}
private static PromptRegistration greetingPromptRegistration() {
private static SyncPromptRegistration greetingPromptRegistration() {
var prompt = new McpSchema.Prompt("greeting", "A friendly greeting prompt",
List.of(new McpSchema.PromptArgument("name", "The name to greet", true)));
return new PromptRegistration(prompt, getPromptRequest -> {
return new SyncPromptRegistration(prompt, getPromptRequest -> {
String nameArgument = (String) getPromptRequest.arguments().get("name");
if (nameArgument == null) {

View File

@@ -19,7 +19,7 @@
<description>Sample Spring Boot application demonstrating MCP server usage</description>
<properties>
<spring-ai-mcp.version>0.5.1</spring-ai-mcp.version>
<spring-ai-mcp.version>0.6.0</spring-ai-mcp.version>
</properties>
<dependencyManagement>

View File

@@ -31,7 +31,7 @@ public class ClientStdio {
System.out.println(new File(".").getAbsolutePath());
var stdioParams = ServerParameters.builder("java")
.args("-Dtransport.mode=stdio", "-jar",
.args("-Dtransport.mode=stdio", "-Dspring.main.web-application-type=none", "-jar",
"model-context-protocol/mcp-webflux-server/target/mcp-webflux-server-0.0.1-SNAPSHOT.jar")
.build();

View File

@@ -40,7 +40,7 @@ public class SampleClient {
public void run() {
var client = McpClient.using(this.transport).sync();
var client = McpClient.sync(this.transport).build();
client.initialize();

View File

@@ -7,6 +7,31 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
public class McpServerApplication {
public static void main(String[] args) {
// var stackThread = new Thread() {
// @Override
// public void run() {
// try {
// while (true) {
// Thread.sleep(5000);
// java.util.Collection<java.lang.StackTraceElement[]> a1 =
// java.lang.Thread.getAllStackTraces()
// .values();
// for (java.lang.StackTraceElement[] a2 : a1) {
// System.out.println("==========");
// for (java.lang.StackTraceElement a3 : a2) {
// System.out.println(a3.toString());
// }
// }
// }
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// };
// stackThread.setDaemon(false);
// stackThread.start();
SpringApplication.run(McpServerApplication.class, args);
}

View File

@@ -8,17 +8,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.mcp.server.McpAsyncServer;
import org.springframework.ai.mcp.server.McpServer;
import org.springframework.ai.mcp.server.McpServer.PromptRegistration;
import org.springframework.ai.mcp.server.McpServer.ResourceRegistration;
import org.springframework.ai.mcp.server.McpServer.ToolRegistration;
import org.springframework.ai.mcp.server.McpServerFeatures;
import org.springframework.ai.mcp.server.McpSyncServer;
import org.springframework.ai.mcp.server.transport.StdioServerTransport;
import org.springframework.ai.mcp.server.transport.WebFluxSseServerTransport;
import org.springframework.ai.mcp.spec.McpSchema;
import org.springframework.ai.mcp.spec.McpSchema.CallToolResult;
import org.springframework.ai.mcp.spec.McpSchema.GetPromptResult;
import org.springframework.ai.mcp.spec.McpSchema.LoggingMessageNotification;
import org.springframework.ai.mcp.spec.McpSchema.PromptMessage;
import org.springframework.ai.mcp.spec.McpSchema.Role;
import org.springframework.ai.mcp.spec.McpSchema.TextContent;
@@ -62,7 +58,7 @@ public class McpServerConfig {
}
@Bean
public McpAsyncServer mcpServer(ServerMcpTransport transport, OpenLibrary openLibrary) { // @formatter:off
public McpSyncServer mcpServer(ServerMcpTransport transport, OpenLibrary openLibrary) { // @formatter:off
// Configure server capabilities with resource support
var capabilities = McpSchema.ServerCapabilities.builder()
@@ -73,24 +69,24 @@ public class McpServerConfig {
.build();
// Create the server with both tool and resource capabilities
var server = McpServer.using(transport)
McpSyncServer server = McpServer.sync(transport)
.serverInfo("MCP Demo WebFlux Server", "1.0.0")
.capabilities(capabilities)
.resources(systemInfoResourceRegistration()) // Resources
.prompts(greetingPromptRegistration()) // Prompts
.tools(openLibraryToolRegistrations(openLibrary)) // Method based tools
.tools(ToolHelper.toToolRegistration( // java.util.Function based tools
.tools(ToolHelper.toSyncToolRegistration( // java.util.Function based tools
FunctionCallback.builder()
.function("toUpperCase", (Function<ToUpperCaseInput, String>) s -> s.input().toUpperCase())
.description("To upper case")
.inputType(ToUpperCaseInput.class)
.build()))
.async();
.build();
return server; // @formatter:on
} // @formatter:on
public static List<ToolRegistration> openLibraryToolRegistrations(OpenLibrary openLibrary) {
public static List<McpServerFeatures.SyncToolRegistration> openLibraryToolRegistrations(OpenLibrary openLibrary) {
var books = FunctionCallback.builder()
.method("getBooks", String.class)
@@ -104,10 +100,10 @@ public class McpServerConfig {
.targetObject(openLibrary)
.build();
return ToolHelper.toToolRegistration(books, bookTitlesByAuthor);
return ToolHelper.toSyncToolRegistration(books, bookTitlesByAuthor);
}
private static ResourceRegistration systemInfoResourceRegistration() {
private static McpServerFeatures.SyncResourceRegistration systemInfoResourceRegistration() {
// Create a resource registration for system information
var systemInfoResource = new McpSchema.Resource( // @formatter:off
@@ -117,7 +113,7 @@ public class McpServerConfig {
"application/json", null
);
var resourceRegistration = new ResourceRegistration(systemInfoResource, (request) -> {
var resourceRegistration = new McpServerFeatures.SyncResourceRegistration(systemInfoResource, (request) -> {
try {
var systemInfo = Map.of(
"javaVersion", System.getProperty("java.version"),
@@ -140,12 +136,12 @@ public class McpServerConfig {
return resourceRegistration;
}
private static PromptRegistration greetingPromptRegistration() {
private static McpServerFeatures.SyncPromptRegistration greetingPromptRegistration() {
var prompt = new McpSchema.Prompt("greeting", "A friendly greeting prompt",
List.of(new McpSchema.PromptArgument("name", "The name to greet", true)));
return new PromptRegistration(prompt, getPromptRequest -> {
return new McpServerFeatures.SyncPromptRegistration(prompt, getPromptRequest -> {
String nameArgument = (String) getPromptRequest.arguments().get("name");
if (nameArgument == null) {

View File

@@ -18,7 +18,7 @@
<description>Sample Spring Boot application demonstrating MCP WebMvc server usage</description>
<properties>
<spring-ai-mcp.version>0.5.1</spring-ai-mcp.version>
<spring-ai-mcp.version>0.6.0</spring-ai-mcp.version>
</properties>
<dependencyManagement>

View File

@@ -27,7 +27,7 @@ public class ClientStdio {
public static void main(String[] args) {
var stdioParams = ServerParameters.builder("java")
.args("-Dtransport.mode=stdio", "-jar",
.args("-Dtransport.mode=stdio", "-Dspring.main.web-application-type=none", "-jar",
"model-context-protocol/mcp-webmvc-server/target/mcp-webmvc-server-0.0.1-SNAPSHOT.jar")
.build();

View File

@@ -40,7 +40,7 @@ public class SampleClient {
public void run() {
var client = McpClient.using(this.transport).sync();
var client = McpClient.sync(this.transport).build();
client.initialize();

View File

@@ -8,17 +8,15 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.mcp.server.McpAsyncServer;
import org.springframework.ai.mcp.server.McpServer;
import org.springframework.ai.mcp.server.McpServer.PromptRegistration;
import org.springframework.ai.mcp.server.McpServer.ResourceRegistration;
import org.springframework.ai.mcp.server.McpServer.ToolRegistration;
import org.springframework.ai.mcp.server.McpServerFeatures.SyncPromptRegistration;
import org.springframework.ai.mcp.server.McpServerFeatures.SyncResourceRegistration;
import org.springframework.ai.mcp.server.McpServerFeatures.SyncToolRegistration;
import org.springframework.ai.mcp.server.McpSyncServer;
import org.springframework.ai.mcp.server.transport.StdioServerTransport;
import org.springframework.ai.mcp.server.transport.WebMvcSseServerTransport;
import org.springframework.ai.mcp.spec.McpSchema;
import org.springframework.ai.mcp.spec.McpSchema.CallToolResult;
import org.springframework.ai.mcp.spec.McpSchema.GetPromptResult;
import org.springframework.ai.mcp.spec.McpSchema.LoggingMessageNotification;
import org.springframework.ai.mcp.spec.McpSchema.PromptMessage;
import org.springframework.ai.mcp.spec.McpSchema.Role;
import org.springframework.ai.mcp.spec.McpSchema.TextContent;
@@ -63,7 +61,7 @@ public class McpServerConfig implements WebMvcConfigurer {
}
@Bean
public McpAsyncServer mcpServer(ServerMcpTransport transport, OpenLibrary openLibrary) { // @formatter:off
public McpSyncServer mcpServer(ServerMcpTransport transport, OpenLibrary openLibrary) { // @formatter:off
// Configure server capabilities with resource support
var capabilities = McpSchema.ServerCapabilities.builder()
@@ -74,24 +72,24 @@ public class McpServerConfig implements WebMvcConfigurer {
.build();
// Create the server with both tool and resource capabilities
var server = McpServer.using(transport)
var server = McpServer.sync(transport)
.serverInfo("MCP Demo WebMVC Server", "1.0.0")
.capabilities(capabilities)
.resources(systemInfoResourceRegistration())
.prompts(greetingPromptRegistration())
.tools(
ToolHelper.toToolRegistration(
ToolHelper.toSyncToolRegistration(
FunctionCallback.builder()
.function("toUpperCase", (Function<ToUpperCaseInput, String>) s -> s.input().toUpperCase())
.description("To upper case")
.inputType(ToUpperCaseInput.class)
.build()))
.tools(openLibraryToolRegistrations(openLibrary))
.async();
.build();
return server; // @formatter:on
} // @formatter:on
public static List<ToolRegistration> openLibraryToolRegistrations(OpenLibrary openLibrary) {
public static List<SyncToolRegistration> openLibraryToolRegistrations(OpenLibrary openLibrary) {
var books = FunctionCallback.builder()
.method("getBooks", String.class)
@@ -105,10 +103,10 @@ public class McpServerConfig implements WebMvcConfigurer {
.targetObject(openLibrary)
.build();
return ToolHelper.toToolRegistration(books, bookTitlesByAuthor);
return ToolHelper.toSyncToolRegistration(books, bookTitlesByAuthor);
}
private static ResourceRegistration systemInfoResourceRegistration() {
private static SyncResourceRegistration systemInfoResourceRegistration() {
// Create a resource registration for system information
var systemInfoResource = new McpSchema.Resource( // @formatter:off
@@ -118,7 +116,7 @@ public class McpServerConfig implements WebMvcConfigurer {
"application/json", null
);
var resourceRegistration = new ResourceRegistration(systemInfoResource, (request) -> {
var resourceRegistration = new SyncResourceRegistration(systemInfoResource, (request) -> {
try {
var systemInfo = Map.of(
"javaVersion", System.getProperty("java.version"),
@@ -141,12 +139,12 @@ public class McpServerConfig implements WebMvcConfigurer {
return resourceRegistration;
}
private static PromptRegistration greetingPromptRegistration() {
private static SyncPromptRegistration greetingPromptRegistration() {
var prompt = new McpSchema.Prompt("greeting", "A friendly greeting prompt",
List.of(new McpSchema.PromptArgument("name", "The name to greet", true)));
return new PromptRegistration(prompt, getPromptRequest -> {
return new SyncPromptRegistration(prompt, getPromptRequest -> {
String nameArgument = (String) getPromptRequest.arguments().get("name");
if (nameArgument == null) {

View File

@@ -43,7 +43,7 @@
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-ai-mcp</artifactId>
<version>0.5.1</version>
<version>0.6.0</version>
</dependency>
</dependencies>

View File

@@ -80,8 +80,8 @@ public class Application {
getDbPath())
.build();
var mcpClient = McpClient.using(new StdioClientTransport(stdioParams))
.requestTimeout(Duration.ofSeconds(10)).sync();
var mcpClient = McpClient.sync(new StdioClientTransport(stdioParams))
.requestTimeout(Duration.ofSeconds(10)).build();
var init = mcpClient.initialize();

View File

@@ -43,7 +43,7 @@
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-ai-mcp</artifactId>
<version>0.5.1</version>
<version>0.6.0</version>
</dependency>
</dependencies>

View File

@@ -78,8 +78,8 @@ public class Application {
getDbPath())
.build();
var mcpClient = McpClient.using(new StdioClientTransport(stdioParams))
.requestTimeout(Duration.ofSeconds(10)).sync();
var mcpClient = McpClient.sync(new StdioClientTransport(stdioParams))
.requestTimeout(Duration.ofSeconds(10)).build();
var init = mcpClient.initialize();