Appearance
Specification: Domain-Specific MCP Tool Extensions, Graph Introspection & Autonomous Agent Sidecars
NOTE
Status: design proposal. Not necessarily implemented. Check the relevant README or ADR before relying on it; kernel concepts in the kernel specification take precedence. Neo4j, mentioned below, is retired (ADR-2245).
- Status: Ready for Implementation
- Requirement ID: PRD-SYS-054
- ADR Reference: ADR-2398
- Triage Label:
ready-for-agent
Problem Statement
In the Cadence Clinical high-assurance eClinical platform, autonomous AI coding agents operating across microservices frequently need to inspect complex clinical metadata (CDISC USDM v3/v4 graphs in Neo4j), validate dynamic eCRF schema migrations in PostgreSQL, and verify reactive outbox event streams.
Currently, agents face critical friction when interacting with deep clinical domain layers:
- Lack of Domain-Level MCP Tooling: While generic diagnostics exist (
doctor_diagnose,run_fast_tests), agents lack dedicated Model Context Protocol (MCP) tools for domain introspection, forcing them to construct raw Cypher queries, execute ad-hoc SQL against internal databases, or scrape logs. - Context Bloat on Large Clinical Schemas: Inspecting complete USDM study graphs or SDTM domain mappings emits tens of thousands of tokens, rapidly overflowing the agent's context window and degrading reasoning fidelity.
- Absence of Parameterized Clinical Event Replay: Agents cannot easily simulate or replay reactive clinical events (e.g. subject enrollment advances, adverse event triggers, lab value outbox emissions) through typed MCP interfaces during TDD loops.
- Manual Verification of Cross-Service Contracts: Verifying contract parity between the Study Designer (Neo4j), EDC Execution Engine (PostgreSQL), and Interop Gateway requires multi-step manual script invocations rather than single-turn MCP tool calls.
Solution
Extend the native Cadence MCP Server (cadence mcp) with a dedicated suite of high-fidelity, domain-specific clinical tools equipped with progressive disclosure token zoom controls:
usdm_graph_inspectTool:- Provides structured inspection of CDISC USDM v3/v4 protocol graph entities (Study, StudyArm, StudyEpoch, Activity, ScheduleTimeline) from the Neo4j metadata layer with depth, entity type, and summary filtering.
sdtm_pipeline_validateTool:- Validates eCRF-to-SDTM domain mapping rules (DM, AE, LB, VS, CM) and checks Dataset-JSON 1.1 / Define-XML 2.1 compliance with structured error reporting.
clinical_event_streamTool:- Queries and replays outbox events across microservices (SubjectStateChanged, FormSubmitted, LabValueIngested, PaymentTriggered) with event-type filters and time range slicing.
synthetic_patient_generateTool:- Generates GxP-compliant synthetic clinical trial participants with realistic longitudinal schedules of activities, lab observations, and adverse event profiles for reproducible testing.
- Zoom-Envelope Progressiveness:
- All domain MCP responses encapsulate standardized summary cards and emit zoom inspection tokens (
zoom_token) allowing agents to drill into granular sub-graphs or raw event payloads without wasting context.
- All domain MCP responses encapsulate standardized summary cards and emit zoom inspection tokens (
User Stories
- As an autonomous coding agent, I want to call
usdm_graph_inspectvia MCP withsummary=True, so that I can inspect the high-level Schedule of Activities and arm structure without overflowing my context window. - As an autonomous coding agent, I want to query specific USDM entity types (e.g.
Activity,StudyCell,EligibilityCriterion) with depth limits, so that I can verify protocol metadata modeling with surgical precision. - As an autonomous coding agent, I want to call
sdtm_pipeline_validatewith a domain filter (e.g.domain="LB"), so that I can immediately verify whether newly authored laboratory form fields compile to valid CDISC SDTM variables. - As an autonomous coding agent, I want
sdtm_pipeline_validateto return structured validation diagnostics including CDISC Conformance Rules violations, so that I can auto-remediate mapping mismatches. - As an autonomous coding agent, I want to call
clinical_event_streamwith an event type filter (e.g.event_type="SubjectEnrolled"), so that I can inspect reactive outbox payloads emitted during integration test runs. - As an autonomous coding agent, I want
clinical_event_streamto support event replay into test mock listeners, so that I can verify event-driven saga workflows without spinning up external message brokers. - As an autonomous coding agent, I want to invoke
synthetic_patient_generatewith a target protocol and scenario name (e.g.scenario="oncology_dose_limiting_toxicity"), so that I can seed deterministic test subjects with complete longitudinal eCRFs. - As an autonomous coding agent, I want
synthetic_patient_generateto supportdry_run=True, so that I can inspect the generated data structure before persisting it to test database fixtures. - As a human software engineer, I want all new domain MCP tools to be exposed through the unified
uv run cadence mcpcommand, so that I can connect Cursor, Claude Desktop, or Antigravity IDE directly. - As a human software engineer, I want MCP domain tool calls to execute in sub-100ms for in-memory fixtures, preserving inner-loop development velocity.
- As a human software engineer, I want domain tool errors to provide actionable remediation suggestions (
ctaandremediationfields in the response envelope), so that I know the exact follow-up command to fix failures. - As a human software engineer, I want all domain MCP payloads to be strictly typed with Pydantic v2 schemas, preventing runtime serialization errors.
- As a GxP compliance officer, I want all synthetic patient data generated by MCP tools to strictly avoid real Protected Health Information (PHI) and carry explicit synthetic watermarks (
is_synthetic=True). - As a GxP compliance officer, I want all domain MCP tool invocations to be logged in 21 CFR Part 11 audit records when operating in regulated environments.
- As a QA automation engineer, I want comprehensive unit and contract tests asserting JSON-RPC request and response schemas for all 4 new domain tools under
packages/cli/mcp/server.py. - As a DevOps engineer, I want domain MCP handlers to gracefully fall back to in-memory mock adapters when Neo4j or PostgreSQL Docker containers are offline, ensuring fast offline test execution.
Implementation Decisions
1. Domain MCP Tool Catalog Extension
Extend CadenceMcpServer.TOOLS in packages/cli/mcp/server.py with 4 new tool definitions:
usdm_graph_inspect(protocol_id: str | None = None, entity_type: str | None = None, depth: int = 2, summary: bool = True)sdtm_pipeline_validate(domain: str | None = None, strict_cdisc: bool = True, summary: bool = True)clinical_event_stream(event_type: str | None = None, limit: int = 20, replay: bool = False)synthetic_patient_generate(protocol_id: str = "default", scenario: str = "standard", count: int = 1, dry_run: bool = False)
2. Tooling Core Domain Handlers
Implement dedicated handler functions in packages/tooling_core/src/ToolingHandlers.cs (the Python signatures below describe the shapes):
handle_usdm_graph_inspect(req: UsdmGraphInspectRequest) -> CommandEnvelope[UsdmGraphInspectResponse]handle_sdtm_pipeline_validate(req: SdtmPipelineValidateRequest) -> CommandEnvelope[SdtmPipelineValidateResponse]handle_clinical_event_stream(req: ClinicalEventStreamRequest) -> CommandEnvelope[ClinicalEventStreamResponse]handle_synthetic_patient_generate(req: SyntheticPatientGenerateRequest) -> CommandEnvelope[SyntheticPatientGenerateResponse]
3. Progressive Zoom Envelope Standardization
All responses adhere to the standard CommandEnvelope[T] format:
success: Boolean status indicator.exit_code: Numeric status code.summary: Compact dictionary/narrative metrics for immediate agent consumption.zoom_token: Progressive disclosure token registered in_ZOOM_REGISTRYfor granular inspection viazoom_inspect.remediation: Contextual follow-up command if errors are flagged.data: Typed response object containing filtered domain payload.
Testing Decisions
What Makes a Good Test
- Tests must assert external JSON-RPC protocol compliance (
tools/listreturns updated schemas,tools/callexecutes with valid parameter validation and zoom envelopes). - Tests must run in sub-second time using in-memory mock adapters without requiring live Neo4j or PostgreSQL daemon processes.
Tested Surfaces
- JSON-RPC Schema Tests (
packages/cli/mcp/server.py):- Verify all 10 tools (6 existing + 4 domain extensions) are published in
tools/list. - Verify input parameter validation for each domain tool.
- Verify all 10 tools (6 existing + 4 domain extensions) are published in
- Domain Handler Unit Tests (
packages/tooling_core/tests/test_tooling_core.py):- Verify
handle_usdm_graph_inspectfiltering by entity type and depth. - Verify
handle_sdtm_pipeline_validatevalidation rules and error capture. - Verify
handle_clinical_event_streamfiltering and replay mechanisms. - Verify
handle_synthetic_patient_generatedeterministic seeding anddry_runmodes.
- Verify
- Zoom Inspection Tests:
- Verify zoom tokens generated by domain tools can be unpacked via
handle_zoom_inspect.
- Verify zoom tokens generated by domain tools can be unpacked via
Out of Scope
- Implementing third-party EHR SMART-on-FHIR cloud authentication bridges.
- Replacing the underlying Neo4j Cypher query engine.
- Adding non-standard proprietary metadata schemas outside CDISC USDM and SDTM.
Further Notes
- All code and test cases must be annotated with
@req:PRD-SYS-054for 21 CFR Part 11 traceability. - All implementations must adhere strictly to Python 3.14+ runtime invariants and Ruff formatting rules.