-
Notifications
You must be signed in to change notification settings - Fork 103
Add interface abstractions for SchemaGenerator and Discoverer with DI improvements #215
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
base: main
Are you sure you want to change the base?
Changes from all commits
f98aa60
61ed415
7c5cbcc
9c63ce2
f529681
9efe7ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
|
||
| /** | ||
| * Discovers MCP elements (tools, resources, prompts, resource templates) in directories. | ||
| * | ||
| * @internal | ||
| * | ||
| * @author Antoine Bluchet <soyuka@gmail.com> | ||
| */ | ||
| interface DiscovererInterface | ||
| { | ||
| /** | ||
| * Discover MCP elements in the specified directories and return the discovery state. | ||
| * | ||
| * @param string $basePath the base path for resolving directories | ||
| * @param array<string> $directories list of directories (relative to base path) to scan | ||
| * @param array<string> $excludeDirs list of directories (relative to base path) to exclude from the scan | ||
| */ | ||
| public function discover(string $basePath, array $directories, array $excludeDirs = []): DiscoveryState; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,20 +56,28 @@ | |
| * | ||
| * @author Kyrian Obikwelu <koshnawaza@gmail.com> | ||
| */ | ||
| class SchemaGenerator | ||
| final class SchemaGenerator implements SchemaGeneratorInterface | ||
| { | ||
| public function __construct( | ||
| private readonly DocBlockParser $docBlockParser, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * Generates a JSON Schema object (as a PHP array) for a method's or function's parameters. | ||
| * Generates a JSON Schema object (as a PHP array) for parameters. | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function generate(\ReflectionMethod|\ReflectionFunction $reflection): array | ||
| public function generate(\Reflector $reflection): array | ||
| { | ||
| if ($reflection instanceof \ReflectionClass) { | ||
| throw new \BadMethodCallException('Schema generation from ReflectionClass is not implemented yet. Use ReflectionMethod or ReflectionFunction instead.'); | ||
| } | ||
|
|
||
| if (!$reflection instanceof \ReflectionMethod && !$reflection instanceof \ReflectionFunction) { | ||
| throw new \BadMethodCallException(\sprintf('Schema generation from %s is not supported. Use ReflectionMethod or ReflectionFunction instead.', $reflection::class)); | ||
|
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. here as well please |
||
| } | ||
|
|
||
| $methodSchema = $this->extractMethodLevelSchema($reflection); | ||
|
|
||
| if ($methodSchema && isset($methodSchema['definition'])) { | ||
|
|
@@ -86,7 +94,7 @@ public function generate(\ReflectionMethod|\ReflectionFunction $reflection): arr | |
| * | ||
| * @return SchemaAttributeData | ||
| */ | ||
| private function extractMethodLevelSchema(\ReflectionMethod|\ReflectionFunction $reflection): ?array | ||
| private function extractMethodLevelSchema(\ReflectionFunctionAbstract $reflection): ?array | ||
| { | ||
| $schemaAttrs = $reflection->getAttributes(Schema::class, \ReflectionAttribute::IS_INSTANCEOF); | ||
| if (empty($schemaAttrs)) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?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; | ||
|
|
||
| /** | ||
| * Provides JSON Schema generation for reflected elements. | ||
| * | ||
| * @author Antoine Bluchet <soyuka@gmail.com> | ||
| */ | ||
| interface SchemaGeneratorInterface | ||
| { | ||
| /** | ||
| * Generates a JSON Schema for input parameters. | ||
| * | ||
| * The returned schema must be a valid JSON Schema object (type: 'object') | ||
| * with properties corresponding to a tool's parameters. | ||
| * | ||
| * @return array{ | ||
| * type: 'object', | ||
| * properties: array<string, mixed>|object, | ||
| * required?: string[] | ||
| * } | ||
| */ | ||
| public function generate(\Reflector $reflection): array; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,8 @@ | |
|
|
||
| namespace Mcp\Server; | ||
|
|
||
| use Mcp\Capability\Discovery\DiscovererInterface; | ||
| use Mcp\Capability\Discovery\SchemaGeneratorInterface; | ||
| use Mcp\Capability\Registry; | ||
| use Mcp\Capability\Registry\Container; | ||
| use Mcp\Capability\Registry\ElementReference; | ||
|
|
@@ -58,6 +60,10 @@ final class Builder | |
|
|
||
| private ?ContainerInterface $container = null; | ||
|
|
||
| private ?SchemaGeneratorInterface $schemaGenerator = null; | ||
|
|
||
| private ?DiscovererInterface $discoverer = null; | ||
|
|
||
| private ?SessionFactoryInterface $sessionFactory = null; | ||
|
|
||
| private ?SessionStoreInterface $sessionStore = null; | ||
|
|
@@ -286,6 +292,20 @@ public function setContainer(ContainerInterface $container): self | |
| return $this; | ||
| } | ||
|
|
||
| public function setSchemaGenerator(SchemaGeneratorInterface $schemaGenerator): self | ||
| { | ||
| $this->schemaGenerator = $schemaGenerator; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function setDiscoverer(DiscovererInterface $discoverer): self | ||
| { | ||
| $this->discoverer = $discoverer; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function setSession( | ||
| SessionStoreInterface $sessionStore, | ||
| SessionFactoryInterface $sessionFactory = new SessionFactory(), | ||
|
|
@@ -466,11 +486,12 @@ public function build(): Server | |
|
|
||
| $loaders = [ | ||
| ...$this->loaders, | ||
| new ArrayLoader($this->tools, $this->resources, $this->resourceTemplates, $this->prompts, $logger), | ||
| new ArrayLoader($this->tools, $this->resources, $this->resourceTemplates, $this->prompts, $logger, $this->schemaGenerator), | ||
| ]; | ||
|
|
||
| if (null !== $this->discoveryBasePath) { | ||
| $loaders[] = new DiscoveryLoader($this->discoveryBasePath, $this->discoveryScanDirs, $this->discoveryExcludeDirs, $logger, $this->discoveryCache); | ||
| $discoverer = $this->discoverer ?? $this->createDiscoverer($logger); | ||
| $loaders[] = new DiscoveryLoader($this->discoveryBasePath, $this->discoveryScanDirs, $this->discoveryExcludeDirs, $discoverer); | ||
| } | ||
|
|
||
| foreach ($loaders as $loader) { | ||
|
|
@@ -527,4 +548,15 @@ public function build(): Server | |
|
|
||
| return new Server($protocol, $logger); | ||
| } | ||
|
|
||
| private function createDiscoverer(LoggerInterface $logger): DiscovererInterface | ||
| { | ||
| $discoverer = new \Mcp\Capability\Discovery\Discoverer($logger, null, $this->schemaGenerator); | ||
|
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. could use some imports here |
||
|
|
||
| if (null !== $this->discoveryCache) { | ||
| return new \Mcp\Capability\Discovery\CachedDiscoverer($discoverer, $this->discoveryCache, $logger); | ||
|
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. here as well, please |
||
| } | ||
|
|
||
| return $discoverer; | ||
| } | ||
| } | ||
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 introduce a package specific exception here, see
Mcp\Exceptionnamespace