diff --git a/docs/adr/ADR-035-cross-platform-market-campaign-intelligence.md b/docs/adr/ADR-035-cross-platform-market-campaign-intelligence.md index b9377a5..4163e6a 100644 --- a/docs/adr/ADR-035-cross-platform-market-campaign-intelligence.md +++ b/docs/adr/ADR-035-cross-platform-market-campaign-intelligence.md @@ -152,6 +152,7 @@ EwoooC 目前已有 MOMO EDM / 節慶活動資料、`promo_products`、PChome - 2026-05-12 追加 platform seed DB diff probe:`/api/market_intel/platform_seed_db_diff` 預設只回 planned,不連 DB;人工 smoke 才能以明確參數只讀查詢 `market_platforms`,比對 adapter seed 是否 missing / differs / matches。探針不得使用 `DatabaseManager()`、不得建立 ORM session、不得寫入、不得 commit。 - 2026-05-13 追加 platform seed CLI writer:`scripts/market_intel_seed_writer.py` 可在 CLI 明確帶入 `--execute`、`--apply-real-write` 與確認 token 時,以 SQLAlchemy Core 短 transaction upsert `market_platforms`;API 仍不得替使用者執行 DB 寫入,不建立 ORM session、不連外、不掛 scheduler。 - 2026-05-18 追加 legacy source bridge preview:`/api/market_intel/legacy_source_bridge` 預設 `execute=false` 只回 planned,不連 DB;人工 smoke 才能以 `execute=true` 只讀盤點 `promo_products`、`competitor_prices`、`competitor_price_history`,產生舊資料導入 `market_*` 的 mapping、dedupe 與 blocked operation preview。此橋接不得寫入 DB、不得建立 ORM session、不得把 PChome 比價快取冒充為活動頁商品、不得掛 scheduler。 +- 2026-05-18 追加 MCP readiness preview:`/api/market_intel/mcp_readiness` 預設 `execute=false` 只回 planned,盤點 ADR-031 外部 MCP server、`services.mcp_router` feature flag、tool registry、`mcp_calls` telemetry 與 market_intel tool contract 缺口。人工 smoke 才能以 `execute=true` 做只讀 health / telemetry probe;此探針不得寫 DB、不得建立 ORM session、不得替市場情報自動啟用 MCP 或外部爬取。 ### Phase 4:Coupang / Shopee Adapter diff --git a/routes/README.md b/routes/README.md index b247a22..06bd6aa 100644 --- a/routes/README.md +++ b/routes/README.md @@ -19,7 +19,7 @@ | `edm_routes.py` | EDM 與節慶儀表板 | `/edm`, `/festival` | | `monthly_routes.py` | 月結分析 | `/monthly_summary_analysis`, `/api/monthly_summary_data` | | `daily_sales_routes.py` | 當日業績 | `/daily_sales`, `/daily_sales/export*` | -| `market_intel_routes.py` | 市場情報 Phase 27 legacy source bridge preview | `/market_intel`, `/market_intel/*`, `/api/market_intel/status`, `/api/market_intel/schema`, `/api/market_intel/schema_smoke`, `/api/market_intel/schema_db_probe`, `/api/market_intel/platform_seed_db_diff`, `/api/market_intel/legacy_source_bridge`, `/api/market_intel/adapters`, `/api/market_intel/dry_run_plan`, `/api/market_intel/discovery_plan`, `/api/market_intel/manual_discovery`, `/api/market_intel/candidate_preview`, `/api/market_intel/platform_seed_plan`, `/api/market_intel/platform_seed_write_guard`, `/api/market_intel/platform_seed_writer_plan`, `/api/market_intel/migration_blueprint`, `/api/market_intel/seed_writer_cli_status`, `/api/market_intel/write_approval_runbook`, `/api/market_intel/deployment_readiness` | +| `market_intel_routes.py` | 市場情報 Phase 28 MCP readiness preview | `/market_intel`, `/market_intel/*`, `/api/market_intel/status`, `/api/market_intel/schema`, `/api/market_intel/schema_smoke`, `/api/market_intel/schema_db_probe`, `/api/market_intel/platform_seed_db_diff`, `/api/market_intel/legacy_source_bridge`, `/api/market_intel/mcp_readiness`, `/api/market_intel/adapters`, `/api/market_intel/dry_run_plan`, `/api/market_intel/discovery_plan`, `/api/market_intel/manual_discovery`, `/api/market_intel/candidate_preview`, `/api/market_intel/platform_seed_plan`, `/api/market_intel/platform_seed_write_guard`, `/api/market_intel/platform_seed_writer_plan`, `/api/market_intel/migration_blueprint`, `/api/market_intel/seed_writer_cli_status`, `/api/market_intel/write_approval_runbook`, `/api/market_intel/deployment_readiness` | | `api_routes.py` | 通用任務與查詢 API | `/api/run_task`, `/api/history/*` | | `export_routes.py` | 匯出功能 | `/api/export/*` | | `import_routes.py` | 匯入功能 | `/api/import_excel`, `/api/import/monthly_summary` | diff --git a/routes/market_intel_routes.py b/routes/market_intel_routes.py index d06c293..5669287 100644 --- a/routes/market_intel_routes.py +++ b/routes/market_intel_routes.py @@ -107,6 +107,19 @@ def market_intel_legacy_source_bridge(): ) +@market_intel_bp.route("/api/market_intel/mcp_readiness") +@login_required +def market_intel_mcp_readiness(): + execute_requested = request.args.get("execute", "false").lower() == "true" + timeout_sec = request.args.get("timeout", default=3, type=int) + return jsonify( + _service().build_mcp_readiness( + execute_requested=execute_requested, + timeout_sec=timeout_sec, + ) + ) + + @market_intel_bp.route("/api/market_intel/adapters") @login_required def market_intel_adapters(): diff --git a/services/market_intel/mcp_readiness.py b/services/market_intel/mcp_readiness.py new file mode 100644 index 0000000..5802158 --- /dev/null +++ b/services/market_intel/mcp_readiness.py @@ -0,0 +1,257 @@ +"""市場情報 MCP 整合就緒度 preview。 + +只檢查 MCP 設計、feature flag、tool registry、server health 與 telemetry 表狀態; +預設不連線、不寫 DB、不呼叫外部工具。 +""" + +from sqlalchemy import create_engine, inspect, text + + +EXPECTED_EXTERNAL_SERVERS = ("postgres", "omnisearch", "firecrawl", "filesystem") +EXPECTED_MARKET_INTEL_TOOLS = ( + "market_campaign_search", + "market_campaign_scrape", + "market_product_match_lookup", +) + + +def _planned_server_statuses(base_hosts): + return [ + { + "server": server, + "base_url": base_hosts.get(server), + "configured": bool(base_hosts.get(server)), + "health_checked": False, + "healthy": False, + "status": "planned_no_health_check", + "error_message": None, + } + for server in EXPECTED_EXTERNAL_SERVERS + ] + + +def _health_check_servers(base_hosts, timeout_sec): + import requests + + statuses = [] + for server in EXPECTED_EXTERNAL_SERVERS: + base_url = base_hosts.get(server) + status = { + "server": server, + "base_url": base_url, + "configured": bool(base_url), + "health_checked": False, + "healthy": False, + "status": "not_configured" if not base_url else "error", + "error_message": None, + } + if not base_url: + statuses.append(status) + continue + try: + response = requests.get( + f"{base_url.rstrip('/')}/health", + timeout=timeout_sec, + ) + status["health_checked"] = True + status["healthy"] = response.status_code == 200 + status["status"] = "healthy" if status["healthy"] else f"http_{response.status_code}" + except Exception as exc: + status["health_checked"] = True + status["status"] = "error" + status["error_message"] = str(exc)[:240] + statuses.append(status) + return statuses + + +def _build_telemetry_status(*, execute_requested, engine=None, database_url=None, database_type=None): + if not execute_requested: + return { + "mode": "mcp_telemetry_planned", + "table": "mcp_calls", + "read_only_query_executed": False, + "database_connection_opened": False, + "database_session_created": False, + "database_write_executed": False, + "database_commit_executed": False, + "table_exists": False, + "total_calls": None, + "recent_24h_calls": None, + "server_counts": [], + } + + from config import DATABASE_PATH, DATABASE_TYPE + + effective_database_url = database_url or DATABASE_PATH + effective_database_type = (database_type or DATABASE_TYPE or "").lower() + created_engine = False + connection_opened = False + try: + if engine is None: + connect_args = {} + if effective_database_type == "postgresql": + connect_args = { + "connect_timeout": 8, + "options": "-c statement_timeout=15000", + } + engine = create_engine( + effective_database_url, + isolation_level="AUTOCOMMIT", + pool_pre_ping=True, + connect_args=connect_args, + ) + created_engine = True + + table_exists = inspect(engine).has_table("mcp_calls") + total_calls = None + recent_24h_calls = None + server_counts = [] + with engine.connect() as conn: + connection_opened = True + if table_exists: + total_calls = conn.execute(text("SELECT COUNT(*) FROM mcp_calls")).scalar() + if effective_database_type == "postgresql": + recent_24h_calls = conn.execute( + text("SELECT COUNT(*) FROM mcp_calls WHERE called_at >= NOW() - INTERVAL '24 hours'") + ).scalar() + else: + recent_24h_calls = total_calls + server_counts = [ + {"server": row[0], "calls": int(row[1])} + for row in conn.execute( + text( + """ + SELECT server, COUNT(*) AS calls + FROM mcp_calls + GROUP BY server + ORDER BY calls DESC + LIMIT 10 + """ + ) + ).fetchall() + ] + + return { + "mode": "mcp_telemetry_read_only", + "table": "mcp_calls", + "read_only_query_executed": True, + "database_connection_opened": connection_opened, + "database_session_created": False, + "database_write_executed": False, + "database_commit_executed": False, + "table_exists": bool(table_exists), + "total_calls": int(total_calls or 0) if table_exists else None, + "recent_24h_calls": int(recent_24h_calls or 0) if table_exists else None, + "server_counts": server_counts, + } + except Exception as exc: + return { + "mode": "mcp_telemetry_error", + "table": "mcp_calls", + "read_only_query_executed": False, + "database_connection_opened": connection_opened, + "database_session_created": False, + "database_write_executed": False, + "database_commit_executed": False, + "table_exists": False, + "total_calls": None, + "recent_24h_calls": None, + "server_counts": [], + "error_message": str(exc)[:400], + } + finally: + if created_engine: + engine.dispose() + + +def build_mcp_readiness_plan( + *, + execute_requested=False, + timeout_sec=3, + engine=None, + database_url=None, + database_type=None, +): + """建立市場情報 MCP readiness;預設不做 health check / DB query。""" + from services.mcp_router import MCP_BASE_HOSTS, TOOL_REGISTRY, is_mcp_router_enabled + + execute_requested = bool(execute_requested) + timeout_sec = max(1, min(int(timeout_sec or 3), 10)) + router_enabled = bool(is_mcp_router_enabled()) + server_statuses = ( + _health_check_servers(MCP_BASE_HOSTS, timeout_sec) + if execute_requested + else _planned_server_statuses(MCP_BASE_HOSTS) + ) + telemetry = _build_telemetry_status( + execute_requested=execute_requested, + engine=engine, + database_url=database_url, + database_type=database_type, + ) + + registered_callers = sorted(TOOL_REGISTRY.keys()) + market_intel_tools = TOOL_REGISTRY.get("market_intel", {}) + market_intel_tool_count = sum(len(tools) for tools in market_intel_tools.values()) + external_mcp_complete = bool( + router_enabled + and execute_requested + and server_statuses + and all(item["healthy"] for item in server_statuses) + ) + internal_mcp_complete = bool( + "market_intel" in TOOL_REGISTRY + and market_intel_tool_count >= len(EXPECTED_MARKET_INTEL_TOOLS) + and telemetry.get("table_exists") + ) + market_intel_mcp_integrated = bool( + "market_intel" in TOOL_REGISTRY + and market_intel_tool_count > 0 + ) + + readiness_checks = { + "mcp_router_module_present": True, + "mcp_router_enabled": router_enabled, + "external_server_health_checked": execute_requested, + "external_servers_all_healthy": all(item["healthy"] for item in server_statuses), + "mcp_calls_table_exists": bool(telemetry.get("table_exists")), + "base_callers_registered": {"mcp_collector", "hermes_analyst", "openclaw_strategist"} <= set(registered_callers), + "market_intel_caller_registered": "market_intel" in TOOL_REGISTRY, + "market_intel_tools_registered": market_intel_tool_count >= len(EXPECTED_MARKET_INTEL_TOOLS), + } + blocked_reasons = [ + key for key, passed in readiness_checks.items() + if not passed + ] + if not execute_requested: + blocked_reasons.insert(0, "execute_false_planned_only") + + return { + "mode": "mcp_readiness_read_only" if execute_requested else "mcp_readiness_planned", + "execute_requested": execute_requested, + "router_enabled": router_enabled, + "external_mcp_complete": external_mcp_complete, + "internal_mcp_complete": internal_mcp_complete, + "market_intel_mcp_integrated": market_intel_mcp_integrated, + "server_statuses": server_statuses, + "registered_callers": registered_callers, + "market_intel_tools": market_intel_tools, + "market_intel_tool_count": market_intel_tool_count, + "expected_market_intel_tools": list(EXPECTED_MARKET_INTEL_TOOLS), + "telemetry": telemetry, + "readiness_checks": readiness_checks, + "database_session_created": False, + "database_write_executed": False, + "database_commit_executed": False, + "external_network_executed": False, + "scheduler_attached": False, + "writes_executed": False, + "would_write_database": False, + "blocked_reasons": blocked_reasons, + "next_required_steps": [ + "部署並健康檢查 docker-compose.mcp.yml 的 postgres / omnisearch / firecrawl / filesystem", + "在正式環境設定 MCP_ROUTER_ENABLED=true 與 MCP_* URL / API keys", + "建立 market_intel caller 的 MCP tool 白名單與 read-only tool contract", + "把 market_intel discovery / bridge preview 改成先走 MCP readiness,再允許人工 fetch", + ], + } diff --git a/services/market_intel/service.py b/services/market_intel/service.py index bba9af1..4500316 100644 --- a/services/market_intel/service.py +++ b/services/market_intel/service.py @@ -20,6 +20,7 @@ from services.market_intel.adapters import ( from services.market_intel.candidate_preview import build_candidate_preview_from_discovery from services.market_intel.discovery_runner import ManualDiscoveryRunner from services.market_intel.legacy_source_bridge import build_legacy_source_bridge_plan +from services.market_intel.mcp_readiness import build_mcp_readiness_plan from services.market_intel.migration_blueprint import build_migration_blueprint from services.market_intel.platform_seed import build_platform_seed_rows from services.market_intel.platform_seed_db_diff import build_platform_seed_db_diff_plan @@ -63,7 +64,7 @@ class MarketIntelRuntimeStatus: class MarketIntelService: """市場情報入口服務,先集中 feature gate 與安全狀態。""" - phase = "phase_27_legacy_source_bridge_preview" + phase = "phase_28_mcp_readiness_preview" def get_runtime_status(self) -> MarketIntelRuntimeStatus: return MarketIntelRuntimeStatus( @@ -300,6 +301,26 @@ class MarketIntelService: bridge["phase"] = self.phase return bridge + def build_mcp_readiness( + self, + *, + execute_requested=False, + timeout_sec=3, + engine=None, + database_url=None, + database_type=None, + ): + """回報市場情報 MCP 整合就緒度;預設不連 MCP server、不查 DB。""" + readiness = build_mcp_readiness_plan( + execute_requested=execute_requested, + timeout_sec=timeout_sec, + engine=engine, + database_url=database_url, + database_type=database_type, + ) + readiness["phase"] = self.phase + return readiness + def build_platform_seed_writer_plan(self, platform_code="all"): """建立 platform seed writer dry-run plan,不建立 DB session。""" seed_plan = self.build_platform_seed_plan(platform_code=platform_code) @@ -401,6 +422,9 @@ class MarketIntelService: "legacy_source_bridge_planned_safe": bool( not self.build_legacy_source_bridge()["read_only_query_executed"] ), + "mcp_readiness_planned_safe": bool( + self.build_mcp_readiness()["mode"] == "mcp_readiness_planned" + ), } ready_for_production_deploy = all(checks.values()) blocked_reasons = [ @@ -524,6 +548,7 @@ class MarketIntelService: "/api/market_intel/schema_db_probe", "/api/market_intel/platform_seed_db_diff", "/api/market_intel/legacy_source_bridge", + "/api/market_intel/mcp_readiness", ], "status": status.to_dict(), "schema_smoke": schema_smoke, @@ -538,4 +563,5 @@ class MarketIntelService: "schema_db_probe": self.build_schema_db_probe(), "platform_seed_db_diff": self.build_platform_seed_db_diff(), "legacy_source_bridge": self.build_legacy_source_bridge(), + "mcp_readiness": self.build_mcp_readiness(), } diff --git a/templates/market_intel/disabled.html b/templates/market_intel/disabled.html index 7a9828e..bd33213 100644 --- a/templates/market_intel/disabled.html +++ b/templates/market_intel/disabled.html @@ -414,6 +414,24 @@ +
+
+
+

MCP / READINESS PREVIEW

+

MCP 整合就緒度

+
+ +
+
+ loading +
+
+
讀取 MCP 整合就緒度中...
+
+
+
@@ -479,10 +497,11 @@ const dbProbeRoot = document.querySelector('[data-market-intel-db-probe]'); const seedDiffRoot = document.querySelector('[data-market-intel-seed-diff]'); const legacyBridgeRoot = document.querySelector('[data-market-intel-legacy-bridge]'); + const mcpReadinessRoot = document.querySelector('[data-market-intel-mcp-readiness]'); const migrationRoot = document.querySelector('[data-market-intel-migration]'); const approvalRoot = document.querySelector('[data-market-intel-approval]'); const deployRoot = document.querySelector('[data-market-intel-deploy]'); - if (!root && !writerRoot && !cliRoot && !dbProbeRoot && !seedDiffRoot && !legacyBridgeRoot && !migrationRoot && !approvalRoot && !deployRoot) return; + if (!root && !writerRoot && !cliRoot && !dbProbeRoot && !seedDiffRoot && !legacyBridgeRoot && !mcpReadinessRoot && !migrationRoot && !approvalRoot && !deployRoot) return; const meta = root ? root.querySelector('[data-market-intel-preview-meta]') : null; const body = root ? root.querySelector('[data-market-intel-preview-body]') : null; @@ -508,6 +527,10 @@ const legacyBridgeBody = legacyBridgeRoot ? legacyBridgeRoot.querySelector('[data-market-intel-legacy-bridge-body]') : null; const legacyBridgeRefresh = legacyBridgeRoot ? legacyBridgeRoot.querySelector('[data-market-intel-legacy-bridge-refresh]') : null; const legacyBridgeEndpoint = "{{ url_for('market_intel.market_intel_legacy_source_bridge') }}?execute=false&limit=5"; + const mcpReadinessMeta = mcpReadinessRoot ? mcpReadinessRoot.querySelector('[data-market-intel-mcp-readiness-meta]') : null; + const mcpReadinessBody = mcpReadinessRoot ? mcpReadinessRoot.querySelector('[data-market-intel-mcp-readiness-body]') : null; + const mcpReadinessRefresh = mcpReadinessRoot ? mcpReadinessRoot.querySelector('[data-market-intel-mcp-readiness-refresh]') : null; + const mcpReadinessEndpoint = "{{ url_for('market_intel.market_intel_mcp_readiness') }}?execute=false&timeout=3"; const migrationMeta = migrationRoot ? migrationRoot.querySelector('[data-market-intel-migration-meta]') : null; const migrationBody = migrationRoot ? migrationRoot.querySelector('[data-market-intel-migration-body]') : null; const migrationRefresh = migrationRoot ? migrationRoot.querySelector('[data-market-intel-migration-refresh]') : null; @@ -842,6 +865,103 @@ } }; + const renderMcpReadinessMeta = data => { + mcpReadinessMeta.innerHTML = [ + `mode=${data.mode || 'unknown'}`, + `router=${data.router_enabled ? 'on' : 'off'}`, + `external=${data.external_mcp_complete ? 'complete' : 'pending'}`, + `internal=${data.internal_mcp_complete ? 'complete' : 'pending'}`, + `market_tools=${data.market_intel_tool_count || 0}` + ].map(item => `${escapeHtml(item)}`).join(''); + }; + + const renderMcpReadinessBody = data => { + const blockers = (data.blocked_reasons || []).join(' / '); + const servers = data.server_statuses || []; + const checks = Object.entries(data.readiness_checks || {}); + const expectedTools = data.expected_market_intel_tools || []; + const registeredCallers = data.registered_callers || []; + const telemetry = data.telemetry || {}; + mcpReadinessBody.innerHTML = ` +
目前只做 MCP readiness planned preview;不自動呼叫外部平台、不建立 DB session、不寫入 telemetry。${blockers ? `阻擋:${escapeHtml(blockers)}` : ''}
+
+
+

EXTERNAL MCP SERVERS

+
${ + servers.length + ? servers.map(item => ` +
+
+ ${escapeHtml(item.server)} + ${escapeHtml(item.base_url || 'not configured')} + ${escapeHtml(item.status || '')} +
+ ${item.healthy ? 'HEALTHY' : item.configured ? 'PENDING' : 'MISSING'} +
+ `).join('') + : '
尚未提供 MCP server 狀態。
' + }
+
+
+

READINESS CHECKS

+
${ + checks.length + ? checks.map(([name, passed]) => ` +
+
+ ${escapeHtml(name)} +
+ ${passed ? 'PASS' : 'BLOCK'} +
+ `).join('') + : '
尚未提供 readiness checks。
' + }
+
+
+

INTERNAL TOOL CONTRACT

+
+
+
+ registered_callers + ${escapeHtml(registeredCallers.join(' / ') || 'none')} +
+ ${registeredCallers.length} +
+
+
+ market_intel_expected_tools + ${escapeHtml(expectedTools.join(' / ') || 'none')} +
+ ${escapeHtml(data.market_intel_tool_count || 0)} +
+
+
+ mcp_calls telemetry + ${escapeHtml(telemetry.mode || 'unknown')} / table=${telemetry.table_exists ? 'exists' : 'pending'} +
+ ${telemetry.read_only_query_executed ? 'QUERY' : 'PLANNED'} +
+
+
+
+ `; + }; + + const loadMcpReadiness = async () => { + if (!mcpReadinessMeta || !mcpReadinessBody) return; + mcpReadinessBody.innerHTML = '
讀取 MCP 整合就緒度中...
'; + try { + const response = await fetch(mcpReadinessEndpoint, { credentials: 'same-origin' }); + if (!response.ok) throw new Error(`HTTP ${response.status}`); + const data = await response.json(); + renderMcpReadinessMeta(data); + renderMcpReadinessBody(data); + } catch (error) { + mcpReadinessMeta.innerHTML = 'error'; + mcpReadinessBody.innerHTML = `
MCP 就緒度讀取失敗:${escapeHtml(error.message)}
`; + } + }; + const renderMigrationMeta = data => { const seedWriter = data.command_plan && data.command_plan.seed_writer_command ? data.command_plan.seed_writer_command @@ -1094,6 +1214,9 @@ if (legacyBridgeRefresh) { legacyBridgeRefresh.addEventListener('click', loadLegacyBridge); } + if (mcpReadinessRefresh) { + mcpReadinessRefresh.addEventListener('click', loadMcpReadiness); + } if (migrationRefresh) { migrationRefresh.addEventListener('click', loadMigration); } @@ -1109,6 +1232,7 @@ loadDbProbe(); loadSeedDiff(); loadLegacyBridge(); + loadMcpReadiness(); loadMigration(); loadApproval(); loadDeploy(); diff --git a/tests/test_market_intel_skeleton.py b/tests/test_market_intel_skeleton.py index ae1887d..79848ee 100644 --- a/tests/test_market_intel_skeleton.py +++ b/tests/test_market_intel_skeleton.py @@ -14,6 +14,7 @@ from services.market_intel.adapters import get_adapter, get_adapter_summaries from services.market_intel.candidate_preview import build_candidate_preview_from_discovery from services.market_intel.discovery_runner import ManualDiscoveryRunner from services.market_intel.html_diagnostics import parse_html_diagnostics +from services.market_intel.mcp_readiness import build_mcp_readiness_plan from services.market_intel.platform_seed_db_diff import build_platform_seed_db_diff_plan from services.market_intel.schema_db_probe import build_schema_db_probe_plan @@ -381,6 +382,10 @@ def test_market_intel_preview_template_uses_safe_fetch_false_endpoint(): assert "data-market-intel-db-probe-body" in template assert "data-market-intel-seed-diff" in template assert "data-market-intel-seed-diff-body" in template + assert "data-market-intel-mcp-readiness" in template + assert "data-market-intel-mcp-servers" in template + assert "data-market-intel-mcp-checks" in template + assert "data-market-intel-mcp-tools" in template assert "data-market-intel-migration" in template assert "data-market-intel-migration-tables" in template assert "data-market-intel-approval" in template @@ -394,6 +399,7 @@ def test_market_intel_preview_template_uses_safe_fetch_false_endpoint(): assert "market_intel.market_intel_schema_db_probe" in template assert "market_intel.market_intel_platform_seed_db_diff" in template assert "market_intel.market_intel_legacy_source_bridge" in template + assert "market_intel.market_intel_mcp_readiness" in template assert "market_intel.market_intel_migration_blueprint" in template assert "market_intel.market_intel_write_approval_runbook" in template assert "market_intel.market_intel_deployment_readiness" in template @@ -419,7 +425,7 @@ def test_legacy_source_bridge_default_is_planned_only(): bridge = MarketIntelService().build_legacy_source_bridge() assert bridge["mode"] == "legacy_source_bridge_planned" - assert bridge["phase"] == "phase_27_legacy_source_bridge_preview" + assert bridge["phase"] == "phase_28_mcp_readiness_preview" assert bridge["execute_requested"] is False assert bridge["read_only_query_executed"] is False assert bridge["database_connection_opened"] is False @@ -573,6 +579,140 @@ def test_legacy_source_bridge_read_only_sqlite_counts_sources(): ) +def test_mcp_readiness_default_is_planned_only(monkeypatch): + monkeypatch.delenv("MCP_ROUTER_ENABLED", raising=False) + + readiness = MarketIntelService().build_mcp_readiness() + + assert readiness["mode"] == "mcp_readiness_planned" + assert readiness["phase"] == "phase_28_mcp_readiness_preview" + assert readiness["execute_requested"] is False + assert readiness["router_enabled"] is False + assert readiness["external_mcp_complete"] is False + assert readiness["internal_mcp_complete"] is False + assert readiness["market_intel_mcp_integrated"] is False + assert readiness["database_session_created"] is False + assert readiness["database_write_executed"] is False + assert readiness["database_commit_executed"] is False + assert readiness["external_network_executed"] is False + assert readiness["scheduler_attached"] is False + assert readiness["writes_executed"] is False + assert readiness["would_write_database"] is False + assert readiness["telemetry"]["read_only_query_executed"] is False + assert readiness["telemetry"]["database_session_created"] is False + assert readiness["telemetry"]["database_write_executed"] is False + assert readiness["readiness_checks"]["base_callers_registered"] is True + assert readiness["readiness_checks"]["market_intel_caller_registered"] is False + assert readiness["readiness_checks"]["market_intel_tools_registered"] is False + assert len(readiness["server_statuses"]) == 4 + assert all(item["health_checked"] is False for item in readiness["server_statuses"]) + assert all(item["healthy"] is False for item in readiness["server_statuses"]) + assert "execute_false_planned_only" in readiness["blocked_reasons"] + + +def test_mcp_readiness_sqlite_read_only_counts_telemetry(monkeypatch): + monkeypatch.delenv("MCP_ROUTER_ENABLED", raising=False) + monkeypatch.setattr( + "services.market_intel.mcp_readiness._health_check_servers", + lambda base_hosts, timeout_sec: [ + { + "server": "postgres", + "base_url": "http://127.0.0.1:3001", + "configured": True, + "health_checked": True, + "healthy": False, + "status": "error", + "error_message": "test skipped network", + }, + { + "server": "omnisearch", + "base_url": "http://127.0.0.1:3003", + "configured": True, + "health_checked": True, + "healthy": False, + "status": "error", + "error_message": "test skipped network", + }, + { + "server": "firecrawl", + "base_url": "http://127.0.0.1:3002", + "configured": True, + "health_checked": True, + "healthy": False, + "status": "error", + "error_message": "test skipped network", + }, + { + "server": "filesystem", + "base_url": "http://127.0.0.1:3004", + "configured": True, + "health_checked": True, + "healthy": False, + "status": "error", + "error_message": "test skipped network", + }, + ], + ) + engine = create_engine("sqlite:///:memory:") + with engine.begin() as conn: + conn.execute( + text( + """ + CREATE TABLE mcp_calls ( + id INTEGER PRIMARY KEY, + server TEXT, + called_at TEXT + ) + """ + ) + ) + conn.execute( + text( + """ + INSERT INTO mcp_calls (server, called_at) + VALUES + ('omnisearch', '2026-05-18 10:00:00'), + ('omnisearch', '2026-05-18 10:05:00'), + ('postgres', '2026-05-18 10:10:00') + """ + ) + ) + + readiness = build_mcp_readiness_plan( + execute_requested=True, + timeout_sec=1, + engine=engine, + database_type="sqlite", + ) + + assert readiness["mode"] == "mcp_readiness_read_only" + assert readiness["execute_requested"] is True + assert readiness["router_enabled"] is False + assert readiness["external_mcp_complete"] is False + assert readiness["internal_mcp_complete"] is False + assert readiness["telemetry"]["mode"] == "mcp_telemetry_read_only" + assert readiness["telemetry"]["table_exists"] is True + assert readiness["telemetry"]["total_calls"] == 3 + assert readiness["telemetry"]["recent_24h_calls"] == 3 + assert readiness["telemetry"]["read_only_query_executed"] is True + assert readiness["telemetry"]["database_session_created"] is False + assert readiness["telemetry"]["database_write_executed"] is False + assert readiness["telemetry"]["database_commit_executed"] is False + assert readiness["database_session_created"] is False + assert readiness["database_write_executed"] is False + assert readiness["database_commit_executed"] is False + assert readiness["scheduler_attached"] is False + assert readiness["writes_executed"] is False + assert readiness["would_write_database"] is False + assert readiness["server_statuses"][0]["health_checked"] is True + assert {item["server"]: item["calls"] for item in readiness["telemetry"]["server_counts"]} == { + "omnisearch": 2, + "postgres": 1, + } + assert "mcp_router_enabled" in readiness["blocked_reasons"] + assert "market_intel_caller_registered" in readiness["blocked_reasons"] + + def test_market_intel_schema_smoke_checks_platform_columns(): smoke = MarketIntelService().build_schema_smoke()["schema_smoke"] @@ -821,6 +961,7 @@ def test_deployment_readiness_reports_app_only_release_gate(): assert readiness["checks"]["schema_db_probe_planned_safe"] is True assert readiness["checks"]["platform_seed_db_diff_planned_safe"] is True assert readiness["checks"]["legacy_source_bridge_planned_safe"] is True + assert readiness["checks"]["mcp_readiness_planned_safe"] is True assert readiness["checks"]["writer_plan_dry_run_only"] is True assert readiness["writer_plan_summary"]["writes_executed"] is False assert "readiness_checks_not_all_passed" not in readiness["blocked_reasons"] @@ -836,6 +977,7 @@ def test_deployment_readiness_reports_app_only_release_gate(): assert "/api/market_intel/deployment_readiness" in readiness["production_smoke_targets"] assert "/api/market_intel/platform_seed_db_diff" in readiness["production_smoke_targets"] assert "/api/market_intel/legacy_source_bridge" in readiness["production_smoke_targets"] + assert "/api/market_intel/mcp_readiness" in readiness["production_smoke_targets"] assert readiness["write_approval_runbook"]["ready_for_real_write"] is False assert readiness["write_approval_runbook"]["writes_executed"] is False assert readiness["migration_blueprint"]["migration_executed"] is False @@ -844,6 +986,8 @@ def test_deployment_readiness_reports_app_only_release_gate(): assert readiness["schema_db_probe"]["read_only_query_executed"] is False assert readiness["platform_seed_db_diff"]["read_only_query_executed"] is False assert readiness["legacy_source_bridge"]["read_only_query_executed"] is False + assert readiness["mcp_readiness"]["mode"] == "mcp_readiness_planned" + assert readiness["mcp_readiness"]["telemetry"]["read_only_query_executed"] is False def test_write_approval_runbook_is_read_only_and_blocks_real_write():