Stellar1999 commented on code in PR #15406: URL: https://github.com/apache/dubbo/pull/15406#discussion_r2108436239
########## dubbo-plugin/dubbo-mcp/src/main/java/org/apache/dubbo/mcp/core/McpApplicationDeployListener.java: ########## @@ -0,0 +1,197 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.dubbo.mcp.core; + +import org.apache.dubbo.common.config.Configuration; +import org.apache.dubbo.common.config.ConfigurationUtils; +import org.apache.dubbo.common.constants.CommonConstants; +import org.apache.dubbo.common.constants.LoggerCodeConstants; +import org.apache.dubbo.common.deploy.ApplicationDeployListener; +import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; +import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.threadpool.manager.FrameworkExecutorRepository; +import org.apache.dubbo.common.utils.CollectionUtils; +import org.apache.dubbo.common.utils.NetUtils; +import org.apache.dubbo.config.ProtocolConfig; +import org.apache.dubbo.config.ServiceConfig; +import org.apache.dubbo.config.bootstrap.builders.InternalServiceConfigBuilder; +import org.apache.dubbo.mcp.McpConstant; +import org.apache.dubbo.mcp.tool.DubboMcpGenericCaller; +import org.apache.dubbo.mcp.tool.DubboOpenApiToolConverter; +import org.apache.dubbo.mcp.tool.DubboServiceToolRegistry; +import org.apache.dubbo.mcp.transport.DubboMcpSseTransportProvider; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.FrameworkModel; +import org.apache.dubbo.rpc.model.ProviderModel; +import org.apache.dubbo.rpc.protocol.tri.rest.openapi.DefaultOpenAPIService; + +import java.util.Collection; +import java.util.concurrent.ExecutorService; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.modelcontextprotocol.server.McpAsyncServer; +import io.modelcontextprotocol.server.McpServer; +import io.modelcontextprotocol.spec.McpSchema; + +import static org.apache.dubbo.metadata.util.MetadataServiceVersionUtils.V1; + +public class McpApplicationDeployListener implements ApplicationDeployListener { + + private static final ErrorTypeAwareLogger logger = + LoggerFactory.getErrorTypeAwareLogger(McpApplicationDeployListener.class); + private DubboServiceToolRegistry toolRegistry; + private McpServiceFilter mcpServiceFilter; + private boolean mcpEnable = true; + + private volatile ServiceConfig<McpSseService> serviceConfig; + + private static DubboMcpSseTransportProvider dubboMcpSseTransportProvider; + + @Override + public void onInitialize(ApplicationModel scopeModel) {} + + @Override + public void onStarting(ApplicationModel applicationModel) {} + + public static DubboMcpSseTransportProvider getDubboMcpSseTransportProvider() { + return dubboMcpSseTransportProvider; + } + + @Override + public void onStarted(ApplicationModel applicationModel) { + Configuration globalConf = ConfigurationUtils.getGlobalConfiguration(ApplicationModel.defaultModel()); + mcpEnable = globalConf.getBoolean(McpConstant.SETTINGS_MCP_ENABLE, true); + if (!mcpEnable) { + logger.info("MCP service is disabled, skipping initialization"); + return; + } + try { + logger.info("Initializing MCP server and tools"); + + // Initialize service filter + mcpServiceFilter = new McpServiceFilter(applicationModel); + + dubboMcpSseTransportProvider = new DubboMcpSseTransportProvider(new ObjectMapper()); + McpSchema.ServerCapabilities.ToolCapabilities toolCapabilities = + new McpSchema.ServerCapabilities.ToolCapabilities(true); + McpSchema.ServerCapabilities serverCapabilities = + new McpSchema.ServerCapabilities(null, null, null, null, toolCapabilities); + + McpAsyncServer mcpAsyncServer = McpServer.async(dubboMcpSseTransportProvider) + .capabilities(serverCapabilities) + .build(); + + FrameworkModel frameworkModel = applicationModel.getFrameworkModel(); + DefaultOpenAPIService defaultOpenAPIService = new DefaultOpenAPIService(frameworkModel); + + DubboOpenApiToolConverter toolConverter = new DubboOpenApiToolConverter(defaultOpenAPIService); + + DubboMcpGenericCaller genericCaller = new DubboMcpGenericCaller(applicationModel); + + toolRegistry = new DubboServiceToolRegistry(mcpAsyncServer, toolConverter, genericCaller); + + Collection<ProviderModel> providerModels = + applicationModel.getApplicationServiceRepository().allProviderModels(); + + int registeredCount = 0; + for (ProviderModel pm : providerModels) { + // Check if service should be exposed as MCP tool + if (mcpServiceFilter.shouldExposeAsMcpTool(pm)) { + // Get MCP tool configuration + McpServiceFilter.McpToolConfig toolConfig = mcpServiceFilter.getMcpToolConfig(pm); + toolRegistry.registerService(pm, toolConfig); + registeredCount++; + } + } Review Comment: Do you mean that if the service goes offline, the tool also needs to be taken down? Or rather, we need to make sure the service and the tool stay consistent with each other? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
