From 66a5cd909b32e2b4f5b87c619d0c565e78f2e8c8 Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Sat, 30 Aug 2025 17:15:48 +0300 Subject: [PATCH] fix: improve validation for schema' --- docs-v2/content/en/mcp/mcp.md | 25 +++++++++++++++++++++++++ src/Fields/Field.php | 6 +----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/docs-v2/content/en/mcp/mcp.md b/docs-v2/content/en/mcp/mcp.md index 8f0e9cfc7..d0320d7df 100644 --- a/docs-v2/content/en/mcp/mcp.md +++ b/docs-v2/content/en/mcp/mcp.md @@ -144,6 +144,31 @@ Terminal-based AI clients (like Claude Desktop) can connect to your local MCP se - Audit MCP field access patterns - Implement rate limiting for token-heavy operations +## Common Issues + +### Schema Validation Error + +**Error**: `[ERROR: Received tool input did not match expected schema]` + +**Cause**: This occurs when the field type is not identified correctly by the MCP server, leading to schema mismatches between what the AI agent sends and what Laravel Restify expects. + +**Solution**: You need to explicitly override the field type for the MCP schema using the `toolSchema()` method: + +```php +field('project_id') + ->toolSchema(function(\Laravel\Mcp\Server\Tools\ToolInputSchema $schema) { + $schema->string('project_id') + ->description('The ID of the project associated with the timesheet entry.') + ->required(); + }), +``` + +This approach allows you to: +- Explicitly define the expected data type (string, integer, boolean, etc.) +- Add detailed descriptions for AI agents +- Set validation rules (required, optional) +- Override automatic type inference when it's incorrect + ## Configuration The MCP integration respects your existing Restify configuration and adds MCP-specific options: diff --git a/src/Fields/Field.php b/src/Fields/Field.php index c1b1f7914..3b0fad2bc 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -846,11 +846,7 @@ protected function guessTypeFromValidationRules(): ?string return 'number'; } - if ($this->hasAnyRule($ruleStrings, ['array'])) { - return 'array'; - } - - if ($this->hasAnyRule($ruleStrings, ['email', 'url', 'ip', 'uuid'])) { + if ($this->hasAnyRule($ruleStrings, ['email', 'url', 'ip', 'uuid', 'string', 'regex', 'in', 'array'])) { return 'string'; }