Repository navigation
[Server] Answer a JSON POST with only its own responses #508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vbcherepanov
wants to merge
1
commit into
modelcontextprotocol:main
Choose a base branch
from
vbcherepanov:fix/467-json-response-own-messages
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -509,18 +509,63 @@ private function queueOutgoing(Request|Notification|Response|Error $message, arr | |||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Consume (get and clear) all outgoing messages for a session. | ||||||
| * Consume (get and clear) outgoing messages for a session. | ||||||
| * | ||||||
| * With response ids only the matching responses are taken, the rest stays queued. | ||||||
| * A null id matches an error without an id. | ||||||
| * | ||||||
| * @param list<int|string|null>|null $responseIds | ||||||
| * | ||||||
| * @return array<int, array{message: string, context: array<string, mixed>}> | ||||||
| */ | ||||||
| public function consumeOutgoingMessages(Uuid $sessionId): array | ||||||
| public function consumeOutgoingMessages(Uuid $sessionId, ?array $responseIds = null): array | ||||||
| { | ||||||
| $session = $this->sessionManager->createWithId($sessionId); | ||||||
| /** @var array<int, array{message: string, context: array<string, mixed>}> $queue */ | ||||||
| $queue = $session->get(self::SESSION_OUTGOING_QUEUE, []); | ||||||
| $session->set(self::SESSION_OUTGOING_QUEUE, []); | ||||||
| $session->save(); | ||||||
|
|
||||||
| return $queue; | ||||||
| if (null === $responseIds) { | ||||||
| $session->set(self::SESSION_OUTGOING_QUEUE, []); | ||||||
| $session->save(); | ||||||
|
|
||||||
| return $queue; | ||||||
| } | ||||||
|
|
||||||
| $consumed = []; | ||||||
| $remaining = []; | ||||||
| foreach ($queue as $message) { | ||||||
| if (self::isResponseTo($message, $responseIds)) { | ||||||
| $consumed[] = $message; | ||||||
| } else { | ||||||
| $remaining[] = $message; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if ([] !== $consumed) { | ||||||
| $session->set(self::SESSION_OUTGOING_QUEUE, $remaining); | ||||||
| $session->save(); | ||||||
| } | ||||||
|
|
||||||
| return $consumed; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * @param array{message: string, context: array<string, mixed>} $message | ||||||
| * @param list<int|string|null> $responseIds | ||||||
| */ | ||||||
| private static function isResponseTo(array $message, array $responseIds): bool | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| { | ||||||
| if ('response' !== ($message['context']['type'] ?? null)) { | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| try { | ||||||
| $decoded = json_decode($message['message'], true, flags: \JSON_THROW_ON_ERROR); | ||||||
| } catch (\JsonException) { | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| return \is_array($decoded) && \in_array($decoded['id'] ?? null, $responseIds, true); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -74,6 +74,10 @@ class StreamableHttpTransport extends BaseTransport implements StatelessAwareTra | |||||
| private ?string $immediateResponse = null; | ||||||
| private ?int $immediateStatusCode = null; | ||||||
|
|
||||||
| /** @var list<int|string|null> */ | ||||||
| private array $expectedResponseIds = []; | ||||||
| private bool $batchRequest = false; | ||||||
|
|
||||||
| /** @var list<MiddlewareInterface>|null null until {@see self::listen()} resolves the defaults */ | ||||||
| private ?array $middleware; | ||||||
|
|
||||||
|
|
@@ -186,6 +190,9 @@ protected function handleOptionsRequest(): ResponseInterface | |||||
| */ | ||||||
| protected function handlePostRequest(string $body): ResponseInterface | ||||||
| { | ||||||
| // Concurrent requests of one session share the outgoing queue. | ||||||
| [$this->expectedResponseIds, $this->batchRequest] = self::expectedResponses($body); | ||||||
|
|
||||||
| $this->handleMessage($body, $this->sessionId); | ||||||
|
|
||||||
| // Consume the immediate response exactly once, so a transport instance | ||||||
|
|
@@ -223,15 +230,15 @@ protected function handleDeleteRequest(): ResponseInterface | |||||
|
|
||||||
| protected function createJsonResponse(): ResponseInterface | ||||||
| { | ||||||
| $outgoingMessages = $this->getOutgoingMessages($this->sessionId); | ||||||
| $outgoingMessages = $this->getOutgoingResponses($this->sessionId, $this->expectedResponseIds); | ||||||
|
|
||||||
| if (empty($outgoingMessages)) { | ||||||
| return $this->responseFactory->createResponse(202) | ||||||
| ->withHeader('Content-Type', 'application/json'); | ||||||
| } | ||||||
|
|
||||||
| $messages = array_column($outgoingMessages, 'message'); | ||||||
| $responseBody = 1 === \count($messages) ? $messages[0] : '['.implode(',', $messages).']'; | ||||||
| $responseBody = $this->batchRequest ? '['.implode(',', $messages).']' : $messages[0]; | ||||||
|
|
||||||
| $response = $this->responseFactory->createResponse(200) | ||||||
| ->withHeader('Content-Type', 'application/json') | ||||||
|
|
@@ -461,6 +468,39 @@ private function handleModernRequest(ServerRequestInterface $request, string $bo | |||||
| return $this->responder->respond($this->stateless->handle($body, self::headers($request))); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Ids the body expects responses for (null for a message without a usable id), and whether it is a batch. | ||||||
| * | ||||||
| * @return array{list<int|string|null>, bool} | ||||||
| */ | ||||||
| private static function expectedResponses(string $body): array | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| { | ||||||
| try { | ||||||
| $data = json_decode($body, true, flags: \JSON_THROW_ON_ERROR); | ||||||
| } catch (\JsonException) { | ||||||
| // Parse errors are sent directly, not queued. | ||||||
| return [[], false]; | ||||||
| } | ||||||
|
|
||||||
| if (!\is_array($data) || [] === $data) { | ||||||
| return [[null], false]; | ||||||
| } | ||||||
|
|
||||||
| $batch = array_is_list($data); | ||||||
| $ids = []; | ||||||
|
|
||||||
| foreach ($batch ? $data : [$data] as $message) { | ||||||
| if (\is_array($message) && (isset($message['result']) || isset($message['error']))) { | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| $id = \is_array($message) ? ($message['id'] ?? null) : null; | ||||||
| $ids[] = \is_int($id) || \is_string($id) ? $id : null; | ||||||
| } | ||||||
|
|
||||||
| return [$ids, $batch]; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * @return array<string, string> | ||||||
| */ | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -84,11 +84,12 @@ public function onMessage(callable $listener): void; | |||||
| public function onSessionEnd(callable $listener): void; | ||||||
|
|
||||||
| /** | ||||||
| * Set a provider function to retrieve all queued outgoing messages. | ||||||
| * Set a provider function to retrieve queued outgoing messages. | ||||||
| * | ||||||
| * The transport calls this to retrieve all queued messages for a session. | ||||||
| * The transport calls this to retrieve queued messages for a session. When response ids | ||||||
| * are passed, only the responses to those requests are returned. | ||||||
| * | ||||||
| * @param callable(Uuid $sessionId): array<int, array{message: string, context: array<string, mixed>}> $provider | ||||||
| * @param callable(Uuid $sessionId, list<int|string|null>|null $responseIds=): array<int, array{message: string, context: array<string, mixed>}> $provider | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| */ | ||||||
| public function setOutgoingMessagesProvider(callable $provider): void; | ||||||
|
|
||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this make sense as solution but introduces an issue of potentially piling up
$remainingresponses in the memory, right?