Objective
Implement a mechanism to ensure runtime type safety for direct SDK service calls (ai/services.mjs) without introducing a compilation step.
Current State
- MCP tools are type-safe because
toolService.mjs validates incoming requests against Zod schemas generated from openapi.yaml.
- Direct SDK usage (importing services directly) bypasses this validation, creating a "safety gap" where incorrect shapes can be passed to services.
Proposed Solution
Refactor the existing validation logic into a shared OpenApiValidator utility and use it to create a "Safe SDK" wrapper.
- Extract Validator: Move the
buildZodSchema and schema caching logic from ai/mcp/server/toolService.mjs into a new ai/mcp/validation/OpenApiValidator.mjs module.
- Create Service Wrapper: Implement a factory function (e.g.,
createSafeService) that:
- Accepts a raw service object and the OpenAPI spec.
- Iterates over the service methods.
- Wraps each method with Zod validation logic derived from the OpenAPI Operation ID.
- Update SDK Export: Modify
ai/services.mjs to export these wrapped, validated service instances instead of the raw ones.
Benefits
- DRY: Single source of truth (OpenAPI) for both MCP and SDK validation.
- Zero-Build: Maintains the project's pure ESM / no-compile philosophy.
- Robustness: Prevents runtime errors due to invalid arguments when agents use the SDK directly.
Acceptance Criteria
ai/mcp/validation/OpenApiValidator.mjs exists and contains the extracted Zod logic.
ai/services.mjs exports validated service instances.
- Passing invalid arguments to an SDK method throws a clear Zod validation error.
- Existing MCP tool functionality remains unchanged (regression test).
Objective Implement a mechanism to ensure runtime type safety for direct SDK service calls (
ai/services.mjs) without introducing a compilation step.Current State
toolService.mjsvalidates incoming requests against Zod schemas generated fromopenapi.yaml.Proposed Solution Refactor the existing validation logic into a shared
OpenApiValidatorutility and use it to create a "Safe SDK" wrapper.buildZodSchemaand schema caching logic fromai/mcp/server/toolService.mjsinto a newai/mcp/validation/OpenApiValidator.mjsmodule.createSafeService) that:ai/services.mjsto export these wrapped, validated service instances instead of the raw ones.Benefits
Acceptance Criteria
ai/mcp/validation/OpenApiValidator.mjsexists and contains the extracted Zod logic.ai/services.mjsexports validated service instances.