1717use Ecourty \McpServerBundle \Service \InputSanitizer ;
1818use Ecourty \McpServerBundle \Service \PromptRegistry ;
1919use Psr \EventDispatcher \EventDispatcherInterface ;
20- use Symfony \Component \Console \Attribute \Argument ;
2120use Symfony \Component \HttpFoundation \Response ;
2221
2322/**
@@ -61,21 +60,20 @@ public function handle(JsonRpcRequest $request): array
6160
6261 try {
6362 $ arguments = $ request ->params ['arguments ' ] ?? [];
64- $ sanitizedArguments = $ this ->inputSanitizer ->sanitize ($ arguments );
6563
66- $ this ->validatePromptArguments ($ sanitizedArguments , $ promptDefinition );
67- $ argumentCollection = ArgumentCollection:: fromArray ( $ sanitizedArguments );
64+ $ this ->validatePromptArguments ($ arguments , $ promptDefinition );
65+ $ sanitizedArguments = $ this -> getSanitizedArguments ( $ arguments , $ promptDefinition );
6866
6967 if (method_exists ($ prompt , '__invoke ' ) === false ) {
7068 throw new \LogicException (\sprintf ('Prompt "%s" does not implement the __invoke method. ' , $ promptName ));
7169 }
7270
7371 $ this ->eventDispatcher ?->dispatch(new PromptGetEvent (
7472 promptName: $ promptName ,
75- arguments: $ argumentCollection ,
73+ arguments: $ sanitizedArguments ,
7674 ));
7775
78- $ promptResult = $ prompt ->__invoke ($ argumentCollection );
76+ $ promptResult = $ prompt ->__invoke ($ sanitizedArguments );
7977
8078 if ($ promptResult instanceof PromptResult === false ) {
8179 throw new \LogicException (\sprintf (
@@ -85,11 +83,11 @@ public function handle(JsonRpcRequest $request): array
8583 ));
8684 }
8785
88- $ this ->eventDispatcher ?->dispatch(new PromptResultEvent ($ promptName , $ argumentCollection , $ promptResult ));
86+ $ this ->eventDispatcher ?->dispatch(new PromptResultEvent ($ promptName , $ sanitizedArguments , $ promptResult ));
8987 } catch (\Throwable $ exception ) {
9088 $ this ->eventDispatcher ?->dispatch(new PromptExceptionEvent (
9189 promptName: $ promptName ,
92- arguments: $ argumentCollection ?? null ,
90+ arguments: $ sanitizedArguments ?? null ,
9391 exception: $ exception ,
9492 ));
9593
@@ -103,6 +101,33 @@ public function handle(JsonRpcRequest $request): array
103101 return $ promptResult ->toArray ();
104102 }
105103
104+ /**
105+ * @param array<string, mixed> $inputArgument
106+ */
107+ private function getSanitizedArguments (array $ inputArgument , PromptDefinition $ promptDefinition ): ArgumentCollection
108+ {
109+ $ sanitizedArguments = [];
110+ $ promptArguments = $ promptDefinition ->arguments ?? [];
111+
112+ foreach ($ promptArguments as $ argument ) {
113+ $ argumentName = $ argument ->name ;
114+ $ unsafeArgumentValue = $ inputArgument [$ argumentName ] ?? null ;
115+
116+ if ($ argument ->allowUnsafe === true ) {
117+ $ sanitizedArguments [$ argumentName ] = $ unsafeArgumentValue ;
118+
119+ continue ;
120+ }
121+
122+ $ sanitizedArguments [$ argumentName ] = $ this ->inputSanitizer ->sanitize ($ unsafeArgumentValue );
123+ }
124+
125+ return ArgumentCollection::fromArray ($ sanitizedArguments );
126+ }
127+
128+ /**
129+ * @param array<string, mixed> $arguments
130+ */
106131 private function validatePromptArguments (array $ arguments , PromptDefinition $ promptDefinition ): void
107132 {
108133 $ promptArguments = $ promptDefinition ->arguments ?? [];
0 commit comments