|
| 1 | +/* |
| 2 | + * SchemaCrawler AI |
| 3 | + * http://www.schemacrawler.com |
| 4 | + * Copyright (c) 2000-2025, Sualeh Fatehi <sualeh@hotmail.com>. |
| 5 | + * All rights reserved. |
| 6 | + * SPDX-License-Identifier: CC-BY-NC-4.0 |
| 7 | + */ |
| 8 | + |
| 9 | +package schemacrawler.tools.ai.mcpserver.server.test; |
| 10 | + |
| 11 | +import static org.hamcrest.CoreMatchers.is; |
| 12 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 13 | +import static org.hamcrest.Matchers.not; |
| 14 | +import static org.hamcrest.Matchers.nullValue; |
| 15 | + |
| 16 | +import io.modelcontextprotocol.server.McpServerFeatures.SyncToolSpecification; |
| 17 | +import io.modelcontextprotocol.spec.McpSchema.CallToolRequest; |
| 18 | +import io.modelcontextprotocol.spec.McpSchema.CallToolResult; |
| 19 | +import io.modelcontextprotocol.spec.McpSchema.TextContent; |
| 20 | +import java.sql.Connection; |
| 21 | +import java.util.Collections; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.TestInstance; |
| 24 | +import org.junit.jupiter.api.TestInstance.Lifecycle; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.boot.test.context.TestConfiguration; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 29 | +import schemacrawler.schema.Catalog; |
| 30 | +import schemacrawler.tools.ai.mcpserver.server.ConnectionService; |
| 31 | +import schemacrawler.tools.ai.mcpserver.server.ToolHelper; |
| 32 | +import schemacrawler.tools.ai.mcpserver.utility.EmptyFactory; |
| 33 | +import schemacrawler.tools.ai.tools.FunctionDefinition; |
| 34 | +import schemacrawler.tools.ai.tools.FunctionExecutor; |
| 35 | +import schemacrawler.tools.ai.tools.FunctionReturn; |
| 36 | +import schemacrawler.tools.ai.tools.NoParameters; |
| 37 | +import schemacrawler.tools.ai.tools.TextFunctionReturn; |
| 38 | +import us.fatehi.utility.datasource.DatabaseConnectionSource; |
| 39 | +import us.fatehi.utility.property.PropertyName; |
| 40 | + |
| 41 | +@TestInstance(Lifecycle.PER_CLASS) |
| 42 | +@SpringJUnitConfig( |
| 43 | + classes = {ToolHelperTest.MockConfig.class, ToolHelper.class, ConnectionService.class}) |
| 44 | +public class ToolHelperTest { |
| 45 | + |
| 46 | + /** Trivial function definition whose executor returns a fixed text. */ |
| 47 | + public static class TrivialFunctionDefinition implements FunctionDefinition<NoParameters> { |
| 48 | + |
| 49 | + @Override |
| 50 | + public String getDescription() { |
| 51 | + return "A trivial function for testing ToolHelper"; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String getName() { |
| 56 | + return "trivial"; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Class<NoParameters> getParametersClass() { |
| 61 | + return NoParameters.class; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public FunctionExecutor<NoParameters> newExecutor() { |
| 66 | + return new FunctionExecutor<>() { |
| 67 | + private Connection connection; |
| 68 | + private Catalog catalog; |
| 69 | + |
| 70 | + @Override |
| 71 | + public FunctionReturn call() { |
| 72 | + return new TextFunctionReturn("ok"); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void configure(final NoParameters parameters) {} |
| 77 | + |
| 78 | + public Catalog getCatalog() { |
| 79 | + return catalog; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public PropertyName getCommandName() { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + public Connection getConnection() { |
| 88 | + return connection; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void initialize() {} |
| 93 | + |
| 94 | + public void setCatalog(final Catalog catalog) { |
| 95 | + this.catalog = catalog; |
| 96 | + } |
| 97 | + |
| 98 | + public void setConnection(final Connection connection) { |
| 99 | + this.connection = connection; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public boolean usesConnection() { |
| 104 | + return false; |
| 105 | + } |
| 106 | + }; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @TestConfiguration |
| 111 | + static class MockConfig { |
| 112 | + @Bean |
| 113 | + Catalog catalog() { |
| 114 | + return EmptyFactory.createEmptyCatalog(null); |
| 115 | + } |
| 116 | + |
| 117 | + @Bean |
| 118 | + DatabaseConnectionSource databaseConnectionSource() { |
| 119 | + return EmptyFactory.createEmptyDatabaseConnectionSource(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Autowired private ToolHelper toolHelper; |
| 124 | + |
| 125 | + @Autowired |
| 126 | + ToolHelperTest(final ConnectionService connectionService) {} |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testToolCallHandler() throws Exception { |
| 130 | + final TrivialFunctionDefinition functionDefinition = new TrivialFunctionDefinition(); |
| 131 | + final SyncToolSpecification syncToolSpecification = |
| 132 | + toolHelper.toSyncToolSpecification(functionDefinition); |
| 133 | + final CallToolRequest callToolRequest = new CallToolRequest("", Collections.emptyMap()); |
| 134 | + final CallToolResult callToolResult = |
| 135 | + syncToolSpecification.callHandler().apply(null, callToolRequest); |
| 136 | + assertThat(callToolResult, is(not(nullValue()))); |
| 137 | + final TextContent textContent = (TextContent) callToolResult.content().getFirst(); |
| 138 | + assertThat(textContent.text(), is("ok")); |
| 139 | + } |
| 140 | +} |
0 commit comments