Skip to content

Commit 46b9170

Browse files
alxkmilayaperumalg
authored andcommitted
test: add test coverage for SyncMcpToolCallback.Builder
Co-authored-by: Oleksandr Klymenko <[email protected]> Signed-off-by: Oleksandr Klymenko <[email protected]>
1 parent 0fd9fd5 commit 46b9170

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

mcp/common/src/test/java/org/springframework/ai/mcp/SyncMcpToolCallbackBuilderTest.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,95 @@ void builderShouldSupportMethodChaining() {
123123
assertThat(callback.getToolDefinition().name()).isEqualTo("chained_tool_name");
124124
}
125125

126+
@Test
127+
void builderShouldNormalizeToolNameWithSpecialCharacters() {
128+
McpSyncClient mcpClient = Mockito.mock(McpSyncClient.class);
129+
McpSchema.Implementation clientInfo = new McpSchema.Implementation("test-client", "1.0.0");
130+
when(mcpClient.getClientInfo()).thenReturn(clientInfo);
131+
132+
Tool tool = Mockito.mock(Tool.class);
133+
when(tool.name()).thenReturn("test-tool-with-dashes");
134+
when(tool.description()).thenReturn("Test description");
135+
136+
SyncMcpToolCallback callback = SyncMcpToolCallback.builder().mcpClient(mcpClient).tool(tool).build();
137+
138+
assertThat(callback.getOriginalToolName()).isEqualTo("test-tool-with-dashes");
139+
assertThat(callback.getToolDefinition().name()).isEqualTo("test_tool_with_dashes");
140+
}
141+
142+
@Test
143+
void builderShouldUseCustomPrefixedNameWithoutNormalization() {
144+
McpSyncClient mcpClient = Mockito.mock(McpSyncClient.class);
145+
146+
Tool tool = Mockito.mock(Tool.class);
147+
when(tool.name()).thenReturn("original-name");
148+
when(tool.description()).thenReturn("Test description");
149+
150+
String customName = "custom-name-with-dashes";
151+
152+
SyncMcpToolCallback callback = SyncMcpToolCallback.builder()
153+
.mcpClient(mcpClient)
154+
.tool(tool)
155+
.prefixedToolName(customName)
156+
.build();
157+
158+
assertThat(callback.getOriginalToolName()).isEqualTo("original-name");
159+
assertThat(callback.getToolDefinition().name()).isEqualTo(customName);
160+
}
161+
162+
@Test
163+
void builderShouldHandlePrefixedToolNameAsNull() {
164+
McpSyncClient mcpClient = Mockito.mock(McpSyncClient.class);
165+
McpSchema.Implementation clientInfo = new McpSchema.Implementation("test-client", "1.0.0");
166+
when(mcpClient.getClientInfo()).thenReturn(clientInfo);
167+
168+
Tool tool = Mockito.mock(Tool.class);
169+
when(tool.name()).thenReturn("test-tool");
170+
when(tool.description()).thenReturn("Description");
171+
172+
SyncMcpToolCallback callback = SyncMcpToolCallback.builder()
173+
.mcpClient(mcpClient)
174+
.tool(tool)
175+
.prefixedToolName(null)
176+
.build();
177+
178+
// When null, it should use the default normalized name
179+
assertThat(callback).isNotNull();
180+
assertThat(callback.getToolDefinition().name()).isEqualTo("test_tool");
181+
}
182+
183+
@Test
184+
void builderShouldCreateNewInstancesForEachBuild() {
185+
McpSyncClient mcpClient = Mockito.mock(McpSyncClient.class);
186+
McpSchema.Implementation clientInfo = new McpSchema.Implementation("test-client", "1.0.0");
187+
when(mcpClient.getClientInfo()).thenReturn(clientInfo);
188+
189+
Tool tool = Mockito.mock(Tool.class);
190+
when(tool.name()).thenReturn("test-tool");
191+
when(tool.description()).thenReturn("Test description");
192+
193+
SyncMcpToolCallback.Builder builder = SyncMcpToolCallback.builder().mcpClient(mcpClient).tool(tool);
194+
195+
SyncMcpToolCallback callback1 = builder.build();
196+
SyncMcpToolCallback callback2 = builder.build();
197+
198+
assertThat(callback1).isNotSameAs(callback2);
199+
assertThat(callback1.getOriginalToolName()).isEqualTo(callback2.getOriginalToolName());
200+
}
201+
202+
@Test
203+
void builderShouldThrowExceptionWhenToolContextConverterIsNull() {
204+
McpSyncClient mcpClient = Mockito.mock(McpSyncClient.class);
205+
Tool tool = Mockito.mock(Tool.class);
206+
when(tool.name()).thenReturn("test-tool");
207+
when(tool.description()).thenReturn("Test description");
208+
209+
assertThatThrownBy(() -> SyncMcpToolCallback.builder()
210+
.mcpClient(mcpClient)
211+
.tool(tool)
212+
.toolContextToMcpMetaConverter(null)
213+
.build()).isInstanceOf(IllegalArgumentException.class)
214+
.hasMessage("ToolContextToMcpMetaConverter must not be null");
215+
}
216+
126217
}

0 commit comments

Comments
 (0)