Skip to content

Commit c0c5a26

Browse files
authored
refactor(core): refactor toolkit and skillBox registration (#377)
1 parent 44056ef commit c0c5a26

4 files changed

Lines changed: 177 additions & 177 deletions

File tree

agentscope-core/src/main/java/io/agentscope/core/skill/SkillBox.java

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -461,34 +461,29 @@ public void apply() {
461461
}
462462
skillBox.registerSkill(skill);
463463

464-
if (toolObject != null || agentTool != null || mcpClientWrapper != null) {
464+
if (toolObject != null
465+
|| agentTool != null
466+
|| mcpClientWrapper != null
467+
|| subAgentProvider != null) {
465468
if (toolkit == null && (toolkit = skillBox.toolkit) == null) {
466-
throw new IllegalStateException("Must call toolkit() before apply()");
469+
throw new IllegalStateException(
470+
"Must bind toolkit or call toolkit() before apply()");
467471
}
468472
String skillToolGroup = skill.getSkillId() + "_skill_tools";
469473
if (toolkit.getToolGroup(skillToolGroup) == null) {
470474
toolkit.createToolGroup(skillToolGroup, skillToolGroup, false);
471475
}
472-
Toolkit.ToolRegistration toolRegistration =
473-
toolkit.registration()
474-
.group(skillToolGroup)
475-
.presetParameters(presetParameters)
476-
.extendedModel(extendedModel)
477-
.enableTools(enableTools)
478-
.disableTools(disableTools);
479-
if (toolObject != null) {
480-
toolRegistration.tool(toolObject);
481-
}
482-
if (agentTool != null) {
483-
toolRegistration.agentTool(agentTool);
484-
}
485-
if (mcpClientWrapper != null) {
486-
toolRegistration.mcpClient(mcpClientWrapper);
487-
}
488-
if (subAgentProvider != null) {
489-
toolRegistration.subAgent(subAgentProvider, subAgentConfig);
490-
}
491-
toolRegistration.apply();
476+
toolkit.registration()
477+
.group(skillToolGroup)
478+
.presetParameters(presetParameters)
479+
.extendedModel(extendedModel)
480+
.enableTools(enableTools)
481+
.disableTools(disableTools)
482+
.agentTool(agentTool)
483+
.tool(toolObject)
484+
.mcpClient(mcpClientWrapper)
485+
.subAgent(subAgentProvider, subAgentConfig)
486+
.apply();
492487
}
493488
}
494489
}

agentscope-core/src/main/java/io/agentscope/core/tool/Toolkit.java

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -696,13 +696,6 @@ private ToolRegistration(Toolkit toolkit) {
696696
* @return This builder for chaining
697697
*/
698698
public ToolRegistration tool(Object toolObject) {
699-
if (this.agentTool != null
700-
|| this.mcpClientWrapper != null
701-
|| this.subAgentProvider != null) {
702-
throw new IllegalStateException(
703-
"Cannot set multiple registration types. Use only one of: tool(),"
704-
+ " agentTool(), mcpClient(), or subAgent().");
705-
}
706699
this.toolObject = toolObject;
707700
return this;
708701
}
@@ -714,13 +707,6 @@ public ToolRegistration tool(Object toolObject) {
714707
* @return This builder for chaining
715708
*/
716709
public ToolRegistration agentTool(AgentTool agentTool) {
717-
if (this.toolObject != null
718-
|| this.mcpClientWrapper != null
719-
|| this.subAgentProvider != null) {
720-
throw new IllegalStateException(
721-
"Cannot set multiple registration types. Use only one of: tool(),"
722-
+ " agentTool(), mcpClient(), or subAgent().");
723-
}
724710
this.agentTool = agentTool;
725711
return this;
726712
}
@@ -732,13 +718,6 @@ public ToolRegistration agentTool(AgentTool agentTool) {
732718
* @return This builder for chaining
733719
*/
734720
public ToolRegistration mcpClient(McpClientWrapper mcpClientWrapper) {
735-
if (this.toolObject != null
736-
|| this.agentTool != null
737-
|| this.subAgentProvider != null) {
738-
throw new IllegalStateException(
739-
"Cannot set multiple registration types. Use only one of: tool(),"
740-
+ " agentTool(), mcpClient(), or subAgent().");
741-
}
742721
this.mcpClientWrapper = mcpClientWrapper;
743722
return this;
744723
}
@@ -808,13 +787,6 @@ public ToolRegistration subAgent(SubAgentProvider<?> provider) {
808787
* @see SubAgentConfig#defaults()
809788
*/
810789
public ToolRegistration subAgent(SubAgentProvider<?> provider, SubAgentConfig config) {
811-
if (this.toolObject != null
812-
|| this.agentTool != null
813-
|| this.mcpClientWrapper != null) {
814-
throw new IllegalStateException(
815-
"Cannot set multiple registration types. Use only one of: tool(),"
816-
+ " agentTool(), mcpClient(), or subAgent().");
817-
}
818790
this.subAgentProvider = provider;
819791
this.subAgentConfig = config;
820792
return this;
@@ -893,9 +865,27 @@ public ToolRegistration extendedModel(ExtendedModel extendedModel) {
893865
/**
894866
* Apply the registration with all configured options.
895867
*
896-
* @throws IllegalStateException if none of tool(), agentTool(), or mcpClient() was set
868+
* @throws IllegalStateException if none of tool(), agentTool(), mcpClient() or subAgent() was set
869+
* @throws IllegalStateException if set multiple of: tool(), agentTool(), mcpClient(), or subAgent().
897870
*/
898871
public void apply() {
872+
int toolCount = 0;
873+
if (toolObject != null) toolCount++;
874+
if (agentTool != null) toolCount++;
875+
if (mcpClientWrapper != null) toolCount++;
876+
if (subAgentProvider != null) toolCount++;
877+
878+
if (toolCount == 0) {
879+
throw new IllegalStateException(
880+
"Must call one of: tool(), agentTool(), mcpClient(), or subAgent() before"
881+
+ " apply()");
882+
}
883+
if (toolCount > 1) {
884+
throw new IllegalStateException(
885+
"Cannot set multiple registration types. Use only one of: tool(),"
886+
+ " agentTool(), mcpClient(), or subAgent().");
887+
}
888+
899889
if (toolObject != null) {
900890
toolkit.registerTool(toolObject, groupName, extendedModel, presetParameters);
901891
} else if (agentTool != null) {
@@ -917,10 +907,6 @@ public void apply() {
917907
} else if (subAgentProvider != null) {
918908
SubAgentTool subAgentTool = new SubAgentTool(subAgentProvider, subAgentConfig);
919909
toolkit.registerAgentTool(subAgentTool, groupName, extendedModel, null, null);
920-
} else {
921-
throw new IllegalStateException(
922-
"Must call one of: tool(), agentTool(), mcpClient(), or subAgent() before"
923-
+ " apply()");
924910
}
925911
}
926912
}

agentscope-core/src/test/java/io/agentscope/core/skill/SkillBoxTest.java

Lines changed: 0 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -145,128 +145,6 @@ void testThrowExceptionForNullSkillIdInOperations() {
145145
assertThrows(IllegalArgumentException.class, () -> skillBox.exists(null));
146146
}
147147

148-
@Test
149-
@DisplayName("Should register skill with tool object")
150-
void testRegisterSkillWithToolObject() {
151-
AgentSkill skill =
152-
new AgentSkill("Test Tool Skill", "Skill with tool object", "# Test Tool", null);
153-
154-
skillBox.registration().skill(skill).tool(new TestToolObject()).apply();
155-
156-
assertTrue(skillBox.exists(skill.getSkillId()));
157-
AgentTool tool = toolkit.getTool("test_tool_method");
158-
assertNotNull(tool, "Tool from tool object should be registered");
159-
assertEquals("test_tool_method", tool.getName());
160-
}
161-
162-
@Test
163-
@DisplayName("Should register skill with mcp client")
164-
void testRegisterSkillWithMcpClient() {
165-
// Mock MCP client
166-
McpClientWrapper mcpClient = mock(McpClientWrapper.class);
167-
McpSchema.Tool mockToolInfo =
168-
new McpSchema.Tool(
169-
"mcp_test_tool",
170-
null,
171-
"Test MCP tool",
172-
new McpSchema.JsonSchema("object", null, null, null, null, null),
173-
null,
174-
null,
175-
null);
176-
when(mcpClient.listTools()).thenReturn(Mono.just(List.of(mockToolInfo)));
177-
when(mcpClient.isInitialized()).thenReturn(true);
178-
when(mcpClient.initialize()).thenReturn(Mono.empty());
179-
when(mcpClient.getName()).thenReturn("test-mcp-client");
180-
181-
AgentSkill skill = new AgentSkill("MCP Skill", "Skill with MCP client", "# MCP Test", null);
182-
183-
skillBox.registration().skill(skill).mcpClient(mcpClient).apply();
184-
185-
assertTrue(skillBox.exists(skill.getSkillId()));
186-
AgentTool tool = toolkit.getTool("mcp_test_tool");
187-
assertNotNull(tool, "Tool from MCP client should be registered");
188-
assertEquals("mcp_test_tool", tool.getName());
189-
}
190-
191-
@Test
192-
@DisplayName("Should throw exception when registering multiple tool types")
193-
void testThrowExceptionWhenRegisteringMultipleToolTypes() {
194-
AgentTool agentTool = createTestTool("custom_agent_tool");
195-
TestToolObject toolObject = new TestToolObject();
196-
197-
AgentSkill skill =
198-
new AgentSkill(
199-
"Mixed Skill",
200-
"Skill with both agent tool and tool object",
201-
"# Mixed",
202-
null);
203-
204-
// Should throw when trying to register both agentTool and tool object
205-
IllegalStateException exception =
206-
assertThrows(
207-
IllegalStateException.class,
208-
() ->
209-
skillBox.registration()
210-
.skill(skill)
211-
.agentTool(agentTool)
212-
.tool(toolObject)
213-
.apply());
214-
215-
assertTrue(
216-
exception.getMessage().contains("Cannot set multiple registration types"),
217-
"Exception message should mention multiple registration types");
218-
}
219-
220-
@Test
221-
@DisplayName("Should throw exception when registering tool object and mcp client")
222-
void testThrowExceptionWhenRegisteringToolObjectAndMcpClient() {
223-
TestToolObject toolObject = new TestToolObject();
224-
McpClientWrapper mcpClient = mock(McpClientWrapper.class);
225-
226-
AgentSkill skill =
227-
new AgentSkill("Mixed Skill", "Skill with multiple types", "# Mixed", null);
228-
229-
// Should throw when trying to register both tool object and mcp client
230-
IllegalStateException exception =
231-
assertThrows(
232-
IllegalStateException.class,
233-
() ->
234-
skillBox.registration()
235-
.skill(skill)
236-
.tool(toolObject)
237-
.mcpClient(mcpClient)
238-
.apply());
239-
240-
assertTrue(
241-
exception.getMessage().contains("Cannot set multiple registration types"),
242-
"Exception message should mention multiple registration types");
243-
}
244-
245-
@Test
246-
@DisplayName("Should throw exception when registering agent tool and mcp client")
247-
void testThrowExceptionWhenRegisteringAgentToolAndMcpClient() {
248-
AgentTool agentTool = createTestTool("custom_agent_tool");
249-
McpClientWrapper mcpClient = mock(McpClientWrapper.class);
250-
251-
AgentSkill skill =
252-
new AgentSkill("Mixed Skill", "Skill with multiple types", "# Mixed", null);
253-
254-
// Should throw when trying to register both agent tool and mcp client
255-
IllegalStateException exception =
256-
assertThrows(
257-
IllegalStateException.class,
258-
() ->
259-
skillBox.registration()
260-
.skill(skill)
261-
.agentTool(agentTool)
262-
.mcpClient(mcpClient)
263-
.apply());
264-
265-
assertTrue(
266-
exception.getMessage().contains("Cannot set multiple registration types"),
267-
"Exception message should mention multiple registration types");
268-
}
269-
270148
@Test
271149
@DisplayName("Should successfully register when only tool object is provided")
272150
void testSuccessfullyRegisterWhenOnlyToolObjectProvided() {

0 commit comments

Comments
 (0)