-
Notifications
You must be signed in to change notification settings - Fork 143
[Server] Add PropertyDescriberInterface extension point for SchemaGenerator #314
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
peter-si
wants to merge
3
commits into
modelcontextprotocol:main
Choose a base branch
from
peter-si:feat/property-describer-extension-point
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 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
30 changes: 30 additions & 0 deletions
30
src/Capability/Discovery/PropertyDescriber/DateTimePropertyDescriber.php
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,30 @@ | ||
| <?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\Capability\Discovery\PropertyDescriber; | ||
|
|
||
| use Mcp\Capability\Discovery\PropertyDescriberInterface; | ||
|
|
||
| /** | ||
| * Describes any {@see \DateTimeInterface} implementation as an ISO-8601 | ||
| * date-time string. | ||
| */ | ||
| final class DateTimePropertyDescriber implements PropertyDescriberInterface | ||
| { | ||
| public function describe(string $className): ?array | ||
| { | ||
| if (!is_a($className, \DateTimeInterface::class, true)) { | ||
| return null; | ||
| } | ||
|
|
||
| return ['type' => 'string', 'format' => 'date-time']; | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/Capability/Discovery/PropertyDescriber/UuidPropertyDescriber.php
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,31 @@ | ||
| <?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\Capability\Discovery\PropertyDescriber; | ||
|
|
||
| use Mcp\Capability\Discovery\PropertyDescriberInterface; | ||
| use Symfony\Component\Uid\Uuid; | ||
|
|
||
| /** | ||
| * Describes Symfony UID {@see Uuid} (and subclasses like `UuidV4`, `UuidV7`) | ||
| * as a uuid-format string. | ||
| */ | ||
| final class UuidPropertyDescriber implements PropertyDescriberInterface | ||
| { | ||
| public function describe(string $className): ?array | ||
| { | ||
| if (!is_a($className, Uuid::class, true)) { | ||
| return null; | ||
| } | ||
|
|
||
| return ['type' => 'string', 'format' => 'uuid']; | ||
| } | ||
| } |
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,31 @@ | ||
| <?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\Capability\Discovery; | ||
|
|
||
| /** | ||
| * Translates a PHP class type into a JSON Schema fragment. | ||
| * | ||
| * The {@see SchemaGenerator} consults registered describers, in order, before | ||
| * falling back to generic class inspection. The first describer that returns | ||
| * a non-null schema wins. Implementations let callers teach the generator | ||
| * about value-object types (DateTime, Uuid, etc.) whose JSON Schema | ||
| * representation is more specific than a generic `{type: "object"}`. | ||
| */ | ||
| interface PropertyDescriberInterface | ||
| { | ||
| /** | ||
| * @param class-string $className | ||
| * | ||
| * @return array<string, mixed>|null Schema fragment, or null to pass to the next describer | ||
| */ | ||
| public function describe(string $className): ?array; | ||
| } | ||
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
51 changes: 51 additions & 0 deletions
51
tests/Unit/Capability/Discovery/PropertyDescriber/DateTimePropertyDescriberTest.php
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,51 @@ | ||
| <?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\Tests\Unit\Capability\Discovery\PropertyDescriber; | ||
|
|
||
| use Mcp\Capability\Discovery\PropertyDescriber\DateTimePropertyDescriber; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class DateTimePropertyDescriberTest extends TestCase | ||
| { | ||
| private DateTimePropertyDescriber $describer; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->describer = new DateTimePropertyDescriber(); | ||
| } | ||
|
|
||
| public function testDescribesDateTimeInterfaceAsIsoDateTimeString(): void | ||
| { | ||
| $this->assertSame( | ||
| ['type' => 'string', 'format' => 'date-time'], | ||
| $this->describer->describe(\DateTimeInterface::class), | ||
| ); | ||
| } | ||
|
|
||
| public function testDescribesDateTimeImplementations(): void | ||
| { | ||
| $this->assertSame( | ||
| ['type' => 'string', 'format' => 'date-time'], | ||
| $this->describer->describe(\DateTime::class), | ||
| ); | ||
| $this->assertSame( | ||
| ['type' => 'string', 'format' => 'date-time'], | ||
| $this->describer->describe(\DateTimeImmutable::class), | ||
| ); | ||
| } | ||
|
|
||
| public function testPassesOnUnrelatedClass(): void | ||
| { | ||
| $this->assertNull($this->describer->describe(\stdClass::class)); | ||
| $this->assertNull($this->describer->describe(\Exception::class)); | ||
| } | ||
| } |
54 changes: 54 additions & 0 deletions
54
tests/Unit/Capability/Discovery/PropertyDescriber/UuidPropertyDescriberTest.php
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,54 @@ | ||
| <?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\Tests\Unit\Capability\Discovery\PropertyDescriber; | ||
|
|
||
| use Mcp\Capability\Discovery\PropertyDescriber\UuidPropertyDescriber; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Uid\Uuid; | ||
| use Symfony\Component\Uid\UuidV4; | ||
| use Symfony\Component\Uid\UuidV6; | ||
|
|
||
| final class UuidPropertyDescriberTest extends TestCase | ||
| { | ||
| private UuidPropertyDescriber $describer; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->describer = new UuidPropertyDescriber(); | ||
| } | ||
|
|
||
| public function testDescribesUuidAsUuidFormatString(): void | ||
| { | ||
| $this->assertSame( | ||
| ['type' => 'string', 'format' => 'uuid'], | ||
| $this->describer->describe(Uuid::class), | ||
| ); | ||
| } | ||
|
|
||
| public function testDescribesUuidSubclasses(): void | ||
| { | ||
| $this->assertSame( | ||
| ['type' => 'string', 'format' => 'uuid'], | ||
| $this->describer->describe(UuidV4::class), | ||
| ); | ||
| $this->assertSame( | ||
| ['type' => 'string', 'format' => 'uuid'], | ||
| $this->describer->describe(UuidV6::class), | ||
| ); | ||
| } | ||
|
|
||
| public function testPassesOnUnrelatedClass(): void | ||
| { | ||
| $this->assertNull($this->describer->describe(\stdClass::class)); | ||
| $this->assertNull($this->describer->describe(\DateTime::class)); | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -438,6 +438,37 @@ public function withParameterNamedRequest(string $_request): void | |
| { | ||
| } | ||
|
|
||
| // ===== PROPERTY DESCRIBER FIXTURES ===== | ||
|
|
||
| public function dateTimeParam(\DateTimeImmutable $createdAt): void | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @param \DateTimeInterface $until The cutoff timestamp | ||
| */ | ||
| public function dateTimeWithDescription(\DateTimeInterface $until): void | ||
| { | ||
| } | ||
|
|
||
| public function nullableDateTimeParam(?\DateTimeImmutable $finishedAt = null): void | ||
| { | ||
| } | ||
|
|
||
| public function uuidParam(\Symfony\Component\Uid\Uuid $bookingId): void | ||
|
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 use an import here for the type
Author
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. Done - added |
||
| { | ||
| } | ||
|
|
||
| public function unrelatedObjectParam(\stdClass $config): void | ||
| { | ||
| } | ||
|
|
||
| public function dateTimeWithSchemaAttributeOverride( | ||
| #[Schema(description: 'explicit attribute description')] | ||
| \DateTimeImmutable $deadline, | ||
| ): void { | ||
| } | ||
|
|
||
| // ===== OUTPUT SCHEMA FIXTURES ===== | ||
| #[McpTool( | ||
| outputSchema: [ | ||
|
|
||
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.
since we're only relying on the
$classNameand don't have any dynamic, we could be more explicit while designing this interface.think of sth like
public static function supportedClass(): class-string=> no implicit nullable as encoded non-support
=> static index+lookup instead of iterating over all describers for a property
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.
Changed the interface to
static supportedClass(): class-stringplus a non-nullabledescribe(): array, so support is declared explicitly rather than encoded as anullreturn. The generator matches a parameter's class againstsupportedClass()viais_a()(so\DateTimeInterfacestill covers\DateTimeImmutable,UuidcoversUuidV4, etc.) and memoizes the resolution per concrete class, so describers aren't re-scanned per property.I kept an ordered scan + per-class cache rather than a flat exact-class index, because the shipped describers need to match subclasses/interfaces, which an exact-key map can't express.