2020import modelengine .fel .engine .flows .AiProcessFlow ;
2121import modelengine .fel .engine .operators .patterns .AbstractAgent ;
2222import modelengine .fel .engine .operators .prompts .Prompts ;
23- import modelengine .fel .tool .mcp .client .McpClient ;
24- import modelengine .fel .tool .mcp .client .McpClientFactory ;
25- import modelengine .fel .tool .mcp .entity .Tool ;
2623import modelengine .fel .tool .model .transfer .ToolData ;
2724import modelengine .fit .jober .aipp .domains .appversion .service .AppVersionService ;
2825import modelengine .fit .jober .aipp .enums .MetaInstStatusEnum ;
29- import modelengine .fit .jober .aipp .util .McpUtils ;
26+ import modelengine .fit .jober .aipp .util .LangChain4jMcpClient ;
27+ import modelengine .fit .jober .aipp .util .McpClientFactory ;
3028import modelengine .fitframework .inspection .Validation ;
3129import modelengine .jade .store .service .ToolService ;
3230import modelengine .fit .jade .aipp .formatter .OutputFormatterChain ;
7068import modelengine .fitframework .util .StringUtils ;
7169import modelengine .fitframework .util .UuidUtils ;
7270
71+ import dev .langchain4j .model .chat .request .json .JsonObjectSchema ;
72+ import dev .langchain4j .agent .tool .ToolSpecification ;
73+
7374import java .io .IOException ;
7475import java .util .ArrayList ;
7576import java .util .Collections ;
@@ -113,9 +114,9 @@ public class LlmComponent implements FlowableService {
113114 private final AippModelCenter aippModelCenter ;
114115 private final PromptBuilderChain promptBuilderChain ;
115116 private final AppTaskInstanceService appTaskInstanceService ;
116- private final McpClientFactory mcpClientFactory ;
117117 private final OutputFormatterChain formatterChain ;
118118 private final AppVersionService appVersionService ;
119+ private final McpClientFactory mcpClientFactory ;
119120
120121 /**
121122 * 大模型节点构造器,内部通过提供的 agent 和 tool 构建智能体工作流。
@@ -129,7 +130,9 @@ public class LlmComponent implements FlowableService {
129130 * @param aippModelCenter 表示模型中心的 {@link AippModelCenter}。
130131 * @param promptBuilderChain 表示提示器构造器职责链的 {@link PromptBuilderChain}。
131132 * @param appTaskInstanceService 表示任务实例服务的 {@link AppTaskInstanceService}。
132- * @param mcpClientFactory 表示大模型上下文客户端工厂的 {@link McpClientFactory}。
133+ * @param formatterChain 表示输出格式化器链的 {@link OutputFormatterChain}。
134+ * @param appVersionService 表示应用版本服务的 {@link AppVersionService}。
135+ * @param mcpClientFactory 表示 MCP 客户端工厂的 {@link McpClientFactory}。
133136 */
134137 public LlmComponent (FlowInstanceService flowInstanceService ,
135138 @ Fit ToolService toolService ,
@@ -141,13 +144,15 @@ public LlmComponent(FlowInstanceService flowInstanceService,
141144 PromptBuilderChain promptBuilderChain ,
142145 AppTaskInstanceService appTaskInstanceService ,
143146 OutputFormatterChain formatterChain ,
144- McpClientFactory mcpClientFactory , AppVersionService appVersionService ) {
147+ AppVersionService appVersionService ,
148+ McpClientFactory mcpClientFactory ) {
145149 this .flowInstanceService = flowInstanceService ;
146150 this .toolService = toolService ;
147151 this .aippLogService = aippLogService ;
148152 this .aippLogStreamService = aippLogStreamService ;
149153 this .serializer = notNull (serializer , "The serializer cannot be nul." );
150154 this .aippModelCenter = aippModelCenter ;
155+ this .mcpClientFactory = mcpClientFactory ;
151156
152157 // handleTask从入口开始处理,callback从agent node开始处理
153158 this .agentFlow = AiFlows .<Tip >create ()
@@ -157,7 +162,6 @@ public LlmComponent(FlowInstanceService flowInstanceService,
157162 .close ();
158163 this .promptBuilderChain = promptBuilderChain ;
159164 this .appTaskInstanceService = appTaskInstanceService ;
160- this .mcpClientFactory = notNull (mcpClientFactory , "The mcp client factory cannot be null." );
161165 this .formatterChain = formatterChain ;
162166 this .appVersionService = appVersionService ;
163167 }
@@ -482,12 +486,12 @@ private List<ToolInfo> buildMcpToolInfos(Map<String, Object> mcpServersConfig) {
482486 String url = Validation .notBlank (ObjectUtils .cast (serverConfig .get (AippConst .MCP_SERVER_URL_KEY )),
483487 "The mcp url should not be empty." );
484488
485- try (McpClient mcpClient = this .mcpClientFactory .create (McpUtils . getBaseUrl ( url ),
486- McpUtils . getSseEndpoint ( url ))) {
487- mcpClient . initialize ();
488- List < Tool > tools = mcpClient . getTools ();
489- result . addAll ( tools . stream (). map ( tool -> buildMcpToolInfo ( serverName , tool , serverConfig )) .toList ());
490- } catch (IOException exception ) {
489+ try (LangChain4jMcpClient mcpClient = this .mcpClientFactory .create (url )) {
490+ List < ToolSpecification > tools = mcpClient . getTools ();
491+ result . addAll ( tools . stream ()
492+ . map ( tool -> buildMcpToolInfo ( serverName , tool , serverConfig ))
493+ .toList ());
494+ } catch (Exception exception ) {
491495 throw new AippException (AippErrCode .CALL_MCP_SERVER_FAILED , exception .getMessage ());
492496 }
493497 });
@@ -515,14 +519,23 @@ private ToolInfo buildToolInfo(ToolData toolData) {
515519 .build ();
516520 }
517521
518- private static ToolInfo buildMcpToolInfo (String serverName , Tool tool , Map <String , Object > serverConfig ) {
522+ private static ToolInfo buildMcpToolInfo (String serverName ,
523+ ToolSpecification tool , Map <String , Object > serverConfig ) {
524+ JsonObjectSchema toolParams = tool .parameters ();
525+ Map <String , Object > parametersMap = new HashMap <>();
526+ if (toolParams != null ) {
527+ parametersMap .put ("type" , "object" );
528+ parametersMap .put ("properties" , toolParams .properties ());
529+ parametersMap .put ("required" , toolParams .required ());
530+ }
531+
519532 return ToolInfo .custom ()
520- .name (buildUniqueToolName (AippConst .MCP_SERVER_TYPE , serverName , tool .getName ()))
521- .description (tool .getDescription ())
522- .parameters (tool . getInputSchema () )
533+ .name (buildUniqueToolName (AippConst .MCP_SERVER_TYPE , serverName , tool .name ()))
534+ .description (tool .description ())
535+ .parameters (parametersMap )
523536 .extensions (MapBuilder .<String , Object >get ()
524537 .put (AippConst .MCP_SERVER_KEY , serverConfig )
525- .put (AippConst .TOOL_REAL_NAME , tool .getName ())
538+ .put (AippConst .TOOL_REAL_NAME , tool .name ())
526539 .build ())
527540 .build ();
528541 }
0 commit comments