-
Notifications
You must be signed in to change notification settings - Fork 161
[Schema][Server] feat: support sampling with tools #409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wWzZb
wants to merge
1
commit into
modelcontextprotocol:main
Choose a base branch
from
wWzZb:codex/issue-155-sampling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -19,17 +19,41 @@ | |||||
| * | ||||||
| * @phpstan-type SamplingMessageData = array{ | ||||||
| * role: 'user'|'assistant', | ||||||
| * content: TextContent|ImageContent|AudioContent | ||||||
| * content: array<string, mixed>|array<array<string, mixed>>, | ||||||
| * _meta?: array<string, mixed> | ||||||
| * } | ||||||
| * | ||||||
| * @author Kyrian Obikwelu <koshnawaza@gmail.com> | ||||||
| */ | ||||||
| class SamplingMessage extends Content | ||||||
| { | ||||||
| /** | ||||||
| * @param TextContent|ImageContent|AudioContent|ToolUseContent|ToolResultContent|list<TextContent|ImageContent|AudioContent|ToolUseContent|ToolResultContent> $content | ||||||
| * @param ?array<string, mixed> $meta | ||||||
| */ | ||||||
| public function __construct( | ||||||
| public readonly Role $role, | ||||||
| public readonly TextContent|ImageContent|AudioContent $content, | ||||||
| public readonly TextContent|ImageContent|AudioContent|ToolUseContent|ToolResultContent|array $content, | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please try to introduce a @phpstan-type SamplingContent = TextContent|ImageContent|AudioContent|ToolUseContent|ToolResultContentso we could maybe shorten this with:
Suggested change
|
||||||
| public readonly ?array $meta = null, | ||||||
| ) { | ||||||
| $contents = \is_array($content) ? $content : [$content]; | ||||||
| foreach ($contents as $item) { | ||||||
| if (!$item instanceof TextContent && !$item instanceof ImageContent && !$item instanceof AudioContent && !$item instanceof ToolUseContent && !$item instanceof ToolResultContent) { | ||||||
| throw new InvalidArgumentException('Sampling message content contains an unsupported content block.'); | ||||||
| } | ||||||
| if (Role::User === $role && $item instanceof ToolUseContent) { | ||||||
| throw new InvalidArgumentException('ToolUseContent is only valid in assistant sampling messages.'); | ||||||
| } | ||||||
| if (Role::Assistant === $role && $item instanceof ToolResultContent) { | ||||||
| throw new InvalidArgumentException('ToolResultContent is only valid in user sampling messages.'); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (array_filter($contents, static fn ($item): bool => $item instanceof ToolResultContent) | ||||||
| && array_filter($contents, static fn ($item): bool => !$item instanceof ToolResultContent)) { | ||||||
| throw new InvalidArgumentException('Tool result messages must not contain other content types.'); | ||||||
| } | ||||||
|
|
||||||
| parent::__construct('sampling'); | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -47,26 +71,55 @@ public static function fromArray(array $data): self | |||||
|
|
||||||
| $role = Role::from($data['role']); | ||||||
| $contentData = $data['content']; | ||||||
| $contentType = $contentData['type'] ?? null; | ||||||
| $isSingleContent = isset($contentData['type']); | ||||||
| $contentItems = $isSingleContent ? [$contentData] : $contentData; | ||||||
| $content = []; | ||||||
|
|
||||||
| $contentInstance = match ($contentType) { | ||||||
| 'text' => TextContent::fromArray($contentData), | ||||||
| 'image' => ImageContent::fromArray($contentData), | ||||||
| 'audio' => AudioContent::fromArray($contentData), | ||||||
| default => throw new InvalidArgumentException(\sprintf('Invalid content type "%s" for SamplingMessage.', $contentType)), | ||||||
| }; | ||||||
| foreach ($contentItems as $item) { | ||||||
| if (!\is_array($item)) { | ||||||
| throw new InvalidArgumentException('Invalid content block in SamplingMessage data.'); | ||||||
| } | ||||||
| $content[] = self::hydrateContent($item); | ||||||
| } | ||||||
|
|
||||||
| return new self($role, $contentInstance); | ||||||
| return new self( | ||||||
| $role, | ||||||
| $isSingleContent ? $content[0] : $content, | ||||||
| isset($data['_meta']) && \is_array($data['_meta']) ? $data['_meta'] : null, | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * @return SamplingMessageData | ||||||
| */ | ||||||
| public function jsonSerialize(): array | ||||||
| { | ||||||
| return [ | ||||||
| $data = [ | ||||||
| 'role' => $this->role->value, | ||||||
| 'content' => $this->content, | ||||||
| ]; | ||||||
|
|
||||||
| if (null !== $this->meta) { | ||||||
| $data['_meta'] = $this->meta; | ||||||
| } | ||||||
|
|
||||||
| return $data; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * @param array<string, mixed> $contentData | ||||||
| */ | ||||||
| private static function hydrateContent(array $contentData): TextContent|ImageContent|AudioContent|ToolUseContent|ToolResultContent | ||||||
| { | ||||||
| $contentType = $contentData['type'] ?? null; | ||||||
|
|
||||||
| return match ($contentType) { | ||||||
| 'text' => TextContent::fromArray($contentData), | ||||||
| 'image' => ImageContent::fromArray($contentData), | ||||||
| 'audio' => AudioContent::fromArray($contentData), | ||||||
| 'tool_use' => ToolUseContent::fromArray($contentData), | ||||||
| 'tool_result' => ToolResultContent::fromArray($contentData), | ||||||
| default => throw new InvalidArgumentException(\sprintf('Invalid content type "%s" for SamplingMessage.', $contentType)), | ||||||
| }; | ||||||
| } | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the official PHP MCP SDK. | ||
| * | ||
| * A collaboration between Symfony and the PHP Foundation. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Mcp\Schema\Content; | ||
|
|
||
| use Mcp\Exception\InvalidArgumentException; | ||
|
|
||
| /** | ||
| * The result of a tool use, provided by the user back to the assistant. | ||
| */ | ||
| final class ToolResultContent extends Content | ||
| { | ||
| /** | ||
| * @param Content[] $content | ||
| * @param ?array<string, mixed> $structuredContent | ||
| * @param ?array<string, mixed> $meta | ||
| */ | ||
| public function __construct( | ||
| public readonly string $toolUseId, | ||
| public readonly array $content, | ||
| public readonly ?array $structuredContent = null, | ||
| public readonly bool $isError = false, | ||
| public readonly ?array $meta = null, | ||
| ) { | ||
| foreach ($content as $item) { | ||
| if (!$item instanceof Content || $item instanceof self || $item instanceof ToolUseContent) { | ||
| throw new InvalidArgumentException('Tool result content must contain standard content blocks.'); | ||
| } | ||
| } | ||
|
|
||
| parent::__construct('tool_result'); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $data | ||
| */ | ||
| public static function fromArray(array $data): self | ||
| { | ||
| if (!isset($data['toolUseId']) || !\is_string($data['toolUseId'])) { | ||
| throw new InvalidArgumentException('Missing or invalid "toolUseId" in ToolResultContent data.'); | ||
| } | ||
| if (!isset($data['content']) || !\is_array($data['content'])) { | ||
| throw new InvalidArgumentException('Missing or invalid "content" in ToolResultContent data.'); | ||
| } | ||
|
|
||
| $content = []; | ||
| foreach ($data['content'] as $item) { | ||
| if (!\is_array($item)) { | ||
| throw new InvalidArgumentException('Invalid content block in ToolResultContent data.'); | ||
| } | ||
|
|
||
| $content[] = match ($item['type'] ?? null) { | ||
| 'text' => TextContent::fromArray($item), | ||
| 'image' => ImageContent::fromArray($item), | ||
| 'audio' => AudioContent::fromArray($item), | ||
| 'resource' => EmbeddedResource::fromArray($item), | ||
| default => throw new InvalidArgumentException(\sprintf('Unsupported tool result content type "%s".', $item['type'] ?? null)), | ||
| }; | ||
| } | ||
|
|
||
| return new self( | ||
| $data['toolUseId'], | ||
| $content, | ||
| isset($data['structuredContent']) && \is_array($data['structuredContent']) ? $data['structuredContent'] : null, | ||
| isset($data['isError']) && true === $data['isError'], | ||
| isset($data['_meta']) && \is_array($data['_meta']) ? $data['_meta'] : null, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function jsonSerialize(): array | ||
| { | ||
| $data = [ | ||
| 'type' => $this->type, | ||
| 'toolUseId' => $this->toolUseId, | ||
| 'content' => $this->content, | ||
| 'isError' => $this->isError, | ||
| ]; | ||
|
|
||
| if (null !== $this->structuredContent) { | ||
| $data['structuredContent'] = $this->structuredContent; | ||
| } | ||
| if (null !== $this->meta) { | ||
| $data['_meta'] = $this->meta; | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the official PHP MCP SDK. | ||
| * | ||
| * A collaboration between Symfony and the PHP Foundation. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Mcp\Schema\Content; | ||
|
|
||
| use Mcp\Exception\InvalidArgumentException; | ||
|
|
||
| /** | ||
| * A request from the assistant to call a tool. | ||
| */ | ||
| final class ToolUseContent extends Content | ||
| { | ||
| /** | ||
| * @param array<string, mixed> $input | ||
| * @param ?array<string, mixed> $meta | ||
| */ | ||
| public function __construct( | ||
| public readonly string $id, | ||
| public readonly string $name, | ||
| public readonly array $input, | ||
| public readonly ?array $meta = null, | ||
| ) { | ||
| parent::__construct('tool_use'); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{id?: mixed, name?: mixed, input?: mixed, _meta?: mixed} $data | ||
| */ | ||
| public static function fromArray(array $data): self | ||
| { | ||
| if (!isset($data['id']) || !\is_string($data['id'])) { | ||
| throw new InvalidArgumentException('Missing or invalid "id" in ToolUseContent data.'); | ||
| } | ||
| if (!isset($data['name']) || !\is_string($data['name'])) { | ||
| throw new InvalidArgumentException('Missing or invalid "name" in ToolUseContent data.'); | ||
| } | ||
| if (!isset($data['input']) || !\is_array($data['input'])) { | ||
| throw new InvalidArgumentException('Missing or invalid "input" in ToolUseContent data.'); | ||
| } | ||
|
|
||
| return new self( | ||
| $data['id'], | ||
| $data['name'], | ||
| $data['input'], | ||
| isset($data['_meta']) && \is_array($data['_meta']) ? $data['_meta'] : null, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{type: 'tool_use', id: string, name: string, input: array<string, mixed>|\stdClass, _meta?: array<string, mixed>} | ||
| */ | ||
| public function jsonSerialize(): array | ||
| { | ||
| $data = [ | ||
| 'type' => $this->type, | ||
| 'id' => $this->id, | ||
| 'name' => $this->name, | ||
| 'input' => $this->input ?: new \stdClass(), | ||
| ]; | ||
|
|
||
| if (null !== $this->meta) { | ||
| $data['_meta'] = $this->meta; | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the official PHP MCP SDK. | ||
| * | ||
| * A collaboration between Symfony and the PHP Foundation. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Mcp\Schema\Enum; | ||
|
|
||
| enum SamplingStopReason: string | ||
| { | ||
| case EndTurn = 'endTurn'; | ||
| case StopSequence = 'stopSequence'; | ||
| case MaxTokens = 'maxTokens'; | ||
| case ToolUse = 'toolUse'; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the official PHP MCP SDK. | ||
| * | ||
| * A collaboration between Symfony and the PHP Foundation. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Mcp\Schema\Enum; | ||
|
|
||
| enum ToolChoiceMode: string | ||
| { | ||
| case Auto = 'auto'; | ||
| case Required = 'required'; | ||
| case None = 'none'; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add tests for those code branches