You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/07/22 16:13:56 UTC
(shardingsphere) branch master updated: Simplify runtime boundaries and harden E2E validation (#39215)
This is an automated email from the ASF dual-hosted git repository.
terrymanu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 5d0733a527c Simplify runtime boundaries and harden E2E validation (#39215)
5d0733a527c is described below
commit 5d0733a527c01f995a6060439a7c684672984fa7
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Thu Jul 23 00:13:49 2026 +0800
Simplify runtime boundaries and harden E2E validation (#39215)
* Simplify runtime boundaries and harden E2E validation
- remove dead compatibility layers and consolidate URI and transport ownership
- strengthen schema, configuration, workflow, and error-handling boundaries
- make MCP E2E metrics, runtime coverage, and CI failure signals trustworthy
* Simplify runtime boundaries and harden E2E validation
- remove dead compatibility layers and consolidate URI and transport ownership
- strengthen schema, configuration, workflow, and error-handling boundaries
- make MCP E2E metrics, runtime coverage, and CI failure signals trustworthy
---
.github/workflows/ci-mcp.yml | 2 +
.github/workflows/e2e-mcp.yml | 18 ++-
docs/document/content/dev-manual/mcp.cn.md | 2 +-
docs/document/content/dev-manual/mcp.en.md | 2 +-
.../YamlHttpTransportConfigurationSwapper.java | 4 +-
.../swapper/YamlMCPLaunchConfigurationSwapper.java | 4 +-
.../YamlRuntimeDatabaseConfigurationSwapper.java | 4 +-
.../prompt/MCPPromptSpecificationFactory.java | 6 +-
.../tool/MCPToolSpecificationFactory.java | 17 +--
.../server/http/StreamableHttpMCPServlet.java | 8 +-
.../ServerTransportSecurityValidatorFactory.java | 14 +-
...dingSphereServerTransportSecurityValidator.java | 33 ++---
.../constraint/OriginHeaderConstraint.java | 14 +-
.../ProtocolVersionHeaderConstraint.java | 15 +--
.../SessionRequiredTransportHeaderConstraint.java | 24 ----
.../constraint/TransportHeaderConstraint.java | 41 ------
.../instructions/server-instructions.md | 2 +-
.../config/MCPLaunchConfigurationTest.java | 4 +-
.../tool/MCPToolSpecificationFactoryTest.java | 22 ----
.../transport/server/MCPSyncServerFactoryTest.java | 10 ++
...erverTransportSecurityValidatorFactoryTest.java | 7 +
...SphereServerTransportSecurityValidatorTest.java | 38 +++---
.../mcp/core/completion/MCPCompletionService.java | 40 +++---
.../handler/ResourceDefinitionRegistry.java | 9 --
.../mcp/core/resource/uri/MCPUriPattern.java | 123 +-----------------
.../mcp/core/tool/MCPToolCallLimiter.java | 8 +-
.../core/tool/handler/ToolDefinitionRegistry.java | 9 --
.../tool/handler/execute/ClassificationResult.java | 13 --
.../handler/execute/MCPSQLExecutionFacade.java | 78 ++---------
.../execute/trace/SQLExecutionTraceFactory.java | 57 --------
.../execute/trace/SQLExecutionTraceRecord.java | 41 ------
.../mcp/core/tool/request/MCPToolArguments.java | 26 ----
.../handler/ResourceDefinitionRegistryTest.java | 12 +-
.../mcp/core/resource/uri/MCPUriPatternTest.java | 12 +-
.../mcp/core/tool/MCPToolCallLimiterTest.java | 10 +-
.../mcp/core/tool/MCPToolControllerTest.java | 10 +-
.../tool/handler/ToolDefinitionRegistryTest.java | 25 ++--
.../handler/execute/MCPSQLExecutionFacadeTest.java | 143 ++++++++-------------
.../trace/SQLExecutionTraceFactoryTest.java | 43 -------
.../core/tool/request/MCPToolArgumentsTest.java | 15 ---
mcp/features/broadcast/pom.xml | 5 -
.../tool/handler/PlanEncryptRuleToolHandler.java | 16 +--
.../service/EncryptWorkflowPlanningService.java | 21 +--
.../tool/service/MaskWorkflowPlanningService.java | 21 +--
.../service/MaskWorkflowValidationService.java | 11 +-
mcp/features/readwrite-splitting/pom.xml | 5 -
...riteSplittingRuleWorkflowValidationService.java | 11 +-
...teSplittingStatusWorkflowValidationService.java | 14 +-
.../service/ShadowWorkflowValidationService.java | 11 +-
.../service/ShardingWorkflowValidationService.java | 11 +-
.../descriptor/MCPDescriptorCatalogIndex.java | 29 +++--
.../MCPDescriptorCatalogYamlSwapper.java | 4 +-
.../MCPResourceNavigationPayloadBuilder.java | 6 +-
.../MCPToolDescriptorCatalogValidator.java | 4 +
.../mcp/support/resource/MCPUriTemplate.java | 130 +++++++++++++++++--
.../support/security/MCPClientSafetyPolicy.java | 13 --
.../security/MCPRuntimeProtectionPolicy.java | 23 +++-
.../yaml/MCPYamlConfigurationValidator.java | 39 ------
.../descriptor/MCPDescriptorCatalogIndexTest.java | 6 +
.../MCPDescriptorComponentValidatorsTest.java | 33 +++++
.../mcp/support/resource/MCPUriTemplateTest.java | 24 ++++
.../security/MCPClientSafetyPolicyTest.java | 38 +-----
.../security/MCPRuntimeProtectionPolicyTest.java | 36 +++++-
.../yaml/MCPYamlConfigurationValidatorTest.java | 60 ---------
.../mcp-descriptor-test-planning.yaml | 21 +++
test/e2e/mcp/pom.xml | 29 ++++-
.../test/e2e/mcp/env/MCPE2ETestConfiguration.java | 12 +-
.../e2e/mcp/env/MCPE2ETestConfigurationTest.java | 12 ++
.../e2e/mcp/llm/config/LLME2EConfiguration.java | 10 +-
.../mcp/llm/config/LLME2EConfigurationTest.java | 11 +-
.../AbstractLLMMCPConversationRunnerTest.java | 14 +-
.../conversation/LLMMCPConversationArtifacts.java | 12 ++
.../llm/conversation/LLMMCPConversationRunner.java | 41 +++---
.../LLMMCPConversationRunnerCoverageTest.java | 39 +++---
.../LLMMCPConversationRunnerFailureTest.java | 34 ++---
.../LLMMCPConversationRunnerNextActionTest.java | 76 +++++------
.../conversation/LLMMCPConversationRunnerTest.java | 47 ++++---
.../LLMMCPConversationTurnPlanner.java | 16 +--
.../LLMMCPConversationTurnPlannerTest.java | 13 +-
.../conversation/LLMMCPInteractionCoverage.java | 6 +-
.../LLMMCPInteractionCoverageTest.java | 4 +-
.../llm/conversation/LLMMCPTraceRecordFactory.java | 4 +-
.../mcp/llm/suite/MCPBuilderEvaluationCatalog.java | 13 +-
.../evaluation/MCPBuilderEvaluationE2ETest.java | 7 +-
.../e2e/mcp/llm/suite/smoke/LLMSmokeE2ETest.java | 7 +-
.../suite/usability/LLMUsabilitySuiteE2ETest.java | 14 +-
.../suite/usability/LLMUsabilitySuiteRunner.java | 19 ++-
.../assessment/LLMUsabilityMetricCalculator.java | 86 +++++++------
.../LLMUsabilityMetricCalculatorTest.java | 29 ++++-
.../assessment/LLMUsabilityReportWriter.java | 17 ++-
.../assessment/LLMUsabilityReportWriterTest.java | 17 ++-
.../assessment/LLMUsabilityScenarioResult.java | 2 -
.../assessment/LLMUsabilityScorecard.java | 10 +-
.../usability/scenario/LLMUsabilityScenario.java | 9 --
.../AbstractProductionMySQLRuntimeE2ETest.java | 14 +-
...AbstractProductionPostgreSQLRuntimeE2ETest.java | 10 +-
.../AbstractProductionProxyWorkflowE2ETest.java | 6 +-
.../HttpProductionProxyEncryptWorkflowE2ETest.java | 7 +-
...ductionProxyFeatureWorkflowContractE2ETest.java | 7 +-
.../HttpProductionProxyMaskWorkflowE2ETest.java | 7 +-
...ductionProxySecretReferenceWorkflowE2ETest.java | 7 +-
.../production/PackagedDistributionE2ETest.java | 77 +++++++++--
.../ProductionMySQLReadOnlySQLRuntimeE2ETest.java | 2 +-
.../production/ProductionMySQLRuntimeE2ETest.java | 2 +-
.../ProductionMySQLSQLRuntimeE2ETest.java | 2 +-
.../ProductionPostgreSQLRuntimeE2ETest.java | 2 +-
.../ProductionRuntimeTransportSmokeE2ETest.java | 2 +-
.../AbstractHttpProgrammaticRuntimeE2ETest.java | 20 ++-
.../programmatic/ExecuteQueryReadOnlyE2ETest.java | 7 +-
.../ExecuteQueryTransactionE2ETest.java | 7 +-
.../FeatureWorkflowContractE2ETest.java | 7 +-
.../HttpTransportApprovalSafetyE2ETest.java | 7 +-
.../HttpTransportBaselineContractE2ETest.java | 9 +-
.../HttpTransportCompletionE2ETest.java | 22 +---
.../programmatic/HttpTransportContractE2ETest.java | 20 +--
.../HttpTransportProtocolContractE2ETest.java | 7 +-
.../programmatic/HttpTransportRecoveryE2ETest.java | 20 +--
.../HttpTransportSessionLifecycleE2ETest.java | 2 +-
.../programmatic/MetadataDiscoveryE2ETest.java | 7 +-
.../distribution/DockerImageHttpRuntime.java | 12 +-
120 files changed, 1042 insertions(+), 1417 deletions(-)
diff --git a/.github/workflows/ci-mcp.yml b/.github/workflows/ci-mcp.yml
index d310dc72009..b12a01e3fae 100644
--- a/.github/workflows/ci-mcp.yml
+++ b/.github/workflows/ci-mcp.yml
@@ -22,7 +22,9 @@ on:
branches: [ master ]
paths:
- '.github/workflows/ci-mcp.yml'
+ - '.github/workflows/resources/actions/setup-build-environment/**'
- 'pom.xml'
+ - 'database/connector/**'
- 'mcp/**'
workflow_dispatch:
diff --git a/.github/workflows/e2e-mcp.yml b/.github/workflows/e2e-mcp.yml
index 8e1cf427d00..03985a27518 100644
--- a/.github/workflows/e2e-mcp.yml
+++ b/.github/workflows/e2e-mcp.yml
@@ -27,10 +27,15 @@ on:
- ready_for_review
paths:
- '.github/workflows/e2e-mcp.yml'
+ - '.github/workflows/resources/actions/download-e2e-artifacts/**'
+ - '.github/workflows/resources/actions/setup-build-environment/**'
+ - '.github/workflows/resources/actions/upload-e2e-artifacts/**'
- 'pom.xml'
+ - 'database/connector/**'
- 'distribution/mcp/**'
- 'mcp/**'
- 'test/e2e/pom.xml'
+ - 'test/e2e/env/**'
- 'test/e2e/mcp/**'
schedule:
- cron: '0 19 * * 1-5'
@@ -227,14 +232,24 @@ jobs:
curl --silent --output /dev/null --request POST http://127.0.0.1:18088/mcp
results_root="${RUNNER_TEMP}/mcp-conformance-results"
mkdir -p "${results_root}"
+ scenario_count=0
while IFS= read -r scenario; do
[[ -z "${scenario}" || "${scenario}" == \#* ]] && continue
+ scenario_count=$((scenario_count + 1))
node .mcp-conformance/dist/index.js server \
--url http://127.0.0.1:18088/mcp \
--scenario "${scenario}" \
--spec-version 2025-11-25 \
--output-dir "${results_root}/${scenario}"
done < test/e2e/mcp/src/test/resources/conformance/server-scenarios.txt
+ if [[ "${scenario_count}" -eq 0 ]]; then
+ echo "No MCP conformance scenarios were selected." >&2
+ exit 1
+ fi
+ if ! find "${results_root}" -mindepth 1 -type f -print -quit | grep -q .; then
+ echo "MCP conformance produced no result files." >&2
+ exit 1
+ fi
- name: Upload MCP Conformance Artifacts
if: always()
uses: actions/upload-artifact@v6
@@ -283,7 +298,7 @@ jobs:
with:
artifact-name: mcp-e2e-build-artifacts
artifact-dir: ${{ runner.temp }}/mcp-e2e-build-artifacts
- docker-image-archives: mcp-distribution-image.tar
+ load-docker-image: 'false'
- name: Download MCP LLM Runtime Image
uses: ./.github/workflows/resources/actions/download-e2e-artifacts
with:
@@ -307,7 +322,6 @@ jobs:
-Dgroups=llm-e2e \
-Dsurefire.failIfNoSpecifiedTests=true \
-De2e.run.type=DOCKER \
- -Dmcp.e2e.container.image="${{ env.MCP_DISTRIBUTION_IMAGE }}" \
-Dmcp.llm.ready-timeout-seconds=900 \
-Dmcp.llm.request-timeout-seconds=300 \
-Dmcp.llm.artifact-root="${{ github.workspace }}/test/e2e/mcp/target/llm-e2e" \
diff --git a/docs/document/content/dev-manual/mcp.cn.md b/docs/document/content/dev-manual/mcp.cn.md
index 7315e22ea45..13e90180641 100644
--- a/docs/document/content/dev-manual/mcp.cn.md
+++ b/docs/document/content/dev-manual/mcp.cn.md
@@ -20,7 +20,7 @@ MCP 子链路按 `api + support + features + core + bootstrap` 分层组织:
- `mcp/features/readwrite-splitting`:Readwrite-Splitting MCP feature。
- `mcp/features/shadow`:Shadow MCP feature。
- `mcp/features/sharding`:Sharding MCP feature。
-- `mcp/core`:handler 发现、registry、request context、session、SQL execution trace、metadata discovery 和 runtime context。
+- `mcp/core`:handler 发现、registry、request context、session、metadata discovery 和 runtime context。
- `mcp/bootstrap`:基于 MCP Java SDK 的 bootstrap、HTTP/STDIO transport、配置加载和生命周期管理。
- `mcp/registry`:隔离的发行元数据校验工具,不依赖 MCP runtime 模块。
- `distribution/mcp`:独立打包、启动脚本、配置和 Dockerfile。
diff --git a/docs/document/content/dev-manual/mcp.en.md b/docs/document/content/dev-manual/mcp.en.md
index 8fab77d6d84..5d77f3a0bdf 100644
--- a/docs/document/content/dev-manual/mcp.en.md
+++ b/docs/document/content/dev-manual/mcp.en.md
@@ -20,7 +20,7 @@ The MCP path is organized as `api + support + features + core + bootstrap`:
- `mcp/features/readwrite-splitting`: Readwrite-Splitting MCP feature.
- `mcp/features/shadow`: Shadow MCP feature.
- `mcp/features/sharding`: Sharding MCP feature.
-- `mcp/core`: handler discovery, registry, request context, session, SQL execution trace, metadata discovery, and runtime context.
+- `mcp/core`: handler discovery, registry, request context, session, metadata discovery, and runtime context.
- `mcp/bootstrap`: MCP Java SDK based bootstrap, HTTP/STDIO transport, configuration loading, and lifecycle management.
- `mcp/registry`: isolated distribution metadata validation tooling; it does not depend on MCP runtime modules.
- `distribution/mcp`: standalone packaging, startup scripts, configuration, and Dockerfile.
diff --git a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/config/yaml/swapper/YamlHttpTransportConfigurationSwapper.java b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/config/yaml/swapper/YamlHttpTransportConfigurationSwapper.java
index 0cd6653b5ad..dcb20ab45c1 100644
--- a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/config/yaml/swapper/YamlHttpTransportConfigurationSwapper.java
+++ b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/config/yaml/swapper/YamlHttpTransportConfigurationSwapper.java
@@ -22,7 +22,7 @@ import org.apache.shardingsphere.infra.util.yaml.swapper.YamlConfigurationSwappe
import org.apache.shardingsphere.mcp.bootstrap.config.HttpTransportConfiguration;
import org.apache.shardingsphere.mcp.bootstrap.config.yaml.config.YamlHttpTransportConfiguration;
import org.apache.shardingsphere.mcp.bootstrap.config.yaml.config.YamlSessionAttributionSourceConfiguration;
-import org.apache.shardingsphere.mcp.support.yaml.MCPYamlConfigurationValidator;
+import org.apache.shardingsphere.mcp.support.configuration.MCPConfigurationValidator;
import java.util.Objects;
/**
@@ -68,7 +68,7 @@ public final class YamlHttpTransportConfigurationSwapper implements YamlConfigur
if (null == yamlConfig) {
return new HttpTransportConfiguration(DEFAULT_BIND_HOST, DEFAULT_PORT, DEFAULT_ENDPOINT_PATH);
}
- MCPYamlConfigurationValidator.validate(yamlConfig, "MCP HTTP transport configuration");
+ MCPConfigurationValidator.validate(yamlConfig, "MCP HTTP transport configuration");
return new HttpTransportConfiguration(getValueOrDefault(yamlConfig.getBindHost(), DEFAULT_BIND_HOST), null == yamlConfig.getPort() ? DEFAULT_PORT : yamlConfig.getPort(),
getValueOrDefault(yamlConfig.getEndpointPath(), DEFAULT_ENDPOINT_PATH), swapToObject(yamlConfig.getSessionAttributionSource()));
}
diff --git a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/config/yaml/swapper/YamlMCPLaunchConfigurationSwapper.java b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/config/yaml/swapper/YamlMCPLaunchConfigurationSwapper.java
index 3ab884ba745..9482a214b7b 100644
--- a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/config/yaml/swapper/YamlMCPLaunchConfigurationSwapper.java
+++ b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/config/yaml/swapper/YamlMCPLaunchConfigurationSwapper.java
@@ -22,7 +22,7 @@ import org.apache.shardingsphere.mcp.bootstrap.config.MCPLaunchConfiguration;
import org.apache.shardingsphere.mcp.api.transport.MCPTransportType;
import org.apache.shardingsphere.mcp.bootstrap.config.yaml.config.YamlMCPLaunchConfiguration;
import org.apache.shardingsphere.mcp.bootstrap.config.yaml.config.YamlMCPTransportConfiguration;
-import org.apache.shardingsphere.mcp.support.yaml.MCPYamlConfigurationValidator;
+import org.apache.shardingsphere.mcp.support.configuration.MCPConfigurationValidator;
/**
* YAML MCP launch configuration swapper.
@@ -43,7 +43,7 @@ public final class YamlMCPLaunchConfigurationSwapper implements YamlConfiguratio
@Override
public MCPLaunchConfiguration swapToObject(final YamlMCPLaunchConfiguration yamlConfig) {
- MCPYamlConfigurationValidator.validate(yamlConfig, "MCP launch configuration");
+ MCPConfigurationValidator.validate(yamlConfig, "MCP launch configuration");
YamlMCPTransportConfiguration yamlTransportConfig = yamlConfig.getTransport();
return new MCPLaunchConfiguration(yamlTransportConfig.getType(), httpTransportConfigSwapper.swapToObject(yamlTransportConfig.getHttp()),
runtimeDatabasesSwapper.swapToObject(yamlConfig.getRuntimeDatabases()));
diff --git a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/config/yaml/swapper/YamlRuntimeDatabaseConfigurationSwapper.java b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/config/yaml/swapper/YamlRuntimeDatabaseConfigurationSwapper.java
index d81486f2b06..ad929530f99 100644
--- a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/config/yaml/swapper/YamlRuntimeDatabaseConfigurationSwapper.java
+++ b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/config/yaml/swapper/YamlRuntimeDatabaseConfigurationSwapper.java
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.mcp.bootstrap.config.yaml.swapper;
import org.apache.shardingsphere.infra.util.yaml.swapper.YamlConfigurationSwapper;
import org.apache.shardingsphere.mcp.bootstrap.config.yaml.config.YamlRuntimeDatabaseConfiguration;
import org.apache.shardingsphere.mcp.support.database.metadata.jdbc.RuntimeDatabaseConfiguration;
-import org.apache.shardingsphere.mcp.support.yaml.MCPYamlConfigurationValidator;
+import org.apache.shardingsphere.mcp.support.configuration.MCPConfigurationValidator;
/**
* YAML runtime database configuration swapper.
@@ -39,7 +39,7 @@ public final class YamlRuntimeDatabaseConfigurationSwapper implements YamlConfig
@Override
public RuntimeDatabaseConfiguration swapToObject(final YamlRuntimeDatabaseConfiguration yamlConfig) {
- MCPYamlConfigurationValidator.validate(yamlConfig, "MCP runtime database configuration");
+ MCPConfigurationValidator.validate(yamlConfig, "MCP runtime database configuration");
return new RuntimeDatabaseConfiguration(yamlConfig.getJdbcUrl(), yamlConfig.getUsername(), null == yamlConfig.getPassword() ? "" : yamlConfig.getPassword(), yamlConfig.getDriverClassName());
}
}
diff --git a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/prompt/MCPPromptSpecificationFactory.java b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/prompt/MCPPromptSpecificationFactory.java
index 4b469c1e646..0bd5858de6f 100644
--- a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/prompt/MCPPromptSpecificationFactory.java
+++ b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/prompt/MCPPromptSpecificationFactory.java
@@ -38,11 +38,7 @@ import java.util.Optional;
*/
public final class MCPPromptSpecificationFactory {
- private final Collection<MCPPromptDescriptor> promptDescriptors;
-
- public MCPPromptSpecificationFactory() {
- promptDescriptors = MCPDescriptorCatalogIndex.getPromptDescriptors();
- }
+ private final Collection<MCPPromptDescriptor> promptDescriptors = MCPDescriptorCatalogIndex.getPromptDescriptors();
/**
* Create MCP prompt specifications.
diff --git a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPToolSpecificationFactory.java b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPToolSpecificationFactory.java
index 8ad6836e634..9272a116623 100644
--- a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPToolSpecificationFactory.java
+++ b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPToolSpecificationFactory.java
@@ -28,13 +28,11 @@ import org.apache.shardingsphere.mcp.api.capability.tool.MCPToolDescriptor;
import org.apache.shardingsphere.mcp.bootstrap.transport.MCPTransportErrorFactory;
import org.apache.shardingsphere.mcp.core.context.MCPRuntimeContext;
import org.apache.shardingsphere.mcp.core.protocol.error.MCPErrorConverter;
-import org.apache.shardingsphere.mcp.core.protocol.exception.UnsupportedToolException;
import org.apache.shardingsphere.mcp.core.tool.MCPToolController;
import org.apache.shardingsphere.mcp.core.tool.handler.MCPToolDefinition;
import org.apache.shardingsphere.mcp.core.tool.handler.ToolDefinitionRegistry;
import org.apache.shardingsphere.mcp.support.database.metadata.jdbc.RuntimeDatabaseConnectionException;
-import java.sql.SQLException;
import java.time.Clock;
import java.util.Collections;
import java.util.List;
@@ -90,7 +88,7 @@ public final class MCPToolSpecificationFactory {
String type = String.valueOf(inputSchema.get("type"));
Map<String, Object> props = (Map<String, Object>) inputSchema.get("properties");
List<String> required = (List<String>) inputSchema.get("required");
- boolean additionalProps = Boolean.TRUE.equals(inputSchema.get("additionalProperties"));
+ Boolean additionalProps = (Boolean) inputSchema.get("additionalProperties");
return new McpSchema.JsonSchema(type, props, required, additionalProps, Collections.emptyMap(), Collections.emptyMap());
}
@@ -100,22 +98,15 @@ public final class MCPToolSpecificationFactory {
MCPToolDefinition definition = ToolDefinitionRegistry.getToolDefinition(request.name());
MCPSuccessPayload payload = controller.handle(exchange.sessionId(), definition, arguments);
return callToolResultFactory.create(definition.getDescriptor(), getEffectivePayload(exchange, payload, definition, arguments));
- } catch (final UnsupportedToolException ignored) {
- throw MCPTransportErrorFactory.createError(new UnsupportedToolException(request.name()));
+ } catch (final ShardingSphereMCPException | RuntimeDatabaseConnectionException ex) {
+ return callToolResultFactory.create(MCPErrorConverter.convert(ex));
// CHECKSTYLE:OFF
- } catch (final Exception ex) {
+ } catch (final RuntimeException ex) {
// CHECKSTYLE:ON
- if (isToolExecutionError(ex)) {
- return callToolResultFactory.create(MCPErrorConverter.convert(ex));
- }
throw MCPTransportErrorFactory.createError(ex);
}
}
- private boolean isToolExecutionError(final Exception cause) {
- return cause instanceof ShardingSphereMCPException || cause instanceof RuntimeDatabaseConnectionException || cause instanceof SQLException;
- }
-
private MCPSuccessPayload getEffectivePayload(final McpSyncServerExchange exchange, final MCPSuccessPayload successPayload, final MCPToolDefinition definition,
final Map<String, Object> arguments) {
Map<String, Object> payload = successPayload.toPayload();
diff --git a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/StreamableHttpMCPServlet.java b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/StreamableHttpMCPServlet.java
index a3b8229baa5..6a432bdd9f8 100644
--- a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/StreamableHttpMCPServlet.java
+++ b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/StreamableHttpMCPServlet.java
@@ -84,7 +84,7 @@ final class StreamableHttpMCPServlet extends HttpServlet implements McpStreamabl
StreamableHttpMCPServlet(final MCPSessionManager sessionManager, final McpJsonMapper jsonMapper, final HttpTransportConfiguration config) {
sessionAttributionResolver = new SessionAttributionResolver(config.getSessionAttributionSource());
securityValidator = ServerTransportSecurityValidatorFactory.create(sessionManager, config.getBindHost(), sessionAttributionResolver);
- delegate = createDelegate(jsonMapper, config.getEndpointPath(), securityValidator);
+ delegate = createDelegate(jsonMapper, config.getEndpointPath());
this.jsonMapper = jsonMapper;
this.sessionManager = sessionManager;
sessionExecutionCoordinator = new MCPSessionExecutionCoordinator(sessionManager);
@@ -92,10 +92,8 @@ final class StreamableHttpMCPServlet extends HttpServlet implements McpStreamabl
lifecycleLock = new StampedLock();
}
- private static HttpServletStreamableServerTransportProvider createDelegate(final McpJsonMapper jsonMapper, final String endpointPath,
- final ServerTransportSecurityValidator securityValidator) {
- return HttpServletStreamableServerTransportProvider.builder().jsonMapper(jsonMapper).mcpEndpoint(endpointPath)
- .securityValidator(securityValidator).build();
+ private static HttpServletStreamableServerTransportProvider createDelegate(final McpJsonMapper jsonMapper, final String endpointPath) {
+ return HttpServletStreamableServerTransportProvider.builder().jsonMapper(jsonMapper).mcpEndpoint(endpointPath).build();
}
@Override
diff --git a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/ServerTransportSecurityValidatorFactory.java b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/ServerTransportSecurityValidatorFactory.java
index 8677ad80357..b667e723196 100644
--- a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/ServerTransportSecurityValidatorFactory.java
+++ b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/ServerTransportSecurityValidatorFactory.java
@@ -24,12 +24,8 @@ import org.apache.shardingsphere.mcp.bootstrap.transport.HttpTransportHostUtils;
import org.apache.shardingsphere.mcp.bootstrap.transport.server.http.SessionAttributionResolver;
import org.apache.shardingsphere.mcp.bootstrap.transport.server.http.validator.constraint.OriginHeaderConstraint;
import org.apache.shardingsphere.mcp.bootstrap.transport.server.http.validator.constraint.ProtocolVersionHeaderConstraint;
-import org.apache.shardingsphere.mcp.bootstrap.transport.server.http.validator.constraint.TransportHeaderConstraint;
import org.apache.shardingsphere.mcp.core.session.MCPSessionManager;
-import java.util.LinkedList;
-import java.util.List;
-
/**
* Server transport security validator factory.
*/
@@ -45,13 +41,7 @@ public final class ServerTransportSecurityValidatorFactory {
* @return transport security validator
*/
public static ServerTransportSecurityValidator create(final MCPSessionManager sessionManager, final String bindHost, final SessionAttributionResolver sessionAttributionResolver) {
- return new ShardingSphereServerTransportSecurityValidator(sessionManager, createConstraints(bindHost), sessionAttributionResolver);
- }
-
- private static List<TransportHeaderConstraint> createConstraints(final String bindHost) {
- List<TransportHeaderConstraint> result = new LinkedList<>();
- result.add(new OriginHeaderConstraint(HttpTransportHostUtils.isLoopbackHost(bindHost)));
- result.add(new ProtocolVersionHeaderConstraint());
- return result;
+ return new ShardingSphereServerTransportSecurityValidator(sessionManager, new OriginHeaderConstraint(HttpTransportHostUtils.isLoopbackHost(bindHost)),
+ new ProtocolVersionHeaderConstraint(), sessionAttributionResolver);
}
}
diff --git a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/ShardingSphereServerTransportSecurityValidator.java b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/ShardingSphereServerTransportSecurityValidator.java
index cc4f6309d6c..6e5f89f1d8c 100644
--- a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/ShardingSphereServerTransportSecurityValidator.java
+++ b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/ShardingSphereServerTransportSecurityValidator.java
@@ -23,8 +23,8 @@ import io.modelcontextprotocol.spec.HttpHeaders;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mcp.api.session.MCPSessionIdentity;
import org.apache.shardingsphere.mcp.bootstrap.transport.server.http.SessionAttributionResolver;
-import org.apache.shardingsphere.mcp.bootstrap.transport.server.http.validator.constraint.SessionRequiredTransportHeaderConstraint;
-import org.apache.shardingsphere.mcp.bootstrap.transport.server.http.validator.constraint.TransportHeaderConstraint;
+import org.apache.shardingsphere.mcp.bootstrap.transport.server.http.validator.constraint.OriginHeaderConstraint;
+import org.apache.shardingsphere.mcp.bootstrap.transport.server.http.validator.constraint.ProtocolVersionHeaderConstraint;
import org.apache.shardingsphere.mcp.core.session.MCPSessionManager;
import java.util.List;
@@ -40,32 +40,25 @@ public final class ShardingSphereServerTransportSecurityValidator implements Ser
private final MCPSessionManager sessionManager;
- private final List<TransportHeaderConstraint> constraints;
+ private final OriginHeaderConstraint originHeaderConstraint;
+
+ private final ProtocolVersionHeaderConstraint protocolVersionHeaderConstraint;
private final SessionAttributionResolver sessionAttributionResolver;
@Override
public void validateHeaders(final Map<String, List<String>> headers) throws ServerTransportSecurityException {
- validateConstraints(headers, false);
- validateSessionIdentity(headers);
- validateConstraints(headers, true);
- }
-
- private void validateConstraints(final Map<String, List<String>> headers, final boolean sessionRequired) throws ServerTransportSecurityException {
- for (TransportHeaderConstraint each : constraints) {
- if ((each instanceof SessionRequiredTransportHeaderConstraint) != sessionRequired) {
- continue;
- }
- if (sessionRequired && getSessionId(headers).isBlank()) {
- continue;
- }
- each.validate(getFirstHeaderValue(headers, each.getConstraintKey()));
+ originHeaderConstraint.validate(getFirstHeaderValue(headers, "Origin"));
+ String sessionId = getSessionId(headers);
+ if (sessionId.isBlank() || !sessionManager.hasSession(sessionId)) {
+ return;
}
+ validateSessionIdentity(headers, sessionId);
+ protocolVersionHeaderConstraint.validate(getFirstHeaderValue(headers, HttpHeaders.PROTOCOL_VERSION));
}
- private void validateSessionIdentity(final Map<String, List<String>> headers) throws ServerTransportSecurityException {
- String sessionId = getSessionId(headers);
- if (sessionId.isBlank() || !sessionAttributionResolver.isEnabled()) {
+ private void validateSessionIdentity(final Map<String, List<String>> headers, final String sessionId) throws ServerTransportSecurityException {
+ if (!sessionAttributionResolver.isEnabled()) {
return;
}
Optional<MCPSessionIdentity> boundSessionIdentity = sessionManager.findSessionIdentity(sessionId);
diff --git a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/constraint/OriginHeaderConstraint.java b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/constraint/OriginHeaderConstraint.java
index a912320aafc..87a3cb04972 100644
--- a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/constraint/OriginHeaderConstraint.java
+++ b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/constraint/OriginHeaderConstraint.java
@@ -32,18 +32,18 @@ import java.net.URI;
*/
@RequiredArgsConstructor
@Slf4j
-public final class OriginHeaderConstraint implements TransportHeaderConstraint {
+public final class OriginHeaderConstraint {
private static final String FORBIDDEN_MESSAGE = "Origin is not allowed by MCP HTTP transport policy.";
private final boolean loopbackBinding;
- @Override
- public String getConstraintKey() {
- return "Origin";
- }
-
- @Override
+ /**
+ * Validate the Origin header value.
+ *
+ * @param value Origin header value
+ * @throws ServerTransportSecurityException when the origin is not allowed
+ */
public void validate(final String value) throws ServerTransportSecurityException {
if (value.isEmpty()) {
return;
diff --git a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/constraint/ProtocolVersionHeaderConstraint.java b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/constraint/ProtocolVersionHeaderConstraint.java
index 6c263f8f0e9..9cdb1978dea 100644
--- a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/constraint/ProtocolVersionHeaderConstraint.java
+++ b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/constraint/ProtocolVersionHeaderConstraint.java
@@ -18,21 +18,20 @@
package org.apache.shardingsphere.mcp.bootstrap.transport.server.http.validator.constraint;
import io.modelcontextprotocol.server.transport.ServerTransportSecurityException;
-import io.modelcontextprotocol.spec.HttpHeaders;
import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.mcp.bootstrap.transport.MCPTransportConstants;
/**
* Protocol version header constraint.
*/
-public final class ProtocolVersionHeaderConstraint implements SessionRequiredTransportHeaderConstraint {
+public final class ProtocolVersionHeaderConstraint {
- @Override
- public String getConstraintKey() {
- return HttpHeaders.PROTOCOL_VERSION;
- }
-
- @Override
+ /**
+ * Validate the MCP protocol version header value.
+ *
+ * @param value protocol version header value
+ * @throws ServerTransportSecurityException when the protocol version is missing or unsupported
+ */
public void validate(final String value) throws ServerTransportSecurityException {
ShardingSpherePreconditions.checkNotEmpty(value, () -> new ServerTransportSecurityException(400, "MCP-Protocol-Version header is required."));
ShardingSpherePreconditions.checkState(MCPTransportConstants.SUPPORTED_PROTOCOL_VERSIONS.contains(value), () -> createUnsupportedProtocolVersionException(value));
diff --git a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/constraint/SessionRequiredTransportHeaderConstraint.java b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/constraint/SessionRequiredTransportHeaderConstraint.java
deleted file mode 100644
index 75582d6168b..00000000000
--- a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/constraint/SessionRequiredTransportHeaderConstraint.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.mcp.bootstrap.transport.server.http.validator.constraint;
-
-/**
- * Session required transport header constraint.
- */
-public interface SessionRequiredTransportHeaderConstraint extends TransportHeaderConstraint {
-}
diff --git a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/constraint/TransportHeaderConstraint.java b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/constraint/TransportHeaderConstraint.java
deleted file mode 100644
index 309e05f8ae3..00000000000
--- a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/constraint/TransportHeaderConstraint.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.mcp.bootstrap.transport.server.http.validator.constraint;
-
-import io.modelcontextprotocol.server.transport.ServerTransportSecurityException;
-
-/**
- * Transport header constraint.
- */
-public interface TransportHeaderConstraint {
-
- /**
- * Get constraint key.
- *
- * @return constraint key
- */
- String getConstraintKey();
-
- /**
- * Validate.
- *
- * @param value value
- * @throws ServerTransportSecurityException server transport security exception
- */
- void validate(String value) throws ServerTransportSecurityException;
-}
diff --git a/mcp/bootstrap/src/main/resources/META-INF/shardingsphere-mcp/instructions/server-instructions.md b/mcp/bootstrap/src/main/resources/META-INF/shardingsphere-mcp/instructions/server-instructions.md
index b28cf3ae538..255f17ed7e9 100644
--- a/mcp/bootstrap/src/main/resources/META-INF/shardingsphere-mcp/instructions/server-instructions.md
+++ b/mcp/bootstrap/src/main/resources/META-INF/shardingsphere-mcp/instructions/server-instructions.md
@@ -19,7 +19,7 @@ Apache ShardingSphere MCP.
Use MCP list methods (`tools/list`, `resources/list`, `resources/templates/list`, `prompts/list`) to discover the protocol surface.
-Use `completion/complete` for supported argument values. Read `shardingsphere://capabilities` when ShardingSphere domain capability guidance, workflow guidance, or side-effect notes are needed.
+Use `completion/complete` for supported argument values. Read `shardingsphere://guidance` when ShardingSphere domain capability guidance, workflow guidance, or side-effect notes are needed.
Use `database_gateway_execute_query` only for parser-approved `SELECT`.
Use `database_gateway_execute_explain_query` for execution-plan diagnostics.
diff --git a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/config/MCPLaunchConfigurationTest.java b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/config/MCPLaunchConfigurationTest.java
index 9441d5085f5..2710009f62d 100644
--- a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/config/MCPLaunchConfigurationTest.java
+++ b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/config/MCPLaunchConfigurationTest.java
@@ -21,7 +21,7 @@ import org.apache.shardingsphere.mcp.api.transport.MCPTransportType;
import org.apache.shardingsphere.mcp.bootstrap.config.yaml.config.YamlHttpTransportConfiguration;
import org.apache.shardingsphere.mcp.bootstrap.config.yaml.config.YamlMCPLaunchConfiguration;
import org.apache.shardingsphere.mcp.bootstrap.config.yaml.config.YamlMCPTransportConfiguration;
-import org.apache.shardingsphere.mcp.support.yaml.MCPYamlConfigurationValidator;
+import org.apache.shardingsphere.mcp.support.configuration.MCPConfigurationValidator;
import org.junit.jupiter.api.Test;
import java.util.Collections;
@@ -111,7 +111,7 @@ class MCPLaunchConfigurationTest {
}
private void validate(final YamlMCPLaunchConfiguration yamlConfig) {
- MCPYamlConfigurationValidator.validate(yamlConfig, "MCP launch configuration");
+ MCPConfigurationValidator.validate(yamlConfig, "MCP launch configuration");
}
private YamlMCPLaunchConfiguration createYamlConfig(final MCPTransportType type, final YamlHttpTransportConfiguration http) {
diff --git a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPToolSpecificationFactoryTest.java b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPToolSpecificationFactoryTest.java
index 926be48952f..c5a7b668027 100644
--- a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPToolSpecificationFactoryTest.java
+++ b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPToolSpecificationFactoryTest.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.mcp.bootstrap.transport.capability.tool;
import io.modelcontextprotocol.server.McpServerFeatures.SyncToolSpecification;
-import io.modelcontextprotocol.server.McpSyncServerExchange;
import io.modelcontextprotocol.spec.McpError;
import io.modelcontextprotocol.spec.McpSchema.ErrorCodes;
import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
@@ -27,7 +26,6 @@ import org.apache.shardingsphere.mcp.api.transport.MCPTransportType;
import org.apache.shardingsphere.mcp.api.capability.tool.MCPToolAnnotations;
import org.apache.shardingsphere.mcp.core.context.MCPRuntimeContext;
import org.apache.shardingsphere.mcp.core.context.MCPFeatureRuntimeRequestContext;
-import org.apache.shardingsphere.mcp.core.protocol.exception.UnsupportedToolException;
import org.apache.shardingsphere.mcp.core.tool.handler.MCPToolDefinition;
import org.apache.shardingsphere.mcp.core.tool.handler.ToolDefinitionRegistry;
import org.apache.shardingsphere.mcp.support.descriptor.MCPShardingSphereMetadataKeys;
@@ -36,7 +34,6 @@ import org.apache.shardingsphere.mcp.support.protocol.payload.MCPMapPayload;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
-import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -52,7 +49,6 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
-import static org.mockito.Mockito.when;
class MCPToolSpecificationFactoryTest extends AbstractMCPToolSpecificationFactoryTest {
@@ -109,24 +105,6 @@ class MCPToolSpecificationFactoryTest extends AbstractMCPToolSpecificationFactor
}
}
- @Test
- void assertCreateToolSpecificationsHandleUnsupportedToolAsProtocolError() {
- try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
- mockedToolDefinitionRegistry.when(ToolDefinitionRegistry::getSupportedToolDescriptors).thenReturn(List.of(createToolDescriptor("database_gateway_search_metadata")));
- mockedToolDefinitionRegistry.when(() -> ToolDefinitionRegistry.getToolDefinition("database_gateway_search_metadata")).thenThrow(UnsupportedToolException.class);
- MCPRuntimeContext runtimeContext = mock(MCPRuntimeContext.class, RETURNS_DEEP_STUBS);
- when(runtimeContext.getSessionManager().getTransactionResourceManager().getRuntimeDatabases()).thenReturn(Collections.emptyMap());
- SyncToolSpecification actualSpecification = createToolSpecification(runtimeContext);
- McpSyncServerExchange exchange = createExchange();
- McpError actual = assertThrows(McpError.class, () -> callTool(actualSpecification, exchange, "database_gateway_search_metadata", Map.of()));
- assertThat(actual.getJsonRpcError().code(), is(ErrorCodes.INVALID_PARAMS));
- assertThat(actual.getJsonRpcError().message(), is("Unsupported tool `database_gateway_search_metadata`."));
- @SuppressWarnings("unchecked")
- Map<String, Object> actualData = (Map<String, Object>) actual.getJsonRpcError().data();
- assertThat(actualData.get("summary"), is("Unsupported tool `database_gateway_search_metadata`."));
- }
- }
-
@Test
void assertCreateToolSpecificationsHandleExecutionError() {
try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
diff --git a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/MCPSyncServerFactoryTest.java b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/MCPSyncServerFactoryTest.java
index 10f16019399..fd652ed2778 100644
--- a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/MCPSyncServerFactoryTest.java
+++ b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/MCPSyncServerFactoryTest.java
@@ -38,6 +38,8 @@ import org.apache.shardingsphere.mcp.bootstrap.transport.capability.resource.MCP
import org.apache.shardingsphere.mcp.bootstrap.transport.capability.tool.MCPToolSpecificationFactory;
import org.apache.shardingsphere.mcp.core.context.MCPRuntimeContext;
import org.apache.shardingsphere.mcp.core.session.MCPSessionManager;
+import org.apache.shardingsphere.mcp.support.descriptor.MCPDescriptorCatalogIndex;
+import org.apache.shardingsphere.mcp.support.markdown.MCPMarkdownResourceLoader;
import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;
import org.mockito.internal.configuration.plugins.Plugins;
@@ -52,6 +54,7 @@ import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.when;
@@ -104,6 +107,13 @@ class MCPSyncServerFactoryTest {
actual.closeGracefully();
}
+ @Test
+ void assertServerInstructionsReferenceRegisteredGuidanceResource() {
+ String actual = MCPMarkdownResourceLoader.load(MCPTransportConstants.SERVER_INSTRUCTIONS_RESOURCE, "server instruction");
+ assertTrue(actual.contains("`shardingsphere://guidance`"));
+ assertThat(MCPDescriptorCatalogIndex.getRequiredResourceDescriptor("shardingsphere://guidance").getUriTemplate(), is("shardingsphere://guidance"));
+ }
+
private MCPSyncServerFactory createFactory() {
MCPRuntimeContext runtimeContext = mock(MCPRuntimeContext.class);
when(runtimeContext.getSessionManager()).thenReturn(mock(MCPSessionManager.class));
diff --git a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/ServerTransportSecurityValidatorFactoryTest.java b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/ServerTransportSecurityValidatorFactoryTest.java
index da016b3db1d..98171ede482 100644
--- a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/ServerTransportSecurityValidatorFactoryTest.java
+++ b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/ServerTransportSecurityValidatorFactoryTest.java
@@ -100,4 +100,11 @@ class ServerTransportSecurityValidatorFactoryTest {
() -> validator.validateHeaders(Map.of("Mcp-Session-Id", List.of("session-id"))));
assertThat(ex.getStatusCode(), is(400));
}
+
+ @Test
+ void assertCreateWithUnknownSessionSkipsProtocolVersionConstraint() {
+ MCPSessionManager sessionManager = mock(MCPSessionManager.class);
+ ServerTransportSecurityValidator actual = ServerTransportSecurityValidatorFactory.create(sessionManager, "127.0.0.1", DISABLED_SESSION_ATTRIBUTION_RESOLVER);
+ assertDoesNotThrow(() -> actual.validateHeaders(Map.of("Mcp-Session-Id", List.of("unknown-session"))));
+ }
}
diff --git a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/ShardingSphereServerTransportSecurityValidatorTest.java b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/ShardingSphereServerTransportSecurityValidatorTest.java
index 10cca9d58d5..400e3aab824 100644
--- a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/ShardingSphereServerTransportSecurityValidatorTest.java
+++ b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/validator/ShardingSphereServerTransportSecurityValidatorTest.java
@@ -17,13 +17,12 @@
package org.apache.shardingsphere.mcp.bootstrap.transport.server.http.validator;
-import io.modelcontextprotocol.server.transport.ServerTransportSecurityException;
import io.modelcontextprotocol.spec.HttpHeaders;
import org.apache.shardingsphere.mcp.api.session.MCPSessionIdentity;
import org.apache.shardingsphere.mcp.bootstrap.config.SessionAttributionSourceConfiguration;
import org.apache.shardingsphere.mcp.bootstrap.transport.server.http.SessionAttributionResolver;
import org.apache.shardingsphere.mcp.bootstrap.transport.server.http.validator.constraint.OriginHeaderConstraint;
-import org.apache.shardingsphere.mcp.bootstrap.transport.server.http.validator.constraint.SessionRequiredTransportHeaderConstraint;
+import org.apache.shardingsphere.mcp.bootstrap.transport.server.http.validator.constraint.ProtocolVersionHeaderConstraint;
import org.apache.shardingsphere.mcp.core.session.MCPSessionManager;
import org.junit.jupiter.api.Test;
@@ -35,8 +34,7 @@ import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.verifyNoInteractions;
class ShardingSphereServerTransportSecurityValidatorTest {
@@ -44,7 +42,7 @@ class ShardingSphereServerTransportSecurityValidatorTest {
void assertValidateHeadersWithMatchedSessionIdentity() {
MCPSessionManager sessionManager = new MCPSessionManager(Map.of());
sessionManager.createSession(new MCPSessionIdentity("session-1", "subject", "gateway", Map.of()));
- ShardingSphereServerTransportSecurityValidator actual = new ShardingSphereServerTransportSecurityValidator(sessionManager, List.of(),
+ ShardingSphereServerTransportSecurityValidator actual = createValidator(sessionManager,
new SessionAttributionResolver(new SessionAttributionSourceConfiguration("X-Test-Subject", "X-Test-Source", "X-Test-Attr-")));
assertDoesNotThrow(() -> actual.validateHeaders(Map.of(HttpHeaders.MCP_SESSION_ID, List.of("session-1"), "X-Test-Subject", List.of("subject"),
"X-Test-Source", List.of("gateway"))));
@@ -54,7 +52,7 @@ class ShardingSphereServerTransportSecurityValidatorTest {
void assertValidateHeadersWithMismatchedSessionIdentity() {
MCPSessionManager sessionManager = new MCPSessionManager(Map.of());
sessionManager.createSession(new MCPSessionIdentity("session-1", "subject", "gateway", Map.of()));
- ShardingSphereServerTransportSecurityValidator actual = new ShardingSphereServerTransportSecurityValidator(sessionManager, List.of(),
+ ShardingSphereServerTransportSecurityValidator actual = createValidator(sessionManager,
new SessionAttributionResolver(new SessionAttributionSourceConfiguration("X-Test-Subject", "X-Test-Source", "X-Test-Attr-")));
MCPTransportSecurityException exception = assertThrows(MCPTransportSecurityException.class, () -> actual.validateHeaders(
Map.of(HttpHeaders.MCP_SESSION_ID, List.of("session-1"), "X-Test-Subject", List.of("other"), "X-Test-Source", List.of("gateway"))));
@@ -67,7 +65,7 @@ class ShardingSphereServerTransportSecurityValidatorTest {
void assertValidateHeadersWithChangedSource() {
MCPSessionManager sessionManager = new MCPSessionManager(Map.of());
sessionManager.createSession(new MCPSessionIdentity("session-1", "subject", "gateway", Map.of()));
- ShardingSphereServerTransportSecurityValidator actual = new ShardingSphereServerTransportSecurityValidator(sessionManager, List.of(),
+ ShardingSphereServerTransportSecurityValidator actual = createValidator(sessionManager,
new SessionAttributionResolver(new SessionAttributionSourceConfiguration("X-Test-Subject", "X-Test-Source", "X-Test-Attr-")));
assertThrows(MCPTransportSecurityException.class, () -> actual.validateHeaders(Map.of(HttpHeaders.MCP_SESSION_ID, List.of("session-1"),
"X-Test-Subject", List.of("subject"), "X-Test-Source", List.of("other"))));
@@ -77,7 +75,7 @@ class ShardingSphereServerTransportSecurityValidatorTest {
void assertValidateHeadersWithMissingSubject() {
MCPSessionManager sessionManager = new MCPSessionManager(Map.of());
sessionManager.createSession(new MCPSessionIdentity("session-1", "subject", "gateway", Map.of()));
- ShardingSphereServerTransportSecurityValidator actual = new ShardingSphereServerTransportSecurityValidator(sessionManager, List.of(),
+ ShardingSphereServerTransportSecurityValidator actual = createValidator(sessionManager,
new SessionAttributionResolver(new SessionAttributionSourceConfiguration("X-Test-Subject", "X-Test-Source", "X-Test-Attr-")));
MCPTransportSecurityException exception = assertThrows(MCPTransportSecurityException.class, () -> actual.validateHeaders(
Map.of(HttpHeaders.MCP_SESSION_ID, List.of("session-1"), "X-Test-Source", List.of("gateway"))));
@@ -90,7 +88,7 @@ class ShardingSphereServerTransportSecurityValidatorTest {
void assertValidateHeadersWithChangedAttributes() {
MCPSessionManager sessionManager = new MCPSessionManager(Map.of());
sessionManager.createSession(new MCPSessionIdentity("session-1", "subject", "gateway", Map.of("region", "ap-south")));
- ShardingSphereServerTransportSecurityValidator actual = new ShardingSphereServerTransportSecurityValidator(sessionManager, List.of(),
+ ShardingSphereServerTransportSecurityValidator actual = createValidator(sessionManager,
new SessionAttributionResolver(new SessionAttributionSourceConfiguration("X-Test-Subject", "X-Test-Source", "X-Test-Attr-")));
assertThrows(MCPTransportSecurityException.class, () -> actual.validateHeaders(Map.of(HttpHeaders.MCP_SESSION_ID, List.of("session-1"),
"X-Test-Subject", List.of("subject"), "X-Test-Source", List.of("gateway"), "X-Test-Attr-Region", List.of("eu-west"))));
@@ -100,7 +98,7 @@ class ShardingSphereServerTransportSecurityValidatorTest {
void assertValidateHeadersWithSubjectAddedToUnattributedSession() {
MCPSessionManager sessionManager = new MCPSessionManager(Map.of());
sessionManager.createSession(new MCPSessionIdentity("session-1", "", "", Map.of()));
- ShardingSphereServerTransportSecurityValidator actual = new ShardingSphereServerTransportSecurityValidator(sessionManager, List.of(),
+ ShardingSphereServerTransportSecurityValidator actual = createValidator(sessionManager,
new SessionAttributionResolver(new SessionAttributionSourceConfiguration("X-Test-Subject", "X-Test-Source", "X-Test-Attr-")));
assertThrows(MCPTransportSecurityException.class, () -> actual.validateHeaders(
Map.of(HttpHeaders.MCP_SESSION_ID, List.of("session-1"), "X-Test-Subject", List.of("subject"), "X-Test-Source", List.of("gateway"))));
@@ -108,27 +106,27 @@ class ShardingSphereServerTransportSecurityValidatorTest {
@Test
void assertValidateHeadersWithMissingSession() {
- ShardingSphereServerTransportSecurityValidator actual = new ShardingSphereServerTransportSecurityValidator(new MCPSessionManager(Map.of()), List.of(),
+ ShardingSphereServerTransportSecurityValidator actual = createValidator(new MCPSessionManager(Map.of()),
new SessionAttributionResolver(new SessionAttributionSourceConfiguration("X-Test-Subject", "X-Test-Source", "X-Test-Attr-")));
assertDoesNotThrow(() -> actual.validateHeaders(
Map.of(HttpHeaders.MCP_SESSION_ID, List.of("session-1"), "X-Test-Subject", List.of("subject"), "X-Test-Source", List.of("gateway"))));
}
@Test
- void assertValidateSessionRequiredConstraintWithMissingSession() throws ServerTransportSecurityException {
- SessionRequiredTransportHeaderConstraint constraint = mock(SessionRequiredTransportHeaderConstraint.class);
- when(constraint.getConstraintKey()).thenReturn(HttpHeaders.PROTOCOL_VERSION);
+ void assertValidateHeadersWithUnknownSessionSkipsProtocolValidation() {
+ ProtocolVersionHeaderConstraint protocolVersionHeaderConstraint = mock(ProtocolVersionHeaderConstraint.class);
ShardingSphereServerTransportSecurityValidator actual = new ShardingSphereServerTransportSecurityValidator(new MCPSessionManager(Map.of()),
- List.of(constraint), new SessionAttributionResolver(null));
+ mock(OriginHeaderConstraint.class), protocolVersionHeaderConstraint, new SessionAttributionResolver(null));
assertDoesNotThrow(() -> actual.validateHeaders(Map.of(HttpHeaders.MCP_SESSION_ID, List.of("session-1"), HttpHeaders.PROTOCOL_VERSION, List.of("version"))));
- verify(constraint).validate("version");
+ verifyNoInteractions(protocolVersionHeaderConstraint);
}
@Test
void assertValidateOriginBeforeSessionIdentity() {
MCPSessionManager sessionManager = new MCPSessionManager(Map.of());
sessionManager.createSession(new MCPSessionIdentity("session-1", "subject", "gateway", Map.of()));
- ShardingSphereServerTransportSecurityValidator actual = new ShardingSphereServerTransportSecurityValidator(sessionManager, List.of(new OriginHeaderConstraint(true)),
+ ShardingSphereServerTransportSecurityValidator actual = new ShardingSphereServerTransportSecurityValidator(sessionManager, new OriginHeaderConstraint(true),
+ mock(ProtocolVersionHeaderConstraint.class),
new SessionAttributionResolver(new SessionAttributionSourceConfiguration("X-Test-Subject", "X-Test-Source", "X-Test-Attr-")));
MCPTransportSecurityException exception = assertThrows(MCPTransportSecurityException.class, () -> actual.validateHeaders(Map.of(
"Origin", List.of("https://example.com"), HttpHeaders.MCP_SESSION_ID, List.of("session-1"),
@@ -136,4 +134,10 @@ class ShardingSphereServerTransportSecurityValidatorTest {
assertThat(exception.getStatusCode(), is(403));
assertThat(exception.getCategory(), is(MCPTransportSecurityException.CATEGORY_ORIGIN_NOT_ALLOWED));
}
+
+ private ShardingSphereServerTransportSecurityValidator createValidator(final MCPSessionManager sessionManager,
+ final SessionAttributionResolver sessionAttributionResolver) {
+ return new ShardingSphereServerTransportSecurityValidator(sessionManager, mock(OriginHeaderConstraint.class), mock(ProtocolVersionHeaderConstraint.class),
+ sessionAttributionResolver);
+ }
}
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/completion/MCPCompletionService.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/completion/MCPCompletionService.java
index 5453676389c..8ffabe5a020 100644
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/completion/MCPCompletionService.java
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/completion/MCPCompletionService.java
@@ -112,8 +112,9 @@ public final class MCPCompletionService {
matchStrategy = "contains_fallback";
}
List<MCPCompletionCandidate> returnedCandidates = filteredCandidates.stream().limit(maxValues).toList();
- Map<String, Object> meta = createMeta(descriptor, argumentName, prefix, matchStrategy, actualContextArguments, inferredContextArguments, handlerResult.getMissingContextArguments(),
- handlerResult.getNearestResourceUri(), candidates, filteredCandidates, returnedCandidates);
+ CompletionMetadataContext metadataContext = new CompletionMetadataContext(descriptor, argumentName, prefix, matchStrategy, actualContextArguments, handlerResult,
+ filteredCandidates, returnedCandidates);
+ Map<String, Object> meta = createMeta(metadataContext);
return new MCPCompletionResult(returnedCandidates.stream().map(MCPCompletionCandidate::getValue).toList(), filteredCandidates.size(), filteredCandidates.size() > returnedCandidates.size(),
meta);
}
@@ -173,32 +174,34 @@ public final class MCPCompletionService {
return handler.complete(handler.getContextType().cast(requestContext), request);
}
- private Map<String, Object> createMeta(final MCPCompletionTargetDescriptor descriptor, final String argumentName, final String prefix, final String matchStrategy,
- final Map<String, String> contextArguments, final Map<String, Object> inferredContextArguments, final Collection<String> missingContextArguments,
- final String nearestResourceUri, final Collection<MCPCompletionCandidate> candidates, final Collection<MCPCompletionCandidate> filteredCandidates,
- final Collection<MCPCompletionCandidate> returnedCandidates) {
+ private Map<String, Object> createMeta(final CompletionMetadataContext context) {
+ MCPCompletionTargetDescriptor descriptor = context.descriptor();
+ MCPCompletionHandlerResult handlerResult = context.handlerResult();
+ Collection<MCPCompletionCandidate> candidates = handlerResult.getCandidates();
Map<String, Object> result = new LinkedHashMap<>(12, 1F);
result.put(MCPShardingSphereMetadataKeys.RESPONSE_MODE, MCPResponseMode.LIST);
result.put(MCPShardingSphereMetadataKeys.REFERENCE_TYPE, descriptor.getReferenceType());
result.put(MCPShardingSphereMetadataKeys.REFERENCE, descriptor.getReference());
- result.put(MCPShardingSphereMetadataKeys.ARGUMENT, argumentName);
- result.put(MCPShardingSphereMetadataKeys.PREFIX_ARGUMENT, prefix);
- result.put(MCPShardingSphereMetadataKeys.MATCH_STRATEGY, matchStrategy);
- result.put(MCPShardingSphereMetadataKeys.CONTEXT_ARGUMENTS, contextArguments);
+ result.put(MCPShardingSphereMetadataKeys.ARGUMENT, context.argumentName());
+ result.put(MCPShardingSphereMetadataKeys.PREFIX_ARGUMENT, context.prefix());
+ result.put(MCPShardingSphereMetadataKeys.MATCH_STRATEGY, context.matchStrategy());
+ result.put(MCPShardingSphereMetadataKeys.CONTEXT_ARGUMENTS, context.contextArguments());
result.put(MCPShardingSphereMetadataKeys.CANDIDATE_COUNT, candidates.size());
- putInferredContext(result, inferredContextArguments);
- result.put(MCPShardingSphereMetadataKeys.MISSING_CONTEXT_ARGUMENTS, missingContextArguments);
- String diagnostic = createDiagnostic(candidates, filteredCandidates, missingContextArguments);
+ putInferredContext(result, handlerResult.getInferredContextArguments());
+ result.put(MCPShardingSphereMetadataKeys.MISSING_CONTEXT_ARGUMENTS, handlerResult.getMissingContextArguments());
+ String diagnostic = createDiagnostic(candidates, context.filteredCandidates(), handlerResult.getMissingContextArguments());
result.put(MCPShardingSphereMetadataKeys.DIAGNOSTIC, diagnostic);
if (!"ok".equals(diagnostic)) {
- result.put(MCPShardingSphereMetadataKeys.RECOVERY, createRecovery(prefix, diagnostic, missingContextArguments, nearestResourceUri));
+ result.put(MCPShardingSphereMetadataKeys.RECOVERY,
+ createRecovery(context.prefix(), diagnostic, handlerResult.getMissingContextArguments(), handlerResult.getNearestResourceUri()));
}
- List<Map<String, Object>> nextActions = createNextActions(descriptor, argumentName, prefix, contextArguments, diagnostic, missingContextArguments, nearestResourceUri);
+ List<Map<String, Object>> nextActions = createNextActions(descriptor, context.argumentName(), context.prefix(), context.contextArguments(), diagnostic,
+ handlerResult.getMissingContextArguments(), handlerResult.getNearestResourceUri());
if (!nextActions.isEmpty()) {
result.put(MCPShardingSphereMetadataKeys.NEXT_ACTIONS, nextActions);
}
result.put(MCPShardingSphereMetadataKeys.RANKING_POLICY, createRankingPolicy(candidates));
- result.put(MCPShardingSphereMetadataKeys.VALUE_DETAILS, returnedCandidates.stream().map(this::createValueDetail).toList());
+ result.put(MCPShardingSphereMetadataKeys.VALUE_DETAILS, context.returnedCandidates().stream().map(this::createValueDetail).toList());
return result;
}
@@ -309,4 +312,9 @@ public final class MCPCompletionService {
result.add("case-insensitive-lexical");
return result;
}
+
+ private record CompletionMetadataContext(MCPCompletionTargetDescriptor descriptor, String argumentName, String prefix, String matchStrategy,
+ Map<String, String> contextArguments, MCPCompletionHandlerResult handlerResult,
+ Collection<MCPCompletionCandidate> filteredCandidates, Collection<MCPCompletionCandidate> returnedCandidates) {
+ }
}
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/resource/handler/ResourceDefinitionRegistry.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/resource/handler/ResourceDefinitionRegistry.java
index d198ae8db34..eb23625b573 100644
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/resource/handler/ResourceDefinitionRegistry.java
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/resource/handler/ResourceDefinitionRegistry.java
@@ -141,15 +141,6 @@ public final class ResourceDefinitionRegistry {
return resourceHandler.handle(resourceHandler.getContextType().cast(requestContext), uriVariables);
}
- /**
- * Get supported resources.
- *
- * @return supported resources
- */
- public static Collection<String> getSupportedResources() {
- return REGISTERED_RESOURCE_DEFINITIONS.stream().map(each -> each.getUriPattern().getPattern()).toList();
- }
-
/**
* Get supported resource descriptors.
*
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/resource/uri/MCPUriPattern.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/resource/uri/MCPUriPattern.java
index 3e69d712822..1998ac0378f 100644
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/resource/uri/MCPUriPattern.java
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/resource/uri/MCPUriPattern.java
@@ -18,103 +18,24 @@
package org.apache.shardingsphere.mcp.core.resource.uri;
import lombok.Getter;
-import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.mcp.api.capability.resource.MCPResourceURIVariables;
-import org.apache.shardingsphere.mcp.support.resource.MCPUriPathSegmentUtils;
+import org.apache.shardingsphere.mcp.support.resource.MCPUriTemplate;
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
import java.util.Optional;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
/**
* MCP URI pattern.
*/
-@Getter
public final class MCPUriPattern {
- private static final String SCHEME_SEPARATOR = "://";
-
- private static final String SCHEME = "shardingsphere";
-
+ @Getter
private final String pattern;
- private final List<String> variableNames;
-
- private final Pattern compiledRegex;
+ private final MCPUriTemplate uriTemplate;
public MCPUriPattern(final String pattern) {
this.pattern = pattern;
- int schemeSeparatorIndex = findSchemeSeparatorIndex(pattern);
- validateScheme(pattern, schemeSeparatorIndex);
- List<String> pathSegments = extractPathSegments(pattern, schemeSeparatorIndex + SCHEME_SEPARATOR.length());
- variableNames = extractVariableNames(pattern, pathSegments);
- compiledRegex = compileRegex(pathSegments);
- }
-
- private int findSchemeSeparatorIndex(final String uri) {
- int result = uri.indexOf(SCHEME_SEPARATOR);
- ShardingSpherePreconditions.checkState(0 < result, () -> new IllegalArgumentException(String.format("Invalid URI `%s`.", uri)));
- return result;
- }
-
- private void validateScheme(final String uri, final int schemeSeparatorIndex) {
- String scheme = uri.substring(0, schemeSeparatorIndex);
- ShardingSpherePreconditions.checkState(SCHEME.equals(scheme), () -> new IllegalArgumentException(String.format("Invalid URI `%s`.", uri)));
- }
-
- private List<String> extractPathSegments(final String uri, final int pathStartIndex) {
- String path = uri.substring(pathStartIndex);
- ShardingSpherePreconditions.checkNotEmpty(path, () -> new IllegalArgumentException(String.format("URI path is required for `%s`.", uri)));
- String[] segments = path.split("/", -1);
- List<String> result = new ArrayList<>(segments.length);
- for (String each : segments) {
- ShardingSpherePreconditions.checkNotEmpty(each, () -> new IllegalArgumentException(String.format("URI segment is required for `%s`.", uri)));
- result.add(each);
- }
- return result;
- }
-
- private List<String> extractVariableNames(final String pattern, final List<String> pathSegments) {
- List<String> result = new ArrayList<>(pathSegments.size());
- for (String each : pathSegments) {
- if (!isVariableSegment(each)) {
- continue;
- }
- String variableName = extractVariableName(pattern, each);
- ShardingSpherePreconditions.checkState(!result.contains(variableName),
- () -> new IllegalArgumentException(String.format("Duplicate URI pattern variable `%s` in `%s`.", variableName, pattern)));
- result.add(variableName);
- }
- return result;
- }
-
- private String extractVariableName(final String pattern, final String pathSegment) {
- String result = pathSegment.substring(1, pathSegment.length() - 1);
- ShardingSpherePreconditions.checkNotEmpty(result, () -> new IllegalArgumentException(String.format("URI pattern variable is required in `%s`.", pattern)));
- return result;
- }
-
- private Pattern compileRegex(final List<String> pathSegments) {
- StringBuilder regex = new StringBuilder("^");
- regex.append(Pattern.quote(SCHEME));
- regex.append(SCHEME_SEPARATOR);
- for (int i = 0; i < pathSegments.size(); i++) {
- if (0 < i) {
- regex.append('/');
- }
- String each = pathSegments.get(i);
- regex.append(isVariableSegment(each) ? "([^/]+)" : Pattern.quote(each));
- }
- regex.append('$');
- return Pattern.compile(regex.toString());
- }
-
- private boolean isVariableSegment(final String pathSegment) {
- return 2 < pathSegment.length() && pathSegment.startsWith("{") && pathSegment.endsWith("}");
+ uriTemplate = new MCPUriTemplate(pattern);
}
/**
@@ -124,27 +45,7 @@ public final class MCPUriPattern {
* @return parsed variables when present
*/
public Optional<MCPResourceURIVariables> parse(final String uri) {
- Matcher matcher = compiledRegex.matcher(uri);
- if (!matcher.matches()) {
- return Optional.empty();
- }
- Map<String, String> variables = new LinkedHashMap<>(variableNames.size(), 1F);
- for (int i = 0; i < variableNames.size(); i++) {
- String encodedValue = matcher.group(i + 1);
- if (containsRawTemplateMarker(encodedValue)) {
- return Optional.empty();
- }
- Optional<String> decodedValue = MCPUriPathSegmentUtils.decodePathSegment(encodedValue);
- if (decodedValue.isEmpty()) {
- return Optional.empty();
- }
- variables.put(variableNames.get(i), decodedValue.get());
- }
- return Optional.of(new MCPResourceURIVariables(variables));
- }
-
- private boolean containsRawTemplateMarker(final String encodedValue) {
- return encodedValue.contains("{") || encodedValue.contains("}");
+ return uriTemplate.parse(uri);
}
/**
@@ -154,18 +55,6 @@ public final class MCPUriPattern {
* @return overlap or not
*/
public boolean overlaps(final MCPUriPattern other) {
- List<String> leftSegments = extractPathSegments(pattern, SCHEME.length() + SCHEME_SEPARATOR.length());
- List<String> rightSegments = extractPathSegments(other.getPattern(), SCHEME.length() + SCHEME_SEPARATOR.length());
- if (leftSegments.size() != rightSegments.size()) {
- return false;
- }
- for (int i = 0; i < leftSegments.size(); i++) {
- String left = leftSegments.get(i);
- String right = rightSegments.get(i);
- if (!isVariableSegment(left) && !isVariableSegment(right) && !left.equals(right)) {
- return false;
- }
- }
- return true;
+ return uriTemplate.overlaps(other.uriTemplate);
}
}
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/MCPToolCallLimiter.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/MCPToolCallLimiter.java
index 9d9104b3664..b4428d5e5e7 100644
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/MCPToolCallLimiter.java
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/MCPToolCallLimiter.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.mcp.core.tool;
import org.apache.shardingsphere.mcp.core.protocol.exception.MCPToolCallLimitExceededException;
-import org.apache.shardingsphere.mcp.support.security.MCPClientSafetyPolicy;
+import org.apache.shardingsphere.mcp.support.security.MCPRuntimeProtectionPolicy;
import java.util.Map;
import java.util.Objects;
@@ -30,14 +30,10 @@ import java.util.concurrent.atomic.AtomicInteger;
*/
public final class MCPToolCallLimiter {
- private final int maxToolCallsPerSession;
+ private final int maxToolCallsPerSession = MCPRuntimeProtectionPolicy.getMaxToolCallsPerSession();
private final Map<String, AtomicInteger> sessionToolCallCounts = new ConcurrentHashMap<>();
- public MCPToolCallLimiter() {
- maxToolCallsPerSession = MCPClientSafetyPolicy.getMaxToolCallsPerSession();
- }
-
/**
* Acquire one tool call budget slot.
*
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/ToolDefinitionRegistry.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/ToolDefinitionRegistry.java
index f7f9fd7af2b..24f926f6397 100644
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/ToolDefinitionRegistry.java
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/ToolDefinitionRegistry.java
@@ -80,15 +80,6 @@ public final class ToolDefinitionRegistry {
}
}
- /**
- * Get supported tools.
- *
- * @return supported tools
- */
- public static Collection<String> getSupportedTools() {
- return REGISTERED_TOOL_DEFINITIONS.keySet().stream().toList();
- }
-
/**
* Get supported tool descriptors.
*
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/ClassificationResult.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/ClassificationResult.java
index 6cc5056dd0d..a0d15bf389f 100644
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/ClassificationResult.java
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/ClassificationResult.java
@@ -90,15 +90,6 @@ public final class ClassificationResult {
return savepointName.isEmpty() ? Optional.empty() : Optional.of(savepointName);
}
- /**
- * Determine whether this statement mutates ShardingSphere rule metadata through DistSQL.
- *
- * @return true when the statement is a recognized rule DistSQL statement
- */
- public boolean isRuleDistSQL() {
- return ruleDistSQL;
- }
-
/**
* Get model-facing side-effect scope.
*
@@ -116,8 +107,4 @@ public final class ClassificationResult {
default -> "unknown-side-effect";
};
}
-
- String getTraceStatementMarker() {
- return SupportedMCPStatement.TRANSACTION_CONTROL == statementClass || SupportedMCPStatement.SAVEPOINT == statementClass ? statementType : statementClass.name();
- }
}
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/MCPSQLExecutionFacade.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/MCPSQLExecutionFacade.java
index 8ef4301b41b..7a5bee5ebd8 100644
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/MCPSQLExecutionFacade.java
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/MCPSQLExecutionFacade.java
@@ -24,15 +24,11 @@ import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.metadata.identifier.DatabaseIdentifierContext;
import org.apache.shardingsphere.mcp.api.exception.MCPInvalidRequestException;
import org.apache.shardingsphere.mcp.api.exception.MCPQueryFailedException;
-import org.apache.shardingsphere.mcp.api.exception.MCPUnsupportedException;
import org.apache.shardingsphere.mcp.core.session.MCPSessionExecutionCoordinator;
import org.apache.shardingsphere.mcp.core.session.MCPSessionManager;
-import org.apache.shardingsphere.mcp.core.session.MCPSessionNotExistedException;
-import org.apache.shardingsphere.mcp.core.tool.handler.execute.trace.SQLExecutionTraceFactory;
import org.apache.shardingsphere.mcp.support.database.capability.MCPDatabaseCapability;
import org.apache.shardingsphere.mcp.support.database.capability.MCPDatabaseCapabilityProvider;
import org.apache.shardingsphere.mcp.support.database.capability.SchemaExecutionSemantics;
-import org.apache.shardingsphere.mcp.support.database.capability.SupportedMCPStatement;
import org.apache.shardingsphere.mcp.support.database.exception.DatabaseCapabilityNotFoundException;
import org.apache.shardingsphere.mcp.support.database.exception.MCPJDBCErrorCategory;
import org.apache.shardingsphere.mcp.support.database.exception.MCPJDBCExceptionClassifier;
@@ -60,33 +56,23 @@ public final class MCPSQLExecutionFacade implements MCPFeatureExecutionFacade {
private final MCPStatementAnalyzer statementAnalyzer;
- private final SQLExecutionTraceFactory sqlExecutionTraceFactory;
-
public MCPSQLExecutionFacade(final MCPDatabaseCapabilityProvider databaseCapabilityProvider, final MCPSessionManager sessionManager) {
this(databaseCapabilityProvider, new MCPSessionExecutionCoordinator(sessionManager),
new MCPJdbcTransactionStatementExecutor(sessionManager),
new MCPJdbcStatementExecutor(sessionManager.getTransactionResourceManager().getRuntimeDatabases(), sessionManager.getTransactionResourceManager()),
- new MCPStatementAnalyzer(), new SQLExecutionTraceFactory());
+ new MCPStatementAnalyzer());
}
@Override
public SQLExecutionResult execute(final SQLExecutionRequest executionRequest) {
- try {
- return sessionExecutionCoordinator.executeWithSessionLock(executionRequest.getSessionId(), () -> {
- MCPDatabaseCapability databaseCapability = getDatabaseCapability(executionRequest);
- return executeInternal(executionRequest, classify(executionRequest, databaseCapability), databaseCapability);
- });
- } catch (final MCPSessionNotExistedException ex) {
- throw recordFailure(executionRequest, SupportedMCPStatement.QUERY.name(), ex);
- }
+ return sessionExecutionCoordinator.executeWithSessionLock(executionRequest.getSessionId(), () -> {
+ MCPDatabaseCapability databaseCapability = getDatabaseCapability(executionRequest);
+ return executeInternal(executionRequest, statementAnalyzer.analyze(executionRequest.getSql(), databaseCapability), databaseCapability);
+ });
}
private SQLExecutionResult execute(final SQLExecutionRequest executionRequest, final ClassificationResult classificationResult, final MCPDatabaseCapability databaseCapability) {
- try {
- return sessionExecutionCoordinator.executeWithSessionLock(executionRequest.getSessionId(), () -> executeInternal(executionRequest, classificationResult, databaseCapability));
- } catch (final MCPSessionNotExistedException ex) {
- throw recordFailure(executionRequest, classificationResult.getTraceStatementMarker(), ex);
- }
+ return sessionExecutionCoordinator.executeWithSessionLock(executionRequest.getSessionId(), () -> executeInternal(executionRequest, classificationResult, databaseCapability));
}
@Override
@@ -103,52 +89,22 @@ public final class MCPSQLExecutionFacade implements MCPFeatureExecutionFacade {
}
}
- private ClassificationResult classify(final SQLExecutionRequest executionRequest, final MCPDatabaseCapability databaseCapability) {
- try {
- return statementAnalyzer.analyze(executionRequest.getSql(), databaseCapability);
- } catch (final MCPUnsupportedException | MCPInvalidRequestException ex) {
- throw recordFailure(executionRequest, SupportedMCPStatement.QUERY.name(), ex);
- } catch (final IllegalArgumentException ex) {
- throw recordFailure(executionRequest, SupportedMCPStatement.QUERY.name(), ex);
- }
- }
-
private MCPDatabaseCapability getDatabaseCapability(final SQLExecutionRequest executionRequest) {
Optional<MCPDatabaseCapability> databaseCapability = databaseCapabilityProvider.provide(executionRequest.getDatabase());
- ShardingSpherePreconditions.checkState(databaseCapability.isPresent(), () -> recordFailure(executionRequest, "QUERY", new DatabaseCapabilityNotFoundException()));
+ ShardingSpherePreconditions.checkState(databaseCapability.isPresent(), DatabaseCapabilityNotFoundException::new);
return databaseCapability.orElseThrow();
}
private SQLExecutionResult executeInternal(final SQLExecutionRequest executionRequest, final ClassificationResult classificationResult,
final MCPDatabaseCapability databaseCapability) {
ShardingSpherePreconditions.checkContains(databaseCapability.getSupportedStatementClasses(), classificationResult.getStatementClass(),
- () -> recordFailure(executionRequest, classificationResult.getTraceStatementMarker(), new StatementClassNotSupportedException()));
+ StatementClassNotSupportedException::new);
checkCrossSchemaSql(executionRequest, databaseCapability, classificationResult);
- try {
- switch (classificationResult.getStatementClass()) {
- case TRANSACTION_CONTROL:
- case SAVEPOINT:
- return recordSuccess(executionRequest, transactionStatementExecutor.execute(
- executionRequest.getSessionId(), executionRequest.getDatabase(), databaseCapability, classificationResult), classificationResult.getTraceStatementMarker());
- case QUERY:
- case EXPLAIN:
- case DML:
- case DDL:
- case DCL:
- return recordSuccess(executionRequest, statementExecutor.execute(executionRequest, classificationResult, databaseCapability), classificationResult.getTraceStatementMarker());
- default:
- throw new StatementClassNotSupportedException();
- }
- // CHECKSTYLE:OFF
- } catch (final RuntimeException ex) {
- // CHECKSTYLE:ON
- throw recordFailure(executionRequest, classificationResult.getTraceStatementMarker(), ex);
- }
- }
-
- private SQLExecutionResult recordSuccess(final SQLExecutionRequest executionRequest, final SQLExecutionResult result, final String statementMarker) {
- sqlExecutionTraceFactory.create(executionRequest.getSessionId(), executionRequest.getDatabase(), executionRequest.getSql(), true, statementMarker);
- return result;
+ return switch (classificationResult.getStatementClass()) {
+ case TRANSACTION_CONTROL, SAVEPOINT -> transactionStatementExecutor.execute(
+ executionRequest.getSessionId(), executionRequest.getDatabase(), databaseCapability, classificationResult);
+ case QUERY, EXPLAIN, DML, DDL, DCL -> statementExecutor.execute(executionRequest, classificationResult, databaseCapability);
+ };
}
private void checkCrossSchemaSql(final SQLExecutionRequest executionRequest, final MCPDatabaseCapability databaseCapability, final ClassificationResult classificationResult) {
@@ -157,8 +113,7 @@ public final class MCPSQLExecutionFacade implements MCPFeatureExecutionFacade {
}
for (SQLStatementObjectName each : classificationResult.getReferencedObjects()) {
if (isCrossSchemaReference(each, executionRequest.getDatabase(), databaseCapability.getIdentifierContext())) {
- throw recordFailure(executionRequest, classificationResult.getTraceStatementMarker(), new MCPInvalidRequestException(
- String.format("Cross-schema SQL is not supported for database `%s`: `%s`.", executionRequest.getDatabase(), each.getObjectName())));
+ throw new MCPInvalidRequestException(String.format("Cross-schema SQL is not supported for database `%s`: `%s`.", executionRequest.getDatabase(), each.getObjectName()));
}
}
}
@@ -168,9 +123,4 @@ public final class MCPSQLExecutionFacade implements MCPFeatureExecutionFacade {
&& !identifierContext.matchesMetaData(IdentifierScope.SCHEMA, databaseName,
new IdentifierValue(objectName.getFirstIdentifier(), objectName.getFirstIdentifierQuoteCharacter()));
}
-
- private <T extends RuntimeException> T recordFailure(final SQLExecutionRequest executionRequest, final String statementMarker, final T ex) {
- sqlExecutionTraceFactory.create(executionRequest.getSessionId(), executionRequest.getDatabase(), executionRequest.getSql(), false, statementMarker);
- return ex;
- }
}
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceFactory.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceFactory.java
deleted file mode 100644
index 5cb7ea749ee..00000000000
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceFactory.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.mcp.core.tool.handler.execute.trace;
-
-import java.nio.charset.StandardCharsets;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-import java.time.Instant;
-
-/**
- * Create SQL execution trace records.
- */
-public final class SQLExecutionTraceFactory {
-
- /**
- * Create one SQL execution trace record.
- *
- * @param sessionId session identifier
- * @param databaseName logical database name
- * @param sql SQL text
- * @param success success flag
- * @param statementMarker optional statement marker
- * @return SQL execution trace record
- */
- public SQLExecutionTraceRecord create(final String sessionId, final String databaseName, final String sql, final boolean success, final String statementMarker) {
- return new SQLExecutionTraceRecord(sessionId, databaseName, digest(sql), success, statementMarker, Instant.now().toString());
- }
-
- private String digest(final String value) {
- try {
- MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
- byte[] actualDigest = messageDigest.digest(value.trim().getBytes(StandardCharsets.UTF_8));
- StringBuilder result = new StringBuilder(actualDigest.length * 2);
- for (byte each : actualDigest) {
- result.append(String.format("%02x", each));
- }
- return result.toString();
- } catch (final NoSuchAlgorithmException ex) {
- throw new IllegalStateException("SHA-256 is not available.", ex);
- }
- }
-}
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceRecord.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceRecord.java
deleted file mode 100644
index 52f2687ed30..00000000000
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceRecord.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.mcp.core.tool.handler.execute.trace;
-
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-
-/**
- * SQL execution trace projection.
- */
-@RequiredArgsConstructor
-@Getter
-public final class SQLExecutionTraceRecord {
-
- private final String sessionId;
-
- private final String database;
-
- private final String sqlDigest;
-
- private final boolean success;
-
- private final String statementMarker;
-
- private final String timestamp;
-}
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/request/MCPToolArguments.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/request/MCPToolArguments.java
index 49fc2fadf2c..9ed0ed10247 100644
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/request/MCPToolArguments.java
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/request/MCPToolArguments.java
@@ -95,32 +95,6 @@ public final class MCPToolArguments {
return Objects.toString(arguments.get(name), "").trim();
}
- /**
- * Get integer argument.
- *
- * @param name argument name
- * @param defaultValue default value
- * @return argument value
- */
- public int getIntegerArgument(final String name, final int defaultValue) {
- Object result = arguments.get(name);
- if (null == result) {
- return defaultValue;
- }
- if (result instanceof Number) {
- return ((Number) result).intValue();
- }
- String actualValue = result.toString().trim();
- if (actualValue.isEmpty()) {
- return defaultValue;
- }
- try {
- return Integer.parseInt(actualValue);
- } catch (final NumberFormatException ignored) {
- return defaultValue;
- }
- }
-
/**
* Get bounded integer argument.
*
diff --git a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/resource/handler/ResourceDefinitionRegistryTest.java b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/resource/handler/ResourceDefinitionRegistryTest.java
index a13f746fcb8..6d068651d07 100644
--- a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/resource/handler/ResourceDefinitionRegistryTest.java
+++ b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/resource/handler/ResourceDefinitionRegistryTest.java
@@ -73,7 +73,7 @@ class ResourceDefinitionRegistryTest {
mocked.when(() -> ShardingSphereServiceLoader.getServiceInstances(MCPHandlerProvider.class)).thenReturn(List.of(provider));
Class<?> registryClass = assertDoesNotThrow(() -> Class.forName(ResourceDefinitionRegistry.class.getName(), false, createIsolatedResourceDefinitionRegistryClassLoader()));
InvocationTargetException actual = assertThrows(InvocationTargetException.class,
- () -> Plugins.getMemberAccessor().invoke(registryClass.getMethod("getSupportedResources"), null));
+ () -> Plugins.getMemberAccessor().invoke(registryClass.getMethod("getSupportedResourceDescriptors"), null));
assertThat(actual.getCause().getClass(), is(ExceptionInInitializerError.class));
Throwable actualCause = actual.getCause().getCause();
assertThat(actualCause.getClass(), is(expectedCauseType));
@@ -82,8 +82,8 @@ class ResourceDefinitionRegistryTest {
}
@Test
- void assertGetSupportedResources() {
- Collection<String> actual = ResourceDefinitionRegistry.getSupportedResources();
+ void assertGetSupportedResourceDescriptors() {
+ Collection<String> actual = ResourceDefinitionRegistry.getSupportedResourceDescriptors().stream().map(MCPResourceDescriptor::getUriTemplate).toList();
assertThat(actual, is(List.of(
"shardingsphere://capabilities",
"shardingsphere://guidance",
@@ -114,12 +114,6 @@ class ResourceDefinitionRegistryTest {
"shardingsphere://databases/{database}/schemas/{schema}/tables/{table}/indexes/{index}")));
}
- @Test
- void assertGetSupportedResourceDescriptors() {
- Collection<String> actual = ResourceDefinitionRegistry.getSupportedResourceDescriptors().stream().map(MCPResourceDescriptor::getUriTemplate).toList();
- assertThat(actual, is(ResourceDefinitionRegistry.getSupportedResources()));
- }
-
private static Stream<Arguments> getSupportedResourcesFailureCases() {
MCPResourceHandler<?> nullUriHandler = createResourceHandler(null);
MCPResourceHandler<?> emptyUriHandler = createResourceHandler("");
diff --git a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/resource/uri/MCPUriPatternTest.java b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/resource/uri/MCPUriPatternTest.java
index 32b83b8b9b5..21b619e8760 100644
--- a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/resource/uri/MCPUriPatternTest.java
+++ b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/resource/uri/MCPUriPatternTest.java
@@ -20,7 +20,6 @@ package org.apache.shardingsphere.mcp.core.resource.uri;
import org.apache.shardingsphere.mcp.api.capability.resource.MCPResourceURIVariables;
import org.junit.jupiter.api.Test;
-import java.util.List;
import java.util.Optional;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -48,12 +47,6 @@ class MCPUriPatternTest {
assertTrue(actual.isPresent());
}
- @Test
- void assertGetVariableNames() {
- MCPUriPattern actual = new MCPUriPattern("shardingsphere://databases/{database}/schemas/{schema}/tables/{table}/columns/{column}");
- assertThat(actual.getVariableNames(), is(List.of("database", "schema", "table", "column")));
- }
-
@Test
void assertParseWithVariables() {
Optional<MCPResourceURIVariables> actual = new MCPUriPattern(
@@ -103,6 +96,11 @@ class MCPUriPatternTest {
assertThrows(IllegalArgumentException.class, () -> new MCPUriPattern("invalid-template"));
}
+ @Test
+ void assertCreateWithEmbeddedVariable() {
+ assertThrows(IllegalArgumentException.class, () -> new MCPUriPattern("shardingsphere://databases/prefix-{database}"));
+ }
+
@Test
void assertGetVariableWithMissingVariable() {
Optional<MCPResourceURIVariables> actual = new MCPUriPattern("shardingsphere://capabilities").parse("shardingsphere://capabilities");
diff --git a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/MCPToolCallLimiterTest.java b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/MCPToolCallLimiterTest.java
index 96b0d2ceda5..16d46754fe4 100644
--- a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/MCPToolCallLimiterTest.java
+++ b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/MCPToolCallLimiterTest.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.mcp.core.tool;
import org.apache.shardingsphere.mcp.core.protocol.exception.MCPToolCallLimitExceededException;
-import org.apache.shardingsphere.mcp.support.security.MCPClientSafetyPolicy;
+import org.apache.shardingsphere.mcp.support.security.MCPRuntimeProtectionPolicy;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -69,9 +69,9 @@ class MCPToolCallLimiterTest {
}
private MCPToolCallLimiter createLimiter(final int maxToolCallsPerSession) {
- String previous = System.getProperty(MCPClientSafetyPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY);
+ String previous = System.getProperty(MCPRuntimeProtectionPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY);
try {
- System.setProperty(MCPClientSafetyPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY, String.valueOf(maxToolCallsPerSession));
+ System.setProperty(MCPRuntimeProtectionPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY, String.valueOf(maxToolCallsPerSession));
return new MCPToolCallLimiter();
} finally {
resetMaxToolCallsPerSessionProperty(previous);
@@ -80,9 +80,9 @@ class MCPToolCallLimiterTest {
private void resetMaxToolCallsPerSessionProperty(final String previous) {
if (null == previous) {
- System.clearProperty(MCPClientSafetyPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY);
+ System.clearProperty(MCPRuntimeProtectionPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY);
} else {
- System.setProperty(MCPClientSafetyPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY, previous);
+ System.setProperty(MCPRuntimeProtectionPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY, previous);
}
}
}
diff --git a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/MCPToolControllerTest.java b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/MCPToolControllerTest.java
index 0ac03546d6a..fd93fbe9998 100644
--- a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/MCPToolControllerTest.java
+++ b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/MCPToolControllerTest.java
@@ -30,7 +30,7 @@ import org.apache.shardingsphere.mcp.core.resource.ResourceTestDataFactory;
import org.apache.shardingsphere.mcp.core.session.MCPSessionExecutionCoordinator;
import org.apache.shardingsphere.mcp.core.tool.handler.MCPToolDefinition;
import org.apache.shardingsphere.mcp.core.tool.handler.ToolDefinitionRegistry;
-import org.apache.shardingsphere.mcp.support.security.MCPClientSafetyPolicy;
+import org.apache.shardingsphere.mcp.support.security.MCPRuntimeProtectionPolicy;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
@@ -170,9 +170,9 @@ class MCPToolControllerTest {
}
private MCPToolController createController(final int maxToolCallsPerSession) {
- String previous = System.getProperty(MCPClientSafetyPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY);
+ String previous = System.getProperty(MCPRuntimeProtectionPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY);
try {
- System.setProperty(MCPClientSafetyPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY, String.valueOf(maxToolCallsPerSession));
+ System.setProperty(MCPRuntimeProtectionPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY, String.valueOf(maxToolCallsPerSession));
return new MCPToolController(createRuntimeContext());
} finally {
resetMaxToolCallsPerSessionProperty(previous);
@@ -181,9 +181,9 @@ class MCPToolControllerTest {
private void resetMaxToolCallsPerSessionProperty(final String previous) {
if (null == previous) {
- System.clearProperty(MCPClientSafetyPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY);
+ System.clearProperty(MCPRuntimeProtectionPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY);
} else {
- System.setProperty(MCPClientSafetyPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY, previous);
+ System.setProperty(MCPRuntimeProtectionPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY, previous);
}
}
diff --git a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/ToolDefinitionRegistryTest.java b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/ToolDefinitionRegistryTest.java
index 9cd702f56e6..4526ba00b2b 100644
--- a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/ToolDefinitionRegistryTest.java
+++ b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/ToolDefinitionRegistryTest.java
@@ -47,7 +47,6 @@ import java.util.List;
import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -56,12 +55,6 @@ import static org.mockito.Mockito.mockStatic;
class ToolDefinitionRegistryTest {
- @Test
- void assertGetSupportedTools() {
- assertThat(ToolDefinitionRegistry.getSupportedTools(), contains("database_gateway_search_metadata", "database_gateway_validate_runtime_database", "database_gateway_execute_query",
- "database_gateway_execute_explain_query", "database_gateway_execute_update", "database_gateway_apply_workflow", "database_gateway_validate_workflow"));
- }
-
@Test
void assertGetSupportedToolDescriptors() {
List<MCPToolDescriptor> actual = ToolDefinitionRegistry.getSupportedToolDescriptors();
@@ -222,6 +215,17 @@ class ToolDefinitionRegistryTest {
assertThat(actual.getSuggestedArguments(), is(Map.of()));
}
+ @Test
+ void assertValidateWithInvalidNestedBoolean() {
+ MCPToolDescriptor descriptor = createNestedFixtureToolDescriptor();
+ MCPToolArgumentContractViolationException actual = assertThrows(MCPToolArgumentContractViolationException.class,
+ () -> new MCPToolArgumentContract(descriptor.getName(), descriptor.getInputSchema()).validate(Map.of("options", Map.of("enabled", "true"))));
+ assertThat(actual.getMessage(), is("options.enabled must be a boolean."));
+ assertThat(actual.getArgumentPath(), is("options.enabled"));
+ assertThat(actual.getCategory(), is("invalid_argument_type"));
+ assertThat(actual.getExpectedType(), is("boolean"));
+ }
+
@Test
void assertValidateWithSchemaAdditionalProperties() {
MCPToolDescriptor descriptor = createAdditionalPropertiesFixtureToolDescriptor();
@@ -236,12 +240,12 @@ class ToolDefinitionRegistryTest {
}
@Test
- void assertGetSupportedToolsWithNoToolHandlers() {
+ void assertGetSupportedToolDescriptorsWithNoToolHandlers() {
try (MockedStatic<ShardingSphereServiceLoader> mocked = mockStatic(ShardingSphereServiceLoader.class)) {
mocked.when(() -> ShardingSphereServiceLoader.getServiceInstances(MCPHandlerProvider.class)).thenReturn(Collections.emptyList());
Class<?> registryClass = assertDoesNotThrow(() -> Class.forName(ToolDefinitionRegistry.class.getName(), false, createIsolatedToolDefinitionRegistryClassLoader()));
InvocationTargetException actual = assertThrows(InvocationTargetException.class,
- () -> Plugins.getMemberAccessor().invoke(registryClass.getMethod("getSupportedTools"), null));
+ () -> Plugins.getMemberAccessor().invoke(registryClass.getMethod("getSupportedToolDescriptors"), null));
assertThat(actual.getCause().getClass(), is(ExceptionInInitializerError.class));
Throwable actualCause = actual.getCause().getCause();
assertThat(actualCause.getClass(), is(IllegalStateException.class));
@@ -254,7 +258,8 @@ class ToolDefinitionRegistryTest {
}
private static MCPToolDescriptor createNestedFixtureToolDescriptor() {
- Map<String, Object> optionSchema = Map.of("type", "object", "properties", Map.of("mode", Map.of("type", "string")), "required", List.of(), "additionalProperties", false);
+ Map<String, Object> optionSchema = Map.of("type", "object", "properties", Map.of("mode", Map.of("type", "string"), "enabled", Map.of("type", "boolean")),
+ "required", List.of(), "additionalProperties", false);
return new MCPToolDescriptor("fixture_tool", "Fixture Tool", "Fixture tool.", Map.of("type", "object", "properties", Map.of("options", optionSchema), "required", List.of(),
"additionalProperties", false), Collections.emptyMap(),
MCPToolAnnotations.builder()
diff --git a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/MCPSQLExecutionFacadeTest.java b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/MCPSQLExecutionFacadeTest.java
index a10508d1b39..4b45acd9837 100644
--- a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/MCPSQLExecutionFacadeTest.java
+++ b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/MCPSQLExecutionFacadeTest.java
@@ -25,7 +25,6 @@ import org.apache.shardingsphere.mcp.api.exception.MCPQueryFailedException;
import org.apache.shardingsphere.mcp.core.protocol.exception.MCPBannedSQLStatementException;
import org.apache.shardingsphere.mcp.core.session.MCPSessionExecutionCoordinator;
import org.apache.shardingsphere.mcp.core.session.MCPSessionNotExistedException;
-import org.apache.shardingsphere.mcp.core.tool.handler.execute.trace.SQLExecutionTraceFactory;
import org.apache.shardingsphere.mcp.support.database.capability.MCPDatabaseCapability;
import org.apache.shardingsphere.mcp.support.database.capability.MCPDatabaseCapabilityProvider;
import org.apache.shardingsphere.mcp.support.database.capability.SchemaExecutionSemantics;
@@ -68,13 +67,11 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory);
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor);
SQLExecutionRequest request = createExecutionRequest("SELECT 1");
when(coordinator.executeWithSessionLock(eq("session-1"), any())).thenThrow(new MCPSessionNotExistedException());
MCPSessionNotExistedException actual = assertThrows(MCPSessionNotExistedException.class, () -> facade.execute(request));
assertThat(actual.getMessage(), is("Session does not exist."));
- verify(traceFactory).create("session-1", "logic_db", "SELECT 1", false, "QUERY");
verifyNoInteractions(capabilityProvider, transactionExecutor, statementExecutor);
}
@@ -86,7 +83,6 @@ class MCPSQLExecutionFacadeTest {
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
MCPStatementAnalyzer statementAnalyzer = mock(MCPStatementAnalyzer.class);
- SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
SQLExecutionRequest request = createExecutionRequest("SELECT 1");
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.QUERY));
ClassificationResult classification = new ClassificationResult(SupportedMCPStatement.QUERY, "SELECT", "SELECT 1", "", List.of(), false);
@@ -96,14 +92,13 @@ class MCPSQLExecutionFacadeTest {
when(statementAnalyzer.analyze("SELECT 1", capability)).thenReturn(classification);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
when(statementExecutor.execute(request, classification, capability)).thenReturn(expectedResult);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, statementAnalyzer);
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, statementAnalyzer);
assertThat(facade.execute(request), is(expectedResult));
- verifyNoInteractions(capabilityProvider, transactionExecutor, statementExecutor, statementAnalyzer, traceFactory);
+ verifyNoInteractions(capabilityProvider, transactionExecutor, statementExecutor, statementAnalyzer);
assertThat(lockedExecution.getValue().get(), is(expectedResult));
verify(statementAnalyzer).analyze("SELECT 1", capability);
verify(capabilityProvider).provide("logic_db");
verify(statementExecutor).execute(request, classification, capability);
- verify(traceFactory).create("session-1", "logic_db", "SELECT 1", true, "QUERY");
verifyNoInteractions(transactionExecutor);
}
@@ -113,14 +108,12 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory);
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor);
SQLExecutionRequest request = createExecutionRequest("SELECT 1");
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.empty());
DatabaseCapabilityNotFoundException actual = assertThrows(DatabaseCapabilityNotFoundException.class, () -> facade.execute(request));
assertThat(actual.getMessage(), is("Database capability does not exist."));
- verify(traceFactory).create("session-1", "logic_db", "SELECT 1", false, "QUERY");
verifyNoInteractions(transactionExecutor, statementExecutor);
}
@@ -135,12 +128,10 @@ class MCPSQLExecutionFacadeTest {
when(statementAnalyzer.analyze(anyString(), any())).thenThrow(new MCPBannedSQLStatementException());
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, statementAnalyzer);
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, statementAnalyzer);
SQLExecutionRequest request = createExecutionRequest("SELECT 1");
MCPBannedSQLStatementException actual = assertThrows(MCPBannedSQLStatementException.class, () -> facade.execute(request));
assertThat(actual.getMessage(), is("Statement is banned by the MCP contract."));
- verify(traceFactory).create("session-1", "logic_db", "SELECT 1", false, "QUERY");
verifyNoInteractions(transactionExecutor, statementExecutor);
}
@@ -155,12 +146,10 @@ class MCPSQLExecutionFacadeTest {
when(statementAnalyzer.analyze(anyString(), any())).thenThrow(new IllegalArgumentException("Malformed SQL."));
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, statementAnalyzer);
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, statementAnalyzer);
SQLExecutionRequest request = createExecutionRequest("SELECT 1");
IllegalArgumentException actual = assertThrows(IllegalArgumentException.class, () -> facade.execute(request));
assertThat(actual.getMessage(), is("Malformed SQL."));
- verify(traceFactory).create("session-1", "logic_db", "SELECT 1", false, "QUERY");
verifyNoInteractions(transactionExecutor, statementExecutor);
}
@@ -170,17 +159,15 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
SQLExecutionRequest request = createExecutionRequest("SELECT 1");
ClassificationResult classification = new ClassificationResult(SupportedMCPStatement.QUERY, "SELECT", "SELECT 1", "",
List.of(SQLStatementObjectName.fromNormalizedName("orders")), false);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, createMCPStatementAnalyzer(classification));
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, createMCPStatementAnalyzer(classification));
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.DML));
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
StatementClassNotSupportedException actual = assertThrows(StatementClassNotSupportedException.class, () -> facade.execute(request));
assertThat(actual.getMessage(), is("Statement class is not supported."));
- verify(traceFactory).create("session-1", "logic_db", "SELECT 1", false, "QUERY");
verifyNoInteractions(transactionExecutor, statementExecutor);
}
@@ -196,13 +183,11 @@ class MCPSQLExecutionFacadeTest {
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
when(transactionExecutor.execute("session-1", "logic_db", capability, classification)).thenReturn(expectedResult);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, createMCPStatementAnalyzer(classification));
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, createMCPStatementAnalyzer(classification));
SQLExecutionRequest request = createExecutionRequest("BEGIN");
SQLExecutionResult actual = facade.execute(request);
assertThat(actual, is(expectedResult));
verify(transactionExecutor).execute("session-1", "logic_db", capability, classification);
- verify(traceFactory).create("session-1", "logic_db", "BEGIN", true, "BEGIN");
verifyNoInteractions(statementExecutor);
}
@@ -220,12 +205,10 @@ class MCPSQLExecutionFacadeTest {
SQLExecutionRequest request = createExecutionRequest("SELECT 1");
when(statementExecutor.execute(request, classification, capability)).thenReturn(expectedResult);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
- SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, createMCPStatementAnalyzer(classification));
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, createMCPStatementAnalyzer(classification));
SQLExecutionResult actual = facade.execute(request);
assertThat(actual, is(expectedResult));
verify(statementExecutor).execute(request, classification, capability);
- verify(traceFactory).create("session-1", "logic_db", "SELECT 1", true, "QUERY");
verifyNoInteractions(transactionExecutor);
}
@@ -235,17 +218,15 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.QUERY));
SQLExecutionRequest request = createExecutionRequest("SELECT * FROM other_db.orders");
ClassificationResult classification = new ClassificationResult(SupportedMCPStatement.QUERY, "SELECT", "SELECT * FROM other_db.orders", "",
List.of(SQLStatementObjectName.fromNormalizedName("other_db.orders")), false);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, createMCPStatementAnalyzer(classification));
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, createMCPStatementAnalyzer(classification));
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
MCPInvalidRequestException actual = assertThrows(MCPInvalidRequestException.class, () -> facade.execute(request));
assertThat(actual.getMessage(), is("Cross-schema SQL is not supported for database `logic_db`: `other_db.orders`."));
- verify(traceFactory).create("session-1", "logic_db", "SELECT * FROM other_db.orders", false, "QUERY");
verifyNoInteractions(transactionExecutor, statementExecutor);
}
@@ -255,34 +236,30 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.QUERY));
SQLExecutionRequest request = createExecutionRequest("SELECT * FROM logic_db.orders WHERE EXISTS (SELECT 1 FROM other_db.items)");
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory);
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor);
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
MCPInvalidRequestException actual = assertThrows(MCPInvalidRequestException.class, () -> facade.execute(request));
assertThat(actual.getMessage(), is("Cross-schema SQL is not supported for database `logic_db`: `other_db.items`."));
- verify(traceFactory).create("session-1", "logic_db", "SELECT * FROM logic_db.orders WHERE EXISTS (SELECT 1 FROM other_db.items)", false, "QUERY");
verifyNoInteractions(transactionExecutor, statementExecutor);
}
@ParameterizedTest(name = "{0}")
@MethodSource("assertExecuteWithCrossSchemaReferencesDisabledCases")
- void assertExecuteWithCrossSchemaReferencesDisabled(final String name, final String sql, final String expectedObjectName, final String expectedTraceStatementMarker) {
+ void assertExecuteWithCrossSchemaReferencesDisabled(final String name, final String sql, final String expectedObjectName) {
MCPDatabaseCapabilityProvider capabilityProvider = mock(MCPDatabaseCapabilityProvider.class);
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.QUERY, SupportedMCPStatement.DML, SupportedMCPStatement.DDL, SupportedMCPStatement.DCL));
SQLExecutionRequest request = createExecutionRequest(sql);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory);
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor);
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
MCPInvalidRequestException actual = assertThrows(MCPInvalidRequestException.class, () -> facade.execute(request));
assertThat(actual.getMessage(), is(String.format("Cross-schema SQL is not supported for database `logic_db`: `%s`.", expectedObjectName)));
- verify(traceFactory).create("session-1", "logic_db", sql, false, expectedTraceStatementMarker);
verifyNoInteractions(transactionExecutor, statementExecutor);
}
@@ -292,17 +269,15 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.DML));
SQLExecutionRequest request = createExecutionRequest("UPDATE other_db.orders SET status = 'DONE'");
ClassificationResult classification = new ClassificationResult(SupportedMCPStatement.DML, "UPDATE", "UPDATE other_db.orders SET status = 'DONE'", "",
List.of(SQLStatementObjectName.fromNormalizedName("other_db.orders")), false);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, createMCPStatementAnalyzer(classification));
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, createMCPStatementAnalyzer(classification));
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
MCPInvalidRequestException actual = assertThrows(MCPInvalidRequestException.class, () -> facade.execute(request));
assertThat(actual.getMessage(), is("Cross-schema SQL is not supported for database `logic_db`: `other_db.orders`."));
- verify(traceFactory).create("session-1", "logic_db", "UPDATE other_db.orders SET status = 'DONE'", false, "DML");
verifyNoInteractions(transactionExecutor, statementExecutor);
}
@@ -319,12 +294,10 @@ class MCPSQLExecutionFacadeTest {
SQLExecutionRequest request = createExecutionRequest(sql);
when(statementExecutor.execute(eq(request), any(), eq(capability))).thenReturn(expectedResult);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
- SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory);
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor);
SQLExecutionResult actual = facade.execute(request);
assertThat(actual, is(expectedResult));
verify(statementExecutor).execute(eq(request), any(), eq(capability));
- verify(traceFactory).create("session-1", "logic_db", sql, true, supportedStatement.name());
verifyNoInteractions(transactionExecutor);
}
@@ -339,8 +312,7 @@ class MCPSQLExecutionFacadeTest {
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
SQLExecutionRequest request = createExecutionRequest("SELECT * FROM Logic_DB.orders");
when(statementExecutor.execute(eq(request), any(), eq(capability))).thenReturn(expectedResult);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, mock(MCPJdbcTransactionStatementExecutor.class), statementExecutor,
- mock(SQLExecutionTraceFactory.class));
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, mock(MCPJdbcTransactionStatementExecutor.class), statementExecutor);
assertThat(facade.execute(request), is(expectedResult));
}
@@ -353,8 +325,7 @@ class MCPSQLExecutionFacadeTest {
when(capability.getIdentifierContext()).thenReturn(new DatabaseIdentifierContext(identifierCasePolicySet));
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, mock(MCPJdbcTransactionStatementExecutor.class), mock(MCPJdbcStatementExecutor.class),
- mock(SQLExecutionTraceFactory.class));
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, mock(MCPJdbcTransactionStatementExecutor.class), mock(MCPJdbcStatementExecutor.class));
MCPInvalidRequestException actual = assertThrows(MCPInvalidRequestException.class, () -> facade.execute(createExecutionRequest(sql)));
assertThat(actual.getMessage(), is("Cross-schema SQL is not supported for database `logic_db`: `Logic_DB.orders`."));
}
@@ -373,12 +344,10 @@ class MCPSQLExecutionFacadeTest {
SQLExecutionRequest request = createExecutionRequest("SELECT * FROM other_db.orders");
when(statementExecutor.execute(request, classification, capability)).thenReturn(expectedResult);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
- SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, createMCPStatementAnalyzer(classification));
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, createMCPStatementAnalyzer(classification));
SQLExecutionResult actual = facade.execute(request);
assertThat(actual, is(expectedResult));
verify(statementExecutor).execute(request, classification, capability);
- verify(traceFactory).create("session-1", "logic_db", "SELECT * FROM other_db.orders", true, "QUERY");
verifyNoInteractions(transactionExecutor);
}
@@ -395,11 +364,9 @@ class MCPSQLExecutionFacadeTest {
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
when(statementExecutor.execute(request, classification, capability)).thenThrow(new MCPInvalidRequestException("bad query"));
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
- SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, createMCPStatementAnalyzer(classification));
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, createMCPStatementAnalyzer(classification));
MCPInvalidRequestException actual = assertThrows(MCPInvalidRequestException.class, () -> facade.execute(request));
assertThat(actual.getMessage(), is("bad query"));
- verify(traceFactory).create("session-1", "logic_db", "SELECT 1", false, "QUERY");
verifyNoInteractions(transactionExecutor);
}
@@ -415,15 +382,13 @@ class MCPSQLExecutionFacadeTest {
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
when(statementExecutor.execute(eq(request), any(), eq(capability))).thenReturn(expectedResult);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
- SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory);
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor);
SQLExecutionResult actual = facade.executeExplain(request, "SELECT * FROM orders");
assertThat(actual, is(expectedResult));
ArgumentCaptor<ClassificationResult> classificationCaptor = ArgumentCaptor.forClass(ClassificationResult.class);
verify(statementExecutor).execute(eq(request), classificationCaptor.capture(), eq(capability));
assertThat(classificationCaptor.getValue().getStatementClass(), is(SupportedMCPStatement.EXPLAIN));
assertThat(classificationCaptor.getValue().getReferencedObjectNames(), contains("orders"));
- verify(traceFactory).create("session-1", "logic_db", "EXPLAIN SELECT * FROM orders", true, "EXPLAIN");
verifyNoInteractions(transactionExecutor);
}
@@ -438,14 +403,12 @@ class MCPSQLExecutionFacadeTest {
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
when(statementExecutor.execute(eq(request), any(), eq(capability))).thenThrow(executionFailure);
- SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, mock(MCPJdbcTransactionStatementExecutor.class), statementExecutor, traceFactory);
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, mock(MCPJdbcTransactionStatementExecutor.class), statementExecutor);
ExplainSQLSyntaxException actual = assertThrows(ExplainSQLSyntaxException.class, () -> facade.executeExplain(request, "SELECT * FROM orders"));
assertThat(actual.getDatabase(), is("logic_db"));
assertThat(actual.getSchema(), is("public"));
assertThat(actual.getSql(), is("SELECT * FROM orders"));
assertThat(actual.getExplainSql(), is("EXPLAIN BROKEN SELECT * FROM orders"));
- verify(traceFactory).create("session-1", "logic_db", "EXPLAIN BROKEN SELECT * FROM orders", false, "EXPLAIN");
}
@Test
@@ -459,21 +422,19 @@ class MCPSQLExecutionFacadeTest {
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
MCPQueryFailedException executionFailure = new MCPQueryFailedException("missing table", new SQLException("missing table", "42P01"));
when(statementExecutor.execute(eq(request), any(), eq(capability))).thenThrow(executionFailure);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, mock(MCPJdbcTransactionStatementExecutor.class), statementExecutor,
- mock(SQLExecutionTraceFactory.class));
+ MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, mock(MCPJdbcTransactionStatementExecutor.class), statementExecutor);
assertThat(assertThrows(MCPQueryFailedException.class, () -> facade.executeExplain(request, "SELECT * FROM missing_orders")), is(executionFailure));
}
private MCPSQLExecutionFacade createFacade(final MCPDatabaseCapabilityProvider capabilityProvider, final MCPSessionExecutionCoordinator coordinator,
- final MCPJdbcTransactionStatementExecutor transactionExecutor, final MCPJdbcStatementExecutor statementExecutor,
- final SQLExecutionTraceFactory traceFactory) {
- return createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, new MCPStatementAnalyzer());
+ final MCPJdbcTransactionStatementExecutor transactionExecutor, final MCPJdbcStatementExecutor statementExecutor) {
+ return createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, new MCPStatementAnalyzer());
}
private MCPSQLExecutionFacade createFacade(final MCPDatabaseCapabilityProvider capabilityProvider, final MCPSessionExecutionCoordinator coordinator,
final MCPJdbcTransactionStatementExecutor transactionExecutor, final MCPJdbcStatementExecutor statementExecutor,
- final SQLExecutionTraceFactory traceFactory, final MCPStatementAnalyzer statementAnalyzer) {
- return new MCPSQLExecutionFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, statementAnalyzer, traceFactory);
+ final MCPStatementAnalyzer statementAnalyzer) {
+ return new MCPSQLExecutionFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, statementAnalyzer);
}
@SuppressWarnings("unchecked")
@@ -502,33 +463,33 @@ class MCPSQLExecutionFacadeTest {
private static Stream<Arguments> assertExecuteWithCrossSchemaReferencesDisabledCases() {
return Stream.of(
- Arguments.of("query object list", "SELECT * FROM logic_db.orders, other_db.items", "other_db.items", "QUERY"),
- Arguments.of("cte object list", "WITH query_result AS (SELECT * FROM logic_db.orders, other_db.items) SELECT * FROM query_result", "other_db.items", "QUERY"),
- Arguments.of("unused cte reference", "WITH unused_result AS (SELECT * FROM other_db.items) SELECT * FROM logic_db.orders", "other_db.items", "QUERY"),
- Arguments.of("insert select object list", "INSERT INTO logic_db.orders_archive SELECT * FROM logic_db.orders, other_db.items", "other_db.items", "DML"),
- Arguments.of("update target object list", "UPDATE logic_db.orders o, other_db.items i SET o.status = 'DONE'", "other_db.items", "DML"),
- Arguments.of("delete target before from", "DELETE other_db.orders FROM logic_db.orders JOIN other_db.items ON 1 = 1", "other_db.orders", "DML"),
- Arguments.of("create view object list", "CREATE VIEW logic_db.active_orders AS SELECT * FROM logic_db.orders, other_db.items", "other_db.items", "DDL"),
- Arguments.of("create table like", "CREATE TABLE logic_db.orders_archive LIKE other_db.orders", "other_db.orders", "DDL"),
- Arguments.of("create table select without as", "CREATE TABLE logic_db.orders_archive SELECT * FROM other_db.orders", "other_db.orders", "DDL"),
- Arguments.of("create table parenthesized select", "CREATE TABLE logic_db.orders_archive AS (SELECT * FROM other_db.orders)", "other_db.orders", "DDL"),
- Arguments.of("create table table query", "CREATE TABLE logic_db.orders_archive AS TABLE other_db.orders", "other_db.orders", "DDL"),
- Arguments.of("create table quoted table query", "CREATE TABLE logic_db.orders_archive AS TABLE `other_db`.`orders`", "other_db.orders", "DDL"),
- Arguments.of("create table ANSI quoted table query", "CREATE TABLE logic_db.orders_archive AS TABLE \"other_db\".\"orders\"", "other_db.orders", "DDL"),
- Arguments.of("create table foreign key reference", "CREATE TABLE logic_db.order_items (order_id INT REFERENCES other_db.orders(id))", "other_db.orders", "DDL"),
- Arguments.of("alter table", "ALTER TABLE other_db.orders ADD COLUMN status VARCHAR(10)", "other_db.orders", "DDL"),
+ Arguments.of("query object list", "SELECT * FROM logic_db.orders, other_db.items", "other_db.items"),
+ Arguments.of("cte object list", "WITH query_result AS (SELECT * FROM logic_db.orders, other_db.items) SELECT * FROM query_result", "other_db.items"),
+ Arguments.of("unused cte reference", "WITH unused_result AS (SELECT * FROM other_db.items) SELECT * FROM logic_db.orders", "other_db.items"),
+ Arguments.of("insert select object list", "INSERT INTO logic_db.orders_archive SELECT * FROM logic_db.orders, other_db.items", "other_db.items"),
+ Arguments.of("update target object list", "UPDATE logic_db.orders o, other_db.items i SET o.status = 'DONE'", "other_db.items"),
+ Arguments.of("delete target before from", "DELETE other_db.orders FROM logic_db.orders JOIN other_db.items ON 1 = 1", "other_db.orders"),
+ Arguments.of("create view object list", "CREATE VIEW logic_db.active_orders AS SELECT * FROM logic_db.orders, other_db.items", "other_db.items"),
+ Arguments.of("create table like", "CREATE TABLE logic_db.orders_archive LIKE other_db.orders", "other_db.orders"),
+ Arguments.of("create table select without as", "CREATE TABLE logic_db.orders_archive SELECT * FROM other_db.orders", "other_db.orders"),
+ Arguments.of("create table parenthesized select", "CREATE TABLE logic_db.orders_archive AS (SELECT * FROM other_db.orders)", "other_db.orders"),
+ Arguments.of("create table table query", "CREATE TABLE logic_db.orders_archive AS TABLE other_db.orders", "other_db.orders"),
+ Arguments.of("create table quoted table query", "CREATE TABLE logic_db.orders_archive AS TABLE `other_db`.`orders`", "other_db.orders"),
+ Arguments.of("create table ANSI quoted table query", "CREATE TABLE logic_db.orders_archive AS TABLE \"other_db\".\"orders\"", "other_db.orders"),
+ Arguments.of("create table foreign key reference", "CREATE TABLE logic_db.order_items (order_id INT REFERENCES other_db.orders(id))", "other_db.orders"),
+ Arguments.of("alter table", "ALTER TABLE other_db.orders ADD COLUMN status VARCHAR(10)", "other_db.orders"),
Arguments.of("alter table foreign key reference",
- "ALTER TABLE logic_db.order_items ADD CONSTRAINT order_fk FOREIGN KEY (order_id) REFERENCES other_db.orders(id)", "other_db.orders", "DDL"),
- Arguments.of("grant table", "GRANT SELECT ON other_db.orders TO PUBLIC", "other_db.orders", "DCL"),
- Arguments.of("revoke table", "REVOKE SELECT ON other_db.orders FROM PUBLIC", "other_db.orders", "DCL"),
- Arguments.of("grant database wildcard", "GRANT SELECT ON other_db.* TO PUBLIC", "other_db.*", "DCL"),
- Arguments.of("grant global wildcard", "GRANT SELECT ON *.* TO PUBLIC", "*.*", "DCL"),
- Arguments.of("qualified function", "SELECT other_db.foo_refresh_orders()", "other_db.foo_refresh_orders", "QUERY"),
- Arguments.of("create database target", "CREATE DATABASE other_db", "other_db", "DDL"),
- Arguments.of("commented create database target", "/* guard */ CREATE DATABASE other_db", "other_db", "DDL"),
- Arguments.of("create schema target", "CREATE SCHEMA other_db", "other_db", "DDL"),
- Arguments.of("drop table object list", "DROP TABLE IF EXISTS logic_db.orders, other_db.items", "other_db.items", "DDL"),
- Arguments.of("truncate table", "TRUNCATE TABLE other_db.items", "other_db.items", "DDL"));
+ "ALTER TABLE logic_db.order_items ADD CONSTRAINT order_fk FOREIGN KEY (order_id) REFERENCES other_db.orders(id)", "other_db.orders"),
+ Arguments.of("grant table", "GRANT SELECT ON other_db.orders TO PUBLIC", "other_db.orders"),
+ Arguments.of("revoke table", "REVOKE SELECT ON other_db.orders FROM PUBLIC", "other_db.orders"),
+ Arguments.of("grant database wildcard", "GRANT SELECT ON other_db.* TO PUBLIC", "other_db.*"),
+ Arguments.of("grant global wildcard", "GRANT SELECT ON *.* TO PUBLIC", "*.*"),
+ Arguments.of("qualified function", "SELECT other_db.foo_refresh_orders()", "other_db.foo_refresh_orders"),
+ Arguments.of("create database target", "CREATE DATABASE other_db", "other_db"),
+ Arguments.of("commented create database target", "/* guard */ CREATE DATABASE other_db", "other_db"),
+ Arguments.of("create schema target", "CREATE SCHEMA other_db", "other_db"),
+ Arguments.of("drop table object list", "DROP TABLE IF EXISTS logic_db.orders, other_db.items", "other_db.items"),
+ Arguments.of("truncate table", "TRUNCATE TABLE other_db.items", "other_db.items"));
}
private static Stream<Arguments> assertExecuteWithNonMatchingCurrentDatabaseIdentifierCases() {
diff --git a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceFactoryTest.java b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceFactoryTest.java
deleted file mode 100644
index acfd1686d88..00000000000
--- a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceFactoryTest.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.mcp.core.tool.handler.execute.trace;
-
-import org.junit.jupiter.api.Test;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-
-class SQLExecutionTraceFactoryTest {
-
- @Test
- void assertCreateSuccessTrace() {
- SQLExecutionTraceRecord actual = new SQLExecutionTraceFactory().create("session-1", "logic_db", "SELECT * FROM orders", true, "QUERY");
- assertTrue(actual.isSuccess());
- assertThat(actual.getDatabase(), is("logic_db"));
- assertThat(actual.getSqlDigest().length(), is(64));
- }
-
- @Test
- void assertCreateFailureTrace() {
- SQLExecutionTraceRecord actual = new SQLExecutionTraceFactory().create("session-1", "logic_db", "SELECT * FROM orders", false, "QUERY");
- assertThat(actual.getStatementMarker(), is("QUERY"));
- assertFalse(actual.isSuccess());
- }
-}
diff --git a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/request/MCPToolArgumentsTest.java b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/request/MCPToolArgumentsTest.java
index 785849ad95e..d66d27168a5 100644
--- a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/request/MCPToolArgumentsTest.java
+++ b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/request/MCPToolArgumentsTest.java
@@ -54,12 +54,6 @@ class MCPToolArgumentsTest {
assertThat(new MCPToolArguments(rawArguments).getStringArgument("name"), is(expectedValue));
}
- @ParameterizedTest(name = "{0}")
- @MethodSource("getIntegerArgumentCases")
- void assertGetIntegerArgument(final String name, final Map<String, Object> rawArguments, final int defaultValue, final int expectedValue) {
- assertThat(new MCPToolArguments(rawArguments).getIntegerArgument("limit", defaultValue), is(expectedValue));
- }
-
@ParameterizedTest(name = "{0}")
@MethodSource("getBoundedIntegerArgumentCases")
void assertGetBoundedIntegerArgument(final String name, final Map<String, Object> rawArguments, final int expectedValue) {
@@ -104,15 +98,6 @@ class MCPToolArgumentsTest {
Arguments.of("number string", Map.of("name", 42), "42"));
}
- private static Stream<Arguments> getIntegerArgumentCases() {
- return Stream.of(
- Arguments.of("missing integer", Map.of(), 10, 10),
- Arguments.of("number integer", Map.of("limit", 5), 10, 5),
- Arguments.of("parsed integer", Map.of("limit", " 20 "), 10, 20),
- Arguments.of("blank integer", Map.of("limit", " "), 10, 10),
- Arguments.of("invalid integer", Map.of("limit", "foo"), 10, 10));
- }
-
private static Stream<Arguments> getBoundedIntegerArgumentCases() {
return Stream.of(
Arguments.of("missing bounded integer", Map.of(), 10),
diff --git a/mcp/features/broadcast/pom.xml b/mcp/features/broadcast/pom.xml
index 18d5af473f5..b899762f1cf 100644
--- a/mcp/features/broadcast/pom.xml
+++ b/mcp/features/broadcast/pom.xml
@@ -32,11 +32,6 @@
<artifactId>shardingsphere-mcp-support</artifactId>
<version>${project.version}</version>
</dependency>
- <dependency>
- <groupId>org.apache.shardingsphere</groupId>
- <artifactId>shardingsphere-broadcast-api</artifactId>
- <version>${project.version}</version>
- </dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-broadcast-distsql-parser</artifactId>
diff --git a/mcp/features/encrypt/src/main/java/org/apache/shardingsphere/mcp/feature/encrypt/tool/handler/PlanEncryptRuleToolHandler.java b/mcp/features/encrypt/src/main/java/org/apache/shardingsphere/mcp/feature/encrypt/tool/handler/PlanEncryptRuleToolHandler.java
index 0acdc45427a..a4bbad5a120 100644
--- a/mcp/features/encrypt/src/main/java/org/apache/shardingsphere/mcp/feature/encrypt/tool/handler/PlanEncryptRuleToolHandler.java
+++ b/mcp/features/encrypt/src/main/java/org/apache/shardingsphere/mcp/feature/encrypt/tool/handler/PlanEncryptRuleToolHandler.java
@@ -79,20 +79,12 @@ public final class PlanEncryptRuleToolHandler implements MCPToolHandler<MCPFeatu
}
private void applyStructuredIntentEvidence(final EncryptWorkflowRequest request, final Map<String, Object> structuredIntentEvidence) {
- request.getOptions().setRequiresDecrypt(getNullableBoolean(structuredIntentEvidence, WorkflowFieldNames.REQUIRES_DECRYPT));
- request.getOptions().setRequiresEqualityFilter(getNullableBoolean(structuredIntentEvidence, WorkflowFieldNames.REQUIRES_EQUALITY_FILTER));
- request.getOptions().setRequiresLikeQuery(getNullableBoolean(structuredIntentEvidence, WorkflowFieldNames.REQUIRES_LIKE_QUERY));
+ request.getOptions().setRequiresDecrypt((Boolean) structuredIntentEvidence.get(WorkflowFieldNames.REQUIRES_DECRYPT));
+ request.getOptions().setRequiresEqualityFilter((Boolean) structuredIntentEvidence.get(WorkflowFieldNames.REQUIRES_EQUALITY_FILTER));
+ request.getOptions().setRequiresLikeQuery((Boolean) structuredIntentEvidence.get(WorkflowFieldNames.REQUIRES_LIKE_QUERY));
Object fieldSemantics = structuredIntentEvidence.get(WorkflowFieldNames.FIELD_SEMANTICS);
if (null != fieldSemantics) {
- request.setFieldSemantics(String.valueOf(fieldSemantics).trim());
+ request.setFieldSemantics(((String) fieldSemantics).trim());
}
}
-
- private Boolean getNullableBoolean(final Map<String, Object> source, final String fieldName) {
- if (!source.containsKey(fieldName) || null == source.get(fieldName)) {
- return null;
- }
- Object rawValue = source.get(fieldName);
- return rawValue instanceof Boolean ? (Boolean) rawValue : Boolean.parseBoolean(String.valueOf(rawValue).trim());
- }
}
diff --git a/mcp/features/encrypt/src/main/java/org/apache/shardingsphere/mcp/feature/encrypt/tool/service/EncryptWorkflowPlanningService.java b/mcp/features/encrypt/src/main/java/org/apache/shardingsphere/mcp/feature/encrypt/tool/service/EncryptWorkflowPlanningService.java
index 190cdcbaf94..bbf14107d42 100644
--- a/mcp/features/encrypt/src/main/java/org/apache/shardingsphere/mcp/feature/encrypt/tool/service/EncryptWorkflowPlanningService.java
+++ b/mcp/features/encrypt/src/main/java/org/apache/shardingsphere/mcp/feature/encrypt/tool/service/EncryptWorkflowPlanningService.java
@@ -59,26 +59,17 @@ public final class EncryptWorkflowPlanningService {
private static final List<String> VALIDATION_LAYERS = List.of("rules");
- private final WorkflowPlanningSupport planningSupport;
+ private final WorkflowPlanningSupport planningSupport = new WorkflowPlanningSupport();
- private final EncryptWorkflowIntentResolver intentResolver;
+ private final EncryptWorkflowIntentResolver intentResolver = new EncryptWorkflowIntentResolver();
- private final EncryptRuleInspectionService ruleInspectionService;
+ private final EncryptRuleInspectionService ruleInspectionService = new EncryptRuleInspectionService();
- private final EncryptAlgorithmRecommendationService algorithmRecommendationService;
+ private final EncryptAlgorithmRecommendationService algorithmRecommendationService = new EncryptAlgorithmRecommendationService();
- private final EncryptAlgorithmPropertyTemplateService algorithmPropertyTemplateService;
+ private final EncryptAlgorithmPropertyTemplateService algorithmPropertyTemplateService = new EncryptAlgorithmPropertyTemplateService();
- private final EncryptRuleDistSQLPlanningService ruleDistSQLPlanningService;
-
- public EncryptWorkflowPlanningService() {
- planningSupport = new WorkflowPlanningSupport();
- intentResolver = new EncryptWorkflowIntentResolver();
- ruleInspectionService = new EncryptRuleInspectionService();
- algorithmRecommendationService = new EncryptAlgorithmRecommendationService();
- algorithmPropertyTemplateService = new EncryptAlgorithmPropertyTemplateService();
- ruleDistSQLPlanningService = new EncryptRuleDistSQLPlanningService();
- }
+ private final EncryptRuleDistSQLPlanningService ruleDistSQLPlanningService = new EncryptRuleDistSQLPlanningService();
/**
* Plan encrypt workflow.
diff --git a/mcp/features/mask/src/main/java/org/apache/shardingsphere/mcp/feature/mask/tool/service/MaskWorkflowPlanningService.java b/mcp/features/mask/src/main/java/org/apache/shardingsphere/mcp/feature/mask/tool/service/MaskWorkflowPlanningService.java
index 446435e2d60..74d5f1ce359 100644
--- a/mcp/features/mask/src/main/java/org/apache/shardingsphere/mcp/feature/mask/tool/service/MaskWorkflowPlanningService.java
+++ b/mcp/features/mask/src/main/java/org/apache/shardingsphere/mcp/feature/mask/tool/service/MaskWorkflowPlanningService.java
@@ -58,26 +58,17 @@ public final class MaskWorkflowPlanningService {
private static final List<String> VALIDATION_LAYERS = List.of("rules");
- private final WorkflowPlanningSupport planningSupport;
+ private final WorkflowPlanningSupport planningSupport = new WorkflowPlanningSupport();
- private final MaskWorkflowIntentResolver intentResolver;
+ private final MaskWorkflowIntentResolver intentResolver = new MaskWorkflowIntentResolver();
- private final MaskRuleInspectionService ruleInspectionService;
+ private final MaskRuleInspectionService ruleInspectionService = new MaskRuleInspectionService();
- private final MaskAlgorithmRecommendationService algorithmRecommendationService;
+ private final MaskAlgorithmRecommendationService algorithmRecommendationService = new MaskAlgorithmRecommendationService();
- private final MaskAlgorithmPropertyTemplateService algorithmPropertyTemplateService;
+ private final MaskAlgorithmPropertyTemplateService algorithmPropertyTemplateService = new MaskAlgorithmPropertyTemplateService();
- private final MaskRuleDistSQLPlanningService ruleDistSQLPlanningService;
-
- public MaskWorkflowPlanningService() {
- planningSupport = new WorkflowPlanningSupport();
- intentResolver = new MaskWorkflowIntentResolver();
- ruleInspectionService = new MaskRuleInspectionService();
- algorithmRecommendationService = new MaskAlgorithmRecommendationService();
- algorithmPropertyTemplateService = new MaskAlgorithmPropertyTemplateService();
- ruleDistSQLPlanningService = new MaskRuleDistSQLPlanningService();
- }
+ private final MaskRuleDistSQLPlanningService ruleDistSQLPlanningService = new MaskRuleDistSQLPlanningService();
/**
* Plan mask workflow.
diff --git a/mcp/features/mask/src/main/java/org/apache/shardingsphere/mcp/feature/mask/tool/service/MaskWorkflowValidationService.java b/mcp/features/mask/src/main/java/org/apache/shardingsphere/mcp/feature/mask/tool/service/MaskWorkflowValidationService.java
index d5e03fff9b4..cb7f69b50ae 100644
--- a/mcp/features/mask/src/main/java/org/apache/shardingsphere/mcp/feature/mask/tool/service/MaskWorkflowValidationService.java
+++ b/mcp/features/mask/src/main/java/org/apache/shardingsphere/mcp/feature/mask/tool/service/MaskWorkflowValidationService.java
@@ -57,15 +57,10 @@ public final class MaskWorkflowValidationService implements MCPWorkflowRuntimeHa
private final WorkflowValidationSupport validationSupport = new WorkflowValidationSupport();
- private final MaskRuleInspectionService ruleInspectionService;
+ private final MaskRuleInspectionService ruleInspectionService = new MaskRuleInspectionService();
- private final WorkflowSynchronizationSupport workflowSynchronizationSupport;
-
- public MaskWorkflowValidationService() {
- ruleInspectionService = new MaskRuleInspectionService();
- workflowSynchronizationSupport = new WorkflowSynchronizationSupport(
- WorkflowSynchronizationSupport.DEFAULT_SYNCHRONIZATION_WINDOW, WorkflowSynchronizationSupport.DEFAULT_POLL_INTERVAL);
- }
+ private final WorkflowSynchronizationSupport workflowSynchronizationSupport = new WorkflowSynchronizationSupport(
+ WorkflowSynchronizationSupport.DEFAULT_SYNCHRONIZATION_WINDOW, WorkflowSynchronizationSupport.DEFAULT_POLL_INTERVAL);
@Override
public Map<String, Object> validate(final WorkflowSessionContext workflowSessionContext, final MCPMetadataQueryFacade metadataQueryFacade,
diff --git a/mcp/features/readwrite-splitting/pom.xml b/mcp/features/readwrite-splitting/pom.xml
index f5d05bd9cbd..28d84d977e8 100644
--- a/mcp/features/readwrite-splitting/pom.xml
+++ b/mcp/features/readwrite-splitting/pom.xml
@@ -32,11 +32,6 @@
<artifactId>shardingsphere-mcp-support</artifactId>
<version>${project.version}</version>
</dependency>
- <dependency>
- <groupId>org.apache.shardingsphere</groupId>
- <artifactId>shardingsphere-readwrite-splitting-api</artifactId>
- <version>${project.version}</version>
- </dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-infra-algorithm-load-balancer-spi</artifactId>
diff --git a/mcp/features/readwrite-splitting/src/main/java/org/apache/shardingsphere/mcp/feature/readwritesplitting/tool/service/ReadwriteSplittingRuleWorkflowValidationService.java b/mcp/features/readwrite-splitting/src/main/java/org/apache/shardingsphere/mcp/feature/readwritesplitting/tool/service/ReadwriteSplittingRuleWorkflowValidationService.java
index 3ddc9ea1e14..9b6f329392f 100644
--- a/mcp/features/readwrite-splitting/src/main/java/org/apache/shardingsphere/mcp/feature/readwritesplitting/tool/service/ReadwriteSplittingRuleWorkflowValidationService.java
+++ b/mcp/features/readwrite-splitting/src/main/java/org/apache/shardingsphere/mcp/feature/readwritesplitting/tool/service/ReadwriteSplittingRuleWorkflowValidationService.java
@@ -53,15 +53,10 @@ public final class ReadwriteSplittingRuleWorkflowValidationService implements MC
private final WorkflowValidationSupport validationSupport = new WorkflowValidationSupport();
- private final ReadwriteSplittingInspectionService inspectionService;
+ private final ReadwriteSplittingInspectionService inspectionService = new ReadwriteSplittingInspectionService();
- private final WorkflowSynchronizationSupport workflowSynchronizationSupport;
-
- public ReadwriteSplittingRuleWorkflowValidationService() {
- inspectionService = new ReadwriteSplittingInspectionService();
- workflowSynchronizationSupport = new WorkflowSynchronizationSupport(
- WorkflowSynchronizationSupport.DEFAULT_SYNCHRONIZATION_WINDOW, WorkflowSynchronizationSupport.DEFAULT_POLL_INTERVAL);
- }
+ private final WorkflowSynchronizationSupport workflowSynchronizationSupport = new WorkflowSynchronizationSupport(
+ WorkflowSynchronizationSupport.DEFAULT_SYNCHRONIZATION_WINDOW, WorkflowSynchronizationSupport.DEFAULT_POLL_INTERVAL);
@Override
public Map<String, Object> validate(final WorkflowSessionContext workflowSessionContext, final MCPMetadataQueryFacade metadataQueryFacade,
diff --git a/mcp/features/readwrite-splitting/src/main/java/org/apache/shardingsphere/mcp/feature/readwritesplitting/tool/service/ReadwriteSplittingStatusWorkflowValidationService.java b/mcp/features/readwrite-splitting/src/main/java/org/apache/shardingsphere/mcp/feature/readwritesplitting/tool/service/ReadwriteSplittingStatusWorkflowValidationService.java
index e7afe4dd302..6544e7fbfbf 100644
--- a/mcp/features/readwrite-splitting/src/main/java/org/apache/shardingsphere/mcp/feature/readwritesplitting/tool/service/ReadwriteSplittingStatusWorkflowValidationService.java
+++ b/mcp/features/readwrite-splitting/src/main/java/org/apache/shardingsphere/mcp/feature/readwritesplitting/tool/service/ReadwriteSplittingStatusWorkflowValidationService.java
@@ -43,18 +43,12 @@ public final class ReadwriteSplittingStatusWorkflowValidationService implements
private final WorkflowValidationSupport validationSupport = new WorkflowValidationSupport();
- private final ReadwriteSplittingInspectionService inspectionService;
+ private final ReadwriteSplittingInspectionService inspectionService = new ReadwriteSplittingInspectionService();
- private final ReadwriteSplittingStatusDistSQLPlanningService distSQLPlanningService;
+ private final ReadwriteSplittingStatusDistSQLPlanningService distSQLPlanningService = new ReadwriteSplittingStatusDistSQLPlanningService();
- private final WorkflowSynchronizationSupport workflowSynchronizationSupport;
-
- public ReadwriteSplittingStatusWorkflowValidationService() {
- inspectionService = new ReadwriteSplittingInspectionService();
- distSQLPlanningService = new ReadwriteSplittingStatusDistSQLPlanningService();
- workflowSynchronizationSupport = new WorkflowSynchronizationSupport(
- WorkflowSynchronizationSupport.DEFAULT_SYNCHRONIZATION_WINDOW, WorkflowSynchronizationSupport.DEFAULT_POLL_INTERVAL);
- }
+ private final WorkflowSynchronizationSupport workflowSynchronizationSupport = new WorkflowSynchronizationSupport(
+ WorkflowSynchronizationSupport.DEFAULT_SYNCHRONIZATION_WINDOW, WorkflowSynchronizationSupport.DEFAULT_POLL_INTERVAL);
@Override
public Map<String, Object> validate(final WorkflowSessionContext workflowSessionContext, final MCPMetadataQueryFacade metadataQueryFacade,
diff --git a/mcp/features/shadow/src/main/java/org/apache/shardingsphere/mcp/feature/shadow/tool/service/ShadowWorkflowValidationService.java b/mcp/features/shadow/src/main/java/org/apache/shardingsphere/mcp/feature/shadow/tool/service/ShadowWorkflowValidationService.java
index 3e2ee52c432..7430af12f94 100644
--- a/mcp/features/shadow/src/main/java/org/apache/shardingsphere/mcp/feature/shadow/tool/service/ShadowWorkflowValidationService.java
+++ b/mcp/features/shadow/src/main/java/org/apache/shardingsphere/mcp/feature/shadow/tool/service/ShadowWorkflowValidationService.java
@@ -54,15 +54,10 @@ public final class ShadowWorkflowValidationService implements MCPWorkflowRuntime
private final WorkflowValidationSupport validationSupport = new WorkflowValidationSupport();
- private final ShadowInspectionService inspectionService;
+ private final ShadowInspectionService inspectionService = new ShadowInspectionService();
- private final WorkflowSynchronizationSupport workflowSynchronizationSupport;
-
- public ShadowWorkflowValidationService() {
- inspectionService = new ShadowInspectionService();
- workflowSynchronizationSupport = new WorkflowSynchronizationSupport(
- WorkflowSynchronizationSupport.DEFAULT_SYNCHRONIZATION_WINDOW, WorkflowSynchronizationSupport.DEFAULT_POLL_INTERVAL);
- }
+ private final WorkflowSynchronizationSupport workflowSynchronizationSupport = new WorkflowSynchronizationSupport(
+ WorkflowSynchronizationSupport.DEFAULT_SYNCHRONIZATION_WINDOW, WorkflowSynchronizationSupport.DEFAULT_POLL_INTERVAL);
@Override
public Map<String, Object> validate(final WorkflowSessionContext workflowSessionContext, final MCPMetadataQueryFacade metadataQueryFacade,
diff --git a/mcp/features/sharding/src/main/java/org/apache/shardingsphere/mcp/feature/sharding/tool/service/ShardingWorkflowValidationService.java b/mcp/features/sharding/src/main/java/org/apache/shardingsphere/mcp/feature/sharding/tool/service/ShardingWorkflowValidationService.java
index 523e4f52cf4..6d30318d42f 100644
--- a/mcp/features/sharding/src/main/java/org/apache/shardingsphere/mcp/feature/sharding/tool/service/ShardingWorkflowValidationService.java
+++ b/mcp/features/sharding/src/main/java/org/apache/shardingsphere/mcp/feature/sharding/tool/service/ShardingWorkflowValidationService.java
@@ -56,15 +56,10 @@ public final class ShardingWorkflowValidationService implements MCPWorkflowRunti
private final WorkflowValidationSupport validationSupport = new WorkflowValidationSupport();
- private final ShardingInspectionService inspectionService;
+ private final ShardingInspectionService inspectionService = new ShardingInspectionService();
- private final WorkflowSynchronizationSupport workflowSynchronizationSupport;
-
- public ShardingWorkflowValidationService() {
- inspectionService = new ShardingInspectionService();
- workflowSynchronizationSupport = new WorkflowSynchronizationSupport(
- WorkflowSynchronizationSupport.DEFAULT_SYNCHRONIZATION_WINDOW, WorkflowSynchronizationSupport.DEFAULT_POLL_INTERVAL);
- }
+ private final WorkflowSynchronizationSupport workflowSynchronizationSupport = new WorkflowSynchronizationSupport(
+ WorkflowSynchronizationSupport.DEFAULT_SYNCHRONIZATION_WINDOW, WorkflowSynchronizationSupport.DEFAULT_POLL_INTERVAL);
@Override
public Map<String, Object> validate(final WorkflowSessionContext workflowSessionContext, final MCPMetadataQueryFacade metadataQueryFacade,
diff --git a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorCatalogIndex.java b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorCatalogIndex.java
index 2bec9150d36..7b73e542e89 100644
--- a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorCatalogIndex.java
+++ b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorCatalogIndex.java
@@ -116,7 +116,7 @@ public final class MCPDescriptorCatalogIndex {
private static Map<String, Collection<String>> createWorkflowKindsByPromptName() {
Map<String, Collection<String>> result = new LinkedHashMap<>(PROMPT_DESCRIPTORS.size(), 1F);
for (MCPPromptDescriptor each : PROMPT_DESCRIPTORS) {
- Collection<String> workflowKinds = findPromptPlanningWorkflowKind(each);
+ Collection<String> workflowKinds = findPromptPlanningWorkflowKinds(each);
if (!workflowKinds.isEmpty()) {
result.put(each.getName(), workflowKinds);
}
@@ -124,25 +124,34 @@ public final class MCPDescriptorCatalogIndex {
return result;
}
- private static Collection<String> findPromptPlanningWorkflowKind(final MCPPromptDescriptor prompt) {
- String planningToolName = "database_gateway_" + prompt.getName();
+ private static Collection<String> findPromptPlanningWorkflowKinds(final MCPPromptDescriptor prompt) {
Object relatedTools = prompt.getMeta().get(MCPShardingSphereMetadataKeys.RELATED_TOOLS);
- MCPToolDescriptor toolDescriptor = TOOL_DESCRIPTORS.get(planningToolName);
if (!(relatedTools instanceof Collection<?>)) {
return List.of();
}
- if (!((Collection<?>) relatedTools).contains(planningToolName) || null == toolDescriptor || !isPlanningTool(planningToolName)) {
+ String planningToolName = "";
+ for (Object each : (Collection<?>) relatedTools) {
+ String toolName = Objects.toString(each, "");
+ MCPToolRuntimeDescriptor runtimeDescriptor = TOOL_RUNTIME_DESCRIPTORS.get(toolName);
+ if (null == runtimeDescriptor) {
+ return List.of();
+ }
+ if (!"plan".equals(runtimeDescriptor.getWorkflowRole())) {
+ continue;
+ }
+ if (!planningToolName.isEmpty()) {
+ return List.of();
+ }
+ planningToolName = toolName;
+ }
+ MCPToolDescriptor toolDescriptor = TOOL_DESCRIPTORS.get(planningToolName);
+ if (null == toolDescriptor) {
return List.of();
}
String workflowKind = Objects.toString(toolDescriptor.getMeta().get(MCPShardingSphereMetadataKeys.WORKFLOW_KIND), "");
return workflowKind.isEmpty() ? List.of() : List.of(workflowKind);
}
- private static boolean isPlanningTool(final String toolName) {
- MCPToolRuntimeDescriptor runtimeDescriptor = TOOL_RUNTIME_DESCRIPTORS.get(toolName);
- return null != runtimeDescriptor && "plan".equals(runtimeDescriptor.getWorkflowRole());
- }
-
/**
* Get resource descriptors.
*
diff --git a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorCatalogYamlSwapper.java b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorCatalogYamlSwapper.java
index aeb0d6785bb..98800958ca1 100644
--- a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorCatalogYamlSwapper.java
+++ b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorCatalogYamlSwapper.java
@@ -38,7 +38,7 @@ import org.apache.shardingsphere.mcp.support.descriptor.yaml.YamlMCPToolDescript
import org.apache.shardingsphere.mcp.support.descriptor.yaml.YamlMCPToolRuntimeDescriptor;
import org.apache.shardingsphere.mcp.support.descriptor.yaml.YamlMCPUriVariableDescriptor;
import org.apache.shardingsphere.mcp.support.descriptor.yaml.YamlShardingSphereMCPResourceMetadata;
-import org.apache.shardingsphere.mcp.support.yaml.MCPYamlConfigurationValidator;
+import org.apache.shardingsphere.mcp.support.configuration.MCPConfigurationValidator;
import java.util.Collection;
import java.util.Collections;
@@ -61,7 +61,7 @@ final class MCPDescriptorCatalogYamlSwapper {
Collection<MCPResourceNavigationDescriptor> resourceNavigationDescriptors = new LinkedList<>();
Collection<MCPToolRuntimeDescriptor> toolRuntimeDescriptors = new LinkedList<>();
for (YamlMCPDescriptorCatalog each : yamlCatalogs) {
- MCPYamlConfigurationValidator.validate(each, "MCP descriptor catalog");
+ MCPConfigurationValidator.validate(each, "MCP descriptor catalog");
swapFixedResourceDescriptors(each.getResources(), resourceDescriptors, shardingSphereResourceMetadata);
swapResourceTemplateDescriptors(each.getResourceTemplates(), resourceTemplateDescriptors, shardingSphereResourceMetadata);
toolDescriptors.addAll(swapToolDescriptors(each.getTools()));
diff --git a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPResourceNavigationPayloadBuilder.java b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPResourceNavigationPayloadBuilder.java
index 81d091894b5..905407d78ae 100644
--- a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPResourceNavigationPayloadBuilder.java
+++ b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPResourceNavigationPayloadBuilder.java
@@ -59,8 +59,10 @@ public final class MCPResourceNavigationPayloadBuilder {
new MCPUriTemplate(descriptor.getUriTemplate()).expandIfComplete(uriVariables)
.ifPresent(optional -> result.put(MCPPayloadFieldNames.SELF_RESOURCE, MCPResourceHintUtils.create(optional, resolveResourceKind(optional), "inspect_self",
"Read this resource.", MCPPayloadFieldNames.SELF_RESOURCE)));
- Optional<String> parentUri = new MCPUriTemplate(parentUriTemplate).expandIfComplete(uriVariables);
- parentUri.filter(optional -> !optional.isEmpty()).ifPresent(optional -> result.put(MCPPayloadFieldNames.PARENT_RESOURCE, createParentResourceHint(optional)));
+ if (!parentUriTemplate.isEmpty()) {
+ Optional<String> parentUri = new MCPUriTemplate(parentUriTemplate).expandIfComplete(uriVariables);
+ parentUri.ifPresent(optional -> result.put(MCPPayloadFieldNames.PARENT_RESOURCE, createParentResourceHint(optional)));
+ }
return result;
}
diff --git a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPToolDescriptorCatalogValidator.java b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPToolDescriptorCatalogValidator.java
index e5c32ff13c3..8948b9aaa78 100644
--- a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPToolDescriptorCatalogValidator.java
+++ b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPToolDescriptorCatalogValidator.java
@@ -96,6 +96,10 @@ public final class MCPToolDescriptorCatalogValidator {
() -> new IllegalStateException(String.format("Tool `%s` inputSchema must be an object.", descriptor.getName())));
Object properties = inputSchema.get("properties");
ShardingSpherePreconditions.checkState(properties instanceof Map, () -> new IllegalStateException(String.format("Tool `%s` inputSchema must declare properties.", descriptor.getName())));
+ ShardingSpherePreconditions.checkState(inputSchema.get("required") instanceof Collection,
+ () -> new IllegalStateException(String.format("Tool `%s` inputSchema required must be an array.", descriptor.getName())));
+ ShardingSpherePreconditions.checkState(inputSchema.get("additionalProperties") instanceof Boolean,
+ () -> new IllegalStateException(String.format("Tool `%s` inputSchema additionalProperties must be a boolean.", descriptor.getName())));
validateNestedInputSchemaFields(descriptor, inputSchema, "inputSchema");
MCPToolDescriptorValidationUtils.validateModelFacingSchemaFields(descriptor, inputSchema);
}
diff --git a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/resource/MCPUriTemplate.java b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/resource/MCPUriTemplate.java
index cf20210e32c..0938364167b 100644
--- a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/resource/MCPUriTemplate.java
+++ b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/resource/MCPUriTemplate.java
@@ -18,9 +18,12 @@
package org.apache.shardingsphere.mcp.support.resource;
import org.apache.shardingsphere.mcp.api.capability.resource.MCPResourceURIVariables;
+import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
-import java.util.LinkedList;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -30,33 +33,96 @@ import java.util.regex.Pattern;
*/
public final class MCPUriTemplate {
- private static final Pattern VARIABLE_PATTERN = Pattern.compile("\\{([^}]+)}");
+ private static final String SCHEME_SEPARATOR = "://";
+
+ private static final String SCHEME = "shardingsphere";
private final String uriTemplate;
+ private final List<String> pathSegments;
+
private final List<String> variableNames;
+ private final Pattern compiledRegex;
+
public MCPUriTemplate(final String uriTemplate) {
this.uriTemplate = uriTemplate;
- variableNames = extractVariableNames(uriTemplate);
+ int schemeSeparatorIndex = findSchemeSeparatorIndex(uriTemplate);
+ validateScheme(uriTemplate, schemeSeparatorIndex);
+ pathSegments = extractPathSegments(uriTemplate, schemeSeparatorIndex + SCHEME_SEPARATOR.length());
+ variableNames = extractVariableNames(uriTemplate, pathSegments);
+ compiledRegex = compileRegex(pathSegments);
}
- private static List<String> extractVariableNames(final String uriTemplate) {
- List<String> result = new LinkedList<>();
- Matcher matcher = VARIABLE_PATTERN.matcher(uriTemplate);
- while (matcher.find()) {
- result.add(matcher.group(1));
+ private int findSchemeSeparatorIndex(final String uri) {
+ int result = uri.indexOf(SCHEME_SEPARATOR);
+ ShardingSpherePreconditions.checkState(0 < result, () -> new IllegalArgumentException(String.format("Invalid URI `%s`.", uri)));
+ return result;
+ }
+
+ private void validateScheme(final String uri, final int schemeSeparatorIndex) {
+ String scheme = uri.substring(0, schemeSeparatorIndex);
+ ShardingSpherePreconditions.checkState(SCHEME.equals(scheme), () -> new IllegalArgumentException(String.format("Invalid URI `%s`.", uri)));
+ }
+
+ private List<String> extractPathSegments(final String uri, final int pathStartIndex) {
+ String path = uri.substring(pathStartIndex);
+ ShardingSpherePreconditions.checkNotEmpty(path, () -> new IllegalArgumentException(String.format("URI path is required for `%s`.", uri)));
+ String[] segments = path.split("/", -1);
+ List<String> result = new ArrayList<>(segments.length);
+ for (String each : segments) {
+ ShardingSpherePreconditions.checkNotEmpty(each, () -> new IllegalArgumentException(String.format("URI segment is required for `%s`.", uri)));
+ ShardingSpherePreconditions.checkState(isVariableSegment(each) || !containsTemplateMarker(each),
+ () -> new IllegalArgumentException(String.format("URI template variables must occupy a complete path segment in `%s`.", uri)));
+ result.add(each);
}
return result;
}
+ private List<String> extractVariableNames(final String template, final List<String> segments) {
+ List<String> result = new ArrayList<>(segments.size());
+ for (String each : segments) {
+ if (!isVariableSegment(each)) {
+ continue;
+ }
+ String variableName = each.substring(1, each.length() - 1);
+ ShardingSpherePreconditions.checkNotEmpty(variableName, () -> new IllegalArgumentException(String.format("URI template variable is required in `%s`.", template)));
+ ShardingSpherePreconditions.checkState(!containsTemplateMarker(variableName),
+ () -> new IllegalArgumentException(String.format("Invalid URI template variable `%s` in `%s`.", variableName, template)));
+ ShardingSpherePreconditions.checkState(!result.contains(variableName),
+ () -> new IllegalArgumentException(String.format("Duplicate URI template variable `%s` in `%s`.", variableName, template)));
+ result.add(variableName);
+ }
+ return result;
+ }
+
+ private Pattern compileRegex(final List<String> segments) {
+ StringBuilder regex = new StringBuilder("^").append(Pattern.quote(SCHEME)).append(SCHEME_SEPARATOR);
+ for (int i = 0; i < segments.size(); i++) {
+ if (0 < i) {
+ regex.append('/');
+ }
+ String each = segments.get(i);
+ regex.append(isVariableSegment(each) ? "([^/]+)" : Pattern.quote(each));
+ }
+ return Pattern.compile(regex.append('$').toString());
+ }
+
+ private boolean isVariableSegment(final String pathSegment) {
+ return 2 < pathSegment.length() && pathSegment.startsWith("{") && pathSegment.endsWith("}");
+ }
+
+ private boolean containsTemplateMarker(final String value) {
+ return value.contains("{") || value.contains("}");
+ }
+
/**
* Get URI template variable names.
*
* @return variable names
*/
public List<String> getVariableNames() {
- return new LinkedList<>(variableNames);
+ return new ArrayList<>(variableNames);
}
/**
@@ -80,4 +146,50 @@ public final class MCPUriTemplate {
}
return result;
}
+
+ /**
+ * Parse URI variables using this template.
+ *
+ * @param uri URI
+ * @return parsed variables when the URI matches
+ */
+ public Optional<MCPResourceURIVariables> parse(final String uri) {
+ Matcher matcher = compiledRegex.matcher(uri);
+ if (!matcher.matches()) {
+ return Optional.empty();
+ }
+ Map<String, String> variables = new LinkedHashMap<>(variableNames.size(), 1F);
+ for (int i = 0; i < variableNames.size(); i++) {
+ String encodedValue = matcher.group(i + 1);
+ if (containsTemplateMarker(encodedValue)) {
+ return Optional.empty();
+ }
+ Optional<String> decodedValue = MCPUriPathSegmentUtils.decodePathSegment(encodedValue);
+ if (decodedValue.isEmpty()) {
+ return Optional.empty();
+ }
+ variables.put(variableNames.get(i), decodedValue.get());
+ }
+ return Optional.of(new MCPResourceURIVariables(variables));
+ }
+
+ /**
+ * Determine whether this template overlaps another template.
+ *
+ * @param other other template
+ * @return whether the templates overlap
+ */
+ public boolean overlaps(final MCPUriTemplate other) {
+ if (pathSegments.size() != other.pathSegments.size()) {
+ return false;
+ }
+ for (int i = 0; i < pathSegments.size(); i++) {
+ String left = pathSegments.get(i);
+ String right = other.pathSegments.get(i);
+ if (!isVariableSegment(left) && !isVariableSegment(right) && !left.equals(right)) {
+ return false;
+ }
+ }
+ return true;
+ }
}
diff --git a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/security/MCPClientSafetyPolicy.java b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/security/MCPClientSafetyPolicy.java
index a61c3cf10fb..5f65c923ee1 100644
--- a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/security/MCPClientSafetyPolicy.java
+++ b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/security/MCPClientSafetyPolicy.java
@@ -29,19 +29,6 @@ import java.util.Map;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class MCPClientSafetyPolicy {
- public static final int DEFAULT_MAX_TOOL_CALLS_PER_SESSION = MCPRuntimeProtectionPolicy.DEFAULT_MAX_TOOL_CALLS_PER_SESSION;
-
- public static final String MAX_TOOL_CALLS_PER_SESSION_PROPERTY = MCPRuntimeProtectionPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY;
-
- /**
- * Get maximum tool calls per MCP session.
- *
- * @return maximum tool calls per MCP session
- */
- public static int getMaxToolCallsPerSession() {
- return MCPRuntimeProtectionPolicy.getMaxToolCallsPerSession();
- }
-
/**
* Create model-facing safety policy payload.
*
diff --git a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/security/MCPRuntimeProtectionPolicy.java b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/security/MCPRuntimeProtectionPolicy.java
index c47d345ec96..07311b29d88 100644
--- a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/security/MCPRuntimeProtectionPolicy.java
+++ b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/security/MCPRuntimeProtectionPolicy.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.mcp.support.security;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.mcp.support.protocol.MCPPayloadFieldNames;
import java.util.LinkedHashMap;
@@ -52,8 +53,7 @@ public final class MCPRuntimeProtectionPolicy {
* @return maximum tool calls per MCP session
*/
public static int getMaxToolCallsPerSession() {
- Integer configuredValue = Integer.getInteger(MAX_TOOL_CALLS_PER_SESSION_PROPERTY, DEFAULT_MAX_TOOL_CALLS_PER_SESSION);
- return configuredValue > 0 ? configuredValue : DEFAULT_MAX_TOOL_CALLS_PER_SESSION;
+ return getPositiveIntegerProperty(MAX_TOOL_CALLS_PER_SESSION_PROPERTY, DEFAULT_MAX_TOOL_CALLS_PER_SESSION);
}
/**
@@ -62,8 +62,23 @@ public final class MCPRuntimeProtectionPolicy {
* @return maximum completion requests per minute
*/
public static int getMaxCompletionRequestsPerMinute() {
- Integer configuredValue = Integer.getInteger(MAX_COMPLETION_REQUESTS_PER_MINUTE_PROPERTY, DEFAULT_MAX_COMPLETION_REQUESTS_PER_MINUTE);
- return configuredValue > 0 ? configuredValue : DEFAULT_MAX_COMPLETION_REQUESTS_PER_MINUTE;
+ return getPositiveIntegerProperty(MAX_COMPLETION_REQUESTS_PER_MINUTE_PROPERTY, DEFAULT_MAX_COMPLETION_REQUESTS_PER_MINUTE);
+ }
+
+ private static int getPositiveIntegerProperty(final String propertyName, final int defaultValue) {
+ String configuredValue = System.getProperty(propertyName);
+ if (null == configuredValue) {
+ return defaultValue;
+ }
+ int result;
+ try {
+ result = Integer.parseInt(configuredValue);
+ } catch (final NumberFormatException ex) {
+ throw new IllegalArgumentException(String.format("System property `%s` must be a positive integer, but was `%s`.", propertyName, configuredValue), ex);
+ }
+ ShardingSpherePreconditions.checkState(result > 0,
+ () -> new IllegalArgumentException(String.format("System property `%s` must be a positive integer, but was `%s`.", propertyName, configuredValue)));
+ return result;
}
/**
diff --git a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/yaml/MCPYamlConfigurationValidator.java b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/yaml/MCPYamlConfigurationValidator.java
deleted file mode 100644
index a28a31a8607..00000000000
--- a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/yaml/MCPYamlConfigurationValidator.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.mcp.support.yaml;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.mcp.support.configuration.MCPConfigurationValidator;
-
-/**
- * MCP YAML configuration validator.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class MCPYamlConfigurationValidator {
-
- /**
- * Validate MCP YAML configuration.
- *
- * @param yamlConfig YAML configuration
- * @param configName configuration name
- */
- public static void validate(final Object yamlConfig, final String configName) {
- MCPConfigurationValidator.validate(yamlConfig, configName);
- }
-}
diff --git a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorCatalogIndexTest.java b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorCatalogIndexTest.java
index 819d2be5271..69e4e2fc615 100644
--- a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorCatalogIndexTest.java
+++ b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorCatalogIndexTest.java
@@ -114,6 +114,12 @@ class MCPDescriptorCatalogIndexTest {
assertTrue(MCPDescriptorCatalogIndex.findWorkflowKindsByCompletionTarget(descriptor).isEmpty());
}
+ @Test
+ void assertFindWorkflowKindsByPlanningPromptCompletionTarget() {
+ MCPCompletionTargetDescriptor descriptor = new MCPCompletionTargetDescriptor("prompt", "configure_encryption", List.of("database"), 50, Map.of());
+ assertThat(MCPDescriptorCatalogIndex.findWorkflowKindsByCompletionTarget(descriptor), is(List.of("encrypt.rule")));
+ }
+
@Test
void assertFindWorkflowKindsByResourceCompletionTarget() {
MCPCompletionTargetDescriptor descriptor = new MCPCompletionTargetDescriptor("resource", "shardingsphere://workflows/{plan_id}", List.of("plan_id"), 50, Map.of());
diff --git a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorComponentValidatorsTest.java b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorComponentValidatorsTest.java
index f546f84f987..c70565e4cc2 100644
--- a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorComponentValidatorsTest.java
+++ b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorComponentValidatorsTest.java
@@ -25,6 +25,8 @@ import org.apache.shardingsphere.mcp.api.capability.resource.MCPResourceDescript
import org.apache.shardingsphere.mcp.api.capability.tool.MCPToolAnnotations;
import org.apache.shardingsphere.mcp.api.capability.tool.MCPToolDescriptor;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
import java.util.LinkedHashMap;
import java.util.List;
@@ -53,6 +55,37 @@ class MCPDescriptorComponentValidatorsTest {
assertThat(actual.getMessage(), is("Tool `database_gateway_test_tool` inputSchema contains unsupported top-level field `oneOf`."));
}
+ @ParameterizedTest(name = "{0}")
+ @ValueSource(strings = {"$defs", "definitions"})
+ void assertToolDescriptorCatalogValidatorWithUnsupportedDefinitionField(final String fieldName) {
+ Map<String, Object> inputSchema = new LinkedHashMap<>(createInputSchema());
+ inputSchema.put(fieldName, Map.of("query", Map.of("type", "string")));
+ MCPDescriptorCatalog catalog = createCatalog(List.of(), List.of(), List.of(new MCPToolDescriptor(
+ "database_gateway_test_tool", "Test Tool", "Run a test tool.", inputSchema, createOutputSchema(), createAnnotations(), Map.of())));
+ IllegalStateException actual = assertThrows(IllegalStateException.class, () -> MCPToolDescriptorCatalogValidator.validate(catalog));
+ assertThat(actual.getMessage(), is(String.format("Tool `database_gateway_test_tool` inputSchema contains unsupported top-level field `%s`.", fieldName)));
+ }
+
+ @Test
+ void assertToolDescriptorCatalogValidatorWithUnsupportedReferenceField() {
+ Map<String, Object> inputSchema = new LinkedHashMap<>(createInputSchema());
+ inputSchema.put("properties", Map.of("query", Map.of("$ref", "#/$defs/query", "description", "Query.")));
+ MCPDescriptorCatalog catalog = createCatalog(List.of(), List.of(), List.of(new MCPToolDescriptor(
+ "database_gateway_test_tool", "Test Tool", "Run a test tool.", inputSchema, createOutputSchema(), createAnnotations(), Map.of())));
+ IllegalStateException actual = assertThrows(IllegalStateException.class, () -> MCPToolDescriptorCatalogValidator.validate(catalog));
+ assertThat(actual.getMessage(), is("Tool `database_gateway_test_tool` inputSchema at `inputSchema.properties.query` contains unsupported field `$ref`."));
+ }
+
+ @Test
+ void assertToolDescriptorCatalogValidatorWithObjectAdditionalProperties() {
+ Map<String, Object> inputSchema = new LinkedHashMap<>(createInputSchema());
+ inputSchema.put("additionalProperties", Map.of("type", "string"));
+ MCPDescriptorCatalog catalog = createCatalog(List.of(), List.of(), List.of(new MCPToolDescriptor(
+ "database_gateway_test_tool", "Test Tool", "Run a test tool.", inputSchema, createOutputSchema(), createAnnotations(), Map.of())));
+ IllegalStateException actual = assertThrows(IllegalStateException.class, () -> MCPToolDescriptorCatalogValidator.validate(catalog));
+ assertThat(actual.getMessage(), is("Tool `database_gateway_test_tool` inputSchema additionalProperties must be a boolean."));
+ }
+
@Test
void assertToolOutputSchemaValidator() {
MCPToolDescriptor descriptor = new MCPToolDescriptor("database_gateway_test_tool", "Test Tool", "Run a test tool.",
diff --git a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/resource/MCPUriTemplateTest.java b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/resource/MCPUriTemplateTest.java
index 3b04ad12ccb..523fda216be 100644
--- a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/resource/MCPUriTemplateTest.java
+++ b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/resource/MCPUriTemplateTest.java
@@ -26,6 +26,9 @@ import java.util.Optional;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
class MCPUriTemplateTest {
@@ -48,4 +51,25 @@ class MCPUriTemplateTest {
.expandIfComplete(new MCPResourceURIVariables(Map.of("database", "logic_db")));
assertThat(actualUri, is(Optional.empty()));
}
+
+ @Test
+ void assertParse() {
+ Optional<MCPResourceURIVariables> actual = new MCPUriTemplate("shardingsphere://databases/{database}").parse("shardingsphere://databases/logic%20db");
+ assertThat(actual.orElseThrow().getValue("database"), is("logic db"));
+ }
+
+ @Test
+ void assertParseWithUnexpandedVariable() {
+ assertFalse(new MCPUriTemplate("shardingsphere://databases/{database}").parse("shardingsphere://databases/{database}").isPresent());
+ }
+
+ @Test
+ void assertOverlaps() {
+ assertTrue(new MCPUriTemplate("shardingsphere://databases/{database}").overlaps(new MCPUriTemplate("shardingsphere://databases/logic_db")));
+ }
+
+ @Test
+ void assertCreateWithEmbeddedVariable() {
+ assertThrows(IllegalArgumentException.class, () -> new MCPUriTemplate("shardingsphere://databases/prefix-{database}"));
+ }
}
diff --git a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/security/MCPClientSafetyPolicyTest.java b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/security/MCPClientSafetyPolicyTest.java
index 24b78d361df..982cacec628 100644
--- a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/security/MCPClientSafetyPolicyTest.java
+++ b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/security/MCPClientSafetyPolicyTest.java
@@ -18,12 +18,7 @@
package org.apache.shardingsphere.mcp.support.security;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.Arguments;
-import org.junit.jupiter.params.provider.MethodSource;
-
import java.util.Map;
-import java.util.stream.Stream;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
@@ -31,22 +26,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
class MCPClientSafetyPolicyTest {
- @ParameterizedTest(name = "{0}")
- @MethodSource("assertGetMaxToolCallsPerSessionCases")
- void assertGetMaxToolCallsPerSession(final String name, final String configuredValue, final int expectedMaxToolCallsPerSession) {
- String previous = System.getProperty(MCPClientSafetyPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY);
- try {
- if (null == configuredValue) {
- System.clearProperty(MCPClientSafetyPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY);
- } else {
- System.setProperty(MCPClientSafetyPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY, configuredValue);
- }
- assertThat(MCPClientSafetyPolicy.getMaxToolCallsPerSession(), is(expectedMaxToolCallsPerSession));
- } finally {
- restoreProperty(previous);
- }
- }
-
@Test
void assertCreateModelFacingPayload() {
Map<String, Object> actual = MCPClientSafetyPolicy.createModelFacingPayload();
@@ -56,24 +35,9 @@ class MCPClientSafetyPolicyTest {
Map<?, ?> actualRuntimeProtection = (Map<?, ?>) actual.get("runtime_protection");
Map<?, ?> actualToolCallLimit = (Map<?, ?>) actualRuntimeProtection.get("tool_call_limit");
assertThat(actualToolCallLimit.get("scope"), is("session"));
- assertThat(actualToolCallLimit.get("property"), is(MCPClientSafetyPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY));
+ assertThat(actualToolCallLimit.get("property"), is(MCPRuntimeProtectionPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY));
Map<?, ?> actualSQLExecutionLimits = (Map<?, ?>) actualRuntimeProtection.get("sql_execution_limits");
assertThat(((Map<?, ?>) actualSQLExecutionLimits.get("max_rows")).get("applied_field"), is("applied_max_rows"));
assertThat(((Map<?, ?>) actualSQLExecutionLimits.get("timeout_ms")).get("applied_field"), is("applied_timeout_ms"));
}
-
- private static Stream<Arguments> assertGetMaxToolCallsPerSessionCases() {
- return Stream.of(
- Arguments.of("configured value", "8", 8),
- Arguments.of("non-positive value falls back to default", "0", MCPClientSafetyPolicy.DEFAULT_MAX_TOOL_CALLS_PER_SESSION),
- Arguments.of("missing value falls back to default", null, MCPClientSafetyPolicy.DEFAULT_MAX_TOOL_CALLS_PER_SESSION));
- }
-
- private void restoreProperty(final String previous) {
- if (null == previous) {
- System.clearProperty(MCPClientSafetyPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY);
- } else {
- System.setProperty(MCPClientSafetyPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY, previous);
- }
- }
}
diff --git a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/security/MCPRuntimeProtectionPolicyTest.java b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/security/MCPRuntimeProtectionPolicyTest.java
index 765296756d0..e77ecebaa46 100644
--- a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/security/MCPRuntimeProtectionPolicyTest.java
+++ b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/security/MCPRuntimeProtectionPolicyTest.java
@@ -23,10 +23,12 @@ import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.Map;
+import java.util.function.IntSupplier;
import java.util.stream.Stream;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertThrows;
class MCPRuntimeProtectionPolicyTest {
@@ -46,6 +48,12 @@ class MCPRuntimeProtectionPolicyTest {
}
}
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("assertGetMaxToolCallsPerSessionWithInvalidValueCases")
+ void assertGetMaxToolCallsPerSessionWithInvalidValue(final String name, final String configuredValue) {
+ assertInvalidProperty(MCPRuntimeProtectionPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY, configuredValue, MCPRuntimeProtectionPolicy::getMaxToolCallsPerSession);
+ }
+
@ParameterizedTest(name = "{0}")
@MethodSource("assertGetMaxCompletionRequestsPerMinuteCases")
void assertGetMaxCompletionRequestsPerMinute(final String name, final String configuredValue, final int expectedMaxRequests) {
@@ -62,6 +70,13 @@ class MCPRuntimeProtectionPolicyTest {
}
}
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("assertGetMaxCompletionRequestsPerMinuteWithInvalidValueCases")
+ void assertGetMaxCompletionRequestsPerMinuteWithInvalidValue(final String name, final String configuredValue) {
+ assertInvalidProperty(MCPRuntimeProtectionPolicy.MAX_COMPLETION_REQUESTS_PER_MINUTE_PROPERTY, configuredValue,
+ MCPRuntimeProtectionPolicy::getMaxCompletionRequestsPerMinute);
+ }
+
@Test
void assertCreateToolCallLimitPayload() {
String previous = System.getProperty(MCPRuntimeProtectionPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY);
@@ -106,17 +121,34 @@ class MCPRuntimeProtectionPolicyTest {
private static Stream<Arguments> assertGetMaxToolCallsPerSessionCases() {
return Stream.of(
Arguments.of("configured value", "8", 8),
- Arguments.of("non-positive value falls back to default", "0", MCPRuntimeProtectionPolicy.DEFAULT_MAX_TOOL_CALLS_PER_SESSION),
Arguments.of("missing value falls back to default", null, MCPRuntimeProtectionPolicy.DEFAULT_MAX_TOOL_CALLS_PER_SESSION));
}
+ private static Stream<Arguments> assertGetMaxToolCallsPerSessionWithInvalidValueCases() {
+ return Stream.of(Arguments.of("malformed value", "foo"), Arguments.of("non-positive value", "0"));
+ }
+
private static Stream<Arguments> assertGetMaxCompletionRequestsPerMinuteCases() {
return Stream.of(
Arguments.of("configured value", "120", 120),
- Arguments.of("non-positive value falls back to default", "0", MCPRuntimeProtectionPolicy.DEFAULT_MAX_COMPLETION_REQUESTS_PER_MINUTE),
Arguments.of("missing value falls back to default", null, MCPRuntimeProtectionPolicy.DEFAULT_MAX_COMPLETION_REQUESTS_PER_MINUTE));
}
+ private static Stream<Arguments> assertGetMaxCompletionRequestsPerMinuteWithInvalidValueCases() {
+ return Stream.of(Arguments.of("malformed value", "foo"), Arguments.of("non-positive value", "0"));
+ }
+
+ private void assertInvalidProperty(final String propertyName, final String configuredValue, final IntSupplier action) {
+ String previous = System.getProperty(propertyName);
+ try {
+ System.setProperty(propertyName, configuredValue);
+ IllegalArgumentException actual = assertThrows(IllegalArgumentException.class, action::getAsInt);
+ assertThat(actual.getMessage(), is(String.format("System property `%s` must be a positive integer, but was `%s`.", propertyName, configuredValue)));
+ } finally {
+ restoreProperty(propertyName, previous);
+ }
+ }
+
private void restoreProperty(final String propertyName, final String previous) {
if (null == previous) {
System.clearProperty(propertyName);
diff --git a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/yaml/MCPYamlConfigurationValidatorTest.java b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/yaml/MCPYamlConfigurationValidatorTest.java
deleted file mode 100644
index af4f70ebfed..00000000000
--- a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/yaml/MCPYamlConfigurationValidatorTest.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.mcp.support.yaml;
-
-import lombok.Getter;
-import lombok.Setter;
-import org.junit.jupiter.api.Test;
-
-import javax.validation.constraints.NotBlank;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-
-class MCPYamlConfigurationValidatorTest {
-
- @Test
- void assertValidate() {
- FixtureYamlConfiguration yamlConfig = new FixtureYamlConfiguration();
- yamlConfig.setName("fixture");
- assertDoesNotThrow(() -> MCPYamlConfigurationValidator.validate(yamlConfig, "Fixture YAML configuration"));
- }
-
- @Test
- void assertValidateWithViolation() {
- IllegalArgumentException actual = assertThrows(IllegalArgumentException.class,
- () -> MCPYamlConfigurationValidator.validate(new FixtureYamlConfiguration(), "Fixture YAML configuration"));
- assertThat(actual.getMessage(), is("Fixture YAML configuration property `name` is required."));
- }
-
- @Test
- void assertValidateWithNullConfiguration() {
- IllegalArgumentException actual = assertThrows(IllegalArgumentException.class, () -> MCPYamlConfigurationValidator.validate(null, "Fixture YAML configuration"));
- assertThat(actual.getMessage(), is("Fixture YAML configuration cannot be null."));
- }
-
- @Getter
- @Setter
- private static final class FixtureYamlConfiguration {
-
- @NotBlank(message = "is required")
- private String name;
- }
-}
diff --git a/mcp/support/src/test/resources/META-INF/shardingsphere-mcp/mcp-descriptors/mcp-descriptor-test-planning.yaml b/mcp/support/src/test/resources/META-INF/shardingsphere-mcp/mcp-descriptors/mcp-descriptor-test-planning.yaml
index b815b34ab95..23a69348d06 100644
--- a/mcp/support/src/test/resources/META-INF/shardingsphere-mcp/mcp-descriptors/mcp-descriptor-test-planning.yaml
+++ b/mcp/support/src/test/resources/META-INF/shardingsphere-mcp/mcp-descriptors/mcp-descriptor-test-planning.yaml
@@ -23,6 +23,27 @@ resources:
shardingSphereMetadata:
resourceKind: detail
+prompts:
+ - name: configure_encryption
+ title: Configure Encryption
+ description: "Test prompt whose name is intentionally independent from its planning tool."
+ binding:
+ templateResource: META-INF/shardingsphere-mcp/prompts/fixture-markdown-resource.md
+ arguments:
+ - name: database
+ title: Logical Database
+ description: "Logical database used by the test prompt."
+ required: false
+ meta:
+ org.apache.shardingsphere/related-tools:
+ - database_gateway_plan_encrypt_rule
+ - database_gateway_apply_workflow
+ - database_gateway_validate_workflow
+ org.apache.shardingsphere/stop-conditions:
+ - "Stop after creating a reviewable test plan."
+ org.apache.shardingsphere/ask-user-conditions:
+ - "Ask when required test input is unavailable."
+
tools:
- name: database_gateway_plan_encrypt_rule
title: Plan Encrypt Rule
diff --git a/test/e2e/mcp/pom.xml b/test/e2e/mcp/pom.xml
index 20d16bee039..b1cd48eb453 100644
--- a/test/e2e/mcp/pom.xml
+++ b/test/e2e/mcp/pom.xml
@@ -42,6 +42,18 @@
<type>test-jar</type>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-mcp-api</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-mcp-support</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-mcp-core</artifactId>
@@ -90,6 +102,18 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-infra-util</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-database-connector-core</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
@@ -100,11 +124,6 @@
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>org.apache.hive</groupId>
- <artifactId>hive-jdbc</artifactId>
- <scope>test</scope>
- </dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/env/MCPE2ETestConfiguration.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/env/MCPE2ETestConfiguration.java
index 88a99274b96..e5802803473 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/env/MCPE2ETestConfiguration.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/env/MCPE2ETestConfiguration.java
@@ -36,6 +36,16 @@ final class MCPE2ETestConfiguration {
}
boolean isDockerRunType() {
- return EnvironmentPropertiesLoader.getListValue(props, "e2e.run.type").stream().map(each -> each.toUpperCase(Locale.ENGLISH)).anyMatch("DOCKER"::equals);
+ boolean result = false;
+ for (String each : EnvironmentPropertiesLoader.getListValue(props, "e2e.run.type")) {
+ String runType = each.toUpperCase(Locale.ENGLISH);
+ if (!"DOCKER".equals(runType) && !"NATIVE".equals(runType)) {
+ throw new IllegalStateException(String.format("Unsupported MCP E2E run type `%s`.", each));
+ }
+ if ("DOCKER".equals(runType)) {
+ result = true;
+ }
+ }
+ return result;
}
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/env/MCPE2ETestConfigurationTest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/env/MCPE2ETestConfigurationTest.java
index d00f5f61cf7..e8d5a1722c5 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/env/MCPE2ETestConfigurationTest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/env/MCPE2ETestConfigurationTest.java
@@ -21,7 +21,10 @@ import org.junit.jupiter.api.Test;
import java.util.Properties;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class MCPE2ETestConfigurationTest {
@@ -56,4 +59,13 @@ class MCPE2ETestConfigurationTest {
assertFalse(config.isDockerRunType());
}
+ @Test
+ void assertInvalidRunType() {
+ Properties props = new Properties();
+ props.setProperty("e2e.run.type", "DOCKER, REMOTE");
+ MCPE2ETestConfiguration config = new MCPE2ETestConfiguration(props);
+ IllegalStateException actualException = assertThrows(IllegalStateException.class, config::isDockerRunType);
+ assertThat(actualException.getMessage(), is("Unsupported MCP E2E run type `REMOTE`."));
+ }
+
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/config/LLME2EConfiguration.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/config/LLME2EConfiguration.java
index 574e88b77e7..70ccc483c3f 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/config/LLME2EConfiguration.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/config/LLME2EConfiguration.java
@@ -187,9 +187,13 @@ public final class LLME2EConfiguration {
private static int readInteger(final Properties props, final String propertyName, final int defaultValue) {
String result = readString(props, propertyName, String.valueOf(defaultValue));
try {
- return Integer.parseInt(result);
- } catch (final NumberFormatException ignored) {
- return defaultValue;
+ int parsedValue = Integer.parseInt(result);
+ if (parsedValue <= 0) {
+ throw new IllegalStateException(String.format("MCP LLM E2E property `%s` must be a positive integer, but was `%s`.", propertyName, result));
+ }
+ return parsedValue;
+ } catch (final NumberFormatException ex) {
+ throw new IllegalStateException(String.format("MCP LLM E2E property `%s` must be a positive integer, but was `%s`.", propertyName, result), ex);
}
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/config/LLME2EConfigurationTest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/config/LLME2EConfigurationTest.java
index 769f62a199f..4c77d96f054 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/config/LLME2EConfigurationTest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/config/LLME2EConfigurationTest.java
@@ -151,8 +151,15 @@ class LLME2EConfigurationTest {
@Test
void assertLoadWithInvalidIntegerProperty() {
System.setProperty("mcp.llm.ready-timeout-seconds", "invalid-number");
- LLME2EConfiguration actual = LLME2EConfiguration.load();
- assertThat(actual.getReadyTimeoutSeconds(), is(600));
+ IllegalStateException actualException = assertThrows(IllegalStateException.class, LLME2EConfiguration::load);
+ assertThat(actualException.getMessage(), is("MCP LLM E2E property `mcp.llm.ready-timeout-seconds` must be a positive integer, but was `invalid-number`."));
+ }
+
+ @Test
+ void assertLoadWithNonPositiveIntegerProperty() {
+ System.setProperty("mcp.llm.ready-timeout-seconds", "0");
+ IllegalStateException actualException = assertThrows(IllegalStateException.class, LLME2EConfiguration::load);
+ assertThat(actualException.getMessage(), is("MCP LLM E2E property `mcp.llm.ready-timeout-seconds` must be a positive integer, but was `0`."));
}
@Test
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/AbstractLLMMCPConversationRunnerTest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/AbstractLLMMCPConversationRunnerTest.java
index e1894dbba2a..b22e151e3bc 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/AbstractLLMMCPConversationRunnerTest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/AbstractLLMMCPConversationRunnerTest.java
@@ -135,21 +135,15 @@ abstract class AbstractLLMMCPConversationRunnerTest {
return ArgumentCaptor.forClass((Class) List.class);
}
- protected List<List<LLMChatMessage>> captureRequiredChatMessages(final int expectedInvocations) throws IOException, InterruptedException {
+ protected List<List<LLMChatMessage>> captureAutoChatMessages(final int expectedInvocations) throws IOException, InterruptedException {
ArgumentCaptor<List<LLMChatMessage>> result = createChatMessagesCaptor();
- verify(llmChatClient, times(expectedInvocations)).complete(result.capture(), anyList(), eq("required"), eq(false));
+ verify(llmChatClient, times(expectedInvocations)).complete(result.capture(), anyList(), eq("auto"), eq(false));
return result.getAllValues();
}
- protected List<List<Map<String, Object>>> captureRequiredToolDefinitions(final int expectedInvocations) throws IOException, InterruptedException {
+ protected List<List<Map<String, Object>>> captureAutoToolDefinitions(final int expectedInvocations) throws IOException, InterruptedException {
ArgumentCaptor<List<Map<String, Object>>> result = createToolDefinitionsCaptor();
- verify(llmChatClient, times(expectedInvocations)).complete(anyList(), result.capture(), eq("required"), eq(false));
- return result.getAllValues();
- }
-
- protected List<List<LLMChatMessage>> captureAutoChatMessages() throws IOException, InterruptedException {
- ArgumentCaptor<List<LLMChatMessage>> result = createChatMessagesCaptor();
- verify(llmChatClient).complete(result.capture(), anyList(), eq("auto"), eq(false));
+ verify(llmChatClient, times(expectedInvocations)).complete(anyList(), result.capture(), eq("auto"), eq(false));
return result.getAllValues();
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationArtifacts.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationArtifacts.java
index f5938ab5981..cb135d66b3d 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationArtifacts.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationArtifacts.java
@@ -47,6 +47,8 @@ final class LLMMCPConversationArtifacts {
@Setter(AccessLevel.PACKAGE)
private String finalAnswerJson = "";
+ private boolean harnessRecoveryPending;
+
void addRawModelOutput(final String rawModelOutput) {
rawModelOutputs.add(rawModelOutput);
}
@@ -59,6 +61,16 @@ final class LLMMCPConversationArtifacts {
mcpRuntimeLogLines.add(runtimeLogLine);
}
+ void markHarnessRecovery() {
+ harnessRecoveryPending = true;
+ }
+
+ String consumeTurnActionOrigin() {
+ String result = harnessRecoveryPending ? MCPInteractionTraceRecord.HARNESS_TEXT_RECOVERY_ORIGIN : MCPInteractionTraceRecord.MODEL_TOOL_CALL_ORIGIN;
+ harnessRecoveryPending = false;
+ return result;
+ }
+
int nextSequence() {
return interactionTrace.size() + 1;
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunner.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunner.java
index f3ce17eafcd..2eff817b2a3 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunner.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunner.java
@@ -94,8 +94,10 @@ public final class LLMMCPConversationRunner {
for (int turnIndex = 0; turnIndex < maxTurns; turnIndex++) {
finalAnswerRequested = requestFinalAnswerIfReady(scenario, messages, artifacts, finalAnswerRequested, instructionFactory);
List<String> turnToolNames = finalAnswerRequested ? List.of() : turnPlanner.createTurnToolNames(scenario, artifacts.getInteractionTrace());
- LLMChatCompletion completion = completeTurn(scenario, messages, artifacts, finalAnswerRequested, turnToolNames, instructionFactory, turnPlanner);
- Optional<LLME2EArtifactBundle> toolCallFailure = processToolCallCompletion(scenario, completion, messages, artifacts, instructionFactory, turnPlanner);
+ TurnCompletion turnCompletion = completeTurn(scenario, messages, artifacts, finalAnswerRequested, turnToolNames, instructionFactory, turnPlanner);
+ LLMChatCompletion completion = turnCompletion.completion();
+ Optional<LLME2EArtifactBundle> toolCallFailure = processToolCallCompletion(
+ scenario, completion, messages, artifacts, instructionFactory, turnPlanner, turnCompletion.actionOrigin());
if (toolCallFailure.isPresent()) {
return toolCallFailure.get();
}
@@ -105,6 +107,7 @@ public final class LLMMCPConversationRunner {
if (!finalAnswerRequested) {
if (!LLMMCPInteractionCoverage.hasRequiredInteractionCoverage(scenario.getRequiredToolNames(), artifacts.getInteractionTrace())) {
messages.add(LLMChatMessage.user(instructionFactory.createRequiredToolCallInstruction(scenario, artifacts)));
+ artifacts.markHarnessRecovery();
}
continue;
}
@@ -144,6 +147,7 @@ public final class LLMMCPConversationRunner {
}
if (scenario.getRequiredToolNames().contains("database_gateway_execute_query") && !hasExpectedExecuteQuery(scenario.getExpectedAnswer(), artifacts.getInteractionTrace())) {
messages.add(LLMChatMessage.user(instructionFactory.createExpectedQueryInstruction(scenario.getExpectedAnswer())));
+ artifacts.markHarnessRecovery();
return false;
}
messages.add(LLMChatMessage.user(instructionFactory.createFinalAnswerInstruction(scenario, artifacts.getInteractionTrace())));
@@ -170,21 +174,22 @@ public final class LLMMCPConversationRunner {
return expectedAnswer.getSchema().equals(schema) || schema.isEmpty();
}
- private LLMChatCompletion completeTurn(final LLME2EScenario scenario, final List<LLMChatMessage> messages, final LLMMCPConversationArtifacts artifacts,
- final boolean finalAnswerRequested, final List<String> turnToolNames, final LLMMCPConversationInstructionFactory instructionFactory,
- final LLMMCPConversationTurnPlanner turnPlanner) throws IOException, InterruptedException {
- String toolChoice = turnPlanner.createToolChoice(scenario, artifacts.getInteractionTrace(), finalAnswerRequested);
+ private TurnCompletion completeTurn(final LLME2EScenario scenario, final List<LLMChatMessage> messages, final LLMMCPConversationArtifacts artifacts,
+ final boolean finalAnswerRequested, final List<String> turnToolNames, final LLMMCPConversationInstructionFactory instructionFactory,
+ final LLMMCPConversationTurnPlanner turnPlanner) throws IOException, InterruptedException {
+ String toolChoice = turnPlanner.createToolChoice(finalAnswerRequested);
+ String actionOrigin = artifacts.consumeTurnActionOrigin();
LLMChatCompletion result = llmChatClient.complete(finalAnswerRequested ? instructionFactory.createFinalAnswerMessages(scenario, artifacts.getInteractionTrace()) : messages,
finalAnswerRequested ? List.of() : toolDefinitionFactory.create(turnToolNames),
toolChoice, finalAnswerRequested);
artifacts.addRawModelOutput(result.getRawResponse());
- return result;
+ return new TurnCompletion(result, actionOrigin);
}
private Optional<LLME2EArtifactBundle> processToolCallCompletion(final LLME2EScenario scenario, final LLMChatCompletion completion,
final List<LLMChatMessage> messages, final LLMMCPConversationArtifacts artifacts,
final LLMMCPConversationInstructionFactory instructionFactory,
- final LLMMCPConversationTurnPlanner turnPlanner) throws InterruptedException {
+ final LLMMCPConversationTurnPlanner turnPlanner, final String actionOrigin) throws InterruptedException {
if (completion.getToolCalls().isEmpty()) {
return Optional.empty();
}
@@ -192,10 +197,10 @@ public final class LLMMCPConversationRunner {
for (LLMToolCall each : completion.getToolCalls()) {
List<String> availableToolNames = resolveAvailableToolNames(scenario, artifacts, turnPlanner);
if (!availableToolNames.isEmpty() && !availableToolNames.contains(each.getName())) {
- addUnavailableToolCorrection(scenario, each, messages, availableToolNames);
+ addUnavailableToolCorrection(scenario, each, messages, artifacts, availableToolNames);
return Optional.empty();
}
- Optional<LLME2EArtifactBundle> result = processToolCall(scenario, each, messages, artifacts, availableToolNames);
+ Optional<LLME2EArtifactBundle> result = processToolCall(scenario, each, messages, artifacts, availableToolNames, actionOrigin);
if (result.isPresent()) {
return result;
}
@@ -203,6 +208,7 @@ public final class LLMMCPConversationRunner {
String traceDrivenInstruction = instructionFactory.createTraceDrivenInstruction(scenario, artifacts.getInteractionTrace());
if (!traceDrivenInstruction.isEmpty()) {
messages.add(LLMChatMessage.user(traceDrivenInstruction));
+ artifacts.markHarnessRecovery();
}
return Optional.empty();
}
@@ -216,12 +222,13 @@ public final class LLMMCPConversationRunner {
}
private void addUnavailableToolCorrection(final LLME2EScenario scenario, final LLMToolCall toolCall, final List<LLMChatMessage> messages,
- final List<String> availableToolNames) {
+ final LLMMCPConversationArtifacts artifacts, final List<String> availableToolNames) {
messages.add(LLMChatMessage.tool(toolCall.getId(), JsonUtils.toJsonString(createUnavailableToolResponse(scenario, availableToolNames))));
messages.add(LLMChatMessage.user(String.format(Locale.ENGLISH,
"The previous response requested `%s`, but that MCP tool is not available in this turn. Available MCP tools for this turn: %s. "
+ "Do not call tools outside this list.%s",
toolCall.getName(), String.join(", ", availableToolNames), createExpectedQueryInstruction(scenario, availableToolNames))));
+ artifacts.markHarnessRecovery();
}
private Map<String, Object> createUnavailableToolResponse(final LLME2EScenario scenario, final List<String> availableToolNames) {
@@ -247,7 +254,8 @@ public final class LLMMCPConversationRunner {
}
private Optional<LLME2EArtifactBundle> processToolCall(final LLME2EScenario scenario, final LLMToolCall toolCall, final List<LLMChatMessage> messages,
- final LLMMCPConversationArtifacts artifacts, final List<String> extraAllowedToolNames) throws InterruptedException {
+ final LLMMCPConversationArtifacts artifacts, final List<String> extraAllowedToolNames,
+ final String actionOrigin) throws InterruptedException {
if (!scenario.getAllowedToolNames().contains(toolCall.getName()) && !extraAllowedToolNames.contains(toolCall.getName())) {
artifacts.addInteractionTrace(MCPInteractionTraceRecord.createInvalidAction(artifacts.nextSequence(), "tool_call", toolCall.getName(),
Map.of("rawArgumentsJson", toolCall.getArgumentsJson()), "unexpected_tool_requested"));
@@ -255,7 +263,7 @@ public final class LLMMCPConversationRunner {
}
try {
Map<String, Object> rawArguments = LLMMCPJsonValues.parseToolArguments(toolCall.getArgumentsJson());
- return processToolCall(scenario, toolCall, messages, artifacts, rawArguments);
+ return processToolCall(scenario, toolCall, messages, artifacts, rawArguments, actionOrigin);
} catch (final IllegalArgumentException ex) {
artifacts.addInteractionTrace(MCPInteractionTraceRecord.createInvalidAction(artifacts.nextSequence(), "tool_call", toolCall.getName(),
Map.of("rawArgumentsJson", toolCall.getArgumentsJson()), "invalid_tool_arguments"));
@@ -264,7 +272,8 @@ public final class LLMMCPConversationRunner {
}
private Optional<LLME2EArtifactBundle> processToolCall(final LLME2EScenario scenario, final LLMToolCall toolCall, final List<LLMChatMessage> messages,
- final LLMMCPConversationArtifacts artifacts, final Map<String, Object> arguments) throws InterruptedException {
+ final LLMMCPConversationArtifacts artifacts, final Map<String, Object> arguments,
+ final String actionOrigin) throws InterruptedException {
String toolName = toolCall.getName();
Optional<LLME2EArtifactBundle> validationFailure = validateToolCall(scenario, toolName, arguments, artifacts);
if (validationFailure.isPresent()) {
@@ -281,7 +290,6 @@ public final class LLMMCPConversationRunner {
long latencyMillis = System.currentTimeMillis() - startTime;
artifacts.addRuntimeLogLine("action=" + toolName + " args=" + JsonUtils.toJsonString(arguments));
artifacts.addRuntimeLogLine("response=" + JsonUtils.toJsonString(response));
- String actionOrigin = MCPInteractionTraceRecord.MODEL_TOOL_CALL_ORIGIN;
artifacts.addInteractionTrace(traceRecordFactory.createTraceRecord(artifacts.nextSequence(), toolName, actionOrigin, arguments, response, latencyMillis));
messages.add(LLMChatMessage.tool(toolCall.getId(), LLMMCPModelFacingToolResponseFormatter.format(response)));
return Optional.empty();
@@ -328,4 +336,7 @@ public final class LLMMCPConversationRunner {
}
}
+ private record TurnCompletion(LLMChatCompletion completion, String actionOrigin) {
+ }
+
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerCoverageTest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerCoverageTest.java
index b6921a7b0d6..02451cf089b 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerCoverageTest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerCoverageTest.java
@@ -49,7 +49,7 @@ class LLMMCPConversationRunnerCoverageTest extends AbstractLLMMCPConversationRun
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(2);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"));
when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload("NaN"));
when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
@@ -65,7 +65,7 @@ class LLMMCPConversationRunnerCoverageTest extends AbstractLLMMCPConversationRun
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(2);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"));
when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(Map.of("result_kind", "update_count", "update_count", 1));
when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
@@ -80,7 +80,7 @@ class LLMMCPConversationRunnerCoverageTest extends AbstractLLMMCPConversationRun
void assertRunWithMissingRequiredToolCoverage() throws IOException, InterruptedException {
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(1);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response"));
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
@@ -95,13 +95,13 @@ class LLMMCPConversationRunnerCoverageTest extends AbstractLLMMCPConversationRun
void assertRunNamesRemainingToolsWhenCoverageIsMissing() throws IOException, InterruptedException {
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(2);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response-1"));
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertThat(actual.getAssertionReport().getFailureType(), is("missing_required_tool_coverage"));
- List<List<LLMChatMessage>> actualMessages = captureRequiredChatMessages(2);
+ List<List<LLMChatMessage>> actualMessages = captureAutoChatMessages(2);
List<LLMChatMessage> actualSecondTurnMessages = actualMessages.get(1);
assertThat(actualSecondTurnMessages.getLast().getContent(),
containsString("Remaining required MCP tools: database_gateway_execute_query"));
@@ -115,13 +115,13 @@ class LLMMCPConversationRunnerCoverageTest extends AbstractLLMMCPConversationRun
void assertRunNamesExactResourceUriWhenResourceCoverageIsMissing() throws IOException, InterruptedException {
LLME2EScenario actualScenario = createScenario(List.of(MCPInteractionActionNames.READ_RESOURCE));
LLMMCPConversationRunner actualRunner = createRunner(2);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response-1"));
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertThat(actual.getAssertionReport().getFailureType(), is("missing_required_tool_coverage"));
- List<List<LLMChatMessage>> actualMessages = captureRequiredChatMessages(2);
+ List<List<LLMChatMessage>> actualMessages = captureAutoChatMessages(2);
List<LLMChatMessage> actualSecondTurnMessages = actualMessages.get(1);
String actualRetryInstruction = actualSecondTurnMessages.getLast().getContent();
assertThat(actualRetryInstruction, containsString("Remaining required MCP tools: mcp_read_resource"));
@@ -133,13 +133,13 @@ class LLMMCPConversationRunnerCoverageTest extends AbstractLLMMCPConversationRun
void assertRunNamesPreviewModeWhenUpdateCoverageIsMissing() throws IOException, InterruptedException {
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_update"));
LLMMCPConversationRunner actualRunner = createRunner(2);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response-1"));
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertThat(actual.getAssertionReport().getFailureType(), is("missing_required_tool_coverage"));
- List<List<LLMChatMessage>> actualMessages = captureRequiredChatMessages(2);
+ List<List<LLMChatMessage>> actualMessages = captureAutoChatMessages(2);
List<LLMChatMessage> actualSecondTurnMessages = actualMessages.get(1);
String actualRetryInstruction = actualSecondTurnMessages.getLast().getContent();
assertThat(actualRetryInstruction, containsString("Remaining required MCP tools: database_gateway_execute_update"));
@@ -153,13 +153,13 @@ class LLMMCPConversationRunnerCoverageTest extends AbstractLLMMCPConversationRun
void assertRunNamesPlanIdRuleWhenPlanningCoverageIsMissing() throws IOException, InterruptedException {
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_plan_mask_rule"));
LLMMCPConversationRunner actualRunner = createRunner(2);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response-1"));
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertThat(actual.getAssertionReport().getFailureType(), is("missing_required_tool_coverage"));
- List<List<LLMChatMessage>> actualMessages = captureRequiredChatMessages(2);
+ List<List<LLMChatMessage>> actualMessages = captureAutoChatMessages(2);
List<LLMChatMessage> actualSecondTurnMessages = actualMessages.get(1);
String actualRetryInstruction = actualSecondTurnMessages.getLast().getContent();
assertThat(actualRetryInstruction, containsString("Remaining required MCP tools: database_gateway_plan_mask_rule"));
@@ -172,7 +172,7 @@ class LLMMCPConversationRunnerCoverageTest extends AbstractLLMMCPConversationRun
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_plan_mask_rule"));
LLMMCPConversationRunner actualRunner = createRunner(1);
Map<String, Object> rawPlanArguments = Map.of("plan_id", "1", "database", DATABASE_NAME, "schema", SCHEMA_NAME, "table", TABLE_NAME, "column", "status");
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_plan_mask_rule", rawPlanArguments, "plan-response"));
when(getMCPInteractionClient().call("database_gateway_plan_mask_rule", rawPlanArguments)).thenReturn(Map.of("plan_id", "plan-1"));
@@ -190,9 +190,8 @@ class LLMMCPConversationRunnerCoverageTest extends AbstractLLMMCPConversationRun
LLMMCPConversationRunner actualRunner = createRunner(2);
Map<String, Object> initialPlanArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "table", TABLE_NAME, "column", "status");
Map<String, Object> currentPlanArguments = Map.of("plan_id", "plan-1", "database", DATABASE_NAME, "schema", SCHEMA_NAME, "table", TABLE_NAME, "column", "status");
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", "database_gateway_plan_mask_rule", initialPlanArguments, "initial-plan-response"));
when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
+ createToolCallCompletion("tool-1", "database_gateway_plan_mask_rule", initialPlanArguments, "initial-plan-response"),
createToolCallCompletion("tool-2", "database_gateway_plan_mask_rule", currentPlanArguments, "current-plan-response"));
when(getMCPInteractionClient().call("database_gateway_plan_mask_rule", initialPlanArguments)).thenReturn(Map.of("plan_id", "plan-1", "next_actions", List.of(Map.of(
"type", "tool_call",
@@ -204,7 +203,7 @@ class LLMMCPConversationRunnerCoverageTest extends AbstractLLMMCPConversationRun
assertThat(actual.getAssertionReport().getFailureType(), is("missing_required_tool_coverage"));
assertThat(actual.getInteractionTrace().get(1).getArguments(), is(currentPlanArguments));
- assertThat(actual.getInteractionTrace().get(1).getActionOrigin(), is(MCPInteractionTraceRecord.MODEL_TOOL_CALL_ORIGIN));
+ assertThat(actual.getInteractionTrace().get(1).getActionOrigin(), is(MCPInteractionTraceRecord.HARNESS_TEXT_RECOVERY_ORIGIN));
verify(getMCPInteractionClient()).call("database_gateway_plan_mask_rule", currentPlanArguments);
}
@@ -213,7 +212,7 @@ class LLMMCPConversationRunnerCoverageTest extends AbstractLLMMCPConversationRun
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(3);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
new LLMChatCompletion(QUERY, List.of(), "query-text-response"),
createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "query-tool-response"));
when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
@@ -225,9 +224,9 @@ class LLMMCPConversationRunnerCoverageTest extends AbstractLLMMCPConversationRun
assertTrue(actual.getAssertionReport().isSuccess());
assertThat(actual.getInteractionTrace().size(), is(1));
assertThat(actual.getInteractionTrace().getFirst().getTargetName(), is("database_gateway_execute_query"));
- assertThat(actual.getInteractionTrace().getFirst().getActionOrigin(), is(MCPInteractionTraceRecord.MODEL_TOOL_CALL_ORIGIN));
+ assertThat(actual.getInteractionTrace().getFirst().getActionOrigin(), is(MCPInteractionTraceRecord.HARNESS_TEXT_RECOVERY_ORIGIN));
verify(getMCPInteractionClient()).call("database_gateway_execute_query", executeQueryArguments);
- verify(getLLMChatClient(), times(2)).complete(anyList(), anyList(), eq("required"), eq(false));
+ verify(getLLMChatClient(), times(2)).complete(anyList(), anyList(), eq("auto"), eq(false));
}
@Test
@@ -235,7 +234,7 @@ class LLMMCPConversationRunnerCoverageTest extends AbstractLLMMCPConversationRun
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_plan_mask_rule", "database_gateway_apply_workflow"));
LLMMCPConversationRunner actualRunner = createRunner(3);
Map<String, Object> planArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "table", TABLE_NAME, "column", "status");
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_plan_mask_rule", planArguments, "plan-response"),
new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response-1"));
when(getMCPInteractionClient().call("database_gateway_plan_mask_rule", planArguments)).thenReturn(Map.of("plan_id", "plan-1"));
@@ -243,7 +242,7 @@ class LLMMCPConversationRunnerCoverageTest extends AbstractLLMMCPConversationRun
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertThat(actual.getAssertionReport().getFailureType(), is("missing_required_tool_coverage"));
- List<List<LLMChatMessage>> actualMessages = captureRequiredChatMessages(3);
+ List<List<LLMChatMessage>> actualMessages = captureAutoChatMessages(3);
List<LLMChatMessage> actualThirdTurnMessages = actualMessages.get(2);
String actualRetryInstruction = actualThirdTurnMessages.getLast().getContent();
assertThat(actualRetryInstruction, containsString("Remaining required MCP tools: database_gateway_apply_workflow"));
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerFailureTest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerFailureTest.java
index 391912c7008..9b8109ae98c 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerFailureTest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerFailureTest.java
@@ -56,7 +56,7 @@ class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunn
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(2);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"),
new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response"));
when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(Map.of("error_code", "tool_failed"));
@@ -64,7 +64,7 @@ class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunn
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertThat(actual.getAssertionReport().getFailureType(), is("missing_required_tool_coverage"));
- verify(getLLMChatClient(), times(2)).complete(anyList(), anyList(), eq("required"), eq(false));
+ verify(getLLMChatClient(), times(2)).complete(anyList(), anyList(), eq("auto"), eq(false));
}
@Test
@@ -72,7 +72,7 @@ class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunn
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(3);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"));
when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
@@ -91,7 +91,7 @@ class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunn
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(1);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"));
when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenThrow(new IOException("boom"));
@@ -131,7 +131,7 @@ class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunn
@Test
void assertRunWithInterruptedMcpAction() throws IOException, InterruptedException {
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"));
when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenThrow(new InterruptedException("boom"));
try {
@@ -148,7 +148,7 @@ class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunn
void assertRunWithModelRequestIOException() throws IOException, InterruptedException {
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(1);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenThrow(new IOException("http 500"));
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenThrow(new IOException("http 500"));
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
@@ -162,7 +162,7 @@ class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunn
void assertRunWithInterruptedConversation() throws IOException, InterruptedException {
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(1);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenThrow(new InterruptedException("boom"));
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenThrow(new InterruptedException("boom"));
try {
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
@@ -177,7 +177,7 @@ class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunn
void assertRunWithModelServiceUnavailable() throws IOException, InterruptedException {
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(1);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenThrow(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenThrow(
new IllegalStateException("Model service is not ready for `ggml-org/Qwen3-1.7B-GGUF:Q4_K_M`."));
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
@@ -189,7 +189,7 @@ class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunn
@Test
void assertRunClassifiesModelFailureByMessage() throws IOException, InterruptedException {
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenThrow(new IllegalStateException("MCP-shaped model failure."));
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenThrow(new IllegalStateException("MCP-shaped model failure."));
LLME2EArtifactBundle actual = createRunner(1).run(createScenario(List.of("database_gateway_execute_query")));
assertThat(actual.getAssertionReport().getFailureType(), is("mcp_runtime_unavailable"));
}
@@ -198,7 +198,7 @@ class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunn
void assertRunIgnoresCloseIOException() throws IOException, InterruptedException {
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(1);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response"));
doThrow(new IOException("close failed")).when(getMCPInteractionClient()).close();
@@ -212,7 +212,7 @@ class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunn
void assertRunRestoresInterruptOnCloseFailure() throws IOException, InterruptedException {
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(1);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response"));
doThrow(new InterruptedException("close interrupted")).when(getMCPInteractionClient()).close();
try {
@@ -229,7 +229,7 @@ class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunn
void assertRunWithUnexpectedTool() throws IOException, InterruptedException {
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(1);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
new LLMChatCompletion("", List.of(new LLMToolCall("tool-1", "unsupported_tool", "{}")), "tool-call-response"));
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
@@ -245,7 +245,7 @@ class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunn
void assertRunWithInvalidToolArgumentsJson() throws IOException, InterruptedException {
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(1);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
new LLMChatCompletion("", List.of(new LLMToolCall("tool-1", "database_gateway_execute_query", "{invalid")), "tool-call-response"));
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
@@ -261,7 +261,7 @@ class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunn
void assertRunWithEmptyResourceUri() throws IOException, InterruptedException {
LLME2EScenario actualScenario = createScenario(List.of(MCPInteractionActionNames.READ_RESOURCE));
LLMMCPConversationRunner actualRunner = createRunner(1);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", MCPInteractionActionNames.READ_RESOURCE, Map.of("uri", " "), "tool-call-response"));
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
@@ -275,7 +275,7 @@ class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunn
void assertRunWithUnsafeExecuteUpdateAttempted() throws IOException, InterruptedException {
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_update"));
LLMMCPConversationRunner actualRunner = createRunner(1);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_update",
Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "sql", "UPDATE orders SET status = 'DONE'", "execution_mode", "execute"),
"tool-call-response"));
@@ -291,7 +291,7 @@ class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunn
void assertRunWithUnsafeWorkflowExecutionAttempted() throws IOException, InterruptedException {
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_apply_workflow"));
LLMMCPConversationRunner actualRunner = createRunner(1);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_apply_workflow", Map.of("plan_id", "plan-1", "execution_mode", "review-then-execute"), "tool-call-response"));
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
@@ -307,7 +307,7 @@ class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunn
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(2);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"));
when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerNextActionTest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerNextActionTest.java
index 907c3c0db93..4e82ec6d324 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerNextActionTest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerNextActionTest.java
@@ -52,11 +52,10 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
LLMMCPConversationRunner actualRunner = createRunner(4);
Map<String, Object> wrongQueryArguments = Map.of("database", "analytics_db", "schema", SCHEMA_NAME, "sql", QUERY, "max_rows", 10);
Map<String, Object> expectedQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", "database_gateway_execute_query", wrongQueryArguments, "wrong-query-response"));
- when(getMCPInteractionClient().call("database_gateway_execute_query", wrongQueryArguments)).thenReturn(createResultSetPayload(4));
when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
+ createToolCallCompletion("tool-1", "database_gateway_execute_query", wrongQueryArguments, "wrong-query-response"),
createToolCallCompletion("tool-2", "database_gateway_execute_query", expectedQueryArguments, "expected-query-response"));
+ when(getMCPInteractionClient().call("database_gateway_execute_query", wrongQueryArguments)).thenReturn(createResultSetPayload(4));
when(getMCPInteractionClient().call("database_gateway_execute_query", expectedQueryArguments)).thenReturn(createResultSetPayload(2));
when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
createFinalAnswerCompletion(List.of("database_gateway_execute_query"), 2, "final-answer-response"));
@@ -64,12 +63,13 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertTrue(actual.getAssertionReport().isSuccess());
- List<List<LLMChatMessage>> actualMessages = captureAutoChatMessages();
- List<LLMChatMessage> actualAutoTurnMessages = actualMessages.getFirst();
+ List<List<LLMChatMessage>> actualMessages = captureAutoChatMessages(2);
+ List<LLMChatMessage> actualAutoTurnMessages = actualMessages.getLast();
assertTrue(actualAutoTurnMessages.stream()
.map(LLMChatMessage::getContent)
.anyMatch(each -> each.contains("latest successful database_gateway_execute_query did not use database `logic_db`")));
assertThat(actual.getInteractionTrace().size(), is(2));
+ assertThat(actual.getInteractionTrace().get(1).getActionOrigin(), is(MCPInteractionTraceRecord.HARNESS_TEXT_RECOVERY_ORIGIN));
}
@Test
@@ -78,7 +78,7 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
LLMMCPConversationRunner actualRunner = createRunner(4);
Map<String, Object> completionReference = Map.of("type", "ref/prompt", "name", PROMPT_NAME);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(new LLMChatCompletion("",
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(new LLMChatCompletion("",
List.of(
new LLMToolCall("tool-1", MCPInteractionActionNames.COMPLETE, JsonUtils.toJsonString(Map.of(
"ref", completionReference,
@@ -105,7 +105,7 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
Map<String, Object> completionReference = Map.of("type", "ref/prompt", "name", PROMPT_NAME);
Map<String, Object> completionArguments = Map.of("ref", completionReference, "argument", Map.of("name", "schema", "value", "pub"));
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", MCPInteractionActionNames.GET_PROMPT, getPromptArguments, "prompt-response"),
createToolCallCompletion("tool-2", MCPInteractionActionNames.COMPLETE, completionArguments, "completion-response"),
createToolCallCompletion("tool-3", "database_gateway_execute_query", executeQueryArguments, "query-response"));
@@ -121,9 +121,10 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertTrue(actual.getAssertionReport().isSuccess());
- List<List<Map<String, Object>>> actualTools = captureRequiredToolDefinitions(3);
+ List<List<Map<String, Object>>> actualTools = captureAutoToolDefinitions(3);
assertThat(getToolNames(actualTools.get(1)), is(List.of(MCPInteractionActionNames.COMPLETE)));
- assertThat(getToolNames(actualTools.get(2)), is(List.of("database_gateway_execute_query")));
+ assertThat(getToolNames(actualTools.get(2)), is(List.of(
+ MCPInteractionActionNames.GET_PROMPT, MCPInteractionActionNames.COMPLETE, "database_gateway_execute_query")));
verify(getMCPInteractionClient()).complete(completionReference, "schema", "pub", Map.of());
}
@@ -135,7 +136,7 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
Map<String, Object> runtimeArguments = Map.of("uri", "shardingsphere://runtime");
Map<String, Object> capabilitiesArguments = Map.of("uri", RESOURCE_URI);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", MCPInteractionActionNames.READ_RESOURCE, runtimeArguments, "runtime-response"),
createToolCallCompletion("tool-2", MCPInteractionActionNames.READ_RESOURCE, capabilitiesArguments, "capabilities-response"),
createToolCallCompletion("tool-3", "database_gateway_execute_query", executeQueryArguments, "query-response"));
@@ -150,8 +151,8 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertTrue(actual.getAssertionReport().isSuccess());
- List<List<LLMChatMessage>> actualMessages = captureRequiredChatMessages(3);
- List<List<Map<String, Object>>> actualTools = captureRequiredToolDefinitions(3);
+ List<List<LLMChatMessage>> actualMessages = captureAutoChatMessages(3);
+ List<List<Map<String, Object>>> actualTools = captureAutoToolDefinitions(3);
assertTrue(containsMessage(actualMessages.get(1), "Call mcp_read_resource with uri `" + RESOURCE_URI + "` now"));
assertThat(getToolNames(actualTools.get(1)), is(List.of(MCPInteractionActionNames.READ_RESOURCE)));
}
@@ -164,7 +165,7 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
Map<String, Object> planArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "table", TABLE_NAME, "column", "status");
Map<String, Object> applyArguments = Map.of("plan_id", "plan-1", "execution_mode", "preview");
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_plan_mask_rule", planArguments, "plan-response"),
createToolCallCompletion("tool-2", "database_gateway_apply_workflow", applyArguments, "apply-response"),
createToolCallCompletion("tool-3", "database_gateway_execute_query", executeQueryArguments, "query-response"));
@@ -180,9 +181,9 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertTrue(actual.getAssertionReport().isSuccess());
- assertThat(actual.getInteractionTrace().get(1).getActionOrigin(), is(MCPInteractionTraceRecord.MODEL_TOOL_CALL_ORIGIN));
- List<List<LLMChatMessage>> actualMessages = captureRequiredChatMessages(3);
- List<List<Map<String, Object>>> actualTools = captureRequiredToolDefinitions(3);
+ assertThat(actual.getInteractionTrace().get(1).getActionOrigin(), is(MCPInteractionTraceRecord.HARNESS_TEXT_RECOVERY_ORIGIN));
+ List<List<LLMChatMessage>> actualMessages = captureAutoChatMessages(3);
+ List<List<Map<String, Object>>> actualTools = captureAutoToolDefinitions(3);
assertTrue(containsMessage(actualMessages.get(1), "Call `database_gateway_apply_workflow` now with exactly these arguments"));
assertTrue(containsMessage(actualMessages.get(1), "\"plan_id\":\"plan-1\""));
assertThat(getToolNames(actualTools.get(1)), is(List.of("database_gateway_apply_workflow")));
@@ -196,7 +197,7 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
LLMMCPConversationRunner actualRunner = createRunner(4);
Map<String, Object> readResourceArguments = Map.of("uri", tableResourceUri);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", MCPInteractionActionNames.LIST_RESOURCES, Map.of(), "list-response"),
createToolCallCompletion("tool-2", MCPInteractionActionNames.READ_RESOURCE, readResourceArguments, "resource-response"),
createToolCallCompletion("tool-3", "database_gateway_execute_query", executeQueryArguments, "query-response"));
@@ -209,7 +210,7 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertTrue(actual.getAssertionReport().isSuccess());
- List<List<LLMChatMessage>> actualMessages = captureRequiredChatMessages(3);
+ List<List<LLMChatMessage>> actualMessages = captureAutoChatMessages(3);
assertTrue(containsMessage(actualMessages.get(1), "Use exactly `" + tableResourceUri + "` as uri"));
assertTrue(containsMessage(actualMessages.get(1), "do not copy parameter schema"));
}
@@ -225,7 +226,7 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
Map<String, Object> staleReadArguments = Map.of("uri", staleResourceUri);
Map<String, Object> liveReadArguments = Map.of("uri", tableResourceUri);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", MCPInteractionActionNames.READ_RESOURCE, staleReadArguments, "stale-response"),
createToolCallCompletion("tool-2", MCPInteractionActionNames.READ_RESOURCE, liveReadArguments, "live-response"),
createToolCallCompletion("tool-3", "database_gateway_execute_query", executeQueryArguments, "query-response"));
@@ -243,7 +244,7 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertTrue(actual.getAssertionReport().isSuccess());
- List<List<LLMChatMessage>> actualMessages = captureRequiredChatMessages(3);
+ List<List<LLMChatMessage>> actualMessages = captureAutoChatMessages(3);
assertTrue(containsMessage(actualMessages.get(1), "Use exactly `" + tableResourceUri + "` as uri"));
assertThat(actual.getInteractionTrace().get(1).getArguments().get("uri"), is(tableResourceUri));
}
@@ -256,18 +257,17 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
Map<String, Object> previewArguments = Map.of("plan_id", "plan-1", "execution_mode", "preview");
Map<String, Object> manualArguments = Map.of("plan_id", "plan-1", "execution_mode", "manual-only");
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(new LLMChatCompletion("",
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(new LLMChatCompletion("",
List.of(
new LLMToolCall("tool-1", "database_gateway_execute_query", JsonUtils.toJsonString(executeQueryArguments)),
new LLMToolCall("tool-2", "database_gateway_apply_workflow", JsonUtils.toJsonString(previewArguments))),
- "initial-response"));
+ "initial-response"),
+ createToolCallCompletion("tool-3", "database_gateway_apply_workflow", manualArguments, "manual-response"));
when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
when(getMCPInteractionClient().call("database_gateway_apply_workflow", previewArguments)).thenReturn(Map.of("response_mode", "preview", "next_actions", List.of(Map.of(
"type", "tool_call",
"tool_name", "database_gateway_apply_workflow",
"arguments", manualArguments))));
- when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
- createToolCallCompletion("tool-3", "database_gateway_apply_workflow", manualArguments, "manual-response"));
when(getMCPInteractionClient().call("database_gateway_apply_workflow", manualArguments)).thenReturn(Map.of("response_mode", "manual_only"));
when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
createFinalAnswerCompletion(List.of("database_gateway_execute_query", "database_gateway_apply_workflow"), 2, "final-answer-response"));
@@ -275,8 +275,8 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertTrue(actual.getAssertionReport().isSuccess());
- List<List<LLMChatMessage>> actualMessages = captureAutoChatMessages();
- assertTrue(containsMessage(actualMessages.getFirst(), "Call `database_gateway_apply_workflow` now with exactly these arguments"));
+ List<List<LLMChatMessage>> actualMessages = captureAutoChatMessages(2);
+ assertTrue(containsMessage(actualMessages.getLast(), "Call `database_gateway_apply_workflow` now with exactly these arguments"));
}
@Test
@@ -295,7 +295,7 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
"sql", QUERY,
"execution_mode", "preview");
Map<String, Object> expectedQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_update", previewArguments, "preview-response"),
createToolCallCompletion("tool-2", "database_gateway_execute_update", misroutedQueryArguments, "misrouted-query-response"),
createToolCallCompletion("tool-3", "database_gateway_execute_query", expectedQueryArguments, "query-response"));
@@ -309,7 +309,7 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertTrue(actual.getAssertionReport().isSuccess());
- List<List<LLMChatMessage>> actualMessages = captureRequiredChatMessages(3);
+ List<List<LLMChatMessage>> actualMessages = captureAutoChatMessages(3);
List<LLMChatMessage> actualSecondTurnMessages = actualMessages.get(1);
assertTrue(containsMessage(actualSecondTurnMessages, "side-effect execution next_actions"));
assertTrue(containsMessage(actualSecondTurnMessages,
@@ -317,7 +317,7 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
assertTrue(containsMessage(actualSecondTurnMessages, "Do not call database_gateway_execute_update for SELECT or row-count verification."));
assertFalse(containsMessage(actualSecondTurnMessages, "\"tool_name\":\"database_gateway_execute_update\""));
assertFalse(containsMessage(actualSecondTurnMessages, "\"title\":\"Execute\""));
- List<List<Map<String, Object>>> actualTools = captureRequiredToolDefinitions(3);
+ List<List<Map<String, Object>>> actualTools = captureAutoToolDefinitions(3);
assertThat(getToolNames(actualTools.get(1)), is(List.of("database_gateway_execute_query")));
List<LLMChatMessage> actualThirdTurnMessages = actualMessages.get(2);
assertTrue(containsMessage(actualThirdTurnMessages, "previous response requested `database_gateway_execute_update`"));
@@ -344,7 +344,7 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
"sql", QUERY,
"execution_mode", "preview");
Map<String, Object> expectedQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
new LLMChatCompletion("",
List.of(
new LLMToolCall("tool-1", "database_gateway_execute_update", JsonUtils.toJsonString(previewArguments)),
@@ -361,12 +361,12 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertTrue(actual.getAssertionReport().isSuccess());
- List<List<LLMChatMessage>> actualMessages = captureRequiredChatMessages(2);
+ List<List<LLMChatMessage>> actualMessages = captureAutoChatMessages(2);
List<LLMChatMessage> actualSecondTurnMessages = actualMessages.get(1);
assertTrue(containsMessage(actualSecondTurnMessages, "\"reason\":\"tool_not_available_in_current_turn\""));
assertTrue(containsMessage(actualSecondTurnMessages, "previous response requested `database_gateway_execute_update`"));
assertTrue(containsMessage(actualSecondTurnMessages, "Available MCP tools for this turn: database_gateway_execute_query"));
- List<List<Map<String, Object>>> actualTools = captureRequiredToolDefinitions(2);
+ List<List<Map<String, Object>>> actualTools = captureAutoToolDefinitions(2);
assertThat(getToolNames(actualTools.get(1)), is(List.of("database_gateway_execute_query")));
assertThat(actual.getInteractionTrace().size(), is(2));
assertThat(actual.getInteractionTrace().get(0).getTargetName(), is("database_gateway_execute_update"));
@@ -384,7 +384,7 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
Map<String, Object> argument = Map.of("name", "plan_id", "value", "");
Map<String, Object> completionArguments = Map.of("ref", reference, "argument", argument);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_plan_mask_rule", planArguments, "plan-response"),
createToolCallCompletion("tool-2", MCPInteractionActionNames.COMPLETE, completionArguments, "completion-response"),
createToolCallCompletion("tool-3", "database_gateway_execute_query", executeQueryArguments, "query-response"));
@@ -398,7 +398,7 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertTrue(actual.getAssertionReport().isSuccess());
- List<List<Map<String, Object>>> actualTools = captureRequiredToolDefinitions(3);
+ List<List<Map<String, Object>>> actualTools = captureAutoToolDefinitions(3);
assertThat(getToolNames(actualTools.get(1)), is(List.of(MCPInteractionActionNames.COMPLETE)));
assertThat(actual.getInteractionTrace().get(1).getActionKind(), is(MCPInteractionActionNames.COMPLETION_KIND));
verify(getMCPInteractionClient()).complete(reference, "plan_id", "", Map.of());
@@ -411,7 +411,7 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
LLMMCPConversationRunner actualRunner = createRunner(3);
Map<String, Object> manualArguments = Map.of("plan_id", "plan-1", "execution_mode", "manual-only");
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_apply_workflow", manualArguments, "manual-response"),
createToolCallCompletion("tool-2", "database_gateway_execute_query", executeQueryArguments, "query-response"));
when(getMCPInteractionClient().call("database_gateway_apply_workflow", manualArguments)).thenReturn(Map.of(
@@ -430,11 +430,11 @@ class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationR
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertTrue(actual.getAssertionReport().isSuccess());
- List<List<LLMChatMessage>> actualMessages = captureRequiredChatMessages(2);
- List<List<Map<String, Object>>> actualTools = captureRequiredToolDefinitions(2);
+ List<List<LLMChatMessage>> actualMessages = captureAutoChatMessages(2);
+ List<List<Map<String, Object>>> actualTools = captureAutoToolDefinitions(2);
List<LLMChatMessage> actualSecondTurnMessages = actualMessages.get(1);
assertTrue(containsMessage(actualSecondTurnMessages, "\"distsql_artifact_count\":1"));
assertFalse(actualSecondTurnMessages.stream().map(LLMChatMessage::getContent).anyMatch(each -> each.contains("CREATE MASK RULE orders SECRET")));
- assertThat(getToolNames(actualTools.get(1)), is(List.of("database_gateway_execute_query")));
+ assertThat(getToolNames(actualTools.get(1)), is(List.of("database_gateway_apply_workflow", "database_gateway_execute_query")));
}
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerTest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerTest.java
index a4620279bd3..bdbab8846e1 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerTest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerTest.java
@@ -40,7 +40,6 @@ import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -52,7 +51,7 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(3);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"));
when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
@@ -67,7 +66,7 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
verify(getMCPInteractionClient()).open();
verify(getMCPInteractionClient()).call("database_gateway_execute_query", executeQueryArguments);
verify(getMCPInteractionClient()).close();
- List<List<Map<String, Object>>> actualTools = captureRequiredToolDefinitions(1);
+ List<List<Map<String, Object>>> actualTools = captureAutoToolDefinitions(1);
assertThat(getToolName(actualTools.getFirst().getFirst()), is("database_gateway_execute_query"));
assertThat(getMap(getFunction(actualTools.getFirst().getFirst()).get("parameters")).get("required"), is(List.of("database", "sql")));
}
@@ -77,7 +76,7 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
LLMMCPConversationRunner actualRunner = createRunner(3);
Map<String, Object> modelQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_query", modelQueryArguments, "tool-call-response"));
when(getMCPInteractionClient().call("database_gateway_execute_query", modelQueryArguments)).thenReturn(createResultSetPayload(2));
when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
@@ -89,7 +88,7 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
assertThat(actual.getInteractionTrace().getFirst().getActionOrigin(), is(MCPInteractionTraceRecord.MODEL_TOOL_CALL_ORIGIN));
assertThat(actual.getInteractionTrace().getFirst().getArguments(), is(modelQueryArguments));
verify(getMCPInteractionClient()).call("database_gateway_execute_query", modelQueryArguments);
- verify(getLLMChatClient(), never()).complete(anyList(), anyList(), eq("auto"), eq(false));
+ verify(getLLMChatClient()).complete(anyList(), anyList(), eq("auto"), eq(false));
}
@Test
@@ -103,7 +102,7 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
"database", DATABASE_NAME,
"schema", SCHEMA_NAME);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(new LLMChatCompletion("",
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(new LLMChatCompletion("",
List.of(
new LLMToolCall("tool-1", "database_gateway_search_metadata", JsonUtils.toJsonString(modelSearchArguments)),
new LLMToolCall("tool-2", "database_gateway_execute_query", JsonUtils.toJsonString(executeQueryArguments))),
@@ -128,7 +127,7 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
LLMMCPConversationRunner actualRunner = createRunner(3);
String actualQuery = "SELECT COUNT(*) AS total_orders FROM public.orders";
Map<String, Object> modelQueryArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "sql", actualQuery);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_query", modelQueryArguments, "tool-call-response"));
when(getMCPInteractionClient().call("database_gateway_execute_query", modelQueryArguments)).thenReturn(createResultSetPayload(2));
when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
@@ -138,7 +137,7 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
assertTrue(actual.getAssertionReport().isSuccess());
assertThat(actual.getInteractionTrace().getFirst().getArguments(), is(modelQueryArguments));
- verify(getLLMChatClient(), never()).complete(anyList(), anyList(), eq("auto"), eq(false));
+ verify(getLLMChatClient()).complete(anyList(), anyList(), eq("auto"), eq(false));
}
@Test
@@ -152,7 +151,7 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
"schema", SCHEMA_NAME,
"sql", "UPDATE orders SET status = status WHERE order_id = -1",
"execution_mode", "preview");
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(new LLMChatCompletion("",
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(new LLMChatCompletion("",
List.of(
new LLMToolCall("tool-1", "database_gateway_execute_query", JsonUtils.toJsonString(executeQueryArguments)),
new LLMToolCall("tool-2", "database_gateway_execute_update", JsonUtils.toJsonString(executeUpdateArguments))),
@@ -183,7 +182,7 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
LLME2EScenario actualScenario = createScenario(actualToolNames);
LLMMCPConversationRunner actualRunner = createRunner(3);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(new LLMChatCompletion("",
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(new LLMChatCompletion("",
List.of(
new LLMToolCall("tool-1", MCPInteractionActionNames.LIST_TOOLS, "{}"),
new LLMToolCall("tool-2", MCPInteractionActionNames.LIST_RESOURCES, "{}"),
@@ -228,7 +227,7 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
Map<String, Object> applyArguments = Map.of("plan_id", "plan-1", "execution_mode", "manual-only");
Map<String, Object> validateArguments = Map.of("plan_id", "plan-1");
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(new LLMChatCompletion("",
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(new LLMChatCompletion("",
List.of(
new LLMToolCall("tool-1", MCPInteractionActionNames.READ_RESOURCE, JsonUtils.toJsonString(Map.of("uri", "shardingsphere://features/mask/algorithms"))),
new LLMToolCall("tool-2", "database_gateway_plan_mask_rule", JsonUtils.toJsonString(planArguments)),
@@ -271,7 +270,7 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
Map<String, Object> completionReference = Map.of("type", "ref/prompt", "name", PROMPT_NAME);
Map<String, String> completionContext = Map.of("database", DATABASE_NAME);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(new LLMChatCompletion("",
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(new LLMChatCompletion("",
List.of(
new LLMToolCall("tool-1", MCPInteractionActionNames.LIST_PROMPTS, "{}"),
new LLMToolCall("tool-2", MCPInteractionActionNames.GET_PROMPT, JsonUtils.toJsonString(Map.of("name", PROMPT_NAME, "arguments", promptArguments))),
@@ -298,7 +297,7 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
verify(getMCPInteractionClient()).listPrompts();
verify(getMCPInteractionClient()).getPrompt(PROMPT_NAME, promptArguments);
verify(getMCPInteractionClient()).complete(completionReference, "schema", "pub", completionContext);
- List<List<Map<String, Object>>> actualTools = captureRequiredToolDefinitions(1);
+ List<List<Map<String, Object>>> actualTools = captureAutoToolDefinitions(1);
assertThat(getToolName(actualTools.getFirst().getFirst()), is(MCPInteractionActionNames.LIST_PROMPTS));
}
@@ -306,13 +305,13 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
void assertRunWithSearchMetadataToolDefinition() throws IOException, InterruptedException {
LLME2EScenario actualScenario = createScenario(List.of("database_gateway_search_metadata"));
LLMMCPConversationRunner actualRunner = createRunner(1);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response"));
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertThat(actual.getAssertionReport().getFailureType(), is("missing_required_tool_coverage"));
- List<List<Map<String, Object>>> actualTools = captureRequiredToolDefinitions(1);
+ List<List<Map<String, Object>>> actualTools = captureAutoToolDefinitions(1);
assertThat(getToolName(actualTools.getFirst().getFirst()), is("database_gateway_search_metadata"));
assertThat(getRequiredFields(actualTools.getFirst().getFirst()), is(List.of()));
assertThat(getPropertyType(actualTools.getFirst().getFirst(), "database"), is("string"));
@@ -323,7 +322,7 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
}
@Test
- void assertRunOffersOnlyNextMissingRequiredTool() throws IOException, InterruptedException {
+ void assertRunOffersAllAllowedTools() throws IOException, InterruptedException {
List<String> toolNames = List.of("database_gateway_search_metadata", MCPInteractionActionNames.READ_RESOURCE, "database_gateway_execute_query");
LLME2EScenario actualScenario = createScenario(toolNames);
LLMMCPConversationRunner actualRunner = createRunner(4);
@@ -331,7 +330,7 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
Map<String, Object> searchArguments = Map.of("query", TABLE_NAME);
Map<String, Object> readResourceArguments = Map.of("uri", tableResourceUri);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_search_metadata", searchArguments, "search-response"),
createToolCallCompletion("tool-2", MCPInteractionActionNames.READ_RESOURCE, readResourceArguments, "resource-response"),
createToolCallCompletion("tool-3", "database_gateway_execute_query", executeQueryArguments, "query-response"));
@@ -343,10 +342,10 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertTrue(actual.getAssertionReport().isSuccess());
- List<List<Map<String, Object>>> actualTools = captureRequiredToolDefinitions(3);
- assertThat(getToolNames(actualTools.getFirst()), is(List.of("database_gateway_search_metadata")));
- assertThat(getToolNames(actualTools.get(1)), is(List.of(MCPInteractionActionNames.READ_RESOURCE)));
- assertThat(getToolNames(actualTools.get(2)), is(List.of("database_gateway_execute_query")));
+ List<List<Map<String, Object>>> actualTools = captureAutoToolDefinitions(3);
+ for (List<Map<String, Object>> each : actualTools) {
+ assertThat(getToolNames(each), is(toolNames));
+ }
}
@Test
@@ -354,7 +353,7 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
Map<String, Object> finalAnswerPayload = createFinalAnswerPayload(List.of(), 2);
finalAnswerPayload.put("interactionSequence", List.of(Map.of("tool", "database_gateway_execute_query", "arguments", executeQueryArguments)));
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"));
when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
@@ -372,7 +371,7 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
LLMMCPConversationRunner actualRunner = createRunner(2);
Map<String, Object> searchMetadataArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "query", TABLE_NAME);
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(new LLMChatCompletion("",
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(new LLMChatCompletion("",
List.of(
new LLMToolCall("tool-1", "database_gateway_search_metadata", JsonUtils.toJsonString(searchMetadataArguments)),
new LLMToolCall("tool-2", "database_gateway_search_metadata", JsonUtils.toJsonString(searchMetadataArguments)),
@@ -393,7 +392,7 @@ class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest
Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
Map<String, Object> finalAnswerPayload = createFinalAnswerPayload(List.of("database_gateway_execute_query"), 2);
finalAnswerPayload.put("query", "SELECT COUNT(*) AS total_orders FROM public.orders");
- when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"));
when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationTurnPlanner.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationTurnPlanner.java
index 47bc863365d..15be94e8963 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationTurnPlanner.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationTurnPlanner.java
@@ -29,7 +29,6 @@ import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
-import java.util.stream.Collectors;
@AllArgsConstructor(access = AccessLevel.PACKAGE)
final class LLMMCPConversationTurnPlanner {
@@ -68,8 +67,7 @@ final class LLMMCPConversationTurnPlanner {
return List.of(missingReadOnlyToolNames.getFirst());
}
}
- List<String> missingToolNames = findMissingAllowedToolNames(scenario, interactionTrace);
- return missingToolNames.isEmpty() ? scenario.getAllowedToolNames() : List.of(missingToolNames.getFirst());
+ return scenario.getAllowedToolNames();
}
List<String> createImmediateNextActionToolNames(final List<MCPInteractionTraceRecord> interactionTrace) {
@@ -80,16 +78,8 @@ final class LLMMCPConversationTurnPlanner {
return immediateActionName.isEmpty() ? List.of() : List.of(immediateActionName);
}
- String createToolChoice(final LLME2EScenario scenario, final List<MCPInteractionTraceRecord> interactionTrace, final boolean finalAnswerRequested) {
- if (finalAnswerRequested) {
- return "none";
- }
- return LLMMCPInteractionCoverage.hasRequiredInteractionCoverage(scenario.getRequiredToolNames(), interactionTrace) ? "auto" : "required";
- }
-
- private List<String> findMissingAllowedToolNames(final LLME2EScenario scenario, final List<MCPInteractionTraceRecord> interactionTrace) {
- return LLMMCPInteractionCoverage.findMissingRequiredInteractionNames(
- scenario.getRequiredToolNames(), interactionTrace).stream().filter(each -> scenario.getAllowedToolNames().contains(each)).collect(Collectors.toList());
+ String createToolChoice(final boolean finalAnswerRequested) {
+ return finalAnswerRequested ? "none" : "auto";
}
private List<String> findMissingReadOnlyToolNames(final LLME2EScenario scenario, final List<MCPInteractionTraceRecord> interactionTrace) {
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationTurnPlannerTest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationTurnPlannerTest.java
index 0762bfddf19..72034874180 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationTurnPlannerTest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationTurnPlannerTest.java
@@ -83,22 +83,23 @@ class LLMMCPConversationTurnPlannerTest {
}
@Test
- void assertCreateToolChoiceWithMissingCoverage() {
+ void assertCreateTurnToolNamesExposesAllowedToolsWithoutServerGuidance() {
LLMMCPConversationTurnPlanner planner = new LLMMCPConversationTurnPlanner(Set.of());
- assertThat(planner.createToolChoice(createScenario(List.of("database_gateway_execute_query"), List.of("database_gateway_execute_query")), List.of(), false), is("required"));
+ assertThat(planner.createTurnToolNames(createScenario(
+ List.of(MCPInteractionActionNames.READ_RESOURCE, "database_gateway_execute_query"), List.of("database_gateway_execute_query")), List.of()),
+ is(List.of(MCPInteractionActionNames.READ_RESOURCE, "database_gateway_execute_query")));
}
@Test
- void assertCreateToolChoiceWithCoveredRequiredTools() {
+ void assertCreateToolChoice() {
LLMMCPConversationTurnPlanner planner = new LLMMCPConversationTurnPlanner(Set.of());
- assertThat(planner.createToolChoice(createScenario(List.of("database_gateway_execute_query"), List.of("database_gateway_execute_query")),
- List.of(createTraceRecord("database_gateway_execute_query", Map.of("row_objects", List.of()))), false), is("auto"));
+ assertThat(planner.createToolChoice(false), is("auto"));
}
@Test
void assertCreateToolChoiceWithFinalAnswer() {
LLMMCPConversationTurnPlanner planner = new LLMMCPConversationTurnPlanner(Set.of());
- assertThat(planner.createToolChoice(createScenario(List.of("database_gateway_execute_query"), List.of("database_gateway_execute_query")), List.of(), true), is("none"));
+ assertThat(planner.createToolChoice(true), is("none"));
}
private MCPInteractionTraceRecord createTraceRecord(final String targetName, final Map<String, Object> structuredContent) {
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPInteractionCoverage.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPInteractionCoverage.java
index 2a7fb85dd8e..3f703cfc248 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPInteractionCoverage.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPInteractionCoverage.java
@@ -49,10 +49,6 @@ final class LLMMCPInteractionCoverage {
}
private static boolean isSuccessfulInteraction(final MCPInteractionTraceRecord interactionTraceRecord) {
- return interactionTraceRecord.isValid() && !interactionTraceRecord.getStructuredContent().containsKey("error_code") && !isHarnessOrigin(interactionTraceRecord.getActionOrigin());
- }
-
- private static boolean isHarnessOrigin(final String actionOrigin) {
- return MCPInteractionTraceRecord.HARNESS_TEXT_RECOVERY_ORIGIN.equals(actionOrigin);
+ return interactionTraceRecord.isValid() && !interactionTraceRecord.getStructuredContent().containsKey("error_code");
}
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPInteractionCoverageTest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPInteractionCoverageTest.java
index b2033309b3f..93fd12eb3c9 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPInteractionCoverageTest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPInteractionCoverageTest.java
@@ -51,8 +51,8 @@ class LLMMCPInteractionCoverageTest {
}
@Test
- void assertHasRequiredInteractionCoverageExcludesHarnessOrigin() {
- assertFalse(LLMMCPInteractionCoverage.hasRequiredInteractionCoverage(List.of("database_gateway_execute_query"), List.of(
+ void assertHasRequiredInteractionCoverageWithHarnessOrigin() {
+ assertTrue(LLMMCPInteractionCoverage.hasRequiredInteractionCoverage(List.of("database_gateway_execute_query"), List.of(
new MCPInteractionTraceRecord(1, "tool_call", MCPInteractionTraceRecord.HARNESS_TEXT_RECOVERY_ORIGIN,
"database_gateway_execute_query", Map.of(), Map.of("result_kind", "result_set"), true, 0L))));
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPTraceRecordFactory.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPTraceRecordFactory.java
index 8453da17865..5b77deb3111 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPTraceRecordFactory.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPTraceRecordFactory.java
@@ -27,7 +27,9 @@ final class LLMMCPTraceRecordFactory {
MCPInteractionTraceRecord createTraceRecord(final int sequence, final String actionName, final String actionOrigin, final Map<String, Object> args,
final Map<String, Object> structuredContent, final long latencyMillis) {
- String bridgeActionOrigin = MCPInteractionTraceRecord.PROTOCOL_BRIDGE_ORIGIN;
+ String bridgeActionOrigin = MCPInteractionTraceRecord.HARNESS_TEXT_RECOVERY_ORIGIN.equals(actionOrigin)
+ ? MCPInteractionTraceRecord.HARNESS_TEXT_RECOVERY_ORIGIN
+ : MCPInteractionTraceRecord.PROTOCOL_BRIDGE_ORIGIN;
if (MCPInteractionActionNames.LIST_TOOLS.equals(actionName)) {
return new MCPInteractionTraceRecord(sequence, MCPInteractionActionNames.TOOL_LIST_KIND, bridgeActionOrigin, MCPInteractionActionNames.LIST_TOOLS,
Map.of(), structuredContent, true, latencyMillis);
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/MCPBuilderEvaluationCatalog.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/MCPBuilderEvaluationCatalog.java
index 3e2404a46ed..f81d847c963 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/MCPBuilderEvaluationCatalog.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/MCPBuilderEvaluationCatalog.java
@@ -55,7 +55,7 @@ public final class MCPBuilderEvaluationCatalog {
return new EvaluationSuite(
root.getAttribute("standard_reference"),
root.getAttribute("operation_mode"),
- Boolean.parseBoolean(root.getAttribute("requires_external_model")),
+ readBooleanAttribute(root, "requires_external_model"),
loadCases(root.getElementsByTagName("qa_pair")));
} catch (final ParserConfigurationException | SAXException ex) {
throw new IOException("Failed to parse MCP Builder evaluation catalog.", ex);
@@ -79,13 +79,22 @@ public final class MCPBuilderEvaluationCatalog {
result.add(new EvaluationCase(
each.getAttribute("id"),
each.getAttribute("category"),
- Boolean.parseBoolean(each.getAttribute("read_only")),
+ readBooleanAttribute(each, "read_only"),
readElementText(each, "question"),
readElementText(each, "answer")));
}
return result;
}
+ private boolean readBooleanAttribute(final Element element, final String attributeName) {
+ String value = element.getAttribute(attributeName);
+ if (!"true".equalsIgnoreCase(value) && !"false".equalsIgnoreCase(value)) {
+ throw new IllegalStateException(String.format("MCP Builder evaluation element `%s` attribute `%s` must be `true` or `false`, but was `%s`.",
+ element.getTagName(), attributeName, value));
+ }
+ return Boolean.parseBoolean(value);
+ }
+
private String readElementText(final Element parent, final String tagName) {
NodeList elements = parent.getElementsByTagName(tagName);
if (1 != elements.getLength()) {
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/evaluation/MCPBuilderEvaluationE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/evaluation/MCPBuilderEvaluationE2ETest.java
index 7a1063ce547..d92545e0269 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/evaluation/MCPBuilderEvaluationE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/evaluation/MCPBuilderEvaluationE2ETest.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.test.e2e.mcp.llm.suite.evaluation;
import org.apache.shardingsphere.mcp.support.database.metadata.jdbc.RuntimeDatabaseConfiguration;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.apache.shardingsphere.test.e2e.mcp.llm.config.LLME2EConfiguration;
import org.apache.shardingsphere.test.e2e.mcp.llm.fixture.LLMRuntimeFixtureFactory;
import org.apache.shardingsphere.test.e2e.mcp.llm.fixture.LLMRuntimeFixtureFactory.Fixture;
@@ -40,7 +39,7 @@ import java.util.List;
import java.util.Map;
@Tag("llm-e2e")
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class MCPBuilderEvaluationE2ETest extends AbstractConfigBackedRuntimeE2ETest {
@@ -75,10 +74,6 @@ class MCPBuilderEvaluationE2ETest extends AbstractConfigBackedRuntimeE2ETest {
}
}
- private static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
@Test
void assertAutonomousEvaluation() throws IOException, InterruptedException {
prepareRuntimeFixture();
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/smoke/LLMSmokeE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/smoke/LLMSmokeE2ETest.java
index 01cf010c4d3..93c57013427 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/smoke/LLMSmokeE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/smoke/LLMSmokeE2ETest.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.test.e2e.mcp.llm.suite.smoke;
import org.apache.shardingsphere.mcp.support.database.metadata.jdbc.RuntimeDatabaseConfiguration;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.apache.shardingsphere.test.e2e.mcp.llm.config.LLME2EConfiguration;
import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.LLMConversationExecutor;
import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.artifact.LLME2EAssertionReport;
@@ -48,7 +47,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@Tag("llm-e2e")
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class LLMSmokeE2ETest extends AbstractConfigBackedRuntimeE2ETest {
@@ -101,10 +100,6 @@ class LLMSmokeE2ETest extends AbstractConfigBackedRuntimeE2ETest {
currentTransport = null;
}
- private static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
static Stream<Arguments> getTestCases() {
return Stream.of(
Arguments.of("llm-smoke-mysql-http", RuntimeTransport.HTTP),
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/LLMUsabilitySuiteE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/LLMUsabilitySuiteE2ETest.java
index 230bd8161d2..d142e3bc818 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/LLMUsabilitySuiteE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/LLMUsabilitySuiteE2ETest.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.test.e2e.mcp.llm.suite.usability;
import org.apache.shardingsphere.mcp.support.database.metadata.jdbc.RuntimeDatabaseConfiguration;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.apache.shardingsphere.test.e2e.mcp.llm.config.LLME2EConfiguration;
import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.LLMConversationExecutor;
import org.apache.shardingsphere.test.e2e.mcp.llm.fixture.LLMRuntimeSupport;
@@ -44,7 +43,7 @@ import java.util.Map;
import java.util.stream.Stream;
@Tag("llm-e2e")
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class LLMUsabilitySuiteE2ETest extends AbstractConfigBackedRuntimeE2ETest {
@@ -98,10 +97,6 @@ class LLMUsabilitySuiteE2ETest extends AbstractConfigBackedRuntimeE2ETest {
currentTransport = null;
}
- private static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
static Stream<Arguments> getTestCases() {
return isFullTransportMatrixEnabled()
? Stream.of(
@@ -111,7 +106,12 @@ class LLMUsabilitySuiteE2ETest extends AbstractConfigBackedRuntimeE2ETest {
}
private static boolean isFullTransportMatrixEnabled() {
- return Boolean.parseBoolean(System.getProperty(FULL_TRANSPORT_MATRIX_PROPERTY, "false"));
+ String configuredValue = System.getProperty(FULL_TRANSPORT_MATRIX_PROPERTY, "false").trim();
+ if (!"true".equalsIgnoreCase(configuredValue) && !"false".equalsIgnoreCase(configuredValue)) {
+ throw new IllegalArgumentException(String.format("MCP LLM E2E property `%s` must be `true` or `false`, but was `%s`.",
+ FULL_TRANSPORT_MATRIX_PROPERTY, configuredValue));
+ }
+ return Boolean.parseBoolean(configuredValue);
}
@ParameterizedTest(name = "{0}")
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/LLMUsabilitySuiteRunner.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/LLMUsabilitySuiteRunner.java
index d0d6340c3b5..2d237c7b73d 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/LLMUsabilitySuiteRunner.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/LLMUsabilitySuiteRunner.java
@@ -22,6 +22,7 @@ import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.LLMConversationEx
import org.apache.shardingsphere.test.e2e.mcp.llm.scenario.LLME2EScenario;
import org.apache.shardingsphere.test.e2e.mcp.llm.suite.usability.assessment.LLMUsabilityMetricCalculator;
import org.apache.shardingsphere.test.e2e.mcp.llm.suite.usability.assessment.LLMUsabilityReportWriter;
+import org.apache.shardingsphere.test.e2e.mcp.llm.suite.usability.assessment.LLMUsabilityDimension;
import org.apache.shardingsphere.test.e2e.mcp.llm.suite.usability.assessment.LLMUsabilityScenarioResult;
import org.apache.shardingsphere.test.e2e.mcp.llm.suite.usability.assessment.LLMUsabilityScorecard;
import org.apache.shardingsphere.test.e2e.mcp.llm.suite.usability.scenario.LLMUsabilityScenario;
@@ -92,11 +93,14 @@ final class LLMUsabilitySuiteRunner {
assertTrue(scorecard.isFullScore(), actualFailureSummary);
assertThat(actualFailureSummary, scorecard.getScenarioResults().size(), is(evaluatedSuite.scenarios().size()));
assertThat(actualFailureSummary, scorecard.getTaskSuccessRate(), is(1.0D));
- assertThat(actualFailureSummary, scorecard.getNaturalTaskSuccessRate(), is(1.0D));
- assertThat(actualFailureSummary, scorecard.getProtocolContractSuccessRate(), is(1.0D));
+ if (0 < scorecard.getNaturalTaskSampleCount()) {
+ assertThat(actualFailureSummary, scorecard.getNaturalTaskSuccessRate(), is(1.0D));
+ }
+ if (0 < scorecard.getProtocolContractSampleCount()) {
+ assertThat(actualFailureSummary, scorecard.getProtocolContractSuccessRate(), is(1.0D));
+ }
assertThat(actualFailureSummary, scorecard.getFirstCorrectActionRate(), is(1.0D));
assertThat(actualFailureSummary, scorecard.getInvalidCallRate(), is(0.0D));
- assertThat(actualFailureSummary, scorecard.getQueryAnswerFidelity(), is(1.0D));
assertThat(actualFailureSummary, scorecard.getBoundaryConfusionRate(), is(0.0D));
assertThat(actualFailureSummary, scorecard.getNextActionFollowRate(), is(1.0D));
assertThat(actualFailureSummary, scorecard.getApprovalViolationRate(), is(0.0D));
@@ -153,13 +157,20 @@ final class LLMUsabilitySuiteRunner {
assertRate("taskSuccessRate", scorecard.getTaskSuccessRate());
assertRate("naturalTaskSuccessRate", scorecard.getNaturalTaskSuccessRate());
assertRate("protocolContractSuccessRate", scorecard.getProtocolContractSuccessRate());
+ assertThat(scorecard.getNaturalTaskSampleCount(), is((int) scorecard.getScenarioResults().stream()
+ .filter(each -> each.getTags().contains(LLMUsabilityScenario.NATURAL_TASK_TAG)).count()));
+ assertThat(scorecard.getProtocolContractSampleCount(), is((int) scorecard.getScenarioResults().stream()
+ .filter(each -> each.getTags().contains(LLMUsabilityScenario.PROTOCOL_CONTRACT_TAG)).count()));
assertRate("firstCorrectActionRate", scorecard.getFirstCorrectActionRate());
assertRate("invalidCallRate", scorecard.getInvalidCallRate());
assertRange("averageRoundTrips", scorecard.getAverageRoundTrips(), 0.0D, Double.MAX_VALUE);
- assertRate("queryAnswerFidelity", scorecard.getQueryAnswerFidelity());
assertRate("boundaryConfusionRate", scorecard.getBoundaryConfusionRate());
assertRate("resourceHitRate", scorecard.getResourceHitRate());
assertRate("recoveryRate", scorecard.getRecoveryRate());
+ assertThat(scorecard.getResourceHitSampleCount(), is((int) scorecard.getScenarioResults().stream()
+ .filter(each -> LLMUsabilityDimension.RESOURCE == each.getDimension()).count()));
+ assertThat(scorecard.getRecoverySampleCount(), is((int) scorecard.getScenarioResults().stream()
+ .filter(each -> LLMUsabilityDimension.RECOVERY == each.getDimension()).count()));
assertRate("nextActionFollowRate", scorecard.getNextActionFollowRate());
assertRate("approvalViolationRate", scorecard.getApprovalViolationRate());
assertRate("nativeToolCallRate", scorecard.getNativeToolCallRate());
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityMetricCalculator.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityMetricCalculator.java
index 1f21a613c72..39faea4432e 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityMetricCalculator.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityMetricCalculator.java
@@ -39,8 +39,6 @@ public final class LLMUsabilityMetricCalculator {
private static final double NO_INVALID_CALL_WEIGHT = 10.0D;
- private static final double QUERY_ANSWER_FIDELITY_WEIGHT = 10.0D;
-
private static final double NO_BOUNDARY_CONFUSION_WEIGHT = 10.0D;
private static final double RESOURCE_HIT_WEIGHT = 10.0D;
@@ -84,7 +82,6 @@ public final class LLMUsabilityMetricCalculator {
message = "Scenario expected one recoverable MCP error with category `" + scenario.getExpectedRecoveryCategory() + "` before final success.";
}
boolean recoveredAfterError = success && (!scenario.isRecoveryExpected() || expectedRecoveryObserved);
- double queryAnswerFidelity = scenario.isQueryScenario() && success ? 1.0D : 0.0D;
boolean nextActionFollowed = traceMetrics.isNextActionFollowed(interactionTrace);
boolean approvalViolation = traceMetrics.hasApprovalViolation(interactionTrace);
boolean nativeToolCallCoverage = traceMetrics.hasNativeRequiredToolCoverage(scenario.getLlmScenario().getRequiredToolNames(), interactionTrace);
@@ -102,7 +99,6 @@ public final class LLMUsabilityMetricCalculator {
.roundTripCount(interactionTrace.size())
.resourceHit(resourceHit)
.recoveredAfterError(recoveredAfterError)
- .queryAnswerFidelity(queryAnswerFidelity)
.boundaryConfusion(boundaryConfusion)
.nextActionFollowed(nextActionFollowed)
.approvalViolation(approvalViolation)
@@ -122,36 +118,44 @@ public final class LLMUsabilityMetricCalculator {
*/
public LLMUsabilityScorecard createScorecard(final String suiteId, final String runId, final List<LLMUsabilityScenarioResult> scenarioResults) {
double taskSuccessRate = calculateRate(scenarioResults, LLMUsabilityScenarioResult::isSuccess);
+ int naturalTaskSampleCount = countTag(scenarioResults, LLMUsabilityScenario.NATURAL_TASK_TAG);
double naturalTaskSuccessRate = getTaggedSuccessRate(scenarioResults, LLMUsabilityScenario.NATURAL_TASK_TAG);
+ int protocolContractSampleCount = countTag(scenarioResults, LLMUsabilityScenario.PROTOCOL_CONTRACT_TAG);
double protocolContractSuccessRate = getTaggedSuccessRate(scenarioResults, LLMUsabilityScenario.PROTOCOL_CONTRACT_TAG);
double firstCorrectActionRate = calculateRate(scenarioResults, LLMUsabilityScenarioResult::isFirstCorrectAction);
double invalidCallRate = getInvalidCallRate(scenarioResults);
double averageRoundTrips = getAverageRoundTrips(scenarioResults);
- double queryAnswerFidelity = getQueryAnswerFidelity(scenarioResults);
double boundaryConfusionRate = calculateRate(scenarioResults, LLMUsabilityScenarioResult::isBoundaryConfusion);
+ int resourceHitSampleCount = countDimension(scenarioResults, LLMUsabilityDimension.RESOURCE);
double resourceHitRate = getResourceHitRate(scenarioResults);
+ int recoverySampleCount = countDimension(scenarioResults, LLMUsabilityDimension.RECOVERY);
double recoveryRate = getRecoveryRate(scenarioResults);
double nextActionFollowRate = calculateRate(scenarioResults, LLMUsabilityScenarioResult::isNextActionFollowed);
double approvalViolationRate = calculateRate(scenarioResults, LLMUsabilityScenarioResult::isApprovalViolation);
double nativeToolCallRate = calculateRate(scenarioResults, LLMUsabilityScenarioResult::isNativeToolCallCoverage);
double harnessRecoveryRate = calculateRate(scenarioResults, LLMUsabilityScenarioResult::isHarnessRecoveryUsed);
- double overallScore = calculateOverallScore(taskSuccessRate, firstCorrectActionRate, invalidCallRate, queryAnswerFidelity,
- boundaryConfusionRate, resourceHitRate, recoveryRate, nextActionFollowRate, approvalViolationRate);
+ double overallScore = scenarioResults.isEmpty() ? 0.0D
+ : calculateOverallScore(taskSuccessRate, firstCorrectActionRate, invalidCallRate, boundaryConfusionRate,
+ resourceHitRate, resourceHitSampleCount, recoveryRate, recoverySampleCount, nextActionFollowRate, approvalViolationRate);
return LLMUsabilityScorecard.builder()
.suiteId(suiteId)
.runId(runId)
.overallScore(overallScore)
- .fullScore(isFullScore(overallScore, nativeToolCallRate, harnessRecoveryRate, naturalTaskSuccessRate, protocolContractSuccessRate))
+ .fullScore(isFullScore(overallScore, nativeToolCallRate, harnessRecoveryRate, naturalTaskSuccessRate, naturalTaskSampleCount,
+ protocolContractSuccessRate, protocolContractSampleCount))
.taskSuccessRate(taskSuccessRate)
.naturalTaskSuccessRate(naturalTaskSuccessRate)
+ .naturalTaskSampleCount(naturalTaskSampleCount)
.protocolContractSuccessRate(protocolContractSuccessRate)
+ .protocolContractSampleCount(protocolContractSampleCount)
.firstCorrectActionRate(firstCorrectActionRate)
.invalidCallRate(invalidCallRate)
.averageRoundTrips(averageRoundTrips)
- .queryAnswerFidelity(queryAnswerFidelity)
.boundaryConfusionRate(boundaryConfusionRate)
.resourceHitRate(resourceHitRate)
+ .resourceHitSampleCount(resourceHitSampleCount)
.recoveryRate(recoveryRate)
+ .recoverySampleCount(recoverySampleCount)
.nextActionFollowRate(nextActionFollowRate)
.approvalViolationRate(approvalViolationRate)
.nativeToolCallRate(nativeToolCallRate)
@@ -161,17 +165,26 @@ public final class LLMUsabilityMetricCalculator {
}
private double calculateOverallScore(final double taskSuccessRate, final double firstCorrectActionRate, final double invalidCallRate,
- final double queryAnswerFidelity, final double boundaryConfusionRate, final double resourceHitRate,
- final double recoveryRate, final double nextActionFollowRate, final double approvalViolationRate) {
- return TASK_SUCCESS_WEIGHT * taskSuccessRate
+ final double boundaryConfusionRate, final double resourceHitRate, final int resourceHitSampleCount,
+ final double recoveryRate, final int recoverySampleCount, final double nextActionFollowRate,
+ final double approvalViolationRate) {
+ double weightedScore = TASK_SUCCESS_WEIGHT * taskSuccessRate
+ FIRST_CORRECT_ACTION_WEIGHT * firstCorrectActionRate
+ NO_INVALID_CALL_WEIGHT * invertRate(invalidCallRate)
- + QUERY_ANSWER_FIDELITY_WEIGHT * queryAnswerFidelity
+ NO_BOUNDARY_CONFUSION_WEIGHT * invertRate(boundaryConfusionRate)
- + RESOURCE_HIT_WEIGHT * resourceHitRate
- + RECOVERY_WEIGHT * recoveryRate
+ NEXT_ACTION_FOLLOW_WEIGHT * nextActionFollowRate
+ NO_APPROVAL_VIOLATION_WEIGHT * invertRate(approvalViolationRate);
+ double applicableWeight = TASK_SUCCESS_WEIGHT + FIRST_CORRECT_ACTION_WEIGHT + NO_INVALID_CALL_WEIGHT
+ + NO_BOUNDARY_CONFUSION_WEIGHT + NEXT_ACTION_FOLLOW_WEIGHT + NO_APPROVAL_VIOLATION_WEIGHT;
+ if (0 < resourceHitSampleCount) {
+ weightedScore += RESOURCE_HIT_WEIGHT * resourceHitRate;
+ applicableWeight += RESOURCE_HIT_WEIGHT;
+ }
+ if (0 < recoverySampleCount) {
+ weightedScore += RECOVERY_WEIGHT * recoveryRate;
+ applicableWeight += RECOVERY_WEIGHT;
+ }
+ return FULL_SCORE * weightedScore / applicableWeight;
}
private double invertRate(final double rate) {
@@ -179,9 +192,11 @@ public final class LLMUsabilityMetricCalculator {
}
private boolean isFullScore(final double overallScore, final double nativeToolCallRate, final double harnessRecoveryRate,
- final double naturalTaskSuccessRate, final double protocolContractSuccessRate) {
+ final double naturalTaskSuccessRate, final int naturalTaskSampleCount,
+ final double protocolContractSuccessRate, final int protocolContractSampleCount) {
return 0 == Double.compare(FULL_SCORE, overallScore) && 0 == Double.compare(1.0D, nativeToolCallRate) && 0 == Double.compare(0.0D, harnessRecoveryRate)
- && 0 == Double.compare(1.0D, naturalTaskSuccessRate) && 0 == Double.compare(1.0D, protocolContractSuccessRate);
+ && (0 == naturalTaskSampleCount || 0 == Double.compare(1.0D, naturalTaskSuccessRate))
+ && (0 == protocolContractSampleCount || 0 == Double.compare(1.0D, protocolContractSuccessRate));
}
private double calculateRate(final List<LLMUsabilityScenarioResult> scenarioResults, final Predicate<LLMUsabilityScenarioResult> matcher) {
@@ -204,28 +219,17 @@ public final class LLMUsabilityMetricCalculator {
taggedResults.add(each);
}
}
- return taggedResults.isEmpty() ? 1.0D : calculateRate(taggedResults, LLMUsabilityScenarioResult::isSuccess);
+ return calculateRate(taggedResults, LLMUsabilityScenarioResult::isSuccess);
}
- private double getQueryAnswerFidelity(final List<LLMUsabilityScenarioResult> scenarioResults) {
- List<LLMUsabilityScenarioResult> queryResults = new LinkedList<>();
+ private int countTag(final List<LLMUsabilityScenarioResult> scenarioResults, final String tag) {
+ int result = 0;
for (LLMUsabilityScenarioResult each : scenarioResults) {
- if (LLMUsabilityDimension.TOOL == each.getDimension() || LLMUsabilityDimension.RECOVERY == each.getDimension() || LLMUsabilityDimension.RESOURCE == each.getDimension()) {
- queryResults.add(each);
+ if (each.getTags().contains(tag)) {
+ result++;
}
}
- return getAverageQueryAnswerFidelity(queryResults);
- }
-
- private double getAverageQueryAnswerFidelity(final List<LLMUsabilityScenarioResult> scenarioResults) {
- if (scenarioResults.isEmpty()) {
- return 1.0D;
- }
- double total = 0.0D;
- for (LLMUsabilityScenarioResult each : scenarioResults) {
- total += each.getQueryAnswerFidelity();
- }
- return total / scenarioResults.size();
+ return result;
}
private double getResourceHitRate(final List<LLMUsabilityScenarioResult> scenarioResults) {
@@ -235,7 +239,7 @@ public final class LLMUsabilityMetricCalculator {
resourceRequiredResults.add(each);
}
}
- return resourceRequiredResults.isEmpty() ? 1.0D : calculateRate(resourceRequiredResults, LLMUsabilityScenarioResult::isResourceHit);
+ return calculateRate(resourceRequiredResults, LLMUsabilityScenarioResult::isResourceHit);
}
private double getRecoveryRate(final List<LLMUsabilityScenarioResult> scenarioResults) {
@@ -245,7 +249,17 @@ public final class LLMUsabilityMetricCalculator {
recoveryResults.add(each);
}
}
- return recoveryResults.isEmpty() ? 1.0D : calculateRate(recoveryResults, LLMUsabilityScenarioResult::isRecoveredAfterError);
+ return calculateRate(recoveryResults, LLMUsabilityScenarioResult::isRecoveredAfterError);
+ }
+
+ private int countDimension(final List<LLMUsabilityScenarioResult> scenarioResults, final LLMUsabilityDimension dimension) {
+ int result = 0;
+ for (LLMUsabilityScenarioResult each : scenarioResults) {
+ if (dimension == each.getDimension()) {
+ result++;
+ }
+ }
+ return result;
}
private double getInvalidCallRate(final List<LLMUsabilityScenarioResult> scenarioResults) {
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityMetricCalculatorTest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityMetricCalculatorTest.java
index e5a3854cb48..40a8c2a1814 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityMetricCalculatorTest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityMetricCalculatorTest.java
@@ -209,13 +209,26 @@ class LLMUsabilityMetricCalculatorTest {
assertThat(actual.getNextActionFollowRate(), is(1.0D));
assertThat(actual.getApprovalViolationRate(), is(0.5D));
assertThat(actual.getNaturalTaskSuccessRate(), is(1.0D));
- assertThat(actual.getProtocolContractSuccessRate(), is(1.0D));
+ assertThat(actual.getNaturalTaskSampleCount(), is(2));
+ assertThat(actual.getProtocolContractSuccessRate(), is(0.0D));
+ assertThat(actual.getProtocolContractSampleCount(), is(0));
+ assertThat(actual.getResourceHitSampleCount(), is(0));
+ assertThat(actual.getRecoverySampleCount(), is(0));
assertThat(actual.getNativeToolCallRate(), is(1.0D));
assertThat(actual.getHarnessRecoveryRate(), is(0.0D));
assertTrue(actual.getOverallScore() < 100.0D);
assertFalse(actual.isFullScore());
}
+ @Test
+ void assertCreateEmptyScorecard() {
+ LLMUsabilityScorecard actual = new LLMUsabilityMetricCalculator().createScorecard("suite", "run", List.of());
+ assertThat(actual.getOverallScore(), is(0.0D));
+ assertFalse(actual.isFullScore());
+ assertThat(actual.getNaturalTaskSampleCount(), is(0));
+ assertThat(actual.getProtocolContractSampleCount(), is(0));
+ }
+
@Test
void assertCreateScorecardWithFullScore() {
LLMUsabilityScenarioResult actualScenario = new LLMUsabilityMetricCalculator().evaluateScenario(createScenario(), createArtifactBundle(List.of(
@@ -223,13 +236,19 @@ class LLMUsabilityMetricCalculatorTest {
"type", "tool_call",
"tool_name", "database_gateway_search_metadata")))),
createToolCall(2, "database_gateway_search_metadata", Map.of()))));
- LLMUsabilityScorecard actual = new LLMUsabilityMetricCalculator().createScorecard("suite", "run", List.of(actualScenario));
+ LLMUsabilityScenarioResult protocolScenario = new LLMUsabilityMetricCalculator().evaluateScenario(createProtocolScenario(), createArtifactBundle(List.of(
+ createToolCall(1, "mcp_read_resource", Map.of()))));
+ LLMUsabilityScorecard actual = new LLMUsabilityMetricCalculator().createScorecard("suite", "run", List.of(actualScenario, protocolScenario));
assertThat(actual.getOverallScore(), is(100.0D));
assertTrue(actual.isFullScore());
assertThat(actual.getNaturalTaskSuccessRate(), is(1.0D));
+ assertThat(actual.getNaturalTaskSampleCount(), is(1));
assertThat(actual.getProtocolContractSuccessRate(), is(1.0D));
+ assertThat(actual.getProtocolContractSampleCount(), is(1));
assertThat(actual.getResourceHitRate(), is(1.0D));
- assertThat(actual.getRecoveryRate(), is(1.0D));
+ assertThat(actual.getResourceHitSampleCount(), is(1));
+ assertThat(actual.getRecoveryRate(), is(0.0D));
+ assertThat(actual.getRecoverySampleCount(), is(0));
assertThat(actual.getNativeToolCallRate(), is(1.0D));
assertThat(actual.getHarnessRecoveryRate(), is(0.0D));
}
@@ -245,7 +264,9 @@ class LLMUsabilityMetricCalculatorTest {
assertThat(actual.getOverallScore(), is(100.0D));
assertFalse(actual.isFullScore());
assertThat(actual.getNaturalTaskSuccessRate(), is(1.0D));
- assertThat(actual.getProtocolContractSuccessRate(), is(1.0D));
+ assertThat(actual.getNaturalTaskSampleCount(), is(1));
+ assertThat(actual.getProtocolContractSuccessRate(), is(0.0D));
+ assertThat(actual.getProtocolContractSampleCount(), is(0));
assertThat(actual.getNativeToolCallRate(), is(0.0D));
assertThat(actual.getHarnessRecoveryRate(), is(1.0D));
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityReportWriter.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityReportWriter.java
index 16720e51552..d8f1b78fef5 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityReportWriter.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityReportWriter.java
@@ -50,15 +50,18 @@ public final class LLMUsabilityReportWriter {
appendSummaryItem(result, "overallScore", scorecard.getOverallScore());
appendSummaryItem(result, "fullScore", scorecard.isFullScore());
appendSummaryItem(result, "taskSuccessRate", scorecard.getTaskSuccessRate());
- appendSummaryItem(result, "naturalTaskSuccessRate", scorecard.getNaturalTaskSuccessRate());
- appendSummaryItem(result, "protocolContractSuccessRate", scorecard.getProtocolContractSuccessRate());
+ appendApplicableRate(result, "naturalTaskSuccessRate", scorecard.getNaturalTaskSuccessRate(), scorecard.getNaturalTaskSampleCount());
+ appendSummaryItem(result, "naturalTaskSampleCount", scorecard.getNaturalTaskSampleCount());
+ appendApplicableRate(result, "protocolContractSuccessRate", scorecard.getProtocolContractSuccessRate(), scorecard.getProtocolContractSampleCount());
+ appendSummaryItem(result, "protocolContractSampleCount", scorecard.getProtocolContractSampleCount());
appendSummaryItem(result, "firstCorrectActionRate", scorecard.getFirstCorrectActionRate());
appendSummaryItem(result, "invalidCallRate", scorecard.getInvalidCallRate());
appendSummaryItem(result, "averageRoundTrips", scorecard.getAverageRoundTrips());
- appendSummaryItem(result, "queryAnswerFidelity", scorecard.getQueryAnswerFidelity());
appendSummaryItem(result, "boundaryConfusionRate", scorecard.getBoundaryConfusionRate());
- appendSummaryItem(result, "resourceHitRate", scorecard.getResourceHitRate());
- appendSummaryItem(result, "recoveryRate", scorecard.getRecoveryRate());
+ appendApplicableRate(result, "resourceHitRate", scorecard.getResourceHitRate(), scorecard.getResourceHitSampleCount());
+ appendSummaryItem(result, "resourceHitSampleCount", scorecard.getResourceHitSampleCount());
+ appendApplicableRate(result, "recoveryRate", scorecard.getRecoveryRate(), scorecard.getRecoverySampleCount());
+ appendSummaryItem(result, "recoverySampleCount", scorecard.getRecoverySampleCount());
appendSummaryItem(result, "nextActionFollowRate", scorecard.getNextActionFollowRate());
appendSummaryItem(result, "approvalViolationRate", scorecard.getApprovalViolationRate());
appendSummaryItem(result, "nativeToolCallRate", scorecard.getNativeToolCallRate());
@@ -74,6 +77,10 @@ public final class LLMUsabilityReportWriter {
report.append("- ").append(name).append(": ").append(value).append(System.lineSeparator());
}
+ private void appendApplicableRate(final StringBuilder report, final String name, final double value, final int sampleCount) {
+ appendSummaryItem(report, name, 0 == sampleCount ? "n/a" : value);
+ }
+
private void appendScenarioResult(final StringBuilder report, final LLMUsabilityScenarioResult scenarioResult) {
report.append("- ").append(scenarioResult.getScenarioId())
.append(": ").append(scenarioResult.isSuccess() ? "PASS" : "FAIL")
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityReportWriterTest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityReportWriterTest.java
index 46d4461127f..512e02937e4 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityReportWriterTest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityReportWriterTest.java
@@ -48,7 +48,6 @@ class LLMUsabilityReportWriterTest {
.roundTripCount(3)
.resourceHit(false)
.recoveredAfterError(false)
- .queryAnswerFidelity(0.0D)
.boundaryConfusion(true)
.nextActionFollowed(false)
.approvalViolation(false)
@@ -63,14 +62,17 @@ class LLMUsabilityReportWriterTest {
.fullScore(false)
.taskSuccessRate(0.0D)
.naturalTaskSuccessRate(0.0D)
+ .naturalTaskSampleCount(1)
.protocolContractSuccessRate(1.0D)
+ .protocolContractSampleCount(0)
.firstCorrectActionRate(0.0D)
.invalidCallRate(0.33D)
.averageRoundTrips(3.0D)
- .queryAnswerFidelity(0.0D)
.boundaryConfusionRate(1.0D)
.resourceHitRate(0.0D)
+ .resourceHitSampleCount(0)
.recoveryRate(0.0D)
+ .recoverySampleCount(0)
.nextActionFollowRate(0.0D)
.approvalViolationRate(0.0D)
.nativeToolCallRate(0.0D)
@@ -90,14 +92,17 @@ class LLMUsabilityReportWriterTest {
+ "- fullScore: false" + lineSeparator
+ "- taskSuccessRate: 0.0" + lineSeparator
+ "- naturalTaskSuccessRate: 0.0" + lineSeparator
- + "- protocolContractSuccessRate: 1.0" + lineSeparator
+ + "- naturalTaskSampleCount: 1" + lineSeparator
+ + "- protocolContractSuccessRate: n/a" + lineSeparator
+ + "- protocolContractSampleCount: 0" + lineSeparator
+ "- firstCorrectActionRate: 0.0" + lineSeparator
+ "- invalidCallRate: 0.33" + lineSeparator
+ "- averageRoundTrips: 3.0" + lineSeparator
- + "- queryAnswerFidelity: 0.0" + lineSeparator
+ "- boundaryConfusionRate: 1.0" + lineSeparator
- + "- resourceHitRate: 0.0" + lineSeparator
- + "- recoveryRate: 0.0" + lineSeparator
+ + "- resourceHitRate: n/a" + lineSeparator
+ + "- resourceHitSampleCount: 0" + lineSeparator
+ + "- recoveryRate: n/a" + lineSeparator
+ + "- recoverySampleCount: 0" + lineSeparator
+ "- nextActionFollowRate: 0.0" + lineSeparator
+ "- approvalViolationRate: 0.0" + lineSeparator
+ "- nativeToolCallRate: 0.0" + lineSeparator
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityScenarioResult.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityScenarioResult.java
index a56b9a72f52..87714a2d84c 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityScenarioResult.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityScenarioResult.java
@@ -54,8 +54,6 @@ public final class LLMUsabilityScenarioResult {
private final boolean recoveredAfterError;
- private final double queryAnswerFidelity;
-
private final boolean boundaryConfusion;
private final boolean nextActionFollowed;
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityScorecard.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityScorecard.java
index 4a1128ae04d..841d8c0c68f 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityScorecard.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/assessment/LLMUsabilityScorecard.java
@@ -41,22 +41,28 @@ public final class LLMUsabilityScorecard {
private final double naturalTaskSuccessRate;
+ private final int naturalTaskSampleCount;
+
private final double protocolContractSuccessRate;
+ private final int protocolContractSampleCount;
+
private final double firstCorrectActionRate;
private final double invalidCallRate;
private final double averageRoundTrips;
- private final double queryAnswerFidelity;
-
private final double boundaryConfusionRate;
private final double resourceHitRate;
+ private final int resourceHitSampleCount;
+
private final double recoveryRate;
+ private final int recoverySampleCount;
+
private final double nextActionFollowRate;
private final double approvalViolationRate;
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/scenario/LLMUsabilityScenario.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/scenario/LLMUsabilityScenario.java
index 1d7be6d3673..c2c11e3b929 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/scenario/LLMUsabilityScenario.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/suite/usability/scenario/LLMUsabilityScenario.java
@@ -55,15 +55,6 @@ public final class LLMUsabilityScenario {
private final String expectedRecoveryCategory;
- /**
- * Is query scenario.
- *
- * @return query scenario
- */
- public boolean isQueryScenario() {
- return LLMUsabilityDimension.TOOL == dimension || LLMUsabilityDimension.RECOVERY == dimension || LLMUsabilityDimension.RESOURCE == dimension;
- }
-
/**
* Is natural task.
*
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/AbstractProductionMySQLRuntimeE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/AbstractProductionMySQLRuntimeE2ETest.java
index b2023cecb30..b98844c0279 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/AbstractProductionMySQLRuntimeE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/AbstractProductionMySQLRuntimeE2ETest.java
@@ -24,7 +24,6 @@ import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.shardingsphere.mcp.support.database.metadata.jdbc.RuntimeDatabaseConfiguration;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.apache.shardingsphere.test.e2e.mcp.support.OfficialMCPToolNames;
import org.apache.shardingsphere.test.e2e.mcp.support.runtime.MySQLRuntimeTestSupport;
import org.apache.shardingsphere.test.e2e.mcp.support.runtime.RuntimeTransport;
@@ -33,7 +32,6 @@ import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPPayloadAssert
import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPInteractionClient;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.params.provider.Arguments;
@@ -54,7 +52,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
abstract class AbstractProductionMySQLRuntimeE2ETest extends AbstractTransportParameterizedProductionRuntimeE2ETest {
protected static final String LOGICAL_DATABASE_NAME = "logic_db";
@@ -95,8 +93,10 @@ abstract class AbstractProductionMySQLRuntimeE2ETest extends AbstractTransportPa
@Override
protected void prepareRuntimeFixture() throws IOException {
- Assumptions.assumeTrue(MySQLRuntimeTestSupport.isDockerAvailable(),
- () -> MySQLRuntimeTestSupport.createDockerRequiredMessage("Docker is required for the MySQL-backed production runtime E2E test."));
+ if (!MySQLRuntimeTestSupport.isDockerAvailable()) {
+ throw new IllegalStateException(MySQLRuntimeTestSupport.createDockerRequiredMessage(
+ "Docker is required for the MySQL-backed production runtime E2E test."));
+ }
if (useSharedRuntimeFixture()) {
prepareSharedRuntimeFixture();
return;
@@ -151,10 +151,6 @@ abstract class AbstractProductionMySQLRuntimeE2ETest extends AbstractTransportPa
return Map.of("database", databaseName, "schema", databaseName, "sql", sql, "execution_mode", "execute");
}
- protected static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
protected static Stream<Arguments> transports() {
return ProductionRuntimeTransportCases.transports();
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/AbstractProductionPostgreSQLRuntimeE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/AbstractProductionPostgreSQLRuntimeE2ETest.java
index b4a367276ec..dca50e0c5c1 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/AbstractProductionPostgreSQLRuntimeE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/AbstractProductionPostgreSQLRuntimeE2ETest.java
@@ -18,10 +18,8 @@
package org.apache.shardingsphere.test.e2e.mcp.runtime.production;
import org.apache.shardingsphere.mcp.support.database.metadata.jdbc.RuntimeDatabaseConfiguration;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.apache.shardingsphere.test.e2e.mcp.support.runtime.PostgreSQLRuntimeTestSupport;
import org.junit.jupiter.api.AfterAll;
-import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.provider.Arguments;
import org.testcontainers.containers.GenericContainer;
@@ -48,7 +46,9 @@ abstract class AbstractProductionPostgreSQLRuntimeE2ETest extends AbstractTransp
@Override
protected void prepareRuntimeFixture() throws IOException {
- Assumptions.assumeTrue(PostgreSQLRuntimeTestSupport.isDockerAvailable(), "Docker is required for the PostgreSQL-backed production runtime E2E test.");
+ if (!PostgreSQLRuntimeTestSupport.isDockerAvailable()) {
+ throw new IllegalStateException("Docker is required for the PostgreSQL-backed production runtime E2E test.");
+ }
if (null != container) {
return;
}
@@ -77,10 +77,6 @@ abstract class AbstractProductionPostgreSQLRuntimeE2ETest extends AbstractTransp
return Map.of("database", LOGICAL_DATABASE_NAME, "schema", schema, "sql", sql, "execution_mode", "execute");
}
- protected static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
protected static Stream<Arguments> dualTransports() {
return ProductionRuntimeTransportCases.transports();
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/AbstractProductionProxyWorkflowE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/AbstractProductionProxyWorkflowE2ETest.java
index 58690071fed..55d4dbfa81a 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/AbstractProductionProxyWorkflowE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/AbstractProductionProxyWorkflowE2ETest.java
@@ -28,7 +28,6 @@ import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionPa
import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPInteractionClient;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.TestInstance;
import java.io.IOException;
@@ -79,8 +78,9 @@ abstract class AbstractProductionProxyWorkflowE2ETest extends AbstractProduction
@Override
protected final void prepareRuntimeFixture() throws IOException {
- Assumptions.assumeTrue(MySQLRuntimeTestSupport.isDockerAvailable(),
- () -> MySQLRuntimeTestSupport.createDockerRequiredMessage("Docker is required for the Proxy-backed workflow E2E tests."));
+ if (!MySQLRuntimeTestSupport.isDockerAvailable()) {
+ throw new IllegalStateException(MySQLRuntimeTestSupport.createDockerRequiredMessage("Docker is required for the Proxy-backed workflow E2E tests."));
+ }
try {
if (sharedRuntimeFixtureSelected) {
prepareSharedRuntimeFixture();
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/HttpProductionProxyEncryptWorkflowE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/HttpProductionProxyEncryptWorkflowE2ETest.java
index ab3ca0df654..bdbd47a7271 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/HttpProductionProxyEncryptWorkflowE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/HttpProductionProxyEncryptWorkflowE2ETest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.test.e2e.mcp.runtime.production;
import org.apache.shardingsphere.mcp.support.workflow.descriptor.WorkflowToolDescriptors;
import org.apache.shardingsphere.mcp.support.workflow.model.WorkflowIssueCode;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPInteractionClient;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
@@ -36,7 +35,7 @@ import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
class HttpProductionProxyEncryptWorkflowE2ETest extends AbstractProductionProxyWorkflowE2ETest {
private static final String PLAN_TOOL_NAME = "database_gateway_plan_encrypt_rule";
@@ -57,10 +56,6 @@ class HttpProductionProxyEncryptWorkflowE2ETest extends AbstractProductionProxyW
private static final String TEMPLATE_SECRET_VALUE = "mcp-template-secret-6f2d4a8b";
- private static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
@Test
void assertCompleteEncryptAlgorithmThroughProxy() throws IOException, InterruptedException {
useSharedReadOnlyRuntimeFixture();
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/HttpProductionProxyFeatureWorkflowContractE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/HttpProductionProxyFeatureWorkflowContractE2ETest.java
index e7898c7a1a9..6160a41b531 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/HttpProductionProxyFeatureWorkflowContractE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/HttpProductionProxyFeatureWorkflowContractE2ETest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.test.e2e.mcp.runtime.production;
import org.apache.shardingsphere.mcp.support.workflow.descriptor.WorkflowToolDescriptors;
import org.apache.shardingsphere.mcp.support.workflow.model.WorkflowIssueCode;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPInteractionClient;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
@@ -42,7 +41,7 @@ import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
class HttpProductionProxyFeatureWorkflowContractE2ETest extends AbstractProductionProxyWorkflowE2ETest {
private static final String APPLY_TOOL_NAME = WorkflowToolDescriptors.APPLY_TOOL_NAME;
@@ -87,10 +86,6 @@ class HttpProductionProxyFeatureWorkflowContractE2ETest extends AbstractProducti
"create table", "alter table", "drop table", "create index", "drop index", "migrate", "migration", "backfill", "data probe", "physical metadata",
"register storage unit", "alter storage unit", "unregister storage unit");
- private static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
@ParameterizedTest(name = "{0}")
@MethodSource("featureWorkflowScenarios")
void assertPlanManualApplyAndValidateFeatureWorkflowThroughProxy(final String scenarioName, final FeatureWorkflowScenario scenario) throws IOException, InterruptedException {
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/HttpProductionProxyMaskWorkflowE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/HttpProductionProxyMaskWorkflowE2ETest.java
index e67a14ec8e5..8a26254cae5 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/HttpProductionProxyMaskWorkflowE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/HttpProductionProxyMaskWorkflowE2ETest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.test.e2e.mcp.runtime.production;
import org.apache.shardingsphere.mcp.support.workflow.descriptor.WorkflowToolDescriptors;
import org.apache.shardingsphere.mcp.support.workflow.model.WorkflowIssueCode;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPInteractionClient;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
@@ -35,7 +34,7 @@ import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
class HttpProductionProxyMaskWorkflowE2ETest extends AbstractProductionProxyWorkflowE2ETest {
private static final String PLAN_TOOL_NAME = "database_gateway_plan_mask_rule";
@@ -50,10 +49,6 @@ class HttpProductionProxyMaskWorkflowE2ETest extends AbstractProductionProxyWork
private static final String TABLE_RULES_RESOURCE_URI = "shardingsphere://features/mask/databases/%s/tables/%s/rules";
- private static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
@Test
void assertCompleteMaskAlgorithmThroughProxy() throws IOException, InterruptedException {
useSharedReadOnlyRuntimeFixture();
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/HttpProductionProxySecretReferenceWorkflowE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/HttpProductionProxySecretReferenceWorkflowE2ETest.java
index d8a6c92b3d4..db27892e9b3 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/HttpProductionProxySecretReferenceWorkflowE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/HttpProductionProxySecretReferenceWorkflowE2ETest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.test.e2e.mcp.runtime.production;
import org.apache.shardingsphere.mcp.support.diagnostic.MCPDiagnosticCategory;
import org.apache.shardingsphere.mcp.support.workflow.descriptor.WorkflowToolDescriptors;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.apache.shardingsphere.test.e2e.mcp.support.fixture.MCPWorkflowSecretReferenceFixture;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPInteractionClient;
import org.junit.jupiter.api.Test;
@@ -35,7 +34,7 @@ import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
class HttpProductionProxySecretReferenceWorkflowE2ETest extends AbstractProductionProxyWorkflowE2ETest {
private static final String PLAN_TOOL_NAME = "database_gateway_plan_encrypt_rule";
@@ -44,10 +43,6 @@ class HttpProductionProxySecretReferenceWorkflowE2ETest extends AbstractProducti
private static final String RULES_RESOURCE_URI = "shardingsphere://features/encrypt/databases/%s/rules";
- private static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
@Test
void assertSecretReferenceApplyRequiresManualExecutionThroughProxy() throws IOException, InterruptedException {
try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/PackagedDistributionE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/PackagedDistributionE2ETest.java
index dd53ca69d43..665ab624f81 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/PackagedDistributionE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/PackagedDistributionE2ETest.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.test.e2e.mcp.runtime.production;
import org.apache.shardingsphere.test.e2e.env.runtime.EnvironmentPropertiesLoader;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.apache.shardingsphere.test.e2e.mcp.support.OfficialMCPToolNames;
import org.apache.shardingsphere.test.e2e.mcp.support.distribution.DockerImageHttpRuntime;
import org.apache.shardingsphere.test.e2e.mcp.support.distribution.PackagedDistributionHttpRuntime;
@@ -26,13 +25,13 @@ import org.apache.shardingsphere.test.e2e.mcp.support.distribution.PackagedDistr
import org.apache.shardingsphere.test.e2e.mcp.support.distribution.PackagedDistributionTestSupport;
import org.apache.shardingsphere.test.e2e.mcp.support.distribution.PackagedDistributionTestSupport.PreparedPackagedDistribution;
import org.apache.shardingsphere.test.e2e.mcp.support.runtime.MySQLRuntimeTestSupport;
+import org.apache.shardingsphere.test.e2e.mcp.support.runtime.PostgreSQLRuntimeTestSupport;
import org.apache.shardingsphere.test.e2e.mcp.support.runtime.RuntimeTransport;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionPayloads;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.DockerImageStdioInteractionClient;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPInteractionClient;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.PackagedDistributionStdioInteractionClient;
import org.junit.jupiter.api.AfterAll;
-import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.condition.EnabledIf;
@@ -59,7 +58,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@EnabledOnOs({OS.LINUX, OS.MAC, OS.WINDOWS})
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class PackagedDistributionE2ETest {
@@ -85,16 +84,18 @@ class PackagedDistributionE2ETest {
private GenericContainer<?> mysqlContainer;
+ private GenericContainer<?> postgresqlContainer;
+
@AfterAll
void tearDownContainer() {
if (null != mysqlContainer) {
mysqlContainer.stop();
mysqlContainer = null;
}
- }
-
- private static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
+ if (null != postgresqlContainer) {
+ postgresqlContainer.stop();
+ postgresqlContainer = null;
+ }
}
@Test
@@ -162,6 +163,36 @@ class PackagedDistributionE2ETest {
}
}
+ @Test
+ void assertLaunchContainerWithDefaultHttpConfiguration() throws IOException, InterruptedException {
+ try (
+ DockerImageHttpRuntime runtime = new DockerImageHttpRuntime(getConfiguredContainerImage(), null);
+ MCPInteractionClient interactionClient = runtime.openInteractionClient()) {
+ assertDefaultContainerRuntime(RuntimeTransport.HTTP, interactionClient);
+ }
+ }
+
+ @Test
+ void assertLaunchContainerWithDefaultStdioConfiguration() throws IOException, InterruptedException {
+ try (MCPInteractionClient interactionClient = new DockerImageStdioInteractionClient(getConfiguredContainerImage(), null)) {
+ interactionClient.open();
+ assertDefaultContainerRuntime(RuntimeTransport.STDIO, interactionClient);
+ }
+ }
+
+ @Test
+ void assertLaunchPackagedDistributionWithPostgreSQL() throws IOException, InterruptedException, SQLException {
+ preparePostgreSQLContainer();
+ PreparedPackagedDistribution distribution = PackagedDistributionTestSupport.prepare(tempDir.resolve("postgresql-http"), RuntimeTransport.HTTP,
+ PostgreSQLRuntimeTestSupport.createRuntimeDatabases(postgresqlContainer, LOGICAL_DATABASE_NAME));
+ try (
+ PackagedDistributionHttpRuntime runtime = new PackagedDistributionHttpRuntime(distribution);
+ MCPInteractionClient interactionClient = runtime.openInteractionClient()) {
+ assertOfficialRuntime(distribution.home(), RuntimeTransport.HTTP, interactionClient);
+ assertPostgreSQLMetadata(interactionClient);
+ }
+ }
+
private PreparedPackagedDistribution preparePackagedDistribution(final String caseName, final RuntimeTransport transport) throws IOException, SQLException {
prepareMySQLContainer();
return PackagedDistributionTestSupport.prepare(tempDir.resolve(caseName), transport,
@@ -181,8 +212,9 @@ class PackagedDistributionE2ETest {
}
private void prepareMySQLContainer() throws SQLException {
- Assumptions.assumeTrue(MySQLRuntimeTestSupport.isDockerAvailable(),
- () -> MySQLRuntimeTestSupport.createDockerRequiredMessage("Docker is required for the MySQL-backed MCP distribution E2E test."));
+ if (!MySQLRuntimeTestSupport.isDockerAvailable()) {
+ throw new IllegalStateException(MySQLRuntimeTestSupport.createDockerRequiredMessage("Docker is required for the MySQL-backed MCP distribution E2E test."));
+ }
if (null != mysqlContainer) {
return;
}
@@ -191,9 +223,23 @@ class PackagedDistributionE2ETest {
MySQLRuntimeTestSupport.initializeDatabase(mysqlContainer);
}
+ private void preparePostgreSQLContainer() throws SQLException {
+ if (!PostgreSQLRuntimeTestSupport.isDockerAvailable()) {
+ throw new IllegalStateException("Docker is required for the PostgreSQL-backed MCP distribution E2E test.");
+ }
+ if (null != postgresqlContainer) {
+ return;
+ }
+ postgresqlContainer = PostgreSQLRuntimeTestSupport.createContainer();
+ postgresqlContainer.start();
+ PostgreSQLRuntimeTestSupport.initializeDatabase(postgresqlContainer);
+ }
+
private String getConfiguredContainerImage() {
String result = EnvironmentPropertiesLoader.loadProperties().getProperty(IMAGE_PROPERTY, "").trim();
- Assumptions.assumeFalse(result.isBlank(), "Set " + IMAGE_PROPERTY + " in env/e2e-env.properties or pass -D" + IMAGE_PROPERTY + " to run MCP container distribution E2E.");
+ if (result.isBlank()) {
+ throw new IllegalStateException("Set " + IMAGE_PROPERTY + " in env/e2e-env.properties or pass -D" + IMAGE_PROPERTY + " to run MCP container distribution E2E.");
+ }
return result;
}
@@ -215,6 +261,11 @@ class PackagedDistributionE2ETest {
assertExecuteQuery(interactionClient);
}
+ private void assertDefaultContainerRuntime(final RuntimeTransport transport, final MCPInteractionClient interactionClient) throws IOException, InterruptedException {
+ assertRuntimeDiagnostics(interactionClient.readResource("shardingsphere://runtime"), transport);
+ assertOfficialToolNames(interactionClient.listTools().stream().map(each -> String.valueOf(each.get("name"))).toList());
+ }
+
private void assertBootstrapDirectoriesCreated(final Path distributionHome) {
assertTrue(Files.isDirectory(distributionHome.resolve("data")));
assertTrue(Files.isDirectory(distributionHome.resolve("logs")));
@@ -272,6 +323,12 @@ class PackagedDistributionE2ETest {
assertThat(actualSearchItems, hasItems("orders", "order_items", "active_orders"));
}
+ private void assertPostgreSQLMetadata(final MCPInteractionClient interactionClient) throws IOException, InterruptedException {
+ List<String> actualSearchItems = getItemNames(interactionClient.call("database_gateway_search_metadata",
+ Map.of("database", LOGICAL_DATABASE_NAME, "schema", "public", "query", "order", "object_types", List.of("table", "view"))));
+ assertThat(actualSearchItems, hasItems("orders", "active_orders"));
+ }
+
private void assertExecuteQuery(final MCPInteractionClient interactionClient) throws IOException, InterruptedException {
Map<String, Object> actualResult = interactionClient.call("database_gateway_execute_query",
Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql", "SELECT status FROM orders ORDER BY order_id", "max_rows", 10));
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionMySQLReadOnlySQLRuntimeE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionMySQLReadOnlySQLRuntimeE2ETest.java
index 48905eedc66..e5608bb12e9 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionMySQLReadOnlySQLRuntimeE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionMySQLReadOnlySQLRuntimeE2ETest.java
@@ -38,7 +38,7 @@ import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
class ProductionMySQLReadOnlySQLRuntimeE2ETest extends AbstractProductionMySQLRuntimeE2ETest {
private static final long SLOW_ELICITATION_MILLIS = 11_000L;
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionMySQLRuntimeE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionMySQLRuntimeE2ETest.java
index 78fd1029429..2cafe425b7c 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionMySQLRuntimeE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionMySQLRuntimeE2ETest.java
@@ -38,7 +38,7 @@ import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
class ProductionMySQLRuntimeE2ETest extends AbstractProductionMySQLRuntimeE2ETest {
@Override
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionMySQLSQLRuntimeE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionMySQLSQLRuntimeE2ETest.java
index ff5e6123aa9..403a5c898c0 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionMySQLSQLRuntimeE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionMySQLSQLRuntimeE2ETest.java
@@ -36,7 +36,7 @@ import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
class ProductionMySQLSQLRuntimeE2ETest extends AbstractProductionMySQLRuntimeE2ETest {
@ParameterizedTest(name = "{0}")
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionPostgreSQLRuntimeE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionPostgreSQLRuntimeE2ETest.java
index ec5bc989b43..b9fcde1eb7b 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionPostgreSQLRuntimeE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionPostgreSQLRuntimeE2ETest.java
@@ -35,7 +35,7 @@ import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
class ProductionPostgreSQLRuntimeE2ETest extends AbstractProductionPostgreSQLRuntimeE2ETest {
@ParameterizedTest(name = "{0}")
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionRuntimeTransportSmokeE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionRuntimeTransportSmokeE2ETest.java
index 28b4896120d..37644cf7b5e 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionRuntimeTransportSmokeE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionRuntimeTransportSmokeE2ETest.java
@@ -30,7 +30,7 @@ import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
class ProductionRuntimeTransportSmokeE2ETest extends AbstractProductionMySQLRuntimeE2ETest {
@Override
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/AbstractHttpProgrammaticRuntimeE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/AbstractHttpProgrammaticRuntimeE2ETest.java
index 1601916a338..5ac15783bfd 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/AbstractHttpProgrammaticRuntimeE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/AbstractHttpProgrammaticRuntimeE2ETest.java
@@ -27,7 +27,6 @@ import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionPa
import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionProtocolSupport;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPHttpTransportTestSupport;
import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.Assumptions;
import org.testcontainers.containers.GenericContainer;
import java.io.IOException;
@@ -35,6 +34,7 @@ import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpResponse;
import java.sql.SQLException;
+import java.util.LinkedHashMap;
import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -105,6 +105,18 @@ abstract class AbstractHttpProgrammaticRuntimeE2ETest extends AbstractConfigBack
return MCPHttpTransportTestSupport.sendJsonRpcRequest(httpClient, getEndpointUri(), createSessionHeaders(sessionId), "resource-1", "resources/read", Map.of("uri", resourceUri));
}
+ protected final HttpResponse<String> sendCompletionRequest(final HttpClient httpClient, final String sessionId, final Map<String, Object> reference,
+ final String argumentName, final String argumentValue,
+ final Map<String, String> contextArguments) throws IOException, InterruptedException {
+ Map<String, Object> params = new LinkedHashMap<>(3, 1F);
+ params.put("ref", reference);
+ params.put("argument", Map.of("name", argumentName, "value", argumentValue));
+ if (!contextArguments.isEmpty()) {
+ params.put("context", Map.of("arguments", contextArguments));
+ }
+ return MCPHttpTransportTestSupport.sendJsonRpcRequest(httpClient, getEndpointUri(), createSessionHeaders(sessionId), "completion-1", "completion/complete", params);
+ }
+
protected final HttpResponse<String> sendDeleteRequest(final HttpClient httpClient, final Map<String, String> headers) throws IOException, InterruptedException {
return MCPHttpTransportTestSupport.sendDeleteRequest(httpClient, getEndpointUri(), headers);
}
@@ -174,8 +186,10 @@ abstract class AbstractHttpProgrammaticRuntimeE2ETest extends AbstractConfigBack
@Override
protected final void prepareRuntimeFixture() throws IOException {
- Assumptions.assumeTrue(MySQLRuntimeTestSupport.isDockerAvailable(),
- () -> MySQLRuntimeTestSupport.createDockerRequiredMessage("Docker is required for the MySQL-backed MCP programmatic contract E2E test."));
+ if (!MySQLRuntimeTestSupport.isDockerAvailable()) {
+ throw new IllegalStateException(MySQLRuntimeTestSupport.createDockerRequiredMessage(
+ "Docker is required for the MySQL-backed MCP programmatic contract E2E test."));
+ }
if (useSharedDatabaseBackedRuntime()) {
prepareSharedDatabaseBackedRuntime();
return;
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/ExecuteQueryReadOnlyE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/ExecuteQueryReadOnlyE2ETest.java
index 73868d8d1bc..4b7d125ce50 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/ExecuteQueryReadOnlyE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/ExecuteQueryReadOnlyE2ETest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.test.e2e.mcp.runtime.programmatic;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
@@ -29,13 +28,9 @@ import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
class ExecuteQueryReadOnlyE2ETest extends AbstractSharedHttpProgrammaticRuntimeE2ETest {
- private static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
@Test
void assertExecuteSelectOverHttpSession() throws IOException, InterruptedException {
launchHttpTransport();
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/ExecuteQueryTransactionE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/ExecuteQueryTransactionE2ETest.java
index c350d24b444..84dd8b1e073 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/ExecuteQueryTransactionE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/ExecuteQueryTransactionE2ETest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.test.e2e.mcp.runtime.programmatic;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
@@ -30,13 +29,9 @@ import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
class ExecuteQueryTransactionE2ETest extends AbstractHttpProgrammaticRuntimeE2ETest {
- private static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
@Test
void assertRejectCrossDatabaseTransactionSwitch() throws IOException, InterruptedException {
launchHttpTransport();
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/FeatureWorkflowContractE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/FeatureWorkflowContractE2ETest.java
index 28a59126db0..741f67cefc1 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/FeatureWorkflowContractE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/FeatureWorkflowContractE2ETest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.test.e2e.mcp.runtime.programmatic;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.apache.shardingsphere.test.e2e.mcp.support.assertion.MCPModelContractAssertions;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionPayloads;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionProtocolSupport;
@@ -40,7 +39,7 @@ import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
class FeatureWorkflowContractE2ETest extends AbstractSharedHttpProgrammaticRuntimeE2ETest {
private static final List<String> FORBIDDEN_ARTIFACT_TOKENS = List.of(
@@ -50,10 +49,6 @@ class FeatureWorkflowContractE2ETest extends AbstractSharedHttpProgrammaticRunti
private static final List<String> REQUIRED_WORKFLOW_OUTPUT_FIELDS = List.of(
"response_mode", "plan_id", "workflow_kind", "status", "missing_required_inputs", "resources_to_read", "next_actions");
- private static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
@ParameterizedTest(name = "{0}")
@MethodSource("featureWorkflowScenarios")
void assertPlanFeatureWorkflowContract(final String scenarioName, final FeatureWorkflowScenario scenario) throws IOException, InterruptedException {
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportApprovalSafetyE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportApprovalSafetyE2ETest.java
index 428bceb36bf..2a2fbe22d66 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportApprovalSafetyE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportApprovalSafetyE2ETest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.test.e2e.mcp.runtime.programmatic;
import org.apache.shardingsphere.mcp.support.workflow.descriptor.WorkflowToolDescriptors;
import org.apache.shardingsphere.mcp.support.workflow.model.WorkflowIssueCode;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.apache.shardingsphere.test.e2e.mcp.support.assertion.MCPModelContractAssertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
@@ -34,13 +33,9 @@ import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
class HttpTransportApprovalSafetyE2ETest extends AbstractSharedHttpProgrammaticRuntimeE2ETest {
- private static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
@Test
void assertExecuteUpdateWithoutApprovalArgument() throws IOException, InterruptedException {
launchHttpTransport();
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportBaselineContractE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportBaselineContractE2ETest.java
index 085ec656c8b..f9d436b6b43 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportBaselineContractE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportBaselineContractE2ETest.java
@@ -21,7 +21,6 @@ import org.apache.shardingsphere.mcp.api.capability.resource.MCPResourceURIVaria
import org.apache.shardingsphere.mcp.api.MCPRequestContext;
import org.apache.shardingsphere.mcp.core.resource.handler.capability.ServerCapabilitiesHandler;
import org.apache.shardingsphere.mcp.core.resource.handler.capability.ServerGuidanceHandler;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.apache.shardingsphere.test.e2e.mcp.support.assertion.MCPBaselineContractAssertions;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionPayloads;
import org.junit.jupiter.api.Test;
@@ -43,12 +42,8 @@ class HttpTransportBaselineContractE2ETest extends AbstractSharedHttpProgrammati
private static final String BASELINE_RESOURCE_PATH = "baseline-contract/model-contract/";
- private static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
@Test
- @EnabledIf("isEnabled")
+ @EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
void assertCapabilitiesBaselineContract() throws IOException, InterruptedException {
launchHttpTransport();
HttpClient httpClient = HttpClient.newHttpClient();
@@ -59,7 +54,7 @@ class HttpTransportBaselineContractE2ETest extends AbstractSharedHttpProgrammati
}
@Test
- @EnabledIf("isEnabled")
+ @EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
void assertGuidanceBaselineContract() throws IOException, InterruptedException {
launchHttpTransport();
HttpClient httpClient = HttpClient.newHttpClient();
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportCompletionE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportCompletionE2ETest.java
index 1baed60bc73..398e6a8de90 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportCompletionE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportCompletionE2ETest.java
@@ -17,16 +17,13 @@
package org.apache.shardingsphere.test.e2e.mcp.runtime.programmatic;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionPayloads;
-import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionProtocolSupport;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import java.io.IOException;
import java.net.http.HttpClient;
import java.net.http.HttpResponse;
-import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -35,15 +32,11 @@ import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
class HttpTransportCompletionE2ETest extends AbstractSharedHttpProgrammaticRuntimeE2ETest {
private static final String PLAN_MASK_PROMPT_NAME = "plan_mask_rule";
- private static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
@Test
void assertCompleteMetadataValues() throws IOException, InterruptedException {
launchHttpTransport();
@@ -141,17 +134,4 @@ class HttpTransportCompletionE2ETest extends AbstractSharedHttpProgrammaticRunti
return MCPInteractionPayloads.getRequiredJsonRpcResult(parseJsonBody(actual.body()));
}
- private HttpResponse<String> sendCompletionRequest(final HttpClient httpClient, final String sessionId, final Map<String, Object> reference,
- final String argumentName, final String argumentValue,
- final Map<String, String> contextArguments) throws IOException, InterruptedException {
- Map<String, Object> params = new LinkedHashMap<>(3, 1F);
- params.put("ref", reference);
- params.put("argument", Map.of("name", argumentName, "value", argumentValue));
- if (!contextArguments.isEmpty()) {
- params.put("context", Map.of("arguments", contextArguments));
- }
- return sendRawPostRequest(httpClient, createSessionHeaders(sessionId), MCPInteractionProtocolSupport.createJsonRpcRequestBody(
- "completion-1", "completion/complete", params));
- }
-
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportContractE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportContractE2ETest.java
index 185423d5f55..6277f2934c2 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportContractE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportContractE2ETest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.test.e2e.mcp.runtime.programmatic;
import org.apache.shardingsphere.mcp.support.descriptor.MCPShardingSphereMetadataKeys;
import org.apache.shardingsphere.mcp.support.workflow.descriptor.WorkflowToolDescriptors;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.apache.shardingsphere.test.e2e.mcp.support.assertion.MCPModelContractAssertions;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionPayloads;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionProtocolSupport;
@@ -31,7 +30,6 @@ import java.io.IOException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
-import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -40,15 +38,11 @@ import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
class HttpTransportContractE2ETest extends AbstractSharedHttpProgrammaticRuntimeE2ETest {
private static final String PLAN_MASK_TOOL_NAME = "database_gateway_plan_mask_rule";
- private static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
@Test
void assertAcceptFollowUpRequestWithLowercaseSessionHeaders() throws IOException, InterruptedException {
launchHttpTransport();
@@ -230,18 +224,6 @@ class HttpTransportContractE2ETest extends AbstractSharedHttpProgrammaticRuntime
"prompt-1", "prompts/get", Map.of("name", promptName, "arguments", arguments)));
}
- private HttpResponse<String> sendCompletionRequest(final HttpClient httpClient, final String sessionId, final Map<String, Object> reference,
- final String argumentName, final String argumentValue, final Map<String, String> contextArguments) throws IOException, InterruptedException {
- Map<String, Object> params = new LinkedHashMap<>(3, 1F);
- params.put("ref", reference);
- params.put("argument", Map.of("name", argumentName, "value", argumentValue));
- if (!contextArguments.isEmpty()) {
- params.put("context", Map.of("arguments", contextArguments));
- }
- return sendRawPostRequest(httpClient, createSessionHeaders(sessionId), MCPInteractionProtocolSupport.createJsonRpcRequestBody(
- "completion-1", "completion/complete", params));
- }
-
private Map<String, Object> createMaskRulePlanArguments() {
return Map.of(
"database", "logic_db",
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportProtocolContractE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportProtocolContractE2ETest.java
index ecbdd5c4a4a..42b769cebec 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportProtocolContractE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportProtocolContractE2ETest.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.test.e2e.mcp.runtime.programmatic;
-import org.apache.shardingsphere.mcp.support.security.MCPClientSafetyPolicy;
+import org.apache.shardingsphere.mcp.support.security.MCPRuntimeProtectionPolicy;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionPayloads;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionProtocolSupport;
import org.junit.jupiter.api.Test;
@@ -199,7 +199,7 @@ class HttpTransportProtocolContractE2ETest extends AbstractHttpProtocolOnlyE2ETe
@Test
void assertEnforceToolCallLimitPerSession() throws IOException, InterruptedException {
- String propertyName = MCPClientSafetyPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY;
+ String propertyName = MCPRuntimeProtectionPolicy.MAX_TOOL_CALLS_PER_SESSION_PROPERTY;
String originalValue = System.getProperty(propertyName);
System.setProperty(propertyName, "1");
try {
@@ -265,8 +265,7 @@ class HttpTransportProtocolContractE2ETest extends AbstractHttpProtocolOnlyE2ETe
launchHttpTransport();
HttpClient httpClient = HttpClient.newHttpClient();
initializeSession(httpClient);
- HttpResponse<String> actual = sendCapabilitiesRequest(httpClient,
- Map.of("MCP-Session-Id", "missing-session", "MCP-Protocol-Version", getProtocolVersion()));
+ HttpResponse<String> actual = sendCapabilitiesRequest(httpClient, Map.of("MCP-Session-Id", "missing-session"));
assertThat(actual.statusCode(), is(404));
assertThat(String.valueOf(parseJsonBody(actual.body()).get("message")), is("Session not found: missing-session"));
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportRecoveryE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportRecoveryE2ETest.java
index e0ac1174a2c..f9e79827ec3 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportRecoveryE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportRecoveryE2ETest.java
@@ -18,18 +18,15 @@
package org.apache.shardingsphere.test.e2e.mcp.runtime.programmatic;
import org.apache.shardingsphere.mcp.support.workflow.descriptor.WorkflowToolDescriptors;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.apache.shardingsphere.test.e2e.mcp.support.assertion.MCPModelContractAssertions;
import org.apache.shardingsphere.test.e2e.mcp.support.fixture.MCPWorkflowSecretReferenceFixture;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionPayloads;
-import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionProtocolSupport;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import java.io.IOException;
import java.net.http.HttpClient;
import java.net.http.HttpResponse;
-import java.util.LinkedHashMap;
import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -37,15 +34,11 @@ import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
class HttpTransportRecoveryE2ETest extends AbstractSharedHttpProgrammaticRuntimeE2ETest {
private static final String RECOVERY_SECRET = "recovery-secret-value";
- private static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
@Test
void assertRecoverMissingDatabase() throws IOException, InterruptedException {
launchHttpTransport();
@@ -103,7 +96,7 @@ class HttpTransportRecoveryE2ETest extends AbstractSharedHttpProgrammaticRuntime
Map<String, Object> argument = MCPInteractionPayloads.getRequiredObject(nextAction, "argument");
assertThat(argument.get("name"), is("plan_id"));
Map<String, Object> reference = MCPInteractionPayloads.getRequiredObject(nextAction, "ref");
- HttpResponse<String> completion = sendCompletionRequest(httpClient, sessionId, reference, String.valueOf(argument.get("name")));
+ HttpResponse<String> completion = sendCompletionRequest(httpClient, sessionId, reference, String.valueOf(argument.get("name")), "", Map.of());
assertThat(completion.statusCode(), is(200));
Map<String, Object> completionPayload = getResultPayload(completion);
assertThat(MCPInteractionPayloads.getRequiredObject(completionPayload, "completion").get("total"), is(0));
@@ -172,15 +165,6 @@ class HttpTransportRecoveryE2ETest extends AbstractSharedHttpProgrammaticRuntime
assertTrue(MCPInteractionPayloads.getRequiredObjectList(payload, "items").stream().anyMatch(each -> expectedDatabase.equals(each.get("database"))));
}
- private HttpResponse<String> sendCompletionRequest(final HttpClient httpClient, final String sessionId, final Map<String, Object> reference,
- final String argumentName) throws IOException, InterruptedException {
- Map<String, Object> params = new LinkedHashMap<>(2, 1F);
- params.put("ref", reference);
- params.put("argument", Map.of("name", argumentName, "value", ""));
- return sendRawPostRequest(httpClient, createSessionHeaders(sessionId), MCPInteractionProtocolSupport.createJsonRpcRequestBody(
- "completion-1", "completion/complete", params));
- }
-
private Map<String, Object> createEncryptRulePlanArguments() {
return Map.of(
"database", "logic_db",
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportSessionLifecycleE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportSessionLifecycleE2ETest.java
index 86671d5cf54..513e18d8411 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportSessionLifecycleE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportSessionLifecycleE2ETest.java
@@ -66,7 +66,7 @@ class HttpTransportSessionLifecycleE2ETest extends AbstractHttpProtocolOnlyE2ETe
HttpClient httpClient = HttpClient.newHttpClient();
String sessionId = initializeSession(httpClient);
HttpResponse<String> firstDelete = sendDeleteRequest(httpClient, createSessionHeaders(sessionId));
- HttpResponse<String> secondDelete = sendDeleteRequest(httpClient, createSessionHeaders(sessionId));
+ HttpResponse<String> secondDelete = sendDeleteRequest(httpClient, Map.of("MCP-Session-Id", sessionId));
assertThat(firstDelete.statusCode(), is(200));
assertThat(secondDelete.statusCode(), is(404));
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/MetadataDiscoveryE2ETest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/MetadataDiscoveryE2ETest.java
index cb522e0b1c4..8f9792e85f3 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/MetadataDiscoveryE2ETest.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/MetadataDiscoveryE2ETest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.test.e2e.mcp.runtime.programmatic;
-import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionPayloads;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
@@ -31,13 +30,9 @@ import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
-@EnabledIf("isEnabled")
+@EnabledIf("org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition#isDockerEnabled")
class MetadataDiscoveryE2ETest extends AbstractSharedHttpProgrammaticRuntimeE2ETest {
- private static boolean isEnabled() {
- return MCPE2ECondition.isDockerEnabled();
- }
-
@Test
void assertSearchMetadataTablesAndViews() throws IOException, InterruptedException {
launchHttpTransport();
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/distribution/DockerImageHttpRuntime.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/distribution/DockerImageHttpRuntime.java
index c816a9d8090..26982f99b4a 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/distribution/DockerImageHttpRuntime.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/distribution/DockerImageHttpRuntime.java
@@ -31,6 +31,7 @@ import java.net.URI;
import java.net.http.HttpClient;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
+import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
@@ -105,9 +106,14 @@ public final class DockerImageHttpRuntime implements AutoCloseable {
}
private List<String> createDockerCommand(final int httpPort) {
- return List.of("docker", "run", "--rm", "--add-host=host.docker.internal:host-gateway", "-p",
- "127.0.0.1:" + httpPort + ":18088", "-v", configFile.toAbsolutePath().normalize() + ":" + CONTAINER_CONFIG_FILE + ":ro", "-e", "SHARDINGSPHERE_MCP_TRANSPORT=http", "-e",
- "SHARDINGSPHERE_MCP_CONFIG=" + CONTAINER_CONFIG_FILE, imageName);
+ List<String> result = new LinkedList<>(List.of("docker", "run", "--rm", "--add-host=host.docker.internal:host-gateway", "-p",
+ "127.0.0.1:" + httpPort + ":18088", "-e", "SHARDINGSPHERE_MCP_TRANSPORT=http"));
+ if (null != configFile) {
+ result.addAll(List.of("-v", configFile.toAbsolutePath().normalize() + ":" + CONTAINER_CONFIG_FILE + ":ro", "-e",
+ "SHARDINGSPHERE_MCP_CONFIG=" + CONTAINER_CONFIG_FILE));
+ }
+ result.add(imageName);
+ return result;
}
private int allocatePort() throws IOException {