|
| 1 | +/* |
| 2 | + * Copyright 2023-present ByteChef Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.bytechef.ai.mcp.server.config; |
| 18 | + |
| 19 | +import com.bytechef.embedded.execution.facade.ToolFacade; |
| 20 | +import com.bytechef.embedded.execution.facade.dto.ToolDTO; |
| 21 | +import com.bytechef.platform.constant.Environment; |
| 22 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 24 | +import io.modelcontextprotocol.server.McpServer; |
| 25 | +import io.modelcontextprotocol.server.McpSyncServer; |
| 26 | +import io.modelcontextprotocol.server.transport.WebMvcSseServerTransport; |
| 27 | +import io.modelcontextprotocol.spec.McpSchema; |
| 28 | +import io.modelcontextprotocol.spec.ServerMcpTransport; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.function.Function; |
| 33 | +import org.springframework.ai.mcp.McpToolUtils; |
| 34 | +import org.springframework.ai.tool.ToolCallback; |
| 35 | +import org.springframework.ai.tool.function.FunctionToolCallback; |
| 36 | +import org.springframework.context.annotation.Bean; |
| 37 | +import org.springframework.web.servlet.function.RouterFunction; |
| 38 | +import org.springframework.web.servlet.function.ServerResponse; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author Ivica Cardic |
| 42 | + */ |
| 43 | +//@Configuration |
| 44 | +public class McpServerConfiguration { |
| 45 | + |
| 46 | + private final ToolFacade toolFacade; |
| 47 | + |
| 48 | + @SuppressFBWarnings("EI") |
| 49 | + public McpServerConfiguration(ToolFacade toolFacade) { |
| 50 | + this.toolFacade = toolFacade; |
| 51 | + } |
| 52 | + |
| 53 | + @Bean |
| 54 | + WebMvcSseServerTransport webMvcSseServerTransport(ObjectMapper objectMapper) { |
| 55 | + // TODO - Set /embedded/mcp/message, check ConnectedUserAuthenticationFilter |
| 56 | + // TODO - Set /embedded/sse |
| 57 | + |
| 58 | + return new WebMvcSseServerTransport(objectMapper, "/api/embedded/v1/mcp/message"); |
| 59 | + } |
| 60 | + |
| 61 | + @Bean |
| 62 | + RouterFunction<ServerResponse> routerFunction(WebMvcSseServerTransport transport) { |
| 63 | + return transport.getRouterFunction(); |
| 64 | + } |
| 65 | + |
| 66 | + @Bean |
| 67 | + McpSyncServer mcpServer(ServerMcpTransport transport) { |
| 68 | + var capabilities = McpSchema.ServerCapabilities.builder() |
| 69 | + .resources(false, true) |
| 70 | + .tools(true) |
| 71 | + .prompts(true) |
| 72 | + .logging() |
| 73 | + .build(); |
| 74 | + |
| 75 | + return McpServer.sync(transport) |
| 76 | + .serverInfo("MCP ByteChef Embedded Server", "1.0.0") |
| 77 | + .capabilities(capabilities) |
| 78 | + .tools(McpToolUtils.toSyncToolRegistration(getToolCallbacks())) |
| 79 | + .build(); |
| 80 | + } |
| 81 | + |
| 82 | + public List<ToolCallback> getToolCallbacks() { |
| 83 | + List<ToolCallback> toolCallbacks = new ArrayList<>(); |
| 84 | + |
| 85 | + List<ToolDTO> toolDTOs = toolFacade.getTools(); |
| 86 | + |
| 87 | + for (ToolDTO toolDTO : toolDTOs) { |
| 88 | + FunctionToolCallback.Builder<Map<String, Object>, Object> builder = FunctionToolCallback |
| 89 | + .builder(toolDTO.name(), getToolCallbackFunction(toolDTO.name())) |
| 90 | + .inputType(Map.class) |
| 91 | + .inputSchema(toolDTO.parameters()); |
| 92 | + |
| 93 | + if (toolDTO.description() != null) { |
| 94 | + builder.description(toolDTO.description()); |
| 95 | + } |
| 96 | + |
| 97 | + toolCallbacks.add(builder.build()); |
| 98 | + } |
| 99 | + |
| 100 | + return toolCallbacks; |
| 101 | + } |
| 102 | + |
| 103 | + private Function<Map<String, Object>, Object> getToolCallbackFunction(String toolName) { |
| 104 | + return request -> toolFacade.executeTool(toolName, request, Environment.PRODUCTION, null); |
| 105 | + } |
| 106 | +} |
0 commit comments