Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions app/Http/Controllers/Api/ContextAssetUploadController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Project;
use App\Models\Story;
use App\Services\Context\AssetUploader;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use InvalidArgumentException;

class ContextAssetUploadController extends Controller
{
public function __invoke(Request $request, AssetUploader $uploader): JsonResponse
{
$validated = $request->validate([
'file' => ['required', 'file'],
'title' => ['nullable', 'string', 'max:255'],
'project_id' => ['required_without:story_id', 'nullable', 'integer', 'prohibited_with:story_id'],
'story_id' => ['required_without:project_id', 'nullable', 'integer', 'prohibited_with:project_id'],
]);

$user = $request->user();

if (! empty($validated['story_id'])) {
$story = Story::query()->with('feature')->find($validated['story_id']);

if (! $story) {
return response()->json(['error' => 'Story not found.'], 404);
}

$project = $story->feature?->project;

if (! $project || ! in_array($project->id, $user->accessibleProjectIds(), true)) {
return response()->json(['error' => 'Story not accessible.'], 403);
}
} else {
$project = Project::query()->find($validated['project_id']);

if (! $project) {
return response()->json(['error' => 'Project not found.'], 404);
}

if (! in_array($project->id, $user->accessibleProjectIds(), true)) {
return response()->json(['error' => 'Project not accessible.'], 403);
}

$story = null;
}

try {
$item = $uploader->store($request->file('file'), $project, $story ?? null, $user);
} catch (InvalidArgumentException $e) {
return response()->json(['error' => $e->getMessage()], 422);
}

if (isset($validated['title']) && $validated['title'] !== null) {
$item->update(['title' => $validated['title']]);
}

return response()->json([
'id' => $item->id,
'project_id' => $item->project_id,
'story_id' => $item->story_id,
'type' => $item->type->value,
'title' => $item->title,
'metadata' => $item->metadata,
], 201);
}
}
10 changes: 10 additions & 0 deletions app/Mcp/Servers/SpecifyServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use App\Mcp\Tools\AddAcceptanceCriterionTool;
use App\Mcp\Tools\AddGithubRepoToProjectTool;
use App\Mcp\Tools\AddProjectAssetTool;
use App\Mcp\Tools\AddStoryAssetTool;
use App\Mcp\Tools\AddStoryDependencyTool;
use App\Mcp\Tools\ApprovePlanTool;
use App\Mcp\Tools\ApproveStoryTool;
Expand All @@ -13,6 +15,7 @@
use App\Mcp\Tools\CreateScenarioTool;
use App\Mcp\Tools\CreateStoryTool;
use App\Mcp\Tools\CurrentContextTool;
use App\Mcp\Tools\DeleteContextItemTool;
use App\Mcp\Tools\GenerateTasksTool;
use App\Mcp\Tools\GetFeatureTool;
use App\Mcp\Tools\GetPlanTool;
Expand All @@ -22,6 +25,7 @@
use App\Mcp\Tools\GetStoryTool;
use App\Mcp\Tools\GetTaskTool;
use App\Mcp\Tools\ListActivityTool;
use App\Mcp\Tools\ListContextItemsTool;
use App\Mcp\Tools\ListFeaturesTool;
use App\Mcp\Tools\ListPlansTool;
use App\Mcp\Tools\ListProjectsTool;
Expand All @@ -44,6 +48,7 @@
use App\Mcp\Tools\SubmitPlanTool;
use App\Mcp\Tools\SubmitStoryTool;
use App\Mcp\Tools\SwitchProjectTool;
use App\Mcp\Tools\UpdateContextItemTool;
use App\Mcp\Tools\UpdateFeatureTool;
use App\Mcp\Tools\UpdatePlanTool;
use App\Mcp\Tools\UpdateProjectTool;
Expand Down Expand Up @@ -131,6 +136,11 @@ class SpecifyServer extends Server
ListReposTool::class,
GetRepoTool::class,
ListActivityTool::class,
ListContextItemsTool::class,
AddProjectAssetTool::class,
AddStoryAssetTool::class,
UpdateContextItemTool::class,
DeleteContextItemTool::class,
Comment on lines +139 to +143
];

protected array $resources = [
Expand Down
81 changes: 81 additions & 0 deletions app/Mcp/Tools/AddProjectAssetTool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace App\Mcp\Tools;

use App\Enums\ContextItemType;
use App\Mcp\Concerns\ResolvesProjectAccess;
use App\Services\Context\ContextItemWriter;
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Illuminate\Validation\ValidationException;
use InvalidArgumentException;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Attributes\Description;
use Laravel\Mcp\Server\Tool;

#[Description('Add a text note or link asset to a project. Assets are shared reference material available to all stories in the project for AI plan generation.')]
class AddProjectAssetTool extends Tool
{
use ResolvesProjectAccess;

protected string $name = 'add-project-asset';

public function handle(Request $request, ContextItemWriter $writer): Response
{
$user = $this->resolveUser($request);
if ($user instanceof Response) {
return $user;
}

try {
$validated = $request->validate([
'project_id' => ['required', 'integer'],
'type' => ['required', 'string', 'in:text,link'],
'title' => ['required', 'string', 'max:255'],
'body' => ['required_if:type,text', 'nullable', 'string', 'max:10000'],
'url' => ['required_if:type,link', 'nullable', 'string', 'url', 'max:2048'],
]);
} catch (ValidationException $e) {
return Response::error(implode(' ', array_merge(...array_values($e->errors()))));
}

$project = $this->resolveAccessibleProject($validated['project_id'], $user);
if ($project instanceof Response) {
return $project;
}

$type = ContextItemType::from($validated['type']);
$metadata = $type === ContextItemType::Text
? ['body' => $validated['body'] ?? '']
: ['url' => $validated['url'] ?? ''];

try {
$item = $writer->createProjectItem($project, [
'type' => $type,
'title' => $validated['title'],
'metadata' => $metadata,
], $user);
} catch (InvalidArgumentException $e) {
return Response::error($e->getMessage());
}

return Response::json([
'id' => $item->id,
'project_id' => $item->project_id,
'type' => $item->type->value,
'title' => $item->title,
'metadata' => $item->metadata,
]);
}

public function schema(JsonSchema $schema): array
{
return [
'project_id' => $schema->integer()->description('Project to attach the asset to.')->required(),
'type' => $schema->string()->enum(['text', 'link'])->description('Asset type: "text" for a note, "link" for a URL.')->required(),
'title' => $schema->string()->description('Asset title.')->required(),
'body' => $schema->string()->description('Body text. Required when type is "text".'),
'url' => $schema->string()->description('URL. Required when type is "link".'),
];
}
}
82 changes: 82 additions & 0 deletions app/Mcp/Tools/AddStoryAssetTool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace App\Mcp\Tools;

use App\Enums\ContextItemType;
use App\Mcp\Concerns\ResolvesProjectAccess;
use App\Services\Context\ContextItemWriter;
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Illuminate\Validation\ValidationException;
use InvalidArgumentException;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Attributes\Description;
use Laravel\Mcp\Server\Tool;

#[Description('Add a text note or link asset scoped to a specific story. Story assets are auto-included in the story\'s AI context and reopen the story for approval.')]
class AddStoryAssetTool extends Tool
{
use ResolvesProjectAccess;

protected string $name = 'add-story-asset';

public function handle(Request $request, ContextItemWriter $writer): Response
{
$user = $this->resolveUser($request);
if ($user instanceof Response) {
return $user;
}

try {
$validated = $request->validate([
'story_id' => ['required', 'integer'],
'type' => ['required', 'string', 'in:text,link'],
'title' => ['required', 'string', 'max:255'],
'body' => ['required_if:type,text', 'nullable', 'string', 'max:10000'],
'url' => ['required_if:type,link', 'nullable', 'string', 'url', 'max:2048'],
]);
} catch (ValidationException $e) {
return Response::error(implode(' ', array_merge(...array_values($e->errors()))));
}

$story = $this->resolveAccessibleStory($validated['story_id'], $user);
if ($story instanceof Response) {
return $story;
}

$type = ContextItemType::from($validated['type']);
$metadata = $type === ContextItemType::Text
? ['body' => $validated['body'] ?? '']
: ['url' => $validated['url'] ?? ''];

try {
$item = $writer->createStoryItem($story, [
'type' => $type,
'title' => $validated['title'],
'metadata' => $metadata,
], $user);
} catch (InvalidArgumentException $e) {
return Response::error($e->getMessage());
}

return Response::json([
'id' => $item->id,
'project_id' => $item->project_id,
'story_id' => $item->story_id,
'type' => $item->type->value,
'title' => $item->title,
'metadata' => $item->metadata,
]);
}

public function schema(JsonSchema $schema): array
{
return [
'story_id' => $schema->integer()->description('Story to attach the asset to.')->required(),
'type' => $schema->string()->enum(['text', 'link'])->description('Asset type: "text" for a note, "link" for a URL.')->required(),
'title' => $schema->string()->description('Asset title.')->required(),
'body' => $schema->string()->description('Body text. Required when type is "text".'),
'url' => $schema->string()->description('URL. Required when type is "link".'),
];
}
}
51 changes: 51 additions & 0 deletions app/Mcp/Tools/DeleteContextItemTool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Mcp\Tools;

use App\Mcp\Concerns\ResolvesProjectAccess;
use App\Models\ContextItem;
use App\Services\Context\ContextItemWriter;
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Attributes\Description;
use Laravel\Mcp\Server\Tool;

#[Description('Delete a context asset by ID. Story-scoped deletions reopen the story for approval.')]
class DeleteContextItemTool extends Tool
{
use ResolvesProjectAccess;

protected string $name = 'delete-context-item';

public function handle(Request $request, ContextItemWriter $writer): Response
{
$user = $this->resolveUser($request);
if ($user instanceof Response) {
return $user;
}

$validated = $request->validate([
'id' => ['required', 'integer'],
]);

$item = ContextItem::query()
->whereIn('project_id', $user->accessibleProjectIds())
->find($validated['id']);

if (! $item) {
return Response::error('Context item not found.');
}

$writer->delete($item, $user);

return Response::json(['deleted' => true, 'id' => $validated['id']]);
}

public function schema(JsonSchema $schema): array
{
return [
'id' => $schema->integer()->description('Context item ID to delete.')->required(),
];
}
}
Loading