From 25ad5ed052847f0543070a99c256f6bd7a085b4f Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Fri, 24 Jan 2025 20:24:08 +0100 Subject: [PATCH] Remove the spring-ai-mcp module from the MCP Java SDK Signed-off-by: Christian Tzolov --- mcp-bom/pom.xml | 6 - pom.xml | 1 - spring-ai-mcp/README.md | 5 - spring-ai-mcp/pom.xml | 53 ----- .../ai/mcp/spring/McpFunctionCallback.java | 68 ------ .../ai/mcp/spring/ToolHelper.java | 218 ------------------ 6 files changed, 351 deletions(-) delete mode 100644 spring-ai-mcp/README.md delete mode 100644 spring-ai-mcp/pom.xml delete mode 100644 spring-ai-mcp/src/main/java/org/springframework/ai/mcp/spring/McpFunctionCallback.java delete mode 100644 spring-ai-mcp/src/main/java/org/springframework/ai/mcp/spring/ToolHelper.java diff --git a/mcp-bom/pom.xml b/mcp-bom/pom.xml index 23e86ad..eb73bc8 100644 --- a/mcp-bom/pom.xml +++ b/mcp-bom/pom.xml @@ -54,12 +54,6 @@ ${project.version} - - - org.springframework.experimental - spring-ai-mcp - ${project.version} - diff --git a/pom.xml b/pom.xml index 68ba29c..2092d7d 100644 --- a/pom.xml +++ b/pom.xml @@ -107,7 +107,6 @@ mcp mcp-transport/mcp-webflux-sse-transport mcp-transport/mcp-webmvc-sse-transport - spring-ai-mcp mcp-docs mcp-test diff --git a/spring-ai-mcp/README.md b/spring-ai-mcp/README.md deleted file mode 100644 index 8ba646f..0000000 --- a/spring-ai-mcp/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# Spring AI MCP Spring - -Spring Integration module for Model Control Protocol (MCP) that provides Spring-specific functionality for working with MCP clients. - -Find more at [Java MCP SDK](https://docs.spring.io/spring-ai-mcp/reference/spring-mcp.html) \ No newline at end of file diff --git a/spring-ai-mcp/pom.xml b/spring-ai-mcp/pom.xml deleted file mode 100644 index fd2e156..0000000 --- a/spring-ai-mcp/pom.xml +++ /dev/null @@ -1,53 +0,0 @@ - - - 4.0.0 - - org.springframework.experimental - mcp-parent - 0.7.0-SNAPSHOT - - spring-ai-mcp - Spring AI MCP - Spring Framework integration for Model Context Protocol (MCP), providing Spring AI function calling capabilities and Spring-friendly abstractions for MCP clients and MCP servers - https://github.com/spring-projects-experimental/spring-ai-mcp - - - https://github.com/spring-projects-experimental/spring-ai-mcp - git://github.com/spring-projects-experimental/spring-ai-mcp.git - git@github.com:spring-projects-experimental/spring-ai-mcp.git - - - - - org.springframework.ai - spring-ai-core - ${spring-ai.version} - - - org.springframework - spring-context - - - org.springframework - spring-messaging - - - - - - org.springframework.experimental - mcp - ${project.version} - - - - org.springframework - spring-core - ${springframework.version} - - - - - diff --git a/spring-ai-mcp/src/main/java/org/springframework/ai/mcp/spring/McpFunctionCallback.java b/spring-ai-mcp/src/main/java/org/springframework/ai/mcp/spring/McpFunctionCallback.java deleted file mode 100644 index e06b026..0000000 --- a/spring-ai-mcp/src/main/java/org/springframework/ai/mcp/spring/McpFunctionCallback.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2024-2024 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.ai.mcp.spring; - -import java.util.Map; - -import org.springframework.ai.mcp.client.McpSyncClient; -import org.springframework.ai.mcp.spec.McpSchema.CallToolRequest; -import org.springframework.ai.mcp.spec.McpSchema.CallToolResult; -import org.springframework.ai.mcp.spec.McpSchema.Tool; - -import org.springframework.ai.model.ModelOptionsUtils; -import org.springframework.ai.model.function.FunctionCallback; - -/** - * @author Christian Tzolov - */ - -public class McpFunctionCallback implements FunctionCallback { - - // TODO: revisit function calling as well to handle the async case - private final McpSyncClient mcpClient; - - private final Tool tool; - - public McpFunctionCallback(McpSyncClient clientSession, Tool tool) { - this.mcpClient = clientSession; - this.tool = tool; - } - - @Override - public String getName() { - return this.tool.name(); - } - - @Override - public String getDescription() { - return this.tool.description(); - } - - @Override - public String getInputTypeSchema() { - return ModelOptionsUtils.toJsonString(this.tool.inputSchema()); - } - - @Override - public String call(String functionInput) { - Map arguments = ModelOptionsUtils.jsonToMap(functionInput); - CallToolResult response = this.mcpClient.callTool(new CallToolRequest(this.getName(), arguments)); - // Todo handle errors - return ModelOptionsUtils.toJsonString(response.content()); - } - -} diff --git a/spring-ai-mcp/src/main/java/org/springframework/ai/mcp/spring/ToolHelper.java b/spring-ai-mcp/src/main/java/org/springframework/ai/mcp/spring/ToolHelper.java deleted file mode 100644 index a3c5e44..0000000 --- a/spring-ai-mcp/src/main/java/org/springframework/ai/mcp/spring/ToolHelper.java +++ /dev/null @@ -1,218 +0,0 @@ -/* -* Copyright 2024 - 2024 the original author or authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* https://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ -package org.springframework.ai.mcp.spring; - -import java.util.List; -import java.util.Map; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.fasterxml.jackson.module.jsonSchema.JsonSchema; -import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator; - -import org.springframework.ai.chat.model.ToolContext; -import org.springframework.ai.mcp.server.McpServer; -import org.springframework.ai.mcp.server.McpServerFeatures; -import org.springframework.ai.mcp.spec.McpSchema; -import org.springframework.ai.model.ModelOptionsUtils; -import org.springframework.ai.model.function.FunctionCallback; -import org.springframework.util.ClassUtils; - -/** - * Utility class that provides helper methods for working with Model Context Protocol - * (MCP) tools in a Spring AI environment. This class facilitates the integration between - * Spring AI's function callbacks and MCP's tool system. - * - *

- * The MCP tool system enables servers to expose executable functionality to language - * models, allowing them to interact with external systems, perform computations, and take - * actions in the real world. Each tool is uniquely identified by a name and includes - * metadata describing its schema. - * - *

- * This helper class provides methods to: - *

    - *
  • Convert Spring AI's {@link FunctionCallback} instances to MCP tool - * registrations
  • - *
  • Generate JSON schemas for tool input validation
  • - *
- * - * @author Christian Tzolov - * @see org.springframework.ai.model.function.FunctionCallback - * @see org.springframework.ai.mcp.server.McpServer.ToolRegistration - * @see org.springframework.ai.mcp.spec.McpSchema.Tool - */ -public final class ToolHelper { - - private ToolHelper() { - } - - public static List toSyncToolRegistration( - List functionCallbacks) { - return functionCallbacks.stream().map(ToolHelper::toSyncToolRegistration).toList(); - } - - @Deprecated - public static List toToolRegistration(List functionCallbacks) { - return functionCallbacks.stream().map(ToolHelper::toToolRegistration).toList(); - } - - public static List toSyncToolRegistration( - FunctionCallback... functionCallbacks) { - return toSyncToolRegistration(List.of(functionCallbacks)); - } - - @Deprecated - public static List toToolRegistration(FunctionCallback... functionCallbacks) { - return toToolRegistration(List.of(functionCallbacks)); - } - - /** - * Converts a Spring AI FunctionCallback to an MCP SyncToolRegistration. This enables - * Spring AI functions to be exposed as MCP tools that can be discovered and invoked - * by language models. - * - *

- * The conversion process: - *

    - *
  • Creates an MCP Tool with the function's name and input schema
  • - *
  • Wraps the function's execution in a SyncToolRegistration that handles the MCP - * protocol
  • - *
  • Provides error handling and result formatting according to MCP - * specifications
  • - *
- * - * You can use the FunctionCallback builder to create a new instance of - * FunctionCallback using either java.util.function.Function or Method reference. - * @param functionCallback the Spring AI function callback to convert - * @return an MCP SyncToolRegistration that wraps the function callback - * @throws RuntimeException if there's an error during the function execution - */ - public static McpServerFeatures.SyncToolRegistration toSyncToolRegistration(FunctionCallback functionCallback) { - var tool = new McpSchema.Tool(functionCallback.getName(), functionCallback.getName(), - functionCallback.getInputTypeSchema()); - - return new McpServerFeatures.SyncToolRegistration(tool, request -> { - try { - String callResult = functionCallback.call(ModelOptionsUtils.toJsonString(request)); - return new McpSchema.CallToolResult(List.of(new McpSchema.TextContent(callResult)), false); - } - catch (Exception e) { - return new McpSchema.CallToolResult(List.of(new McpSchema.TextContent(e.getMessage())), true); - } - }); - } - - /** - * Converts a Spring AI FunctionCallback to an MCP ToolRegistration. This enables - * Spring AI functions to be exposed as MCP tools that can be discovered and invoked - * by language models. - * - *

- * The conversion process: - *

    - *
  • Creates an MCP Tool with the function's name and input schema
  • - *
  • Wraps the function's execution in a ToolRegistration that handles the MCP - * protocol
  • - *
  • Provides error handling and result formatting according to MCP - * specifications
  • - *
- * - * You can use the FunctionCallback builder to create a new instance of - * FunctionCallback using either java.util.function.Function or Method reference. - * @param functionCallback the Spring AI function callback to convert - * @return an MCP ToolRegistration that wraps the function callback - * @throws RuntimeException if there's an error during the function execution - * @deprecated Use {@link #toSyncToolRegistration(FunctionCallback)}. - */ - @Deprecated - public static McpServer.ToolRegistration toToolRegistration(FunctionCallback functionCallback) { - - var tool = new McpSchema.Tool(functionCallback.getName(), functionCallback.getName(), - functionCallback.getInputTypeSchema()); - - return new McpServer.ToolRegistration(tool, request -> { - try { - String callResult = functionCallback.call(ModelOptionsUtils.toJsonString(request)); - return new McpSchema.CallToolResult(List.of(new McpSchema.TextContent(callResult)), false); - } - catch (Exception e) { - return new McpSchema.CallToolResult(List.of(new McpSchema.TextContent(e.getMessage())), true); - } - }); - } - - /** - * Generates a JSON schema for a map of named classes using the default ObjectMapper. - * This schema can be used to validate tool inputs according to the MCP specification. - * @param namedClasses a map of class names to their corresponding Class objects - * @return a JSON schema string that describes the structure of the named classes - * @throws RuntimeException if schema generation fails - * @see #generateJsonSchema(Map, ObjectMapper) - */ - public static String generateJsonSchema(Map> namedClasses) { - return generateJsonSchema(namedClasses, new ObjectMapper()); - } - - /** - * Generates a JSON schema for a map of named classes using a custom ObjectMapper. The - * generated schema follows the JSON Schema Draft 2020-12 specification and describes - * the structure of the provided classes. - * - *

- * This method: - *

    - *
  • Creates a schema that validates the structure of tool inputs
  • - *
  • Excludes ToolContext class from schema generation
  • - *
  • Uses Jackson's JsonSchemaGenerator for accurate type representation
  • - *
- * @param namedClasses a map of class names to their corresponding Class objects - * @param mapper the ObjectMapper to use for JSON processing - * @return a JSON schema string that describes the structure of the named classes - * @throws RuntimeException if schema generation fails - */ - public static String generateJsonSchema(Map> namedClasses, ObjectMapper mapper) { - try { - JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper); - - ObjectNode rootNode = mapper.createObjectNode(); - rootNode.put("$schema", "https://json-schema.org/draft/2020-12/schema"); - rootNode.put("type", "object"); - ObjectNode propertiesNode = rootNode.putObject("properties"); - - for (Map.Entry> entry : namedClasses.entrySet()) { - String className = entry.getKey(); - Class clazz = entry.getValue(); - - if (ClassUtils.isAssignable(clazz, ToolContext.class)) { - // Skip the ToolContext class from the schema generation. - continue; - } - - JsonSchema schema = schemaGen.generateSchema(clazz); - JsonNode schemaNode = mapper.valueToTree(schema); - propertiesNode.set(className, schemaNode); - } - - return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode); - } - catch (Exception e) { - throw new RuntimeException(e); - } - } - -}