1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
12: */
13:
14: namespace Nexus\Mcp\Client\Auth;
15:
16: use Amp\Cancellation;
17: use Amp\Http\Client\DelegateHttpClient;
18: use Nexus\Mcp\Core\Auth\ResourceIdentifier;
19: use Nexus\Mcp\Core\Auth\ScopeSet;
20: use Nexus\Mcp\Core\Exception\RuntimeException;
21: use Psr\Log\LoggerInterface;
22:
23: /**
24: * What a grant strategy draws on to obtain an access token.
25: */
26: final readonly class GrantContext
27: {
28: /**
29: * @internal Built by the authorization coordinator and handed to a strategy, never constructed by one
30: */
31: public function __construct(
32: public DiscoveredResource $discovered,
33: public ResourceIdentifier $resource,
34: public ScopeSet $scopes,
35: public AuthorizationOptions $options,
36: public DelegateHttpClient $httpClient,
37: public LoggerInterface $logger,
38: private ClientRegistrar $registrar,
39: private TokenEndpoint $tokenEndpoint,
40: ) {
41: }
42:
43: /**
44: * Resolves the `client_id` to present, preferring pre-registered credentials, then a Client ID Metadata
45: * Document, then Dynamic Client Registration.
46: *
47: * @throws RuntimeException
48: */
49: public function resolveRegistration(Cancellation $cancellation): ClientRegistration
50: {
51: return $this->registrar->resolve($this->discovered->server, $this->options, $cancellation);
52: }
53:
54: /**
55: * @param array<string, string> $parameters Full form body of the grant, `grant_type` included
56: * @param null|ScopeSet $requestedScopes Scopes the token carries when the response names none, defaulting to the context's
57: */
58: public function requestToken(
59: ClientRegistration $registration,
60: array $parameters,
61: Cancellation $cancellation,
62: ?ScopeSet $requestedScopes = null,
63: ): AccessToken {
64: return $this->tokenEndpoint->requestToken(
65: $this->discovered->server,
66: $registration,
67: $parameters,
68: $requestedScopes ?? $this->scopes,
69: $cancellation,
70: );
71: }
72: }
73: