66
77use InvalidArgumentException ;
88use JsonSerializable ;
9+ use PhpMcp \Server \CallContext ;
910use PhpMcp \Server \Exception \McpServerException ;
1011use Psr \Container \ContainerInterface ;
1112use ReflectionException ;
@@ -30,33 +31,33 @@ public function __construct(
3031 $ this ->isManual = $ isManual ;
3132 }
3233
33- public function handle (ContainerInterface $ container , array $ arguments ): mixed
34+ public function handle (ContainerInterface $ container , array $ arguments, ? CallContext $ callContext = null ): mixed
3435 {
3536 if (is_string ($ this ->handler )) {
3637 if (class_exists ($ this ->handler ) && method_exists ($ this ->handler , '__invoke ' )) {
3738 $ reflection = new \ReflectionMethod ($ this ->handler , '__invoke ' );
38- $ arguments = $ this ->prepareArguments ($ reflection , $ arguments );
39+ $ arguments = $ this ->prepareArguments ($ reflection , $ arguments, $ callContext );
3940 $ instance = $ container ->get ($ this ->handler );
4041 return call_user_func ($ instance , ...$ arguments );
4142 }
4243
4344 if (function_exists ($ this ->handler )) {
4445 $ reflection = new \ReflectionFunction ($ this ->handler );
45- $ arguments = $ this ->prepareArguments ($ reflection , $ arguments );
46+ $ arguments = $ this ->prepareArguments ($ reflection , $ arguments, $ callContext );
4647 return call_user_func ($ this ->handler , ...$ arguments );
4748 }
4849 }
4950
5051 if (is_callable ($ this ->handler )) {
5152 $ reflection = $ this ->getReflectionForCallable ($ this ->handler );
52- $ arguments = $ this ->prepareArguments ($ reflection , $ arguments );
53+ $ arguments = $ this ->prepareArguments ($ reflection , $ arguments, $ callContext );
5354 return call_user_func ($ this ->handler , ...$ arguments );
5455 }
5556
5657 if (is_array ($ this ->handler )) {
5758 [$ className , $ methodName ] = $ this ->handler ;
5859 $ reflection = new \ReflectionMethod ($ className , $ methodName );
59- $ arguments = $ this ->prepareArguments ($ reflection , $ arguments );
60+ $ arguments = $ this ->prepareArguments ($ reflection , $ arguments, $ callContext );
6061
6162 $ instance = $ container ->get ($ className );
6263 return call_user_func ([$ instance , $ methodName ], ...$ arguments );
@@ -66,15 +67,22 @@ public function handle(ContainerInterface $container, array $arguments): mixed
6667 }
6768
6869
69- protected function prepareArguments (\ReflectionFunctionAbstract $ reflection , array $ arguments ): array
70+ protected function prepareArguments (\ReflectionFunctionAbstract $ reflection , array $ arguments, ? CallContext $ callContext ): array
7071 {
7172 $ finalArgs = [];
7273
7374 foreach ($ reflection ->getParameters () as $ parameter ) {
7475 // TODO: Handle variadic parameters.
7576 $ paramName = $ parameter ->getName ();
77+ $ paramType = $ parameter ->getType ();
7678 $ paramPosition = $ parameter ->getPosition ();
7779
80+ if ($ paramType ?->getName() === CallContext::class) {
81+ $ finalArgs [$ paramPosition ] = $ callContext ;
82+
83+ continue ;
84+ }
85+
7886 if (isset ($ arguments [$ paramName ])) {
7987 $ argument = $ arguments [$ paramName ];
8088 try {
0 commit comments