Tool schema for create_container fails VS Code validation: array type must have items

View original issue on GitHub  ·  Variant 1

Fixing VS Code Validation Failure: "array type must have items" in mcp-server-docker

You might encounter an error when using mcp-server-docker with VS Code's MCP integration. Specifically, the validation of the mcp_docker_create_container tool fails with the message: "Error: tool parameters array type must have items". This article will guide you through understanding and resolving this issue.

Understanding the Problem

This error arises because the JSON schema generated for the mcp_docker_create_container tool's parameters doesn't fully comply with the schema validation requirements of some clients, like VS Code. The schema defines an array type without specifying the type of items it should contain, which triggers the validation error.

Environment

This issue commonly occurs in the following environment:

Reproducing the Error

You can reproduce this error with these steps:

  1. Configure the MCP server: Add the following configuration to your VS Code settings:
    
    "docker": {
      "command": "uvx",
      "args": ["mcp-server-docker"]
    }
        
  2. Start VS Code MCP and validate tools.
  3. Observe the validation failure: You should see the error message "Error: tool parameters array type must have items" for the mcp_docker_create_container tool.

Root Cause Analysis

The root cause lies in how the ports field within the CreateContainerInput data model is defined. The schema for this field includes a tuple branch, which translates to a JSON schema like this:


{
  "type": "array",
  "prefixItems": [{"type":"string"},{"type":"integer"}],
  "minItems": 2,
  "maxItems": 2
}

The problem is that even though the schema specifies the types of the first and second items in the array using prefixItems, it lacks a general items definition. Some schema validators require an items definition for all array types, leading to the validation failure.

The field definition in Python looks like this:


ports: dict[str, int | list[int] | tuple[str, int] | None] | None

Solution: Addressing the Schema Validation Issue

There are a couple of ways to resolve this issue:

1. Replacing Tuple with an Object

One approach is to replace the tuple[str, int] type with a more explicit object representation. This would involve changing the MCP input schema to use an object with named properties instead of a tuple. For example:

Instead of:


["host", 8080]

Use:


{ "host": "host", "port": 8080 }

This would require changes in the data model definition to reflect this new structure.

2. Defining json_schema_extra

Another approach is to define a json_schema_extra for the ports field. This allows you to explicitly specify the JSON schema for that field, ensuring that all array branches include an items definition. This method allows you to override the default schema generation.

Example (conceptual):


from typing import Dict, Union, List, Tuple, Optional
from pydantic import BaseModel, Field

class CreateContainerInput(BaseModel):
    ports: Optional[Dict[str, Union[int, List[int], Tuple[str, int], None]]] = Field(
        default=None,
        json_schema_extra={
            "oneOf": [
                {"type": "integer"},
                {"type": "array", "items": {"type": "integer"}},
                {"type": "array", "prefixItems": [{"type": "string"}, {"type": "integer"}], "minItems": 2, "maxItems": 2, "items": {"type": ["string", "integer"]}},
                {"type": "null"}
            ]
        }
    )

This example demonstrates how you can use json_schema_extra to provide a more detailed schema definition, including the missing items property for the tuple-based array.

Practical Tips and Considerations

By understanding the root cause of the "array type must have items" error and applying the suggested solutions, you can ensure that your mcp-server-docker tools validate correctly in VS Code and other MCP clients.