-
Notifications
You must be signed in to change notification settings - Fork 24
Add some tests for MCP implementation and tools #2420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2a2cd23
Add integration tests for GeneralCommandTools
Hazel-Datastax 18a3d71
format
Hazel-Datastax 92735e5
add one more IT
Hazel-Datastax 8f21cf0
fix null in ToolResponse
Hazel-Datastax f9a9665
add unit test for McpResource
Hazel-Datastax ae39d46
Merge branch 'main' into hazel/mcp-tests
Hazel-Datastax 0c37738
update comment
Hazel-Datastax db790ce
update and fix tests
Hazel-Datastax e8f568c
update comments
Hazel-Datastax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src/test/java/io/stargate/sgv2/jsonapi/api/v1/mcp/GeneralCommandToolsMcpIntegrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| package io.stargate.sgv2.jsonapi.api.v1.mcp; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import io.quarkiverse.mcp.server.MetaKey; | ||
| import io.quarkus.test.common.WithTestResource; | ||
| import io.quarkus.test.junit.QuarkusIntegrationTest; | ||
| import io.stargate.sgv2.jsonapi.api.model.command.CommandName; | ||
| import io.stargate.sgv2.jsonapi.testresource.DseTestResource; | ||
| import io.vertx.core.json.JsonArray; | ||
| import io.vertx.core.json.JsonObject; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.*; | ||
|
|
||
| /** | ||
| * MCP integration tests for {@link GeneralCommandTools}. Uses the Streamable HTTP transport via | ||
| * McpAssured to test all general-level MCP tools end-to-end. | ||
| */ | ||
| @QuarkusIntegrationTest | ||
| @WithTestResource(value = DseTestResource.class) | ||
| class GeneralCommandToolsMcpIntegrationTest extends McpIntegrationTestBase { | ||
|
|
||
| private static final String EXTRA_KEYSPACE = "new_ks"; | ||
|
|
||
| @Nested | ||
| @TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
| class CreateFindAndDropKeyspaceToolCall { | ||
| @Test | ||
| @Order(1) | ||
| void testCreateKeyspaceToolCall() { | ||
| callToolAndAssert( | ||
| CommandName.Names.CREATE_KEYSPACE, | ||
| Map.of("name", EXTRA_KEYSPACE), | ||
| response -> { | ||
| assertFalse(response.isError()); | ||
| assertNotNull(response._meta()); | ||
| assertNull(response.structuredContent()); | ||
| assertThat(response.content()).isEmpty(); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(2) | ||
| void testFindKeyspaceToolCallAfterCreateKeyspace() { | ||
| callToolAndAssert( | ||
| CommandName.Names.FIND_KEYSPACES, | ||
| Map.of(), | ||
| response -> { | ||
| // check mcp response | ||
| assertFalse(response.isError()); | ||
| assertNotNull(response._meta()); | ||
| assertNull(response.structuredContent()); | ||
|
|
||
| // check the new keyspace is there | ||
| var status = (JsonObject) response._meta().get(MetaKey.of("status")); | ||
| assertNotNull(status, "Status should not be null"); | ||
| JsonArray keyspaces = status.getJsonArray("keyspaces"); | ||
| assertNotNull(keyspaces, "Keyspaces array should not be null"); | ||
| assertTrue( | ||
| keyspaces.contains(EXTRA_KEYSPACE), "New created Keyspace should be in the list"); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(3) | ||
| void testDropKeyspaceToolCall() { | ||
| callToolAndAssert( | ||
| CommandName.Names.DROP_KEYSPACE, | ||
| Map.of("name", EXTRA_KEYSPACE), | ||
| response -> { | ||
| assertFalse(response.isError()); | ||
| assertNotNull(response._meta()); | ||
| assertNull(response.structuredContent()); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(4) | ||
| void testFindKeyspaceToolCallAfterDropKeyspace() { | ||
| callToolAndAssert( | ||
| CommandName.Names.FIND_KEYSPACES, | ||
| Map.of(), | ||
| response -> { | ||
| // check mcp response | ||
| assertFalse(response.isError()); | ||
| assertNotNull(response._meta()); | ||
| assertNull(response.structuredContent()); | ||
|
|
||
| // check the new keyspace is dropped | ||
| var status = (JsonObject) response._meta().get(MetaKey.of("status")); | ||
| assertNotNull(status, "Status should not be null"); | ||
| JsonArray keyspaces = status.getJsonArray("keyspaces"); | ||
| assertNotNull(keyspaces, "Keyspaces array should not be null"); | ||
| assertFalse( | ||
| keyspaces.contains(EXTRA_KEYSPACE), | ||
| "Keyspace should be dropped and not in the list"); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void findEmbeddingProvidersToolCall() { | ||
| callToolAndAssert( | ||
| "findEmbeddingProviders", | ||
| Map.of(), | ||
| response -> { | ||
| assertFalse(response.isError()); | ||
| assertNotNull(response._meta()); | ||
| assertNull(response.structuredContent()); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void findRerankingProvidersToolCall() { | ||
| callToolAndAssert( | ||
| "findRerankingProviders", | ||
| Map.of(), | ||
| response -> { | ||
| assertFalse(response.isError()); | ||
| assertNotNull(response._meta()); | ||
| assertNull(response.structuredContent()); | ||
| }); | ||
| } | ||
| } |
126 changes: 126 additions & 0 deletions
126
src/test/java/io/stargate/sgv2/jsonapi/api/v1/mcp/McpIntegrationTestBase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package io.stargate.sgv2.jsonapi.api.v1.mcp; | ||
|
|
||
| import static io.stargate.sgv2.jsonapi.api.v1.util.IntegrationTestUtils.*; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
|
||
| import io.quarkiverse.mcp.server.ToolResponse; | ||
| import io.quarkiverse.mcp.server.test.McpAssured; | ||
| import io.quarkiverse.mcp.server.test.McpAssured.McpStreamableTestClient; | ||
| import io.stargate.sgv2.jsonapi.config.constants.HttpConstants; | ||
| import io.vertx.core.MultiMap; | ||
| import java.net.URI; | ||
| import java.util.Base64; | ||
| import java.util.Map; | ||
| import java.util.function.Consumer; | ||
| import org.apache.commons.lang3.RandomStringUtils; | ||
| import org.eclipse.microprofile.config.ConfigProvider; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.TestInstance; | ||
|
|
||
| /** | ||
| * Abstract base class for MCP integration tests. Provides a shared MCP client instance, | ||
| * authentication, and utility methods for invoking MCP tools via the Streamable HTTP transport. | ||
| */ | ||
| @TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
| public abstract class McpIntegrationTestBase { | ||
|
|
||
| private static final String MCP_PATH = "/v1/mcp"; | ||
|
|
||
| /** MCP Assured cannot automatically resolve the URI (like Rest Assured) */ | ||
| private static final String MCP_HOSTNAME = "http://localhost:"; | ||
|
|
||
| /** Test keyspace name, with a random suffix for isolation. */ | ||
| protected final String keyspaceName = | ||
| "mcp_ks_" + RandomStringUtils.insecure().nextAlphanumeric(8).toLowerCase(); | ||
|
|
||
| /** Test collection name, with a random suffix for isolation. */ | ||
| protected final String collectionName = | ||
| "mcp_col_" + RandomStringUtils.insecure().nextAlphanumeric(8).toLowerCase(); | ||
|
|
||
| /** Shared MCP client instance, connected once per test class. */ | ||
| protected McpStreamableTestClient mcpClient; | ||
|
|
||
| /** | ||
| * Initializes the shared MCP client before all tests in the class. Connects to the local test | ||
| * server using Streamable HTTP transport with authentication headers. | ||
| */ | ||
| @BeforeAll | ||
| void setUpMcpClient() { | ||
| mcpClient = | ||
| McpAssured.newStreamableClient() | ||
| .setBaseUri(URI.create(MCP_HOSTNAME + getTestPort())) | ||
| .setMcpPath(MCP_PATH) | ||
| .setAdditionalHeaders(msg -> authHeaders()) | ||
| .build() | ||
| .connect(); | ||
| } | ||
|
|
||
| /** Disconnects and releases the shared MCP client after all tests in the class have run. */ | ||
| @AfterAll | ||
| void tearDownMcpClient() { | ||
| if (mcpClient != null) { | ||
| mcpClient.disconnect(); | ||
| mcpClient = null; | ||
| } | ||
| } | ||
|
|
||
| protected int getTestPort() { | ||
| try { | ||
| return ConfigProvider.getConfig().getValue("quarkus.http.test-port", Integer.class); | ||
| } catch (Exception e) { | ||
| return Integer.parseInt(System.getProperty("quarkus.http.test-port")); | ||
| } | ||
| } | ||
|
|
||
| /** Build authentication headers matching the Token header format used by the REST API. */ | ||
| protected MultiMap authHeaders() { | ||
| String credential = | ||
| "Cassandra:" | ||
| + Base64.getEncoder().encodeToString(getCassandraUsername().getBytes()) | ||
| + ":" | ||
| + Base64.getEncoder().encodeToString(getCassandraPassword().getBytes()); | ||
| return MultiMap.caseInsensitiveMultiMap() | ||
| .add(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, credential); | ||
| } | ||
|
|
||
| /** Create a keyspace via the MCP createKeyspace tool. */ | ||
| protected void createKeyspace(String keyspace) { | ||
| callToolAndAssert( | ||
| "createKeyspace", Map.of("name", keyspace), response -> assertFalse(response.isError())); | ||
| } | ||
|
|
||
| /** Drop a keyspace via the MCP dropKeyspace tool. */ | ||
| protected void dropKeyspace(String keyspace) { | ||
| callToolAndAssert( | ||
| "dropKeyspace", Map.of("name", keyspace), response -> assertFalse(response.isError())); | ||
| } | ||
|
|
||
| /** Create a collection via the MCP createCollection tool. */ | ||
| protected void createCollection(String keyspace, String collection) { | ||
| callToolAndAssert( | ||
| "createCollection", | ||
| Map.of("keyspace", keyspace, "collection", collection), | ||
| response -> assertFalse(response.isError())); | ||
| } | ||
|
|
||
| /** Delete a collection via the MCP deleteCollection tool */ | ||
| protected void deleteCollection(String keyspace, String collection) { | ||
| callToolAndAssert( | ||
| "deleteCollection", | ||
| Map.of("keyspace", keyspace, "collection", collection), | ||
| response -> assertFalse(response.isError())); | ||
| } | ||
|
Comment on lines
+88
to
+113
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no usage for now, reserved for other tools IT |
||
|
|
||
| /** | ||
| * Execute an MCP tool call and assert the response using the shared client. | ||
| * | ||
| * @param toolName the MCP tool name to invoke | ||
| * @param args the tool arguments | ||
| * @param assertFn assertion function for the ToolResponse | ||
| */ | ||
| protected void callToolAndAssert( | ||
| String toolName, Map<String, Object> args, Consumer<ToolResponse> assertFn) { | ||
| mcpClient.when().toolsCall(toolName, args, assertFn).thenAssertResults(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replace null with empty list/map