Skip to content

Commit 9e778b3

Browse files
authored
fixing store and update via mcp
(#655) * fix: fixing store and update via mcp * Fix styling --------- Co-authored-by: binaryk <[email protected]>
1 parent b303661 commit 9e778b3

36 files changed

+822
-106
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
coverage: none
2525

2626
- name: Install dependencies
27-
run: composer install --no-dev --prefer-dist --no-interaction --optimize-autoloader
27+
run: composer install --prefer-dist --no-interaction --optimize-autoloader
2828

2929
- name: Run tests
3030
run: |

src/Eager/Related.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function resolveField(Repository $repository): EagerField
6464
{
6565
return $this
6666
->field
67-
->forMcp($repository->isForMcp())
67+
->forMcp($repository->detectMcpRequest())
6868
->columns($this->getColumns())
6969
->resolve($repository);
7070
}

src/Eager/RelatedCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public function forRequest(RestifyRequest $request, Repository $repository): sel
184184
->inRequest($request, $repository)
185185
->when($request->isShowRequest(), fn (self $collection) => $collection->forShow($request, $repository))
186186
->when($request->isIndexRequest(), fn (self $collection) => $collection->forIndex($request, $repository))
187-
->when($repository->isForMcp(), fn (self $collection) => $collection->forIndex($request, $repository));
187+
->when($repository->detectMcpRequest(), fn (self $collection) => $collection->forIndex($request, $repository));
188188
}
189189

190190
public function unserialized(RestifyRequest $request, Repository $repository)

src/Fields/BelongsToMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function resolve($repository, $attribute = null)
5858

5959
return $repositoryFromClass
6060
->allowToShow(
61-
$this->isForMcp() ? app(McpRequest::class) : app(RestifyRequest::class)
61+
$this->detectMcpRequest() ? app(McpRequest::class) : app(RestifyRequest::class)
6262
)
6363
->withPivots(
6464
PivotsCollection::make($this->pivotFields)

src/Fields/EagerField.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class EagerField extends Field
3131

3232
private RelatedQuery $relatedQuery;
3333

34-
public bool $forMcp = false;
34+
public ?string $forMcp = null;
3535

3636
public function __construct($attribute, ?string $parentRepository = null)
3737
{
@@ -88,7 +88,7 @@ public function resolve($repository, $attribute = null)
8888
$serializableRepository = $this->repositoryClass::resolveWith($relatedModel);
8989

9090
$this->value = $serializableRepository
91-
->allowToShow($this->isForMcp() ? app(McpRequest::class) : app(RestifyRequest::class))
91+
->allowToShow($this->detectMcpRequest() ? app(McpRequest::class) : app(RestifyRequest::class))
9292
->columns()
9393
->eager($this);
9494
} catch (AuthorizationException) {
@@ -164,7 +164,7 @@ public function qualifySortable(RestifyRequest $request): ?string
164164
return $table.'.attributes.'.$this->sortableColumn;
165165
}
166166

167-
public function forMcp(bool|callable $forMcp = false): self
167+
public function forMcp(null|string|callable $forMcp = null): self
168168
{
169169
if (is_callable($forMcp)) {
170170
$this->forMcp = $forMcp();
@@ -177,7 +177,7 @@ public function forMcp(bool|callable $forMcp = false): self
177177
return $this;
178178
}
179179

180-
public function isForMcp(): bool
180+
public function detectMcpRequest(): ?string
181181
{
182182
return $this->forMcp;
183183
}

src/MCP/Concerns/HasMcpTools.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ public function mcpAllowsActions(): bool
4848

4949
public function mcpAllowsGetters(): bool
5050
{
51-
return true;
51+
return false;
5252
}
5353
}

src/MCP/Concerns/McpDestroyTool.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace Binaryk\LaravelRestify\MCP\Concerns;
44

5-
use Binaryk\LaravelRestify\MCP\Requests\McpRequest;
5+
use Binaryk\LaravelRestify\MCP\Requests\McpDestroyRequest;
66
use Laravel\Mcp\Server\Tools\ToolInputSchema;
77

88
/**
99
* @mixin \Binaryk\LaravelRestify\Repositories\Repository
1010
*/
1111
trait McpDestroyTool
1212
{
13-
public function deleteTool(array $arguments, McpRequest $request): array
13+
public function deleteTool(array $arguments, McpDestroyRequest $request): array
1414
{
1515
$id = $arguments['id'] ?? null;
1616
unset($arguments['id']);

src/MCP/Concerns/McpIndexTool.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Binaryk\LaravelRestify\Filters\MatchesCollection;
77
use Binaryk\LaravelRestify\Filters\MatchFilter;
88
use Binaryk\LaravelRestify\Filters\SearchablesCollection;
9+
use Binaryk\LaravelRestify\MCP\Requests\McpIndexRequest;
910
use Binaryk\LaravelRestify\MCP\Requests\McpRequest;
1011
use Laravel\Mcp\Server\Tools\ToolInputSchema;
1112

@@ -14,7 +15,7 @@
1415
*/
1516
trait McpIndexTool
1617
{
17-
public function indexTool(array $arguments, McpRequest $request): array
18+
public function indexTool(array $arguments, McpIndexRequest $request): array
1819
{
1920
$request->merge($arguments);
2021
$this->sanitizeToolRequest($request, $arguments);
@@ -33,7 +34,7 @@ public static function indexToolSchema(ToolInputSchema $schema): void
3334
->description("Number of $key per page");
3435

3536
$schema->string('include')
36-
->description(static::formatRelationshipDocumentation());
37+
->description(static::formatRelationshipDocumentation(app(McpIndexRequest::class)));
3738

3839
$searchableFields = (new SearchablesCollection(static::searchables()))->formatForDocumentation();
3940

@@ -66,11 +67,11 @@ public static function indexToolSchema(ToolInputSchema $schema): void
6667

6768
return match ($matchFilter->getType()) {
6869
RestifySearchable::MATCH_INTEGER, 'integer' => $schema->integer($filterKey)
69-
->description("Filter $key resource. Filter scription: ".$matchFilter->description()),
70+
->description("Filter $key resource. Description: ".$matchFilter->description()),
7071
RestifySearchable::MATCH_BOOL, 'boolean' => $schema->boolean($filterKey)
71-
->description("Filter $key resource. Filter scription: ".$matchFilter->description()),
72+
->description("Filter $key resource. Description: ".$matchFilter->description()),
7273
default => $schema->string($filterKey)
73-
->description("Filter $key resource. Filter scription: ".$matchFilter->description())
74+
->description("Filter $key resource. Description: ".$matchFilter->description())
7475
};
7576
});
7677
}

src/MCP/Concerns/McpShowTool.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace Binaryk\LaravelRestify\MCP\Concerns;
44

5-
use Binaryk\LaravelRestify\MCP\Requests\McpRequest;
5+
use Binaryk\LaravelRestify\MCP\Requests\McpShowRequest;
66
use Laravel\Mcp\Server\Tools\ToolInputSchema;
77

88
/**
99
* @mixin \Binaryk\LaravelRestify\Repositories\Repository
1010
*/
1111
trait McpShowTool
1212
{
13-
public function showTool(array $arguments, McpRequest $request): array
13+
public function showTool(array $arguments, McpShowRequest $request): array
1414
{
1515
$id = $arguments['id'] ?? null;
1616
unset($arguments['id']);

src/MCP/Concerns/McpStoreTool.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,30 @@
33
namespace Binaryk\LaravelRestify\MCP\Concerns;
44

55
use Binaryk\LaravelRestify\Fields\Field;
6-
use Binaryk\LaravelRestify\MCP\Requests\McpRequest;
6+
use Binaryk\LaravelRestify\MCP\Requests\McpStoreRequest;
77
use Laravel\Mcp\Server\Tools\ToolInputSchema;
88

99
/**
1010
* @mixin \Binaryk\LaravelRestify\Repositories\Repository
1111
*/
1212
trait McpStoreTool
1313
{
14-
public function storeTool(array $arguments, McpRequest $request): array
14+
public function storeTool(array $arguments, McpStoreRequest $request): array
1515
{
1616
$request->merge($arguments);
1717
$this->sanitizeToolRequest($request, $arguments);
1818

19-
return $this->store($request);
19+
return $this
20+
->allowToStore($request)
21+
->store($request)
22+
->getData(true);
2023
}
2124

2225
public static function storeToolSchema(ToolInputSchema $schema): void
2326
{
2427
$repository = static::resolveWith(static::newModel());
2528

26-
$repository->collectFields($request = app(McpRequest::class))
29+
$repository->collectFields($request = app(McpStoreRequest::class))
2730
->forStore($request, $repository)
2831
->withoutActions($request, $repository)
2932
->each(function (Field $field) use ($schema, $repository) {

0 commit comments

Comments
 (0)