|
14 | 14 | use Psr\Log\LoggerInterface; |
15 | 15 | use Symfony\AI\McpSdk\Exception\ExceptionInterface; |
16 | 16 | use Symfony\AI\McpSdk\Exception\HandlerNotFoundException; |
| 17 | +use Symfony\AI\McpSdk\Exception\InvalidInputMessageException; |
17 | 18 | use Symfony\AI\McpSdk\Exception\NotFoundExceptionInterface; |
18 | 19 | use Symfony\AI\McpSdk\Message\Error; |
19 | 20 | use Symfony\AI\McpSdk\Message\Factory; |
@@ -51,41 +52,49 @@ public function __construct( |
51 | 52 | } |
52 | 53 |
|
53 | 54 | /** |
| 55 | + * @return iterable<string|null> |
| 56 | + * |
54 | 57 | * @throws ExceptionInterface |
55 | 58 | * @throws \JsonException |
56 | 59 | */ |
57 | | - public function process(string $message): ?string |
| 60 | + public function process(string $input): iterable |
58 | 61 | { |
59 | | - $this->logger->info('Received message to process', ['message' => $message]); |
| 62 | + $this->logger->info('Received message to process', ['message' => $input]); |
60 | 63 |
|
61 | 64 | try { |
62 | | - $message = $this->messageFactory->create($message); |
| 65 | + $messages = $this->messageFactory->create($input); |
63 | 66 | } catch (\JsonException $e) { |
64 | 67 | $this->logger->warning('Failed to decode json message', ['exception' => $e]); |
65 | 68 |
|
66 | | - return $this->encodeResponse(Error::parseError($e->getMessage())); |
67 | | - } catch (\InvalidArgumentException $e) { |
68 | | - $this->logger->warning('Failed to create message', ['exception' => $e]); |
| 69 | + yield $this->encodeResponse(Error::parseError($e->getMessage())); |
69 | 70 |
|
70 | | - return $this->encodeResponse(Error::invalidRequest(0, $e->getMessage())); |
| 71 | + return; |
71 | 72 | } |
72 | 73 |
|
73 | | - $this->logger->info('Decoded incoming message', ['message' => $message]); |
| 74 | + foreach ($messages as $message) { |
| 75 | + if ($message instanceof InvalidInputMessageException) { |
| 76 | + $this->logger->warning('Failed to create message', ['exception' => $message]); |
| 77 | + yield $this->encodeResponse(Error::invalidRequest(0, $message->getMessage())); |
| 78 | + continue; |
| 79 | + } |
74 | 80 |
|
75 | | - try { |
76 | | - return $message instanceof Notification |
77 | | - ? $this->handleNotification($message) |
78 | | - : $this->encodeResponse($this->handleRequest($message)); |
79 | | - } catch (\DomainException) { |
80 | | - return null; |
81 | | - } catch (NotFoundExceptionInterface $e) { |
82 | | - $this->logger->warning(\sprintf('Failed to create response: %s', $e->getMessage()), ['exception' => $e]); |
| 81 | + $this->logger->info('Decoded incoming message', ['message' => $message]); |
83 | 82 |
|
84 | | - return $this->encodeResponse(Error::methodNotFound($message->id ?? 0, $e->getMessage())); |
85 | | - } catch (\InvalidArgumentException $e) { |
86 | | - $this->logger->warning(\sprintf('Invalid argument: %s', $e->getMessage()), ['exception' => $e]); |
| 83 | + try { |
| 84 | + yield $message instanceof Notification |
| 85 | + ? $this->handleNotification($message) |
| 86 | + : $this->encodeResponse($this->handleRequest($message)); |
| 87 | + } catch (\DomainException) { |
| 88 | + yield null; |
| 89 | + } catch (NotFoundExceptionInterface $e) { |
| 90 | + $this->logger->warning(\sprintf('Failed to create response: %s', $e->getMessage()), ['exception' => $e]); |
87 | 91 |
|
88 | | - return $this->encodeResponse(Error::invalidParams($message->id ?? 0, $e->getMessage())); |
| 92 | + yield $this->encodeResponse(Error::methodNotFound($message->id ?? 0, $e->getMessage())); |
| 93 | + } catch (\InvalidArgumentException $e) { |
| 94 | + $this->logger->warning(\sprintf('Invalid argument: %s', $e->getMessage()), ['exception' => $e]); |
| 95 | + |
| 96 | + yield $this->encodeResponse(Error::invalidParams($message->id ?? 0, $e->getMessage())); |
| 97 | + } |
89 | 98 | } |
90 | 99 | } |
91 | 100 |
|
|
0 commit comments