1313
1414use Mcp \Exception \InvalidArgumentException ;
1515use Mcp \Exception \RegistryException ;
16+ use Mcp \Server \ClientAwareInterface ;
1617use Mcp \Server \ClientGateway ;
18+ use Mcp \Server \Session \SessionInterface ;
1719use Psr \Container \ContainerInterface ;
1820
1921/**
@@ -31,12 +33,18 @@ public function __construct(
3133 */
3234 public function handle (ElementReference $ reference , array $ arguments ): mixed
3335 {
36+ $ session = $ arguments ['_session ' ];
37+
3438 if (\is_string ($ reference ->handler )) {
3539 if (class_exists ($ reference ->handler ) && method_exists ($ reference ->handler , '__invoke ' )) {
3640 $ reflection = new \ReflectionMethod ($ reference ->handler , '__invoke ' );
3741 $ instance = $ this ->getClassInstance ($ reference ->handler );
3842 $ arguments = $ this ->prepareArguments ($ reflection , $ arguments );
3943
44+ if ($ instance instanceof ClientAwareInterface) {
45+ $ instance ->setClientGateway (new ClientGateway ($ session ));
46+ }
47+
4048 return \call_user_func ($ instance , ...$ arguments );
4149 }
4250
@@ -49,7 +57,7 @@ public function handle(ElementReference $reference, array $arguments): mixed
4957 }
5058
5159 if (\is_callable ($ reference ->handler )) {
52- $ reflection = $ this ->getReflectionForCallable ($ reference ->handler );
60+ $ reflection = $ this ->getReflectionForCallable ($ reference ->handler , $ session );
5361 $ arguments = $ this ->prepareArguments ($ reflection , $ arguments );
5462
5563 return \call_user_func ($ reference ->handler , ...$ arguments );
@@ -59,6 +67,11 @@ public function handle(ElementReference $reference, array $arguments): mixed
5967 [$ className , $ methodName ] = $ reference ->handler ;
6068 $ reflection = new \ReflectionMethod ($ className , $ methodName );
6169 $ instance = $ this ->getClassInstance ($ className );
70+
71+ if ($ instance instanceof ClientAwareInterface) {
72+ $ instance ->setClientGateway (new ClientGateway ($ session ));
73+ }
74+
6275 $ arguments = $ this ->prepareArguments ($ reflection , $ arguments );
6376
6477 return \call_user_func ([$ instance , $ methodName ], ...$ arguments );
@@ -130,7 +143,7 @@ private function prepareArguments(\ReflectionFunctionAbstract $reflection, array
130143 /**
131144 * Gets a ReflectionMethod or ReflectionFunction for a callable.
132145 */
133- private function getReflectionForCallable (callable $ handler ): \ReflectionMethod |\ReflectionFunction
146+ private function getReflectionForCallable (callable $ handler, SessionInterface $ session ): \ReflectionMethod |\ReflectionFunction
134147 {
135148 if (\is_string ($ handler )) {
136149 return new \ReflectionFunction ($ handler );
@@ -143,6 +156,10 @@ private function getReflectionForCallable(callable $handler): \ReflectionMethod|
143156 if (\is_array ($ handler ) && 2 === \count ($ handler )) {
144157 [$ class , $ method ] = $ handler ;
145158
159+ if ($ class instanceof ClientAwareInterface) {
160+ $ class ->setClientGateway (new ClientGateway ($ session ));
161+ }
162+
146163 return new \ReflectionMethod ($ class , $ method );
147164 }
148165
0 commit comments