Skip to content
Draft
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
24 changes: 24 additions & 0 deletions src/Enums/OpenAI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Laravel\Mcp\Enums;

enum OpenAI: string
{
case OUTPUT_TEMPLATE = 'openai/outputTemplate';
case WIDGET_ACCESSIBLE = 'openai/widgetAccessible';
case TOOL_INVOKING = 'openai/toolInvocation/invoking';
case TOOL_INVOKED = 'openai/toolInvocation/invoked';

case WIDGET_DESCRIPTION = 'openai/widgetDescription';
case WIDGET_PREFERS_BORDER = 'openai/widgetPrefersBorder';
case WIDGET_CSP = 'openai/widgetCSP';
case WIDGET_DOMAIN = 'openai/widgetDomain';

case LOCALE = 'openai/locale';
case USER_AGENT = 'openai/userAgent';
case USER_LOCATION = 'openai/userLocation';

case MIME_TYPE = 'text/html+skybridge';
}
13 changes: 13 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Macroable;
use Illuminate\View\View;
use JsonException;
use Laravel\Mcp\Enums\Role;
use Laravel\Mcp\Exceptions\NotImplementedException;
use Laravel\Mcp\Server\Content\App;
use Laravel\Mcp\Server\Content\Blob;
use Laravel\Mcp\Server\Content\Notification;
use Laravel\Mcp\Server\Content\Text;
Expand Down Expand Up @@ -58,6 +60,17 @@ public static function blob(string $content): static
return new static(new Blob($content));
}

public static function app(string|View $view, ?callable $config = null): static
{
$view = $view instanceof View ? $view->render() : $view;

$app = new App($view);

return new static(
$config ? $config($app) : $app
);
}

public static function error(string $text): static
{
return new static(new Text($text), isError: true);
Expand Down
117 changes: 117 additions & 0 deletions src/Server/Content/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

declare(strict_types=1);

namespace Laravel\Mcp\Server\Content;

use Exception;
use Laravel\Mcp\Enums\OpenAI;
use Laravel\Mcp\Server\Contracts\Content;
use Laravel\Mcp\Server\Prompt;
use Laravel\Mcp\Server\Resource;
use Laravel\Mcp\Server\Tool;

class App implements Content
{
/**
* @var array<string, mixed>
*/
protected array $meta = [];

public function __construct(
protected string $text,
) {}

/**
* @return array<string, mixed>
*/
public function toTool(Tool $tool): array
{
throw new Exception('App should only be used from a Resource.');
}

/**
* @return array<string, mixed>
*/
public function toPrompt(Prompt $prompt): array
{
throw new Exception('App should only be used from a Resource.');
}

/**
* @return array<string, mixed>
*/
public function toResource(Resource $resource): array
{
return array_filter([
'text' => $this->text,
'uri' => $resource->uri(),
'name' => $resource->name(),
'title' => $resource->title(),
'mimeType' => $resource->mimeType(),
'_meta' => $this->meta,
], filled(...));
}

/**
* @param array<string, mixed>|null $meta
* @return ($meta is null ? array<string, mixed> : self)
*/
public function meta(?array $meta = null): self|array
{
if (is_null($meta)) {
return $this->meta;
}

$this->meta = array_merge($this->meta, $meta);

return $this;
}

public function prefersBorder(bool $value = true): self
{
$this->meta[OpenAI::WIDGET_PREFERS_BORDER->value] = $value;

return $this;
}

public function widgetDescription(string $value): self
{
$this->meta[OpenAI::WIDGET_DESCRIPTION->value] = $value;

return $this;
}

/**
* @param array<string, mixed> $value
*/
public function widgetCSP(array $value): self
{
$this->meta[OpenAI::WIDGET_CSP->value] = $value;

return $this;
}

public function widgetDomain(string $value): self
{
$this->meta[OpenAI::WIDGET_DOMAIN->value] = $value;

return $this;
}

public function __toString(): string
{
return $this->text;
}

/**
* @return array<string, mixed>
*/
public function toArray(): array
{
return [
'type' => 'text',
'text' => $this->text,
];
}
}
Loading