Skip to content

Conversation

@jonathannorris
Copy link
Member

@jonathannorris jonathannorris commented Aug 11, 2025

Problem

The MCP server was failing to start with validation errors: tool parameters array type must have items. This occurred because z.array(z.any()) doesn't generate the required items property for MCP JSON schemas.

Solution

  • Replaced z.array(z.any()) with z.array(z.unknown()) in zodClient.ts (6 fixes)
  • Added comprehensive test suite that validates all 27 MCP tools for proper schema format
  • Added documentation explaining MCP compatibility requirements

Testing

  • ✅ MCP server starts without validation errors
  • ✅ All tools properly registered in Claude Desktop
  • ✅ New tests prevent regression of this issue

The z.unknown() type generates proper JSON schemas while maintaining runtime compatibility.

@jonathannorris jonathannorris requested a review from a team as a code owner August 11, 2025 19:12
@jonathannorris jonathannorris changed the title fix: replace z.array(z.any()) with z.array(z.unknown()) for MCP schema compatibility fix(mcp): MCP schema validation error for array types Aug 11, 2025
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Aug 11, 2025

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
devcycle-mcp-server fbd4d61 Aug 11 2025, 08:45 PM

This comment was marked as outdated.

@JamieSinn
Copy link
Member

Fixed the cloudflare build command for PRs to skip deploying and just echo out. needs a rebase

@jonathannorris jonathannorris requested a review from Copilot August 11, 2025 20:44
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes MCP (Model Context Protocol) schema validation errors caused by array types missing the required items property. The issue occurred because z.array(z.any()) doesn't generate proper JSON schemas for MCP compatibility.

  • Replaces z.array(z.any()) with z.array(z.unknown()) in zodClient.ts (6 instances)
  • Adds comprehensive test suite to validate MCP schema compliance for all 27 tools
  • Includes documentation explaining MCP compatibility requirements

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/api/zodClient.ts Replaces z.array(z.any()) with z.array(z.unknown()) and adds MCP compatibility documentation
src/mcp/server.schema.test.ts Adds comprehensive test suite to validate MCP schema compliance and prevent regressions

)
}

if (errors.length > 0) {
Copy link

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable validationErrors is being incremented with individual tool errors, but the final error message states 'Found ${validationErrors.length} tools with invalid schemas' which counts array elements, not tools. This will show an incorrect count since each tool can have multiple errors. Consider tracking the number of invalid tools separately.

Suggested change
if (errors.length > 0) {
if (errors.length > 0) {
invalidToolCount++

Copilot uses AI. Check for mistakes.
Comment on lines +86 to +97
// Create a mock API client
const mockApiClient = {
executeWithLogging: sinon.stub(),
executeWithDashboardLink: sinon.stub(),
setSelectedProject: sinon.stub(),
hasProjectKey: sinon.stub().resolves(true),
getOrgId: sinon.stub().returns('test-org'),
getUserId: sinon.stub().returns('test-user'),
hasProjectAccess: sinon.stub().resolves(true),
getUserContext: sinon.stub().resolves({}),
}

Copy link

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mock API client is duplicated in both test cases (lines 86-96 and implicitly recreated in the second test). Consider extracting this to a helper function or beforeEach block to reduce code duplication.

Suggested change
// Create a mock API client
const mockApiClient = {
executeWithLogging: sinon.stub(),
executeWithDashboardLink: sinon.stub(),
setSelectedProject: sinon.stub(),
hasProjectKey: sinon.stub().resolves(true),
getOrgId: sinon.stub().returns('test-org'),
getUserId: sinon.stub().returns('test-user'),
hasProjectAccess: sinon.stub().resolves(true),
getUserContext: sinon.stub().resolves({}),
}
// Helper to create a fresh mock API client for each test
function createMockApiClient() {
return {
executeWithLogging: sinon.stub(),
executeWithDashboardLink: sinon.stub(),
setSelectedProject: sinon.stub(),
hasProjectKey: sinon.stub().resolves(true),
getOrgId: sinon.stub().returns('test-org'),
getUserId: sinon.stub().returns('test-user'),
hasProjectAccess: sinon.stub().resolves(true),
getUserContext: sinon.stub().resolves({}),
}
}
let mockApiClient: ReturnType<typeof createMockApiClient>
beforeEach(() => {
mockApiClient = createMockApiClient()
})

Copilot uses AI. Check for mistakes.
`1. Open src/api/zodClient.ts`,
`2. Search for 'z.array(z.any())'`,
`3. Replace with 'z.array(z.unknown())'`,
`4. Also check for 'z.record(z.any())' and replace with 'z.record(z.unknown())'`,
Copy link

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message suggests checking for z.record(z.any()), but the validation function validateSchemaArrays only checks for array types with missing items, not record types. Either add validation for record types or remove this suggestion from the error message.

Suggested change
`4. Also check for 'z.record(z.any())' and replace with 'z.record(z.unknown())'`,

Copilot uses AI. Check for mistakes.
@jonathannorris jonathannorris merged commit b26e3cf into feat-mcp-server Aug 11, 2025
9 checks passed
@jonathannorris jonathannorris deleted the fix-schema-issue-2 branch August 11, 2025 20:45
jonathannorris added a commit that referenced this pull request Aug 11, 2025
* fix: replace z.array(z.any()) with z.array(z.unknown()) for MCP schema compatibility

* test: add MCP schema validation tests to prevent array items issues

* fix: types in tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants