[Server] Implement variadic parameter handling in ReferenceHandler#36
[Server] Implement variadic parameter handling in ReferenceHandler#36monayemislam wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
- Add support for variadic parameters (...) in prepareArguments method - Collect all numeric-indexed arguments starting from parameter position - Add proper error handling for variadic parameter processing - Remove TODO comment and complete the implementation - Maintain backward compatibility with existing parameter handling Fixes the TODO item in ReferenceHandler.php line 88 and enables proper handling of functions with variadic parameters in MCP tools and resources.
chr-hertel
left a comment
There was a problem hiding this comment.
Hey @monayemislam, good catch that todo comment - thanks for picking up on that!
I made some smaller comments, but can you share how you tested this?
| $paramName = $parameter->getName(); | ||
| $paramPosition = $parameter->getPosition(); | ||
|
|
||
| // Handle variadic parameters |
There was a problem hiding this comment.
i think this is clear with the next line of code already and we can remove this comment
| // Handle variadic parameters |
| foreach ($arguments as $key => $value) { | ||
| if (is_numeric($key) && $key >= $paramPosition) { |
There was a problem hiding this comment.
this reads like array_slice could be of help 👍
| try { | ||
| $variadicArgs[] = $this->castArgumentType($value, $parameter); | ||
| } catch (InvalidArgumentException $e) { | ||
| throw RegistryException::invalidParams($e->getMessage(), $e); | ||
| } catch (\Throwable $e) { | ||
| throw RegistryException::internalError("Error processing variadic parameter `{$paramName}`: {$e->getMessage()}", $e); | ||
| } |
There was a problem hiding this comment.
this is basically the same like in line 112-118 - does it make sense to shift the throw RegistryException into the castArgumentType method instead and skip the try-catch in both cases?
or somehow merge this block into lower if-else part as well?
|
Hi @monayemislam, do you want to continue here? |
| if ($parameter->isVariadic()) { | ||
| // For variadic parameters, collect all remaining arguments | ||
| $variadicArgs = []; | ||
| foreach ($arguments as $key => $value) { | ||
| if (is_numeric($key) && $key >= $paramPosition) { | ||
| try { | ||
| $variadicArgs[] = $this->castArgumentType($value, $parameter); | ||
| } catch (InvalidArgumentException $e) { | ||
| throw RegistryException::invalidParams($e->getMessage(), $e); | ||
| } catch (\Throwable $e) { | ||
| throw RegistryException::internalError("Error processing variadic parameter `{$paramName}`: {$e->getMessage()}", $e); | ||
| } | ||
| } | ||
| } | ||
| $finalArgs[$paramPosition] = $variadicArgs; | ||
| continue; | ||
| } |
There was a problem hiding this comment.
| if ($parameter->isVariadic()) { | |
| // For variadic parameters, collect all remaining arguments | |
| $variadicArgs = []; | |
| foreach ($arguments as $key => $value) { | |
| if (is_numeric($key) && $key >= $paramPosition) { | |
| try { | |
| $variadicArgs[] = $this->castArgumentType($value, $parameter); | |
| } catch (InvalidArgumentException $e) { | |
| throw RegistryException::invalidParams($e->getMessage(), $e); | |
| } catch (\Throwable $e) { | |
| throw RegistryException::internalError("Error processing variadic parameter `{$paramName}`: {$e->getMessage()}", $e); | |
| } | |
| } | |
| } | |
| $finalArgs[$paramPosition] = $variadicArgs; | |
| continue; | |
| } | |
| if ($parameter->isVariadic()) { | |
| return $arguments; | |
| } |
This is sufficient for a use case where the handler has ...$arguments:
$definition['handler'] = fn (...$arguments) => ($this->operationHandler)($mcpName, $arguments);
Could you enlighten me with your use case where you gather the type of the variadic argument? What's the use case?
|
Closing here for now - please feel free to reopen and continue the work if it still matters to you. |
Fixes the TODO item in ReferenceHandler.php line 88 and enables proper handling of functions with variadic parameters in MCP tools and resources.