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
53 changes: 53 additions & 0 deletions src/Capability/Discovery/Discoverer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
use Mcp\Schema\ResourceDefinition;
use Mcp\Schema\ResourceTemplate;
use Mcp\Schema\Tool;
use McpClient\Entity\ClientConfig;
use McpServer\Attributes\McpPermissions;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Finder\Finder;
Expand All @@ -53,6 +55,7 @@ public function __construct(
private readonly LoggerInterface $logger = new NullLogger(),
private ?DocBlockParser $docBlockParser = null,
private ?SchemaGeneratorInterface $schemaGenerator = null,
private readonly ?ClientConfig $clientConfig = null,
) {
if (!class_exists(Finder::class)) {
throw new RuntimeException('File-based discovery requires symfony/finder. Run: composer require symfony/finder');
Expand Down Expand Up @@ -175,6 +178,9 @@ private function processFile(SplFileInfo $file, array &$discoveredCount, array &

if (!$processedViaClassAttribute) {
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
if ($this->clientConfig && !$this->matchesClaims($method)) {
continue;
}
if (
$method->getDeclaringClass()->getName() !== $reflectionClass->getName()
|| $method->isStatic() || $method->isAbstract() || $method->isConstructor() || $method->isDestructor() || '__invoke' === $method->getName()
Expand Down Expand Up @@ -452,4 +458,51 @@ private function getClassFromFile(SplFileInfo $file): ?string

return null;
}

/**
* @param \ReflectionMethod $method
* @return bool
*/
private function matchesClaims(\ReflectionMethod $method): bool
{
$attrs = $method->getAttributes(McpPermissions::class);

if (empty($attrs)) {
return false;
}

/** @var McpPermissions $permissionAttrs */
$permissionAttrs = $attrs[0]->newInstance();

foreach (get_object_vars($permissionAttrs) as $key => $allowedValues) {
//skip attributes that aren't specified
if ($allowedValues === null) {
continue;
}

switch ($key) {
case 'permissions':
$claimValue = $this->clientConfig->getPermissions();
if (empty(array_intersect($allowedValues, $claimValue))) {
return false;
}
break;
case 'language':
$claimValue = $this->clientConfig->getLanguage()->getLanguageCode();
if (!in_array($claimValue, $allowedValues, false)) {
return false;
}
break;
case 'clientOrigin':
$claimValue = $this->clientConfig->getClientOrigin();
if (!in_array($claimValue, $allowedValues, false)) {
return false;
}
break;
default:
return false;
}
}
return true;
}
}
7 changes: 7 additions & 0 deletions src/Capability/Registry/ReferenceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
use Mcp\Server\ClientGateway;
use Mcp\Server\RequestContext;
use Mcp\Server\Session\SessionInterface;
use McpClient\Entity\ClientConfig;
use McpServer\Primitives\AbstractClaimsAwarePrimitive;
use McpServer\Session\SessionClaimsService;
use Psr\Container\ContainerInterface;

/**
Expand Down Expand Up @@ -72,6 +75,10 @@ public function handle(ElementReference $reference, array $arguments): mixed
[$className, $methodName] = $reference->handler;
$reflection = new \ReflectionMethod($className, $methodName);
$instance = $this->getClassInstance($className);
$claims = $session? $this->container->get(SessionClaimsService::class)->getClaims($session):null;
if ($claims instanceof ClientConfig && $instance instanceof AbstractClaimsAwarePrimitive) {
($instance)->setClaims($claims);
}
$arguments = $this->prepareArguments($reflection, $arguments);

return \call_user_func([$instance, $methodName], ...$arguments);
Expand Down
9 changes: 9 additions & 0 deletions src/Server/Handler/Request/CompletionCompleteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Mcp\Schema\ResourceReference;
use Mcp\Schema\Result\CompletionCompleteResult;
use Mcp\Server\Session\SessionInterface;
use McpServer\Primitives\AbstractClaimsAwarePrimitive;
use McpServer\Session\SessionClaimsService;
use Psr\Container\ContainerInterface;

/**
Expand Down Expand Up @@ -77,6 +79,13 @@ public function handle(Request $request, SessionInterface $session): Response|Er
if (!$provider instanceof ProviderInterface) {
return Error::forInternalError('Invalid completion provider type', $request->getId());
}
if ($provider instanceof AbstractClaimsAwarePrimitive) {
$claimsService = $this->container->get(SessionClaimsService::class);
$claims = $claimsService->getClaims($session);
if ($claims) {
$provider->setClaims($claims);
}
}

$completions = $provider->getCompletions($value);
$total = \count($completions);
Expand Down
Loading