LearnNewsExamplesServices
Frontmatter
id7843
title[AI] Implement Runtime Type Safety for AI SDK via OpenAPI Wrappers
stateClosed
labels
enhancementdeveloper-experienceairefactoring
assigneestobiu
createdAtNov 21, 2025, 3:14 PM
updatedAtNov 21, 2025, 3:31 PM
githubUrlhttps://github.com/neomjs/neo/issues/7843
authortobiu
commentsCount1
parentIssuenull
subIssues[]
subIssuesCompleted0
subIssuesTotal0
blockedBy[]
blocking[]
closedAtNov 21, 2025, 3:31 PM

[AI] Implement Runtime Type Safety for AI SDK via OpenAPI Wrappers

Closed v11.8.0 enhancementdeveloper-experienceairefactoring
tobiu
tobiu commented on Nov 21, 2025, 3:14 PM

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.

  1. Extract Validator: Move the buildZodSchema and schema caching logic from ai/mcp/server/toolService.mjs into a new ai/mcp/validation/OpenApiValidator.mjs module.
  2. 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.
  3. 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).
tobiu added the enhancement label on Nov 21, 2025, 3:14 PM
tobiu added the developer-experience label on Nov 21, 2025, 3:14 PM
tobiu added the ai label on Nov 21, 2025, 3:14 PM
tobiu added the refactoring label on Nov 21, 2025, 3:14 PM
tobiu assigned to @tobiu on Nov 21, 2025, 3:18 PM
tobiu referenced in commit 9cc5337 - "[AI] Implement Runtime Type Safety for AI SDK via OpenAPI Wrappers #7843" on Nov 21, 2025, 3:31 PM
tobiu
tobiu Nov 21, 2025, 3:31 PM
Image
tobiu closed this issue on Nov 21, 2025, 3:31 PM