Skip to content
Closed
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';
}
53 changes: 53 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 All @@ -19,6 +21,16 @@ class Response
use Conditionable;
use Macroable;

/**
* @var array<string, mixed>
*/
protected array $meta = [];

/**
* @var array<string, mixed>
*/
protected array $structured_content = [];

protected function __construct(
protected Content $content,
protected Role $role = Role::USER,
Expand All @@ -40,6 +52,17 @@ public static function text(string $text): static
return new static(new Text($text));
}

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
);
}

/**
* @internal
*
Expand Down Expand Up @@ -103,4 +126,34 @@ public function role(): Role
{
return $this->role;
}

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

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

return $this;
}

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

$this->structured_content = array_merge($this->structured_content, $structuredContent);

return $this;
}
}
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,
];
}
}
13 changes: 10 additions & 3 deletions src/Server/Methods/CallTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,20 @@ public function handle(JsonRpcRequest $request, ServerContext $context): Generat
}

/**
* @return callable(Collection<int, Response>): array{content: array<int, array<string, mixed>>, isError: bool}
* @return callable(Collection<int, Response>):array{
* _meta?: array<string, mixed>,
* content?: array<int, array<string, mixed>>,
* isError?: bool,
* structuredContent?: array<string, mixed>,
* }
*/
protected function serializable(Tool $tool): callable
{
return fn (Collection $responses): array => [
return fn (Collection $responses): array => array_filter([
'content' => $responses->map(fn (Response $response): array => $response->content()->toTool($tool))->all(),
'isError' => $responses->contains(fn (Response $response): bool => $response->isError()),
];
'structuredContent' => $responses->flatMap(fn (Response $response): array => $response->structuredContent())->all(),
'_meta' => $responses->flatMap(fn (Response $response): array => $response->meta())->all(),
], filled(...));
}
}
13 changes: 13 additions & 0 deletions src/Server/Primitive.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ abstract class Primitive implements Arrayable

protected string $description = '';

/**
* @var array<string, mixed>
*/
protected array $meta = [];

public function name(): string
{
return $this->name === ''
Expand All @@ -40,6 +45,14 @@ public function description(): string
: $this->description;
}

/**
* @return array<string, mixed>
*/
public function meta(): array
{
return $this->meta;
}

public function eligibleForRegistration(): bool
{
if (method_exists($this, 'shouldRegister')) {
Expand Down
35 changes: 27 additions & 8 deletions src/Server/Tool.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\JsonSchema\JsonSchema;
use Laravel\Mcp\Server\Contracts\Tools\Annotation;
use Laravel\Mcp\Support\SecurityScheme;
use ReflectionAttribute;
use ReflectionClass;

Expand All @@ -19,6 +20,14 @@ public function schema(JsonSchema $schema): array
return [];
}

/**
* @return array<string, mixed>
*/
public function securitySchemes(SecurityScheme $scheme): array
{
return [];
}

/**
* @return array<string, mixed>
*/
Expand Down Expand Up @@ -51,7 +60,9 @@ public function toMethodCall(): array
* title?: string|null,
* description?: string|null,
* inputSchema?: array<string, mixed>,
* annotations?: array<string, mixed>|object
* securitySchemes?: array<string, mixed>,
* annotations?: array<string, mixed>|object,
* _meta?: array<string, mixed>,
* }
*/
public function toArray(): array
Expand All @@ -63,12 +74,20 @@ public function toArray(): array

$schema['properties'] ??= (object) [];

return [
'name' => $this->name(),
'title' => $this->title(),
'description' => $this->description(),
'inputSchema' => $schema,
'annotations' => $annotations === [] ? (object) [] : $annotations,
];
return array_merge(
[
'name' => $this->name(),
'title' => $this->title(),
'description' => $this->description(),
'inputSchema' => $schema,
'annotations' => $annotations === [] ? (object) [] : $annotations,
],
array_filter([
'securitySchemes' => SecurityScheme::make(
$this->securitySchemes(...),
),
'_meta' => filled($this->meta()) ? $this->meta() : null,
], filled(...))
);
}
}
Loading
Loading