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/05/25 07:24:37 UTC
(shardingsphere) branch master updated: Refactor mcp module (#38714)
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 2a41f872534 Refactor mcp module (#38714)
2a41f872534 is described below
commit 2a41f872534d888c0fff5ca8c953b3b208cb4f30
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon May 25 15:24:31 2026 +0800
Refactor mcp module (#38714)
* Refactor mcp module
* Refactor mcp module
* Refactor mcp module
* Refactor mcp module
---
docs/mcp/ShardingSphere-MCP-Detailed-Design.md | 21 +-
docs/mcp/ShardingSphere-MCP-PRD.md | 39 +-
docs/mcp/ShardingSphere-MCP-Technical-Design.md | 27 +-
mcp/README.md | 15 +-
mcp/README_ZH.md | 12 +-
.../bootstrap/MCPDocumentationContractTest.java | 2 +-
.../AbstractMCPToolSpecificationFactoryTest.java | 308 ++++++
...MCPToolSpecificationElicitationFactoryTest.java | 397 +++++++
.../tool/MCPToolSpecificationFactoryTest.java | 605 +----------
.../mcp/core/completion/MCPCompletionService.java | 1 -
.../descriptor/CoreToolDescriptorValidator.java | 2 +-
.../tool/handler/execute/ClassificationResult.java | 2 +-
.../handler/execute/MCPSQLExecutionFacade.java | 56 +-
...ationClass.java => SQLStatementObjectName.java} | 24 +-
.../execute/SQLStatementObjectNameReader.java | 88 ++
...ifier.java => SQLStatementSafetyValidator.java} | 107 +-
.../handler/execute/SQLStatementStructure.java | 1 +
.../execute/SQLStatementTargetResolver.java | 81 +-
.../tool/handler/execute/StatementClassifier.java | 195 +---
.../SQLExecutionTraceFactory.java} | 18 +-
.../SQLExecutionTraceRecord.java} | 10 +-
.../CoreToolDescriptorValidatorTest.java | 15 +-
.../capability/ServerCapabilitiesHandlerTest.java | 16 +-
.../tool/handler/ToolDefinitionRegistryTest.java | 18 +-
.../handler/execute/MCPSQLExecutionFacadeTest.java | 234 ++--
.../SQLExecutionTraceFactoryTest.java} | 18 +-
.../metadata/SearchMetadataToolServiceTest.java | 5 +-
.../service/EncryptWorkflowPlanningService.java | 2 +-
.../EncryptRuleDistSQLPlanningServiceTest.java | 2 +-
.../service/PhysicalDDLPlanningServiceTest.java | 4 +-
.../MaskRuleDistSQLPlanningServiceTest.java | 2 +-
.../descriptor/MCPShardingSphereMetadataKeys.java | 2 -
.../workflow/service/WorkflowPlanningSupport.java | 2 +-
.../support/workflow/service/WorkflowSQLUtils.java | 3 +-
.../MCPDescriptorCatalogValidatorTest.java | 2 +-
.../support/protocol/MCPNextActionUtilsTest.java | 16 +-
.../workflow/service/WorkflowSQLUtilsTest.java | 2 +-
.../AbstractLLMMCPConversationRunnerTest.java | 178 +++
.../LLMMCPConversationRunnerCoverageTest.java | 259 +++++
.../LLMMCPConversationRunnerFailureTest.java | 309 ++++++
.../LLMMCPConversationRunnerNextActionTest.java | 432 ++++++++
.../conversation/LLMMCPConversationRunnerTest.java | 1144 ++------------------
.../conversation/client/LLMChatModelClient.java | 43 +-
.../AbstractProductionMySQLRuntimeE2ETest.java | 383 +++++++
.../production/PackagedDistributionE2ETest.java | 4 +-
.../production/ProductionMySQLRuntimeE2ETest.java | 668 +-----------
.../ProductionMySQLSQLRuntimeE2ETest.java | 373 +++++++
.../programmatic/HttpTransportContractE2ETest.java | 148 ---
.../HttpTransportProtocolContractE2ETest.java | 191 ++++
.../programmatic/MetadataDiscoveryE2ETest.java | 29 +-
.../assertion/MCPModelContractAssertions.java | 4 +-
.../distribution/DockerImageHttpRuntime.java | 51 +-
.../PackagedDistributionHttpRuntime.java | 40 +-
.../support/runtime/MySQLRuntimeTestSupport.java | 42 +-
.../e2e/mcp/support/runtime/ReadinessProbe.java | 231 ++++
.../mcp/support/runtime/ReadinessProbeTest.java | 86 ++
56 files changed, 3694 insertions(+), 3275 deletions(-)
diff --git a/docs/mcp/ShardingSphere-MCP-Detailed-Design.md b/docs/mcp/ShardingSphere-MCP-Detailed-Design.md
index 361a2c37f64..500f39bad3b 100644
--- a/docs/mcp/ShardingSphere-MCP-Detailed-Design.md
+++ b/docs/mcp/ShardingSphere-MCP-Detailed-Design.md
@@ -155,7 +155,7 @@ shardingsphere
- `session`
- `metadata`
- `execute`
- - `audit`
+ - `trace`
- `config`
- `common`
- 说明:
@@ -177,8 +177,8 @@ shardingsphere
- `execute`
- `execute_query` facade
- `DatabaseRuntime` assembly and JDBC-backed runtime context factory
- - `audit`
- - audit facade
+ - `trace`
+ - SQL execution trace facade
- `config`
- MCP 内部配置模型
- `common`
@@ -392,7 +392,7 @@ stateDiagram-v2
- 调用 ShardingSphere parse / route / execute
- 统一结果映射
- 统一错误映射
- - 审计输出
+ - SQL execution trace 输出
### 10.2 执行阶段
- 内部固定为 7 个阶段:
@@ -422,7 +422,7 @@ stateDiagram-v2
- `savepoint`
- `explain_analyze`
- `WITH` 只表示 CTE 写法,不单独形成产品 statement class。
-- `statement class` 用于 capability、audit 和执行分支。
+- `statement class` 用于 capability、SQL execution trace 和执行分支。
- `statement type` 用于表达更具体的用户可读动作,如 `SELECT`、`UPDATE`、`MERGE`。
### 10.4 统一结果
@@ -484,23 +484,24 @@ stateDiagram-v2
- `driverClassName`
- schema 采用 strict mapping
- unknown key 直接报错
- - legacy `runtime.*` alias 不再作为专门迁移分支保留
+ - `runtime.*` alias 不再作为专门迁移分支保留
- per-database capability booleans 不再接受为 operator-facing 配置
- distribution 默认配置提供一段 demo JDBC runtime,确保发行包第一次启动即可验证非空 metadata 与真实执行链路;真实部署需替换为目标环境配置
-## 12. 运行边界与审计详细设计
+## 12. 运行边界与 SQL Execution Trace 详细设计
### 12.1 运行边界
- V1 内置 runtime 聚焦 session 协商、会话状态维护与运行边界校验。
- follow-up production runtime 路径要求通过 `runtimeDatabases` 显式装配真实 metadata 与执行适配,不再允许空 runtime 作为成功启动兜底。
- HTTP 端点如需对外暴露,应放在受信网络、上游网关、反向代理或其他网络边界之后。
-### 12.2 审计模型
+### 12.2 SQL execution trace 模型
+- V1 只生成当前 SQL 执行路径的本地 trace 摘要,不承诺持久化合规日志或全量 MCP 活动记录。
- 统一输出:
- sessionId
- database
- - operationClass
- - operationDigest
+ - statementClass
+ - statementDigest
- successOrFailure
- errorCode
- transactionMarker
diff --git a/docs/mcp/ShardingSphere-MCP-PRD.md b/docs/mcp/ShardingSphere-MCP-PRD.md
index fe717f2a780..60e5f7fd598 100644
--- a/docs/mcp/ShardingSphere-MCP-PRD.md
+++ b/docs/mcp/ShardingSphere-MCP-PRD.md
@@ -20,7 +20,7 @@
- 当前数据库接入主要存在以下问题:
- 不同数据库 MCP 的资源命名、工具定义、返回结构和能力边界不一致。
- 上层 Agent 需要分别适配多种数据库,接入成本高。
-- 企业希望统一数据库访问的对象暴露、审计、超时和结果限制。
+- 企业希望统一数据库访问的对象暴露、SQL execution trace、超时和结果限制。
- 即使同样是列库、查结构、执行 SQL,不同数据库的行为、错误和结果表达也不一致。
- ShardingSphere 具备统一接入多种数据库的能力,因此适合作为统一 MCP 出口。
@@ -73,7 +73,7 @@
- Agent 统一发现多个数据库中的可访问对象。
- Agent 统一读取表结构、字段、视图等元数据。
- Agent 在不同数据库下使用同一套工具执行 SQL。
-- 平台统一承接审计、超时和结果限制要求。
+- 平台统一承接 SQL execution trace、超时和结果限制要求。
- 数据库结构变化后,Agent 在分钟级看到 MCP 资源更新。
- MCP 会话内通过事务控制语句管理事务边界。
@@ -178,11 +178,11 @@
### 工具定义
- `list_databases`
- `list_schemas(database)`
-- `list_tables(database, schema, search?, page_size?, page_token?)`
-- `list_views(database, schema, search?, page_size?, page_token?)`
-- `list_columns(database, schema, object_type, object_name, search?, page_size?, page_token?)`
-- `list_indexes(database, schema, table, search?, page_size?, page_token?)`
-- `search_metadata(database?, schema?, query, object_types?, page_size?, page_token?)`
+- `list_tables(database, schema, search?)`
+- `list_views(database, schema, search?)`
+- `list_columns(database, schema, object_type, object_name, search?)`
+- `list_indexes(database, schema, table, search?)`
+- `search_metadata(database?, schema?, query, object_types?)`
- `describe_table(database, schema, table)`
- `describe_view(database, schema, view)`
- `get_capabilities(database?)`
@@ -210,13 +210,14 @@
- `get_capabilities(database)` 返回数据库级 `capability`。
- 当当前 `database` 不支持 `index` 时,`list_indexes` 统一返回 `unsupported`。
-## 13. 搜索、过滤与分页
+## 13. 搜索、过滤与收窄
### V1 必须支持
- 列表查询支持关键字搜索。
-- 列表查询支持分页。
+- 大结果通过 `truncated`、`total_count`、`returned_count` 和收窄提示表达。
- 元数据搜索支持按对象类型过滤。
-- 分页结果必须返回下一页标识或等价机制。
+- item-list 响应保留 application-level `has_more` 和 `continuation_mode` 字段;当响应支持 ShardingSphere application-level pagination 时才返回 `next_page_token`。
+- 上述 structured payload 字段不是 MCP list 方法的 `cursor` 或 `nextCursor`。
- 不允许客户端通过完整枚举完成大型数据库结构发现。
### `search_metadata.object_types` 正式支持
@@ -288,7 +289,7 @@
## 16. MCP 会话与事务语义
- 每个 MCP 会话对应一个逻辑会话上下文。
-- V1 内置运行时直接使用 `session_id` 作为审计标签。
+- V1 内置运行时直接使用 `session_id` 作为 SQL execution trace 标签。
- 会话期间该 `session_id` 保持不变。
- 会话不支持恢复;会话结束即逻辑上下文结束。
- 默认执行模式为 `autocommit = true`。
@@ -394,12 +395,12 @@
- `index` 是否支持由 `supported_object_types` 明确声明。
- DDL、DCL、`EXPLAIN ANALYZE` 的事务边界行为必须由数据库级 `capability` 明确声明。
-## 20. 运行边界与审计
+## 20. 运行边界与 SQL Execution Trace
### V1 当前实现
- 内置 runtime 不拦截调用请求。
- HTTP 端点如需对外暴露,应放在受信网络、上游网关、反向代理或其他网络边界之后。
-- 元数据读取、工具调用与 SQL 执行都必须纳入审计。
+- SQL 执行路径生成本地 trace 摘要;V1 不承诺持久化合规日志或全量 MCP 活动记录。
### V1 不纳入正式验收
- 内置对象级可见性裁剪
@@ -424,13 +425,13 @@
- 若结构变更通过当前 MCP 会话执行成功,当前会话中应尽快可见。
- 全局视角仍以 1 分钟内同步为准。
-## 23. 审计基线
+## 23. SQL Execution Trace 基线
-### 审计记录至少应包含
+### SQL execution trace 记录至少应包含
- `session_id`
- `database`
-- `operation_class`
-- `operation_digest`
+- `statement_class`
+- `statement_digest`
- `success_or_failure`
- `error_code`
- `transaction_marker`
@@ -519,10 +520,10 @@
1. 必须提供统一数据库访问与 SQL 执行公共面。
2. 必须统一 `database / schema / table / view / column / capability` 对象语义。
3. `index` 必须作为数据库级可选公共对象,通过 `capability` 明确声明。
-4. 必须支持对象发现的搜索、过滤与分页。
+4. 必须支持对象发现的搜索、过滤和大结果收窄提示。
5. 必须提供正式公共工具 `execute_query`。
6. 必须支持通用 SQL 执行,但 V1 不包含 `USE`、`SET`、`COPY`、`LOAD`、`CALL` 和各数据库专有高风险元命令。
-7. 必须定义 MCP 会话语义、审计标签和事务语义。
+7. 必须定义 MCP 会话语义、SQL execution trace 标签和事务语义。
8. 必须区分事务控制语义与 `savepoint` 语义。
9. 必须提供统一执行结果模型。
10. 必须补充服务级 `capability` 与数据库级 `capability` 的正式内容模型。
diff --git a/docs/mcp/ShardingSphere-MCP-Technical-Design.md b/docs/mcp/ShardingSphere-MCP-Technical-Design.md
index ce98cfa94c5..0b7ec0ed921 100644
--- a/docs/mcp/ShardingSphere-MCP-Technical-Design.md
+++ b/docs/mcp/ShardingSphere-MCP-Technical-Design.md
@@ -31,7 +31,7 @@
- 统一 SQL 执行入口 `execute_query`
- 统一错误模型
- 统一事务与 `savepoint` 能力边界
- - 统一审计与治理能力
+ - 统一 SQL execution trace 与治理可见性
- 当前仓库和生态事实如下:
- 根工程模块结构见 `pom.xml`(line 33)
- 根工程 Java 基线为 8,`pom.xml`(line 55)
@@ -60,7 +60,7 @@
- `capability`
- `execute_query`
- `transaction matrix`
- - `audit`
+ - `SQL execution trace`
- 统一错误模型
## 5. 非目标
@@ -216,7 +216,7 @@ shardingsphere
- tool catalog 与参数归一化
- 通用 payload / error payload builder
- error model
-- audit facade
+- SQL execution trace facade
- 对 ShardingSphere 内核能力的统一门面
- runtime service 聚合
- JDBC runtime 配置模型
@@ -299,7 +299,7 @@ shardingsphere
- STDIO 本地调试入口
- metadata discovery
- `execute_query`
- - 审计与变化可见性
+ - SQL execution trace 与变化可见性
#### 9.1.4 Rollback Guidance
- 如需回滚:
@@ -406,7 +406,7 @@ flowchart TB
E --> F["Metadata / Governance / Execution"]
F --> G["Underlying Databases"]
- H["Audit / Metrics / Logging"] --> C
+ H["Trace / Metrics / Logging"] --> C
H --> D
```
@@ -538,7 +538,7 @@ flowchart TB
- 当前 database 上下文
- 当前事务状态
- `savepoint` 状态
- - 审计使用的 session_id
+ - SQL execution trace 使用的 session_id
### 14.2 HTTP 会话策略
- 由于 PRD 已定义事务与 `savepoint` 语义,HTTP 路径必须维护逻辑 MCP 会话。
@@ -581,19 +581,20 @@ flowchart TB
- 验收标准的技术来源
- 支持矩阵维护的统一资产
-## 15. 运行边界与审计设计
+## 15. 运行边界与 SQL Execution Trace 设计
### 15.1 运行边界
- V1 内置 runtime 聚焦 session 协商、会话状态维护与运行边界校验。
- follow-up production runtime 路径通过 `runtimeDatabases` 显式装配真实 metadata 与执行适配,不再允许默认空 `MetadataCatalog` / 空 `DatabaseRuntime` 作为成功启动路径。
- HTTP 服务如需对外暴露,应放在受信网络、上游网关、反向代理或其他网络边界之后。
-### 15.2 审计基线
+### 15.2 SQL execution trace 基线
+- V1 只生成 SQL 执行路径的本地 trace 摘要,不承诺持久化合规日志或全量 MCP 活动记录。
- 统一输出:
- session_id
- database
- - operation_class
- - operation_digest
+ - statement_class
+ - statement_digest
- success_or_failure
- error_code
- transaction_marker
@@ -607,7 +608,7 @@ flowchart TB
- 独立进程
- 独立容器
- 独立配置目录
- - 独立日志与审计
+ - 独立日志与 SQL execution trace
- 独立版本发布
### 16.2 调试模型
@@ -729,7 +730,7 @@ apache-shardingsphere-mcp-<version>/
- 当前 V1 打包运行时默认具备:
- 服务日志目录
- `conf/logback.xml` 日志配置
- - 审计相关代码路径
+ - SQL execution trace 相关代码路径
- `readiness / liveness` 属于后续运行时增强项,当前 standalone runtime 未直接提供。
- capability 快照
- tool 调用统计
@@ -818,7 +819,7 @@ apache-shardingsphere-mcp-<version>/
- 阶段 3:执行与治理
- 完成 `execute_query`
- 完成事务能力矩阵
- - 完成 audit 治理链路
+ - 完成 SQL execution trace 治理可见性链路
- 阶段 4:部署与联调
- 完成 HTTP 服务化部署
- 完成 STDIO 本地调试
diff --git a/mcp/README.md b/mcp/README.md
index 9a4306867d6..33813f3d78e 100644
--- a/mcp/README.md
+++ b/mcp/README.md
@@ -121,6 +121,8 @@ Notes:
- The current public tools are `database_gateway_search_metadata`, `database_gateway_execute_query`, `database_gateway_execute_update`, `database_gateway_plan_encrypt_rule`, `database_gateway_plan_mask_rule`, `database_gateway_apply_workflow`, and `database_gateway_validate_workflow`.
- `database_gateway_execute_query` accepts classifier-approved `SELECT` and `EXPLAIN ANALYZE` statements only, and rejects known side-effecting query forms.
Use `database_gateway_execute_update` for DML, DDL, DCL, transaction control, savepoints, and other supported side-effecting SQL.
+- SQL execution classification recognizes qualified and quoted object names only for safety checks and cross-schema guarding.
+ This does not expand workflow planning input support or introduce a general SQL expression parser.
- `database_gateway_execute_query.max_rows` uses server default `100` when omitted or set to `0`; explicit values from `1` to `5000` bound returned rows.
- The encrypt and mask workflow targets logical databases exposed by ShardingSphere-Proxy; the dedicated usage notes appear below.
- `database_gateway_search_metadata.object_types` accepts `database`, `schema`, `table`, `view`, `column`, `index`, and `sequence` only.
@@ -195,9 +197,10 @@ Descriptors must describe what the model should use the surface for, not just re
- `fingerprints` records deterministic hashes for descriptor, prompt, navigation, and model-facing schema surfaces so test artifacts can prove which MCP surface a model used.
- Item-list responses always include `items`, `count`, `has_more`, and `continuation_mode`. `next_page_token` appears only when a response supports ShardingSphere application-level pagination;
these structured payload fields are not MCP list-method `cursor` or `nextCursor`.
- Resource reads also include `self_uri`, and include typed `parent_resource` or typed `next_resources` when applicable.
+ Large result payloads use typed `truncated`, `total_count`, `returned_count`, and `large_result_guidance` fields when the server needs a narrower follow-up query.
+ Resource reads include `self_uri`, and include typed `parent_resource` or typed `next_resources` when applicable.
- Workflow tool responses include `missing_required_inputs`, `clarification_questions`, `resources_to_read`, `review_summary`, and `next_actions`
- so a model can continue the workflow without guessing or relying on legacy recommendation fields.
+ so a model can continue the workflow without guessing or relying on removed recommendation alias fields.
- Recoverable error payloads keep `message`, and add `recovery` hints for missing arguments, unsupported tools or resources,
invalid enum values, workflow state errors, and unsafe SQL tool selection.
JSON-RPC numeric error codes are the MCP protocol error contract.
@@ -356,7 +359,7 @@ Reference:
## Runtime Notes
- The packaged `conf/mcp-http.yaml` now ships with a demo multi-database JDBC `runtimeDatabases` block so the distribution can prove logical-database discovery and real query execution on the first run.
-- For real deployments, replace the `runtimeDatabases` block with your own logical database mapping and JDBC connection properties. Each logical database entry must declare its own required runtime fields; schema discovery now comes from JDBC metadata, and legacy `runtime.*` aliases are no longer supported.
+- For real deployments, replace the `runtimeDatabases` block with your own logical database mapping and JDBC connection properties. Each logical database entry must declare its own required runtime fields; schema discovery now comes from JDBC metadata, and `runtime.*` aliases are no longer supported.
- `driverClassName` is optional for JDBC 4 drivers that auto-register through `DriverManager`. Keep it only when your target driver requires an explicit override.
- The packaged distribution keeps the official MCP baseline jars, including encrypt and mask, under `lib/`.
- If your target database driver or an extra MCP feature jar is not already packaged, copy that jar under `plugins/` before running `bin/start.sh` or `bin\start.bat`.
@@ -1174,8 +1177,8 @@ curl -sS http://127.0.0.1:18088/mcp \
- `encrypt drop` removes rules only; physical derived columns and indexes still require manual cleanup
- no existing-data migration or backfill
- no automatic rollback support
-- no audit persistence
-- V1 supports unquoted SQL identifiers only
+- no SQL execution trace persistence
+- workflow and generated DDL/DistSQL planning inputs use standard unquoted logical identifiers only
## Registry and OCI Publication
@@ -1281,7 +1284,7 @@ sh test/e2e/mcp/src/test/resources/docker/llm-runtime/build-local.sh
- `mcp/support`: database metadata, execution, capability, and workflow contexts, models, facades, SPI, and reusable helpers for MCP core and pluggable features
- `mcp/features/encrypt`: encrypt tools, resources, planning / apply / validation, and algorithm visibility assembly
- `mcp/features/mask`: mask tools, resources, planning / apply / validation, and algorithm visibility assembly
-- `mcp/core`: handler discovery, registry, request scope implementation, session, audit, execute-query runtime service assembly, JDBC runtime configuration, metadata discovery, `DatabaseRuntime` assembly, and the JDBC-backed runtime context factory
+- `mcp/core`: handler discovery, registry, request scope implementation, session, SQL execution trace creation, execute-query runtime service assembly, JDBC runtime configuration, metadata discovery, `DatabaseRuntime` assembly, and the JDBC-backed runtime context factory
- `mcp/bootstrap`: MCP Java SDK based bootstrap, HTTP / STDIO transport, top-level config loading, feature SPI aggregation, and lifecycle management
- `distribution/mcp`: standalone packaging, scripts, config, Dockerfile
- `test/e2e/mcp`: end-to-end contract validation
diff --git a/mcp/README_ZH.md b/mcp/README_ZH.md
index 75d4466d58b..4b7d25b3a91 100644
--- a/mcp/README_ZH.md
+++ b/mcp/README_ZH.md
@@ -120,6 +120,8 @@ curl -sS http://127.0.0.1:18088/mcp \
- 当前 public tools 包括 `database_gateway_search_metadata`、`database_gateway_execute_query`、`database_gateway_execute_update`、`database_gateway_plan_encrypt_rule`、`database_gateway_plan_mask_rule`、`database_gateway_apply_workflow` 和 `database_gateway_validate_workflow`。
- `database_gateway_execute_query` 只接受分类器批准的 `SELECT` 和 `EXPLAIN ANALYZE`,并拒绝已知有副作用的查询形态;
DML、DDL、DCL、事务控制、savepoint 以及其他支持的有副作用 SQL 要使用 `database_gateway_execute_update`。
+- SQL 执行分类会识别限定名和带引号对象名,但用途仅限安全分类与跨 schema 防护;
+ 这不代表 workflow 规划输入支持这些写法,也不代表 MCP 内置通用 SQL 表达式解析器。
- `database_gateway_execute_query.max_rows` 省略或传 `0` 时使用服务端默认值 `100`;显式传 `1` 到 `5000` 用于限制返回行数。
- 加密与脱敏 workflow 面向由 ShardingSphere-Proxy 暴露的逻辑库;下文会单独说明这部分的前置条件和使用方式。
- `database_gateway_search_metadata.object_types` 只接受 `database`、`schema`、`table`、`view`、`column`、`index`、`sequence`。
@@ -193,7 +195,7 @@ Descriptor 必须说明模型该如何使用这个 surface,而不是只重复
- `shardingsphere://runtime` 暴露轻量运行时状态,`shardingsphere://workflows/{plan_id}` 支持按 plan_id 回读 workflow plan。
- `fingerprints` 记录 descriptor、prompt、navigation 和模型可见 schema 的确定性哈希,让测试产物能证明模型使用的是哪一版 MCP surface。
- item-list 响应总会包含 `items`、`count`、`has_more` 和 `continuation_mode`。`next_page_token` 只会在响应支持 ShardingSphere application-level pagination 时出现;
- 这些 structured payload 字段不是 MCP list 方法的 `cursor` 或 `nextCursor`。
+ 这些 structured payload 字段不是 MCP list 方法的 `cursor` 或 `nextCursor`。大结果 payload 在需要收窄后续查询时使用 typed `truncated`、`total_count`、`returned_count` 和 `large_result_guidance` 字段。
resource read 还会包含 `self_uri`,并在适用时包含 typed `parent_resource` 或 typed `next_resources`。
- Workflow tool 响应包含 `missing_required_inputs`、`clarification_questions`、`resources_to_read` 和 `next_actions`,
让模型可以按结构化提示继续补问、预览、执行或校验。
@@ -326,7 +328,7 @@ bin\start.bat conf\mcp-stdio.yaml
## Runtime 说明
- 发行包里的 `conf/mcp-http.yaml` 现在默认内置一段 demo 多数据库 JDBC `runtimeDatabases` 配置,所以第一次启动就能验证逻辑库发现和真实 query 执行。
-- 如果要接真实部署,请把 `runtimeDatabases` 段替换成你自己的逻辑库映射和 JDBC 连接属性。每个逻辑库条目都需要显式声明所需的 runtime 字段;schema 发现改为依赖 JDBC metadata,legacy `runtime.*` alias 已不再支持。
+- 如果要接真实部署,请把 `runtimeDatabases` 段替换成你自己的逻辑库映射和 JDBC 连接属性。每个逻辑库条目都需要显式声明所需的 runtime 字段;schema 发现改为依赖 JDBC metadata,`runtime.*` alias 已不再支持。
- 对支持 JDBC 4 自动注册的驱动,`driverClassName` 可以不写;只有目标驱动需要显式覆盖时再配置。
- 发行包默认把 MCP 官方基线 jar,包括 encrypt 和 mask feature,放在 `lib/` 下。
- 如果目标数据库驱动或额外的 MCP feature jar 没有随发行包提供,请先把对应 jar 放到 `plugins/`,再执行 `bin/start.sh` 或 `bin\start.bat`。
@@ -1144,8 +1146,8 @@ curl -sS http://127.0.0.1:18088/mcp \
- `encrypt drop` 只删除规则;物理派生列和索引仍需人工清理
- 不处理存量数据迁移或回填
- 不提供自动回滚能力
-- 不做审计落库
-- V1 只支持未加引号的 SQL identifier
+- 不持久化 SQL 执行 trace
+- workflow 与生成 DDL/DistSQL 的规划输入只接受标准未加引号的逻辑 identifier
## Registry 与 OCI 发布
@@ -1250,7 +1252,7 @@ sh test/e2e/mcp/src/test/resources/docker/llm-runtime/build-local.sh
- `mcp/support`:为 MCP core 与可插拔 feature 提供 database metadata、execution、capability、workflow context、model、facade、SPI 与复用 helper
- `mcp/features/encrypt`:encrypt tools、resources、planning / apply / validation 与算法可见性装配
- `mcp/features/mask`:mask tools、resources、planning / apply / validation 与算法可见性装配
-- `mcp/core`:handler 发现、registry、request scope 实现、session、audit、execute-query runtime service 聚合,以及 JDBC runtime 配置模型、metadata 发现、`DatabaseRuntime` 装配与 JDBC-backed runtime context factory
+- `mcp/core`:handler 发现、registry、request scope 实现、session、SQL execution trace 创建、execute-query runtime service 聚合,以及 JDBC runtime 配置模型、metadata 发现、`DatabaseRuntime` 装配与 JDBC-backed runtime context factory
- `mcp/bootstrap`:基于 MCP Java SDK 的 bootstrap、HTTP / STDIO transport、顶层配置加载、feature SPI 汇总与生命周期管理
- `distribution/mcp`:独立打包、启动脚本、配置、Dockerfile
- `test/e2e/mcp`:端到端契约验证
diff --git a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/MCPDocumentationContractTest.java b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/MCPDocumentationContractTest.java
index 6c266bfa79b..1bab8ecde80 100644
--- a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/MCPDocumentationContractTest.java
+++ b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/MCPDocumentationContractTest.java
@@ -47,7 +47,7 @@ class MCPDocumentationContractTest {
}
@Test
- void assertDocumentationOmitsLegacyPayloadFields() throws IOException {
+ void assertDocumentationOmitsRemovedPayloadFields() throws IOException {
String actual = Files.readString(resolveMCPDirectory().resolve("README.md")) + Files.readString(resolveMCPDirectory().resolve("README_ZH.md"));
for (String each : List.of("pending_questions", "resource_uri", "parent_uri", "next_resource_uris", "read_resources_first", "empty_reason", "not_found_reason")) {
assertFalse(actual.contains(each));
diff --git a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/AbstractMCPToolSpecificationFactoryTest.java b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/AbstractMCPToolSpecificationFactoryTest.java
new file mode 100644
index 00000000000..56d99424d52
--- /dev/null
+++ b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/AbstractMCPToolSpecificationFactoryTest.java
@@ -0,0 +1,308 @@
+/*
+ * 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.capability.tool;
+
+import io.modelcontextprotocol.server.McpServerFeatures.SyncToolSpecification;
+import io.modelcontextprotocol.server.McpSyncServerExchange;
+import io.modelcontextprotocol.spec.McpSchema;
+import io.modelcontextprotocol.spec.McpSchema.CallToolRequest;
+import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
+import org.apache.shardingsphere.mcp.api.protocol.response.MCPResponse;
+import org.apache.shardingsphere.mcp.api.tool.MCPToolHandler;
+import org.apache.shardingsphere.mcp.api.tool.descriptor.MCPToolAnnotations;
+import org.apache.shardingsphere.mcp.api.tool.descriptor.MCPToolDescriptor;
+import org.apache.shardingsphere.mcp.core.context.MCPRequestScope;
+import org.apache.shardingsphere.mcp.core.context.MCPRuntimeContext;
+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;
+import org.apache.shardingsphere.mcp.support.protocol.MCPResourceHintUtils;
+import org.mockito.MockedStatic;
+
+import java.time.Clock;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+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;
+import static org.mockito.ArgumentMatchers.any;
+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;
+
+abstract class AbstractMCPToolSpecificationFactoryTest {
+
+ protected McpSyncServerExchange createElicitationExchange(final McpSchema.ElicitResult elicitationResult) {
+ return createElicitationExchange(elicitationResult, McpSchema.ClientCapabilities.builder().elicitation().build());
+ }
+
+ protected McpSyncServerExchange createElicitationExchange(final McpSchema.ElicitResult elicitationResult, final McpSchema.ClientCapabilities clientCapabilities) {
+ McpSyncServerExchange result = mock(McpSyncServerExchange.class);
+ when(result.sessionId()).thenReturn("session-id");
+ when(result.getClientCapabilities()).thenReturn(clientCapabilities);
+ when(result.createElicitation(any())).thenReturn(elicitationResult);
+ return result;
+ }
+
+ protected McpSchema.ClientCapabilities createFormOnlyClientCapabilities() {
+ return new McpSchema.ClientCapabilities(
+ Collections.emptyMap(), null, null,
+ new McpSchema.ClientCapabilities.Elicitation(new McpSchema.ClientCapabilities.Elicitation.Form(), null));
+ }
+
+ protected McpSchema.ClientCapabilities createFormAndUrlClientCapabilities() {
+ return new McpSchema.ClientCapabilities(
+ Collections.emptyMap(), null, null,
+ new McpSchema.ClientCapabilities.Elicitation(new McpSchema.ClientCapabilities.Elicitation.Form(), new McpSchema.ClientCapabilities.Elicitation.Url()));
+ }
+
+ protected McpSyncServerExchange createUrlOnlyElicitationExchange() {
+ McpSyncServerExchange result = mock(McpSyncServerExchange.class);
+ when(result.sessionId()).thenReturn("session-id");
+ when(result.getClientCapabilities()).thenReturn(new McpSchema.ClientCapabilities(
+ Collections.emptyMap(), null, null,
+ new McpSchema.ClientCapabilities.Elicitation(null, new McpSchema.ClientCapabilities.Elicitation.Url())));
+ return result;
+ }
+
+ protected McpSyncServerExchange createThrowingElicitationExchange() {
+ McpSyncServerExchange result = mock(McpSyncServerExchange.class);
+ when(result.sessionId()).thenReturn("session-id");
+ when(result.getClientCapabilities()).thenReturn(McpSchema.ClientCapabilities.builder().elicitation().build());
+ when(result.createElicitation(any())).thenThrow(new IllegalStateException("unsupported"));
+ return result;
+ }
+
+ protected SyncToolSpecification findToolSpecification(final List<SyncToolSpecification> specifications, final String toolName) {
+ return specifications.stream().filter(each -> toolName.equals(each.tool().name())).findFirst().orElseThrow();
+ }
+
+ protected MCPToolDefinition mockSupportedTool(final MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry, final MCPToolDescriptor toolDescriptor) {
+ MCPToolDefinition result = new MCPToolDefinition(toolDescriptor, mock(MCPToolHandler.class));
+ mockedToolDefinitionRegistry.when(ToolDefinitionRegistry::getSupportedToolDescriptors).thenReturn(List.of(toolDescriptor));
+ mockedToolDefinitionRegistry.when(() -> ToolDefinitionRegistry.getToolDefinition(toolDescriptor.getName())).thenReturn(result);
+ return result;
+ }
+
+ protected void mockToolDispatch(final MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry, final MCPToolDefinition toolDefinition,
+ final Map<String, Object> arguments, final MCPResponse response) {
+ mockedToolDefinitionRegistry.when(() -> ToolDefinitionRegistry.dispatch(any(MCPRequestScope.class), eq(toolDefinition), eq("session-id"), eq(arguments)))
+ .thenReturn(response);
+ }
+
+ protected MCPRuntimeContext createRuntimeContext(final String activeTransport) {
+ MCPRuntimeContext result = mock(MCPRuntimeContext.class, RETURNS_DEEP_STUBS);
+ when(result.getSessionManager().getTransactionResourceManager().getRuntimeDatabases()).thenReturn(Collections.emptyMap());
+ when(result.getActiveTransport()).thenReturn(activeTransport);
+ return result;
+ }
+
+ protected void assertStructuredFallback(final CallToolResult actual, final String expectedReason, final boolean expectedFormMode, final boolean expectedUrlMode,
+ final String expectedInteraction) {
+ Map<String, Object> actualPayload = getStructuredContent(actual);
+ assertThat(actualPayload.get("fallback_reason"), is(expectedReason));
+ Map<?, ?> actualSupport = (Map<?, ?>) actualPayload.get("elicitation_support");
+ assertThat(actualSupport.get("form_mode"), is(expectedFormMode));
+ assertThat(actualSupport.get("url_mode"), is(expectedUrlMode));
+ assertThat(actualSupport.get("selected_interaction"), is(expectedInteraction));
+ assertFalse(actual.isError());
+ }
+
+ protected void assertSanitizedSensitiveFallback(final CallToolResult actual) {
+ Map<String, Object> actualPayload = getStructuredContent(actual);
+ List<?> actualQuestions = (List<?>) actualPayload.get("clarification_questions");
+ assertFalse(actualQuestions.isEmpty());
+ Map<?, ?> actualQuestion = (Map<?, ?>) actualQuestions.get(0);
+ assertThat(actualQuestion.get("input_type"), is("secret"));
+ assertTrue(Boolean.TRUE.equals(actualQuestion.get("secret")));
+ assertTrue(actualQuestion.containsKey("message"));
+ assertFalse(actualQuestion.containsKey("display_message"));
+ List<?> actualNextActions = (List<?>) actualPayload.get("next_actions");
+ assertThat(((Map<?, ?>) actualNextActions.get(0)).get("type"), is("terminal"));
+ assertFalse(String.valueOf(actualPayload).contains("Provide access token."));
+ assertFalse(String.valueOf(actualPayload).contains("Provide display name."));
+ assertFalse(String.valueOf(actualPayload).contains("Provide props."));
+ }
+
+ @SuppressWarnings("unchecked")
+ protected Map<String, Object> getStructuredContent(final CallToolResult actual) {
+ return (Map<String, Object>) actual.structuredContent();
+ }
+
+ protected CallToolResult createCallToolResult(final String toolName, final MCPResponse response) {
+ try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
+ MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createToolDescriptorWithoutOutputSchema(toolName));
+ mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
+ MCPRuntimeContext runtimeContext = mock(MCPRuntimeContext.class, RETURNS_DEEP_STUBS);
+ when(runtimeContext.getSessionManager().getTransactionResourceManager().getRuntimeDatabases()).thenReturn(Collections.emptyMap());
+ SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(runtimeContext).createToolSpecifications().get(0);
+ McpSyncServerExchange exchange = mock(McpSyncServerExchange.class);
+ when(exchange.sessionId()).thenReturn("session-id");
+ return actualSpecification.callHandler().apply(exchange, new CallToolRequest(toolName, Map.of()));
+ }
+ }
+
+ protected Map<String, Object> createClarifyingPayload() {
+ return Map.of(
+ "plan_id", "plan-1",
+ "status", "clarifying",
+ "clarification_questions", List.of(
+ createClarifyingQuestion("custom_properties.display-name", "string", false, "Provide display name."),
+ createClarifyingQuestion("requires_review", "boolean", false, "Require review?")));
+ }
+
+ protected Map<String, Object> createClarifyingPayload(final Map<String, Object> question) {
+ return Map.of(
+ "plan_id", "plan-1",
+ "status", "clarifying",
+ "clarification_questions", List.of(question));
+ }
+
+ protected Map<String, Object> createClarifyingPayloadWithoutPlanId() {
+ return Map.of(
+ "status", "clarifying",
+ "clarification_questions", List.of(createClarifyingQuestion("custom_properties.display-name", "string", false, "Provide display name.")));
+ }
+
+ protected Map<String, Object> createClarifyingQuestion(final String field, final String inputType, final boolean secret, final String displayMessage) {
+ return Map.of("field", field, "input_type", inputType, "secret", secret, "display_message", displayMessage);
+ }
+
+ protected Map<String, Object> createClarifyingQuestion(final String field, final String inputType, final boolean secret,
+ final String displayMessage, final List<String> allowedValues) {
+ return Map.of("field", field, "input_type", inputType, "secret", secret, "display_message", displayMessage, "allowed_values", allowedValues);
+ }
+
+ protected Map<String, Object> createElicitedArguments() {
+ return Map.of(
+ "plan_id", "plan-1",
+ "custom_properties", Map.of("display-name", "foo_display"),
+ "intent", Map.of("requires_review", true));
+ }
+
+ protected Map<String, Object> createExpectedElicitRequestedSchema() {
+ Map<String, Object> properties = new LinkedHashMap<>(2, 1F);
+ properties.put("field_1", Map.of("type", "string", "description", "Provide display name."));
+ properties.put("field_2", Map.of("type", "boolean", "description", "Require review?"));
+ Map<String, Object> result = new LinkedHashMap<>(4, 1F);
+ result.put("type", "object");
+ result.put("properties", properties);
+ result.put("required", List.of("field_1", "field_2"));
+ result.put("additionalProperties", false);
+ return result;
+ }
+
+ protected MCPToolDescriptor createToolDescriptor(final String toolName) {
+ Map<String, Object> properties = new LinkedHashMap<>(2, 1F);
+ properties.put("query", Map.of("type", "string", "description", "Search query."));
+ properties.put("object_types", Map.of("type", "array", "description", "Optional object-type filter.",
+ "items", Map.of("type", "string", "description", "Object type.", "enum", List.of("TABLE", "VIEW"))));
+ return new MCPToolDescriptor(toolName, "Search Metadata", "Search database metadata.", createInputSchema(properties, List.of("query")),
+ Map.of("type", "object"), new MCPToolAnnotations("Search Metadata", true, false, true, true),
+ Map.of(MCPShardingSphereMetadataKeys.RELATED_RESOURCE_URIS, List.of("shardingsphere://databases")));
+ }
+
+ protected MCPToolDescriptor createToolDescriptor(final String toolName, final MCPToolAnnotations annotations) {
+ return new MCPToolDescriptor(toolName, "Fixture Tool", "Run a fixture tool.", createInputSchema(Map.of(), List.of()), Map.of("type", "object"), annotations, Collections.emptyMap());
+ }
+
+ protected MCPToolDescriptor createToolDescriptorWithoutOutputSchema(final String toolName) {
+ return new MCPToolDescriptor(toolName, "Fixture Tool", "Run a fixture tool.", createInputSchema(Map.of(), List.of()), Collections.emptyMap(),
+ new MCPToolAnnotations("Fixture Tool", true, false, true, true), Collections.emptyMap());
+ }
+
+ protected MCPToolDescriptor createStrictToolDescriptor(final String toolName) {
+ return new MCPToolDescriptor(toolName, "Search Metadata", "Search database metadata.", createInputSchema(Map.of(), List.of()),
+ Map.of("type", "object", "properties", Map.of("status", Map.of("type", "string")), "required", List.of("status")),
+ new MCPToolAnnotations("Search Metadata", true, false, true, true), Collections.emptyMap());
+ }
+
+ protected MCPToolDescriptor createPlanningToolDescriptor(final String toolName) {
+ Map<String, Object> properties = new LinkedHashMap<>(2, 1F);
+ properties.put("custom_properties", Map.of("type", "object", "description", "Custom properties.", "additionalProperties", true));
+ properties.put("intent", Map.of("type", "object", "description", "Intent.", "properties",
+ Map.of("requires_review", Map.of("type", "boolean", "description", "Requires review.")), "required", List.of(), "additionalProperties", false));
+ return new MCPToolDescriptor(toolName, "Plan Custom Rule", "Plan a custom rule.", createInputSchema(properties, List.of()),
+ Map.of("type", "object"), new MCPToolAnnotations("Plan Custom Rule", false, false, true, true), Collections.emptyMap());
+ }
+
+ protected MCPToolDescriptor createAmbiguousPlanningToolDescriptor(final String toolName) {
+ Map<String, Object> objectProperty = Map.of(
+ "type", "object",
+ "description", "Intent.",
+ "properties", Map.of("requires_review", Map.of("type", "boolean", "description", "Requires review.")),
+ "required", List.of(),
+ "additionalProperties", false);
+ Map<String, Object> properties = new LinkedHashMap<>(2, 1F);
+ properties.put("intent", objectProperty);
+ properties.put("review_policy", objectProperty);
+ return new MCPToolDescriptor(toolName, "Plan Custom Rule", "Plan a custom rule.", createInputSchema(properties, List.of()),
+ Map.of("type", "object"), new MCPToolAnnotations("Plan Custom Rule", false, false, true, true), Collections.emptyMap());
+ }
+
+ protected Map<String, Object> createInputSchema(final Map<String, Object> properties, final List<String> required) {
+ Map<String, Object> result = new LinkedHashMap<>(4, 1F);
+ result.put("type", "object");
+ result.put("properties", properties);
+ result.put("required", required);
+ result.put("additionalProperties", false);
+ return result;
+ }
+
+ protected List<Map<String, Object>> createResourceHints(final String uriPrefix, final String sourceField, final int count) {
+ List<Map<String, Object>> result = new LinkedList<>();
+ for (int i = 0; i < count; i++) {
+ result.add(MCPResourceHintUtils.create(uriPrefix + i, "logical-database", "inspect_detail", "Read resource.", sourceField));
+ }
+ return result;
+ }
+
+ protected static final class MutableClock extends Clock {
+
+ private Instant instant = Instant.parse("2026-05-22T00:00:00Z");
+
+ @Override
+ public ZoneId getZone() {
+ return ZoneId.of("UTC");
+ }
+
+ @Override
+ public Clock withZone(final ZoneId zone) {
+ return this;
+ }
+
+ @Override
+ public Instant instant() {
+ return instant;
+ }
+
+ protected void advance(final Duration duration) {
+ instant = instant.plus(duration);
+ }
+ }
+}
diff --git a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPToolSpecificationElicitationFactoryTest.java b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPToolSpecificationElicitationFactoryTest.java
new file mode 100644
index 00000000000..92dd1e233cf
--- /dev/null
+++ b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPToolSpecificationElicitationFactoryTest.java
@@ -0,0 +1,397 @@
+/*
+ * 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.capability.tool;
+
+import io.modelcontextprotocol.server.McpServerFeatures.SyncToolSpecification;
+import io.modelcontextprotocol.server.McpSyncServerExchange;
+import io.modelcontextprotocol.spec.McpSchema;
+import io.modelcontextprotocol.spec.McpSchema.CallToolRequest;
+import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
+import org.apache.shardingsphere.mcp.api.protocol.response.MCPResponse;
+import org.apache.shardingsphere.mcp.core.context.MCPRequestScope;
+import org.apache.shardingsphere.mcp.core.context.MCPRuntimeContext;
+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;
+import org.apache.shardingsphere.mcp.support.protocol.response.MCPMapResponse;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.MockedStatic;
+
+import java.time.Clock;
+import java.time.Duration;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.isA;
+import static org.mockito.ArgumentMatchers.any;
+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.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class MCPToolSpecificationElicitationFactoryTest extends AbstractMCPToolSpecificationFactoryTest {
+
+ @Test
+ void assertCreateToolSpecificationsHandleInteractiveElicitation() {
+ assertInteractiveElicitation(McpSchema.ClientCapabilities.builder().elicitation().build());
+ }
+
+ @Test
+ void assertCreateToolSpecificationsHandleInteractiveElicitationWithFormOnlyClient() {
+ assertInteractiveElicitation(createFormOnlyClientCapabilities());
+ }
+
+ @Test
+ void assertCreateToolSpecificationsHandleInteractiveElicitationWithFormAndUrlClient() {
+ assertInteractiveElicitation(createFormAndUrlClientCapabilities());
+ }
+
+ private void assertInteractiveElicitation(final McpSchema.ClientCapabilities clientCapabilities) {
+ try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
+ String toolName = "database_gateway_plan_encrypt_rule";
+ MCPResponse clarifyingResponse = new MCPMapResponse(createClarifyingPayload());
+ MCPResponse plannedResponse = new MCPMapResponse(Map.of("status", "planned"));
+ MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor(toolName));
+ mockedToolDefinitionRegistry.when(() -> ToolDefinitionRegistry.dispatch(any(MCPRequestScope.class), eq(toolDefinition), eq("session-id"), any()))
+ .thenReturn(clarifyingResponse,
+ plannedResponse);
+ SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
+ McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT,
+ Map.of("field_1", "foo_display", "field_2", true)), clientCapabilities);
+ CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest(toolName, Map.of()));
+ assertThat(actual.structuredContent(), is(Map.of("status", "planned")));
+ ArgumentCaptor<McpSchema.ElicitRequest> requestCaptor = ArgumentCaptor.forClass(McpSchema.ElicitRequest.class);
+ verify(exchange).createElicitation(requestCaptor.capture());
+ assertThat(requestCaptor.getValue().meta().get(MCPShardingSphereMetadataKeys.TOOL), is(toolName));
+ assertThat(requestCaptor.getValue().meta().get(MCPShardingSphereMetadataKeys.PLAN_ID), is("plan-1"));
+ assertThat(requestCaptor.getValue().meta().get(MCPShardingSphereMetadataKeys.FORM_REQUEST_ID), isA(String.class));
+ assertThat(requestCaptor.getValue().requestedSchema(), is(createExpectedElicitRequestedSchema()));
+ mockedToolDefinitionRegistry.verify(() -> ToolDefinitionRegistry.dispatch(any(MCPRequestScope.class), eq(toolDefinition), eq("session-id"), eq(createElicitedArguments())));
+ }
+ }
+
+ @Test
+ void assertCreateToolSpecificationsSkipElicitationWithSecretQuestion() {
+ assertCreateToolSpecificationsSkipUnsafeElicitation(createClarifyingQuestion("custom_properties.display-name", "string", true, "Provide display name."));
+ }
+
+ @Test
+ void assertCreateToolSpecificationsSkipElicitationWithSecretInputType() {
+ assertCreateToolSpecificationsSkipUnsafeElicitation(createClarifyingQuestion("custom_properties.display-name", "secret", false, "Provide display name."));
+ }
+
+ @Test
+ void assertCreateToolSpecificationsSkipElicitationWithSensitiveFieldName() {
+ assertCreateToolSpecificationsSkipUnsafeElicitation(createClarifyingQuestion("primary_algorithm_properties.access-token", "string", false, "Provide access token."));
+ }
+
+ @Test
+ void assertCreateToolSpecificationsFallbackWithUrlModeForSensitiveQuestion() {
+ try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
+ String toolName = "database_gateway_plan_encrypt_rule";
+ MCPResponse response = new MCPMapResponse(createClarifyingPayload(
+ createClarifyingQuestion("primary_algorithm_properties.access-token", "string", false, "Provide access token.")));
+ MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor(toolName));
+ mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
+ SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
+ McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of()), createFormAndUrlClientCapabilities());
+ CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest(toolName, Map.of()));
+ assertStructuredFallback(actual, "url_mode_not_implemented", true, true, "url_fallback");
+ assertSanitizedSensitiveFallback(actual);
+ verify(exchange, never()).createElicitation(any());
+ }
+ }
+
+ @Test
+ void assertCreateToolSpecificationsSkipElicitationWithoutPlanId() {
+ assertCreateToolSpecificationsSkipUnsafeElicitationWithPayload(createClarifyingPayloadWithoutPlanId(), "missing_plan_id", "structured_fallback");
+ }
+
+ @Test
+ void assertCreateToolSpecificationsSkipElicitationWithUnknownAlgorithmSecretFlag() {
+ assertCreateToolSpecificationsSkipUnsafeElicitationWithPayload(Map.of(
+ "plan_id", "plan-1",
+ "status", "clarifying",
+ "clarification_questions", List.of(Map.of(
+ "field", "primary_algorithm_properties.props",
+ "input_type", "string",
+ "display_message", "Provide props."))),
+ "sensitive_form_blocked", "url_fallback");
+ }
+
+ @Test
+ void assertCreateToolSpecificationsSkipElicitationWithAmbiguousFieldBinding() {
+ try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
+ String toolName = "database_gateway_plan_encrypt_rule";
+ Map<String, Object> expectedPayload = createClarifyingPayload(createClarifyingQuestion("requires_review", "boolean", false, "Require review?"));
+ MCPResponse response = new MCPMapResponse(expectedPayload);
+ MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createAmbiguousPlanningToolDescriptor(toolName));
+ mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
+ SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
+ McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of("field_1", true)));
+ CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest(toolName, Map.of()));
+ assertStructuredFallback(actual, "ambiguous_field_binding", true, false, "structured_fallback");
+ verify(exchange, never()).createElicitation(any());
+ }
+ }
+
+ @Test
+ void assertCreateToolSpecificationsSkipElicitationForUrlOnlyClient() {
+ try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
+ String toolName = "database_gateway_plan_encrypt_rule";
+ Map<String, Object> expectedPayload = createClarifyingPayload();
+ MCPResponse response = new MCPMapResponse(expectedPayload);
+ MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor(toolName));
+ mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
+ SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
+ McpSyncServerExchange exchange = createUrlOnlyElicitationExchange();
+ CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest(toolName, Map.of()));
+ assertStructuredFallback(actual, "client_unsupported", false, true, "structured_fallback");
+ verify(exchange, never()).createElicitation(any());
+ }
+ }
+
+ @Test
+ void assertCreateToolSpecificationsFallbackForStreamableHttp() {
+ try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
+ String toolName = "database_gateway_plan_encrypt_rule";
+ MCPResponse response = new MCPMapResponse(createClarifyingPayload());
+ MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor(toolName));
+ mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
+ SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("http")).createToolSpecifications().get(0);
+ McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of("field_1", "foo_display")));
+ CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest(toolName, Map.of()));
+ assertStructuredFallback(actual, "remote_identity_required", true, false, "structured_fallback");
+ verify(exchange, never()).createElicitation(any());
+ }
+ }
+
+ private void assertCreateToolSpecificationsSkipUnsafeElicitation(final Map<String, Object> question) {
+ try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
+ String toolName = "database_gateway_plan_encrypt_rule";
+ Map<String, Object> expectedPayload = createClarifyingPayload(question);
+ MCPResponse response = new MCPMapResponse(expectedPayload);
+ MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor(toolName));
+ mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
+ SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
+ McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of("custom_properties.display-name", "foo_display")));
+ CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest(toolName, Map.of()));
+ assertStructuredFallback(actual, "sensitive_form_blocked", true, false, "url_fallback");
+ assertSanitizedSensitiveFallback(actual);
+ verify(exchange, never()).createElicitation(any());
+ }
+ }
+
+ private void assertCreateToolSpecificationsSkipUnsafeElicitationWithPayload(final Map<String, Object> expectedPayload, final String expectedReason,
+ final String expectedInteraction) {
+ try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
+ String toolName = "database_gateway_plan_encrypt_rule";
+ MCPResponse response = new MCPMapResponse(expectedPayload);
+ MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor(toolName));
+ mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
+ SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
+ McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of("field_1", "foo_display")));
+ CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest(toolName, Map.of()));
+ assertStructuredFallback(actual, expectedReason, true, false, expectedInteraction);
+ if ("url_fallback".equals(expectedInteraction)) {
+ assertSanitizedSensitiveFallback(actual);
+ }
+ verify(exchange, never()).createElicitation(any());
+ }
+ }
+
+ @Test
+ void assertCreateToolSpecificationsSkipElicitationForNonPlanningTool() {
+ try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
+ Map<String, Object> expectedPayload = createClarifyingPayload();
+ MCPResponse response = new MCPMapResponse(expectedPayload);
+ MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createToolDescriptor("database_gateway_search_metadata"));
+ mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
+ MCPRuntimeContext runtimeContext = mock(MCPRuntimeContext.class, RETURNS_DEEP_STUBS);
+ when(runtimeContext.getSessionManager().getTransactionResourceManager().getRuntimeDatabases()).thenReturn(Collections.emptyMap());
+ SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(runtimeContext).createToolSpecifications().get(0);
+ McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of()));
+ CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest("database_gateway_search_metadata", Map.of()));
+ assertThat(actual.structuredContent(), is(expectedPayload));
+ verify(exchange, never()).createElicitation(any());
+ }
+ }
+
+ @Test
+ void assertCreateToolSpecificationsSkipElicitationWithoutRuntimeDescriptor() {
+ try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
+ Map<String, Object> expectedPayload = createClarifyingPayload();
+ MCPResponse response = new MCPMapResponse(expectedPayload);
+ MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createToolDescriptor("fixture_ping"));
+ mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
+ MCPRuntimeContext runtimeContext = mock(MCPRuntimeContext.class, RETURNS_DEEP_STUBS);
+ when(runtimeContext.getSessionManager().getTransactionResourceManager().getRuntimeDatabases()).thenReturn(Collections.emptyMap());
+ SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(runtimeContext).createToolSpecifications().get(0);
+ McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of()));
+ CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest("fixture_ping", Map.of()));
+ assertThat(actual.structuredContent(), is(expectedPayload));
+ verify(exchange, never()).createElicitation(any());
+ }
+ }
+
+ @Test
+ void assertCreateToolSpecificationsFallbackWithoutElicitation() {
+ try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
+ MCPResponse response = new MCPMapResponse(createClarifyingPayload());
+ MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor("database_gateway_plan_encrypt_rule"));
+ mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
+ SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
+ McpSyncServerExchange exchange = mock(McpSyncServerExchange.class);
+ when(exchange.sessionId()).thenReturn("session-id");
+ CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest("database_gateway_plan_encrypt_rule", Map.of()));
+ assertStructuredFallback(actual, "client_unsupported", false, false, "structured_fallback");
+ verify(exchange, never()).createElicitation(any());
+ }
+ }
+
+ @Test
+ void assertCreateToolSpecificationsFallbackWhenElicitationDeclined() {
+ assertCreateToolSpecificationsFallbackWhenElicitationAction(McpSchema.ElicitResult.Action.DECLINE);
+ }
+
+ @Test
+ void assertCreateToolSpecificationsFallbackWhenElicitationCancelled() {
+ assertCreateToolSpecificationsFallbackWhenElicitationAction(McpSchema.ElicitResult.Action.CANCEL);
+ }
+
+ @Test
+ void assertCreateToolSpecificationsFallbackWhenElicitedContentHasExtraField() {
+ assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(Map.of("field_1", "foo_display", "field_2", true, "field_3", "unexpected"));
+ }
+
+ @Test
+ void assertCreateToolSpecificationsFallbackWhenElicitedContentMissesField() {
+ assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(Map.of("field_1", "foo_display"));
+ }
+
+ @Test
+ void assertCreateToolSpecificationsFallbackWhenElicitedContentHasBlankRequiredValue() {
+ assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(Map.of("field_1", " ", "field_2", true));
+ }
+
+ @Test
+ void assertCreateToolSpecificationsFallbackWhenElicitedContentTypeMismatches() {
+ assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(Map.of("field_1", 1, "field_2", true));
+ }
+
+ @Test
+ void assertCreateToolSpecificationsFallbackWhenElicitedContentViolatesAllowedValues() {
+ Map<String, Object> expectedPayload = createClarifyingPayload(createClarifyingQuestion(
+ "custom_properties.display-name", "string", false, "Provide display name.", List.of("foo_display")));
+ assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(expectedPayload, Map.of("field_1", "bar_display"));
+ }
+
+ @Test
+ void assertCreateToolSpecificationsFallbackWhenElicitationFails() {
+ try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
+ MCPResponse response = new MCPMapResponse(createClarifyingPayload());
+ MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor("database_gateway_plan_encrypt_rule"));
+ mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
+ SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
+ McpSyncServerExchange exchange = createThrowingElicitationExchange();
+ CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest("database_gateway_plan_encrypt_rule", Map.of()));
+ assertStructuredFallback(actual, "elicitation_failed", true, false, "structured_fallback");
+ verify(exchange).createElicitation(any());
+ }
+ }
+
+ @Test
+ void assertCreateToolSpecificationsFallbackWhenElicitationResultMalformed() {
+ assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid("malformed_elicitation_result", new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, null));
+ }
+
+ @Test
+ void assertCreateToolSpecificationsFallbackWhenElicitationExpires() {
+ try (
+ MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class);
+ MockedStatic<Clock> mockedClock = mockStatic(Clock.class)) {
+ MutableClock clock = new MutableClock();
+ mockedClock.when(Clock::systemUTC).thenReturn(clock);
+ MCPResponse response = new MCPMapResponse(createClarifyingPayload());
+ MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor("database_gateway_plan_encrypt_rule"));
+ mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
+ SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
+ McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of("field_1", "foo_display", "field_2", true)));
+ when(exchange.createElicitation(any())).thenAnswer(invocation -> {
+ clock.advance(Duration.ofMinutes(11L));
+ return new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of("field_1", "foo_display", "field_2", true));
+ });
+ CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest("database_gateway_plan_encrypt_rule", Map.of()));
+ assertStructuredFallback(actual, "stale_elicitation", true, false, "structured_fallback");
+ mockedToolDefinitionRegistry.verify(() -> ToolDefinitionRegistry.dispatch(any(MCPRequestScope.class), eq(toolDefinition), eq("session-id"), eq(createElicitedArguments())), never());
+ }
+ }
+
+ private void assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(final Map<String, Object> elicitedContent) {
+ assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(createClarifyingPayload(), elicitedContent);
+ }
+
+ private void assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(final Map<String, Object> expectedPayload, final Map<String, Object> elicitedContent) {
+ try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
+ MCPResponse response = new MCPMapResponse(expectedPayload);
+ MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor("database_gateway_plan_encrypt_rule"));
+ mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
+ SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
+ McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, elicitedContent));
+ CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest("database_gateway_plan_encrypt_rule", Map.of()));
+ assertStructuredFallback(actual, "invalid_elicited_content", true, false, "structured_fallback");
+ verify(exchange).createElicitation(any());
+ mockedToolDefinitionRegistry.verify(() -> ToolDefinitionRegistry.dispatch(any(MCPRequestScope.class), eq(toolDefinition), eq("session-id"), eq(createElicitedArguments())), never());
+ }
+ }
+
+ private void assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(final String expectedReason, final McpSchema.ElicitResult elicitedResult) {
+ try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
+ MCPResponse response = new MCPMapResponse(createClarifyingPayload());
+ MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor("database_gateway_plan_encrypt_rule"));
+ mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
+ SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
+ McpSyncServerExchange exchange = createElicitationExchange(elicitedResult);
+ CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest("database_gateway_plan_encrypt_rule", Map.of()));
+ assertStructuredFallback(actual, expectedReason, true, false, "structured_fallback");
+ verify(exchange).createElicitation(any());
+ mockedToolDefinitionRegistry.verify(() -> ToolDefinitionRegistry.dispatch(any(MCPRequestScope.class), eq(toolDefinition), eq("session-id"), eq(createElicitedArguments())), never());
+ }
+ }
+
+ private void assertCreateToolSpecificationsFallbackWhenElicitationAction(final McpSchema.ElicitResult.Action action) {
+ try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
+ Map<String, Object> expectedPayload = createClarifyingPayload();
+ MCPResponse response = new MCPMapResponse(expectedPayload);
+ MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor("database_gateway_plan_encrypt_rule"));
+ mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
+ SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
+ McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(action, Map.of()));
+ CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest("database_gateway_plan_encrypt_rule", Map.of()));
+ assertThat(actual.structuredContent(), is(expectedPayload));
+ verify(exchange).createElicitation(any());
+ }
+ }
+}
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 23c6b564312..fd59a5049c3 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
@@ -29,10 +29,8 @@ import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
import io.modelcontextprotocol.spec.McpSchema.ResourceLink;
import io.modelcontextprotocol.spec.McpSchema.TextContent;
import org.apache.shardingsphere.mcp.api.protocol.response.MCPResponse;
-import org.apache.shardingsphere.mcp.api.tool.MCPToolHandler;
import org.apache.shardingsphere.mcp.api.tool.descriptor.MCPToolAnnotations;
import org.apache.shardingsphere.mcp.api.tool.descriptor.MCPToolDescriptor;
-import org.apache.shardingsphere.mcp.core.context.MCPRequestScope;
import org.apache.shardingsphere.mcp.core.context.MCPRuntimeContext;
import org.apache.shardingsphere.mcp.core.protocol.exception.UnsupportedToolException;
import org.apache.shardingsphere.mcp.core.protocol.response.MCPErrorResponse;
@@ -42,16 +40,9 @@ import org.apache.shardingsphere.mcp.support.descriptor.MCPShardingSphereMetadat
import org.apache.shardingsphere.mcp.support.protocol.MCPResourceHintUtils;
import org.apache.shardingsphere.mcp.support.protocol.response.MCPMapResponse;
import org.junit.jupiter.api.Test;
-import org.mockito.ArgumentCaptor;
import org.mockito.MockedStatic;
-import java.time.Clock;
-import java.time.Duration;
-import java.time.Instant;
-import java.time.ZoneId;
import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@@ -63,16 +54,12 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.mockito.ArgumentMatchers.any;
-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.never;
-import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-class MCPToolSpecificationFactoryTest {
+class MCPToolSpecificationFactoryTest extends AbstractMCPToolSpecificationFactoryTest {
@Test
void assertCreateToolSpecifications() {
@@ -311,594 +298,4 @@ class MCPToolSpecificationFactoryTest {
return toolDescriptor.getOutputSchema().containsKey("examples") ? (List<Map<String, Object>>) toolDescriptor.getOutputSchema().get("examples") : List.of();
}
- @Test
- void assertCreateToolSpecificationsHandleInteractiveElicitation() {
- assertInteractiveElicitation(McpSchema.ClientCapabilities.builder().elicitation().build());
- }
-
- @Test
- void assertCreateToolSpecificationsHandleInteractiveElicitationWithFormOnlyClient() {
- assertInteractiveElicitation(createFormOnlyClientCapabilities());
- }
-
- @Test
- void assertCreateToolSpecificationsHandleInteractiveElicitationWithFormAndUrlClient() {
- assertInteractiveElicitation(createFormAndUrlClientCapabilities());
- }
-
- private void assertInteractiveElicitation(final McpSchema.ClientCapabilities clientCapabilities) {
- try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
- String toolName = "database_gateway_plan_encrypt_rule";
- MCPResponse clarifyingResponse = new MCPMapResponse(createClarifyingPayload());
- MCPResponse plannedResponse = new MCPMapResponse(Map.of("status", "planned"));
- MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor(toolName));
- mockedToolDefinitionRegistry.when(() -> ToolDefinitionRegistry.dispatch(any(MCPRequestScope.class), eq(toolDefinition), eq("session-id"), any()))
- .thenReturn(clarifyingResponse,
- plannedResponse);
- SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
- McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT,
- Map.of("field_1", "foo_display", "field_2", true)), clientCapabilities);
- CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest(toolName, Map.of()));
- assertThat(actual.structuredContent(), is(Map.of("status", "planned")));
- ArgumentCaptor<McpSchema.ElicitRequest> requestCaptor = ArgumentCaptor.forClass(McpSchema.ElicitRequest.class);
- verify(exchange).createElicitation(requestCaptor.capture());
- assertThat(requestCaptor.getValue().meta().get(MCPShardingSphereMetadataKeys.TOOL), is(toolName));
- assertThat(requestCaptor.getValue().meta().get(MCPShardingSphereMetadataKeys.PLAN_ID), is("plan-1"));
- assertThat(requestCaptor.getValue().meta().get(MCPShardingSphereMetadataKeys.FORM_REQUEST_ID), isA(String.class));
- assertThat(requestCaptor.getValue().requestedSchema(), is(createExpectedElicitRequestedSchema()));
- mockedToolDefinitionRegistry.verify(() -> ToolDefinitionRegistry.dispatch(any(MCPRequestScope.class), eq(toolDefinition), eq("session-id"), eq(createElicitedArguments())));
- }
- }
-
- @Test
- void assertCreateToolSpecificationsSkipElicitationWithSecretQuestion() {
- assertCreateToolSpecificationsSkipUnsafeElicitation(createClarifyingQuestion("custom_properties.display-name", "string", true, "Provide display name."));
- }
-
- @Test
- void assertCreateToolSpecificationsSkipElicitationWithSecretInputType() {
- assertCreateToolSpecificationsSkipUnsafeElicitation(createClarifyingQuestion("custom_properties.display-name", "secret", false, "Provide display name."));
- }
-
- @Test
- void assertCreateToolSpecificationsSkipElicitationWithSensitiveFieldName() {
- assertCreateToolSpecificationsSkipUnsafeElicitation(createClarifyingQuestion("primary_algorithm_properties.access-token", "string", false, "Provide access token."));
- }
-
- @Test
- void assertCreateToolSpecificationsFallbackWithUrlModeForSensitiveQuestion() {
- try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
- String toolName = "database_gateway_plan_encrypt_rule";
- MCPResponse response = new MCPMapResponse(createClarifyingPayload(
- createClarifyingQuestion("primary_algorithm_properties.access-token", "string", false, "Provide access token.")));
- MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor(toolName));
- mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
- SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
- McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of()), createFormAndUrlClientCapabilities());
- CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest(toolName, Map.of()));
- assertStructuredFallback(actual, "url_mode_not_implemented", true, true, "url_fallback");
- assertSanitizedSensitiveFallback(actual);
- verify(exchange, never()).createElicitation(any());
- }
- }
-
- @Test
- void assertCreateToolSpecificationsSkipElicitationWithoutPlanId() {
- assertCreateToolSpecificationsSkipUnsafeElicitationWithPayload(createClarifyingPayloadWithoutPlanId(), "missing_plan_id", "structured_fallback");
- }
-
- @Test
- void assertCreateToolSpecificationsSkipElicitationWithUnknownAlgorithmSecretFlag() {
- assertCreateToolSpecificationsSkipUnsafeElicitationWithPayload(Map.of(
- "plan_id", "plan-1",
- "status", "clarifying",
- "clarification_questions", List.of(Map.of(
- "field", "primary_algorithm_properties.props",
- "input_type", "string",
- "display_message", "Provide props."))),
- "sensitive_form_blocked", "url_fallback");
- }
-
- @Test
- void assertCreateToolSpecificationsSkipElicitationWithAmbiguousFieldBinding() {
- try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
- String toolName = "database_gateway_plan_encrypt_rule";
- Map<String, Object> expectedPayload = createClarifyingPayload(createClarifyingQuestion("requires_review", "boolean", false, "Require review?"));
- MCPResponse response = new MCPMapResponse(expectedPayload);
- MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createAmbiguousPlanningToolDescriptor(toolName));
- mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
- SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
- McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of("field_1", true)));
- CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest(toolName, Map.of()));
- assertStructuredFallback(actual, "ambiguous_field_binding", true, false, "structured_fallback");
- verify(exchange, never()).createElicitation(any());
- }
- }
-
- @Test
- void assertCreateToolSpecificationsSkipElicitationForUrlOnlyClient() {
- try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
- String toolName = "database_gateway_plan_encrypt_rule";
- Map<String, Object> expectedPayload = createClarifyingPayload();
- MCPResponse response = new MCPMapResponse(expectedPayload);
- MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor(toolName));
- mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
- SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
- McpSyncServerExchange exchange = createUrlOnlyElicitationExchange();
- CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest(toolName, Map.of()));
- assertStructuredFallback(actual, "client_unsupported", false, true, "structured_fallback");
- verify(exchange, never()).createElicitation(any());
- }
- }
-
- @Test
- void assertCreateToolSpecificationsFallbackForStreamableHttp() {
- try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
- String toolName = "database_gateway_plan_encrypt_rule";
- MCPResponse response = new MCPMapResponse(createClarifyingPayload());
- MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor(toolName));
- mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
- SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("http")).createToolSpecifications().get(0);
- McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of("field_1", "foo_display")));
- CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest(toolName, Map.of()));
- assertStructuredFallback(actual, "remote_identity_required", true, false, "structured_fallback");
- verify(exchange, never()).createElicitation(any());
- }
- }
-
- private void assertCreateToolSpecificationsSkipUnsafeElicitation(final Map<String, Object> question) {
- try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
- String toolName = "database_gateway_plan_encrypt_rule";
- Map<String, Object> expectedPayload = createClarifyingPayload(question);
- MCPResponse response = new MCPMapResponse(expectedPayload);
- MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor(toolName));
- mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
- SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
- McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of("custom_properties.display-name", "foo_display")));
- CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest(toolName, Map.of()));
- assertStructuredFallback(actual, "sensitive_form_blocked", true, false, "url_fallback");
- assertSanitizedSensitiveFallback(actual);
- verify(exchange, never()).createElicitation(any());
- }
- }
-
- private void assertCreateToolSpecificationsSkipUnsafeElicitationWithPayload(final Map<String, Object> expectedPayload, final String expectedReason,
- final String expectedInteraction) {
- try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
- String toolName = "database_gateway_plan_encrypt_rule";
- MCPResponse response = new MCPMapResponse(expectedPayload);
- MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor(toolName));
- mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
- SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
- McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of("field_1", "foo_display")));
- CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest(toolName, Map.of()));
- assertStructuredFallback(actual, expectedReason, true, false, expectedInteraction);
- if ("url_fallback".equals(expectedInteraction)) {
- assertSanitizedSensitiveFallback(actual);
- }
- verify(exchange, never()).createElicitation(any());
- }
- }
-
- @Test
- void assertCreateToolSpecificationsSkipElicitationForNonPlanningTool() {
- try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
- Map<String, Object> expectedPayload = createClarifyingPayload();
- MCPResponse response = new MCPMapResponse(expectedPayload);
- MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createToolDescriptor("database_gateway_search_metadata"));
- mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
- MCPRuntimeContext runtimeContext = mock(MCPRuntimeContext.class, RETURNS_DEEP_STUBS);
- when(runtimeContext.getSessionManager().getTransactionResourceManager().getRuntimeDatabases()).thenReturn(Collections.emptyMap());
- SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(runtimeContext).createToolSpecifications().get(0);
- McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of()));
- CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest("database_gateway_search_metadata", Map.of()));
- assertThat(actual.structuredContent(), is(expectedPayload));
- verify(exchange, never()).createElicitation(any());
- }
- }
-
- @Test
- void assertCreateToolSpecificationsSkipElicitationWithoutRuntimeDescriptor() {
- try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
- Map<String, Object> expectedPayload = createClarifyingPayload();
- MCPResponse response = new MCPMapResponse(expectedPayload);
- MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createToolDescriptor("fixture_ping"));
- mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
- MCPRuntimeContext runtimeContext = mock(MCPRuntimeContext.class, RETURNS_DEEP_STUBS);
- when(runtimeContext.getSessionManager().getTransactionResourceManager().getRuntimeDatabases()).thenReturn(Collections.emptyMap());
- SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(runtimeContext).createToolSpecifications().get(0);
- McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of()));
- CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest("fixture_ping", Map.of()));
- assertThat(actual.structuredContent(), is(expectedPayload));
- verify(exchange, never()).createElicitation(any());
- }
- }
-
- @Test
- void assertCreateToolSpecificationsFallbackWithoutElicitation() {
- try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
- MCPResponse response = new MCPMapResponse(createClarifyingPayload());
- MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor("database_gateway_plan_encrypt_rule"));
- mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
- SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
- McpSyncServerExchange exchange = mock(McpSyncServerExchange.class);
- when(exchange.sessionId()).thenReturn("session-id");
- CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest("database_gateway_plan_encrypt_rule", Map.of()));
- assertStructuredFallback(actual, "client_unsupported", false, false, "structured_fallback");
- verify(exchange, never()).createElicitation(any());
- }
- }
-
- @Test
- void assertCreateToolSpecificationsFallbackWhenElicitationDeclined() {
- assertCreateToolSpecificationsFallbackWhenElicitationAction(McpSchema.ElicitResult.Action.DECLINE);
- }
-
- @Test
- void assertCreateToolSpecificationsFallbackWhenElicitationCancelled() {
- assertCreateToolSpecificationsFallbackWhenElicitationAction(McpSchema.ElicitResult.Action.CANCEL);
- }
-
- @Test
- void assertCreateToolSpecificationsFallbackWhenElicitedContentHasExtraField() {
- assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(Map.of("field_1", "foo_display", "field_2", true, "field_3", "unexpected"));
- }
-
- @Test
- void assertCreateToolSpecificationsFallbackWhenElicitedContentMissesField() {
- assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(Map.of("field_1", "foo_display"));
- }
-
- @Test
- void assertCreateToolSpecificationsFallbackWhenElicitedContentHasBlankRequiredValue() {
- assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(Map.of("field_1", " ", "field_2", true));
- }
-
- @Test
- void assertCreateToolSpecificationsFallbackWhenElicitedContentTypeMismatches() {
- assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(Map.of("field_1", 1, "field_2", true));
- }
-
- @Test
- void assertCreateToolSpecificationsFallbackWhenElicitedContentViolatesAllowedValues() {
- Map<String, Object> expectedPayload = createClarifyingPayload(createClarifyingQuestion(
- "custom_properties.display-name", "string", false, "Provide display name.", List.of("foo_display")));
- assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(expectedPayload, Map.of("field_1", "bar_display"));
- }
-
- @Test
- void assertCreateToolSpecificationsFallbackWhenElicitationFails() {
- try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
- MCPResponse response = new MCPMapResponse(createClarifyingPayload());
- MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor("database_gateway_plan_encrypt_rule"));
- mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
- SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
- McpSyncServerExchange exchange = createThrowingElicitationExchange();
- CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest("database_gateway_plan_encrypt_rule", Map.of()));
- assertStructuredFallback(actual, "elicitation_failed", true, false, "structured_fallback");
- verify(exchange).createElicitation(any());
- }
- }
-
- @Test
- void assertCreateToolSpecificationsFallbackWhenElicitationResultMalformed() {
- assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid("malformed_elicitation_result", new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, null));
- }
-
- @Test
- void assertCreateToolSpecificationsFallbackWhenElicitationExpires() {
- try (
- MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class);
- MockedStatic<Clock> mockedClock = mockStatic(Clock.class)) {
- MutableClock clock = new MutableClock();
- mockedClock.when(Clock::systemUTC).thenReturn(clock);
- MCPResponse response = new MCPMapResponse(createClarifyingPayload());
- MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor("database_gateway_plan_encrypt_rule"));
- mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
- SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
- McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of("field_1", "foo_display", "field_2", true)));
- when(exchange.createElicitation(any())).thenAnswer(invocation -> {
- clock.advance(Duration.ofMinutes(11L));
- return new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of("field_1", "foo_display", "field_2", true));
- });
- CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest("database_gateway_plan_encrypt_rule", Map.of()));
- assertStructuredFallback(actual, "stale_elicitation", true, false, "structured_fallback");
- mockedToolDefinitionRegistry.verify(() -> ToolDefinitionRegistry.dispatch(any(MCPRequestScope.class), eq(toolDefinition), eq("session-id"), eq(createElicitedArguments())), never());
- }
- }
-
- private void assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(final Map<String, Object> elicitedContent) {
- assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(createClarifyingPayload(), elicitedContent);
- }
-
- private void assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(final Map<String, Object> expectedPayload, final Map<String, Object> elicitedContent) {
- try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
- MCPResponse response = new MCPMapResponse(expectedPayload);
- MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor("database_gateway_plan_encrypt_rule"));
- mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
- SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
- McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, elicitedContent));
- CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest("database_gateway_plan_encrypt_rule", Map.of()));
- assertStructuredFallback(actual, "invalid_elicited_content", true, false, "structured_fallback");
- verify(exchange).createElicitation(any());
- mockedToolDefinitionRegistry.verify(() -> ToolDefinitionRegistry.dispatch(any(MCPRequestScope.class), eq(toolDefinition), eq("session-id"), eq(createElicitedArguments())), never());
- }
- }
-
- private void assertCreateToolSpecificationsFallbackWhenElicitedContentInvalid(final String expectedReason, final McpSchema.ElicitResult elicitedResult) {
- try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
- MCPResponse response = new MCPMapResponse(createClarifyingPayload());
- MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor("database_gateway_plan_encrypt_rule"));
- mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
- SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
- McpSyncServerExchange exchange = createElicitationExchange(elicitedResult);
- CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest("database_gateway_plan_encrypt_rule", Map.of()));
- assertStructuredFallback(actual, expectedReason, true, false, "structured_fallback");
- verify(exchange).createElicitation(any());
- mockedToolDefinitionRegistry.verify(() -> ToolDefinitionRegistry.dispatch(any(MCPRequestScope.class), eq(toolDefinition), eq("session-id"), eq(createElicitedArguments())), never());
- }
- }
-
- private void assertCreateToolSpecificationsFallbackWhenElicitationAction(final McpSchema.ElicitResult.Action action) {
- try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
- Map<String, Object> expectedPayload = createClarifyingPayload();
- MCPResponse response = new MCPMapResponse(expectedPayload);
- MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createPlanningToolDescriptor("database_gateway_plan_encrypt_rule"));
- mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
- SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(createRuntimeContext("stdio")).createToolSpecifications().get(0);
- McpSyncServerExchange exchange = createElicitationExchange(new McpSchema.ElicitResult(action, Map.of()));
- CallToolResult actual = actualSpecification.callHandler().apply(exchange, new CallToolRequest("database_gateway_plan_encrypt_rule", Map.of()));
- assertThat(actual.structuredContent(), is(expectedPayload));
- verify(exchange).createElicitation(any());
- }
- }
-
- private McpSyncServerExchange createElicitationExchange(final McpSchema.ElicitResult elicitationResult) {
- return createElicitationExchange(elicitationResult, McpSchema.ClientCapabilities.builder().elicitation().build());
- }
-
- private McpSyncServerExchange createElicitationExchange(final McpSchema.ElicitResult elicitationResult, final McpSchema.ClientCapabilities clientCapabilities) {
- McpSyncServerExchange result = mock(McpSyncServerExchange.class);
- when(result.sessionId()).thenReturn("session-id");
- when(result.getClientCapabilities()).thenReturn(clientCapabilities);
- when(result.createElicitation(any())).thenReturn(elicitationResult);
- return result;
- }
-
- private McpSchema.ClientCapabilities createFormOnlyClientCapabilities() {
- return new McpSchema.ClientCapabilities(
- Collections.emptyMap(), null, null,
- new McpSchema.ClientCapabilities.Elicitation(new McpSchema.ClientCapabilities.Elicitation.Form(), null));
- }
-
- private McpSchema.ClientCapabilities createFormAndUrlClientCapabilities() {
- return new McpSchema.ClientCapabilities(
- Collections.emptyMap(), null, null,
- new McpSchema.ClientCapabilities.Elicitation(new McpSchema.ClientCapabilities.Elicitation.Form(), new McpSchema.ClientCapabilities.Elicitation.Url()));
- }
-
- private McpSyncServerExchange createUrlOnlyElicitationExchange() {
- McpSyncServerExchange result = mock(McpSyncServerExchange.class);
- when(result.sessionId()).thenReturn("session-id");
- when(result.getClientCapabilities()).thenReturn(new McpSchema.ClientCapabilities(
- Collections.emptyMap(), null, null,
- new McpSchema.ClientCapabilities.Elicitation(null, new McpSchema.ClientCapabilities.Elicitation.Url())));
- return result;
- }
-
- private McpSyncServerExchange createThrowingElicitationExchange() {
- McpSyncServerExchange result = mock(McpSyncServerExchange.class);
- when(result.sessionId()).thenReturn("session-id");
- when(result.getClientCapabilities()).thenReturn(McpSchema.ClientCapabilities.builder().elicitation().build());
- when(result.createElicitation(any())).thenThrow(new IllegalStateException("unsupported"));
- return result;
- }
-
- private SyncToolSpecification findToolSpecification(final List<SyncToolSpecification> specifications, final String toolName) {
- return specifications.stream().filter(each -> toolName.equals(each.tool().name())).findFirst().orElseThrow();
- }
-
- private MCPToolDefinition mockSupportedTool(final MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry, final MCPToolDescriptor toolDescriptor) {
- MCPToolDefinition result = new MCPToolDefinition(toolDescriptor, mock(MCPToolHandler.class));
- mockedToolDefinitionRegistry.when(ToolDefinitionRegistry::getSupportedToolDescriptors).thenReturn(List.of(toolDescriptor));
- mockedToolDefinitionRegistry.when(() -> ToolDefinitionRegistry.getToolDefinition(toolDescriptor.getName())).thenReturn(result);
- return result;
- }
-
- private void mockToolDispatch(final MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry, final MCPToolDefinition toolDefinition,
- final Map<String, Object> arguments, final MCPResponse response) {
- mockedToolDefinitionRegistry.when(() -> ToolDefinitionRegistry.dispatch(any(MCPRequestScope.class), eq(toolDefinition), eq("session-id"), eq(arguments)))
- .thenReturn(response);
- }
-
- private MCPRuntimeContext createRuntimeContext(final String activeTransport) {
- MCPRuntimeContext result = mock(MCPRuntimeContext.class, RETURNS_DEEP_STUBS);
- when(result.getSessionManager().getTransactionResourceManager().getRuntimeDatabases()).thenReturn(Collections.emptyMap());
- when(result.getActiveTransport()).thenReturn(activeTransport);
- return result;
- }
-
- private void assertStructuredFallback(final CallToolResult actual, final String expectedReason, final boolean expectedFormMode, final boolean expectedUrlMode,
- final String expectedInteraction) {
- Map<String, Object> actualPayload = getStructuredContent(actual);
- assertThat(actualPayload.get("fallback_reason"), is(expectedReason));
- Map<?, ?> actualSupport = (Map<?, ?>) actualPayload.get("elicitation_support");
- assertThat(actualSupport.get("form_mode"), is(expectedFormMode));
- assertThat(actualSupport.get("url_mode"), is(expectedUrlMode));
- assertThat(actualSupport.get("selected_interaction"), is(expectedInteraction));
- assertFalse(actual.isError());
- }
-
- private void assertSanitizedSensitiveFallback(final CallToolResult actual) {
- Map<String, Object> actualPayload = getStructuredContent(actual);
- List<?> actualQuestions = (List<?>) actualPayload.get("clarification_questions");
- assertFalse(actualQuestions.isEmpty());
- Map<?, ?> actualQuestion = (Map<?, ?>) actualQuestions.get(0);
- assertThat(actualQuestion.get("input_type"), is("secret"));
- assertTrue(Boolean.TRUE.equals(actualQuestion.get("secret")));
- assertTrue(actualQuestion.containsKey("message"));
- assertFalse(actualQuestion.containsKey("display_message"));
- List<?> actualNextActions = (List<?>) actualPayload.get("next_actions");
- assertThat(((Map<?, ?>) actualNextActions.get(0)).get("type"), is("terminal"));
- assertFalse(String.valueOf(actualPayload).contains("Provide access token."));
- assertFalse(String.valueOf(actualPayload).contains("Provide display name."));
- assertFalse(String.valueOf(actualPayload).contains("Provide props."));
- }
-
- @SuppressWarnings("unchecked")
- private Map<String, Object> getStructuredContent(final CallToolResult actual) {
- return (Map<String, Object>) actual.structuredContent();
- }
-
- private CallToolResult createCallToolResult(final String toolName, final MCPResponse response) {
- try (MockedStatic<ToolDefinitionRegistry> mockedToolDefinitionRegistry = mockStatic(ToolDefinitionRegistry.class)) {
- MCPToolDefinition toolDefinition = mockSupportedTool(mockedToolDefinitionRegistry, createToolDescriptorWithoutOutputSchema(toolName));
- mockToolDispatch(mockedToolDefinitionRegistry, toolDefinition, Map.of(), response);
- MCPRuntimeContext runtimeContext = mock(MCPRuntimeContext.class, RETURNS_DEEP_STUBS);
- when(runtimeContext.getSessionManager().getTransactionResourceManager().getRuntimeDatabases()).thenReturn(Collections.emptyMap());
- SyncToolSpecification actualSpecification = new MCPToolSpecificationFactory(runtimeContext).createToolSpecifications().get(0);
- McpSyncServerExchange exchange = mock(McpSyncServerExchange.class);
- when(exchange.sessionId()).thenReturn("session-id");
- return actualSpecification.callHandler().apply(exchange, new CallToolRequest(toolName, Map.of()));
- }
- }
-
- private Map<String, Object> createClarifyingPayload() {
- return Map.of(
- "plan_id", "plan-1",
- "status", "clarifying",
- "clarification_questions", List.of(
- createClarifyingQuestion("custom_properties.display-name", "string", false, "Provide display name."),
- createClarifyingQuestion("requires_review", "boolean", false, "Require review?")));
- }
-
- private Map<String, Object> createClarifyingPayload(final Map<String, Object> question) {
- return Map.of(
- "plan_id", "plan-1",
- "status", "clarifying",
- "clarification_questions", List.of(question));
- }
-
- private Map<String, Object> createClarifyingPayloadWithoutPlanId() {
- return Map.of(
- "status", "clarifying",
- "clarification_questions", List.of(createClarifyingQuestion("custom_properties.display-name", "string", false, "Provide display name.")));
- }
-
- private Map<String, Object> createClarifyingQuestion(final String field, final String inputType, final boolean secret, final String displayMessage) {
- return Map.of("field", field, "input_type", inputType, "secret", secret, "display_message", displayMessage);
- }
-
- private Map<String, Object> createClarifyingQuestion(final String field, final String inputType, final boolean secret,
- final String displayMessage, final List<String> allowedValues) {
- return Map.of("field", field, "input_type", inputType, "secret", secret, "display_message", displayMessage, "allowed_values", allowedValues);
- }
-
- private Map<String, Object> createElicitedArguments() {
- return Map.of(
- "plan_id", "plan-1",
- "custom_properties", Map.of("display-name", "foo_display"),
- "intent", Map.of("requires_review", true));
- }
-
- private Map<String, Object> createExpectedElicitRequestedSchema() {
- Map<String, Object> properties = new LinkedHashMap<>(2, 1F);
- properties.put("field_1", Map.of("type", "string", "description", "Provide display name."));
- properties.put("field_2", Map.of("type", "boolean", "description", "Require review?"));
- Map<String, Object> result = new LinkedHashMap<>(4, 1F);
- result.put("type", "object");
- result.put("properties", properties);
- result.put("required", List.of("field_1", "field_2"));
- result.put("additionalProperties", false);
- return result;
- }
-
- private MCPToolDescriptor createToolDescriptor(final String toolName) {
- Map<String, Object> properties = new LinkedHashMap<>(2, 1F);
- properties.put("query", Map.of("type", "string", "description", "Search query."));
- properties.put("object_types", Map.of("type", "array", "description", "Optional object-type filter.",
- "items", Map.of("type", "string", "description", "Object type.", "enum", List.of("TABLE", "VIEW"))));
- return new MCPToolDescriptor(toolName, "Search Metadata", "Search database metadata.", createInputSchema(properties, List.of("query")),
- Map.of("type", "object"), new MCPToolAnnotations("Search Metadata", true, false, true, true),
- Map.of(MCPShardingSphereMetadataKeys.RELATED_RESOURCE_URIS, List.of("shardingsphere://databases")));
- }
-
- private MCPToolDescriptor createToolDescriptor(final String toolName, final MCPToolAnnotations annotations) {
- return new MCPToolDescriptor(toolName, "Fixture Tool", "Run a fixture tool.", createInputSchema(Map.of(), List.of()), Map.of("type", "object"), annotations, Collections.emptyMap());
- }
-
- private MCPToolDescriptor createToolDescriptorWithoutOutputSchema(final String toolName) {
- return new MCPToolDescriptor(toolName, "Fixture Tool", "Run a fixture tool.", createInputSchema(Map.of(), List.of()), Collections.emptyMap(),
- new MCPToolAnnotations("Fixture Tool", true, false, true, true), Collections.emptyMap());
- }
-
- private MCPToolDescriptor createStrictToolDescriptor(final String toolName) {
- return new MCPToolDescriptor(toolName, "Search Metadata", "Search database metadata.", createInputSchema(Map.of(), List.of()),
- Map.of("type", "object", "properties", Map.of("status", Map.of("type", "string")), "required", List.of("status")),
- new MCPToolAnnotations("Search Metadata", true, false, true, true), Collections.emptyMap());
- }
-
- private MCPToolDescriptor createPlanningToolDescriptor(final String toolName) {
- Map<String, Object> properties = new LinkedHashMap<>(2, 1F);
- properties.put("custom_properties", Map.of("type", "object", "description", "Custom properties.", "additionalProperties", true));
- properties.put("intent", Map.of("type", "object", "description", "Intent.", "properties",
- Map.of("requires_review", Map.of("type", "boolean", "description", "Requires review.")), "required", List.of(), "additionalProperties", false));
- return new MCPToolDescriptor(toolName, "Plan Custom Rule", "Plan a custom rule.", createInputSchema(properties, List.of()),
- Map.of("type", "object"), new MCPToolAnnotations("Plan Custom Rule", false, false, true, true), Collections.emptyMap());
- }
-
- private MCPToolDescriptor createAmbiguousPlanningToolDescriptor(final String toolName) {
- Map<String, Object> objectProperty = Map.of(
- "type", "object",
- "description", "Intent.",
- "properties", Map.of("requires_review", Map.of("type", "boolean", "description", "Requires review.")),
- "required", List.of(),
- "additionalProperties", false);
- Map<String, Object> properties = new LinkedHashMap<>(2, 1F);
- properties.put("intent", objectProperty);
- properties.put("review_policy", objectProperty);
- return new MCPToolDescriptor(toolName, "Plan Custom Rule", "Plan a custom rule.", createInputSchema(properties, List.of()),
- Map.of("type", "object"), new MCPToolAnnotations("Plan Custom Rule", false, false, true, true), Collections.emptyMap());
- }
-
- private Map<String, Object> createInputSchema(final Map<String, Object> properties, final List<String> required) {
- Map<String, Object> result = new LinkedHashMap<>(4, 1F);
- result.put("type", "object");
- result.put("properties", properties);
- result.put("required", required);
- result.put("additionalProperties", false);
- return result;
- }
-
- private List<Map<String, Object>> createResourceHints(final String uriPrefix, final String sourceField, final int count) {
- List<Map<String, Object>> result = new LinkedList<>();
- for (int i = 0; i < count; i++) {
- result.add(MCPResourceHintUtils.create(uriPrefix + i, "logical-database", "inspect_detail", "Read resource.", sourceField));
- }
- return result;
- }
-
- private static final class MutableClock extends Clock {
-
- private Instant instant = Instant.parse("2026-05-22T00:00:00Z");
-
- @Override
- public ZoneId getZone() {
- return ZoneId.of("UTC");
- }
-
- @Override
- public Clock withZone(final ZoneId zone) {
- return this;
- }
-
- @Override
- public Instant instant() {
- return instant;
- }
-
- private void advance(final Duration duration) {
- instant = instant.plus(duration);
- }
- }
}
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 31a85266cc6..e01ae8e2a4f 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
@@ -164,7 +164,6 @@ public final class MCPCompletionService {
result.put(MCPShardingSphereMetadataKeys.CANDIDATE_COUNT, candidates.size());
result.put(MCPShardingSphereMetadataKeys.MATCHED_CANDIDATE_COUNT, filteredCandidates.size());
result.put(MCPShardingSphereMetadataKeys.RETURNED_CANDIDATE_COUNT, returnedCandidates.size());
- result.put(MCPShardingSphereMetadataKeys.CONTINUATION_MODE, filteredCandidates.size() > returnedCandidates.size() ? "pagination" : "none");
putInferredContext(result, inferredContextArguments);
result.put(MCPShardingSphereMetadataKeys.MISSING_CONTEXT_ARGUMENTS, missingContextArguments);
String diagnostic = createDiagnostic(candidates, filteredCandidates, missingContextArguments);
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/descriptor/CoreToolDescriptorValidator.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/descriptor/CoreToolDescriptorValidator.java
index c764f0e984c..9989e05b759 100644
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/descriptor/CoreToolDescriptorValidator.java
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/descriptor/CoreToolDescriptorValidator.java
@@ -50,7 +50,7 @@ public final class CoreToolDescriptorValidator implements MCPToolDescriptorValid
public void validate(final MCPToolDescriptor toolDescriptor) {
if (SEARCH_METADATA.equals(toolDescriptor.getName())) {
MCPToolDescriptorValidationUtils.validateRequiredOutputFields(toolDescriptor,
- List.of("response_mode", "items", "count", "has_more", "continuation_mode", "search_context", "total_match_count"));
+ List.of("response_mode", "items", "count", "search_context", "total_match_count"));
validateSearchMetadataOutputItems(toolDescriptor);
return;
}
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 15f4640d4fd..032a33b7b7b 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
@@ -101,7 +101,7 @@ public final class ClassificationResult {
return savepointName.isEmpty() ? Optional.empty() : Optional.of(savepointName);
}
- String getAuditMarker() {
+ 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 bad0550dfe5..06658001a3a 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
@@ -19,19 +19,19 @@ package org.apache.shardingsphere.mcp.core.tool.handler.execute;
import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.mcp.api.protocol.exception.MCPInvalidRequestException;
+import org.apache.shardingsphere.mcp.api.protocol.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.spi.MCPFeatureExecutionFacade;
-import org.apache.shardingsphere.mcp.support.database.capability.SupportedMCPStatement;
import org.apache.shardingsphere.mcp.support.database.capability.MCPDatabaseCapabilityProvider;
+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.StatementClassNotSupportedException;
+import org.apache.shardingsphere.mcp.support.database.spi.MCPFeatureExecutionFacade;
import org.apache.shardingsphere.mcp.support.database.tool.request.SQLExecutionRequest;
import org.apache.shardingsphere.mcp.support.database.tool.response.SQLExecutionResponse;
-import org.apache.shardingsphere.mcp.api.protocol.exception.MCPUnsupportedException;
-import org.apache.shardingsphere.mcp.core.session.MCPSessionManager;
-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.audit.AuditRecorder;
import java.util.Locale;
import java.util.Optional;
@@ -49,15 +49,29 @@ public final class MCPSQLExecutionFacade implements MCPFeatureExecutionFacade {
private final MCPJdbcStatementExecutor statementExecutor;
- private final SQLStatementScanner scanner = new SQLStatementScanner();
+ private final StatementClassifier statementClassifier;
- private final AuditRecorder auditRecorder = new AuditRecorder();
+ private final SQLStatementScanner scanner;
+
+ 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 StatementClassifier(), new SQLStatementScanner(), new SQLExecutionTraceFactory());
+ }
+
+ MCPSQLExecutionFacade(final MCPDatabaseCapabilityProvider databaseCapabilityProvider, final MCPSessionExecutionCoordinator sessionExecutionCoordinator,
+ final MCPJdbcTransactionStatementExecutor transactionStatementExecutor, final MCPJdbcStatementExecutor statementExecutor,
+ final StatementClassifier statementClassifier, final SQLStatementScanner scanner, final SQLExecutionTraceFactory sqlExecutionTraceFactory) {
this.databaseCapabilityProvider = databaseCapabilityProvider;
- sessionExecutionCoordinator = new MCPSessionExecutionCoordinator(sessionManager);
- transactionStatementExecutor = new MCPJdbcTransactionStatementExecutor(sessionManager);
- statementExecutor = new MCPJdbcStatementExecutor(sessionManager.getTransactionResourceManager().getRuntimeDatabases(), sessionManager.getTransactionResourceManager());
+ this.sessionExecutionCoordinator = sessionExecutionCoordinator;
+ this.transactionStatementExecutor = transactionStatementExecutor;
+ this.statementExecutor = statementExecutor;
+ this.statementClassifier = statementClassifier;
+ this.scanner = scanner;
+ this.sqlExecutionTraceFactory = sqlExecutionTraceFactory;
}
/**
@@ -81,40 +95,40 @@ public final class MCPSQLExecutionFacade implements MCPFeatureExecutionFacade {
MCPDatabaseCapability actualDatabaseCapability = databaseCapability.orElseThrow();
ClassificationResult classificationResult;
try {
- classificationResult = new StatementClassifier().classify(executionRequest.getSql());
+ classificationResult = statementClassifier.classify(executionRequest.getSql());
} catch (final MCPUnsupportedException | IllegalArgumentException ex) {
throw recordFailure(executionRequest, SupportedMCPStatement.QUERY.name(), ex);
}
ShardingSpherePreconditions.checkContains(actualDatabaseCapability.getSupportedStatementClasses(), classificationResult.getStatementClass(),
- () -> recordFailure(executionRequest, classificationResult.getAuditMarker(), new StatementClassNotSupportedException()));
+ () -> recordFailure(executionRequest, classificationResult.getTraceStatementMarker(), new StatementClassNotSupportedException()));
checkCrossSchemaSql(executionRequest, actualDatabaseCapability, classificationResult);
try {
switch (classificationResult.getStatementClass()) {
case TRANSACTION_CONTROL:
case SAVEPOINT:
return recordSuccess(executionRequest, transactionStatementExecutor.execute(
- executionRequest.getSessionId(), executionRequest.getDatabase(), actualDatabaseCapability, classificationResult), classificationResult.getAuditMarker());
+ executionRequest.getSessionId(), executionRequest.getDatabase(), actualDatabaseCapability, classificationResult), classificationResult.getTraceStatementMarker());
case QUERY:
case DML:
case DDL:
case DCL:
- return recordSuccess(executionRequest, statementExecutor.execute(executionRequest, classificationResult, actualDatabaseCapability), classificationResult.getAuditMarker());
+ return recordSuccess(executionRequest, statementExecutor.execute(executionRequest, classificationResult, actualDatabaseCapability), classificationResult.getTraceStatementMarker());
case EXPLAIN_ANALYZE:
ShardingSpherePreconditions.checkState(actualDatabaseCapability.isSupportsExplainAnalyze(), () -> new MCPUnsupportedException("EXPLAIN ANALYZE is not supported."));
return recordSuccess(executionRequest, statementExecutor.execute(executionRequest, classificationResult, actualDatabaseCapability),
- classificationResult.getAuditMarker());
+ classificationResult.getTraceStatementMarker());
default:
throw new StatementClassNotSupportedException();
}
// CHECKSTYLE:OFF
} catch (final RuntimeException ex) {
// CHECKSTYLE:ON
- throw recordFailure(executionRequest, classificationResult.getAuditMarker(), ex);
+ throw recordFailure(executionRequest, classificationResult.getTraceStatementMarker(), ex);
}
}
private SQLExecutionResponse recordSuccess(final SQLExecutionRequest executionRequest, final SQLExecutionResponse response, final String statementMarker) {
- auditRecorder.recordQueryExecution(executionRequest.getSessionId(), executionRequest.getDatabase(), executionRequest.getSql(), true, statementMarker);
+ sqlExecutionTraceFactory.create(executionRequest.getSessionId(), executionRequest.getDatabase(), executionRequest.getSql(), true, statementMarker);
return response;
}
@@ -124,7 +138,7 @@ public final class MCPSQLExecutionFacade implements MCPFeatureExecutionFacade {
}
for (String each : classificationResult.getReferencedObjectNames()) {
if (isCrossSchemaReference(each, executionRequest.getDatabase(), classificationResult)) {
- throw recordFailure(executionRequest, classificationResult.getAuditMarker(), new MCPInvalidRequestException(
+ throw recordFailure(executionRequest, classificationResult.getTraceStatementMarker(), new MCPInvalidRequestException(
String.format("Cross-schema SQL is not supported for database `%s`: `%s`.", executionRequest.getDatabase(), each)));
}
}
@@ -166,7 +180,7 @@ public final class MCPSQLExecutionFacade implements MCPFeatureExecutionFacade {
}
private <T extends RuntimeException> T recordFailure(final SQLExecutionRequest executionRequest, final String statementMarker, final T ex) {
- auditRecorder.recordQueryExecution(executionRequest.getSessionId(), executionRequest.getDatabase(), executionRequest.getSql(), false, statementMarker);
+ 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/audit/OperationClass.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/SQLStatementObjectName.java
similarity index 63%
rename from mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/audit/OperationClass.java
rename to mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/SQLStatementObjectName.java
index eac2ad44f43..d206b837438 100644
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/audit/OperationClass.java
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/SQLStatementObjectName.java
@@ -15,12 +15,24 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.mcp.core.tool.handler.execute.audit;
+package org.apache.shardingsphere.mcp.core.tool.handler.execute;
-/**
- * Audit operation classes.
- */
-public enum OperationClass {
+final class SQLStatementObjectName {
+
+ private final String objectName;
+
+ private final int nextIndex;
+
+ SQLStatementObjectName(final String objectName, final int nextIndex) {
+ this.objectName = objectName;
+ this.nextIndex = nextIndex;
+ }
+
+ String objectName() {
+ return objectName;
+ }
- RESOURCE_READ, METADATA_TOOL, QUERY_EXECUTION
+ int nextIndex() {
+ return nextIndex;
+ }
}
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/SQLStatementObjectNameReader.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/SQLStatementObjectNameReader.java
new file mode 100644
index 00000000000..80c7c43b9c3
--- /dev/null
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/SQLStatementObjectNameReader.java
@@ -0,0 +1,88 @@
+/*
+ * 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;
+
+import lombok.AccessLevel;
+import lombok.RequiredArgsConstructor;
+
+import java.util.List;
+
+@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
+final class SQLStatementObjectNameReader {
+
+ private static final List<String> OBJECT_LIST_STOP_KEYWORDS = List.of("JOIN", "WHERE", "ON", "USING", "SET", "VALUES", "RETURNING", "GROUP", "ORDER", "HAVING", "LIMIT", "OFFSET",
+ "FETCH", "UNION", "EXCEPT", "INTERSECT", "WHEN", "FROM", "TO");
+
+ private final SQLStatementScanner scanner;
+
+ SQLStatementObjectName readObjectName(final List<SQLStatementToken> tokens, final int startIndex, final String... optionalKeywords) {
+ int currentIndex = startIndex;
+ while (currentIndex < tokens.size()) {
+ SQLStatementToken token = tokens.get(currentIndex);
+ if (scanner.isKeyword(token, optionalKeywords)) {
+ currentIndex++;
+ continue;
+ }
+ if ("(".equals(token.text())) {
+ return new SQLStatementObjectName("", currentIndex);
+ }
+ return readQualifiedName(tokens, currentIndex);
+ }
+ return new SQLStatementObjectName("", currentIndex);
+ }
+
+ SQLStatementObjectName readQualifiedName(final List<SQLStatementToken> tokens, final int startIndex) {
+ if (startIndex >= tokens.size() || !isObjectNameSegment(tokens.get(startIndex))) {
+ return new SQLStatementObjectName("", startIndex);
+ }
+ StringBuilder result = new StringBuilder(scanner.normalizeIdentifier(tokens.get(startIndex).text()));
+ int currentIndex = startIndex + 1;
+ while (currentIndex + 1 < tokens.size() && ".".equals(tokens.get(currentIndex).text()) && isObjectNameSegment(tokens.get(currentIndex + 1))) {
+ result.append('.').append(scanner.normalizeIdentifier(tokens.get(currentIndex + 1).text()));
+ currentIndex += 2;
+ }
+ return new SQLStatementObjectName(result.toString(), currentIndex);
+ }
+
+ int skipObjectTail(final List<SQLStatementToken> tokens, final int startIndex) {
+ int result = startIndex;
+ int parenthesesDepth = 0;
+ while (result < tokens.size()) {
+ SQLStatementToken token = tokens.get(result);
+ if ("(".equals(token.text())) {
+ parenthesesDepth++;
+ result++;
+ continue;
+ }
+ if (")".equals(token.text())) {
+ parenthesesDepth--;
+ result++;
+ continue;
+ }
+ if (0 == parenthesesDepth && (",".equals(token.text()) || OBJECT_LIST_STOP_KEYWORDS.contains(token.upperText()))) {
+ return result;
+ }
+ result++;
+ }
+ return result;
+ }
+
+ private boolean isObjectNameSegment(final SQLStatementToken token) {
+ return token.identifier() || "*".equals(token.text());
+ }
+}
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/StatementClassifier.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/SQLStatementSafetyValidator.java
similarity index 67%
copy from mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/StatementClassifier.java
copy to mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/SQLStatementSafetyValidator.java
index 2d8269f79c9..5d02ca865ee 100644
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/StatementClassifier.java
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/SQLStatementSafetyValidator.java
@@ -17,18 +17,16 @@
package org.apache.shardingsphere.mcp.core.tool.handler.execute;
-import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
+import lombok.AccessLevel;
+import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mcp.core.protocol.exception.MCPBannedSQLStatementException;
import org.apache.shardingsphere.mcp.core.protocol.exception.MCPLockingReadStatementException;
import org.apache.shardingsphere.mcp.support.database.capability.SupportedMCPStatement;
import java.util.List;
-import java.util.Locale;
-/**
- * Classify one SQL statement into the MCP statement classes.
- */
-public final class StatementClassifier {
+@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
+final class SQLStatementSafetyValidator {
private static final List<String> SIDE_EFFECTING_FUNCTION_NAMES = List.of("NEXTVAL", "SETVAL", "GET_LOCK", "RELEASE_LOCK", "RELEASE_ALL_LOCKS", "PG_ADVISORY_LOCK",
"PG_ADVISORY_XACT_LOCK", "PG_TRY_ADVISORY_LOCK", "PG_TRY_ADVISORY_XACT_LOCK", "PG_ADVISORY_UNLOCK", "PG_ADVISORY_UNLOCK_ALL", "SET_CONFIG",
@@ -38,56 +36,27 @@ public final class StatementClassifier {
private static final List<String> METADATA_LOOKUP_FUNCTION_NAMES = List.of("TO_REGCLASS", "TO_REGTYPE", "TO_REGPROC", "TO_REGPROCEDURE", "TO_REGOPER", "TO_REGOPERATOR",
"TO_REGNAMESPACE", "TO_REGROLE", "OBJECT_ID");
- private final SQLStatementScanner scanner = new SQLStatementScanner();
-
- private final SQLStatementStructureResolver structureResolver = new SQLStatementStructureResolver(scanner);
-
- private final SQLStatementClassResolver statementClassResolver = new SQLStatementClassResolver();
+ private final SQLStatementScanner scanner;
- private final SQLStatementTargetResolver targetResolver = new SQLStatementTargetResolver(scanner, structureResolver);
-
- /**
- * Classify one SQL statement.
- *
- * @param sql SQL text
- * @return classification result
- * @throws MCPBannedSQLStatementException when the SQL is banned by contract
- * @throws MetadataIntrospectionSQLStatementException when the SQL should use MCP metadata resources
- */
- public ClassificationResult classify(final String sql) {
- String actualSql = scanner.normalizeSingleStatement(sql);
- String leadingSql = actualSql.substring(scanner.skipInsignificant(actualSql, 0)).trim();
- String upperLeadingSql = leadingSql.toUpperCase(Locale.ENGLISH);
- if (isBannedCommand(upperLeadingSql, actualSql)) {
+ void checkLeadingStatement(final String upperSql, final String sql) {
+ if (isBannedCommand(upperSql, sql)) {
throw new MCPBannedSQLStatementException();
}
- if (isMetadataIntrospectionStatement(upperLeadingSql)) {
- throw new MetadataIntrospectionSQLStatementException(extractStatementType(upperLeadingSql));
- }
- if (upperLeadingSql.startsWith("EXPLAIN ANALYZE")) {
- String explainedSql = leadingSql.substring("EXPLAIN ANALYZE".length()).trim();
- SQLStatementStructure explainedStatementStructure = structureResolver.resolve(explainedSql);
- SupportedMCPStatement explainedStatementClass = statementClassResolver.resolve(explainedStatementStructure);
- checkSideEffectingSelectInto(explainedStatementStructure);
- checkLockingRead(explainedStatementClass, scanner.tokenize(explainedSql));
- return new ClassificationResult(SupportedMCPStatement.EXPLAIN_ANALYZE, "EXPLAIN ANALYZE", actualSql, targetResolver.resolve(explainedStatementStructure), "",
- explainedStatementClass, targetResolver.resolveAll(explainedStatementStructure));
- }
- if (isSavepointStatement(upperLeadingSql)) {
- String statementType = extractStatementType(upperLeadingSql);
- String savepointName = extractSavepointName(leadingSql);
- validateSavepointName(statementType, savepointName);
- return new ClassificationResult(SupportedMCPStatement.SAVEPOINT, statementType, actualSql, "", savepointName);
+ if (isMetadataIntrospectionStatement(upperSql)) {
+ throw new MetadataIntrospectionSQLStatementException(extractStatementType(upperSql));
}
- if (isTransactionControlStatement(upperLeadingSql)) {
- return new ClassificationResult(SupportedMCPStatement.TRANSACTION_CONTROL, extractStatementType(upperLeadingSql), actualSql, "", "");
- }
- SQLStatementStructure statementStructure = structureResolver.resolve(actualSql);
- SupportedMCPStatement statementClass = statementClassResolver.resolve(statementStructure);
+ }
+
+ void checkStructuredStatement(final SupportedMCPStatement statementClass, final SQLStatementStructure statementStructure) {
checkSideEffectingSelectInto(statementStructure);
- checkLockingRead(statementClass, scanner.tokenize(actualSql));
- return new ClassificationResult(statementClass, statementStructure.statementType(), actualSql, targetResolver.resolve(statementStructure), "", null,
- targetResolver.resolveAll(statementStructure));
+ checkLockingRead(statementClass, scanner.tokenize(statementStructure.mainSql()));
+ for (SQLCommonTableExpression each : statementStructure.commonTableExpressions()) {
+ checkStructuredStatement(resolveCommonTableExpressionStatementClass(each.statementStructure()), each.statementStructure());
+ }
+ }
+
+ private SupportedMCPStatement resolveCommonTableExpressionStatementClass(final SQLStatementStructure statementStructure) {
+ return "SELECT".equals(statementStructure.statementType()) && !statementStructure.containsDataModifyingCommonTableExpression() ? SupportedMCPStatement.QUERY : SupportedMCPStatement.DML;
}
private boolean isBannedCommand(final String upperSql, final String sql) {
@@ -257,20 +226,6 @@ public final class StatementClassifier {
&& scanner.isKeyword(tokens.get(index + 3), "MODE");
}
- private boolean isTransactionControlStatement(final String upperSql) {
- return "BEGIN".equals(upperSql)
- || "START TRANSACTION".equals(upperSql)
- || "COMMIT".equals(upperSql)
- || "ROLLBACK".equals(upperSql);
- }
-
- private boolean isSavepointStatement(final String upperSql) {
- return "SAVEPOINT".equals(upperSql)
- || upperSql.startsWith("SAVEPOINT ")
- || upperSql.startsWith("ROLLBACK TO SAVEPOINT")
- || upperSql.startsWith("RELEASE SAVEPOINT");
- }
-
private String extractStatementType(final String upperSql) {
if (upperSql.startsWith("START TRANSACTION")) {
return "START TRANSACTION";
@@ -283,26 +238,4 @@ public final class StatementClassifier {
}
return upperSql.split("\\s+")[0];
}
-
- private String extractSavepointName(final String sql) {
- String[] tokens = sql.split("\\s+");
- if ("SAVEPOINT".equalsIgnoreCase(tokens[0]) && tokens.length >= 2) {
- return tokens[tokens.length - 1];
- }
- if ("RELEASE".equalsIgnoreCase(tokens[0]) && tokens.length >= 3) {
- return tokens[tokens.length - 1];
- }
- if ("ROLLBACK".equalsIgnoreCase(tokens[0]) && tokens.length >= 4) {
- return tokens[tokens.length - 1];
- }
- return "";
- }
-
- private void validateSavepointName(final String statementType, final String savepointName) {
- if (!savepointName.isEmpty()) {
- return;
- }
- ShardingSpherePreconditions.checkState(!"SAVEPOINT".equals(statementType) && !"ROLLBACK TO SAVEPOINT".equals(statementType) && !"RELEASE SAVEPOINT".equals(statementType),
- () -> new IllegalArgumentException("Savepoint name is required."));
- }
}
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/SQLStatementStructure.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/SQLStatementStructure.java
index 8dd6c3b81f8..08823c97887 100644
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/SQLStatementStructure.java
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/SQLStatementStructure.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.mcp.core.tool.handler.execute;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
+
import java.util.Collection;
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/SQLStatementTargetResolver.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/SQLStatementTargetResolver.java
index ceb9932bfcc..babc0ce4201 100644
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/SQLStatementTargetResolver.java
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/SQLStatementTargetResolver.java
@@ -17,9 +17,6 @@
package org.apache.shardingsphere.mcp.core.tool.handler.execute;
-import lombok.AccessLevel;
-import lombok.RequiredArgsConstructor;
-
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
@@ -28,20 +25,24 @@ import java.util.List;
import java.util.Optional;
import java.util.Set;
-@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
final class SQLStatementTargetResolver {
private static final List<String> SQL_OBJECT_TYPE_KEYWORDS = List.of("TABLE", "VIEW", "INDEX", "SEQUENCE", "DATABASE", "SCHEMA", "TYPE", "FUNCTION", "PROCEDURE", "TRIGGER", "POLICY");
- private static final List<String> OBJECT_LIST_STOP_KEYWORDS = List.of("JOIN", "WHERE", "ON", "USING", "SET", "VALUES", "RETURNING", "GROUP", "ORDER", "HAVING", "LIMIT", "OFFSET",
- "FETCH", "UNION", "EXCEPT", "INTERSECT", "WHEN", "FROM", "TO");
-
private static final List<String> DELETE_MODIFIER_KEYWORDS = List.of("LOW_PRIORITY", "QUICK", "IGNORE");
private final SQLStatementScanner scanner;
private final SQLStatementStructureResolver structureResolver;
+ private final SQLStatementObjectNameReader objectNameReader;
+
+ SQLStatementTargetResolver(final SQLStatementScanner scanner, final SQLStatementStructureResolver structureResolver) {
+ this.scanner = scanner;
+ this.structureResolver = structureResolver;
+ objectNameReader = new SQLStatementObjectNameReader(scanner);
+ }
+
String resolve(final SQLStatementStructure statementStructure) {
Collection<String> objectNames = resolveAll(statementStructure);
return objectNames.isEmpty() ? "" : objectNames.iterator().next();
@@ -288,12 +289,12 @@ final class SQLStatementTargetResolver {
final Collection<String> visitedAliases, final Collection<String> result, final String... optionalKeywords) {
int currentIndex = startIndex;
while (currentIndex < tokens.size()) {
- ObjectNameToken objectName = readObjectName(tokens, currentIndex, optionalKeywords);
+ SQLStatementObjectName objectName = objectNameReader.readObjectName(tokens, currentIndex, optionalKeywords);
if (objectName.objectName().isEmpty()) {
return;
}
collectObjectName(statementStructure, objectName.objectName(), visitedAliases, result);
- currentIndex = skipObjectTail(tokens, objectName.nextIndex());
+ currentIndex = objectNameReader.skipObjectTail(tokens, objectName.nextIndex());
if (currentIndex >= tokens.size() || !",".equals(tokens.get(currentIndex).text())) {
return;
}
@@ -301,66 +302,10 @@ final class SQLStatementTargetResolver {
}
}
- private int skipObjectTail(final List<SQLStatementToken> tokens, final int startIndex) {
- int result = startIndex;
- int parenthesesDepth = 0;
- while (result < tokens.size()) {
- SQLStatementToken token = tokens.get(result);
- if ("(".equals(token.text())) {
- parenthesesDepth++;
- result++;
- continue;
- }
- if (")".equals(token.text())) {
- parenthesesDepth--;
- result++;
- continue;
- }
- if (0 == parenthesesDepth && (",".equals(token.text()) || OBJECT_LIST_STOP_KEYWORDS.contains(token.upperText()))) {
- return result;
- }
- result++;
- }
- return result;
- }
-
- private ObjectNameToken readObjectName(final List<SQLStatementToken> tokens, final int startIndex, final String... optionalKeywords) {
- int currentIndex = startIndex;
- while (currentIndex < tokens.size()) {
- SQLStatementToken token = tokens.get(currentIndex);
- if (scanner.isKeyword(token, optionalKeywords)) {
- currentIndex++;
- continue;
- }
- if ("(".equals(token.text())) {
- return new ObjectNameToken("", currentIndex);
- }
- return readQualifiedName(tokens, currentIndex);
- }
- return new ObjectNameToken("", currentIndex);
- }
-
- private ObjectNameToken readQualifiedName(final List<SQLStatementToken> tokens, final int startIndex) {
- if (startIndex >= tokens.size() || !isObjectNameSegment(tokens.get(startIndex))) {
- return new ObjectNameToken("", startIndex);
- }
- StringBuilder result = new StringBuilder(scanner.normalizeIdentifier(tokens.get(startIndex).text()));
- int currentIndex = startIndex + 1;
- while (currentIndex + 1 < tokens.size() && ".".equals(tokens.get(currentIndex).text()) && isObjectNameSegment(tokens.get(currentIndex + 1))) {
- result.append('.').append(scanner.normalizeIdentifier(tokens.get(currentIndex + 1).text()));
- currentIndex += 2;
- }
- return new ObjectNameToken(result.toString(), currentIndex);
- }
-
- private boolean isObjectNameSegment(final SQLStatementToken token) {
- return token.identifier() || "*".equals(token.text());
- }
-
private void collectQualifiedFunctionNames(final List<SQLStatementToken> tokens, final Collection<String> result) {
int index = 0;
while (index < tokens.size()) {
- ObjectNameToken objectName = readQualifiedName(tokens, index);
+ SQLStatementObjectName objectName = objectNameReader.readQualifiedName(tokens, index);
if (isQualifiedFunctionName(tokens, objectName)) {
addObjectName(result, objectName.objectName());
index = objectName.nextIndex() - 1;
@@ -369,7 +314,7 @@ final class SQLStatementTargetResolver {
}
}
- private boolean isQualifiedFunctionName(final List<SQLStatementToken> tokens, final ObjectNameToken objectName) {
+ private boolean isQualifiedFunctionName(final List<SQLStatementToken> tokens, final SQLStatementObjectName objectName) {
return objectName.objectName().contains(".") && !objectName.objectName().endsWith(".*") && objectName.nextIndex() < tokens.size()
&& "(".equals(tokens.get(objectName.nextIndex()).text());
}
@@ -431,6 +376,4 @@ final class SQLStatementTargetResolver {
return result.toString();
}
- private record ObjectNameToken(String objectName, int nextIndex) {
- }
}
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/StatementClassifier.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/StatementClassifier.java
index 2d8269f79c9..5445c9c57e9 100644
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/StatementClassifier.java
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/StatementClassifier.java
@@ -18,11 +18,8 @@
package org.apache.shardingsphere.mcp.core.tool.handler.execute;
import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
-import org.apache.shardingsphere.mcp.core.protocol.exception.MCPBannedSQLStatementException;
-import org.apache.shardingsphere.mcp.core.protocol.exception.MCPLockingReadStatementException;
import org.apache.shardingsphere.mcp.support.database.capability.SupportedMCPStatement;
-import java.util.List;
import java.util.Locale;
/**
@@ -30,16 +27,10 @@ import java.util.Locale;
*/
public final class StatementClassifier {
- private static final List<String> SIDE_EFFECTING_FUNCTION_NAMES = List.of("NEXTVAL", "SETVAL", "GET_LOCK", "RELEASE_LOCK", "RELEASE_ALL_LOCKS", "PG_ADVISORY_LOCK",
- "PG_ADVISORY_XACT_LOCK", "PG_TRY_ADVISORY_LOCK", "PG_TRY_ADVISORY_XACT_LOCK", "PG_ADVISORY_UNLOCK", "PG_ADVISORY_UNLOCK_ALL", "SET_CONFIG",
- "PG_REPLICATION_SLOT_ADVANCE", "PG_LOGICAL_SLOT_GET_CHANGES", "PG_LOGICAL_SLOT_GET_BINARY_CHANGES", "PG_LOGICAL_EMIT_MESSAGE", "PG_SWITCH_WAL", "PG_RELOAD_CONF",
- "PG_CANCEL_BACKEND", "PG_TERMINATE_BACKEND");
-
- private static final List<String> METADATA_LOOKUP_FUNCTION_NAMES = List.of("TO_REGCLASS", "TO_REGTYPE", "TO_REGPROC", "TO_REGPROCEDURE", "TO_REGOPER", "TO_REGOPERATOR",
- "TO_REGNAMESPACE", "TO_REGROLE", "OBJECT_ID");
-
private final SQLStatementScanner scanner = new SQLStatementScanner();
+ private final SQLStatementSafetyValidator safetyValidator = new SQLStatementSafetyValidator(scanner);
+
private final SQLStatementStructureResolver structureResolver = new SQLStatementStructureResolver(scanner);
private final SQLStatementClassResolver statementClassResolver = new SQLStatementClassResolver();
@@ -51,25 +42,17 @@ public final class StatementClassifier {
*
* @param sql SQL text
* @return classification result
- * @throws MCPBannedSQLStatementException when the SQL is banned by contract
- * @throws MetadataIntrospectionSQLStatementException when the SQL should use MCP metadata resources
*/
public ClassificationResult classify(final String sql) {
String actualSql = scanner.normalizeSingleStatement(sql);
String leadingSql = actualSql.substring(scanner.skipInsignificant(actualSql, 0)).trim();
String upperLeadingSql = leadingSql.toUpperCase(Locale.ENGLISH);
- if (isBannedCommand(upperLeadingSql, actualSql)) {
- throw new MCPBannedSQLStatementException();
- }
- if (isMetadataIntrospectionStatement(upperLeadingSql)) {
- throw new MetadataIntrospectionSQLStatementException(extractStatementType(upperLeadingSql));
- }
+ safetyValidator.checkLeadingStatement(upperLeadingSql, actualSql);
if (upperLeadingSql.startsWith("EXPLAIN ANALYZE")) {
String explainedSql = leadingSql.substring("EXPLAIN ANALYZE".length()).trim();
SQLStatementStructure explainedStatementStructure = structureResolver.resolve(explainedSql);
SupportedMCPStatement explainedStatementClass = statementClassResolver.resolve(explainedStatementStructure);
- checkSideEffectingSelectInto(explainedStatementStructure);
- checkLockingRead(explainedStatementClass, scanner.tokenize(explainedSql));
+ safetyValidator.checkStructuredStatement(explainedStatementClass, explainedStatementStructure);
return new ClassificationResult(SupportedMCPStatement.EXPLAIN_ANALYZE, "EXPLAIN ANALYZE", actualSql, targetResolver.resolve(explainedStatementStructure), "",
explainedStatementClass, targetResolver.resolveAll(explainedStatementStructure));
}
@@ -84,179 +67,11 @@ public final class StatementClassifier {
}
SQLStatementStructure statementStructure = structureResolver.resolve(actualSql);
SupportedMCPStatement statementClass = statementClassResolver.resolve(statementStructure);
- checkSideEffectingSelectInto(statementStructure);
- checkLockingRead(statementClass, scanner.tokenize(actualSql));
+ safetyValidator.checkStructuredStatement(statementClass, statementStructure);
return new ClassificationResult(statementClass, statementStructure.statementType(), actualSql, targetResolver.resolve(statementStructure), "", null,
targetResolver.resolveAll(statementStructure));
}
- private boolean isBannedCommand(final String upperSql, final String sql) {
- return upperSql.startsWith("USE ")
- || upperSql.startsWith("SET ")
- || upperSql.startsWith("COPY ")
- || upperSql.startsWith("LOAD ")
- || upperSql.startsWith("CALL ")
- || scanner.containsMySQLExecutableComment(sql)
- || scanner.containsUserVariableAssignment(sql)
- || containsBannedDialectPattern(scanner.tokenize(sql));
- }
-
- private boolean isMetadataIntrospectionStatement(final String upperSql) {
- return "SHOW".equals(upperSql)
- || upperSql.startsWith("SHOW ")
- || "DESCRIBE".equals(upperSql)
- || upperSql.startsWith("DESCRIBE ")
- || "DESC".equals(upperSql)
- || upperSql.startsWith("DESC ");
- }
-
- private boolean containsBannedDialectPattern(final List<SQLStatementToken> tokens) {
- return containsSelectIntoFile(tokens)
- || containsKeywordSequence(tokens, "ALTER", "SYSTEM")
- || containsUserOrRoleManagement(tokens)
- || containsSideEffectingQueryPattern(tokens)
- || containsMetadataLookupFunction(tokens);
- }
-
- private boolean containsSelectIntoFile(final List<SQLStatementToken> tokens) {
- for (int index = 0; index + 1 < tokens.size(); index++) {
- if (!scanner.isKeyword(tokens.get(index), "INTO")) {
- continue;
- }
- if (scanner.isKeyword(tokens.get(index + 1), "OUTFILE") || scanner.isKeyword(tokens.get(index + 1), "DUMPFILE")) {
- return true;
- }
- }
- return false;
- }
-
- private boolean containsUserOrRoleManagement(final List<SQLStatementToken> tokens) {
- return containsKeywordSequence(tokens, "CREATE", "USER")
- || containsKeywordSequence(tokens, "ALTER", "USER")
- || containsKeywordSequence(tokens, "DROP", "USER")
- || containsKeywordSequence(tokens, "CREATE", "ROLE")
- || containsKeywordSequence(tokens, "ALTER", "ROLE")
- || containsKeywordSequence(tokens, "DROP", "ROLE");
- }
-
- private boolean containsSideEffectingQueryPattern(final List<SQLStatementToken> tokens) {
- return containsKeywordSequence(tokens, "NEXT", "VALUE", "FOR")
- || containsSequenceNextvalPseudocolumn(tokens)
- || containsSideEffectingFunction(tokens)
- || containsLastInsertIdMutation(tokens);
- }
-
- private boolean containsSideEffectingFunction(final List<SQLStatementToken> tokens) {
- for (int index = 0; index + 1 < tokens.size(); index++) {
- if ("(".equals(tokens.get(index + 1).text()) && SIDE_EFFECTING_FUNCTION_NAMES.contains(tokens.get(index).upperText())) {
- return true;
- }
- }
- return false;
- }
-
- private boolean containsMetadataLookupFunction(final List<SQLStatementToken> tokens) {
- for (int index = 0; index + 1 < tokens.size(); index++) {
- if ("(".equals(tokens.get(index + 1).text()) && METADATA_LOOKUP_FUNCTION_NAMES.contains(tokens.get(index).upperText())) {
- return true;
- }
- }
- return false;
- }
-
- private boolean containsLastInsertIdMutation(final List<SQLStatementToken> tokens) {
- for (int index = 0; index + 2 < tokens.size(); index++) {
- if (scanner.isKeyword(tokens.get(index), "LAST_INSERT_ID") && "(".equals(tokens.get(index + 1).text()) && !")".equals(tokens.get(index + 2).text())) {
- return true;
- }
- }
- return false;
- }
-
- private boolean containsSequenceNextvalPseudocolumn(final List<SQLStatementToken> tokens) {
- for (int index = 2; index < tokens.size(); index++) {
- if (scanner.isKeyword(tokens.get(index), "NEXTVAL") && ".".equals(tokens.get(index - 1).text()) && tokens.get(index - 2).identifier()) {
- return true;
- }
- }
- return false;
- }
-
- private boolean containsKeywordSequence(final List<SQLStatementToken> tokens, final String... keywords) {
- for (int index = 0; index + keywords.length <= tokens.size(); index++) {
- if (containsKeywordSequence(tokens, index, keywords)) {
- return true;
- }
- }
- return false;
- }
-
- private boolean containsKeywordSequence(final List<SQLStatementToken> tokens, final int startIndex, final String... keywords) {
- for (int index = 0; index < keywords.length; index++) {
- if (!scanner.isKeyword(tokens.get(startIndex + index), keywords[index])) {
- return false;
- }
- }
- return true;
- }
-
- private void checkLockingRead(final SupportedMCPStatement statementClass, final List<SQLStatementToken> tokens) {
- if (SupportedMCPStatement.QUERY == statementClass && containsLockingReadClause(tokens)) {
- throw new MCPLockingReadStatementException();
- }
- }
-
- private void checkSideEffectingSelectInto(final SQLStatementStructure statementStructure) {
- if ("SELECT".equals(statementStructure.statementType()) && containsTopLevelKeyword(scanner.tokenize(statementStructure.mainSql()), "INTO")) {
- throw new MCPBannedSQLStatementException();
- }
- }
-
- private boolean containsLockingReadClause(final List<SQLStatementToken> tokens) {
- for (int index = 0; index < tokens.size(); index++) {
- if (containsLockingReadForClause(tokens, index) || containsLockInShareModeClause(tokens, index)) {
- return true;
- }
- }
- return false;
- }
-
- private boolean containsTopLevelKeyword(final List<SQLStatementToken> tokens, final String keyword) {
- int parenthesesDepth = 0;
- for (SQLStatementToken each : tokens) {
- if ("(".equals(each.text())) {
- parenthesesDepth++;
- continue;
- }
- if (")".equals(each.text())) {
- parenthesesDepth--;
- continue;
- }
- if (0 == parenthesesDepth && scanner.isKeyword(each, keyword)) {
- return true;
- }
- }
- return false;
- }
-
- private boolean containsLockingReadForClause(final List<SQLStatementToken> tokens, final int index) {
- if (!scanner.isKeyword(tokens.get(index), "FOR") || index + 1 >= tokens.size()) {
- return false;
- }
- return scanner.isKeyword(tokens.get(index + 1), "UPDATE", "SHARE")
- || index + 2 < tokens.size() && scanner.isKeyword(tokens.get(index + 1), "KEY") && scanner.isKeyword(tokens.get(index + 2), "SHARE")
- || index + 3 < tokens.size() && scanner.isKeyword(tokens.get(index + 1), "NO") && scanner.isKeyword(tokens.get(index + 2), "KEY")
- && scanner.isKeyword(tokens.get(index + 3), "UPDATE");
- }
-
- private boolean containsLockInShareModeClause(final List<SQLStatementToken> tokens, final int index) {
- return index + 3 < tokens.size()
- && scanner.isKeyword(tokens.get(index), "LOCK")
- && scanner.isKeyword(tokens.get(index + 1), "IN")
- && scanner.isKeyword(tokens.get(index + 2), "SHARE")
- && scanner.isKeyword(tokens.get(index + 3), "MODE");
- }
-
private boolean isTransactionControlStatement(final String upperSql) {
return "BEGIN".equals(upperSql)
|| "START TRANSACTION".equals(upperSql)
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/audit/AuditRecorder.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceFactory.java
similarity index 71%
rename from mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/audit/AuditRecorder.java
rename to mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceFactory.java
index 6c51a930ab4..5cb7ea749ee 100644
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/audit/AuditRecorder.java
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceFactory.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.mcp.core.tool.handler.execute.audit;
+package org.apache.shardingsphere.mcp.core.tool.handler.execute.trace;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
@@ -23,26 +23,22 @@ import java.security.NoSuchAlgorithmException;
import java.time.Instant;
/**
- * Record MCP audit events for metadata and query activity.
+ * Create SQL execution trace records.
*/
-public final class AuditRecorder {
+public final class SQLExecutionTraceFactory {
/**
- * Record one query-execution audit event.
+ * 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 recorded audit entry
+ * @return SQL execution trace record
*/
- public AuditRecord recordQueryExecution(final String sessionId, final String databaseName, final String sql, final boolean success, final String statementMarker) {
- return record(sessionId, databaseName, sql, success, statementMarker);
- }
-
- private AuditRecord record(final String sessionId, final String databaseName, final String operationSource, final boolean success, final String statementMarker) {
- return new AuditRecord(sessionId, databaseName, OperationClass.QUERY_EXECUTION, digest(operationSource), success, statementMarker, Instant.now().toString());
+ 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) {
diff --git a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/audit/AuditRecord.java b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceRecord.java
similarity index 87%
rename from mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/audit/AuditRecord.java
rename to mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceRecord.java
index ccc6d448abf..52f2687ed30 100644
--- a/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/audit/AuditRecord.java
+++ b/mcp/core/src/main/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceRecord.java
@@ -15,25 +15,23 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.mcp.core.tool.handler.execute.audit;
+package org.apache.shardingsphere.mcp.core.tool.handler.execute.trace;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
- * Audit record projection.
+ * SQL execution trace projection.
*/
@RequiredArgsConstructor
@Getter
-public final class AuditRecord {
+public final class SQLExecutionTraceRecord {
private final String sessionId;
private final String database;
- private final OperationClass operationClass;
-
- private final String operationDigest;
+ private final String sqlDigest;
private final boolean success;
diff --git a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/descriptor/CoreToolDescriptorValidatorTest.java b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/descriptor/CoreToolDescriptorValidatorTest.java
index ea4797ca0d6..cd4075c0707 100644
--- a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/descriptor/CoreToolDescriptorValidatorTest.java
+++ b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/descriptor/CoreToolDescriptorValidatorTest.java
@@ -27,7 +27,6 @@ import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class CoreToolDescriptorValidatorTest {
@@ -40,13 +39,9 @@ class CoreToolDescriptorValidatorTest {
@Test
void assertSearchMetadataDocumentsCompleteSearchResult() {
MCPToolDescriptor descriptor = MCPDescriptorCatalogIndex.getRequiredToolDescriptor("database_gateway_search_metadata");
- assertFalse(getInputProperties(descriptor).containsKey("page_size"));
- assertFalse(getInputProperties(descriptor).containsKey("page_token"));
- assertFalse(getOutputProperties(descriptor).containsKey("next_page_token"));
- assertThat(findOutputProperty(descriptor, "has_more").get("description"),
- is("Whether an application-level continuation mode is active. Metadata search returns false; not MCP list pagination."));
- assertThat(findOutputProperty(descriptor, "continuation_mode").get("description"),
- is("ShardingSphere application continuation mode for this metadata search result; not MCP cursor or nextCursor semantics."));
+ assertTrue(getInputProperties(descriptor).containsKey("query"));
+ assertTrue(getOutputProperties(descriptor).containsKey("search_context"));
+ assertTrue(getOutputProperties(descriptor).containsKey("total_match_count"));
}
@Test
@@ -81,10 +76,6 @@ class CoreToolDescriptorValidatorTest {
assertThat(actual.getMessage(), is("Tool `database_gateway_execute_update` must declare execution_mode."));
}
- private Map<?, ?> findOutputProperty(final MCPToolDescriptor toolDescriptor, final String fieldName) {
- return (Map<?, ?>) getOutputProperties(toolDescriptor).get(fieldName);
- }
-
private Map<?, ?> getInputProperties(final MCPToolDescriptor toolDescriptor) {
return (Map<?, ?>) toolDescriptor.getInputSchema().get("properties");
}
diff --git a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/resource/handler/capability/ServerCapabilitiesHandlerTest.java b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/resource/handler/capability/ServerCapabilitiesHandlerTest.java
index 0cb0b1c5bba..dd6c8006ef0 100644
--- a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/resource/handler/capability/ServerCapabilitiesHandlerTest.java
+++ b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/resource/handler/capability/ServerCapabilitiesHandlerTest.java
@@ -57,7 +57,7 @@ class ServerCapabilitiesHandlerTest {
assertNextActionContract(actual);
assertCommonFlows(actual);
assertSecurityHints(actual);
- assertLegacyPayloadFieldsAbsent(actual);
+ assertRemovedPayloadFieldsAbsent(actual);
assertResourcePayloadContracts(actual);
assertCoreToolSchemas(actual);
}
@@ -199,7 +199,7 @@ class ServerCapabilitiesHandlerTest {
assertTrue(String.valueOf(actualClientSafetyPolicy.get("abuse_guard")).contains("counted before dispatch"));
}
- private void assertLegacyPayloadFieldsAbsent(final Map<String, Object> capabilities) {
+ private void assertRemovedPayloadFieldsAbsent(final Map<String, Object> capabilities) {
String actual = JsonUtils.toJsonString(capabilities);
for (String each : List.of("pending_questions", "parent_uri", "next_resource_uris", "read_resources_first", "empty_reason", "not_found_reason")) {
assertFalse(actual.contains(each));
@@ -254,27 +254,27 @@ class ServerCapabilitiesHandlerTest {
Map<?, ?> inspectMetadataPrompt = findPrompt(capabilities, "inspect_metadata");
Map<?, ?> schemaArgument = findPromptArgument(inspectMetadataPrompt, "schema");
assertThat(((Map<?, ?>) schemaArgument.get("completion")).get("required_context_arguments"), is(List.of("database")));
- assertNoLegacyPublicAliasFields(capabilities);
+ assertNoRemovedPublicAliasFields(capabilities);
}
- private void assertNoLegacyPublicAliasFields(final Object value) {
+ private void assertNoRemovedPublicAliasFields(final Object value) {
if (value instanceof Map) {
- assertNoLegacyPublicAliasFieldMap((Map<?, ?>) value);
+ assertNoRemovedPublicAliasFieldMap((Map<?, ?>) value);
} else if (value instanceof Collection) {
for (Object each : (Collection<?>) value) {
- assertNoLegacyPublicAliasFields(each);
+ assertNoRemovedPublicAliasFields(each);
}
}
}
- private void assertNoLegacyPublicAliasFieldMap(final Map<?, ?> value) {
+ private void assertNoRemovedPublicAliasFieldMap(final Map<?, ?> value) {
assertFalse(value.containsKey("recommended_next_tool"));
assertFalse(value.containsKey("suggested_next_tool"));
assertFalse(value.containsKey("suggested_next_tools"));
assertFalse(value.containsKey("recommended_recovery"));
assertFalse(value.containsKey("suggested_next_action"));
for (Object each : value.values()) {
- assertNoLegacyPublicAliasFields(each);
+ assertNoRemovedPublicAliasFields(each);
}
}
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 d6fcddf78e0..b68526ec8a4 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
@@ -177,22 +177,12 @@ class ToolDefinitionRegistryTest {
}
@Test
- void assertDispatchWithRemovedSearchPageSizeArgument() {
+ void assertDispatchWithUnknownSearchArgument() {
MCPToolArgumentContractViolationException actual = assertThrows(MCPToolArgumentContractViolationException.class,
- () -> dispatch("database_gateway_search_metadata", Map.of("query", "order", "page_size", 10)));
- assertThat(actual.getMessage(), is("page_size is not a supported argument for database_gateway_search_metadata."));
+ () -> dispatch("database_gateway_search_metadata", Map.of("query", "order", "client_hint", "narrow")));
+ assertThat(actual.getMessage(), is("client_hint is not a supported argument for database_gateway_search_metadata."));
assertThat(actual.getCategory(), is("unknown_argument"));
- assertThat(actual.getArgumentPath(), is("page_size"));
- assertThat(actual.getSuggestedArguments(), is(Map.of("query", "order")));
- }
-
- @Test
- void assertDispatchWithRemovedSearchPageTokenArgument() {
- MCPToolArgumentContractViolationException actual = assertThrows(MCPToolArgumentContractViolationException.class,
- () -> dispatch("database_gateway_search_metadata", Map.of("query", "order", "page_token", "offset")));
- assertThat(actual.getMessage(), is("page_token is not a supported argument for database_gateway_search_metadata."));
- assertThat(actual.getCategory(), is("unknown_argument"));
- assertThat(actual.getArgumentPath(), is("page_token"));
+ assertThat(actual.getArgumentPath(), is("client_hint"));
assertThat(actual.getSuggestedArguments(), is(Map.of("query", "order")));
}
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 c7459327bbc..721d9efec94 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
@@ -21,9 +21,8 @@ import org.apache.shardingsphere.mcp.api.protocol.exception.MCPInvalidRequestExc
import org.apache.shardingsphere.mcp.api.protocol.exception.MCPUnsupportedException;
import org.apache.shardingsphere.mcp.core.protocol.exception.MCPBannedSQLStatementException;
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.audit.AuditRecorder;
+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.SupportedMCPStatement;
@@ -35,8 +34,6 @@ 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 org.mockito.MockedConstruction;
-import org.mockito.internal.configuration.plugins.Plugins;
import java.util.Collections;
import java.util.List;
@@ -52,7 +49,6 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
@@ -65,13 +61,13 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- AuditRecorder auditRecorder = mock(AuditRecorder.class);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, auditRecorder);
- SQLExecutionRequest request = createExecutionRequest("SELECT 1");
+ SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
+ final MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory);
+ final 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(auditRecorder).recordQueryExecution("session-1", "logic_db", "SELECT 1", false, "QUERY");
+ verify(traceFactory).create("session-1", "logic_db", "SELECT 1", false, "QUERY");
verifyNoInteractions(capabilityProvider, transactionExecutor, statementExecutor);
}
@@ -81,14 +77,14 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- AuditRecorder auditRecorder = mock(AuditRecorder.class);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, auditRecorder);
- SQLExecutionRequest request = createExecutionRequest("SELECT 1");
+ SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
+ final MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory);
+ final 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(auditRecorder).recordQueryExecution("session-1", "logic_db", "SELECT 1", false, "QUERY");
+ verify(traceFactory).create("session-1", "logic_db", "SELECT 1", false, "QUERY");
verifyNoInteractions(transactionExecutor, statementExecutor);
}
@@ -98,20 +94,17 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- AuditRecorder auditRecorder = mock(AuditRecorder.class);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, auditRecorder);
- SQLExecutionRequest request = createExecutionRequest("SELECT 1");
+ SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
+ final SQLExecutionRequest request = createExecutionRequest("SELECT 1");
+ StatementClassifier statementClassifier = mock(StatementClassifier.class);
+ final MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, statementClassifier);
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.QUERY), false);
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
- try (
- MockedConstruction<StatementClassifier> mocked = mockConstruction(StatementClassifier.class,
- (mock, context) -> when(mock.classify(anyString())).thenThrow(new MCPBannedSQLStatementException()))) {
- MCPBannedSQLStatementException actual = assertThrows(MCPBannedSQLStatementException.class, () -> facade.execute(request));
- assertThat(actual.getMessage(), is("Statement is banned by the MCP contract."));
- assertThat(mocked.constructed().size(), is(1));
- }
- verify(auditRecorder).recordQueryExecution("session-1", "logic_db", "SELECT 1", false, "QUERY");
+ when(statementClassifier.classify(anyString())).thenThrow(new MCPBannedSQLStatementException());
+ 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);
}
@@ -121,19 +114,16 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- AuditRecorder auditRecorder = mock(AuditRecorder.class);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, auditRecorder);
- SQLExecutionRequest request = createExecutionRequest("SELECT 1");
+ SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
+ final SQLExecutionRequest request = createExecutionRequest("SELECT 1");
ClassificationResult classification = new ClassificationResult(SupportedMCPStatement.QUERY, "SELECT", "SELECT 1", "orders", "");
+ final MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, createStatementClassifier(classification));
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.DML), false);
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
- try (MockedConstruction<StatementClassifier> mocked = mockClassification(classification)) {
- StatementClassNotSupportedException actual = assertThrows(StatementClassNotSupportedException.class, () -> facade.execute(request));
- assertThat(actual.getMessage(), is("Statement class is not supported."));
- assertThat(mocked.constructed().size(), is(1));
- }
- verify(auditRecorder).recordQueryExecution("session-1", "logic_db", "SELECT 1", false, "QUERY");
+ 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);
}
@@ -143,22 +133,19 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- AuditRecorder auditRecorder = mock(AuditRecorder.class);
+ SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.TRANSACTION_CONTROL), false);
- SQLExecutionRequest request = createExecutionRequest("BEGIN");
+ final SQLExecutionRequest request = createExecutionRequest("BEGIN");
ClassificationResult classification = new ClassificationResult(SupportedMCPStatement.TRANSACTION_CONTROL, "BEGIN", "BEGIN", "", "");
SQLExecutionResponse response = SQLExecutionResponse.statementAck(SupportedMCPStatement.TRANSACTION_CONTROL, "BEGIN", "Transaction started.");
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, auditRecorder);
+ final MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, createStatementClassifier(classification));
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
when(transactionExecutor.execute("session-1", "logic_db", capability, classification)).thenReturn(response);
- try (MockedConstruction<StatementClassifier> mocked = mockClassification(classification)) {
- SQLExecutionResponse actual = facade.execute(request);
- assertThat(actual, is(response));
- assertThat(mocked.constructed().size(), is(1));
- }
+ SQLExecutionResponse actual = facade.execute(request);
+ assertThat(actual, is(response));
verify(transactionExecutor).execute("session-1", "logic_db", capability, classification);
- verify(auditRecorder).recordQueryExecution("session-1", "logic_db", "BEGIN", true, "BEGIN");
+ verify(traceFactory).create("session-1", "logic_db", "BEGIN", true, "BEGIN");
verifyNoInteractions(statementExecutor);
}
@@ -168,22 +155,19 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- AuditRecorder auditRecorder = mock(AuditRecorder.class);
+ SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.QUERY), false);
- SQLExecutionRequest request = createExecutionRequest("SELECT 1");
+ final SQLExecutionRequest request = createExecutionRequest("SELECT 1");
ClassificationResult classification = new ClassificationResult(SupportedMCPStatement.QUERY, "SELECT", "SELECT 1", "orders", "");
SQLExecutionResponse response = SQLExecutionResponse.resultSet(SupportedMCPStatement.QUERY, "SELECT", Collections.emptyList(), Collections.emptyList(), false);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, auditRecorder);
+ final MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, createStatementClassifier(classification));
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
when(statementExecutor.execute(request, classification, capability)).thenReturn(response);
- try (MockedConstruction<StatementClassifier> mocked = mockClassification(classification)) {
- SQLExecutionResponse actual = facade.execute(request);
- assertThat(actual, is(response));
- assertThat(mocked.constructed().size(), is(1));
- }
+ SQLExecutionResponse actual = facade.execute(request);
+ assertThat(actual, is(response));
verify(statementExecutor).execute(request, classification, capability);
- verify(auditRecorder).recordQueryExecution("session-1", "logic_db", "SELECT 1", true, "QUERY");
+ verify(traceFactory).create("session-1", "logic_db", "SELECT 1", true, "QUERY");
verifyNoInteractions(transactionExecutor);
}
@@ -193,20 +177,17 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- AuditRecorder auditRecorder = mock(AuditRecorder.class);
+ SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.QUERY), false);
- SQLExecutionRequest request = createExecutionRequest("SELECT * FROM other_db.orders");
+ final SQLExecutionRequest request = createExecutionRequest("SELECT * FROM other_db.orders");
ClassificationResult classification = new ClassificationResult(SupportedMCPStatement.QUERY, "SELECT", "SELECT * FROM other_db.orders", "other_db.orders", "", null,
List.of("other_db.orders"));
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, auditRecorder);
+ final MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, createStatementClassifier(classification));
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
- try (MockedConstruction<StatementClassifier> mocked = mockClassification(classification)) {
- 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`."));
- assertThat(mocked.constructed().size(), is(1));
- }
- verify(auditRecorder).recordQueryExecution("session-1", "logic_db", "SELECT * FROM other_db.orders", false, "QUERY");
+ 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);
}
@@ -216,34 +197,34 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- AuditRecorder auditRecorder = mock(AuditRecorder.class);
+ SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.QUERY), false);
- SQLExecutionRequest request = createExecutionRequest("SELECT * FROM logic_db.orders WHERE EXISTS (SELECT 1 FROM other_db.items)");
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, auditRecorder);
+ final SQLExecutionRequest request = createExecutionRequest("SELECT * FROM logic_db.orders WHERE EXISTS (SELECT 1 FROM other_db.items)");
+ final MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory);
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(auditRecorder).recordQueryExecution("session-1", "logic_db", "SELECT * FROM logic_db.orders WHERE EXISTS (SELECT 1 FROM other_db.items)", false, "QUERY");
+ 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 expectedAuditMarker) {
+ void assertExecuteWithCrossSchemaReferencesDisabled(final String name, final String sql, final String expectedObjectName, final String expectedTraceStatementMarker) {
MCPDatabaseCapabilityProvider capabilityProvider = mock(MCPDatabaseCapabilityProvider.class);
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- AuditRecorder auditRecorder = mock(AuditRecorder.class);
+ SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.QUERY, SupportedMCPStatement.DML, SupportedMCPStatement.DDL, SupportedMCPStatement.DCL), false);
- SQLExecutionRequest request = createExecutionRequest(sql);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, auditRecorder);
+ final SQLExecutionRequest request = createExecutionRequest(sql);
+ final MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory);
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(auditRecorder).recordQueryExecution("session-1", "logic_db", sql, false, expectedAuditMarker);
+ verify(traceFactory).create("session-1", "logic_db", sql, false, expectedTraceStatementMarker);
verifyNoInteractions(transactionExecutor, statementExecutor);
}
@@ -253,20 +234,17 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- AuditRecorder auditRecorder = mock(AuditRecorder.class);
+ SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.DML), false);
- SQLExecutionRequest request = createExecutionRequest("UPDATE other_db.orders SET status = 'DONE'");
+ final SQLExecutionRequest request = createExecutionRequest("UPDATE other_db.orders SET status = 'DONE'");
ClassificationResult classification = new ClassificationResult(SupportedMCPStatement.DML, "UPDATE", "UPDATE other_db.orders SET status = 'DONE'", "other_db.orders", "",
null, List.of("other_db.orders"));
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, auditRecorder);
+ final MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, createStatementClassifier(classification));
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
- try (MockedConstruction<StatementClassifier> mocked = mockClassification(classification)) {
- 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`."));
- assertThat(mocked.constructed().size(), is(1));
- }
- verify(auditRecorder).recordQueryExecution("session-1", "logic_db", "UPDATE other_db.orders SET status = 'DONE'", false, "DML");
+ 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);
}
@@ -276,23 +254,20 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- AuditRecorder auditRecorder = mock(AuditRecorder.class);
+ SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.QUERY), false);
- SQLExecutionRequest request = createExecutionRequest("SELECT * FROM logic_db.orders");
+ final SQLExecutionRequest request = createExecutionRequest("SELECT * FROM logic_db.orders");
ClassificationResult classification = new ClassificationResult(SupportedMCPStatement.QUERY, "SELECT", "SELECT * FROM logic_db.orders", "logic_db.orders", "", null,
List.of("logic_db.orders"));
SQLExecutionResponse response = SQLExecutionResponse.resultSet(SupportedMCPStatement.QUERY, "SELECT", Collections.emptyList(), Collections.emptyList(), false);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, auditRecorder);
+ final MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, createStatementClassifier(classification));
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
when(statementExecutor.execute(request, classification, capability)).thenReturn(response);
- try (MockedConstruction<StatementClassifier> mocked = mockClassification(classification)) {
- SQLExecutionResponse actual = facade.execute(request);
- assertThat(actual, is(response));
- assertThat(mocked.constructed().size(), is(1));
- }
+ SQLExecutionResponse actual = facade.execute(request);
+ assertThat(actual, is(response));
verify(statementExecutor).execute(request, classification, capability);
- verify(auditRecorder).recordQueryExecution("session-1", "logic_db", "SELECT * FROM logic_db.orders", true, "QUERY");
+ verify(traceFactory).create("session-1", "logic_db", "SELECT * FROM logic_db.orders", true, "QUERY");
verifyNoInteractions(transactionExecutor);
}
@@ -302,23 +277,20 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- AuditRecorder auditRecorder = mock(AuditRecorder.class);
+ SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.QUERY), false, true);
- SQLExecutionRequest request = createExecutionRequest("SELECT * FROM other_db.orders");
+ final SQLExecutionRequest request = createExecutionRequest("SELECT * FROM other_db.orders");
ClassificationResult classification = new ClassificationResult(SupportedMCPStatement.QUERY, "SELECT", "SELECT * FROM other_db.orders", "other_db.orders", "", null,
List.of("other_db.orders"));
SQLExecutionResponse response = SQLExecutionResponse.resultSet(SupportedMCPStatement.QUERY, "SELECT", Collections.emptyList(), Collections.emptyList(), false);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, auditRecorder);
+ final MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, createStatementClassifier(classification));
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
when(statementExecutor.execute(request, classification, capability)).thenReturn(response);
- try (MockedConstruction<StatementClassifier> mocked = mockClassification(classification)) {
- SQLExecutionResponse actual = facade.execute(request);
- assertThat(actual, is(response));
- assertThat(mocked.constructed().size(), is(1));
- }
+ SQLExecutionResponse actual = facade.execute(request);
+ assertThat(actual, is(response));
verify(statementExecutor).execute(request, classification, capability);
- verify(auditRecorder).recordQueryExecution("session-1", "logic_db", "SELECT * FROM other_db.orders", true, "QUERY");
+ verify(traceFactory).create("session-1", "logic_db", "SELECT * FROM other_db.orders", true, "QUERY");
verifyNoInteractions(transactionExecutor);
}
@@ -328,19 +300,16 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- AuditRecorder auditRecorder = mock(AuditRecorder.class);
- SQLExecutionRequest request = createExecutionRequest("EXPLAIN ANALYZE SELECT 1");
+ SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
+ final SQLExecutionRequest request = createExecutionRequest("EXPLAIN ANALYZE SELECT 1");
ClassificationResult classification = new ClassificationResult(SupportedMCPStatement.EXPLAIN_ANALYZE, "EXPLAIN ANALYZE", "EXPLAIN ANALYZE SELECT 1", "orders", "");
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.EXPLAIN_ANALYZE), false);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, auditRecorder);
+ final MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, createStatementClassifier(classification));
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
- try (MockedConstruction<StatementClassifier> mocked = mockClassification(classification)) {
- MCPUnsupportedException actual = assertThrows(MCPUnsupportedException.class, () -> facade.execute(request));
- assertThat(actual.getMessage(), is("EXPLAIN ANALYZE is not supported."));
- assertThat(mocked.constructed().size(), is(1));
- }
- verify(auditRecorder).recordQueryExecution("session-1", "logic_db", "EXPLAIN ANALYZE SELECT 1", false, "EXPLAIN_ANALYZE");
+ MCPUnsupportedException actual = assertThrows(MCPUnsupportedException.class, () -> facade.execute(request));
+ assertThat(actual.getMessage(), is("EXPLAIN ANALYZE is not supported."));
+ verify(traceFactory).create("session-1", "logic_db", "EXPLAIN ANALYZE SELECT 1", false, "EXPLAIN_ANALYZE");
verifyNoInteractions(transactionExecutor, statementExecutor);
}
@@ -350,20 +319,17 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- AuditRecorder auditRecorder = mock(AuditRecorder.class);
+ SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.QUERY), false);
- SQLExecutionRequest request = createExecutionRequest("SELECT 1");
+ final SQLExecutionRequest request = createExecutionRequest("SELECT 1");
ClassificationResult classification = new ClassificationResult(SupportedMCPStatement.QUERY, "SELECT", "SELECT 1", "orders", "");
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, auditRecorder);
+ final MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, createStatementClassifier(classification));
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
when(statementExecutor.execute(request, classification, capability)).thenThrow(new MCPInvalidRequestException("bad query"));
- try (MockedConstruction<StatementClassifier> mocked = mockClassification(classification)) {
- MCPInvalidRequestException actual = assertThrows(MCPInvalidRequestException.class, () -> facade.execute(request));
- assertThat(actual.getMessage(), is("bad query"));
- assertThat(mocked.constructed().size(), is(1));
- }
- verify(auditRecorder).recordQueryExecution("session-1", "logic_db", "SELECT 1", false, "QUERY");
+ 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);
}
@@ -373,42 +339,32 @@ class MCPSQLExecutionFacadeTest {
MCPSessionExecutionCoordinator coordinator = mock(MCPSessionExecutionCoordinator.class);
MCPJdbcTransactionStatementExecutor transactionExecutor = mock(MCPJdbcTransactionStatementExecutor.class);
MCPJdbcStatementExecutor statementExecutor = mock(MCPJdbcStatementExecutor.class);
- AuditRecorder auditRecorder = mock(AuditRecorder.class);
+ SQLExecutionTraceFactory traceFactory = mock(SQLExecutionTraceFactory.class);
MCPDatabaseCapability capability = createCapability(Set.of(SupportedMCPStatement.EXPLAIN_ANALYZE), true);
- SQLExecutionRequest request = createExecutionRequest("EXPLAIN ANALYZE SELECT 1");
+ final SQLExecutionRequest request = createExecutionRequest("EXPLAIN ANALYZE SELECT 1");
ClassificationResult classification = new ClassificationResult(SupportedMCPStatement.EXPLAIN_ANALYZE, "EXPLAIN ANALYZE", "EXPLAIN ANALYZE SELECT 1", "orders", "");
SQLExecutionResponse response = SQLExecutionResponse.resultSet(SupportedMCPStatement.EXPLAIN_ANALYZE, "EXPLAIN ANALYZE", Collections.emptyList(), Collections.emptyList(), false);
- MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, auditRecorder);
+ final MCPSQLExecutionFacade facade = createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, createStatementClassifier(classification));
mockSessionLock(coordinator);
when(capabilityProvider.provide("logic_db")).thenReturn(Optional.of(capability));
when(statementExecutor.execute(request, classification, capability)).thenReturn(response);
- try (MockedConstruction<StatementClassifier> mocked = mockClassification(classification)) {
- SQLExecutionResponse actual = facade.execute(request);
- assertThat(actual, is(response));
- assertThat(mocked.constructed().size(), is(1));
- }
+ SQLExecutionResponse actual = facade.execute(request);
+ assertThat(actual, is(response));
verify(statementExecutor).execute(request, classification, capability);
- verify(auditRecorder).recordQueryExecution("session-1", "logic_db", "EXPLAIN ANALYZE SELECT 1", true, "EXPLAIN_ANALYZE");
+ verify(traceFactory).create("session-1", "logic_db", "EXPLAIN ANALYZE SELECT 1", true, "EXPLAIN_ANALYZE");
verifyNoInteractions(transactionExecutor);
}
private MCPSQLExecutionFacade createFacade(final MCPDatabaseCapabilityProvider capabilityProvider, final MCPSessionExecutionCoordinator coordinator,
final MCPJdbcTransactionStatementExecutor transactionExecutor, final MCPJdbcStatementExecutor statementExecutor,
- final AuditRecorder auditRecorder) {
- MCPSQLExecutionFacade result = new MCPSQLExecutionFacade(capabilityProvider, new MCPSessionManager(Collections.emptyMap()));
- setField(result, "sessionExecutionCoordinator", coordinator);
- setField(result, "transactionStatementExecutor", transactionExecutor);
- setField(result, "statementExecutor", statementExecutor);
- setField(result, "auditRecorder", auditRecorder);
- return result;
+ final SQLExecutionTraceFactory traceFactory) {
+ return createFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, traceFactory, new StatementClassifier());
}
- private void setField(final Object target, final String fieldName, final Object value) {
- try {
- Plugins.getMemberAccessor().set(MCPSQLExecutionFacade.class.getDeclaredField(fieldName), target, value);
- } catch (final ReflectiveOperationException ex) {
- throw new IllegalStateException(ex);
- }
+ private MCPSQLExecutionFacade createFacade(final MCPDatabaseCapabilityProvider capabilityProvider, final MCPSessionExecutionCoordinator coordinator,
+ final MCPJdbcTransactionStatementExecutor transactionExecutor, final MCPJdbcStatementExecutor statementExecutor,
+ final SQLExecutionTraceFactory traceFactory, final StatementClassifier statementClassifier) {
+ return new MCPSQLExecutionFacade(capabilityProvider, coordinator, transactionExecutor, statementExecutor, statementClassifier, new SQLStatementScanner(), traceFactory);
}
@SuppressWarnings("unchecked")
@@ -428,8 +384,10 @@ class MCPSQLExecutionFacadeTest {
return result;
}
- private MockedConstruction<StatementClassifier> mockClassification(final ClassificationResult classificationResult) {
- return mockConstruction(StatementClassifier.class, (mock, context) -> when(mock.classify(anyString())).thenReturn(classificationResult));
+ private StatementClassifier createStatementClassifier(final ClassificationResult classificationResult) {
+ StatementClassifier result = mock(StatementClassifier.class);
+ when(result.classify(anyString())).thenReturn(classificationResult);
+ return result;
}
private static Stream<Arguments> assertExecuteWithCrossSchemaReferencesDisabledCases() {
diff --git a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/audit/AuditRecorderTest.java b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceFactoryTest.java
similarity index 65%
rename from mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/audit/AuditRecorderTest.java
rename to mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceFactoryTest.java
index a40c752c89a..acfd1686d88 100644
--- a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/audit/AuditRecorderTest.java
+++ b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/execute/trace/SQLExecutionTraceFactoryTest.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.mcp.core.tool.handler.execute.audit;
+package org.apache.shardingsphere.mcp.core.tool.handler.execute.trace;
import org.junit.jupiter.api.Test;
@@ -24,23 +24,19 @@ import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
-class AuditRecorderTest {
+class SQLExecutionTraceFactoryTest {
@Test
- void assertRecordQueryExecution() {
- AuditRecorder auditRecorder = new AuditRecorder();
- AuditRecord actual = auditRecorder.recordQueryExecution("session-1", "logic_db", "SELECT * FROM orders", true, "QUERY");
- assertThat(actual.getOperationClass(), is(OperationClass.QUERY_EXECUTION));
+ 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.getOperationDigest().length(), is(64));
+ assertThat(actual.getSqlDigest().length(), is(64));
}
@Test
- void assertRecordFailedQueryExecution() {
- AuditRecorder auditRecorder = new AuditRecorder();
- AuditRecord actual = auditRecorder.recordQueryExecution("session-1", "logic_db", "SELECT * FROM orders", false, "QUERY");
- assertThat(actual.getOperationClass(), is(OperationClass.QUERY_EXECUTION));
+ 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/handler/metadata/SearchMetadataToolServiceTest.java b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/metadata/SearchMetadataToolServiceTest.java
index 70b6f03374d..0f4ef5ed210 100644
--- a/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/metadata/SearchMetadataToolServiceTest.java
+++ b/mcp/core/src/test/java/org/apache/shardingsphere/mcp/core/tool/handler/metadata/SearchMetadataToolServiceTest.java
@@ -107,8 +107,7 @@ class SearchMetadataToolServiceTest {
assertThat(actualSearchContext.get("database_scope"), is("single_database"));
assertThat(actualSearchContext.get("schema"), is(""));
assertThat(actualSearchContext.get("object_types"), is(List.of("table", "view")));
- assertFalse(actualSearchContext.containsKey("page_size"));
- assertFalse(actualSearchContext.containsKey("page_token"));
+ assertThat(actualSearchContext.keySet(), is(Set.of("query", "database", "database_scope", "schema", "object_types")));
assertThat(actual.getTotalMatchCount(), is(4));
}
@@ -123,7 +122,7 @@ class SearchMetadataToolServiceTest {
assertThat(extractResourceHintUri(actual.getItems().get(2)), is("shardingsphere://databases/runtime_db/schemas/public/sequences/order_seq"));
assertThat(extractParentResourceHintUri(actual.getItems().get(2)), is("shardingsphere://databases/runtime_db/schemas/public/sequences"));
assertThat(actual.getItems().get(2).getMatchKind(), is("all"));
- assertFalse(actual.getSearchContext().containsKey("page_size"));
+ assertThat(actual.getSearchContext().keySet(), is(Set.of("query", "database", "database_scope", "schema", "object_types")));
}
@Test
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 ff50de5f5b5..a7e5580bfee 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
@@ -191,7 +191,7 @@ public final class EncryptWorkflowPlanningService {
snapshot.getIssues().add(new WorkflowIssue(WorkflowIssueCode.ENCRYPT_DROP_SCOPE_LIMITED, "warning", "planning-artifacts",
"Encrypt drop only removes the rule. MCP will not restore historical plaintext data.", "Review business impact before execution.", true, Map.of()));
snapshot.getIssues().add(new WorkflowIssue(WorkflowIssueCode.PHYSICAL_CLEANUP_REQUIRED, "warning", "planning-artifacts",
- "Encrypt drop does not clean up legacy physical derived columns or indexes in V1.",
+ "Encrypt drop does not clean up pre-existing physical derived columns or indexes in V1.",
"Clean up obsolete physical artifacts manually if they are no longer needed.", true, Map.of()));
return;
}
diff --git a/mcp/features/encrypt/src/test/java/org/apache/shardingsphere/mcp/feature/encrypt/tool/service/EncryptRuleDistSQLPlanningServiceTest.java b/mcp/features/encrypt/src/test/java/org/apache/shardingsphere/mcp/feature/encrypt/tool/service/EncryptRuleDistSQLPlanningServiceTest.java
index 39e1f24ffcf..edf9e2ed192 100644
--- a/mcp/features/encrypt/src/test/java/org/apache/shardingsphere/mcp/feature/encrypt/tool/service/EncryptRuleDistSQLPlanningServiceTest.java
+++ b/mcp/features/encrypt/src/test/java/org/apache/shardingsphere/mcp/feature/encrypt/tool/service/EncryptRuleDistSQLPlanningServiceTest.java
@@ -75,7 +75,7 @@ class EncryptRuleDistSQLPlanningServiceTest {
EncryptWorkflowRequest request = createRequest("create", false, false);
request.setColumn("phone;drop");
MCPInvalidRequestException actualException = assertThrows(MCPInvalidRequestException.class, () -> service.planEncryptRule(request, createDerivedColumnPlan(), List.of()));
- assertThat(actualException.getMessage(), is("column `phone;drop` contains unsupported characters. Only unquoted SQL identifiers are supported in V1."));
+ assertThat(actualException.getMessage(), is("column `phone;drop` contains unsupported characters. Workflow and generated SQL planning support standard unquoted identifiers only."));
}
@Test
diff --git a/mcp/features/encrypt/src/test/java/org/apache/shardingsphere/mcp/feature/encrypt/tool/service/PhysicalDDLPlanningServiceTest.java b/mcp/features/encrypt/src/test/java/org/apache/shardingsphere/mcp/feature/encrypt/tool/service/PhysicalDDLPlanningServiceTest.java
index 4450886eb07..b429bb06804 100644
--- a/mcp/features/encrypt/src/test/java/org/apache/shardingsphere/mcp/feature/encrypt/tool/service/PhysicalDDLPlanningServiceTest.java
+++ b/mcp/features/encrypt/src/test/java/org/apache/shardingsphere/mcp/feature/encrypt/tool/service/PhysicalDDLPlanningServiceTest.java
@@ -60,7 +60,7 @@ class PhysicalDDLPlanningServiceTest {
void assertPlanAddColumnArtifactsRejectsUnsafeTable() {
MCPInvalidRequestException actualException = assertThrows(MCPInvalidRequestException.class,
() -> service.planAddColumnArtifacts("orders;drop", createDerivedColumnPlan(false, false), new LinkedHashSet<>(), ""));
- assertThat(actualException.getMessage(), is("table `orders;drop` contains unsupported characters. Only unquoted SQL identifiers are supported in V1."));
+ assertThat(actualException.getMessage(), is("table `orders;drop` contains unsupported characters. Workflow and generated SQL planning support standard unquoted identifiers only."));
}
@Test
@@ -69,7 +69,7 @@ class PhysicalDDLPlanningServiceTest {
derivedColumnPlan.setCipherColumnName("phone cipher");
MCPInvalidRequestException actualException = assertThrows(MCPInvalidRequestException.class,
() -> service.planAddColumnArtifacts("orders", derivedColumnPlan, new LinkedHashSet<>(), ""));
- assertThat(actualException.getMessage(), is("cipher_column `phone cipher` contains unsupported characters. Only unquoted SQL identifiers are supported in V1."));
+ assertThat(actualException.getMessage(), is("cipher_column `phone cipher` contains unsupported characters. Workflow and generated SQL planning support standard unquoted identifiers only."));
}
private DerivedColumnPlan createDerivedColumnPlan(final boolean assistedQuery, final boolean likeQuery) {
diff --git a/mcp/features/mask/src/test/java/org/apache/shardingsphere/mcp/feature/mask/tool/service/MaskRuleDistSQLPlanningServiceTest.java b/mcp/features/mask/src/test/java/org/apache/shardingsphere/mcp/feature/mask/tool/service/MaskRuleDistSQLPlanningServiceTest.java
index 55a75671dd2..7ecaed5639a 100644
--- a/mcp/features/mask/src/test/java/org/apache/shardingsphere/mcp/feature/mask/tool/service/MaskRuleDistSQLPlanningServiceTest.java
+++ b/mcp/features/mask/src/test/java/org/apache/shardingsphere/mcp/feature/mask/tool/service/MaskRuleDistSQLPlanningServiceTest.java
@@ -67,7 +67,7 @@ class MaskRuleDistSQLPlanningServiceTest {
void assertPlanMaskRuleRejectsUnsafeExistingColumn() {
MCPInvalidRequestException actualException = assertThrows(MCPInvalidRequestException.class,
() -> service.planMaskRule(createRequest("alter"), List.of(Map.of("column", "bad column", "algorithm_type", "MD5"))));
- assertThat(actualException.getMessage(), is("column `bad column` contains unsupported characters. Only unquoted SQL identifiers are supported in V1."));
+ assertThat(actualException.getMessage(), is("column `bad column` contains unsupported characters. Workflow and generated SQL planning support standard unquoted identifiers only."));
}
@Test
diff --git a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPShardingSphereMetadataKeys.java b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPShardingSphereMetadataKeys.java
index 355f0576f52..187deacb428 100644
--- a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPShardingSphereMetadataKeys.java
+++ b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/descriptor/MCPShardingSphereMetadataKeys.java
@@ -93,8 +93,6 @@ public final class MCPShardingSphereMetadataKeys {
public static final String RETURNED_CANDIDATE_COUNT = PREFIX + "returned-candidate-count";
- public static final String CONTINUATION_MODE = PREFIX + "continuation-mode";
-
public static final String MISSING_CONTEXT_ARGUMENTS = PREFIX + "missing-context-arguments";
public static final String DIAGNOSTIC = PREFIX + "diagnostic";
diff --git a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowPlanningSupport.java b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowPlanningSupport.java
index c7279351d2c..0501a965d55 100644
--- a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowPlanningSupport.java
+++ b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowPlanningSupport.java
@@ -229,7 +229,7 @@ public final class WorkflowPlanningSupport {
}
snapshot.getIssues().add(new WorkflowIssue(WorkflowIssueCode.UNSUPPORTED_IDENTIFIER, "error", "discovering",
String.format("%s identifier `%s` contains unsupported characters.", fieldName, identifier),
- "Use unquoted SQL identifiers only in V1.", false, Map.of("field", fieldName, "identifier", identifier)));
+ "Use standard unquoted logical identifiers for workflow planning.", false, Map.of("field", fieldName, "identifier", identifier)));
return false;
}
diff --git a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowSQLUtils.java b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowSQLUtils.java
index 07b205a75ac..f0963a7f01b 100644
--- a/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowSQLUtils.java
+++ b/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowSQLUtils.java
@@ -60,7 +60,8 @@ public final class WorkflowSQLUtils {
public static void checkSafeIdentifier(final String fieldName, final String identifier) {
String actualIdentifier = trimToEmpty(identifier);
ShardingSpherePreconditions.checkState(actualIdentifier.isEmpty() || isSafeIdentifier(actualIdentifier),
- () -> new MCPInvalidRequestException(String.format("%s `%s` contains unsupported characters. Only unquoted SQL identifiers are supported in V1.", fieldName, actualIdentifier)));
+ () -> new MCPInvalidRequestException(String.format(
+ "%s `%s` contains unsupported characters. Workflow and generated SQL planning support standard unquoted identifiers only.", fieldName, actualIdentifier)));
}
/**
diff --git a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorCatalogValidatorTest.java b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorCatalogValidatorTest.java
index 261b98125b7..5e3e188e2e0 100644
--- a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorCatalogValidatorTest.java
+++ b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/descriptor/MCPDescriptorCatalogValidatorTest.java
@@ -50,7 +50,7 @@ class MCPDescriptorCatalogValidatorTest {
void assertValidateRejectsPublicAliasOutputField() {
assertValidationError(createCatalog(List.of(), List.of(createToolDescriptor(
"database_gateway_test_tool", new MCPToolAnnotations("Test Tool", true, false, true, true),
- createOutputSchema(Map.of("recommended_next_tool", Map.of("type", "string", "description", "Legacy alias.")))))),
+ createOutputSchema(Map.of("recommended_next_tool", Map.of("type", "string", "description", "Removed alias.")))))),
"Tool `database_gateway_test_tool` outputSchema must use canonical fields instead of banned `recommended_next_tool`.");
}
diff --git a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/protocol/MCPNextActionUtilsTest.java b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/protocol/MCPNextActionUtilsTest.java
index d49017be8ca..d38a00af5f1 100644
--- a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/protocol/MCPNextActionUtilsTest.java
+++ b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/protocol/MCPNextActionUtilsTest.java
@@ -38,7 +38,7 @@ class MCPNextActionUtilsTest {
assertThat(actual.get("resource_uri"), is("shardingsphere://capabilities"));
assertThat(actual.get("reason"), is("Read capabilities."));
assertFalse(actual.containsKey("requires_user_approval"));
- assertNoLegacyAliases(actual);
+ assertNoRemovedAliases(actual);
}
@Test
@@ -50,7 +50,7 @@ class MCPNextActionUtilsTest {
assertThat(actual.get("tool_name"), is("database_gateway_search_metadata"));
assertThat(actual.get("arguments"), is(Map.of("query", "orders")));
assertFalse(actual.containsKey("requires_user_approval"));
- assertNoLegacyAliases(actual);
+ assertNoRemovedAliases(actual);
}
@Test
@@ -61,7 +61,7 @@ class MCPNextActionUtilsTest {
assertThat(actual.get("tool_name"), is("database_gateway_execute_update"));
assertThat(actual.get("arguments"), is(Map.of("execution_mode", "preview")));
assertFalse(actual.containsKey("requires_user_approval"));
- assertNoLegacyAliases(actual);
+ assertNoRemovedAliases(actual);
}
@Test
@@ -85,7 +85,7 @@ class MCPNextActionUtilsTest {
assertThat(actual.get("resume_target"), is("database_gateway_search_metadata"));
assertThat(actual.get("resume_arguments"), is(Map.of("query", "orders")));
assertFalse(actual.containsKey("requires_user_approval"));
- assertNoLegacyAliases(actual);
+ assertNoRemovedAliases(actual);
}
@Test
@@ -104,7 +104,7 @@ class MCPNextActionUtilsTest {
assertFalse(actual.containsKey("resume_target_type"));
assertFalse(actual.containsKey("resume_target"));
assertFalse(actual.containsKey("resume_arguments"));
- assertNoLegacyAliases(actual);
+ assertNoRemovedAliases(actual);
}
@Test
@@ -115,7 +115,7 @@ class MCPNextActionUtilsTest {
assertThat(actual.get("question"), is("Choose execution mode."));
assertThat(actual.get("required_inputs"), is(List.of("execution_mode")));
assertFalse(actual.containsKey("requires_user_approval"));
- assertNoLegacyAliases(actual);
+ assertNoRemovedAliases(actual);
}
@Test
@@ -125,7 +125,7 @@ class MCPNextActionUtilsTest {
assertThat(actual.get("title"), is("Stop"));
assertThat(actual.get("reason"), is("Done."));
assertFalse(actual.containsKey("requires_user_approval"));
- assertNoLegacyAliases(actual);
+ assertNoRemovedAliases(actual);
}
@Test
@@ -145,7 +145,7 @@ class MCPNextActionUtilsTest {
assertFalse(action.containsKey("depends_on"));
}
- private void assertNoLegacyAliases(final Map<String, Object> action) {
+ private void assertNoRemovedAliases(final Map<String, Object> action) {
assertFalse(action.containsKey("target_tool"));
assertFalse(action.containsKey("target_resource"));
assertFalse(action.containsKey("required_arguments"));
diff --git a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowSQLUtilsTest.java b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowSQLUtilsTest.java
index e436d10fc2a..74d33c253ef 100644
--- a/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowSQLUtilsTest.java
+++ b/mcp/support/src/test/java/org/apache/shardingsphere/mcp/support/workflow/service/WorkflowSQLUtilsTest.java
@@ -44,7 +44,7 @@ class WorkflowSQLUtilsTest {
@Test
void assertCheckSafeIdentifierRejectsUnsafeIdentifier() {
Exception actualException = assertThrows(RuntimeException.class, () -> WorkflowSQLUtils.checkSafeIdentifier("table", "bad table"));
- assertThat(actualException.getMessage(), is("table `bad table` contains unsupported characters. Only unquoted SQL identifiers are supported in V1."));
+ assertThat(actualException.getMessage(), is("table `bad table` contains unsupported characters. Workflow and generated SQL planning support standard unquoted identifiers only."));
}
@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
new file mode 100644
index 00000000000..a3641184c59
--- /dev/null
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/AbstractLLMMCPConversationRunnerTest.java
@@ -0,0 +1,178 @@
+/*
+ * 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.test.e2e.mcp.llm.conversation;
+
+import org.apache.shardingsphere.infra.util.json.JsonUtils;
+import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.client.LLMChatCompletion;
+import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.client.LLMChatMessage;
+import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.client.LLMChatModelClient;
+import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.client.LLMToolCall;
+import org.apache.shardingsphere.test.e2e.mcp.llm.scenario.LLME2EScenario;
+import org.apache.shardingsphere.test.e2e.mcp.llm.scenario.LLMStructuredAnswer;
+import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPInteractionClient;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+@ExtendWith(MockitoExtension.class)
+abstract class AbstractLLMMCPConversationRunnerTest {
+
+ protected static final String DATABASE_NAME = "logic_db";
+
+ protected static final String SCHEMA_NAME = "public";
+
+ protected static final String TABLE_NAME = "orders";
+
+ protected static final String QUERY = "SELECT COUNT(*) AS total_orders FROM orders";
+
+ protected static final String RESOURCE_URI = "shardingsphere://capabilities";
+
+ protected static final String PROMPT_NAME = "inspect_metadata";
+
+ @Mock
+ private LLMChatModelClient llmChatClient;
+
+ @Mock
+ private MCPInteractionClient mcpInteractionClient;
+
+ protected LLMChatModelClient getLLMChatClient() {
+ return llmChatClient;
+ }
+
+ protected MCPInteractionClient getMCPInteractionClient() {
+ return mcpInteractionClient;
+ }
+
+ protected LLMMCPConversationRunner createRunner(final int maxTurns) {
+ return new LLMMCPConversationRunner(maxTurns, llmChatClient, mcpInteractionClient);
+ }
+
+ protected LLME2EScenario createScenario(final List<String> toolNames) {
+ return createScenario(toolNames, "user-prompt");
+ }
+
+ protected LLME2EScenario createScenario(final List<String> toolNames, final String userPrompt) {
+ LLMStructuredAnswer expectedAnswer = new LLMStructuredAnswer(DATABASE_NAME, SCHEMA_NAME, TABLE_NAME, QUERY, 2, toolNames);
+ return new LLME2EScenario("scenario-id", "system-prompt", userPrompt, expectedAnswer, toolNames, toolNames);
+ }
+
+ protected Map<String, Object> createExecuteQueryArguments(final String sql) {
+ return Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "sql", sql, "max_rows", 10);
+ }
+
+ protected LLMChatCompletion createToolCallCompletion(final String toolCallId, final String toolName, final Map<String, Object> arguments, final String rawResponse) {
+ return new LLMChatCompletion("", List.of(new LLMToolCall(toolCallId, toolName, JsonUtils.toJsonString(arguments))), rawResponse);
+ }
+
+ protected LLMChatCompletion createFinalAnswerCompletion(final List<String> interactionSequence, final Object totalOrders, final String rawResponse) {
+ return new LLMChatCompletion(JsonUtils.toJsonString(createFinalAnswerPayload(interactionSequence, totalOrders)), List.of(), rawResponse);
+ }
+
+ protected boolean containsMessage(final List<LLMChatMessage> messages, final String expectedContent) {
+ return messages.stream().map(LLMChatMessage::getContent).anyMatch(each -> each.contains(expectedContent));
+ }
+
+ protected Map<String, Object> createResultSetPayload(final Object totalOrders) {
+ return Map.of("result_kind", "result_set", "rows", List.of(List.of(totalOrders)));
+ }
+
+ protected Map<String, Object> createFinalAnswerPayload(final List<String> interactionSequence, final Object totalOrders) {
+ Map<String, Object> result = new LinkedHashMap<>(6, 1F);
+ result.put("database", DATABASE_NAME);
+ result.put("schema", SCHEMA_NAME);
+ result.put("table", TABLE_NAME);
+ result.put("query", QUERY);
+ result.put("totalOrders", totalOrders);
+ result.put("interactionSequence", interactionSequence);
+ return result;
+ }
+
+ protected static Map<String, Object> createMutatedFinalAnswerPayload(final String fieldName, final Object fieldValue) {
+ Map<String, Object> result = new LinkedHashMap<>(6, 1F);
+ result.put("database", DATABASE_NAME);
+ result.put("schema", SCHEMA_NAME);
+ result.put("table", TABLE_NAME);
+ result.put("query", QUERY);
+ result.put("totalOrders", 2);
+ result.put("interactionSequence", List.of("database_gateway_execute_query"));
+ result.put(fieldName, fieldValue);
+ return result;
+ }
+
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ protected ArgumentCaptor<List<Map<String, Object>>> createToolDefinitionsCaptor() {
+ return ArgumentCaptor.forClass((Class) List.class);
+ }
+
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ protected ArgumentCaptor<List<LLMChatMessage>> createChatMessagesCaptor() {
+ return ArgumentCaptor.forClass((Class) List.class);
+ }
+
+ @SuppressWarnings("unchecked")
+ protected Map<String, Object> getFunction(final Map<String, Object> toolDefinition) {
+ return (Map<String, Object>) toolDefinition.get("function");
+ }
+
+ protected String getToolName(final Map<String, Object> toolDefinition) {
+ return String.valueOf(getFunction(toolDefinition).get("name"));
+ }
+
+ protected List<String> getToolNames(final List<Map<String, Object>> toolDefinitions) {
+ return toolDefinitions.stream().map(this::getToolName).toList();
+ }
+
+ protected List<String> getRequiredFields(final Map<String, Object> toolDefinition) {
+ return castToStringList(getMap(getFunction(toolDefinition).get("parameters")).get("required"));
+ }
+
+ protected String getPropertyType(final Map<String, Object> propertyDefinition) {
+ return String.valueOf(propertyDefinition.get("type"));
+ }
+
+ protected String getPropertyType(final Map<String, Object> toolDefinition, final String propertyName) {
+ return getPropertyType(getPropertyDefinition(toolDefinition, propertyName));
+ }
+
+ protected String getNestedPropertyType(final Map<String, Object> toolDefinition, final String propertyName, final String nestedPropertyName) {
+ return getPropertyType(getMap(getPropertyDefinition(toolDefinition, propertyName).get(nestedPropertyName)));
+ }
+
+ protected Map<String, Object> getPropertyDefinition(final Map<String, Object> toolDefinition, final String propertyName) {
+ return getMap(getProperty(toolDefinition, propertyName));
+ }
+
+ protected Object getProperty(final Map<String, Object> toolDefinition, final String propertyName) {
+ return getMap(getMap(getFunction(toolDefinition).get("parameters")).get("properties")).get(propertyName);
+ }
+
+ @SuppressWarnings("unchecked")
+ protected Map<String, Object> getMap(final Object value) {
+ return (Map<String, Object>) value;
+ }
+
+ @SuppressWarnings("unchecked")
+ protected List<String> castToStringList(final Object value) {
+ return (List<String>) value;
+ }
+}
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
new file mode 100644
index 00000000000..e6061d10f99
--- /dev/null
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerCoverageTest.java
@@ -0,0 +1,259 @@
+/*
+ * 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.test.e2e.mcp.llm.conversation;
+
+import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.artifact.LLME2EArtifactBundle;
+import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.client.LLMChatCompletion;
+import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.client.LLMChatMessage;
+import org.apache.shardingsphere.test.e2e.mcp.llm.scenario.LLME2EScenario;
+import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionActionNames;
+import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionTraceRecord;
+import org.hamcrest.CoreMatchers;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+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.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class LLMMCPConversationRunnerCoverageTest extends AbstractLLMMCPConversationRunnerTest {
+
+ @Test
+ void assertRunWithNonNumericExecuteQueryTrace() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(2);
+ final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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(
+ createFinalAnswerCompletion(List.of("database_gateway_execute_query"), 2, "final-answer-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("unexpected_query_result"));
+ }
+
+ @Test
+ void assertRunWithNonResultSetExecuteQueryTrace() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(2);
+ final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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(
+ createFinalAnswerCompletion(List.of("database_gateway_execute_query"), 2, "final-answer-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("unexpected_query_result"));
+ }
+
+ @Test
+ void assertRunWithMissingRequiredToolCoverage() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(1);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
+ assertThat(actual.getInteractionTrace().size(), CoreMatchers.is(0));
+ verify(getMCPInteractionClient()).open();
+ verify(getMCPInteractionClient()).close();
+ }
+
+ @Test
+ void assertRunNamesRemainingToolsWhenCoverageIsMissing() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(2);
+ final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response-1"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
+ verify(getLLMChatClient(), times(2)).complete(actualMessages.capture(), anyList(), eq("required"), eq(false));
+ final List<LLMChatMessage> actualSecondTurnMessages = actualMessages.getAllValues().get(1);
+ assertThat(actualSecondTurnMessages.get(actualSecondTurnMessages.size() - 1).getContent(),
+ CoreMatchers.containsString("Remaining required MCP tools: database_gateway_execute_query"));
+ assertThat(actualSecondTurnMessages.get(actualSecondTurnMessages.size() - 1).getContent(), CoreMatchers.containsString("actual MCP tool_call"));
+ assertThat(actualSecondTurnMessages.get(actualSecondTurnMessages.size() - 1).getContent(), CoreMatchers.containsString("database `logic_db`"));
+ assertThat(actualSecondTurnMessages.get(actualSecondTurnMessages.size() - 1).getContent(), CoreMatchers.containsString("schema `public`"));
+ assertThat(actualSecondTurnMessages.get(actualSecondTurnMessages.size() - 1).getContent(), CoreMatchers.containsString("sql `SELECT COUNT(*) AS total_orders FROM orders`"));
+ }
+
+ @Test
+ void assertRunNamesExactResourceUriWhenResourceCoverageIsMissing() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of(MCPInteractionActionNames.READ_RESOURCE));
+ final LLMMCPConversationRunner actualRunner = createRunner(2);
+ final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response-1"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
+ verify(getLLMChatClient(), times(2)).complete(actualMessages.capture(), anyList(), eq("required"), eq(false));
+ final List<LLMChatMessage> actualSecondTurnMessages = actualMessages.getAllValues().get(1);
+ final String actualRetryInstruction = actualSecondTurnMessages.get(actualSecondTurnMessages.size() - 1).getContent();
+ assertThat(actualRetryInstruction, CoreMatchers.containsString("Remaining required MCP tools: mcp_read_resource"));
+ assertThat(actualRetryInstruction, CoreMatchers.containsString("exact shardingsphere:// URI"));
+ assertThat(actualRetryInstruction, CoreMatchers.containsString("do not invent abbreviated URI strings"));
+ }
+
+ @Test
+ void assertRunNamesPreviewModeWhenUpdateCoverageIsMissing() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_update"));
+ final LLMMCPConversationRunner actualRunner = createRunner(2);
+ final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response-1"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
+ verify(getLLMChatClient(), times(2)).complete(actualMessages.capture(), anyList(), eq("required"), eq(false));
+ final List<LLMChatMessage> actualSecondTurnMessages = actualMessages.getAllValues().get(1);
+ final String actualRetryInstruction = actualSecondTurnMessages.get(actualSecondTurnMessages.size() - 1).getContent();
+ assertThat(actualRetryInstruction, CoreMatchers.containsString("Remaining required MCP tools: database_gateway_execute_update"));
+ assertThat(actualRetryInstruction, CoreMatchers.containsString("database `logic_db`"));
+ assertThat(actualRetryInstruction, CoreMatchers.containsString("schema `public`"));
+ assertThat(actualRetryInstruction, CoreMatchers.containsString("execution_mode=preview"));
+ assertThat(actualRetryInstruction, CoreMatchers.containsString("do not use execution_mode=execute"));
+ }
+
+ @Test
+ void assertRunNamesPlanIdRuleWhenPlanningCoverageIsMissing() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_plan_mask_rule"));
+ final LLMMCPConversationRunner actualRunner = createRunner(2);
+ final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response-1"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
+ verify(getLLMChatClient(), times(2)).complete(actualMessages.capture(), anyList(), eq("required"), eq(false));
+ final List<LLMChatMessage> actualSecondTurnMessages = actualMessages.getAllValues().get(1);
+ final String actualRetryInstruction = actualSecondTurnMessages.get(actualSecondTurnMessages.size() - 1).getContent();
+ assertThat(actualRetryInstruction, CoreMatchers.containsString("Remaining required MCP tools: database_gateway_plan_mask_rule"));
+ assertThat(actualRetryInstruction, CoreMatchers.containsString("For a new database_gateway_plan_* call"));
+ assertThat(actualRetryInstruction, CoreMatchers.containsString("omit plan_id"));
+ }
+
+ @Test
+ void assertRunRemovesInitialPlanningPlanId() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_plan_mask_rule"));
+ final LLMMCPConversationRunner actualRunner = createRunner(1);
+ final Map<String, Object> rawPlanArguments = Map.of("plan_id", "1", "database", DATABASE_NAME, "schema", SCHEMA_NAME, "table", TABLE_NAME, "column", "status");
+ final Map<String, Object> expectedPlanArguments = Map.of("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", rawPlanArguments, "plan-response"));
+ when(getMCPInteractionClient().call("database_gateway_plan_mask_rule", expectedPlanArguments)).thenReturn(Map.of("plan_id", "plan-1"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
+ assertThat(actual.getInteractionTrace().get(0).getArguments(), CoreMatchers.is(expectedPlanArguments));
+ assertThat(actual.getInteractionTrace().get(0).getActionOrigin(), CoreMatchers.is(MCPInteractionTraceRecord.HARNESS_ARGUMENT_NORMALIZATION_ORIGIN));
+ verify(getMCPInteractionClient()).call("database_gateway_plan_mask_rule", expectedPlanArguments);
+ }
+
+ @Test
+ void assertRunKeepsCurrentPlanningPlanId() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_plan_mask_rule"));
+ final LLMMCPConversationRunner actualRunner = createRunner(2);
+ final Map<String, Object> initialPlanArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "table", TABLE_NAME, "column", "status");
+ final 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-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",
+ "tool_name", "database_gateway_plan_mask_rule",
+ "arguments", currentPlanArguments))));
+ when(getMCPInteractionClient().call("database_gateway_plan_mask_rule", currentPlanArguments)).thenReturn(Map.of("plan_id", "plan-1"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
+ assertThat(actual.getInteractionTrace().get(1).getArguments(), CoreMatchers.is(currentPlanArguments));
+ assertThat(actual.getInteractionTrace().get(1).getActionOrigin(), CoreMatchers.is(MCPInteractionTraceRecord.MODEL_TOOL_CALL_ORIGIN));
+ verify(getMCPInteractionClient()).call("database_gateway_plan_mask_rule", currentPlanArguments);
+ }
+
+ @Test
+ void assertRunRequiresToolCallWhenExpectedExecuteQueryIsReturnedAsText() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(3);
+ final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ createFinalAnswerCompletion(List.of("database_gateway_execute_query"), 2, "final-answer-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertTrue(actual.getAssertionReport().isSuccess());
+ assertThat(actual.getInteractionTrace().size(), CoreMatchers.is(1));
+ assertThat(actual.getInteractionTrace().get(0).getTargetName(), CoreMatchers.is("database_gateway_execute_query"));
+ assertThat(actual.getInteractionTrace().get(0).getActionOrigin(), CoreMatchers.is(MCPInteractionTraceRecord.MODEL_TOOL_CALL_ORIGIN));
+ verify(getMCPInteractionClient()).call("database_gateway_execute_query", executeQueryArguments);
+ verify(getLLMChatClient(), times(2)).complete(anyList(), anyList(), eq("required"), eq(false));
+ }
+
+ @Test
+ void assertRunNamesLatestPlanIdWhenWorkflowCoverageIsMissing() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_plan_mask_rule", "database_gateway_apply_workflow"));
+ final LLMMCPConversationRunner actualRunner = createRunner(3);
+ final Map<String, Object> planArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "table", TABLE_NAME, "column", "status");
+ final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
+ verify(getLLMChatClient(), times(3)).complete(actualMessages.capture(), anyList(), eq("required"), eq(false));
+ final List<LLMChatMessage> actualThirdTurnMessages = actualMessages.getAllValues().get(2);
+ final String actualRetryInstruction = actualThirdTurnMessages.get(actualThirdTurnMessages.size() - 1).getContent();
+ assertThat(actualRetryInstruction, CoreMatchers.containsString("Remaining required MCP tools: database_gateway_apply_workflow"));
+ assertThat(actualRetryInstruction, CoreMatchers.containsString("set plan_id `plan-1`"));
+ assertThat(actualRetryInstruction, CoreMatchers.containsString("do not use placeholder text `plan_id`"));
+ }
+}
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
new file mode 100644
index 00000000000..55fac28a895
--- /dev/null
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerFailureTest.java
@@ -0,0 +1,309 @@
+/*
+ * 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.test.e2e.mcp.llm.conversation;
+
+import org.apache.shardingsphere.infra.util.json.JsonUtils;
+import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.artifact.LLME2EArtifactBundle;
+import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.client.LLMChatCompletion;
+import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.client.LLMToolCall;
+import org.apache.shardingsphere.test.e2e.mcp.llm.scenario.LLME2EScenario;
+import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionActionNames;
+import org.hamcrest.CoreMatchers;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Stream;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.anyList;
+import static org.mockito.ArgumentMatchers.anyMap;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class LLMMCPConversationRunnerFailureTest extends AbstractLLMMCPConversationRunnerTest {
+
+ @Test
+ void assertRunWithExecuteQueryErrorPayloadIgnoredForCoverage() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(2);
+ final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
+ verify(getLLMChatClient(), times(2)).complete(anyList(), anyList(), eq("required"), eq(false));
+ }
+
+ @Test
+ void assertRunWithInvalidFinalJson() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(3);
+ final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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(
+ new LLMChatCompletion("not-json", List.of(), "invalid-final-response-1"),
+ new LLMChatCompletion("still-not-json", List.of(), "invalid-final-response-2"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("invalid_final_json"));
+ assertThat(actual.getRawModelOutputs(), CoreMatchers.is(List.of("tool-call-response", "invalid-final-response-1", "invalid-final-response-2")));
+ verify(getMCPInteractionClient()).open();
+ verify(getMCPInteractionClient()).close();
+ }
+
+ @Test
+ void assertRunWithMcpActionFailure() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(1);
+ final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("mcp_runtime_unavailable"));
+ }
+
+ @Test
+ void assertRunWithMcpRuntimeUnavailable() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(1);
+ doThrow(new IOException("boom")).when(getMCPInteractionClient()).open();
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("mcp_runtime_unavailable"));
+ verify(getMCPInteractionClient()).open();
+ verify(getMCPInteractionClient()).close();
+ }
+
+ @Test
+ void assertRunWithModelRequestIOException() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(1);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenThrow(new IOException("http 500"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("model_service_unavailable"));
+ verify(getMCPInteractionClient()).open();
+ verify(getMCPInteractionClient()).close();
+ }
+
+ @Test
+ void assertRunWithInterruptedConversation() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(1);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenThrow(new InterruptedException("boom"));
+ try {
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("model_service_unavailable"));
+ assertTrue(Thread.currentThread().isInterrupted());
+ } finally {
+ Thread.interrupted();
+ }
+ }
+
+ @Test
+ void assertRunWithModelServiceUnavailable() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(1);
+ doThrow(new IllegalStateException("Model service is not ready for `ggml-org/Qwen3-1.7B-GGUF:Q4_K_M`.")).when(getLLMChatClient()).waitUntilReady();
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("model_service_unavailable"));
+ verify(getMCPInteractionClient(), never()).open();
+ verify(getMCPInteractionClient()).close();
+ }
+
+ @Test
+ void assertRunIgnoresCloseIOException() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(1);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response"));
+ doThrow(new IOException("close failed")).when(getMCPInteractionClient()).close();
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
+ verify(getMCPInteractionClient()).close();
+ }
+
+ @Test
+ void assertRunRestoresInterruptOnCloseFailure() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(1);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response"));
+ doThrow(new InterruptedException("close interrupted")).when(getMCPInteractionClient()).close();
+ try {
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
+ assertTrue(Thread.currentThread().isInterrupted());
+ } finally {
+ Thread.interrupted();
+ }
+ }
+
+ @Test
+ void assertRunWithUnexpectedTool() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(1);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ new LLMChatCompletion("", List.of(new LLMToolCall("tool-1", "unsupported_tool", "{}")), "tool-call-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("unexpected_tool_requested"));
+ assertThat(actual.getInteractionTrace().size(), CoreMatchers.is(1));
+ verify(getMCPInteractionClient()).open();
+ verify(getMCPInteractionClient(), never()).call(anyString(), anyMap());
+ verify(getMCPInteractionClient()).close();
+ }
+
+ @Test
+ void assertRunWithInvalidToolArgumentsJson() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(1);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ new LLMChatCompletion("", List.of(new LLMToolCall("tool-1", "database_gateway_execute_query", "{invalid")), "tool-call-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("invalid_tool_arguments"));
+ assertThat(actual.getInteractionTrace().get(0).getTargetName(), CoreMatchers.is("database_gateway_execute_query"));
+ verify(getMCPInteractionClient()).open();
+ verify(getMCPInteractionClient(), never()).call(anyString(), anyMap());
+ verify(getMCPInteractionClient()).close();
+ }
+
+ @Test
+ void assertRunWithEmptyResourceUri() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of(MCPInteractionActionNames.READ_RESOURCE));
+ final LLMMCPConversationRunner actualRunner = createRunner(1);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ createToolCallCompletion("tool-1", MCPInteractionActionNames.READ_RESOURCE, Map.of("uri", " "), "tool-call-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("invalid_tool_arguments"));
+ assertThat(actual.getInteractionTrace().get(0).getActionKind(), CoreMatchers.is(MCPInteractionActionNames.RESOURCE_READ_KIND));
+ verify(getMCPInteractionClient(), never()).readResource(anyString());
+ }
+
+ @Test
+ void assertRunWithUnsafeSqlAttempted() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(1);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ createToolCallCompletion("tool-1", "database_gateway_execute_query",
+ Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "sql", "UPDATE orders SET status = 'DONE'"),
+ "tool-call-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("unsafe_sql_attempted"));
+ assertThat(actual.getInteractionTrace().get(0).getTargetName(), CoreMatchers.is("database_gateway_execute_query"));
+ verify(getMCPInteractionClient(), never()).call(anyString(), anyMap());
+ }
+
+ @Test
+ void assertRunWithUnsafeExecuteUpdateAttempted() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_update"));
+ final LLMMCPConversationRunner actualRunner = createRunner(1);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("unsafe_sql_execution_attempted"));
+ assertThat(actual.getInteractionTrace().get(0).getTargetName(), CoreMatchers.is("database_gateway_execute_update"));
+ verify(getMCPInteractionClient(), never()).call(anyString(), anyMap());
+ }
+
+ @Test
+ void assertRunWithUnsafeWorkflowExecutionAttempted() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_apply_workflow"));
+ final LLMMCPConversationRunner actualRunner = createRunner(1);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ createToolCallCompletion("tool-1", "database_gateway_apply_workflow", Map.of("plan_id", "plan-1", "execution_mode", "review-then-execute"), "tool-call-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("unsafe_workflow_execution_attempted"));
+ assertThat(actual.getInteractionTrace().get(0).getTargetName(), CoreMatchers.is("database_gateway_apply_workflow"));
+ verify(getMCPInteractionClient(), never()).call(anyString(), anyMap());
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("unexpectedQueryResultCases")
+ void assertRunWithUnexpectedQueryResult(final String caseName, final Map<String, Object> finalAnswerPayload) throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(2);
+ final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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(
+ new LLMChatCompletion(JsonUtils.toJsonString(finalAnswerPayload), List.of(), caseName));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("unexpected_query_result"));
+ verify(getMCPInteractionClient()).call("database_gateway_execute_query", executeQueryArguments);
+ }
+
+ private static Stream<Arguments> unexpectedQueryResultCases() {
+ return Stream.of(
+ Arguments.of("database mismatch", createMutatedFinalAnswerPayload("database", "other_db")),
+ Arguments.of("schema mismatch", createMutatedFinalAnswerPayload("schema", "other_schema")),
+ Arguments.of("table mismatch", createMutatedFinalAnswerPayload("table", "other_table")),
+ Arguments.of("query mismatch", createMutatedFinalAnswerPayload("query", "SELECT * FROM orders")),
+ Arguments.of("totalOrders mismatch", createMutatedFinalAnswerPayload("totalOrders", 3)),
+ Arguments.of("interactionSequence mismatch", createMutatedFinalAnswerPayload("interactionSequence", List.of(MCPInteractionActionNames.READ_RESOURCE))));
+ }
+
+}
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
new file mode 100644
index 00000000000..b3c6df4263c
--- /dev/null
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/LLMMCPConversationRunnerNextActionTest.java
@@ -0,0 +1,432 @@
+/*
+ * 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.test.e2e.mcp.llm.conversation;
+
+import org.apache.shardingsphere.infra.util.json.JsonUtils;
+import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.artifact.LLME2EArtifactBundle;
+import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.client.LLMChatCompletion;
+import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.client.LLMChatMessage;
+import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.client.LLMToolCall;
+import org.apache.shardingsphere.test.e2e.mcp.llm.scenario.LLME2EScenario;
+import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionActionNames;
+import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionTraceRecord;
+import org.hamcrest.CoreMatchers;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.io.IOException;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+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.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class LLMMCPConversationRunnerNextActionTest extends AbstractLLMMCPConversationRunnerTest {
+
+ @Test
+ void assertRunRetriesExpectedExecuteQueryBeforeFinalAnswer() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(4);
+ final Map<String, Object> wrongQueryArguments = Map.of("database", "analytics_db", "schema", SCHEMA_NAME, "sql", QUERY, "max_rows", 10);
+ final Map<String, Object> expectedQueryArguments = createExecuteQueryArguments(QUERY);
+ final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
+ 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-2", "database_gateway_execute_query", expectedQueryArguments, "expected-query-response"));
+ 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"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertTrue(actual.getAssertionReport().isSuccess());
+ verify(getLLMChatClient()).complete(actualMessages.capture(), anyList(), eq("auto"), eq(false));
+ final List<LLMChatMessage> actualAutoTurnMessages = actualMessages.getValue();
+ 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(), CoreMatchers.is(2));
+ }
+
+ @Test
+ void assertRunRecoversCompletionArguments() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of(MCPInteractionActionNames.COMPLETE, "database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(4);
+ final Map<String, Object> completionReference = Map.of("type", "ref/prompt", "name", PROMPT_NAME);
+ final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ createToolCallCompletion("tool-1", MCPInteractionActionNames.COMPLETE, Map.of("argument_name", "schema", "argument_value", "pub"), "bad-completion-response"),
+ new LLMChatCompletion("",
+ List.of(
+ new LLMToolCall("tool-2", MCPInteractionActionNames.COMPLETE, JsonUtils.toJsonString(Map.of(
+ "reference", completionReference,
+ "argument_name", "schema",
+ "argument_value", "pub"))),
+ new LLMToolCall("tool-3", "database_gateway_execute_query", JsonUtils.toJsonString(executeQueryArguments))),
+ "retry-completion-response"));
+ when(getMCPInteractionClient().complete(completionReference, "schema", "pub", Map.of())).thenReturn(Map.of("completion", Map.of("values", List.of(SCHEMA_NAME))));
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ createFinalAnswerCompletion(List.of(MCPInteractionActionNames.COMPLETE, "database_gateway_execute_query"), 2, "final-answer-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertTrue(actual.getAssertionReport().isSuccess());
+ assertThat(actual.getInteractionTrace().size(), CoreMatchers.is(3));
+ assertThat(actual.getInteractionTrace().get(0).getStructuredContent().get("error_code"), CoreMatchers.is("invalid_tool_arguments"));
+ verify(getMCPInteractionClient()).complete(completionReference, "schema", "pub", Map.of());
+ }
+
+ @Test
+ void assertRunNormalizesCompletionArgumentName() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of(MCPInteractionActionNames.COMPLETE, "database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(3);
+ final Map<String, Object> completionReference = Map.of("type", "ref/prompt", "name", PROMPT_NAME);
+ final Map<String, String> completionContext = Map.of("database", DATABASE_NAME);
+ final Map<String, Object> completionArguments = new LinkedHashMap<>(4, 1F);
+ completionArguments.put("reference", completionReference);
+ completionArguments.put("reference argument_name", "schema");
+ completionArguments.put("argument_value", "pub");
+ completionArguments.put("context_arguments", completionContext);
+ final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ createToolCallCompletion("tool-1", MCPInteractionActionNames.COMPLETE, completionArguments, "completion-response"),
+ createToolCallCompletion("tool-2", "database_gateway_execute_query", executeQueryArguments, "query-response"));
+ when(getMCPInteractionClient().complete(completionReference, "schema", "pub", completionContext)).thenReturn(Map.of("completion", Map.of("values", List.of(SCHEMA_NAME))));
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ createFinalAnswerCompletion(List.of(MCPInteractionActionNames.COMPLETE, "database_gateway_execute_query"), 2, "final-answer-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertTrue(actual.getAssertionReport().isSuccess());
+ assertThat(actual.getInteractionTrace().size(), CoreMatchers.is(2));
+ verify(getMCPInteractionClient()).complete(completionReference, "schema", "pub", completionContext);
+ }
+
+ @Test
+ void assertRunDefaultsCompletionReferenceFromPrompt() throws IOException, InterruptedException {
+ final LLME2EScenario actualScenario = createScenario(List.of(MCPInteractionActionNames.GET_PROMPT, MCPInteractionActionNames.COMPLETE, "database_gateway_execute_query"));
+ final LLMMCPConversationRunner actualRunner = createRunner(5);
+ final Map<String, Object> getPromptArguments = Map.of("name", PROMPT_NAME);
+ final Map<String, Object> completionReference = Map.of("type", "ref/prompt", "name", PROMPT_NAME);
+ final Map<String, Object> completionArguments = Map.of("argument_name", "schema", "argument_value", "pub");
+ final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
+ final ArgumentCaptor<List<Map<String, Object>>> actualTools = createToolDefinitionsCaptor();
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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"));
+ when(getMCPInteractionClient().getPrompt(PROMPT_NAME, Map.of())).thenReturn(Map.of("messages", List.of(), "next_actions", List.of(Map.of(
+ "type", "completion",
+ "arguments", Map.of("reference", completionReference, "argument_name", "schema", "argument_value", "pub")))));
+ when(getMCPInteractionClient().complete(completionReference, "schema", "pub", Map.of())).thenReturn(Map.of("completion", Map.of("values", List.of(SCHEMA_NAME))));
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ createFinalAnswerCompletion(List.of(MCPInteractionActionNames.GET_PROMPT, MCPInteractionActionNames.COMPLETE, "database_gateway_execute_query"), 2, "final-answer-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertTrue(actual.getAssertionReport().isSuccess());
+ verify(getLLMChatClient(), times(3)).complete(anyList(), actualTools.capture(), eq("required"), eq(false));
+ assertThat(getToolNames(actualTools.getAllValues().get(1)), CoreMatchers.is(List.of(MCPInteractionActionNames.COMPLETE)));
+ assertThat(getToolNames(actualTools.getAllValues().get(2)), CoreMatchers.is(List.of("database_gateway_execute_query")));
+ verify(getMCPInteractionClient()).complete(completionReference, "schema", "pub", Map.of());
+ }
+
+ @Test
+ void assertRunPromptsImmediateResourceNextAction() throws IOException, InterruptedException {
+ final List<String> toolNames = List.of(MCPInteractionActionNames.READ_RESOURCE, "database_gateway_execute_query");
+ final LLME2EScenario actualScenario = createScenario(toolNames);
+ final LLMMCPConversationRunner actualRunner = createRunner(4);
+ final Map<String, Object> runtimeArguments = Map.of("uri", "shardingsphere://runtime");
+ final Map<String, Object> capabilitiesArguments = Map.of("uri", RESOURCE_URI);
+ final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
+ final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
+ final ArgumentCaptor<List<Map<String, Object>>> actualTools = createToolDefinitionsCaptor();
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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"));
+ when(getMCPInteractionClient().readResource("shardingsphere://runtime")).thenReturn(Map.of("next_actions", List.of(Map.of(
+ "type", "resource_read",
+ "resource_uri", RESOURCE_URI))));
+ when(getMCPInteractionClient().readResource(RESOURCE_URI)).thenReturn(Map.of("response_mode", "capabilities"));
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ createFinalAnswerCompletion(List.of(MCPInteractionActionNames.READ_RESOURCE, "database_gateway_execute_query"), 2, "final-answer-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertTrue(actual.getAssertionReport().isSuccess());
+ verify(getLLMChatClient(), times(3)).complete(actualMessages.capture(), actualTools.capture(), eq("required"), eq(false));
+ assertTrue(containsMessage(actualMessages.getAllValues().get(1), "Call mcp_read_resource with uri `" + RESOURCE_URI + "` now"));
+ assertThat(getToolNames(actualTools.getAllValues().get(1)), CoreMatchers.is(List.of(MCPInteractionActionNames.READ_RESOURCE)));
+ }
+
+ @Test
+ void assertRunPromptsImmediateToolNextAction() throws IOException, InterruptedException {
+ final List<String> toolNames = List.of("database_gateway_plan_mask_rule", "database_gateway_apply_workflow", "database_gateway_execute_query");
+ final LLME2EScenario actualScenario = createScenario(toolNames);
+ final LLMMCPConversationRunner actualRunner = createRunner(4);
+ final Map<String, Object> planArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "table", TABLE_NAME, "column", "status");
+ final Map<String, Object> applyArguments = Map.of("plan_id", "plan-1", "execution_mode", "preview");
+ final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
+ final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
+ final ArgumentCaptor<List<Map<String, Object>>> actualTools = createToolDefinitionsCaptor();
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ createToolCallCompletion("tool-1", "database_gateway_plan_mask_rule", planArguments, "plan-response"),
+ createToolCallCompletion("tool-2", "database_gateway_apply_workflow", Map.of("plan_id", "1", "execution_mode", "preview"), "apply-response"),
+ createToolCallCompletion("tool-3", "database_gateway_execute_query", executeQueryArguments, "query-response"));
+ when(getMCPInteractionClient().call("database_gateway_plan_mask_rule", planArguments)).thenReturn(Map.of("plan_id", "plan-1", "next_actions", List.of(Map.of(
+ "type", "tool_call",
+ "tool_name", "database_gateway_apply_workflow",
+ "arguments", applyArguments))));
+ when(getMCPInteractionClient().call("database_gateway_apply_workflow", applyArguments)).thenReturn(Map.of("response_mode", "preview"));
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ createFinalAnswerCompletion(toolNames, 2, "final-answer-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertTrue(actual.getAssertionReport().isSuccess());
+ assertThat(actual.getInteractionTrace().get(1).getActionOrigin(), CoreMatchers.is(MCPInteractionTraceRecord.HARNESS_ARGUMENT_NORMALIZATION_ORIGIN));
+ verify(getLLMChatClient(), times(3)).complete(actualMessages.capture(), actualTools.capture(), eq("required"), eq(false));
+ assertTrue(containsMessage(actualMessages.getAllValues().get(1), "Call `database_gateway_apply_workflow` now with exactly these arguments"));
+ assertTrue(containsMessage(actualMessages.getAllValues().get(1), "\"plan_id\":\"plan-1\""));
+ assertThat(getToolNames(actualTools.getAllValues().get(1)), CoreMatchers.is(List.of("database_gateway_apply_workflow")));
+ }
+
+ @Test
+ void assertRunPromptsExactResourceAfterList() throws IOException, InterruptedException {
+ final List<String> toolNames = List.of(MCPInteractionActionNames.LIST_RESOURCES, MCPInteractionActionNames.READ_RESOURCE, "database_gateway_execute_query");
+ final String tableResourceUri = "shardingsphere://databases/logic_db/schemas/public/tables/orders";
+ final LLME2EScenario actualScenario = createScenario(toolNames, "Read exact `" + tableResourceUri + "` before querying.");
+ final LLMMCPConversationRunner actualRunner = createRunner(4);
+ final Map<String, Object> readResourceArguments = Map.of("uri", "{description=Resource URI to read., type=string}");
+ final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
+ final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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"));
+ when(getMCPInteractionClient().listResources()).thenReturn(Map.of("resources", List.of(Map.of("uri", RESOURCE_URI))));
+ when(getMCPInteractionClient().readResource(tableResourceUri)).thenReturn(Map.of("found", true));
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ createFinalAnswerCompletion(toolNames, 2, "final-answer-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertTrue(actual.getAssertionReport().isSuccess());
+ verify(getLLMChatClient(), times(3)).complete(actualMessages.capture(), anyList(), eq("required"), eq(false));
+ assertTrue(containsMessage(actualMessages.getAllValues().get(1), "Use exactly `" + tableResourceUri + "` as uri"));
+ assertTrue(containsMessage(actualMessages.getAllValues().get(1), "do not copy parameter schema"));
+ }
+
+ @Test
+ void assertRunPromptsLiveResourceAfterStaleResourceMiss() throws IOException, InterruptedException {
+ final List<String> toolNames = List.of(MCPInteractionActionNames.READ_RESOURCE, "database_gateway_execute_query");
+ final String staleResourceUri = "shardingsphere://databases/unknown/schemas/unknown/tables/orders";
+ final String tableResourceUri = "shardingsphere://databases/logic_db/schemas/public/tables/orders";
+ final LLME2EScenario actualScenario = createScenario(toolNames,
+ "Read stale `" + staleResourceUri + "`, recover by reading exact live table resource `" + tableResourceUri + "`, then query.");
+ final LLMMCPConversationRunner actualRunner = createRunner(4);
+ final Map<String, Object> staleReadArguments = Map.of("uri", staleResourceUri);
+ final Map<String, Object> liveReadArguments = Map.of("uri", tableResourceUri);
+ final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
+ final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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"));
+ when(getMCPInteractionClient().readResource(staleResourceUri)).thenReturn(Map.of(
+ "found", false,
+ "next_actions", List.of(Map.of(
+ "type", "resource_read",
+ "resource_uri", "shardingsphere://databases/unknown/schemas/unknown/tables"))));
+ when(getMCPInteractionClient().readResource(tableResourceUri)).thenReturn(Map.of("found", true));
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ createFinalAnswerCompletion(toolNames, 2, "final-answer-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertTrue(actual.getAssertionReport().isSuccess());
+ verify(getLLMChatClient(), times(3)).complete(actualMessages.capture(), anyList(), eq("required"), eq(false));
+ assertTrue(containsMessage(actualMessages.getAllValues().get(1), "Use exactly `" + tableResourceUri + "` as uri"));
+ assertThat(actual.getInteractionTrace().get(1).getArguments().get("uri"), CoreMatchers.is(tableResourceUri));
+ }
+
+ @Test
+ void assertRunFollowsPendingNextActionBeforeFinalAnswer() throws IOException, InterruptedException {
+ final List<String> toolNames = List.of("database_gateway_apply_workflow", "database_gateway_execute_query");
+ final LLME2EScenario actualScenario = createScenario(toolNames);
+ final LLMMCPConversationRunner actualRunner = createRunner(3);
+ final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
+ final Map<String, Object> previewArguments = Map.of("plan_id", "plan-1", "execution_mode", "preview");
+ final Map<String, Object> manualArguments = Map.of("plan_id", "plan-1", "execution_mode", "manual-only");
+ final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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"));
+ 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"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertTrue(actual.getAssertionReport().isSuccess());
+ verify(getLLMChatClient()).complete(actualMessages.capture(), anyList(), eq("auto"), eq(false));
+ assertTrue(containsMessage(actualMessages.getValue(), "Call `database_gateway_apply_workflow` now with exactly these arguments"));
+ }
+
+ @Test
+ void assertRunIgnoresSideEffectExecutionNextActionBeforeReadOnlyVerification() throws IOException, InterruptedException {
+ final List<String> toolNames = List.of("database_gateway_execute_update", "database_gateway_execute_query");
+ final LLME2EScenario actualScenario = createScenario(toolNames);
+ final LLMMCPConversationRunner actualRunner = createRunner(3);
+ final Map<String, Object> previewArguments = Map.of(
+ "database", DATABASE_NAME,
+ "schema", SCHEMA_NAME,
+ "sql", "UPDATE orders SET status = status WHERE order_id = -1",
+ "execution_mode", "preview");
+ final Map<String, Object> executeQueryArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "sql", QUERY);
+ final Map<String, Object> misroutedQueryArguments = Map.of(
+ "database", DATABASE_NAME,
+ "schema", SCHEMA_NAME,
+ "sql", QUERY,
+ "execution_mode", "preview");
+ final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
+ final ArgumentCaptor<List<Map<String, Object>>> actualTools = createToolDefinitionsCaptor();
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ createToolCallCompletion("tool-1", "database_gateway_execute_update", previewArguments, "preview-response"),
+ createToolCallCompletion("tool-2", "database_gateway_execute_update", misroutedQueryArguments, "query-response"));
+ when(getMCPInteractionClient().call("database_gateway_execute_update", previewArguments)).thenReturn(Map.of("response_mode", "preview", "next_actions", List.of(Map.of(
+ "type", "tool_call",
+ "tool_name", "database_gateway_execute_update",
+ "arguments", Map.of("database", DATABASE_NAME, "sql", "UPDATE orders SET status = status WHERE order_id = -1", "execution_mode", "execute")))));
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ createFinalAnswerCompletion(toolNames, 2, "final-answer-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertTrue(actual.getAssertionReport().isSuccess());
+ verify(getLLMChatClient(), times(2)).complete(actualMessages.capture(), actualTools.capture(), eq("required"), eq(false));
+ final List<LLMChatMessage> actualSecondTurnMessages = actualMessages.getAllValues().get(1);
+ assertTrue(containsMessage(actualSecondTurnMessages, "side-effect execution next_actions"));
+ assertThat(getToolNames(actualTools.getAllValues().get(1)), CoreMatchers.is(List.of("database_gateway_execute_query")));
+ assertThat(actual.getInteractionTrace().get(1).getTargetName(), CoreMatchers.is("database_gateway_execute_query"));
+ assertThat(actual.getInteractionTrace().get(1).getActionOrigin(), CoreMatchers.is(MCPInteractionTraceRecord.HARNESS_ARGUMENT_NORMALIZATION_ORIGIN));
+ verify(getMCPInteractionClient(), never()).call("database_gateway_execute_update", misroutedQueryArguments);
+ }
+
+ @Test
+ void assertRunRoutesReadOnlyCallAfterSideEffectPreviewInSameCompletion() throws IOException, InterruptedException {
+ final List<String> toolNames = List.of("database_gateway_execute_update", "database_gateway_execute_query");
+ final LLME2EScenario actualScenario = createScenario(toolNames);
+ final LLMMCPConversationRunner actualRunner = createRunner(3);
+ final Map<String, Object> previewArguments = Map.of(
+ "database", DATABASE_NAME,
+ "schema", SCHEMA_NAME,
+ "sql", "UPDATE orders SET status = status WHERE order_id = -1",
+ "execution_mode", "preview");
+ final Map<String, Object> executeQueryArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "sql", QUERY);
+ final Map<String, Object> misroutedQueryArguments = Map.of(
+ "database", DATABASE_NAME,
+ "schema", SCHEMA_NAME,
+ "sql", QUERY,
+ "execution_mode", "preview");
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(new LLMChatCompletion("",
+ List.of(
+ new LLMToolCall("tool-1", "database_gateway_execute_update", JsonUtils.toJsonString(previewArguments)),
+ new LLMToolCall("tool-2", "database_gateway_execute_update", JsonUtils.toJsonString(misroutedQueryArguments))),
+ "compound-tool-response"));
+ when(getMCPInteractionClient().call("database_gateway_execute_update", previewArguments)).thenReturn(Map.of("response_mode", "preview", "next_actions", List.of(Map.of(
+ "type", "tool_call",
+ "tool_name", "database_gateway_execute_update",
+ "arguments", Map.of("database", DATABASE_NAME, "sql", "UPDATE orders SET status = status WHERE order_id = -1", "execution_mode", "execute")))));
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ createFinalAnswerCompletion(toolNames, 2, "final-answer-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertTrue(actual.getAssertionReport().isSuccess());
+ assertThat(actual.getInteractionTrace().get(0).getTargetName(), CoreMatchers.is("database_gateway_execute_update"));
+ assertThat(actual.getInteractionTrace().get(1).getTargetName(), CoreMatchers.is("database_gateway_execute_query"));
+ assertThat(actual.getInteractionTrace().get(1).getActionOrigin(), CoreMatchers.is(MCPInteractionTraceRecord.HARNESS_ARGUMENT_NORMALIZATION_ORIGIN));
+ verify(getMCPInteractionClient(), never()).call("database_gateway_execute_update", misroutedQueryArguments);
+ }
+
+ @Test
+ void assertRunCompactsManualArtifactsBeforeReadOnlyVerification() throws IOException, InterruptedException {
+ final List<String> toolNames = List.of("database_gateway_apply_workflow", "database_gateway_execute_query");
+ final LLME2EScenario actualScenario = createScenario(toolNames);
+ final LLMMCPConversationRunner actualRunner = createRunner(3);
+ final Map<String, Object> manualArguments = Map.of("plan_id", "plan-1", "execution_mode", "manual-only");
+ final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
+ final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
+ final ArgumentCaptor<List<Map<String, Object>>> actualTools = createToolDefinitionsCaptor();
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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(
+ "response_mode", "manual_only",
+ "manual_artifact_summary", Map.of("distsql_artifact_count", 1),
+ "manual_artifacts", List.of(Map.of("distsql_artifacts", List.of(Map.of("sql", "CREATE MASK RULE orders SECRET")))),
+ "next_actions", List.of(Map.of(
+ "type", "ask_user",
+ "reason", "Confirm manual artifacts were executed."))));
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ createFinalAnswerCompletion(toolNames, 2, "final-answer-response"));
+
+ final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
+
+ assertTrue(actual.getAssertionReport().isSuccess());
+ verify(getLLMChatClient(), times(2)).complete(actualMessages.capture(), actualTools.capture(), eq("required"), eq(false));
+ final List<LLMChatMessage> actualSecondTurnMessages = actualMessages.getAllValues().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.getAllValues().get(1)), CoreMatchers.is(List.of("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 d50135a7d37..657f5e08aac 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
@@ -21,62 +21,31 @@ import org.apache.shardingsphere.infra.util.json.JsonUtils;
import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.artifact.LLME2EArtifactBundle;
import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.client.LLMChatCompletion;
import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.client.LLMChatMessage;
-import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.client.LLMChatModelClient;
import org.apache.shardingsphere.test.e2e.mcp.llm.conversation.client.LLMToolCall;
import org.apache.shardingsphere.test.e2e.mcp.llm.scenario.LLME2EScenario;
-import org.apache.shardingsphere.test.e2e.mcp.llm.scenario.LLMStructuredAnswer;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionActionNames;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionTraceRecord;
-import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPInteractionClient;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
-import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.Arguments;
-import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.ArgumentCaptor;
-import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.io.IOException;
-import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
-import java.util.stream.Stream;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyList;
-import static org.mockito.ArgumentMatchers.anyMap;
-import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
-class LLMMCPConversationRunnerTest {
-
- private static final String DATABASE_NAME = "logic_db";
-
- private static final String SCHEMA_NAME = "public";
-
- private static final String TABLE_NAME = "orders";
-
- private static final String QUERY = "SELECT COUNT(*) AS total_orders FROM orders";
-
- private static final String RESOURCE_URI = "shardingsphere://capabilities";
-
- private static final String PROMPT_NAME = "inspect_metadata";
-
- @Mock
- private LLMChatModelClient llmChatClient;
-
- @Mock
- private MCPInteractionClient mcpInteractionClient;
+class LLMMCPConversationRunnerTest extends AbstractLLMMCPConversationRunnerTest {
@Test
void assertRunWithExecuteQuery() throws IOException, InterruptedException {
@@ -84,10 +53,10 @@ class LLMMCPConversationRunnerTest {
final LLMMCPConversationRunner actualRunner = createRunner(3);
final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
final ArgumentCaptor<List<Map<String, Object>>> actualTools = createToolDefinitionsCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).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"));
final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
@@ -96,10 +65,10 @@ class LLMMCPConversationRunnerTest {
assertThat(actual.getInteractionTrace().size(), CoreMatchers.is(1));
assertThat(actual.getInteractionTrace().get(0).getActionOrigin(), CoreMatchers.is(MCPInteractionTraceRecord.MODEL_TOOL_CALL_ORIGIN));
assertThat(actual.getRawModelOutputs(), CoreMatchers.is(List.of("tool-call-response", "final-answer-response")));
- verify(llmChatClient).complete(anyList(), actualTools.capture(), eq("required"), eq(false));
- verify(mcpInteractionClient).open();
- verify(mcpInteractionClient).call("database_gateway_execute_query", executeQueryArguments);
- verify(mcpInteractionClient).close();
+ verify(getLLMChatClient()).complete(anyList(), actualTools.capture(), eq("required"), eq(false));
+ verify(getMCPInteractionClient()).open();
+ verify(getMCPInteractionClient()).call("database_gateway_execute_query", executeQueryArguments);
+ verify(getMCPInteractionClient()).close();
assertThat(getToolName(actualTools.getValue().get(0)), CoreMatchers.is("database_gateway_execute_query"));
assertThat(getMap(getFunction(actualTools.getValue().get(0)).get("parameters")).get("required"), CoreMatchers.is(List.of("database", "sql")));
}
@@ -110,10 +79,10 @@ class LLMMCPConversationRunnerTest {
final LLMMCPConversationRunner actualRunner = createRunner(3);
final Map<String, Object> modelQueryArguments = Map.of("database", DATABASE_NAME, "sql", QUERY, "max_rows", 10);
final Map<String, Object> expectedQueryArguments = createExecuteQueryArguments(QUERY);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_query", modelQueryArguments, "tool-call-response"));
- when(mcpInteractionClient.call("database_gateway_execute_query", expectedQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ 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"));
final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
@@ -121,8 +90,8 @@ class LLMMCPConversationRunnerTest {
assertTrue(actual.getAssertionReport().isSuccess());
assertThat(actual.getInteractionTrace().get(0).getActionOrigin(), CoreMatchers.is(MCPInteractionTraceRecord.HARNESS_ARGUMENT_NORMALIZATION_ORIGIN));
assertThat(actual.getInteractionTrace().get(0).getArguments(), CoreMatchers.is(expectedQueryArguments));
- verify(mcpInteractionClient).call("database_gateway_execute_query", expectedQueryArguments);
- verify(llmChatClient, never()).complete(anyList(), anyList(), eq("auto"), eq(false));
+ verify(getMCPInteractionClient()).call("database_gateway_execute_query", expectedQueryArguments);
+ verify(getLLMChatClient(), never()).complete(anyList(), anyList(), eq("auto"), eq(false));
}
@Test
@@ -137,15 +106,15 @@ class LLMMCPConversationRunnerTest {
"database", DATABASE_NAME,
"schema", SCHEMA_NAME);
final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(new LLMChatCompletion("",
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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))),
"tool-call-response"));
- when(mcpInteractionClient.call("database_gateway_search_metadata", expectedSearchArguments)).thenReturn(
+ when(getMCPInteractionClient().call("database_gateway_search_metadata", expectedSearchArguments)).thenReturn(
Map.of("items", List.of(Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "table", TABLE_NAME))));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
createFinalAnswerCompletion(toolNames, 2, "final-answer-response"));
final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
@@ -153,7 +122,7 @@ class LLMMCPConversationRunnerTest {
assertTrue(actual.getAssertionReport().isSuccess());
assertThat(actual.getInteractionTrace().get(0).getActionOrigin(), CoreMatchers.is(MCPInteractionTraceRecord.HARNESS_ARGUMENT_NORMALIZATION_ORIGIN));
assertThat(actual.getInteractionTrace().get(0).getArguments(), CoreMatchers.is(expectedSearchArguments));
- verify(mcpInteractionClient).call("database_gateway_search_metadata", expectedSearchArguments);
+ verify(getMCPInteractionClient()).call("database_gateway_search_metadata", expectedSearchArguments);
}
@Test
@@ -163,17 +132,17 @@ class LLMMCPConversationRunnerTest {
final String actualQuery = "SELECT COUNT(*) AS total_orders FROM public.orders";
final Map<String, Object> modelQueryArguments = Map.of("database", DATABASE_NAME, "sql", actualQuery);
final Map<String, Object> expectedQueryArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "sql", actualQuery);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_query", modelQueryArguments, "tool-call-response"));
- when(mcpInteractionClient.call("database_gateway_execute_query", expectedQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ 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"));
final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertTrue(actual.getAssertionReport().isSuccess());
assertThat(actual.getInteractionTrace().get(0).getArguments(), CoreMatchers.is(expectedQueryArguments));
- verify(llmChatClient, never()).complete(anyList(), anyList(), eq("auto"), eq(false));
+ verify(getLLMChatClient(), never()).complete(anyList(), anyList(), eq("auto"), eq(false));
}
@Test
@@ -188,20 +157,20 @@ class LLMMCPConversationRunnerTest {
"sql", "UPDATE orders SET status = status WHERE order_id = -1",
"execution_mode", "preview");
final ArgumentCaptor<List<LLMChatMessage>> actualFinalMessages = createChatMessagesCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(new LLMChatCompletion("",
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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))),
"tool-call-response"));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(mcpInteractionClient.call("database_gateway_execute_update", executeUpdateArguments)).thenReturn(Map.of("result_kind", "preview"));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getMCPInteractionClient().call("database_gateway_execute_update", executeUpdateArguments)).thenReturn(Map.of("result_kind", "preview"));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
createFinalAnswerCompletion(toolNames, 2, "final-answer-response"));
final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertTrue(actual.getAssertionReport().isSuccess());
- verify(llmChatClient).complete(actualFinalMessages.capture(), eq(List.of()), eq("none"), eq(true));
+ verify(getLLMChatClient()).complete(actualFinalMessages.capture(), eq(List.of()), eq("none"), eq(true));
final List<LLMChatMessage> actualMessages = actualFinalMessages.getValue();
final String actualInstruction = actualMessages.get(actualMessages.size() - 1).getContent();
assertThat(actualInstruction, CoreMatchers.containsString("interactionSequence exactly to this JSON array: [\"database_gateway_execute_query\",\"database_gateway_execute_update\"]"));
@@ -214,16 +183,16 @@ class LLMMCPConversationRunnerTest {
final LLME2EScenario actualScenario = createScenario(actualToolNames);
final LLMMCPConversationRunner actualRunner = createRunner(3);
final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(new LLMChatCompletion("",
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(new LLMChatCompletion("",
List.of(
new LLMToolCall("tool-1", MCPInteractionActionNames.LIST_RESOURCES, "{}"),
new LLMToolCall("tool-2", MCPInteractionActionNames.READ_RESOURCE, JsonUtils.toJsonString(Map.of("uri", RESOURCE_URI))),
new LLMToolCall("tool-3", "database_gateway_execute_query", JsonUtils.toJsonString(executeQueryArguments))),
"tool-call-response"));
- when(mcpInteractionClient.listResources()).thenReturn(Map.of("resources", List.of(Map.of("uri", RESOURCE_URI))));
- when(mcpInteractionClient.readResource(RESOURCE_URI)).thenReturn(Map.of("supportedTools", List.of("database_gateway_search_metadata", "database_gateway_execute_query")));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload("2"));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ when(getMCPInteractionClient().listResources()).thenReturn(Map.of("resources", List.of(Map.of("uri", RESOURCE_URI))));
+ when(getMCPInteractionClient().readResource(RESOURCE_URI)).thenReturn(Map.of("supportedTools", List.of("database_gateway_search_metadata", "database_gateway_execute_query")));
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload("2"));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
createFinalAnswerCompletion(actualToolNames, "2", "final-answer-response"));
final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
@@ -235,9 +204,9 @@ class LLMMCPConversationRunnerTest {
assertThat(actual.getInteractionTrace().get(1).getActionKind(), CoreMatchers.is(MCPInteractionActionNames.RESOURCE_READ_KIND));
assertThat(actual.getInteractionTrace().get(2).getTargetName(), CoreMatchers.is("database_gateway_execute_query"));
assertThat(actual.getInteractionTrace().get(2).getActionOrigin(), CoreMatchers.is(MCPInteractionTraceRecord.MODEL_TOOL_CALL_ORIGIN));
- verify(mcpInteractionClient).listResources();
- verify(mcpInteractionClient).readResource(RESOURCE_URI);
- verify(mcpInteractionClient).call("database_gateway_execute_query", executeQueryArguments);
+ verify(getMCPInteractionClient()).listResources();
+ verify(getMCPInteractionClient()).readResource(RESOURCE_URI);
+ verify(getMCPInteractionClient()).call("database_gateway_execute_query", executeQueryArguments);
}
@Test
@@ -251,7 +220,7 @@ class LLMMCPConversationRunnerTest {
final Map<String, Object> applyArguments = Map.of("plan_id", "plan-1", "execution_mode", "manual-only");
final Map<String, Object> validateArguments = Map.of("plan_id", "plan-1");
final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(new LLMChatCompletion("",
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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)),
@@ -261,14 +230,15 @@ class LLMMCPConversationRunnerTest {
new LLMToolCall("tool-6", "database_gateway_validate_workflow", JsonUtils.toJsonString(validateArguments)),
new LLMToolCall("tool-7", "database_gateway_execute_query", JsonUtils.toJsonString(executeQueryArguments))),
"tool-call-response"));
- when(mcpInteractionClient.readResource("shardingsphere://features/mask/algorithms")).thenReturn(Map.of("algorithms", List.of(Map.of("type", "MD5"))));
- when(mcpInteractionClient.call("database_gateway_plan_mask_rule", planArguments)).thenReturn(Map.of("plan_id", "plan-1", "resources_to_read", List.of(Map.of("uri", workflowResourceUri))));
- when(mcpInteractionClient.readResource("shardingsphere://runtime")).thenReturn(Map.of("status", "available", "capability_fingerprint", "abc"));
- when(mcpInteractionClient.readResource(workflowResourceUri)).thenReturn(Map.of("plan_id", "plan-1", "status", "planned"));
- when(mcpInteractionClient.call("database_gateway_apply_workflow", applyArguments)).thenReturn(Map.of("status", "awaiting-manual-execution", "response_mode", "manual_only"));
- when(mcpInteractionClient.call("database_gateway_validate_workflow", validateArguments)).thenReturn(Map.of("status", "passed", "overall_status", "passed"));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ when(getMCPInteractionClient().readResource("shardingsphere://features/mask/algorithms")).thenReturn(Map.of("algorithms", List.of(Map.of("type", "MD5"))));
+ when(getMCPInteractionClient().call("database_gateway_plan_mask_rule", planArguments))
+ .thenReturn(Map.of("plan_id", "plan-1", "resources_to_read", List.of(Map.of("uri", workflowResourceUri))));
+ when(getMCPInteractionClient().readResource("shardingsphere://runtime")).thenReturn(Map.of("status", "available", "capability_fingerprint", "abc"));
+ when(getMCPInteractionClient().readResource(workflowResourceUri)).thenReturn(Map.of("plan_id", "plan-1", "status", "planned"));
+ when(getMCPInteractionClient().call("database_gateway_apply_workflow", applyArguments)).thenReturn(Map.of("status", "awaiting-manual-execution", "response_mode", "manual_only"));
+ when(getMCPInteractionClient().call("database_gateway_validate_workflow", validateArguments)).thenReturn(Map.of("status", "passed", "overall_status", "passed"));
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
createFinalAnswerCompletion(List.of(
MCPInteractionActionNames.READ_RESOURCE, "database_gateway_plan_mask_rule", MCPInteractionActionNames.READ_RESOURCE, "database_gateway_apply_workflow",
"database_gateway_validate_workflow", "database_gateway_execute_query"), 2,
@@ -278,9 +248,9 @@ class LLMMCPConversationRunnerTest {
assertTrue(actual.getAssertionReport().isSuccess());
assertThat(actual.getInteractionTrace().size(), CoreMatchers.is(7));
- verify(mcpInteractionClient).readResource(workflowResourceUri);
- verify(mcpInteractionClient).call("database_gateway_apply_workflow", applyArguments);
- verify(mcpInteractionClient).call("database_gateway_validate_workflow", validateArguments);
+ verify(getMCPInteractionClient()).readResource(workflowResourceUri);
+ verify(getMCPInteractionClient()).call("database_gateway_apply_workflow", applyArguments);
+ verify(getMCPInteractionClient()).call("database_gateway_validate_workflow", validateArguments);
}
@Test
@@ -295,7 +265,7 @@ class LLMMCPConversationRunnerTest {
final Map<String, String> completionContext = Map.of("database", DATABASE_NAME);
final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
final ArgumentCaptor<List<Map<String, Object>>> actualTools = createToolDefinitionsCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(new LLMChatCompletion("",
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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))),
@@ -306,11 +276,11 @@ class LLMMCPConversationRunnerTest {
"context_arguments", completionContext))),
new LLMToolCall("tool-4", "database_gateway_execute_query", JsonUtils.toJsonString(executeQueryArguments))),
"tool-call-response"));
- when(mcpInteractionClient.listPrompts()).thenReturn(Map.of("prompts", List.of(Map.of("name", PROMPT_NAME))));
- when(mcpInteractionClient.getPrompt(PROMPT_NAME, promptArguments)).thenReturn(Map.of("description", "Inspect metadata", "messages", List.of()));
- when(mcpInteractionClient.complete(completionReference, "schema", "pub", completionContext)).thenReturn(Map.of("completion", Map.of("values", List.of("public"))));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ when(getMCPInteractionClient().listPrompts()).thenReturn(Map.of("prompts", List.of(Map.of("name", PROMPT_NAME))));
+ when(getMCPInteractionClient().getPrompt(PROMPT_NAME, promptArguments)).thenReturn(Map.of("description", "Inspect metadata", "messages", List.of()));
+ when(getMCPInteractionClient().complete(completionReference, "schema", "pub", completionContext)).thenReturn(Map.of("completion", Map.of("values", List.of("public"))));
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
createFinalAnswerCompletion(actualToolNames, 2, "final-answer-response"));
final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
@@ -320,10 +290,10 @@ class LLMMCPConversationRunnerTest {
assertThat(actual.getInteractionTrace().get(0).getActionKind(), CoreMatchers.is(MCPInteractionActionNames.PROMPT_LIST_KIND));
assertThat(actual.getInteractionTrace().get(1).getActionKind(), CoreMatchers.is(MCPInteractionActionNames.PROMPT_GET_KIND));
assertThat(actual.getInteractionTrace().get(2).getActionKind(), CoreMatchers.is(MCPInteractionActionNames.COMPLETION_KIND));
- verify(llmChatClient).complete(anyList(), actualTools.capture(), eq("required"), eq(false));
- verify(mcpInteractionClient).listPrompts();
- verify(mcpInteractionClient).getPrompt(PROMPT_NAME, promptArguments);
- verify(mcpInteractionClient).complete(completionReference, "schema", "pub", completionContext);
+ verify(getLLMChatClient()).complete(anyList(), actualTools.capture(), eq("required"), eq(false));
+ verify(getMCPInteractionClient()).listPrompts();
+ verify(getMCPInteractionClient()).getPrompt(PROMPT_NAME, promptArguments);
+ verify(getMCPInteractionClient()).complete(completionReference, "schema", "pub", completionContext);
assertThat(getToolName(actualTools.getValue().get(0)), CoreMatchers.is(MCPInteractionActionNames.LIST_PROMPTS));
}
@@ -332,13 +302,13 @@ class LLMMCPConversationRunnerTest {
final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_search_metadata"));
final LLMMCPConversationRunner actualRunner = createRunner(1);
final ArgumentCaptor<List<Map<String, Object>>> actualTools = createToolDefinitionsCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response"));
final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
- verify(llmChatClient).complete(anyList(), actualTools.capture(), eq("required"), eq(false));
+ verify(getLLMChatClient()).complete(anyList(), actualTools.capture(), eq("required"), eq(false));
assertThat(getToolName(actualTools.getValue().get(0)), CoreMatchers.is("database_gateway_search_metadata"));
assertThat(getRequiredFields(actualTools.getValue().get(0)), CoreMatchers.is(List.of()));
assertThat(getPropertyType(actualTools.getValue().get(0), "database"), CoreMatchers.is("string"));
@@ -358,134 +328,24 @@ class LLMMCPConversationRunnerTest {
final Map<String, Object> readResourceArguments = Map.of("uri", tableResourceUri);
final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
final ArgumentCaptor<List<Map<String, Object>>> actualTools = createToolDefinitionsCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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"));
- when(mcpInteractionClient.call("database_gateway_search_metadata", searchArguments)).thenReturn(Map.of("items", List.of(Map.of("table", TABLE_NAME))));
- when(mcpInteractionClient.readResource(tableResourceUri)).thenReturn(Map.of("table", TABLE_NAME));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(createFinalAnswerCompletion(toolNames, 2, "final-answer-response"));
+ when(getMCPInteractionClient().call("database_gateway_search_metadata", searchArguments)).thenReturn(Map.of("items", List.of(Map.of("table", TABLE_NAME))));
+ when(getMCPInteractionClient().readResource(tableResourceUri)).thenReturn(Map.of("table", TABLE_NAME));
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(createFinalAnswerCompletion(toolNames, 2, "final-answer-response"));
final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertTrue(actual.getAssertionReport().isSuccess());
- verify(llmChatClient, times(3)).complete(anyList(), actualTools.capture(), eq("required"), eq(false));
+ verify(getLLMChatClient(), times(3)).complete(anyList(), actualTools.capture(), eq("required"), eq(false));
assertThat(getToolNames(actualTools.getAllValues().get(0)), CoreMatchers.is(List.of("database_gateway_search_metadata")));
assertThat(getToolNames(actualTools.getAllValues().get(1)), CoreMatchers.is(List.of(MCPInteractionActionNames.READ_RESOURCE)));
assertThat(getToolNames(actualTools.getAllValues().get(2)), CoreMatchers.is(List.of("database_gateway_execute_query")));
}
- @Test
- void assertRunWithUnexpectedTool() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(1);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- new LLMChatCompletion("", List.of(new LLMToolCall("tool-1", "unsupported_tool", "{}")), "tool-call-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("unexpected_tool_requested"));
- assertThat(actual.getInteractionTrace().size(), CoreMatchers.is(1));
- verify(mcpInteractionClient).open();
- verify(mcpInteractionClient, never()).call(anyString(), anyMap());
- verify(mcpInteractionClient).close();
- }
-
- @Test
- void assertRunWithInvalidToolArgumentsJson() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(1);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- new LLMChatCompletion("", List.of(new LLMToolCall("tool-1", "database_gateway_execute_query", "{invalid")), "tool-call-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("invalid_tool_arguments"));
- assertThat(actual.getInteractionTrace().get(0).getTargetName(), CoreMatchers.is("database_gateway_execute_query"));
- verify(mcpInteractionClient).open();
- verify(mcpInteractionClient, never()).call(anyString(), anyMap());
- verify(mcpInteractionClient).close();
- }
-
- @Test
- void assertRunWithEmptyResourceUri() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of(MCPInteractionActionNames.READ_RESOURCE));
- final LLMMCPConversationRunner actualRunner = createRunner(1);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", MCPInteractionActionNames.READ_RESOURCE, Map.of("uri", " "), "tool-call-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("invalid_tool_arguments"));
- assertThat(actual.getInteractionTrace().get(0).getActionKind(), CoreMatchers.is(MCPInteractionActionNames.RESOURCE_READ_KIND));
- verify(mcpInteractionClient, never()).readResource(anyString());
- }
-
- @Test
- void assertRunWithUnsafeSqlAttempted() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(1);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", "database_gateway_execute_query",
- Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "sql", "UPDATE orders SET status = 'DONE'"),
- "tool-call-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("unsafe_sql_attempted"));
- assertThat(actual.getInteractionTrace().get(0).getTargetName(), CoreMatchers.is("database_gateway_execute_query"));
- verify(mcpInteractionClient, never()).call(anyString(), anyMap());
- }
-
- @Test
- void assertRunWithUnsafeExecuteUpdateAttempted() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_update"));
- final LLMMCPConversationRunner actualRunner = createRunner(1);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), 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"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("unsafe_sql_execution_attempted"));
- assertThat(actual.getInteractionTrace().get(0).getTargetName(), CoreMatchers.is("database_gateway_execute_update"));
- verify(mcpInteractionClient, never()).call(anyString(), anyMap());
- }
-
- @Test
- void assertRunWithUnsafeWorkflowExecutionAttempted() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_apply_workflow"));
- final LLMMCPConversationRunner actualRunner = createRunner(1);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", "database_gateway_apply_workflow", Map.of("plan_id", "plan-1", "execution_mode", "review-then-execute"), "tool-call-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("unsafe_workflow_execution_attempted"));
- assertThat(actual.getInteractionTrace().get(0).getTargetName(), CoreMatchers.is("database_gateway_apply_workflow"));
- verify(mcpInteractionClient, never()).call(anyString(), anyMap());
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("unexpectedQueryResultCases")
- void assertRunWithUnexpectedQueryResult(final String caseName, final Map<String, Object> finalAnswerPayload) throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(2);
- final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
- new LLMChatCompletion(JsonUtils.toJsonString(finalAnswerPayload), List.of(), caseName));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("unexpected_query_result"));
- verify(mcpInteractionClient).call("database_gateway_execute_query", executeQueryArguments);
- }
-
@Test
void assertRunAcceptsObjectInteractionSequence() throws IOException, InterruptedException {
final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
@@ -493,10 +353,10 @@ class LLMMCPConversationRunnerTest {
final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
final Map<String, Object> finalAnswerPayload = createFinalAnswerPayload(List.of(), 2);
finalAnswerPayload.put("interactionSequence", List.of(Map.of("tool", "database_gateway_execute_query", "arguments", executeQueryArguments)));
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
new LLMChatCompletion(JsonUtils.toJsonString(finalAnswerPayload), List.of(), "final-answer-response"));
final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
@@ -511,15 +371,15 @@ class LLMMCPConversationRunnerTest {
final LLMMCPConversationRunner actualRunner = createRunner(2);
final Map<String, Object> searchMetadataArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "query", TABLE_NAME);
final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(new LLMChatCompletion("",
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), 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)),
new LLMToolCall("tool-3", "database_gateway_execute_query", JsonUtils.toJsonString(executeQueryArguments))),
"tool-call-response"));
- when(mcpInteractionClient.call("database_gateway_search_metadata", searchMetadataArguments)).thenReturn(Map.of("items", List.of()));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ when(getMCPInteractionClient().call("database_gateway_search_metadata", searchMetadataArguments)).thenReturn(Map.of("items", List.of()));
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
createFinalAnswerCompletion(toolNames, 2, "final-answer-response"));
final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
@@ -534,866 +394,14 @@ class LLMMCPConversationRunnerTest {
final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
final Map<String, Object> finalAnswerPayload = createFinalAnswerPayload(List.of("database_gateway_execute_query"), 2);
finalAnswerPayload.put("query", "SELECT COUNT(*) AS total_orders FROM public.orders");
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
+ when(getLLMChatClient().complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
+ when(getMCPInteractionClient().call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
+ when(getLLMChatClient().complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
new LLMChatCompletion(JsonUtils.toJsonString(finalAnswerPayload), List.of(), "final-answer-response"));
final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
assertTrue(actual.getAssertionReport().isSuccess());
}
-
- private static Stream<Arguments> unexpectedQueryResultCases() {
- return Stream.of(
- Arguments.of("database mismatch", createMutatedFinalAnswerPayload("database", "other_db")),
- Arguments.of("schema mismatch", createMutatedFinalAnswerPayload("schema", "other_schema")),
- Arguments.of("table mismatch", createMutatedFinalAnswerPayload("table", "other_table")),
- Arguments.of("query mismatch", createMutatedFinalAnswerPayload("query", "SELECT * FROM orders")),
- Arguments.of("totalOrders mismatch", createMutatedFinalAnswerPayload("totalOrders", 3)),
- Arguments.of("interactionSequence mismatch", createMutatedFinalAnswerPayload("interactionSequence", List.of(MCPInteractionActionNames.READ_RESOURCE))));
- }
-
- @Test
- void assertRunWithNonNumericExecuteQueryTrace() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(2);
- final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload("NaN"));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
- createFinalAnswerCompletion(List.of("database_gateway_execute_query"), 2, "final-answer-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("unexpected_query_result"));
- }
-
- @Test
- void assertRunWithNonResultSetExecuteQueryTrace() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(2);
- final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(Map.of("result_kind", "update_count", "update_count", 1));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
- createFinalAnswerCompletion(List.of("database_gateway_execute_query"), 2, "final-answer-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("unexpected_query_result"));
- }
-
- @Test
- void assertRunWithMissingRequiredToolCoverage() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(1);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
- assertThat(actual.getInteractionTrace().size(), CoreMatchers.is(0));
- verify(mcpInteractionClient).open();
- verify(mcpInteractionClient).close();
- }
-
- @Test
- void assertRunNamesRemainingToolsWhenCoverageIsMissing() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(2);
- final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response-1"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
- verify(llmChatClient, times(2)).complete(actualMessages.capture(), anyList(), eq("required"), eq(false));
- final List<LLMChatMessage> actualSecondTurnMessages = actualMessages.getAllValues().get(1);
- assertThat(actualSecondTurnMessages.get(actualSecondTurnMessages.size() - 1).getContent(),
- CoreMatchers.containsString("Remaining required MCP tools: database_gateway_execute_query"));
- assertThat(actualSecondTurnMessages.get(actualSecondTurnMessages.size() - 1).getContent(), CoreMatchers.containsString("actual MCP tool_call"));
- assertThat(actualSecondTurnMessages.get(actualSecondTurnMessages.size() - 1).getContent(), CoreMatchers.containsString("database `logic_db`"));
- assertThat(actualSecondTurnMessages.get(actualSecondTurnMessages.size() - 1).getContent(), CoreMatchers.containsString("schema `public`"));
- assertThat(actualSecondTurnMessages.get(actualSecondTurnMessages.size() - 1).getContent(), CoreMatchers.containsString("sql `SELECT COUNT(*) AS total_orders FROM orders`"));
- }
-
- @Test
- void assertRunNamesExactResourceUriWhenResourceCoverageIsMissing() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of(MCPInteractionActionNames.READ_RESOURCE));
- final LLMMCPConversationRunner actualRunner = createRunner(2);
- final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response-1"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
- verify(llmChatClient, times(2)).complete(actualMessages.capture(), anyList(), eq("required"), eq(false));
- final List<LLMChatMessage> actualSecondTurnMessages = actualMessages.getAllValues().get(1);
- final String actualRetryInstruction = actualSecondTurnMessages.get(actualSecondTurnMessages.size() - 1).getContent();
- assertThat(actualRetryInstruction, CoreMatchers.containsString("Remaining required MCP tools: mcp_read_resource"));
- assertThat(actualRetryInstruction, CoreMatchers.containsString("exact shardingsphere:// URI"));
- assertThat(actualRetryInstruction, CoreMatchers.containsString("do not invent abbreviated URI strings"));
- }
-
- @Test
- void assertRunNamesPreviewModeWhenUpdateCoverageIsMissing() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_update"));
- final LLMMCPConversationRunner actualRunner = createRunner(2);
- final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response-1"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
- verify(llmChatClient, times(2)).complete(actualMessages.capture(), anyList(), eq("required"), eq(false));
- final List<LLMChatMessage> actualSecondTurnMessages = actualMessages.getAllValues().get(1);
- final String actualRetryInstruction = actualSecondTurnMessages.get(actualSecondTurnMessages.size() - 1).getContent();
- assertThat(actualRetryInstruction, CoreMatchers.containsString("Remaining required MCP tools: database_gateway_execute_update"));
- assertThat(actualRetryInstruction, CoreMatchers.containsString("database `logic_db`"));
- assertThat(actualRetryInstruction, CoreMatchers.containsString("schema `public`"));
- assertThat(actualRetryInstruction, CoreMatchers.containsString("execution_mode=preview"));
- assertThat(actualRetryInstruction, CoreMatchers.containsString("do not use execution_mode=execute"));
- }
-
- @Test
- void assertRunNamesPlanIdRuleWhenPlanningCoverageIsMissing() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_plan_mask_rule"));
- final LLMMCPConversationRunner actualRunner = createRunner(2);
- final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response-1"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
- verify(llmChatClient, times(2)).complete(actualMessages.capture(), anyList(), eq("required"), eq(false));
- final List<LLMChatMessage> actualSecondTurnMessages = actualMessages.getAllValues().get(1);
- final String actualRetryInstruction = actualSecondTurnMessages.get(actualSecondTurnMessages.size() - 1).getContent();
- assertThat(actualRetryInstruction, CoreMatchers.containsString("Remaining required MCP tools: database_gateway_plan_mask_rule"));
- assertThat(actualRetryInstruction, CoreMatchers.containsString("For a new database_gateway_plan_* call"));
- assertThat(actualRetryInstruction, CoreMatchers.containsString("omit plan_id"));
- }
-
- @Test
- void assertRunRemovesInitialPlanningPlanId() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_plan_mask_rule"));
- final LLMMCPConversationRunner actualRunner = createRunner(1);
- final Map<String, Object> rawPlanArguments = Map.of("plan_id", "1", "database", DATABASE_NAME, "schema", SCHEMA_NAME, "table", TABLE_NAME, "column", "status");
- final Map<String, Object> expectedPlanArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "table", TABLE_NAME, "column", "status");
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", "database_gateway_plan_mask_rule", rawPlanArguments, "plan-response"));
- when(mcpInteractionClient.call("database_gateway_plan_mask_rule", expectedPlanArguments)).thenReturn(Map.of("plan_id", "plan-1"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
- assertThat(actual.getInteractionTrace().get(0).getArguments(), CoreMatchers.is(expectedPlanArguments));
- assertThat(actual.getInteractionTrace().get(0).getActionOrigin(), CoreMatchers.is(MCPInteractionTraceRecord.HARNESS_ARGUMENT_NORMALIZATION_ORIGIN));
- verify(mcpInteractionClient).call("database_gateway_plan_mask_rule", expectedPlanArguments);
- }
-
- @Test
- void assertRunKeepsCurrentPlanningPlanId() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_plan_mask_rule"));
- final LLMMCPConversationRunner actualRunner = createRunner(2);
- final Map<String, Object> initialPlanArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "table", TABLE_NAME, "column", "status");
- final Map<String, Object> currentPlanArguments = Map.of("plan_id", "plan-1", "database", DATABASE_NAME, "schema", SCHEMA_NAME, "table", TABLE_NAME, "column", "status");
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", "database_gateway_plan_mask_rule", initialPlanArguments, "initial-plan-response"));
- when(llmChatClient.complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
- createToolCallCompletion("tool-2", "database_gateway_plan_mask_rule", currentPlanArguments, "current-plan-response"));
- when(mcpInteractionClient.call("database_gateway_plan_mask_rule", initialPlanArguments)).thenReturn(Map.of("plan_id", "plan-1", "next_actions", List.of(Map.of(
- "type", "tool_call",
- "tool_name", "database_gateway_plan_mask_rule",
- "arguments", currentPlanArguments))));
- when(mcpInteractionClient.call("database_gateway_plan_mask_rule", currentPlanArguments)).thenReturn(Map.of("plan_id", "plan-1"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
- assertThat(actual.getInteractionTrace().get(1).getArguments(), CoreMatchers.is(currentPlanArguments));
- assertThat(actual.getInteractionTrace().get(1).getActionOrigin(), CoreMatchers.is(MCPInteractionTraceRecord.MODEL_TOOL_CALL_ORIGIN));
- verify(mcpInteractionClient).call("database_gateway_plan_mask_rule", currentPlanArguments);
- }
-
- @Test
- void assertRunRequiresToolCallWhenExpectedExecuteQueryIsReturnedAsText() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(3);
- final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- new LLMChatCompletion(QUERY, List.of(), "query-text-response"),
- createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "query-tool-response"));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
- createFinalAnswerCompletion(List.of("database_gateway_execute_query"), 2, "final-answer-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertTrue(actual.getAssertionReport().isSuccess());
- assertThat(actual.getInteractionTrace().size(), CoreMatchers.is(1));
- assertThat(actual.getInteractionTrace().get(0).getTargetName(), CoreMatchers.is("database_gateway_execute_query"));
- assertThat(actual.getInteractionTrace().get(0).getActionOrigin(), CoreMatchers.is(MCPInteractionTraceRecord.MODEL_TOOL_CALL_ORIGIN));
- verify(mcpInteractionClient).call("database_gateway_execute_query", executeQueryArguments);
- verify(llmChatClient, times(2)).complete(anyList(), anyList(), eq("required"), eq(false));
- }
-
- @Test
- void assertRunNamesLatestPlanIdWhenWorkflowCoverageIsMissing() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_plan_mask_rule", "database_gateway_apply_workflow"));
- final LLMMCPConversationRunner actualRunner = createRunner(3);
- final Map<String, Object> planArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "table", TABLE_NAME, "column", "status");
- final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), 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(mcpInteractionClient.call("database_gateway_plan_mask_rule", planArguments)).thenReturn(Map.of("plan_id", "plan-1"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
- verify(llmChatClient, times(3)).complete(actualMessages.capture(), anyList(), eq("required"), eq(false));
- final List<LLMChatMessage> actualThirdTurnMessages = actualMessages.getAllValues().get(2);
- final String actualRetryInstruction = actualThirdTurnMessages.get(actualThirdTurnMessages.size() - 1).getContent();
- assertThat(actualRetryInstruction, CoreMatchers.containsString("Remaining required MCP tools: database_gateway_apply_workflow"));
- assertThat(actualRetryInstruction, CoreMatchers.containsString("set plan_id `plan-1`"));
- assertThat(actualRetryInstruction, CoreMatchers.containsString("do not use placeholder text `plan_id`"));
- }
-
- @Test
- void assertRunRetriesExpectedExecuteQueryBeforeFinalAnswer() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(4);
- final Map<String, Object> wrongQueryArguments = Map.of("database", "analytics_db", "schema", SCHEMA_NAME, "sql", QUERY, "max_rows", 10);
- final Map<String, Object> expectedQueryArguments = createExecuteQueryArguments(QUERY);
- final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", "database_gateway_execute_query", wrongQueryArguments, "wrong-query-response"));
- when(mcpInteractionClient.call("database_gateway_execute_query", wrongQueryArguments)).thenReturn(createResultSetPayload(4));
- when(llmChatClient.complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
- createToolCallCompletion("tool-2", "database_gateway_execute_query", expectedQueryArguments, "expected-query-response"));
- when(mcpInteractionClient.call("database_gateway_execute_query", expectedQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
- createFinalAnswerCompletion(List.of("database_gateway_execute_query"), 2, "final-answer-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertTrue(actual.getAssertionReport().isSuccess());
- verify(llmChatClient).complete(actualMessages.capture(), anyList(), eq("auto"), eq(false));
- final List<LLMChatMessage> actualAutoTurnMessages = actualMessages.getValue();
- 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(), CoreMatchers.is(2));
- }
-
- @Test
- void assertRunRecoversCompletionArguments() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of(MCPInteractionActionNames.COMPLETE, "database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(4);
- final Map<String, Object> completionReference = Map.of("type", "ref/prompt", "name", PROMPT_NAME);
- final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", MCPInteractionActionNames.COMPLETE, Map.of("argument_name", "schema", "argument_value", "pub"), "bad-completion-response"),
- new LLMChatCompletion("",
- List.of(
- new LLMToolCall("tool-2", MCPInteractionActionNames.COMPLETE, JsonUtils.toJsonString(Map.of(
- "reference", completionReference,
- "argument_name", "schema",
- "argument_value", "pub"))),
- new LLMToolCall("tool-3", "database_gateway_execute_query", JsonUtils.toJsonString(executeQueryArguments))),
- "retry-completion-response"));
- when(mcpInteractionClient.complete(completionReference, "schema", "pub", Map.of())).thenReturn(Map.of("completion", Map.of("values", List.of(SCHEMA_NAME))));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
- createFinalAnswerCompletion(List.of(MCPInteractionActionNames.COMPLETE, "database_gateway_execute_query"), 2, "final-answer-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertTrue(actual.getAssertionReport().isSuccess());
- assertThat(actual.getInteractionTrace().size(), CoreMatchers.is(3));
- assertThat(actual.getInteractionTrace().get(0).getStructuredContent().get("error_code"), CoreMatchers.is("invalid_tool_arguments"));
- verify(mcpInteractionClient).complete(completionReference, "schema", "pub", Map.of());
- }
-
- @Test
- void assertRunNormalizesCompletionArgumentName() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of(MCPInteractionActionNames.COMPLETE, "database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(3);
- final Map<String, Object> completionReference = Map.of("type", "ref/prompt", "name", PROMPT_NAME);
- final Map<String, String> completionContext = Map.of("database", DATABASE_NAME);
- final Map<String, Object> completionArguments = new LinkedHashMap<>(4, 1F);
- completionArguments.put("reference", completionReference);
- completionArguments.put("reference argument_name", "schema");
- completionArguments.put("argument_value", "pub");
- completionArguments.put("context_arguments", completionContext);
- final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", MCPInteractionActionNames.COMPLETE, completionArguments, "completion-response"),
- createToolCallCompletion("tool-2", "database_gateway_execute_query", executeQueryArguments, "query-response"));
- when(mcpInteractionClient.complete(completionReference, "schema", "pub", completionContext)).thenReturn(Map.of("completion", Map.of("values", List.of(SCHEMA_NAME))));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
- createFinalAnswerCompletion(List.of(MCPInteractionActionNames.COMPLETE, "database_gateway_execute_query"), 2, "final-answer-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertTrue(actual.getAssertionReport().isSuccess());
- assertThat(actual.getInteractionTrace().size(), CoreMatchers.is(2));
- verify(mcpInteractionClient).complete(completionReference, "schema", "pub", completionContext);
- }
-
- @Test
- void assertRunDefaultsCompletionReferenceFromPrompt() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of(MCPInteractionActionNames.GET_PROMPT, MCPInteractionActionNames.COMPLETE, "database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(5);
- final Map<String, Object> getPromptArguments = Map.of("name", PROMPT_NAME);
- final Map<String, Object> completionReference = Map.of("type", "ref/prompt", "name", PROMPT_NAME);
- final Map<String, Object> completionArguments = Map.of("argument_name", "schema", "argument_value", "pub");
- final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- final ArgumentCaptor<List<Map<String, Object>>> actualTools = createToolDefinitionsCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), 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"));
- when(mcpInteractionClient.getPrompt(PROMPT_NAME, Map.of())).thenReturn(Map.of("messages", List.of(), "next_actions", List.of(Map.of(
- "type", "completion",
- "arguments", Map.of("reference", completionReference, "argument_name", "schema", "argument_value", "pub")))));
- when(mcpInteractionClient.complete(completionReference, "schema", "pub", Map.of())).thenReturn(Map.of("completion", Map.of("values", List.of(SCHEMA_NAME))));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
- createFinalAnswerCompletion(List.of(MCPInteractionActionNames.GET_PROMPT, MCPInteractionActionNames.COMPLETE, "database_gateway_execute_query"), 2, "final-answer-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertTrue(actual.getAssertionReport().isSuccess());
- verify(llmChatClient, times(3)).complete(anyList(), actualTools.capture(), eq("required"), eq(false));
- assertThat(getToolNames(actualTools.getAllValues().get(1)), CoreMatchers.is(List.of(MCPInteractionActionNames.COMPLETE)));
- assertThat(getToolNames(actualTools.getAllValues().get(2)), CoreMatchers.is(List.of("database_gateway_execute_query")));
- verify(mcpInteractionClient).complete(completionReference, "schema", "pub", Map.of());
- }
-
- @Test
- void assertRunPromptsImmediateResourceNextAction() throws IOException, InterruptedException {
- final List<String> toolNames = List.of(MCPInteractionActionNames.READ_RESOURCE, "database_gateway_execute_query");
- final LLME2EScenario actualScenario = createScenario(toolNames);
- final LLMMCPConversationRunner actualRunner = createRunner(4);
- final Map<String, Object> runtimeArguments = Map.of("uri", "shardingsphere://runtime");
- final Map<String, Object> capabilitiesArguments = Map.of("uri", RESOURCE_URI);
- final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
- final ArgumentCaptor<List<Map<String, Object>>> actualTools = createToolDefinitionsCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), 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"));
- when(mcpInteractionClient.readResource("shardingsphere://runtime")).thenReturn(Map.of("next_actions", List.of(Map.of(
- "type", "resource_read",
- "resource_uri", RESOURCE_URI))));
- when(mcpInteractionClient.readResource(RESOURCE_URI)).thenReturn(Map.of("response_mode", "capabilities"));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
- createFinalAnswerCompletion(List.of(MCPInteractionActionNames.READ_RESOURCE, "database_gateway_execute_query"), 2, "final-answer-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertTrue(actual.getAssertionReport().isSuccess());
- verify(llmChatClient, times(3)).complete(actualMessages.capture(), actualTools.capture(), eq("required"), eq(false));
- assertTrue(containsMessage(actualMessages.getAllValues().get(1), "Call mcp_read_resource with uri `" + RESOURCE_URI + "` now"));
- assertThat(getToolNames(actualTools.getAllValues().get(1)), CoreMatchers.is(List.of(MCPInteractionActionNames.READ_RESOURCE)));
- }
-
- @Test
- void assertRunPromptsImmediateToolNextAction() throws IOException, InterruptedException {
- final List<String> toolNames = List.of("database_gateway_plan_mask_rule", "database_gateway_apply_workflow", "database_gateway_execute_query");
- final LLME2EScenario actualScenario = createScenario(toolNames);
- final LLMMCPConversationRunner actualRunner = createRunner(4);
- final Map<String, Object> planArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "table", TABLE_NAME, "column", "status");
- final Map<String, Object> applyArguments = Map.of("plan_id", "plan-1", "execution_mode", "preview");
- final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
- final ArgumentCaptor<List<Map<String, Object>>> actualTools = createToolDefinitionsCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", "database_gateway_plan_mask_rule", planArguments, "plan-response"),
- createToolCallCompletion("tool-2", "database_gateway_apply_workflow", Map.of("plan_id", "1", "execution_mode", "preview"), "apply-response"),
- createToolCallCompletion("tool-3", "database_gateway_execute_query", executeQueryArguments, "query-response"));
- when(mcpInteractionClient.call("database_gateway_plan_mask_rule", planArguments)).thenReturn(Map.of("plan_id", "plan-1", "next_actions", List.of(Map.of(
- "type", "tool_call",
- "tool_name", "database_gateway_apply_workflow",
- "arguments", applyArguments))));
- when(mcpInteractionClient.call("database_gateway_apply_workflow", applyArguments)).thenReturn(Map.of("response_mode", "preview"));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
- createFinalAnswerCompletion(toolNames, 2, "final-answer-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertTrue(actual.getAssertionReport().isSuccess());
- assertThat(actual.getInteractionTrace().get(1).getActionOrigin(), CoreMatchers.is(MCPInteractionTraceRecord.HARNESS_ARGUMENT_NORMALIZATION_ORIGIN));
- verify(llmChatClient, times(3)).complete(actualMessages.capture(), actualTools.capture(), eq("required"), eq(false));
- assertTrue(containsMessage(actualMessages.getAllValues().get(1), "Call `database_gateway_apply_workflow` now with exactly these arguments"));
- assertTrue(containsMessage(actualMessages.getAllValues().get(1), "\"plan_id\":\"plan-1\""));
- assertThat(getToolNames(actualTools.getAllValues().get(1)), CoreMatchers.is(List.of("database_gateway_apply_workflow")));
- }
-
- @Test
- void assertRunPromptsExactResourceAfterList() throws IOException, InterruptedException {
- final List<String> toolNames = List.of(MCPInteractionActionNames.LIST_RESOURCES, MCPInteractionActionNames.READ_RESOURCE, "database_gateway_execute_query");
- final String tableResourceUri = "shardingsphere://databases/logic_db/schemas/public/tables/orders";
- final LLME2EScenario actualScenario = createScenario(toolNames, "Read exact `" + tableResourceUri + "` before querying.");
- final LLMMCPConversationRunner actualRunner = createRunner(4);
- final Map<String, Object> readResourceArguments = Map.of("uri", "{description=Resource URI to read., type=string}");
- final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), 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"));
- when(mcpInteractionClient.listResources()).thenReturn(Map.of("resources", List.of(Map.of("uri", RESOURCE_URI))));
- when(mcpInteractionClient.readResource(tableResourceUri)).thenReturn(Map.of("found", true));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
- createFinalAnswerCompletion(toolNames, 2, "final-answer-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertTrue(actual.getAssertionReport().isSuccess());
- verify(llmChatClient, times(3)).complete(actualMessages.capture(), anyList(), eq("required"), eq(false));
- assertTrue(containsMessage(actualMessages.getAllValues().get(1), "Use exactly `" + tableResourceUri + "` as uri"));
- assertTrue(containsMessage(actualMessages.getAllValues().get(1), "do not copy parameter schema"));
- }
-
- @Test
- void assertRunPromptsLiveResourceAfterStaleResourceMiss() throws IOException, InterruptedException {
- final List<String> toolNames = List.of(MCPInteractionActionNames.READ_RESOURCE, "database_gateway_execute_query");
- final String staleResourceUri = "shardingsphere://databases/unknown/schemas/unknown/tables/orders";
- final String tableResourceUri = "shardingsphere://databases/logic_db/schemas/public/tables/orders";
- final LLME2EScenario actualScenario = createScenario(toolNames,
- "Read stale `" + staleResourceUri + "`, recover by reading exact live table resource `" + tableResourceUri + "`, then query.");
- final LLMMCPConversationRunner actualRunner = createRunner(4);
- final Map<String, Object> staleReadArguments = Map.of("uri", staleResourceUri);
- final Map<String, Object> liveReadArguments = Map.of("uri", tableResourceUri);
- final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), 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"));
- when(mcpInteractionClient.readResource(staleResourceUri)).thenReturn(Map.of(
- "found", false,
- "next_actions", List.of(Map.of(
- "type", "resource_read",
- "resource_uri", "shardingsphere://databases/unknown/schemas/unknown/tables"))));
- when(mcpInteractionClient.readResource(tableResourceUri)).thenReturn(Map.of("found", true));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
- createFinalAnswerCompletion(toolNames, 2, "final-answer-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertTrue(actual.getAssertionReport().isSuccess());
- verify(llmChatClient, times(3)).complete(actualMessages.capture(), anyList(), eq("required"), eq(false));
- assertTrue(containsMessage(actualMessages.getAllValues().get(1), "Use exactly `" + tableResourceUri + "` as uri"));
- assertThat(actual.getInteractionTrace().get(1).getArguments().get("uri"), CoreMatchers.is(tableResourceUri));
- }
-
- @Test
- void assertRunFollowsPendingNextActionBeforeFinalAnswer() throws IOException, InterruptedException {
- final List<String> toolNames = List.of("database_gateway_apply_workflow", "database_gateway_execute_query");
- final LLME2EScenario actualScenario = createScenario(toolNames);
- final LLMMCPConversationRunner actualRunner = createRunner(3);
- final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- final Map<String, Object> previewArguments = Map.of("plan_id", "plan-1", "execution_mode", "preview");
- final Map<String, Object> manualArguments = Map.of("plan_id", "plan-1", "execution_mode", "manual-only");
- final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), 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"));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(mcpInteractionClient.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(llmChatClient.complete(anyList(), anyList(), eq("auto"), eq(false))).thenReturn(
- createToolCallCompletion("tool-3", "database_gateway_apply_workflow", manualArguments, "manual-response"));
- when(mcpInteractionClient.call("database_gateway_apply_workflow", manualArguments)).thenReturn(Map.of("response_mode", "manual_only"));
- when(llmChatClient.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"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertTrue(actual.getAssertionReport().isSuccess());
- verify(llmChatClient).complete(actualMessages.capture(), anyList(), eq("auto"), eq(false));
- assertTrue(containsMessage(actualMessages.getValue(), "Call `database_gateway_apply_workflow` now with exactly these arguments"));
- }
-
- @Test
- void assertRunIgnoresSideEffectExecutionNextActionBeforeReadOnlyVerification() throws IOException, InterruptedException {
- final List<String> toolNames = List.of("database_gateway_execute_update", "database_gateway_execute_query");
- final LLME2EScenario actualScenario = createScenario(toolNames);
- final LLMMCPConversationRunner actualRunner = createRunner(3);
- final Map<String, Object> previewArguments = Map.of(
- "database", DATABASE_NAME,
- "schema", SCHEMA_NAME,
- "sql", "UPDATE orders SET status = status WHERE order_id = -1",
- "execution_mode", "preview");
- final Map<String, Object> executeQueryArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "sql", QUERY);
- final Map<String, Object> misroutedQueryArguments = Map.of(
- "database", DATABASE_NAME,
- "schema", SCHEMA_NAME,
- "sql", QUERY,
- "execution_mode", "preview");
- final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
- final ArgumentCaptor<List<Map<String, Object>>> actualTools = createToolDefinitionsCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", "database_gateway_execute_update", previewArguments, "preview-response"),
- createToolCallCompletion("tool-2", "database_gateway_execute_update", misroutedQueryArguments, "query-response"));
- when(mcpInteractionClient.call("database_gateway_execute_update", previewArguments)).thenReturn(Map.of("response_mode", "preview", "next_actions", List.of(Map.of(
- "type", "tool_call",
- "tool_name", "database_gateway_execute_update",
- "arguments", Map.of("database", DATABASE_NAME, "sql", "UPDATE orders SET status = status WHERE order_id = -1", "execution_mode", "execute")))));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
- createFinalAnswerCompletion(toolNames, 2, "final-answer-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertTrue(actual.getAssertionReport().isSuccess());
- verify(llmChatClient, times(2)).complete(actualMessages.capture(), actualTools.capture(), eq("required"), eq(false));
- final List<LLMChatMessage> actualSecondTurnMessages = actualMessages.getAllValues().get(1);
- assertTrue(containsMessage(actualSecondTurnMessages, "side-effect execution next_actions"));
- assertThat(getToolNames(actualTools.getAllValues().get(1)), CoreMatchers.is(List.of("database_gateway_execute_query")));
- assertThat(actual.getInteractionTrace().get(1).getTargetName(), CoreMatchers.is("database_gateway_execute_query"));
- assertThat(actual.getInteractionTrace().get(1).getActionOrigin(), CoreMatchers.is(MCPInteractionTraceRecord.HARNESS_ARGUMENT_NORMALIZATION_ORIGIN));
- verify(mcpInteractionClient, never()).call("database_gateway_execute_update", misroutedQueryArguments);
- }
-
- @Test
- void assertRunRoutesReadOnlyCallAfterSideEffectPreviewInSameCompletion() throws IOException, InterruptedException {
- final List<String> toolNames = List.of("database_gateway_execute_update", "database_gateway_execute_query");
- final LLME2EScenario actualScenario = createScenario(toolNames);
- final LLMMCPConversationRunner actualRunner = createRunner(3);
- final Map<String, Object> previewArguments = Map.of(
- "database", DATABASE_NAME,
- "schema", SCHEMA_NAME,
- "sql", "UPDATE orders SET status = status WHERE order_id = -1",
- "execution_mode", "preview");
- final Map<String, Object> executeQueryArguments = Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "sql", QUERY);
- final Map<String, Object> misroutedQueryArguments = Map.of(
- "database", DATABASE_NAME,
- "schema", SCHEMA_NAME,
- "sql", QUERY,
- "execution_mode", "preview");
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(new LLMChatCompletion("",
- List.of(
- new LLMToolCall("tool-1", "database_gateway_execute_update", JsonUtils.toJsonString(previewArguments)),
- new LLMToolCall("tool-2", "database_gateway_execute_update", JsonUtils.toJsonString(misroutedQueryArguments))),
- "compound-tool-response"));
- when(mcpInteractionClient.call("database_gateway_execute_update", previewArguments)).thenReturn(Map.of("response_mode", "preview", "next_actions", List.of(Map.of(
- "type", "tool_call",
- "tool_name", "database_gateway_execute_update",
- "arguments", Map.of("database", DATABASE_NAME, "sql", "UPDATE orders SET status = status WHERE order_id = -1", "execution_mode", "execute")))));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
- createFinalAnswerCompletion(toolNames, 2, "final-answer-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertTrue(actual.getAssertionReport().isSuccess());
- assertThat(actual.getInteractionTrace().get(0).getTargetName(), CoreMatchers.is("database_gateway_execute_update"));
- assertThat(actual.getInteractionTrace().get(1).getTargetName(), CoreMatchers.is("database_gateway_execute_query"));
- assertThat(actual.getInteractionTrace().get(1).getActionOrigin(), CoreMatchers.is(MCPInteractionTraceRecord.HARNESS_ARGUMENT_NORMALIZATION_ORIGIN));
- verify(mcpInteractionClient, never()).call("database_gateway_execute_update", misroutedQueryArguments);
- }
-
- @Test
- void assertRunCompactsManualArtifactsBeforeReadOnlyVerification() throws IOException, InterruptedException {
- final List<String> toolNames = List.of("database_gateway_apply_workflow", "database_gateway_execute_query");
- final LLME2EScenario actualScenario = createScenario(toolNames);
- final LLMMCPConversationRunner actualRunner = createRunner(3);
- final Map<String, Object> manualArguments = Map.of("plan_id", "plan-1", "execution_mode", "manual-only");
- final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- final ArgumentCaptor<List<LLMChatMessage>> actualMessages = createChatMessagesCaptor();
- final ArgumentCaptor<List<Map<String, Object>>> actualTools = createToolDefinitionsCaptor();
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", "database_gateway_apply_workflow", manualArguments, "manual-response"),
- createToolCallCompletion("tool-2", "database_gateway_execute_query", executeQueryArguments, "query-response"));
- when(mcpInteractionClient.call("database_gateway_apply_workflow", manualArguments)).thenReturn(Map.of(
- "response_mode", "manual_only",
- "manual_artifact_summary", Map.of("distsql_artifact_count", 1),
- "manual_artifacts", List.of(Map.of("distsql_artifacts", List.of(Map.of("sql", "CREATE MASK RULE orders SECRET")))),
- "next_actions", List.of(Map.of(
- "type", "ask_user",
- "reason", "Confirm manual artifacts were executed."))));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
- createFinalAnswerCompletion(toolNames, 2, "final-answer-response"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertTrue(actual.getAssertionReport().isSuccess());
- verify(llmChatClient, times(2)).complete(actualMessages.capture(), actualTools.capture(), eq("required"), eq(false));
- final List<LLMChatMessage> actualSecondTurnMessages = actualMessages.getAllValues().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.getAllValues().get(1)), CoreMatchers.is(List.of("database_gateway_execute_query")));
- }
-
- @Test
- void assertRunWithExecuteQueryErrorPayloadIgnoredForCoverage() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(2);
- final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), 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(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(Map.of("error_code", "tool_failed"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
- verify(llmChatClient, times(2)).complete(anyList(), anyList(), eq("required"), eq(false));
- }
-
- @Test
- void assertRunWithInvalidFinalJson() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(3);
- final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenReturn(createResultSetPayload(2));
- when(llmChatClient.complete(anyList(), eq(List.of()), eq("none"), eq(true))).thenReturn(
- new LLMChatCompletion("not-json", List.of(), "invalid-final-response-1"),
- new LLMChatCompletion("still-not-json", List.of(), "invalid-final-response-2"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("invalid_final_json"));
- assertThat(actual.getRawModelOutputs(), CoreMatchers.is(List.of("tool-call-response", "invalid-final-response-1", "invalid-final-response-2")));
- verify(mcpInteractionClient).open();
- verify(mcpInteractionClient).close();
- }
-
- @Test
- void assertRunWithMcpActionFailure() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(1);
- final Map<String, Object> executeQueryArguments = createExecuteQueryArguments(QUERY);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- createToolCallCompletion("tool-1", "database_gateway_execute_query", executeQueryArguments, "tool-call-response"));
- when(mcpInteractionClient.call("database_gateway_execute_query", executeQueryArguments)).thenThrow(new IOException("boom"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("mcp_runtime_unavailable"));
- }
-
- @Test
- void assertRunWithMcpRuntimeUnavailable() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(1);
- doThrow(new IOException("boom")).when(mcpInteractionClient).open();
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("mcp_runtime_unavailable"));
- verify(mcpInteractionClient).open();
- verify(mcpInteractionClient).close();
- }
-
- @Test
- void assertRunWithModelRequestIOException() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(1);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenThrow(new IOException("http 500"));
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("model_service_unavailable"));
- verify(mcpInteractionClient).open();
- verify(mcpInteractionClient).close();
- }
-
- @Test
- void assertRunWithInterruptedConversation() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(1);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenThrow(new InterruptedException("boom"));
- try {
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("model_service_unavailable"));
- assertTrue(Thread.currentThread().isInterrupted());
- } finally {
- Thread.interrupted();
- }
- }
-
- @Test
- void assertRunWithModelServiceUnavailable() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(1);
- doThrow(new IllegalStateException("Model service is not ready for `ggml-org/Qwen3-1.7B-GGUF:Q4_K_M`.")).when(llmChatClient).waitUntilReady();
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("model_service_unavailable"));
- verify(mcpInteractionClient, never()).open();
- verify(mcpInteractionClient).close();
- }
-
- @Test
- void assertRunIgnoresCloseIOException() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(1);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response"));
- doThrow(new IOException("close failed")).when(mcpInteractionClient).close();
-
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
- verify(mcpInteractionClient).close();
- }
-
- @Test
- void assertRunRestoresInterruptOnCloseFailure() throws IOException, InterruptedException {
- final LLME2EScenario actualScenario = createScenario(List.of("database_gateway_execute_query"));
- final LLMMCPConversationRunner actualRunner = createRunner(1);
- when(llmChatClient.complete(anyList(), anyList(), eq("required"), eq(false))).thenReturn(
- new LLMChatCompletion("I already know the answer.", List.of(), "direct-answer-response"));
- doThrow(new InterruptedException("close interrupted")).when(mcpInteractionClient).close();
- try {
- final LLME2EArtifactBundle actual = actualRunner.run(actualScenario);
-
- assertThat(actual.getAssertionReport().getFailureType(), CoreMatchers.is("missing_required_tool_coverage"));
- assertTrue(Thread.currentThread().isInterrupted());
- } finally {
- Thread.interrupted();
- }
- }
-
- private LLMMCPConversationRunner createRunner(final int maxTurns) {
- return new LLMMCPConversationRunner(maxTurns, llmChatClient, mcpInteractionClient);
- }
-
- private LLME2EScenario createScenario(final List<String> toolNames) {
- return createScenario(toolNames, "user-prompt");
- }
-
- private LLME2EScenario createScenario(final List<String> toolNames, final String userPrompt) {
- LLMStructuredAnswer expectedAnswer = new LLMStructuredAnswer(DATABASE_NAME, SCHEMA_NAME, TABLE_NAME, QUERY, 2, toolNames);
- return new LLME2EScenario("scenario-id", "system-prompt", userPrompt, expectedAnswer, toolNames, toolNames);
- }
-
- private Map<String, Object> createExecuteQueryArguments(final String sql) {
- return Map.of("database", DATABASE_NAME, "schema", SCHEMA_NAME, "sql", sql, "max_rows", 10);
- }
-
- private LLMChatCompletion createToolCallCompletion(final String toolCallId, final String toolName, final Map<String, Object> arguments, final String rawResponse) {
- return new LLMChatCompletion("", List.of(new LLMToolCall(toolCallId, toolName, JsonUtils.toJsonString(arguments))), rawResponse);
- }
-
- private LLMChatCompletion createFinalAnswerCompletion(final List<String> interactionSequence, final Object totalOrders, final String rawResponse) {
- return new LLMChatCompletion(JsonUtils.toJsonString(createFinalAnswerPayload(interactionSequence, totalOrders)), List.of(), rawResponse);
- }
-
- private boolean containsMessage(final List<LLMChatMessage> messages, final String expectedContent) {
- return messages.stream().map(LLMChatMessage::getContent).anyMatch(each -> each.contains(expectedContent));
- }
-
- private Map<String, Object> createResultSetPayload(final Object totalOrders) {
- return Map.of("result_kind", "result_set", "rows", List.of(List.of(totalOrders)));
- }
-
- private Map<String, Object> createFinalAnswerPayload(final List<String> interactionSequence, final Object totalOrders) {
- Map<String, Object> result = new LinkedHashMap<>(6, 1F);
- result.put("database", DATABASE_NAME);
- result.put("schema", SCHEMA_NAME);
- result.put("table", TABLE_NAME);
- result.put("query", QUERY);
- result.put("totalOrders", totalOrders);
- result.put("interactionSequence", interactionSequence);
- return result;
- }
-
- private static Map<String, Object> createMutatedFinalAnswerPayload(final String fieldName, final Object fieldValue) {
- Map<String, Object> result = new LinkedHashMap<>(6, 1F);
- result.put("database", DATABASE_NAME);
- result.put("schema", SCHEMA_NAME);
- result.put("table", TABLE_NAME);
- result.put("query", QUERY);
- result.put("totalOrders", 2);
- result.put("interactionSequence", List.of("database_gateway_execute_query"));
- result.put(fieldName, fieldValue);
- return result;
- }
-
- @SuppressWarnings({"unchecked", "rawtypes"})
- private ArgumentCaptor<List<Map<String, Object>>> createToolDefinitionsCaptor() {
- return ArgumentCaptor.forClass((Class) List.class);
- }
-
- @SuppressWarnings({"unchecked", "rawtypes"})
- private ArgumentCaptor<List<LLMChatMessage>> createChatMessagesCaptor() {
- return ArgumentCaptor.forClass((Class) List.class);
- }
-
- @SuppressWarnings("unchecked")
- private Map<String, Object> getFunction(final Map<String, Object> toolDefinition) {
- return (Map<String, Object>) toolDefinition.get("function");
- }
-
- private String getToolName(final Map<String, Object> toolDefinition) {
- return String.valueOf(getFunction(toolDefinition).get("name"));
- }
-
- private List<String> getToolNames(final List<Map<String, Object>> toolDefinitions) {
- return toolDefinitions.stream().map(this::getToolName).toList();
- }
-
- private List<String> getRequiredFields(final Map<String, Object> toolDefinition) {
- return castToStringList(getMap(getFunction(toolDefinition).get("parameters")).get("required"));
- }
-
- private String getPropertyType(final Map<String, Object> propertyDefinition) {
- return String.valueOf(propertyDefinition.get("type"));
- }
-
- private String getPropertyType(final Map<String, Object> toolDefinition, final String propertyName) {
- return getPropertyType(getPropertyDefinition(toolDefinition, propertyName));
- }
-
- private String getNestedPropertyType(final Map<String, Object> toolDefinition, final String propertyName, final String nestedPropertyName) {
- return getPropertyType(getMap(getPropertyDefinition(toolDefinition, propertyName).get(nestedPropertyName)));
- }
-
- private Map<String, Object> getPropertyDefinition(final Map<String, Object> toolDefinition, final String propertyName) {
- return getMap(getProperty(toolDefinition, propertyName));
- }
-
- private Object getProperty(final Map<String, Object> toolDefinition, final String propertyName) {
- return getMap(getMap(getFunction(toolDefinition).get("parameters")).get("properties")).get(propertyName);
- }
-
- @SuppressWarnings("unchecked")
- private Map<String, Object> getMap(final Object value) {
- return (Map<String, Object>) value;
- }
-
- @SuppressWarnings("unchecked")
- private List<String> castToStringList(final Object value) {
- return (List<String>) value;
- }
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/client/LLMChatModelClient.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/client/LLMChatModelClient.java
index c3b76d46b5d..6a3fd6bd7e7 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/client/LLMChatModelClient.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/llm/conversation/client/LLMChatModelClient.java
@@ -22,6 +22,8 @@ import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.util.json.JsonUtils;
import org.apache.shardingsphere.test.e2e.mcp.llm.config.LLME2EConfiguration;
+import org.apache.shardingsphere.test.e2e.mcp.support.runtime.ReadinessProbe;
+import org.apache.shardingsphere.test.e2e.mcp.support.runtime.ReadinessProbe.ReadinessResult;
import java.io.IOException;
import java.net.URI;
@@ -61,37 +63,18 @@ public final class LLMChatModelClient {
* @throws IllegalStateException model service is not ready
*/
public void waitUntilReady() throws InterruptedException {
- long startTime = System.currentTimeMillis();
- long deadline = startTime + config.getReadyTimeoutSeconds() * 1000L;
- long intervalMillis = INITIAL_READINESS_INTERVAL_MILLIS;
- int attemptCount = 0;
- Exception lastFailure = null;
- while (System.currentTimeMillis() < deadline) {
- attemptCount++;
- try {
- if (isReadinessContractReady()) {
- return;
- }
- } catch (final IOException ex) {
- lastFailure = ex;
- } catch (final IllegalStateException ex) {
- lastFailure = ex;
- if (isNonRetryableReadinessFailure(ex)) {
- throw createReadinessException(lastFailure, attemptCount, startTime);
- }
- }
- intervalMillis = waitForNextReadinessAttempt(deadline, intervalMillis);
- }
- throw createReadinessException(lastFailure, attemptCount, startTime);
+ new ReadinessProbe(config.getReadyTimeoutSeconds() * 1000L, INITIAL_READINESS_INTERVAL_MILLIS, MAX_READINESS_INTERVAL_MILLIS)
+ .waitUntilReady(this::checkReadiness, this::createReadinessException);
}
- private long waitForNextReadinessAttempt(final long deadline, final long intervalMillis) throws InterruptedException {
- long remainingMillis = deadline - System.currentTimeMillis();
- if (0L >= remainingMillis) {
- return intervalMillis;
+ private ReadinessResult<Boolean> checkReadiness() throws InterruptedException {
+ try {
+ return isReadinessContractReady() ? ReadinessResult.ready(Boolean.TRUE) : ReadinessResult.retry(null);
+ } catch (final IOException ex) {
+ return ReadinessResult.retry(ex);
+ } catch (final IllegalStateException ex) {
+ return isNonRetryableReadinessFailure(ex) ? ReadinessResult.failed(ex) : ReadinessResult.retry(ex);
}
- Thread.sleep(Math.min(intervalMillis, remainingMillis));
- return Math.min(MAX_READINESS_INTERVAL_MILLIS, intervalMillis * 2L);
}
private boolean isNonRetryableReadinessFailure(final Exception cause) {
@@ -99,8 +82,8 @@ public final class LLMChatModelClient {
return message.contains("HTTP 400") || message.contains("HTTP 401");
}
- private IllegalStateException createReadinessException(final Exception cause, final int attemptCount, final long startTimeMillis) {
- IllegalStateException result = new IllegalStateException(createReadinessFailureMessage(cause, attemptCount, System.currentTimeMillis() - startTimeMillis));
+ private IllegalStateException createReadinessException(final Exception cause, final int attemptCount, final long elapsedMillis) {
+ IllegalStateException result = new IllegalStateException(createReadinessFailureMessage(cause, attemptCount, elapsedMillis));
if (null != cause) {
result.initCause(cause);
}
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
new file mode 100644
index 00000000000..02dfbc051ec
--- /dev/null
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/AbstractProductionMySQLRuntimeE2ETest.java
@@ -0,0 +1,383 @@
+/*
+ * 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.test.e2e.mcp.runtime.production;
+
+import io.modelcontextprotocol.client.McpClient;
+import io.modelcontextprotocol.client.McpSyncClient;
+import io.modelcontextprotocol.client.transport.HttpClientStreamableHttpTransport;
+import io.modelcontextprotocol.client.transport.ServerParameters;
+import io.modelcontextprotocol.client.transport.StdioClientTransport;
+import io.modelcontextprotocol.spec.McpClientTransport;
+import io.modelcontextprotocol.spec.McpSchema;
+import io.modelcontextprotocol.spec.ProtocolVersions;
+import org.apache.shardingsphere.mcp.bootstrap.MCPBootstrap;
+import org.apache.shardingsphere.mcp.bootstrap.transport.MCPTransportJsonMapperFactory;
+import org.apache.shardingsphere.mcp.support.database.metadata.jdbc.RuntimeDatabaseConfiguration;
+import org.apache.shardingsphere.mcp.support.descriptor.MCPShardingSphereMetadataKeys;
+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;
+import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionPayloads;
+import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPPayloadAssertions;
+import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPInteractionClient;
+import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPStdioLogbackConfiguration;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assumptions;
+import org.junit.jupiter.api.condition.EnabledIf;
+import org.junit.jupiter.params.provider.Arguments;
+import org.testcontainers.containers.GenericContainer;
+
+import java.io.IOException;
+import java.net.URI;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.sql.SQLException;
+import java.time.Duration;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Stream;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.hasItems;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.isA;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@EnabledIf("isEnabled")
+abstract class AbstractProductionMySQLRuntimeE2ETest extends AbstractTransportParameterizedProductionRuntimeE2ETest {
+
+ protected static final String LOGICAL_DATABASE_NAME = "logic_db";
+
+ protected static final String PHYSICAL_DATABASE_NAME = "orders";
+
+ protected static final String MASK_PLAN_TOOL_NAME = "database_gateway_plan_mask_rule";
+
+ protected static final String STDIO_LOGBACK_CONFIG_FILE_NAME = "mcp-e2e-sdk-stdio-logback.xml";
+
+ private GenericContainer<?> container;
+
+ private String physicalSchemaName;
+
+ protected GenericContainer<?> getContainer() {
+ return container;
+ }
+
+ protected String getPhysicalSchemaName() {
+ return physicalSchemaName;
+ }
+
+ @AfterEach
+ void tearDownContainer() {
+ if (null != container) {
+ container.stop();
+ container = null;
+ }
+ physicalSchemaName = null;
+ }
+
+ @Override
+ protected void prepareRuntimeFixture() throws IOException {
+ Assumptions.assumeTrue(MySQLRuntimeTestSupport.isDockerAvailable(),
+ () -> MySQLRuntimeTestSupport.createDockerRequiredMessage("Docker is required for the MySQL-backed production runtime E2E test."));
+ container = MySQLRuntimeTestSupport.createContainer();
+ container.start();
+ try {
+ MySQLRuntimeTestSupport.initializeDatabase(container);
+ String detectedSchemaName = MySQLRuntimeTestSupport.detectSchema(container);
+ physicalSchemaName = detectedSchemaName.isEmpty() ? PHYSICAL_DATABASE_NAME : detectedSchemaName;
+ } catch (final SQLException ex) {
+ throw new IOException(ex);
+ }
+ }
+
+ @Override
+ protected Map<String, RuntimeDatabaseConfiguration> getRuntimeDatabases() {
+ return MySQLRuntimeTestSupport.createRuntimeDatabases(container, LOGICAL_DATABASE_NAME);
+ }
+
+ protected static Map<String, Object> createExecuteUpdateArguments(final String sql) {
+ return Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql", sql, "execution_mode", "execute");
+ }
+
+ protected static Map<String, Object> createExecuteUpdateArguments(final String databaseName, final String sql) {
+ return Map.of("database", databaseName, "schema", databaseName, "sql", sql, "execution_mode", "execute");
+ }
+
+ protected static boolean isEnabled() {
+ return MCPE2ECondition.isProductionMySQLEnabled() || MCPE2ECondition.isProductionMySQLStdioEnabled();
+ }
+
+ protected static Stream<Arguments> transports() {
+ return runtimeTransports().map(each -> Arguments.of(getTransportName(each), each));
+ }
+
+ protected static Stream<Arguments> assertReadSingleMetadataResourceCases() {
+ return runtimeTransports().flatMap(each -> Stream.of(
+ Arguments.of(getTransportName(each) + " database detail", each, "shardingsphere://databases/logic_db", "database", LOGICAL_DATABASE_NAME),
+ Arguments.of(getTransportName(each) + " schema detail", each, "shardingsphere://databases/logic_db/schemas/logic_db", "schema", LOGICAL_DATABASE_NAME),
+ Arguments.of(getTransportName(each) + " table column detail", each,
+ "shardingsphere://databases/logic_db/schemas/logic_db/tables/orders/columns/status", "column", "status"),
+ Arguments.of(getTransportName(each) + " view detail", each, "shardingsphere://databases/logic_db/schemas/logic_db/views/active_orders", "view", "active_orders"),
+ Arguments.of(getTransportName(each) + " view column detail", each,
+ "shardingsphere://databases/logic_db/schemas/logic_db/views/active_orders/columns/status", "column", "status"),
+ Arguments.of(getTransportName(each) + " index detail", each,
+ "shardingsphere://databases/logic_db/schemas/logic_db/tables/orders/indexes/idx_orders_status", "index", "idx_orders_status")));
+ }
+
+ protected static Stream<Arguments> assertReadCollectionMetadataResourceCases() {
+ return runtimeTransports().flatMap(each -> Stream.of(
+ Arguments.of(getTransportName(each) + " schemas list", each, "shardingsphere://databases/logic_db/schemas", "schema", List.of(LOGICAL_DATABASE_NAME)),
+ Arguments.of(getTransportName(each) + " tables list", each,
+ "shardingsphere://databases/logic_db/schemas/logic_db/tables", "table", List.of("order_items", "orders")),
+ Arguments.of(getTransportName(each) + " table columns list", each,
+ "shardingsphere://databases/logic_db/schemas/logic_db/tables/orders/columns", "column", List.of("amount", "order_id", "status")),
+ Arguments.of(getTransportName(each) + " view columns list", each,
+ "shardingsphere://databases/logic_db/schemas/logic_db/views/active_orders/columns", "column", List.of("order_id", "status"))));
+ }
+
+ protected static Stream<RuntimeTransport> runtimeTransports() {
+ Stream.Builder<RuntimeTransport> result = Stream.builder();
+ if (MCPE2ECondition.isProductionMySQLEnabled()) {
+ result.add(RuntimeTransport.HTTP);
+ }
+ if (MCPE2ECondition.isProductionMySQLStdioEnabled()) {
+ result.add(RuntimeTransport.STDIO);
+ }
+ return result.build();
+ }
+
+ protected static String getTransportName(final RuntimeTransport transport) {
+ return RuntimeTransport.HTTP == transport ? "http" : "stdio";
+ }
+
+ protected Map<String, RuntimeDatabaseConfiguration> createPreparedProgrammaticRuntimeDatabases() throws IOException {
+ prepareRuntime();
+ try {
+ return MySQLRuntimeTestSupport.createPreparedProgrammaticRuntimeDatabases(container);
+ } catch (final SQLException ex) {
+ throw new IOException("Failed to initialize MySQL programmatic runtime databases.", ex);
+ }
+ }
+
+ protected Map<String, RuntimeDatabaseConfiguration> createMismatchedRuntimeDatabases() throws IOException {
+ prepareRuntime();
+ RuntimeDatabaseConfiguration runtimeDatabase = MySQLRuntimeTestSupport.createRuntimeDatabases(container, LOGICAL_DATABASE_NAME).get(LOGICAL_DATABASE_NAME);
+ return Map.of(LOGICAL_DATABASE_NAME, new RuntimeDatabaseConfiguration("PostgreSQL", runtimeDatabase.getJdbcUrl(), runtimeDatabase.getUsername(),
+ runtimeDatabase.getPassword(), runtimeDatabase.getDriverClassName()));
+ }
+
+ protected void openAndCloseInteractionClient(final Map<String, RuntimeDatabaseConfiguration> runtimeDatabases) {
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient(runtimeDatabases)) {
+ interactionClient.listResources();
+ } catch (final IOException | InterruptedException ex) {
+ throw new IllegalStateException(ex);
+ }
+ }
+
+ protected List<String> readTableNames(final MCPInteractionClient interactionClient, final String databaseName) throws IOException, InterruptedException {
+ return getPayloadItems(interactionClient.readResource(String.format("shardingsphere://databases/%s/schemas/%s/tables", databaseName, databaseName)))
+ .stream().map(each -> String.valueOf(each.get("table"))).toList();
+ }
+
+ protected void assertOfficialToolNames(final List<String> actualToolNames) {
+ assertThat(actualToolNames, containsInAnyOrder(OfficialMCPToolNames.getAll().toArray()));
+ }
+
+ protected void assertToolDefinition(final List<Map<String, Object>> tools, final String toolName, final String expectedTitle,
+ final String expectedRequiredField, final String expectedPropertyField, final String expectedPropertyType) {
+ MCPPayloadAssertions.assertToolDefinition(tools, toolName, expectedTitle, expectedRequiredField, expectedPropertyField, expectedPropertyType);
+ }
+
+ protected void assertJsonRpcErrorWithoutResult(final Map<String, Object> actual, final String requestId) {
+ assertThat(String.valueOf(actual.get("jsonrpc")), is("2.0"));
+ assertThat(String.valueOf(actual.get("id")), is(requestId));
+ assertTrue(MCPInteractionPayloads.hasJsonRpcError(actual));
+ assertFalse(actual.containsKey("result"));
+ Map<String, Object> error = getMap(actual.get("error"));
+ assertThat(error.get("code"), isA(Number.class));
+ assertFalse(String.valueOf(error.get("message")).isBlank());
+ }
+
+ protected void assertRecoveryResponse(final Map<String, Object> actual) {
+ assertThat(String.valueOf(actual.get("response_mode")), is("recovery"));
+ assertFalse(String.valueOf(actual.get("message")).isBlank());
+ }
+
+ protected void assertRecoveryResponse(final Map<String, Object> actual, final String expectedMessage) {
+ assertRecoveryResponse(actual);
+ assertThat(String.valueOf(actual.get("message")), is(expectedMessage));
+ }
+
+ protected void assertRecoveryResponse(final Map<String, Object> actual, final String expectedMessage, final String expectedCategory) {
+ assertRecoveryResponse(actual, expectedMessage);
+ assertThat(String.valueOf(getMap(actual.get("recovery")).get("category")), is(expectedCategory));
+ }
+
+ protected void assertAiNativeCapabilities(final Map<String, Object> capabilities) {
+ assertTrue(capabilities.containsKey("model_first_summary"));
+ assertTrue(capabilities.containsKey("model_contract"));
+ assertTrue(capabilities.containsKey("surface_summary"));
+ assertTrue(capabilities.containsKey("field_naming_contract"));
+ assertTrue(capabilities.containsKey("next_action_contract"));
+ assertTrue(capabilities.containsKey("common_flows"));
+ assertTrue(capabilities.containsKey("security_hints"));
+ assertTrue(capabilities.containsKey("fingerprints"));
+ assertFalse(getMap(capabilities.get("fingerprints")).isEmpty());
+ assertFalse(((List<?>) capabilities.get("common_flows")).isEmpty());
+ Map<String, Object> modelFirstSummary = getMap(capabilities.get("model_first_summary"));
+ assertThat(getMap(modelFirstSummary.get("official_discovery_methods")).get("tools"), is("tools/list"));
+ assertThat(modelFirstSummary.get("optional_catalog_resource"), is("shardingsphere://capabilities"));
+ assertThat(getMap(getMap(modelFirstSummary.get("sql_tool_selection")).get("read_only")).get("tool"), is("database_gateway_execute_query"));
+ assertThat(getMap(getMap(modelFirstSummary.get("workflow_rule")).get("preview_tool")).get("tool"), is("database_gateway_apply_workflow"));
+ Map<String, Object> surfaceSummary = getMap(capabilities.get("surface_summary"));
+ assertThat(getMap(surfaceSummary.get("official_discovery_methods")).get("resources"), is("resources/list"));
+ assertThat(surfaceSummary.get("read_only_sql_tool"), is("database_gateway_execute_query"));
+ assertThat(surfaceSummary.get("side_effect_sql_tool"), is("database_gateway_execute_update"));
+ }
+
+ protected void assertAiNativeDiscovery(final MCPInteractionClient interactionClient) throws IOException, InterruptedException {
+ Map<String, Object> runtimeStatus = interactionClient.readResource("shardingsphere://runtime");
+ assertThat(String.valueOf(runtimeStatus.get("status")), is("available"));
+ assertThat(String.valueOf(runtimeStatus.get("configured_database_count")), is("1"));
+ assertTrue(getResources(interactionClient.listResources()).stream().anyMatch(each -> "shardingsphere://runtime".equals(each.get("uri"))));
+ assertTrue(getResourceTemplates(interactionClient.listResourceTemplates()).stream()
+ .anyMatch(each -> "shardingsphere://databases/{database}/schemas/{schema}/tables/{table}".equals(each.get("uriTemplate"))));
+ assertTrue(interactionClient.listTools().stream().anyMatch(each -> "database_gateway_execute_update".equals(each.get("name")) && getMap(each.get("outputSchema")).containsKey("properties")));
+ Map<String, Object> promptPayload = interactionClient.getPrompt("inspect_metadata",
+ Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "query", "orders"));
+ assertTrue(String.valueOf(promptPayload).contains("Stop conditions"));
+ Map<String, Object> completionPayload = interactionClient.complete(Map.of("type", "ref/prompt", "name", "inspect_metadata"),
+ "schema", "log", Map.of("database", LOGICAL_DATABASE_NAME));
+ assertTrue(((List<?>) getMap(completionPayload.get("completion")).get("values")).contains(LOGICAL_DATABASE_NAME));
+ }
+
+ protected void assertAiNativeSqlPreview(final MCPInteractionClient interactionClient) throws IOException, InterruptedException {
+ Map<String, Object> actual = interactionClient.call("database_gateway_execute_update",
+ Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql", "UPDATE orders SET status = status WHERE order_id = -1", "execution_mode", "preview"));
+ assertThat(String.valueOf(actual.get("response_mode")), is("preview"));
+ assertThat(String.valueOf(actual.get("result_kind")), is("preview"));
+ assertThat(String.valueOf(actual.get("preview_semantics")), is("classification_only"));
+ assertFalse((Boolean) actual.get("would_execute"));
+ List<Map<String, Object>> nextActions = getMapList(actual.get("next_actions"));
+ assertThat(nextActions.stream().map(each -> String.valueOf(each.get("type"))).toList(), is(List.of("tool_call")));
+ assertFalse(nextActions.get(0).containsKey("requires_user_approval"));
+ }
+
+ protected void assertAiNativeSqlResult(final MCPInteractionClient interactionClient) throws IOException, InterruptedException {
+ Map<String, Object> actual = interactionClient.call("database_gateway_execute_query",
+ Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql", "SELECT order_id, status FROM orders ORDER BY order_id", "max_rows", 1));
+ assertThat(String.valueOf(actual.get("result_kind")), is("result_set"));
+ assertThat(String.valueOf(actual.get("row_object_status")), is("available"));
+ assertThat(((List<?>) actual.get("row_objects")).size(), is(1));
+ assertThat(String.valueOf(actual.get("truncated")), is("true"));
+ assertThat(String.valueOf(getMapList(actual.get("next_actions")).get(0).get("type")), is("ask_user"));
+ }
+
+ protected McpSyncClient createElicitationClient(final RuntimeTransport transport, final List<McpSchema.ElicitRequest> elicitationRequests) throws IOException {
+ return McpClient.sync(createClientTransport(transport))
+ .clientInfo(new McpSchema.Implementation("mcp-e2e-elicitation", "MCP E2E Elicitation", "1.0.0"))
+ .capabilities(McpSchema.ClientCapabilities.builder().elicitation().build())
+ .requestTimeout(Duration.ofSeconds(30L))
+ .initializationTimeout(Duration.ofSeconds(30L))
+ .elicitation(request -> createElicitationResult(elicitationRequests, request))
+ .build();
+ }
+
+ protected McpClientTransport createClientTransport(final RuntimeTransport transport) throws IOException {
+ return RuntimeTransport.HTTP == transport
+ ? createHttpClientTransport()
+ : createStdioClientTransport();
+ }
+
+ protected McpClientTransport createHttpClientTransport() throws IOException {
+ URI endpointUri = getHttpEndpointUri();
+ return HttpClientStreamableHttpTransport.builder(String.format("%s://%s:%d", endpointUri.getScheme(), endpointUri.getHost(), endpointUri.getPort()))
+ .endpoint(endpointUri.getPath()).build();
+ }
+
+ protected StdioClientTransport createStdioClientTransport() throws IOException {
+ Path configFile = getConfigFile();
+ return new ProtocolAwareStdioClientTransport(ServerParameters.builder(Paths.get(System.getProperty("java.home"), "bin", "java").toString())
+ .args("-Dlogback.configurationFile=" + MCPStdioLogbackConfiguration.createForConfig(configFile, STDIO_LOGBACK_CONFIG_FILE_NAME),
+ "-cp", System.getProperty("java.class.path"), MCPBootstrap.class.getName(), configFile.toString())
+ .build());
+ }
+
+ protected McpSchema.ElicitResult createElicitationResult(final List<McpSchema.ElicitRequest> elicitationRequests,
+ final McpSchema.ElicitRequest request) {
+ elicitationRequests.add(request);
+ List<String> requiredFields = getStringList(request.requestedSchema().get("required"));
+ return new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of(
+ requiredFields.get(0), "1",
+ requiredFields.get(1), "3"));
+ }
+
+ protected void assertElicitationRequest(final List<McpSchema.ElicitRequest> actualRequests) {
+ assertThat(actualRequests.size(), is(1));
+ McpSchema.ElicitRequest actual = actualRequests.get(0);
+ assertThat(actual.meta().get(MCPShardingSphereMetadataKeys.TOOL), is(MASK_PLAN_TOOL_NAME));
+ assertFalse(String.valueOf(actual.meta().get(MCPShardingSphereMetadataKeys.PLAN_ID)).isBlank());
+ Map<String, Object> actualRequestedSchema = actual.requestedSchema();
+ assertThat(actualRequestedSchema.get("type"), is("object"));
+ assertFalse((Boolean) actualRequestedSchema.get("additionalProperties"));
+ Map<String, Object> actualProperties = castToMap(actualRequestedSchema.get("properties"));
+ assertTrue(actualProperties.containsKey("field_1"));
+ assertTrue(actualProperties.containsKey("field_2"));
+ assertThat(String.valueOf(castToMap(actualProperties.get("field_1")).get("description")), is("Please provide property `from-x`."));
+ assertThat(String.valueOf(castToMap(actualProperties.get("field_2")).get("description")), is("Please provide property `to-y`."));
+ assertFalse(actualProperties.keySet().stream().map(String::valueOf).anyMatch(each -> each.contains("secret") || each.contains("password") || each.contains("token")));
+ assertThat(getStringList(actualRequestedSchema.get("required")), hasItems("field_1", "field_2"));
+ }
+
+ @SuppressWarnings("unchecked")
+ protected Map<String, Object> castStructuredContent(final Object value) {
+ return (Map<String, Object>) value;
+ }
+
+ @SuppressWarnings("unchecked")
+ protected Map<String, Object> castToMap(final Object value) {
+ return (Map<String, Object>) value;
+ }
+
+ protected Map<String, Object> getMap(final Object value) {
+ return value instanceof Map ? castToMap(value) : Map.of();
+ }
+
+ protected List<Map<String, Object>> getMapList(final Object value) {
+ return MCPPayloadAssertions.getMapList(value);
+ }
+
+ protected List<String> getStringList(final Object value) {
+ return ((List<?>) value).stream().map(String::valueOf).toList();
+ }
+
+ protected static final class ProtocolAwareStdioClientTransport extends StdioClientTransport {
+
+ protected ProtocolAwareStdioClientTransport(final ServerParameters params) {
+ super(params, MCPTransportJsonMapperFactory.create());
+ }
+
+ @Override
+ public List<String> protocolVersions() {
+ return List.of(ProtocolVersions.MCP_2025_06_18, ProtocolVersions.MCP_2025_11_25);
+ }
+ }
+}
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 55e4810abd9..13c1eec3269 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
@@ -186,7 +186,7 @@ class PackagedDistributionE2ETest {
private void assertOfficialRuntime(final Path distributionHome, final RuntimeTransport transport,
final MCPInteractionClient interactionClient) throws IOException, InterruptedException {
assertBootstrapDirectoriesCreated(distributionHome);
- assertLegacyExtensionDirectoryRemoved(distributionHome);
+ assertRemovedExtensionDirectoryAbsent(distributionHome);
assertOfficialFeatureJarsPackaged(distributionHome);
assertRuntimeDiagnostics(interactionClient.readResource("shardingsphere://runtime"), transport);
assertDatabaseNames(interactionClient.readResource("shardingsphere://databases"));
@@ -209,7 +209,7 @@ class PackagedDistributionE2ETest {
assertTrue(Files.isDirectory(distributionHome.resolve("plugins")));
}
- private void assertLegacyExtensionDirectoryRemoved(final Path distributionHome) {
+ private void assertRemovedExtensionDirectoryAbsent(final Path distributionHome) {
assertFalse(Files.exists(distributionHome.resolve("ext-lib")));
}
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 aa9fdaa7bf1..77ba75fffbb 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
@@ -17,101 +17,28 @@
package org.apache.shardingsphere.test.e2e.mcp.runtime.production;
-import io.modelcontextprotocol.client.McpClient;
-import io.modelcontextprotocol.client.McpSyncClient;
-import io.modelcontextprotocol.client.transport.HttpClientStreamableHttpTransport;
-import io.modelcontextprotocol.client.transport.ServerParameters;
-import io.modelcontextprotocol.client.transport.StdioClientTransport;
-import io.modelcontextprotocol.spec.McpClientTransport;
-import io.modelcontextprotocol.spec.McpSchema;
-import io.modelcontextprotocol.spec.ProtocolVersions;
-import org.apache.shardingsphere.mcp.bootstrap.MCPBootstrap;
-import org.apache.shardingsphere.mcp.bootstrap.transport.MCPTransportJsonMapperFactory;
import org.apache.shardingsphere.mcp.bootstrap.transport.MCPTransportConstants;
-import org.apache.shardingsphere.mcp.support.database.metadata.jdbc.RuntimeDatabaseConnectionException;
-import org.apache.shardingsphere.mcp.support.database.metadata.jdbc.RuntimeDatabaseConfiguration;
-import org.apache.shardingsphere.mcp.support.descriptor.MCPShardingSphereMetadataKeys;
import org.apache.shardingsphere.mcp.support.markdown.MCPMarkdownResourceLoader;
-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;
-import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionPayloads;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPPayloadAssertions;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPInteractionClient;
-import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPStdioLogbackConfiguration;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
-import org.testcontainers.containers.GenericContainer;
import java.io.IOException;
-import java.net.URI;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.sql.SQLException;
-import java.time.Duration;
import java.util.List;
import java.util.Map;
-import java.util.concurrent.CopyOnWriteArrayList;
-import java.util.stream.Stream;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
-import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.isA;
import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@EnabledIf("isEnabled")
-class ProductionMySQLRuntimeE2ETest extends AbstractTransportParameterizedProductionRuntimeE2ETest {
-
- private static final String LOGICAL_DATABASE_NAME = "logic_db";
-
- private static final String PHYSICAL_DATABASE_NAME = "orders";
-
- private static final String MASK_PLAN_TOOL_NAME = "database_gateway_plan_mask_rule";
-
- private static final String STDIO_LOGBACK_CONFIG_FILE_NAME = "mcp-e2e-sdk-stdio-logback.xml";
-
- private GenericContainer<?> container;
-
- private String physicalSchemaName;
-
- @AfterEach
- void tearDownContainer() {
- if (null != container) {
- container.stop();
- container = null;
- }
- physicalSchemaName = null;
- }
-
- @Override
- protected void prepareRuntimeFixture() throws IOException {
- Assumptions.assumeTrue(MySQLRuntimeTestSupport.isDockerAvailable(),
- () -> MySQLRuntimeTestSupport.createDockerRequiredMessage("Docker is required for the MySQL-backed production runtime E2E test."));
- container = MySQLRuntimeTestSupport.createContainer();
- container.start();
- try {
- MySQLRuntimeTestSupport.initializeDatabase(container);
- String detectedSchemaName = MySQLRuntimeTestSupport.detectSchema(container);
- physicalSchemaName = detectedSchemaName.isEmpty() ? PHYSICAL_DATABASE_NAME : detectedSchemaName;
- } catch (final SQLException ex) {
- throw new IOException(ex);
- }
- }
-
- @Override
- protected Map<String, RuntimeDatabaseConfiguration> getRuntimeDatabases() {
- return MySQLRuntimeTestSupport.createRuntimeDatabases(container, LOGICAL_DATABASE_NAME);
- }
+class ProductionMySQLRuntimeE2ETest extends AbstractProductionMySQLRuntimeE2ETest {
@ParameterizedTest(name = "{0}")
@MethodSource("transports")
@@ -294,597 +221,4 @@ class ProductionMySQLRuntimeE2ETest extends AbstractTransportParameterizedProduc
}
}
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertExecuteSelectWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- Map<String, Object> actual = 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));
- assertThat(String.valueOf(actual.get("result_kind")), is("result_set"));
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertExecuteSelectWithTruncationWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- Map<String, Object> actual = interactionClient.call("database_gateway_execute_query",
- Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql", "SELECT order_id, status FROM orders ORDER BY order_id", "max_rows", 1));
- assertThat(String.valueOf(actual.get("result_kind")), is("result_set"));
- assertThat(((List<?>) actual.get("rows")).size(), is(1));
- assertThat(String.valueOf(actual.get("truncated")), is("true"));
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertExecuteExplainAnalyzeSelectWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- Map<String, Object> actual = interactionClient.call("database_gateway_execute_query",
- Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql", "EXPLAIN ANALYZE SELECT * FROM orders WHERE order_id = 1", "max_rows", 10));
- assertThat(String.valueOf(actual.get("result_kind")), is("result_set"));
- assertThat(String.valueOf(actual.get("statement_type")), is("EXPLAIN ANALYZE"));
- assertFalse(((List<?>) actual.get("rows")).isEmpty());
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertExecuteQueryTimeoutWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- Map<String, Object> actual = interactionClient.call("database_gateway_execute_query",
- Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql", "SELECT SLEEP(2)", "timeout_ms", 1));
- assertRecoveryResponse(actual);
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertRejectExecuteMultiStatementWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- Map<String, Object> actual = interactionClient.call("database_gateway_execute_query",
- Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql", "SELECT 1; SELECT 2"));
- assertRecoveryResponse(actual, "Only one SQL statement is allowed.", "multiple_sql_statements");
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertRejectExplainAnalyzeUpdateFromReadOnlyToolWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- Map<String, Object> actual = interactionClient.call("database_gateway_execute_query",
- Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql",
- "EXPLAIN ANALYZE UPDATE orders SET status = 'DONE' WHERE order_id = 1"));
- assertRecoveryResponse(actual,
- "database_gateway_execute_query only supports classifier-approved QUERY and EXPLAIN_ANALYZE statements. Use database_gateway_execute_update for side-effecting SQL.",
- "unsafe_sql_attempted");
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertRejectLockingReadFromReadOnlyToolWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- Map<String, Object> actual = interactionClient.call("database_gateway_execute_query",
- Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql", "SELECT * FROM orders FOR UPDATE"));
- assertRecoveryResponse(actual, "Locking read statements such as SELECT ... FOR UPDATE are not supported by the MCP read-only contract.");
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertRejectLockingReadFromUpdateToolWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- Map<String, Object> actual = interactionClient.call("database_gateway_execute_update",
- createExecuteUpdateArguments("SELECT * FROM orders FOR UPDATE"));
- assertRecoveryResponse(actual, "Locking read statements such as SELECT ... FOR UPDATE are not supported by the MCP read-only contract.");
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertElicitMaskPlanningWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException {
- useTransport(transport);
- List<McpSchema.ElicitRequest> actualElicitationRequests = new CopyOnWriteArrayList<>();
- try (McpSyncClient client = createElicitationClient(transport, actualElicitationRequests)) {
- client.initialize();
- McpSchema.CallToolResult actual = client.callTool(new McpSchema.CallToolRequest(MASK_PLAN_TOOL_NAME, Map.of(
- "database", LOGICAL_DATABASE_NAME,
- "schema", LOGICAL_DATABASE_NAME,
- "table", "orders",
- "column", "status",
- "operation_type", "create",
- "algorithm_type", "MASK_FROM_X_TO_Y")));
- Map<String, Object> actualPayload = castStructuredContent(actual.structuredContent());
- if (RuntimeTransport.HTTP == transport) {
- assertThat(String.valueOf(actualPayload.get("status")), is("clarifying"));
- assertThat(String.valueOf(actualPayload.get("fallback_reason")), is("remote_identity_required"));
- assertTrue(actualElicitationRequests.isEmpty());
- return;
- }
- assertThat(String.valueOf(actualPayload.get("status")), is("planned"));
- assertThat(String.valueOf(actualPayload.get("current_step")), is("review"));
- assertThat(String.valueOf(castToMap(castToMap(actualPayload.get("masked_property_preview")).get("primary")).get("from-x")), is("1"));
- assertThat(String.valueOf(castToMap(castToMap(actualPayload.get("masked_property_preview")).get("primary")).get("to-y")), is("3"));
- assertElicitationRequest(actualElicitationRequests);
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertExecuteUpdateWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws SQLException, IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- Map<String, Object> actual = interactionClient.call("database_gateway_execute_update",
- createExecuteUpdateArguments("UPDATE orders SET status = 'PENDING' WHERE order_id = 1"));
- assertThat(String.valueOf(actual.get("result_kind")), is("update_count"));
- assertThat(String.valueOf(actual.get("affected_rows")), is("1"));
- assertThat(MySQLRuntimeTestSupport.querySingleString(container, String.format("SELECT status FROM %s.orders WHERE order_id = 1", physicalSchemaName)), is("PENDING"));
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertExecuteUpdateWithoutApprovalArgumentWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- Map<String, Object> actual = interactionClient.call("database_gateway_execute_update",
- createExecuteUpdateArguments("UPDATE orders SET status = status WHERE order_id = -1"));
- assertThat(String.valueOf(actual.get("result_kind")), is("update_count"));
- assertThat(String.valueOf(actual.get("affected_rows")), is("0"));
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertRejectSequenceResourceWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- Map<String, Object> actual = interactionClient.readResource(
- String.format("shardingsphere://databases/%s/schemas/%s/sequences", LOGICAL_DATABASE_NAME, LOGICAL_DATABASE_NAME));
- assertThat(String.valueOf(actual.get("error_code")), is("json_rpc_error"));
- assertThat(String.valueOf(actual.get("message")), is("Sequence resources are not supported for the current database."));
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertExecuteRollbackWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws SQLException, IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- Map<String, Object> beginResponse = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("BEGIN"));
- interactionClient.call("database_gateway_execute_update",
- createExecuteUpdateArguments("UPDATE orders SET status = 'PENDING' WHERE order_id = 1"));
- Map<String, Object> rollbackResponse = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("ROLLBACK"));
- assertThat(String.valueOf(beginResponse.get("message")), is("Transaction started."));
- assertThat(String.valueOf(rollbackResponse.get("message")), is("Transaction rolled back."));
- assertThat(MySQLRuntimeTestSupport.querySingleString(container, String.format("SELECT status FROM %s.orders WHERE order_id = 1", physicalSchemaName)), is("NEW"));
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertRejectBlankSavepointNameWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- Map<String, Object> actual = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("SAVEPOINT"));
- assertRecoveryResponse(actual, "Savepoint name is required.");
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertExecuteSavepointFlowWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws SQLException, IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("BEGIN"));
- interactionClient.call("database_gateway_execute_update",
- createExecuteUpdateArguments("UPDATE orders SET status = 'PENDING' WHERE order_id = 1"));
- Map<String, Object> savepointResponse = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("SAVEPOINT sp_1"));
- interactionClient.call("database_gateway_execute_update",
- createExecuteUpdateArguments("UPDATE orders SET status = 'DONE' WHERE order_id = 1"));
- Map<String, Object> rollbackResponse = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("ROLLBACK TO SAVEPOINT sp_1"));
- Map<String, Object> commitResponse = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("COMMIT"));
- assertThat(String.valueOf(savepointResponse.get("message")), is("Savepoint created."));
- assertThat(String.valueOf(rollbackResponse.get("message")), is("Savepoint rolled back."));
- assertThat(String.valueOf(commitResponse.get("message")), is("Transaction committed."));
- assertThat(MySQLRuntimeTestSupport.querySingleString(container, String.format("SELECT status FROM %s.orders WHERE order_id = 1", physicalSchemaName)), is("PENDING"));
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertExecuteReleaseSavepointWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("BEGIN"));
- Map<String, Object> savepointResponse = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("SAVEPOINT sp_release"));
- Map<String, Object> releaseResponse = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("RELEASE SAVEPOINT sp_release"));
- Map<String, Object> commitResponse = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("COMMIT"));
- assertThat(String.valueOf(savepointResponse.get("message")), is("Savepoint created."));
- assertThat(String.valueOf(releaseResponse.get("message")), is("Savepoint released."));
- assertThat(String.valueOf(commitResponse.get("message")), is("Transaction committed."));
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertExecuteDdlRefreshesMetadataWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- Map<String, Object> executeResponse = interactionClient.call("database_gateway_execute_update",
- createExecuteUpdateArguments("CREATE TABLE orders_archive (order_id INT PRIMARY KEY)"));
- Map<String, Object> actualItem = MCPPayloadAssertions.getSingleItem(interactionClient.readResource(
- "shardingsphere://databases/logic_db/schemas/logic_db/tables/orders_archive"));
- assertThat(String.valueOf(executeResponse.get("message")), is("Statement executed."));
- assertThat(String.valueOf(actualItem.get("table")), is("orders_archive"));
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertCloseRollsBackPendingTransactionWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws SQLException, IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("BEGIN"));
- interactionClient.call("database_gateway_execute_update",
- createExecuteUpdateArguments("UPDATE orders SET status = 'PENDING' WHERE order_id = 1"));
- interactionClient.close();
- assertThat(MySQLRuntimeTestSupport.querySingleString(container, String.format("SELECT status FROM %s.orders WHERE order_id = 1", physicalSchemaName)), is("NEW"));
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertAiNativeDeterministicInteractionLoopWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
- assertAiNativeCapabilities(interactionClient.readResource("shardingsphere://capabilities"));
- assertAiNativeDiscovery(interactionClient);
- Map<String, Object> searchMetadataPayload = interactionClient.call("database_gateway_search_metadata",
- Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "query", "orders", "object_types", List.of("table")));
- Map<String, Object> tableHit = MCPPayloadAssertions.findItem(searchMetadataPayload, "name", "orders");
- String tableResourceUri = String.valueOf(getMap(tableHit.get("resource")).get("uri"));
- assertThat(tableResourceUri, is("shardingsphere://databases/logic_db/schemas/logic_db/tables/orders"));
- assertFalse(getMapList(tableHit.get("next_resources")).isEmpty());
- MCPPayloadAssertions.assertSingleItemValue(interactionClient.readResource(tableResourceUri), "table", "orders");
- assertAiNativeSqlPreview(interactionClient);
- assertAiNativeSqlResult(interactionClient);
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertListDatabasesWithMultipleRuntimeDatabasesWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient(createPreparedProgrammaticRuntimeDatabases())) {
- List<String> actualDatabaseNames = getPayloadItems(interactionClient.readResource("shardingsphere://databases")).stream()
- .map(each -> String.valueOf(each.get("database"))).toList();
- assertThat(actualDatabaseNames, hasItems(LOGICAL_DATABASE_NAME, "analytics_db", "warehouse"));
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertRefreshMetadataVisibleForTargetDatabaseOnlyWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient firstInteractionClient = createOpenedInteractionClient(createPreparedProgrammaticRuntimeDatabases())) {
- firstInteractionClient.call("database_gateway_execute_update",
- createExecuteUpdateArguments(LOGICAL_DATABASE_NAME, "CREATE TABLE orders_archive (order_id INT PRIMARY KEY)"));
- List<String> firstSessionTableNames = readTableNames(firstInteractionClient, LOGICAL_DATABASE_NAME);
- try (MCPInteractionClient secondInteractionClient = createOpenedInteractionClient(createPreparedProgrammaticRuntimeDatabases())) {
- List<String> secondSessionTableNames = readTableNames(secondInteractionClient, LOGICAL_DATABASE_NAME);
- List<String> analyticsDatabaseTableNames = readTableNames(secondInteractionClient, "analytics_db");
- assertTrue(firstSessionTableNames.contains("orders_archive"));
- assertTrue(secondSessionTableNames.contains("orders_archive"));
- assertFalse(analyticsDatabaseTableNames.contains("orders_archive"));
- assertTrue(analyticsDatabaseTableNames.contains("metrics"));
- }
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertRejectCrossDatabaseTransactionSwitchWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
- useTransport(transport);
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient(createPreparedProgrammaticRuntimeDatabases())) {
- interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments(LOGICAL_DATABASE_NAME, "BEGIN"));
- Map<String, Object> actual = interactionClient.call("database_gateway_execute_query",
- Map.of("database", "analytics_db", "schema", "analytics_db", "sql", "SELECT metric_name FROM metrics ORDER BY metric_id"));
- assertRecoveryResponse(actual, "Cross-database transaction switching is not supported.");
- }
- }
-
- @ParameterizedTest(name = "{0}")
- @MethodSource("transports")
- void assertRejectMismatchedDatabaseTypeWithActualMySQLBackend(final String name, final RuntimeTransport transport) {
- useTransport(transport);
- if (RuntimeTransport.HTTP == transport) {
- RuntimeDatabaseConnectionException actual = assertThrows(RuntimeDatabaseConnectionException.class,
- () -> openAndCloseInteractionClient(createMismatchedRuntimeDatabases()));
- assertThat(actual.getMessage(), is("Runtime database `logic_db` connection failed: invalid_configuration."));
- assertThat(actual.getCategory(), is(RuntimeDatabaseConnectionException.CATEGORY_INVALID_CONFIGURATION));
- assertThat(actual.getCause().getMessage(), is("Configured databaseType `PostgreSQL` does not match actual database type `MySQL` for database `logic_db`."));
- return;
- }
- IllegalStateException actual = assertThrows(IllegalStateException.class, () -> openAndCloseInteractionClient(createMismatchedRuntimeDatabases()));
- assertThat(actual.getMessage(), containsString("Runtime database `logic_db` connection failed: invalid_configuration."));
- assertThat(actual.getMessage(), containsString("Configured databaseType `PostgreSQL` does not match actual database type `MySQL` for database `logic_db`."));
- }
-
- private static Map<String, Object> createExecuteUpdateArguments(final String sql) {
- return Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql", sql, "execution_mode", "execute");
- }
-
- private static Map<String, Object> createExecuteUpdateArguments(final String databaseName, final String sql) {
- return Map.of("database", databaseName, "schema", databaseName, "sql", sql, "execution_mode", "execute");
- }
-
- private static boolean isEnabled() {
- return MCPE2ECondition.isProductionMySQLEnabled() || MCPE2ECondition.isProductionMySQLStdioEnabled();
- }
-
- private static Stream<Arguments> transports() {
- return runtimeTransports().map(each -> Arguments.of(getTransportName(each), each));
- }
-
- private static Stream<Arguments> assertReadSingleMetadataResourceCases() {
- return runtimeTransports().flatMap(each -> Stream.of(
- Arguments.of(getTransportName(each) + " database detail", each, "shardingsphere://databases/logic_db", "database", LOGICAL_DATABASE_NAME),
- Arguments.of(getTransportName(each) + " schema detail", each, "shardingsphere://databases/logic_db/schemas/logic_db", "schema", LOGICAL_DATABASE_NAME),
- Arguments.of(getTransportName(each) + " table column detail", each,
- "shardingsphere://databases/logic_db/schemas/logic_db/tables/orders/columns/status", "column", "status"),
- Arguments.of(getTransportName(each) + " view detail", each, "shardingsphere://databases/logic_db/schemas/logic_db/views/active_orders", "view", "active_orders"),
- Arguments.of(getTransportName(each) + " view column detail", each,
- "shardingsphere://databases/logic_db/schemas/logic_db/views/active_orders/columns/status", "column", "status"),
- Arguments.of(getTransportName(each) + " index detail", each,
- "shardingsphere://databases/logic_db/schemas/logic_db/tables/orders/indexes/idx_orders_status", "index", "idx_orders_status")));
- }
-
- private static Stream<Arguments> assertReadCollectionMetadataResourceCases() {
- return runtimeTransports().flatMap(each -> Stream.of(
- Arguments.of(getTransportName(each) + " schemas list", each, "shardingsphere://databases/logic_db/schemas", "schema", List.of(LOGICAL_DATABASE_NAME)),
- Arguments.of(getTransportName(each) + " tables list", each,
- "shardingsphere://databases/logic_db/schemas/logic_db/tables", "table", List.of("order_items", "orders")),
- Arguments.of(getTransportName(each) + " table columns list", each,
- "shardingsphere://databases/logic_db/schemas/logic_db/tables/orders/columns", "column", List.of("amount", "order_id", "status")),
- Arguments.of(getTransportName(each) + " view columns list", each,
- "shardingsphere://databases/logic_db/schemas/logic_db/views/active_orders/columns", "column", List.of("order_id", "status"))));
- }
-
- private static Stream<RuntimeTransport> runtimeTransports() {
- Stream.Builder<RuntimeTransport> result = Stream.builder();
- if (MCPE2ECondition.isProductionMySQLEnabled()) {
- result.add(RuntimeTransport.HTTP);
- }
- if (MCPE2ECondition.isProductionMySQLStdioEnabled()) {
- result.add(RuntimeTransport.STDIO);
- }
- return result.build();
- }
-
- private static String getTransportName(final RuntimeTransport transport) {
- return RuntimeTransport.HTTP == transport ? "http" : "stdio";
- }
-
- private Map<String, RuntimeDatabaseConfiguration> createPreparedProgrammaticRuntimeDatabases() throws IOException {
- prepareRuntime();
- try {
- return MySQLRuntimeTestSupport.createPreparedProgrammaticRuntimeDatabases(container);
- } catch (final SQLException ex) {
- throw new IOException("Failed to initialize MySQL programmatic runtime databases.", ex);
- }
- }
-
- private Map<String, RuntimeDatabaseConfiguration> createMismatchedRuntimeDatabases() throws IOException {
- prepareRuntime();
- RuntimeDatabaseConfiguration runtimeDatabase = MySQLRuntimeTestSupport.createRuntimeDatabases(container, LOGICAL_DATABASE_NAME).get(LOGICAL_DATABASE_NAME);
- return Map.of(LOGICAL_DATABASE_NAME, new RuntimeDatabaseConfiguration("PostgreSQL", runtimeDatabase.getJdbcUrl(), runtimeDatabase.getUsername(),
- runtimeDatabase.getPassword(), runtimeDatabase.getDriverClassName()));
- }
-
- private void openAndCloseInteractionClient(final Map<String, RuntimeDatabaseConfiguration> runtimeDatabases) {
- try (MCPInteractionClient interactionClient = createOpenedInteractionClient(runtimeDatabases)) {
- interactionClient.listResources();
- } catch (final IOException | InterruptedException ex) {
- throw new IllegalStateException(ex);
- }
- }
-
- private List<String> readTableNames(final MCPInteractionClient interactionClient, final String databaseName) throws IOException, InterruptedException {
- return getPayloadItems(interactionClient.readResource(String.format("shardingsphere://databases/%s/schemas/%s/tables", databaseName, databaseName)))
- .stream().map(each -> String.valueOf(each.get("table"))).toList();
- }
-
- private void assertOfficialToolNames(final List<String> actualToolNames) {
- assertThat(actualToolNames, containsInAnyOrder(OfficialMCPToolNames.getAll().toArray()));
- }
-
- private void assertToolDefinition(final List<Map<String, Object>> tools, final String toolName, final String expectedTitle,
- final String expectedRequiredField, final String expectedPropertyField, final String expectedPropertyType) {
- MCPPayloadAssertions.assertToolDefinition(tools, toolName, expectedTitle, expectedRequiredField, expectedPropertyField, expectedPropertyType);
- }
-
- private void assertJsonRpcErrorWithoutResult(final Map<String, Object> actual, final String requestId) {
- assertThat(String.valueOf(actual.get("jsonrpc")), is("2.0"));
- assertThat(String.valueOf(actual.get("id")), is(requestId));
- assertTrue(MCPInteractionPayloads.hasJsonRpcError(actual));
- assertFalse(actual.containsKey("result"));
- Map<String, Object> error = getMap(actual.get("error"));
- assertThat(error.get("code"), isA(Number.class));
- assertFalse(String.valueOf(error.get("message")).isBlank());
- }
-
- private void assertRecoveryResponse(final Map<String, Object> actual) {
- assertThat(String.valueOf(actual.get("response_mode")), is("recovery"));
- assertFalse(String.valueOf(actual.get("message")).isBlank());
- }
-
- private void assertRecoveryResponse(final Map<String, Object> actual, final String expectedMessage) {
- assertRecoveryResponse(actual);
- assertThat(String.valueOf(actual.get("message")), is(expectedMessage));
- }
-
- private void assertRecoveryResponse(final Map<String, Object> actual, final String expectedMessage, final String expectedCategory) {
- assertRecoveryResponse(actual, expectedMessage);
- assertThat(String.valueOf(getMap(actual.get("recovery")).get("category")), is(expectedCategory));
- }
-
- private void assertAiNativeCapabilities(final Map<String, Object> capabilities) {
- assertTrue(capabilities.containsKey("model_first_summary"));
- assertTrue(capabilities.containsKey("model_contract"));
- assertTrue(capabilities.containsKey("surface_summary"));
- assertTrue(capabilities.containsKey("field_naming_contract"));
- assertTrue(capabilities.containsKey("next_action_contract"));
- assertTrue(capabilities.containsKey("common_flows"));
- assertTrue(capabilities.containsKey("security_hints"));
- assertTrue(capabilities.containsKey("fingerprints"));
- assertFalse(getMap(capabilities.get("fingerprints")).isEmpty());
- assertFalse(((List<?>) capabilities.get("common_flows")).isEmpty());
- Map<String, Object> modelFirstSummary = getMap(capabilities.get("model_first_summary"));
- assertThat(getMap(modelFirstSummary.get("official_discovery_methods")).get("tools"), is("tools/list"));
- assertThat(modelFirstSummary.get("optional_catalog_resource"), is("shardingsphere://capabilities"));
- assertThat(getMap(getMap(modelFirstSummary.get("sql_tool_selection")).get("read_only")).get("tool"), is("database_gateway_execute_query"));
- assertThat(getMap(getMap(modelFirstSummary.get("workflow_rule")).get("preview_tool")).get("tool"), is("database_gateway_apply_workflow"));
- Map<String, Object> surfaceSummary = getMap(capabilities.get("surface_summary"));
- assertThat(getMap(surfaceSummary.get("official_discovery_methods")).get("resources"), is("resources/list"));
- assertThat(surfaceSummary.get("read_only_sql_tool"), is("database_gateway_execute_query"));
- assertThat(surfaceSummary.get("side_effect_sql_tool"), is("database_gateway_execute_update"));
- }
-
- private void assertAiNativeDiscovery(final MCPInteractionClient interactionClient) throws IOException, InterruptedException {
- Map<String, Object> runtimeStatus = interactionClient.readResource("shardingsphere://runtime");
- assertThat(String.valueOf(runtimeStatus.get("status")), is("available"));
- assertThat(String.valueOf(runtimeStatus.get("configured_database_count")), is("1"));
- assertTrue(getResources(interactionClient.listResources()).stream().anyMatch(each -> "shardingsphere://runtime".equals(each.get("uri"))));
- assertTrue(getResourceTemplates(interactionClient.listResourceTemplates()).stream()
- .anyMatch(each -> "shardingsphere://databases/{database}/schemas/{schema}/tables/{table}".equals(each.get("uriTemplate"))));
- assertTrue(interactionClient.listTools().stream().anyMatch(each -> "database_gateway_execute_update".equals(each.get("name")) && getMap(each.get("outputSchema")).containsKey("properties")));
- Map<String, Object> promptPayload = interactionClient.getPrompt("inspect_metadata",
- Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "query", "orders"));
- assertTrue(String.valueOf(promptPayload).contains("Stop conditions"));
- Map<String, Object> completionPayload = interactionClient.complete(Map.of("type", "ref/prompt", "name", "inspect_metadata"),
- "schema", "log", Map.of("database", LOGICAL_DATABASE_NAME));
- assertTrue(((List<?>) getMap(completionPayload.get("completion")).get("values")).contains(LOGICAL_DATABASE_NAME));
- }
-
- private void assertAiNativeSqlPreview(final MCPInteractionClient interactionClient) throws IOException, InterruptedException {
- Map<String, Object> actual = interactionClient.call("database_gateway_execute_update",
- Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql", "UPDATE orders SET status = status WHERE order_id = -1", "execution_mode", "preview"));
- assertThat(String.valueOf(actual.get("response_mode")), is("preview"));
- assertThat(String.valueOf(actual.get("result_kind")), is("preview"));
- assertThat(String.valueOf(actual.get("preview_semantics")), is("classification_only"));
- assertFalse((Boolean) actual.get("would_execute"));
- List<Map<String, Object>> nextActions = getMapList(actual.get("next_actions"));
- assertThat(nextActions.stream().map(each -> String.valueOf(each.get("type"))).toList(), is(List.of("tool_call")));
- assertFalse(nextActions.get(0).containsKey("requires_user_approval"));
- }
-
- private void assertAiNativeSqlResult(final MCPInteractionClient interactionClient) throws IOException, InterruptedException {
- Map<String, Object> actual = interactionClient.call("database_gateway_execute_query",
- Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql", "SELECT order_id, status FROM orders ORDER BY order_id", "max_rows", 1));
- assertThat(String.valueOf(actual.get("result_kind")), is("result_set"));
- assertThat(String.valueOf(actual.get("row_object_status")), is("available"));
- assertThat(((List<?>) actual.get("row_objects")).size(), is(1));
- assertThat(String.valueOf(actual.get("truncated")), is("true"));
- assertThat(String.valueOf(getMapList(actual.get("next_actions")).get(0).get("type")), is("ask_user"));
- }
-
- private McpSyncClient createElicitationClient(final RuntimeTransport transport, final List<McpSchema.ElicitRequest> elicitationRequests) throws IOException {
- return McpClient.sync(createClientTransport(transport))
- .clientInfo(new McpSchema.Implementation("mcp-e2e-elicitation", "MCP E2E Elicitation", "1.0.0"))
- .capabilities(McpSchema.ClientCapabilities.builder().elicitation().build())
- .requestTimeout(Duration.ofSeconds(30L))
- .initializationTimeout(Duration.ofSeconds(30L))
- .elicitation(request -> createElicitationResult(elicitationRequests, request))
- .build();
- }
-
- private McpClientTransport createClientTransport(final RuntimeTransport transport) throws IOException {
- return RuntimeTransport.HTTP == transport
- ? createHttpClientTransport()
- : createStdioClientTransport();
- }
-
- private McpClientTransport createHttpClientTransport() throws IOException {
- URI endpointUri = getHttpEndpointUri();
- return HttpClientStreamableHttpTransport.builder(String.format("%s://%s:%d", endpointUri.getScheme(), endpointUri.getHost(), endpointUri.getPort()))
- .endpoint(endpointUri.getPath()).build();
- }
-
- private StdioClientTransport createStdioClientTransport() throws IOException {
- Path configFile = getConfigFile();
- return new ProtocolAwareStdioClientTransport(ServerParameters.builder(Paths.get(System.getProperty("java.home"), "bin", "java").toString())
- .args("-Dlogback.configurationFile=" + MCPStdioLogbackConfiguration.createForConfig(configFile, STDIO_LOGBACK_CONFIG_FILE_NAME),
- "-cp", System.getProperty("java.class.path"), MCPBootstrap.class.getName(), configFile.toString())
- .build());
- }
-
- private McpSchema.ElicitResult createElicitationResult(final List<McpSchema.ElicitRequest> elicitationRequests,
- final McpSchema.ElicitRequest request) {
- elicitationRequests.add(request);
- List<String> requiredFields = getStringList(request.requestedSchema().get("required"));
- return new McpSchema.ElicitResult(McpSchema.ElicitResult.Action.ACCEPT, Map.of(
- requiredFields.get(0), "1",
- requiredFields.get(1), "3"));
- }
-
- private void assertElicitationRequest(final List<McpSchema.ElicitRequest> actualRequests) {
- assertThat(actualRequests.size(), is(1));
- McpSchema.ElicitRequest actual = actualRequests.get(0);
- assertThat(actual.meta().get(MCPShardingSphereMetadataKeys.TOOL), is(MASK_PLAN_TOOL_NAME));
- assertFalse(String.valueOf(actual.meta().get(MCPShardingSphereMetadataKeys.PLAN_ID)).isBlank());
- Map<String, Object> actualRequestedSchema = actual.requestedSchema();
- assertThat(actualRequestedSchema.get("type"), is("object"));
- assertFalse((Boolean) actualRequestedSchema.get("additionalProperties"));
- Map<String, Object> actualProperties = castToMap(actualRequestedSchema.get("properties"));
- assertTrue(actualProperties.containsKey("field_1"));
- assertTrue(actualProperties.containsKey("field_2"));
- assertThat(String.valueOf(castToMap(actualProperties.get("field_1")).get("description")), is("Please provide property `from-x`."));
- assertThat(String.valueOf(castToMap(actualProperties.get("field_2")).get("description")), is("Please provide property `to-y`."));
- assertFalse(actualProperties.keySet().stream().map(String::valueOf).anyMatch(each -> each.contains("secret") || each.contains("password") || each.contains("token")));
- assertThat(getStringList(actualRequestedSchema.get("required")), hasItems("field_1", "field_2"));
- }
-
- @SuppressWarnings("unchecked")
- private Map<String, Object> castStructuredContent(final Object value) {
- return (Map<String, Object>) value;
- }
-
- @SuppressWarnings("unchecked")
- private Map<String, Object> castToMap(final Object value) {
- return (Map<String, Object>) value;
- }
-
- private Map<String, Object> getMap(final Object value) {
- return value instanceof Map ? castToMap(value) : Map.of();
- }
-
- private List<Map<String, Object>> getMapList(final Object value) {
- return MCPPayloadAssertions.getMapList(value);
- }
-
- private List<String> getStringList(final Object value) {
- return ((List<?>) value).stream().map(String::valueOf).toList();
- }
-
- private static final class ProtocolAwareStdioClientTransport extends StdioClientTransport {
-
- private ProtocolAwareStdioClientTransport(final ServerParameters params) {
- super(params, MCPTransportJsonMapperFactory.create());
- }
-
- @Override
- public List<String> protocolVersions() {
- return List.of(ProtocolVersions.MCP_2025_06_18, ProtocolVersions.MCP_2025_11_25);
- }
- }
}
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
new file mode 100644
index 00000000000..f3b1f79528f
--- /dev/null
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/production/ProductionMySQLSQLRuntimeE2ETest.java
@@ -0,0 +1,373 @@
+/*
+ * 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.test.e2e.mcp.runtime.production;
+
+import io.modelcontextprotocol.client.McpSyncClient;
+import io.modelcontextprotocol.spec.McpSchema;
+import org.apache.shardingsphere.mcp.support.database.metadata.jdbc.RuntimeDatabaseConnectionException;
+import org.apache.shardingsphere.test.e2e.mcp.support.runtime.MySQLRuntimeTestSupport;
+import org.apache.shardingsphere.test.e2e.mcp.support.runtime.RuntimeTransport;
+import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPPayloadAssertions;
+import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPInteractionClient;
+import org.junit.jupiter.api.condition.EnabledIf;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.hasItems;
+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;
+
+@EnabledIf("isEnabled")
+class ProductionMySQLSQLRuntimeE2ETest extends AbstractProductionMySQLRuntimeE2ETest {
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertExecuteSelectWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ Map<String, Object> actual = 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));
+ assertThat(String.valueOf(actual.get("result_kind")), is("result_set"));
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertExecuteSelectWithTruncationWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ Map<String, Object> actual = interactionClient.call("database_gateway_execute_query",
+ Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql", "SELECT order_id, status FROM orders ORDER BY order_id", "max_rows", 1));
+ assertThat(String.valueOf(actual.get("result_kind")), is("result_set"));
+ assertThat(((List<?>) actual.get("rows")).size(), is(1));
+ assertThat(String.valueOf(actual.get("truncated")), is("true"));
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertExecuteExplainAnalyzeSelectWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ Map<String, Object> actual = interactionClient.call("database_gateway_execute_query",
+ Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql", "EXPLAIN ANALYZE SELECT * FROM orders WHERE order_id = 1", "max_rows", 10));
+ assertThat(String.valueOf(actual.get("result_kind")), is("result_set"));
+ assertThat(String.valueOf(actual.get("statement_type")), is("EXPLAIN ANALYZE"));
+ assertFalse(((List<?>) actual.get("rows")).isEmpty());
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertExecuteQueryTimeoutWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ Map<String, Object> actual = interactionClient.call("database_gateway_execute_query",
+ Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql", "SELECT SLEEP(2)", "timeout_ms", 1));
+ assertRecoveryResponse(actual);
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertRejectExecuteMultiStatementWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ Map<String, Object> actual = interactionClient.call("database_gateway_execute_query",
+ Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql", "SELECT 1; SELECT 2"));
+ assertRecoveryResponse(actual, "Only one SQL statement is allowed.", "multiple_sql_statements");
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertRejectExplainAnalyzeUpdateFromReadOnlyToolWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ Map<String, Object> actual = interactionClient.call("database_gateway_execute_query",
+ Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql",
+ "EXPLAIN ANALYZE UPDATE orders SET status = 'DONE' WHERE order_id = 1"));
+ assertRecoveryResponse(actual,
+ "database_gateway_execute_query only supports classifier-approved QUERY and EXPLAIN_ANALYZE statements. Use database_gateway_execute_update for side-effecting SQL.",
+ "unsafe_sql_attempted");
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertRejectLockingReadFromReadOnlyToolWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ Map<String, Object> actual = interactionClient.call("database_gateway_execute_query",
+ Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "sql", "SELECT * FROM orders FOR UPDATE"));
+ assertRecoveryResponse(actual, "Locking read statements such as SELECT ... FOR UPDATE are not supported by the MCP read-only contract.");
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertRejectLockingReadFromUpdateToolWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ Map<String, Object> actual = interactionClient.call("database_gateway_execute_update",
+ createExecuteUpdateArguments("SELECT * FROM orders FOR UPDATE"));
+ assertRecoveryResponse(actual, "Locking read statements such as SELECT ... FOR UPDATE are not supported by the MCP read-only contract.");
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertElicitMaskPlanningWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException {
+ useTransport(transport);
+ List<McpSchema.ElicitRequest> actualElicitationRequests = new CopyOnWriteArrayList<>();
+ try (McpSyncClient client = createElicitationClient(transport, actualElicitationRequests)) {
+ client.initialize();
+ McpSchema.CallToolResult actual = client.callTool(new McpSchema.CallToolRequest(MASK_PLAN_TOOL_NAME, Map.of(
+ "database", LOGICAL_DATABASE_NAME,
+ "schema", LOGICAL_DATABASE_NAME,
+ "table", "orders",
+ "column", "status",
+ "operation_type", "create",
+ "algorithm_type", "MASK_FROM_X_TO_Y")));
+ Map<String, Object> actualPayload = castStructuredContent(actual.structuredContent());
+ if (RuntimeTransport.HTTP == transport) {
+ assertThat(String.valueOf(actualPayload.get("status")), is("clarifying"));
+ assertThat(String.valueOf(actualPayload.get("fallback_reason")), is("remote_identity_required"));
+ assertTrue(actualElicitationRequests.isEmpty());
+ return;
+ }
+ assertThat(String.valueOf(actualPayload.get("status")), is("planned"));
+ assertThat(String.valueOf(actualPayload.get("current_step")), is("review"));
+ assertThat(String.valueOf(castToMap(castToMap(actualPayload.get("masked_property_preview")).get("primary")).get("from-x")), is("1"));
+ assertThat(String.valueOf(castToMap(castToMap(actualPayload.get("masked_property_preview")).get("primary")).get("to-y")), is("3"));
+ assertElicitationRequest(actualElicitationRequests);
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertExecuteUpdateWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws SQLException, IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ Map<String, Object> actual = interactionClient.call("database_gateway_execute_update",
+ createExecuteUpdateArguments("UPDATE orders SET status = 'PENDING' WHERE order_id = 1"));
+ assertThat(String.valueOf(actual.get("result_kind")), is("update_count"));
+ assertThat(String.valueOf(actual.get("affected_rows")), is("1"));
+ assertThat(MySQLRuntimeTestSupport.querySingleString(getContainer(), String.format("SELECT status FROM %s.orders WHERE order_id = 1", getPhysicalSchemaName())), is("PENDING"));
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertExecuteUpdateWithoutApprovalArgumentWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ Map<String, Object> actual = interactionClient.call("database_gateway_execute_update",
+ createExecuteUpdateArguments("UPDATE orders SET status = status WHERE order_id = -1"));
+ assertThat(String.valueOf(actual.get("result_kind")), is("update_count"));
+ assertThat(String.valueOf(actual.get("affected_rows")), is("0"));
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertRejectSequenceResourceWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ Map<String, Object> actual = interactionClient.readResource(
+ String.format("shardingsphere://databases/%s/schemas/%s/sequences", LOGICAL_DATABASE_NAME, LOGICAL_DATABASE_NAME));
+ assertThat(String.valueOf(actual.get("error_code")), is("json_rpc_error"));
+ assertThat(String.valueOf(actual.get("message")), is("Sequence resources are not supported for the current database."));
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertExecuteRollbackWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws SQLException, IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ Map<String, Object> beginResponse = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("BEGIN"));
+ interactionClient.call("database_gateway_execute_update",
+ createExecuteUpdateArguments("UPDATE orders SET status = 'PENDING' WHERE order_id = 1"));
+ Map<String, Object> rollbackResponse = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("ROLLBACK"));
+ assertThat(String.valueOf(beginResponse.get("message")), is("Transaction started."));
+ assertThat(String.valueOf(rollbackResponse.get("message")), is("Transaction rolled back."));
+ assertThat(MySQLRuntimeTestSupport.querySingleString(getContainer(), String.format("SELECT status FROM %s.orders WHERE order_id = 1", getPhysicalSchemaName())), is("NEW"));
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertRejectBlankSavepointNameWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ Map<String, Object> actual = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("SAVEPOINT"));
+ assertRecoveryResponse(actual, "Savepoint name is required.");
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertExecuteSavepointFlowWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws SQLException, IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("BEGIN"));
+ interactionClient.call("database_gateway_execute_update",
+ createExecuteUpdateArguments("UPDATE orders SET status = 'PENDING' WHERE order_id = 1"));
+ Map<String, Object> savepointResponse = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("SAVEPOINT sp_1"));
+ interactionClient.call("database_gateway_execute_update",
+ createExecuteUpdateArguments("UPDATE orders SET status = 'DONE' WHERE order_id = 1"));
+ Map<String, Object> rollbackResponse = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("ROLLBACK TO SAVEPOINT sp_1"));
+ Map<String, Object> commitResponse = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("COMMIT"));
+ assertThat(String.valueOf(savepointResponse.get("message")), is("Savepoint created."));
+ assertThat(String.valueOf(rollbackResponse.get("message")), is("Savepoint rolled back."));
+ assertThat(String.valueOf(commitResponse.get("message")), is("Transaction committed."));
+ assertThat(MySQLRuntimeTestSupport.querySingleString(getContainer(), String.format("SELECT status FROM %s.orders WHERE order_id = 1", getPhysicalSchemaName())), is("PENDING"));
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertExecuteReleaseSavepointWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("BEGIN"));
+ Map<String, Object> savepointResponse = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("SAVEPOINT sp_release"));
+ Map<String, Object> releaseResponse = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("RELEASE SAVEPOINT sp_release"));
+ Map<String, Object> commitResponse = interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("COMMIT"));
+ assertThat(String.valueOf(savepointResponse.get("message")), is("Savepoint created."));
+ assertThat(String.valueOf(releaseResponse.get("message")), is("Savepoint released."));
+ assertThat(String.valueOf(commitResponse.get("message")), is("Transaction committed."));
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertExecuteDdlRefreshesMetadataWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ Map<String, Object> executeResponse = interactionClient.call("database_gateway_execute_update",
+ createExecuteUpdateArguments("CREATE TABLE orders_archive (order_id INT PRIMARY KEY)"));
+ Map<String, Object> actualItem = MCPPayloadAssertions.getSingleItem(interactionClient.readResource(
+ "shardingsphere://databases/logic_db/schemas/logic_db/tables/orders_archive"));
+ assertThat(String.valueOf(executeResponse.get("message")), is("Statement executed."));
+ assertThat(String.valueOf(actualItem.get("table")), is("orders_archive"));
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertCloseRollsBackPendingTransactionWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws SQLException, IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments("BEGIN"));
+ interactionClient.call("database_gateway_execute_update",
+ createExecuteUpdateArguments("UPDATE orders SET status = 'PENDING' WHERE order_id = 1"));
+ interactionClient.close();
+ assertThat(MySQLRuntimeTestSupport.querySingleString(getContainer(), String.format("SELECT status FROM %s.orders WHERE order_id = 1", getPhysicalSchemaName())), is("NEW"));
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertAiNativeDeterministicInteractionLoopWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient()) {
+ assertAiNativeCapabilities(interactionClient.readResource("shardingsphere://capabilities"));
+ assertAiNativeDiscovery(interactionClient);
+ Map<String, Object> searchMetadataPayload = interactionClient.call("database_gateway_search_metadata",
+ Map.of("database", LOGICAL_DATABASE_NAME, "schema", LOGICAL_DATABASE_NAME, "query", "orders", "object_types", List.of("table")));
+ Map<String, Object> tableHit = MCPPayloadAssertions.findItem(searchMetadataPayload, "name", "orders");
+ String tableResourceUri = String.valueOf(getMap(tableHit.get("resource")).get("uri"));
+ assertThat(tableResourceUri, is("shardingsphere://databases/logic_db/schemas/logic_db/tables/orders"));
+ assertFalse(getMapList(tableHit.get("next_resources")).isEmpty());
+ MCPPayloadAssertions.assertSingleItemValue(interactionClient.readResource(tableResourceUri), "table", "orders");
+ assertAiNativeSqlPreview(interactionClient);
+ assertAiNativeSqlResult(interactionClient);
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertListDatabasesWithMultipleRuntimeDatabasesWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient(createPreparedProgrammaticRuntimeDatabases())) {
+ List<String> actualDatabaseNames = getPayloadItems(interactionClient.readResource("shardingsphere://databases")).stream()
+ .map(each -> String.valueOf(each.get("database"))).toList();
+ assertThat(actualDatabaseNames, hasItems(LOGICAL_DATABASE_NAME, "analytics_db", "warehouse"));
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertRefreshMetadataVisibleForTargetDatabaseOnlyWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient firstInteractionClient = createOpenedInteractionClient(createPreparedProgrammaticRuntimeDatabases())) {
+ firstInteractionClient.call("database_gateway_execute_update",
+ createExecuteUpdateArguments(LOGICAL_DATABASE_NAME, "CREATE TABLE orders_archive (order_id INT PRIMARY KEY)"));
+ List<String> firstSessionTableNames = readTableNames(firstInteractionClient, LOGICAL_DATABASE_NAME);
+ try (MCPInteractionClient secondInteractionClient = createOpenedInteractionClient(createPreparedProgrammaticRuntimeDatabases())) {
+ List<String> secondSessionTableNames = readTableNames(secondInteractionClient, LOGICAL_DATABASE_NAME);
+ List<String> analyticsDatabaseTableNames = readTableNames(secondInteractionClient, "analytics_db");
+ assertTrue(firstSessionTableNames.contains("orders_archive"));
+ assertTrue(secondSessionTableNames.contains("orders_archive"));
+ assertFalse(analyticsDatabaseTableNames.contains("orders_archive"));
+ assertTrue(analyticsDatabaseTableNames.contains("metrics"));
+ }
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertRejectCrossDatabaseTransactionSwitchWithActualMySQLBackend(final String name, final RuntimeTransport transport) throws IOException, InterruptedException {
+ useTransport(transport);
+ try (MCPInteractionClient interactionClient = createOpenedInteractionClient(createPreparedProgrammaticRuntimeDatabases())) {
+ interactionClient.call("database_gateway_execute_update", createExecuteUpdateArguments(LOGICAL_DATABASE_NAME, "BEGIN"));
+ Map<String, Object> actual = interactionClient.call("database_gateway_execute_query",
+ Map.of("database", "analytics_db", "schema", "analytics_db", "sql", "SELECT metric_name FROM metrics ORDER BY metric_id"));
+ assertRecoveryResponse(actual, "Cross-database transaction switching is not supported.");
+ }
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("transports")
+ void assertRejectMismatchedDatabaseTypeWithActualMySQLBackend(final String name, final RuntimeTransport transport) {
+ useTransport(transport);
+ if (RuntimeTransport.HTTP == transport) {
+ RuntimeDatabaseConnectionException actual = assertThrows(RuntimeDatabaseConnectionException.class,
+ () -> openAndCloseInteractionClient(createMismatchedRuntimeDatabases()));
+ assertThat(actual.getMessage(), is("Runtime database `logic_db` connection failed: invalid_configuration."));
+ assertThat(actual.getCategory(), is(RuntimeDatabaseConnectionException.CATEGORY_INVALID_CONFIGURATION));
+ assertThat(actual.getCause().getMessage(), is("Configured databaseType `PostgreSQL` does not match actual database type `MySQL` for database `logic_db`."));
+ return;
+ }
+ IllegalStateException actual = assertThrows(IllegalStateException.class, () -> openAndCloseInteractionClient(createMismatchedRuntimeDatabases()));
+ assertThat(actual.getMessage(), containsString("Runtime database `logic_db` connection failed: invalid_configuration."));
+ assertThat(actual.getMessage(), containsString("Configured databaseType `PostgreSQL` does not match actual database type `MySQL` for database `logic_db`."));
+ }
+}
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 baf97e0707a..e14349e0ea1 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
@@ -52,103 +52,6 @@ class HttpTransportContractE2ETest extends AbstractHttpProgrammaticRuntimeE2ETes
return MCPE2ECondition.isContractEnabled();
}
- @Test
- void assertInitializeSessionAndProtocolHeaders() throws IOException, InterruptedException {
- launchHttpTransport();
- HttpClient httpClient = HttpClient.newHttpClient();
- HttpResponse<String> actual = sendInitializeRequest(httpClient);
- assertThat(actual.statusCode(), is(200));
- assertTrue(actual.headers().firstValue("Content-Type").orElse("").startsWith("application/json"));
- assertThat(actual.headers().firstValue("MCP-Protocol-Version").orElse(""), is(getProtocolVersion()));
- String actualSessionId = actual.headers().firstValue("MCP-Session-Id").orElse("");
- assertFalse(actualSessionId.isEmpty());
- Map<String, Object> actualPayload = parseJsonBody(actual.body());
- Map<String, Object> actualResult = castToMap(actualPayload.get("result"));
- assertThat(String.valueOf(actualPayload.get("jsonrpc")), is("2.0"));
- assertThat(String.valueOf(actualResult.get("protocolVersion")), is(getProtocolVersion()));
- assertThat(sendInitializedNotification(httpClient, actualSessionId).statusCode(), is(202));
- }
-
- @Test
- void assertRejectInitializeWithoutAcceptHeader() throws IOException, InterruptedException {
- launchHttpTransport();
- HttpClient httpClient = HttpClient.newHttpClient();
- HttpRequest request = HttpRequest.newBuilder(getEndpointUri())
- .header("Content-Type", "application/json")
- .POST(HttpRequest.BodyPublishers.ofString(MCPHttpTransportTestSupport.createJsonRpcRequestBody(
- "init-1", "initialize", MCPHttpTransportTestSupport.createInitializeRequestParams("mcp-e2e-programmatic"))))
- .build();
- HttpResponse<String> actual = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
- assertThat(actual.statusCode(), is(400));
- assertFalse(actual.headers().firstValue("MCP-Session-Id").isPresent());
- }
-
- @Test
- void assertRejectInitializeWithUnsupportedAcceptHeader() throws IOException, InterruptedException {
- launchHttpTransport();
- HttpClient httpClient = HttpClient.newHttpClient();
- HttpResponse<String> actual = sendInitializeRequest(httpClient, Map.of("Accept", "application/json"),
- MCPHttpTransportTestSupport.createInitializeRequestParams("mcp-e2e-programmatic"));
- assertThat(actual.statusCode(), is(400));
- assertFalse(actual.headers().firstValue("MCP-Session-Id").isPresent());
- }
-
- @Test
- void assertRejectInitializeWithSseOnlyAcceptHeader() throws IOException, InterruptedException {
- launchHttpTransport();
- HttpClient httpClient = HttpClient.newHttpClient();
- HttpResponse<String> actual = sendInitializeRequest(httpClient, Map.of("Accept", "text/event-stream"),
- MCPHttpTransportTestSupport.createInitializeRequestParams("mcp-e2e-programmatic"));
- assertThat(actual.statusCode(), is(400));
- assertFalse(actual.headers().firstValue("MCP-Session-Id").isPresent());
- }
-
- @Test
- void assertRejectEventStreamWithoutAcceptHeader() throws IOException, InterruptedException {
- launchHttpTransport();
- HttpClient httpClient = HttpClient.newHttpClient();
- String sessionId = initializeSession(httpClient);
- HttpResponse<String> actual = openEventStream(httpClient, createSessionHeaders(sessionId));
- assertThat(actual.statusCode(), is(400));
- }
-
- @Test
- void assertRejectEventStreamWithUnsupportedAcceptHeader() throws IOException, InterruptedException {
- launchHttpTransport();
- HttpClient httpClient = HttpClient.newHttpClient();
- String sessionId = initializeSession(httpClient);
- Map<String, String> headers = new LinkedHashMap<>(createSessionHeaders(sessionId));
- headers.put("Accept", "application/json");
- HttpResponse<String> actual = openEventStream(httpClient, headers);
- assertThat(actual.statusCode(), is(400));
- }
-
- @Test
- void assertAcceptInitializeWithUnsupportedProtocolVersion() throws IOException, InterruptedException {
- launchHttpTransport();
- HttpClient httpClient = HttpClient.newHttpClient();
- Map<String, Object> initializeRequestParams = new LinkedHashMap<>(MCPHttpTransportTestSupport.createInitializeRequestParams("mcp-e2e-programmatic"));
- initializeRequestParams.put("protocolVersion", "2024-11-05");
- HttpResponse<String> actual = sendInitializeRequest(httpClient, initializeRequestParams);
- assertThat(actual.statusCode(), is(200));
- assertThat(actual.headers().firstValue("MCP-Protocol-Version").orElse(""), is(getProtocolVersion()));
- Map<String, Object> actualPayload = parseJsonBody(actual.body());
- assertThat(String.valueOf(castToMap(actualPayload.get("result")).get("protocolVersion")), is(getProtocolVersion()));
- }
-
- @Test
- void assertAcceptInitializeWithoutProtocolVersion() throws IOException, InterruptedException {
- launchHttpTransport();
- HttpClient httpClient = HttpClient.newHttpClient();
- Map<String, Object> initializeRequestParams = new LinkedHashMap<>(MCPHttpTransportTestSupport.createInitializeRequestParams("mcp-e2e-programmatic"));
- initializeRequestParams.remove("protocolVersion");
- HttpResponse<String> actual = sendInitializeRequest(httpClient, initializeRequestParams);
- assertThat(actual.statusCode(), is(200));
- assertThat(actual.headers().firstValue("MCP-Protocol-Version").orElse(""), is(getProtocolVersion()));
- Map<String, Object> actualPayload = parseJsonBody(actual.body());
- assertThat(String.valueOf(castToMap(actualPayload.get("result")).get("protocolVersion")), is(getProtocolVersion()));
- }
-
@Test
void assertAcceptFollowUpRequestWithLowercaseSessionHeaders() throws IOException, InterruptedException {
launchHttpTransport();
@@ -446,55 +349,4 @@ class HttpTransportContractE2ETest extends AbstractHttpProgrammaticRuntimeE2ETes
private Map<String, Object> findByKey(final List<Map<String, Object>> values, final String key, final String expectedValue) {
return values.stream().filter(each -> expectedValue.equals(each.get(key))).findFirst().orElseThrow();
}
-
- @Test
- void assertRejectFollowUpRequestWithoutProtocolHeader() throws IOException, InterruptedException {
- launchHttpTransport();
- HttpClient httpClient = HttpClient.newHttpClient();
- String sessionId = initializeSession(httpClient);
- HttpResponse<String> actual = sendCapabilitiesRequest(httpClient, Map.of("MCP-Session-Id", sessionId));
- assertThat(actual.statusCode(), is(400));
- }
-
- @Test
- void assertRejectFollowUpRequestWithProtocolMismatch() throws IOException, InterruptedException {
- launchHttpTransport();
- HttpClient httpClient = HttpClient.newHttpClient();
- String sessionId = initializeSession(httpClient);
- HttpResponse<String> actual = sendCapabilitiesRequest(httpClient, Map.of("MCP-Session-Id", sessionId, "MCP-Protocol-Version", "2024-11-05"));
- assertThat(actual.statusCode(), is(400));
- }
-
- @Test
- void assertRejectFollowUpRequestWithMissingSession() throws IOException, InterruptedException {
- launchHttpTransport();
- HttpClient httpClient = HttpClient.newHttpClient();
- initializeSession(httpClient);
- HttpResponse<String> actual = sendCapabilitiesRequest(httpClient,
- Map.of("MCP-Session-Id", "missing-session", "MCP-Protocol-Version", getProtocolVersion()));
- assertThat(actual.statusCode(), is(404));
- assertThat(String.valueOf(parseJsonBody(actual.body()).get("message")), is("Session not found: missing-session"));
- }
-
- @Test
- void assertRejectDeleteWithoutSessionHeader() throws IOException, InterruptedException {
- launchHttpTransport();
- HttpClient httpClient = HttpClient.newHttpClient();
- HttpResponse<String> actual = sendDeleteRequest(httpClient, Map.of("MCP-Protocol-Version", getProtocolVersion()));
- assertThat(actual.statusCode(), is(400));
- assertThat(String.valueOf(parseJsonBody(actual.body()).get("message")), is("Session ID required in mcp-session-id header"));
- }
-
- @Test
- void assertCloseSessionOnDelete() throws IOException, InterruptedException {
- launchHttpTransport();
- HttpClient httpClient = HttpClient.newHttpClient();
- String sessionId = initializeSession(httpClient);
- Map<String, String> sessionHeaders = Map.of("MCP-Session-Id", sessionId, "MCP-Protocol-Version", getProtocolVersion());
- HttpResponse<String> deleteResponse = sendDeleteRequest(httpClient, sessionHeaders);
- HttpResponse<String> actual = sendCapabilitiesRequest(httpClient, sessionHeaders);
- assertThat(deleteResponse.statusCode(), is(200));
- assertThat(actual.statusCode(), is(404));
- assertThat(String.valueOf(parseJsonBody(actual.body()).get("message")), is("Session not found: " + sessionId));
- }
}
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
new file mode 100644
index 00000000000..57b68ce3070
--- /dev/null
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/runtime/programmatic/HttpTransportProtocolContractE2ETest.java
@@ -0,0 +1,191 @@
+/*
+ * 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.test.e2e.mcp.runtime.programmatic;
+
+import org.apache.shardingsphere.test.e2e.mcp.env.MCPE2ECondition;
+import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPHttpTransportTestSupport;
+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.HttpRequest;
+import java.net.http.HttpResponse;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+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;
+
+@EnabledIf("isEnabled")
+class HttpTransportProtocolContractE2ETest extends AbstractHttpProgrammaticRuntimeE2ETest {
+
+ private static boolean isEnabled() {
+ return MCPE2ECondition.isContractEnabled();
+ }
+
+ @Test
+ void assertInitializeSessionAndProtocolHeaders() throws IOException, InterruptedException {
+ launchHttpTransport();
+ HttpClient httpClient = HttpClient.newHttpClient();
+ HttpResponse<String> actual = sendInitializeRequest(httpClient);
+ assertThat(actual.statusCode(), is(200));
+ assertTrue(actual.headers().firstValue("Content-Type").orElse("").startsWith("application/json"));
+ assertThat(actual.headers().firstValue("MCP-Protocol-Version").orElse(""), is(getProtocolVersion()));
+ String actualSessionId = actual.headers().firstValue("MCP-Session-Id").orElse("");
+ assertFalse(actualSessionId.isEmpty());
+ Map<String, Object> actualPayload = parseJsonBody(actual.body());
+ Map<String, Object> actualResult = castToMap(actualPayload.get("result"));
+ assertThat(String.valueOf(actualPayload.get("jsonrpc")), is("2.0"));
+ assertThat(String.valueOf(actualResult.get("protocolVersion")), is(getProtocolVersion()));
+ assertThat(sendInitializedNotification(httpClient, actualSessionId).statusCode(), is(202));
+ }
+
+ @Test
+ void assertRejectInitializeWithoutAcceptHeader() throws IOException, InterruptedException {
+ launchHttpTransport();
+ HttpClient httpClient = HttpClient.newHttpClient();
+ HttpRequest request = HttpRequest.newBuilder(getEndpointUri())
+ .header("Content-Type", "application/json")
+ .POST(HttpRequest.BodyPublishers.ofString(MCPHttpTransportTestSupport.createJsonRpcRequestBody(
+ "init-1", "initialize", MCPHttpTransportTestSupport.createInitializeRequestParams("mcp-e2e-programmatic"))))
+ .build();
+ HttpResponse<String> actual = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
+ assertThat(actual.statusCode(), is(400));
+ assertFalse(actual.headers().firstValue("MCP-Session-Id").isPresent());
+ }
+
+ @Test
+ void assertRejectInitializeWithUnsupportedAcceptHeader() throws IOException, InterruptedException {
+ launchHttpTransport();
+ HttpClient httpClient = HttpClient.newHttpClient();
+ HttpResponse<String> actual = sendInitializeRequest(httpClient, Map.of("Accept", "application/json"),
+ MCPHttpTransportTestSupport.createInitializeRequestParams("mcp-e2e-programmatic"));
+ assertThat(actual.statusCode(), is(400));
+ assertFalse(actual.headers().firstValue("MCP-Session-Id").isPresent());
+ }
+
+ @Test
+ void assertRejectInitializeWithSseOnlyAcceptHeader() throws IOException, InterruptedException {
+ launchHttpTransport();
+ HttpClient httpClient = HttpClient.newHttpClient();
+ HttpResponse<String> actual = sendInitializeRequest(httpClient, Map.of("Accept", "text/event-stream"),
+ MCPHttpTransportTestSupport.createInitializeRequestParams("mcp-e2e-programmatic"));
+ assertThat(actual.statusCode(), is(400));
+ assertFalse(actual.headers().firstValue("MCP-Session-Id").isPresent());
+ }
+
+ @Test
+ void assertRejectEventStreamWithoutAcceptHeader() throws IOException, InterruptedException {
+ launchHttpTransport();
+ HttpClient httpClient = HttpClient.newHttpClient();
+ String sessionId = initializeSession(httpClient);
+ HttpResponse<String> actual = openEventStream(httpClient, createSessionHeaders(sessionId));
+ assertThat(actual.statusCode(), is(400));
+ }
+
+ @Test
+ void assertRejectEventStreamWithUnsupportedAcceptHeader() throws IOException, InterruptedException {
+ launchHttpTransport();
+ HttpClient httpClient = HttpClient.newHttpClient();
+ String sessionId = initializeSession(httpClient);
+ Map<String, String> headers = new LinkedHashMap<>(createSessionHeaders(sessionId));
+ headers.put("Accept", "application/json");
+ HttpResponse<String> actual = openEventStream(httpClient, headers);
+ assertThat(actual.statusCode(), is(400));
+ }
+
+ @Test
+ void assertAcceptInitializeWithUnsupportedProtocolVersion() throws IOException, InterruptedException {
+ launchHttpTransport();
+ HttpClient httpClient = HttpClient.newHttpClient();
+ Map<String, Object> initializeRequestParams = new LinkedHashMap<>(MCPHttpTransportTestSupport.createInitializeRequestParams("mcp-e2e-programmatic"));
+ initializeRequestParams.put("protocolVersion", "2024-11-05");
+ HttpResponse<String> actual = sendInitializeRequest(httpClient, initializeRequestParams);
+ assertThat(actual.statusCode(), is(200));
+ assertThat(actual.headers().firstValue("MCP-Protocol-Version").orElse(""), is(getProtocolVersion()));
+ Map<String, Object> actualPayload = parseJsonBody(actual.body());
+ assertThat(String.valueOf(castToMap(actualPayload.get("result")).get("protocolVersion")), is(getProtocolVersion()));
+ }
+
+ @Test
+ void assertAcceptInitializeWithoutProtocolVersion() throws IOException, InterruptedException {
+ launchHttpTransport();
+ HttpClient httpClient = HttpClient.newHttpClient();
+ Map<String, Object> initializeRequestParams = new LinkedHashMap<>(MCPHttpTransportTestSupport.createInitializeRequestParams("mcp-e2e-programmatic"));
+ initializeRequestParams.remove("protocolVersion");
+ HttpResponse<String> actual = sendInitializeRequest(httpClient, initializeRequestParams);
+ assertThat(actual.statusCode(), is(200));
+ assertThat(actual.headers().firstValue("MCP-Protocol-Version").orElse(""), is(getProtocolVersion()));
+ Map<String, Object> actualPayload = parseJsonBody(actual.body());
+ assertThat(String.valueOf(castToMap(actualPayload.get("result")).get("protocolVersion")), is(getProtocolVersion()));
+ }
+
+ @Test
+ void assertRejectFollowUpRequestWithoutProtocolHeader() throws IOException, InterruptedException {
+ launchHttpTransport();
+ HttpClient httpClient = HttpClient.newHttpClient();
+ String sessionId = initializeSession(httpClient);
+ HttpResponse<String> actual = sendCapabilitiesRequest(httpClient, Map.of("MCP-Session-Id", sessionId));
+ assertThat(actual.statusCode(), is(400));
+ }
+
+ @Test
+ void assertRejectFollowUpRequestWithProtocolMismatch() throws IOException, InterruptedException {
+ launchHttpTransport();
+ HttpClient httpClient = HttpClient.newHttpClient();
+ String sessionId = initializeSession(httpClient);
+ HttpResponse<String> actual = sendCapabilitiesRequest(httpClient, Map.of("MCP-Session-Id", sessionId, "MCP-Protocol-Version", "2024-11-05"));
+ assertThat(actual.statusCode(), is(400));
+ }
+
+ @Test
+ void assertRejectFollowUpRequestWithMissingSession() throws IOException, InterruptedException {
+ launchHttpTransport();
+ HttpClient httpClient = HttpClient.newHttpClient();
+ initializeSession(httpClient);
+ HttpResponse<String> actual = sendCapabilitiesRequest(httpClient,
+ Map.of("MCP-Session-Id", "missing-session", "MCP-Protocol-Version", getProtocolVersion()));
+ assertThat(actual.statusCode(), is(404));
+ assertThat(String.valueOf(parseJsonBody(actual.body()).get("message")), is("Session not found: missing-session"));
+ }
+
+ @Test
+ void assertRejectDeleteWithoutSessionHeader() throws IOException, InterruptedException {
+ launchHttpTransport();
+ HttpClient httpClient = HttpClient.newHttpClient();
+ HttpResponse<String> actual = sendDeleteRequest(httpClient, Map.of("MCP-Protocol-Version", getProtocolVersion()));
+ assertThat(actual.statusCode(), is(400));
+ assertThat(String.valueOf(parseJsonBody(actual.body()).get("message")), is("Session ID required in mcp-session-id header"));
+ }
+
+ @Test
+ void assertCloseSessionOnDelete() throws IOException, InterruptedException {
+ launchHttpTransport();
+ HttpClient httpClient = HttpClient.newHttpClient();
+ String sessionId = initializeSession(httpClient);
+ Map<String, String> sessionHeaders = Map.of("MCP-Session-Id", sessionId, "MCP-Protocol-Version", getProtocolVersion());
+ HttpResponse<String> deleteResponse = sendDeleteRequest(httpClient, sessionHeaders);
+ HttpResponse<String> actual = sendCapabilitiesRequest(httpClient, sessionHeaders);
+ assertThat(deleteResponse.statusCode(), is(200));
+ assertThat(actual.statusCode(), is(404));
+ assertThat(String.valueOf(parseJsonBody(actual.body()).get("message")), is("Session not found: " + sessionId));
+ }
+}
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 6b62493bbae..d12c72ff4d2 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
@@ -96,7 +96,7 @@ class MetadataDiscoveryE2ETest extends AbstractHttpProgrammaticRuntimeE2ETest {
}
@Test
- void assertSearchMetadataDoesNotUseApplicationPagination() throws IOException, InterruptedException {
+ void assertSearchMetadataReturnsCompleteItemList() throws IOException, InterruptedException {
launchHttpTransport();
HttpClient httpClient = HttpClient.newHttpClient();
String sessionId = initializeSession(httpClient);
@@ -104,24 +104,24 @@ class MetadataDiscoveryE2ETest extends AbstractHttpProgrammaticRuntimeE2ETest {
createSearchArguments("logic_db", "logic_db", "order", List.of("table", "view")));
assertThat(actual.statusCode(), is(200));
Map<String, Object> actualPayload = getStructuredContent(actual.body());
+ assertThat(String.valueOf(actualPayload.get("response_mode")), is("search"));
+ assertThat(actualPayload.get("count"), is(3));
+ assertThat(actualPayload.get("total_match_count"), is(3));
assertThat(getItemNames(actualPayload), is(List.of("order_items", "orders", "active_orders")));
- assertFalse((boolean) actualPayload.get("has_more"));
- assertThat(String.valueOf(actualPayload.get("continuation_mode")), is("none"));
- assertFalse(actualPayload.containsKey("next_page_token"));
}
@Test
- void assertRejectUnsupportedPageTokenArgument() throws IOException, InterruptedException {
+ void assertRejectUnsupportedSearchArgument() throws IOException, InterruptedException {
launchHttpTransport();
HttpClient httpClient = HttpClient.newHttpClient();
String sessionId = initializeSession(httpClient);
Map<String, Object> arguments = createSearchArguments("logic_db", "logic_db", "order", List.of("table", "view"));
- arguments.put("page_token", "99");
+ arguments.put("client_hint", "narrow");
HttpResponse<String> actual = sendToolCallRequest(httpClient, sessionId, "database_gateway_search_metadata", arguments);
assertThat(actual.statusCode(), is(200));
Map<String, Object> recovery = getRecoveryPayload(getStructuredContent(actual.body()), "validation");
assertThat(String.valueOf(recovery.get("category")), is("unknown_argument"));
- assertThat(String.valueOf(recovery.get("field")), is("page_token"));
+ assertThat(String.valueOf(recovery.get("field")), is("client_hint"));
assertThat(recovery.get("suggested_arguments"), is(createSearchArguments("logic_db", "logic_db", "order", List.of("table", "view"))));
}
@@ -137,21 +137,6 @@ class MetadataDiscoveryE2ETest extends AbstractHttpProgrammaticRuntimeE2ETest {
assertThat(String.valueOf(actualPayload.get("message")), is("Schema cannot be provided without database."));
}
- @Test
- void assertRejectUnsupportedPageSizeArgument() throws IOException, InterruptedException {
- launchHttpTransport();
- HttpClient httpClient = HttpClient.newHttpClient();
- String sessionId = initializeSession(httpClient);
- Map<String, Object> arguments = createSearchArguments("logic_db", "logic_db", "order", List.of("table", "view"));
- arguments.put("page_size", 2);
- HttpResponse<String> actual = sendToolCallRequest(httpClient, sessionId, "database_gateway_search_metadata", arguments);
- assertThat(actual.statusCode(), is(200));
- Map<String, Object> recovery = getRecoveryPayload(getStructuredContent(actual.body()), "validation");
- assertThat(String.valueOf(recovery.get("category")), is("unknown_argument"));
- assertThat(String.valueOf(recovery.get("field")), is("page_size"));
- assertThat(recovery.get("suggested_arguments"), is(createSearchArguments("logic_db", "logic_db", "order", List.of("table", "view"))));
- }
-
@Test
void assertRejectUnsupportedObjectType() throws IOException, InterruptedException {
launchHttpTransport();
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/assertion/MCPModelContractAssertions.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/assertion/MCPModelContractAssertions.java
index a6c9b6fd399..c177359bdd3 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/assertion/MCPModelContractAssertions.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/assertion/MCPModelContractAssertions.java
@@ -44,7 +44,7 @@ public final class MCPModelContractAssertions {
}
/**
- * Assert that no legacy public fields are present recursively.
+ * Assert that no banned public fields are present recursively.
*
* @param value model-facing payload value
*/
@@ -75,7 +75,7 @@ public final class MCPModelContractAssertions {
private static void assertNoBannedPublicFieldMap(final Map<?, ?> value) {
for (Object each : value.keySet()) {
- assertFalse(BANNED_PUBLIC_FIELDS.contains(String.valueOf(each)), () -> "Legacy model-facing field returned: " + each);
+ assertFalse(BANNED_PUBLIC_FIELDS.contains(String.valueOf(each)), () -> "Banned model-facing field returned: " + each);
}
for (Object each : value.values()) {
assertNoBannedPublicFields(each);
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 a764540480e..41625472f50 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
@@ -17,6 +17,8 @@
package org.apache.shardingsphere.test.e2e.mcp.support.distribution;
+import org.apache.shardingsphere.test.e2e.mcp.support.runtime.ReadinessProbe;
+import org.apache.shardingsphere.test.e2e.mcp.support.runtime.ReadinessProbe.ReadinessResult;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPHttpInteractionClient;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPInteractionClient;
@@ -28,7 +30,6 @@ 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;
@@ -70,26 +71,8 @@ public final class DockerImageHttpRuntime implements AutoCloseable {
*/
public MCPInteractionClient openInteractionClient() throws IOException, InterruptedException {
startIfNeeded();
- long deadline = System.currentTimeMillis() + STARTUP_TIMEOUT_MILLIS;
- IllegalStateException lastException = null;
- while (System.currentTimeMillis() < deadline) {
- if (!process.isAlive()) {
- throw createStartupFailure(lastException);
- }
- MCPHttpInteractionClient result = new MCPHttpInteractionClient(URI.create("http://127.0.0.1:" + httpPort + "/mcp"), HttpClient.newHttpClient());
- try {
- result.open();
- return result;
- } catch (final IOException | IllegalStateException ex) {
- lastException = new IllegalStateException("Docker MCP HTTP distribution is not ready yet.", ex);
- closeInteractionClientQuietly(result);
- long remainingMillis = deadline - System.currentTimeMillis();
- if (0L < remainingMillis) {
- Thread.sleep(Math.min(STARTUP_POLL_INTERVAL_MILLIS, remainingMillis));
- }
- }
- }
- throw createStartupFailure(lastException);
+ return new ReadinessProbe(STARTUP_TIMEOUT_MILLIS, STARTUP_POLL_INTERVAL_MILLIS, STARTUP_POLL_INTERVAL_MILLIS)
+ .waitUntilReady(this::openInteractionClientIfReady, this::createStartupFailure);
}
@Override
@@ -125,13 +108,9 @@ public final class DockerImageHttpRuntime implements AutoCloseable {
}
private List<String> createDockerCommand(final int httpPort) {
- List<String> result = new LinkedList<>();
- result.addAll(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));
- return result;
+ 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);
}
private int allocatePort() throws IOException {
@@ -157,7 +136,21 @@ public final class DockerImageHttpRuntime implements AutoCloseable {
}
}
- private IllegalStateException createStartupFailure(final IllegalStateException cause) {
+ private ReadinessResult<MCPInteractionClient> openInteractionClientIfReady() throws InterruptedException {
+ if (!process.isAlive()) {
+ return ReadinessResult.failed(null);
+ }
+ MCPHttpInteractionClient result = new MCPHttpInteractionClient(URI.create("http://127.0.0.1:" + httpPort + "/mcp"), HttpClient.newHttpClient());
+ try {
+ result.open();
+ return ReadinessResult.ready(result);
+ } catch (final IOException | IllegalStateException ex) {
+ closeInteractionClientQuietly(result);
+ return ReadinessResult.retry(new IllegalStateException("Docker MCP HTTP distribution is not ready yet.", ex));
+ }
+ }
+
+ private IllegalStateException createStartupFailure(final Exception cause, final int ignoredAttemptCount, final long ignoredElapsedMillis) {
return new IllegalStateException("Docker MCP HTTP distribution failed to become ready. output: " + ProcessOutputDiagnostics.format(outputMessages), cause);
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/distribution/PackagedDistributionHttpRuntime.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/distribution/PackagedDistributionHttpRuntime.java
index 71de0e7470a..c5dc7930f82 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/distribution/PackagedDistributionHttpRuntime.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/distribution/PackagedDistributionHttpRuntime.java
@@ -18,6 +18,8 @@
package org.apache.shardingsphere.test.e2e.mcp.support.distribution;
import org.apache.shardingsphere.test.e2e.mcp.support.distribution.PackagedDistributionTestSupport.PreparedPackagedDistribution;
+import org.apache.shardingsphere.test.e2e.mcp.support.runtime.ReadinessProbe;
+import org.apache.shardingsphere.test.e2e.mcp.support.runtime.ReadinessProbe.ReadinessResult;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPHttpInteractionClient;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.client.MCPInteractionClient;
@@ -57,26 +59,8 @@ public final class PackagedDistributionHttpRuntime implements AutoCloseable {
*/
public MCPInteractionClient openInteractionClient() throws IOException, InterruptedException {
processSupport.startIfNeeded();
- long deadline = System.currentTimeMillis() + STARTUP_TIMEOUT_MILLIS;
- IllegalStateException lastException = null;
- while (System.currentTimeMillis() < deadline) {
- if (!processSupport.isAlive()) {
- throw createStartupFailure(lastException);
- }
- MCPHttpInteractionClient result = new MCPHttpInteractionClient(distribution.getEndpointUri(), HttpClient.newHttpClient());
- try {
- result.open();
- return result;
- } catch (final IOException | IllegalStateException ex) {
- lastException = new IllegalStateException("Packaged MCP HTTP distribution is not ready yet.", ex);
- closeInteractionClientQuietly(result);
- long remainingMillis = deadline - System.currentTimeMillis();
- if (0L < remainingMillis) {
- Thread.sleep(Math.min(STARTUP_POLL_INTERVAL_MILLIS, remainingMillis));
- }
- }
- }
- throw createStartupFailure(lastException);
+ return new ReadinessProbe(STARTUP_TIMEOUT_MILLIS, STARTUP_POLL_INTERVAL_MILLIS, STARTUP_POLL_INTERVAL_MILLIS)
+ .waitUntilReady(this::openInteractionClientIfReady, this::createStartupFailure);
}
@Override
@@ -84,7 +68,21 @@ public final class PackagedDistributionHttpRuntime implements AutoCloseable {
processSupport.close();
}
- private IllegalStateException createStartupFailure(final IllegalStateException cause) {
+ private ReadinessResult<MCPInteractionClient> openInteractionClientIfReady() throws InterruptedException {
+ if (!processSupport.isAlive()) {
+ return ReadinessResult.failed(null);
+ }
+ MCPHttpInteractionClient result = new MCPHttpInteractionClient(distribution.getEndpointUri(), HttpClient.newHttpClient());
+ try {
+ result.open();
+ return ReadinessResult.ready(result);
+ } catch (final IOException | IllegalStateException ex) {
+ closeInteractionClientQuietly(result);
+ return ReadinessResult.retry(new IllegalStateException("Packaged MCP HTTP distribution is not ready yet.", ex));
+ }
+ }
+
+ private IllegalStateException createStartupFailure(final Exception cause, final int ignoredAttemptCount, final long ignoredElapsedMillis) {
return new IllegalStateException("Packaged MCP HTTP distribution failed to become ready. output: " + ProcessOutputDiagnostics.format(processSupport.getOutputMessages()), cause);
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/runtime/MySQLRuntimeTestSupport.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/runtime/MySQLRuntimeTestSupport.java
index 0c9167259a3..c116155af7f 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/runtime/MySQLRuntimeTestSupport.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/runtime/MySQLRuntimeTestSupport.java
@@ -315,49 +315,31 @@ public final class MySQLRuntimeTestSupport {
private static Connection getConnection(final GenericContainer<?> container, final String databaseName) throws SQLException {
String jdbcUrl = createJdbcUrl(container.getHost(), container.getMappedPort(3306), databaseName);
- long startTime = System.currentTimeMillis();
- long deadline = System.currentTimeMillis() + JDBC_READY_TIMEOUT.toMillis();
- long intervalMillis = JDBC_READY_INITIAL_INTERVAL_MILLIS;
- int attemptCount = 0;
- SQLException lastException = null;
- while (System.currentTimeMillis() < deadline) {
- attemptCount++;
- try {
- return DriverManager.getConnection(jdbcUrl, USERNAME, PASSWORD);
- } catch (final SQLException ex) {
- lastException = ex;
- intervalMillis = sleepBeforeRetry(deadline, intervalMillis);
- }
- }
- throw createJdbcReadyException(lastException, attemptCount, startTime);
- }
-
- private static long sleepBeforeRetry(final long deadline, final long intervalMillis) throws SQLException {
- long remainingMillis = deadline - System.currentTimeMillis();
- if (0L >= remainingMillis) {
- return intervalMillis;
- }
try {
- Thread.sleep(Math.min(intervalMillis, remainingMillis));
- return Math.min(JDBC_READY_MAX_INTERVAL_MILLIS, intervalMillis * 2L);
+ return new ReadinessProbe(JDBC_READY_TIMEOUT.toMillis(), JDBC_READY_INITIAL_INTERVAL_MILLIS, JDBC_READY_MAX_INTERVAL_MILLIS)
+ .waitUntilReady(() -> getConnectionIfReady(jdbcUrl), MySQLRuntimeTestSupport::createJdbcReadyException);
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
throw new SQLException("Interrupted while waiting for MySQL JDBC readiness.", ex);
}
}
- private static SQLException createJdbcReadyException(final SQLException cause, final int attemptCount, final long startTimeMillis) {
+ private static ReadinessProbe.ReadinessResult<Connection> getConnectionIfReady(final String jdbcUrl) {
+ try {
+ return ReadinessProbe.ReadinessResult.ready(DriverManager.getConnection(jdbcUrl, USERNAME, PASSWORD));
+ } catch (final SQLException ex) {
+ return ReadinessProbe.ReadinessResult.retry(ex);
+ }
+ }
+
+ private static SQLException createJdbcReadyException(final Exception cause, final int attemptCount, final long elapsedMillis) {
String result = String.format("MySQL JDBC connection did not become ready after %d attempt(s), elapsedMillis=%d, timeoutMillis=%d.",
- attemptCount, System.currentTimeMillis() - startTimeMillis, JDBC_READY_TIMEOUT.toMillis());
+ attemptCount, elapsedMillis, JDBC_READY_TIMEOUT.toMillis());
return null == cause || null == cause.getMessage() || cause.getMessage().isBlank()
? new SQLException(result)
: new SQLException(result + " Last readiness failure: " + cause.getMessage(), cause);
}
- private static String createJdbcUrl(final String host, final int port) {
- return createJdbcUrl(host, port, DATABASE_NAME);
- }
-
private static String createJdbcUrl(final String host, final int port, final String databaseName) {
String databasePath = databaseName.isEmpty() ? "/" : "/" + databaseName;
return String.format("jdbc:mysql://%s:%d%s?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&characterEncoding=UTF-8&connectTimeout=3000&socketTimeout=3000",
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/runtime/ReadinessProbe.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/runtime/ReadinessProbe.java
new file mode 100644
index 00000000000..c51a08d4e1a
--- /dev/null
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/runtime/ReadinessProbe.java
@@ -0,0 +1,231 @@
+/*
+ * 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.test.e2e.mcp.support.runtime;
+
+import lombok.AccessLevel;
+import lombok.RequiredArgsConstructor;
+
+import java.util.function.LongSupplier;
+
+/**
+ * Readiness retry probe.
+ */
+public final class ReadinessProbe {
+
+ private final long timeoutMillis;
+
+ private final long initialIntervalMillis;
+
+ private final long maxIntervalMillis;
+
+ private final LongSupplier currentTimeMillis;
+
+ private final Sleeper sleeper;
+
+ /**
+ * Construct readiness retry probe.
+ *
+ * @param timeoutMillis timeout milliseconds
+ * @param initialIntervalMillis initial retry interval milliseconds
+ * @param maxIntervalMillis maximum retry interval milliseconds
+ */
+ public ReadinessProbe(final long timeoutMillis, final long initialIntervalMillis, final long maxIntervalMillis) {
+ this(timeoutMillis, initialIntervalMillis, maxIntervalMillis, System::currentTimeMillis, Thread::sleep);
+ }
+
+ ReadinessProbe(final long timeoutMillis, final long initialIntervalMillis, final long maxIntervalMillis, final LongSupplier currentTimeMillis, final Sleeper sleeper) {
+ checkArguments(timeoutMillis, initialIntervalMillis, maxIntervalMillis);
+ this.timeoutMillis = timeoutMillis;
+ this.initialIntervalMillis = initialIntervalMillis;
+ this.maxIntervalMillis = maxIntervalMillis;
+ this.currentTimeMillis = currentTimeMillis;
+ this.sleeper = sleeper;
+ }
+
+ private void checkArguments(final long timeoutMillis, final long initialIntervalMillis, final long maxIntervalMillis) {
+ if (0L >= timeoutMillis) {
+ throw new IllegalArgumentException("Readiness timeout must be positive.");
+ }
+ if (0L >= initialIntervalMillis) {
+ throw new IllegalArgumentException("Readiness initial interval must be positive.");
+ }
+ if (initialIntervalMillis > maxIntervalMillis) {
+ throw new IllegalArgumentException("Readiness maximum interval must not be less than the initial interval.");
+ }
+ }
+
+ /**
+ * Wait until a readiness check succeeds.
+ *
+ * @param readinessCheck readiness check
+ * @param failureFactory failure factory
+ * @param <T> ready value type
+ * @param <E> failure exception type
+ * @return ready value
+ * @throws E failure exception
+ * @throws InterruptedException interrupted exception
+ */
+ public <T, E extends Exception> T waitUntilReady(final ReadinessCheck<T> readinessCheck, final FailureFactory<E> failureFactory) throws E, InterruptedException {
+ long startTimeMillis = currentTimeMillis.getAsLong();
+ long deadlineMillis = startTimeMillis + timeoutMillis;
+ long intervalMillis = initialIntervalMillis;
+ int attemptCount = 0;
+ Exception lastFailure = null;
+ while (currentTimeMillis.getAsLong() < deadlineMillis) {
+ attemptCount++;
+ ReadinessResult<T> readinessResult = readinessCheck.check();
+ if (readinessResult.isReady()) {
+ return readinessResult.getValue();
+ }
+ if (null != readinessResult.getFailure()) {
+ lastFailure = readinessResult.getFailure();
+ }
+ if (readinessResult.isFailed()) {
+ throw failureFactory.create(lastFailure, attemptCount, getElapsedMillis(startTimeMillis));
+ }
+ intervalMillis = sleepBeforeRetry(deadlineMillis, intervalMillis);
+ }
+ throw failureFactory.create(lastFailure, attemptCount, getElapsedMillis(startTimeMillis));
+ }
+
+ private long sleepBeforeRetry(final long deadlineMillis, final long intervalMillis) throws InterruptedException {
+ long remainingMillis = deadlineMillis - currentTimeMillis.getAsLong();
+ if (0L >= remainingMillis) {
+ return intervalMillis;
+ }
+ try {
+ sleeper.sleep(Math.min(intervalMillis, remainingMillis));
+ return Math.min(maxIntervalMillis, intervalMillis * 2L);
+ } catch (final InterruptedException ex) {
+ Thread.currentThread().interrupt();
+ throw ex;
+ }
+ }
+
+ private long getElapsedMillis(final long startTimeMillis) {
+ return currentTimeMillis.getAsLong() - startTimeMillis;
+ }
+
+ /**
+ * Readiness check.
+ *
+ * @param <T> ready value type
+ */
+ @FunctionalInterface
+ public interface ReadinessCheck<T> {
+
+ /**
+ * Check readiness.
+ *
+ * @return readiness result
+ * @throws InterruptedException interrupted exception
+ */
+ ReadinessResult<T> check() throws InterruptedException;
+ }
+
+ /**
+ * Failure factory.
+ *
+ * @param <E> failure exception type
+ */
+ @FunctionalInterface
+ public interface FailureFactory<E extends Exception> {
+
+ /**
+ * Create failure.
+ *
+ * @param cause last readiness failure
+ * @param attemptCount attempt count
+ * @param elapsedMillis elapsed milliseconds
+ * @return failure exception
+ */
+ E create(Exception cause, int attemptCount, long elapsedMillis);
+ }
+
+ @FunctionalInterface
+ interface Sleeper {
+
+ void sleep(long millis) throws InterruptedException;
+ }
+
+ /**
+ * Readiness result.
+ *
+ * @param <T> ready value type
+ */
+ @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
+ public static final class ReadinessResult<T> {
+
+ private final T value;
+
+ private final Exception failure;
+
+ private final boolean ready;
+
+ private final boolean failed;
+
+ /**
+ * Create ready result.
+ *
+ * @param value ready value
+ * @param <T> ready value type
+ * @return ready result
+ */
+ public static <T> ReadinessResult<T> ready(final T value) {
+ return new ReadinessResult<>(value, null, true, false);
+ }
+
+ /**
+ * Create retry result.
+ *
+ * @param failure retryable failure
+ * @param <T> ready value type
+ * @return retry result
+ */
+ public static <T> ReadinessResult<T> retry(final Exception failure) {
+ return new ReadinessResult<>(null, failure, false, false);
+ }
+
+ /**
+ * Create failed result.
+ *
+ * @param failure non-retryable failure
+ * @param <T> ready value type
+ * @return failed result
+ */
+ public static <T> ReadinessResult<T> failed(final Exception failure) {
+ return new ReadinessResult<>(null, failure, false, true);
+ }
+
+ private T getValue() {
+ return value;
+ }
+
+ private Exception getFailure() {
+ return failure;
+ }
+
+ private boolean isReady() {
+ return ready;
+ }
+
+ private boolean isFailed() {
+ return failed;
+ }
+ }
+}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/runtime/ReadinessProbeTest.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/runtime/ReadinessProbeTest.java
new file mode 100644
index 00000000000..4f731780c18
--- /dev/null
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/runtime/ReadinessProbeTest.java
@@ -0,0 +1,86 @@
+/*
+ * 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.test.e2e.mcp.support.runtime;
+
+import org.apache.shardingsphere.test.e2e.mcp.support.runtime.ReadinessProbe.ReadinessResult;
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class ReadinessProbeTest {
+
+ @Test
+ void assertWaitUntilReadyAfterRetry() throws InterruptedException {
+ AtomicLong currentTimeMillis = new AtomicLong();
+ AtomicLong actualSleepMillis = new AtomicLong();
+ AtomicInteger actualAttemptCount = new AtomicInteger();
+ ReadinessProbe probe = new ReadinessProbe(1000L, 250L, 500L, currentTimeMillis::get, millis -> {
+ actualSleepMillis.addAndGet(millis);
+ currentTimeMillis.addAndGet(millis);
+ });
+ String actual = probe.waitUntilReady(() -> 1 == actualAttemptCount.incrementAndGet()
+ ? ReadinessResult.retry(new IllegalStateException("not ready"))
+ : ReadinessResult.ready("ready"), this::createFailure);
+ assertThat(actual, is("ready"));
+ assertThat(actualAttemptCount.get(), is(2));
+ assertThat(actualSleepMillis.get(), is(250L));
+ }
+
+ @Test
+ void assertWaitUntilReadyTimeout() {
+ AtomicLong currentTimeMillis = new AtomicLong();
+ AtomicInteger actualAttemptCount = new AtomicInteger();
+ ReadinessProbe probe = new ReadinessProbe(500L, 250L, 500L, currentTimeMillis::get, currentTimeMillis::addAndGet);
+ IllegalStateException actual = assertThrows(IllegalStateException.class,
+ () -> probe.waitUntilReady(() -> ReadinessResult.retry(new IllegalStateException("not ready " + actualAttemptCount.incrementAndGet())), this::createFailure));
+ assertThat(actual.getMessage(), is("attempts=2, elapsedMillis=500, cause=not ready 2"));
+ }
+
+ @Test
+ void assertWaitUntilReadyInterrupted() {
+ ReadinessProbe probe = new ReadinessProbe(1000L, 250L, 500L, System::currentTimeMillis, ignored -> {
+ throw new InterruptedException("interrupted");
+ });
+ InterruptedException actual = assertThrows(InterruptedException.class,
+ () -> probe.waitUntilReady(() -> ReadinessResult.retry(new IllegalStateException("not ready")), this::createFailure));
+ assertThat(actual.getMessage(), is("interrupted"));
+ assertTrue(Thread.currentThread().isInterrupted());
+ Thread.interrupted();
+ }
+
+ @Test
+ void assertWaitUntilReadyStopsForFailure() {
+ AtomicInteger actualSleepCount = new AtomicInteger();
+ ReadinessProbe probe = new ReadinessProbe(1000L, 250L, 500L, System::currentTimeMillis, ignored -> actualSleepCount.incrementAndGet());
+ IllegalStateException actual = assertThrows(IllegalStateException.class,
+ () -> probe.waitUntilReady(() -> ReadinessResult.failed(new IllegalStateException("bad request")), this::createFailure));
+ assertTrue(actual.getMessage().startsWith("attempts=1, elapsedMillis="));
+ assertTrue(actual.getMessage().endsWith(", cause=bad request"));
+ assertThat(actualSleepCount.get(), is(0));
+ }
+
+ private IllegalStateException createFailure(final Exception cause, final int attemptCount, final long elapsedMillis) {
+ return new IllegalStateException(String.format("attempts=%d, elapsedMillis=%d, cause=%s", attemptCount, elapsedMillis, cause.getMessage()), cause);
+ }
+}