Solon v4.0.2

solon-ai-talent-gateway

</> markdown
2026年6月10日 下午10:56:12
<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon-ai-talent-gateway</artifactId>
</dependency>

1、描述

Solon AI 技能才展,提供 tool/mcp/openapi gateway 能力的技能。内置有:

Talent说明适用场景
OpenApiGatewayTalentOpenAPI 接口网关,自动解析 Swagger/OpenAPI 文档并代理 REST 调用RESTful API 接口管理
ToolGatewayTalent通用工具网关,管理本地业务工具代码注册的 FunctionTool
McpGatewayTalentMCP 服务网关,按名管理 MCP Server 生命周期MCP Client 连接

2、应用示例

  • OpenApiGatewayTalent
import org.noear.solon.ai.chat.ChatModel;
import org.noear.solon.ai.talents.gateway.OpenApiGatewayTalent;
import org.noear.solon.ai.talents.gateway.openapi.ApiSource;
import org.noear.solon.ai.talents.gateway.openapi.ApiAuthenticator;

// 创建 OpenAPI 网关
OpenApiGatewayTalent gateway = new OpenApiGatewayTalent()
        .dynamicThreshold(8)
        .listThreshold(30)
        .searchThreshold(100)
        .defaultAuthenticator(ApiAuthenticator.bearer("your-token"))
        .retryConfig(3);

// 方式 1:简易添加
gateway.addApi("https://api.example.com/openapi.json", "https://api.example.com");

// 方式 2:完整配置
ApiSource source = new ApiSource();
source.setDocUrl("https://api.example.com/openapi.json");
source.setApiBaseUrl("https://api.example.com");
source.setHeaders(Map.of("X-Custom", "value"));
source.setAllowedTools(List.of("getUser", "listOrders"));  // 白名单
source.setDisallowedTools(List.of("deleteUser"));           // 黑名单
source.setAuthenticator(ApiAuthenticator.apiKey("X-Api-Key", "xxx"));
gateway.addApi(source);

// 构建智能体
ChatModel agent = ChatModel.of("https://api.openai.com/v1/chat/completions")
        .apiKey("your-api-key")
        .model("gpt-4o")
        .defaultTalentAdd(gateway)
        .build();

agent.prompt("查询用户 ID 为 123 的订单列表").call();
  • ToolGatewayTalent
import org.noear.solon.ai.chat.ChatModel;
import org.noear.solon.ai.talents.gateway.ToolGatewayTalent;

// 创建工具网关
ToolGatewayTalent toolGateway = new ToolGatewayTalent()
        .dynamicThreshold(8)
        .listThreshold(40)
        .searchThreshold(100)
        .retryConfig(3);

// 添加工具(支持按分组注册)
toolGateway.addTool("财务工具", financeToolProvider);
toolGateway.addTool("HR工具", hrToolProvider);
toolGateway.addTool(singleFunctionTool);

// 构建智能体
ChatModel agent = ChatModel.of("https://api.openai.com/v1/chat/completions")
        .apiKey("your-api-key")
        .model("gpt-4o")
        .defaultTalentAdd(toolGateway)
        .build();

agent.prompt("查询上个月的财务报表").call();
  • McpGatewayTalent
import org.noear.solon.ai.chat.ChatModel;
import org.noear.solon.ai.talents.gateway.McpGatewayTalent;
import org.noear.solon.ai.mcp.client.McpServerParameters;

// 创建 MCP 网关
McpGatewayTalent mcpGateway = new McpGatewayTalent()
        .dynamicThreshold(8)
        .listThreshold(40)
        .searchThreshold(100)
        .retryConfig(3, 1000);

// 方式 1:通过 McpServerParameters 添加(内部自动构建 Provider)
mcpGateway.addMcpServer("weather", McpServerParameters.builder()
        .transport("stdio")
        .command("npx")
        .args("-y", "@modelcontextprotocol/server-weather")
        .build());

// 方式 2:通过 McpClientProvider 添加
McpClientProvider provider = McpClientProviders.fromMcpServer(params);
mcpGateway.addMcpServer("database", provider);

// 构建智能体
ChatModel agent = ChatModel.of("https://api.openai.com/v1/chat/completions")
        .apiKey("your-api-key")
        .model("gpt-4o")
        .defaultTalentAdd(mcpGateway)
        .build();

agent.prompt("查询北京的天气预报").call();