Skip to content

[Client] Allow deferring and resuming tool input - #769

Open
xixi-box wants to merge 2 commits into
modelcontextprotocol:mainfrom
xixi-box:fix/issue-767-7155eae-07fe8f11
Open

xixi-box wants to merge 2 commits into
modelcontextprotocol:mainfrom
xixi-box:fix/issue-767-7155eae-07fe8f11

Conversation

@xixi-box

Copy link
Copy Markdown
Contributor

Fixes #767

Fixes #767. The client protocol resolves every input-required result inside the request loop, so callers cannot save the pending input and answer it later.

Add Client::callToolRound() for the modern lifecycle. It returns the interim result and accepts keyed answers and opaque state on a later call. callTool() continues to resolve input automatically with its existing signature and return type.

Tests: vendor/bin/phpunit --colors=never --testdox tests/Integration/StatelessClientTest.php --filter testDefersAndResumesToolInput.
Result: vendor/bin/phpunit --colors=never tests/Unit/Client tests/Integration/StatelessClientTest.php tests/Integration/StatelessLifecycleTest.php: passed on base (1450 tests, 5080 assertions)
Result: vendor/bin/phpunit --colors=never --testdox tests/Integration/StatelessClientTest.php --filter testDefersAndResumesToolInput: failed on base 7155eae with exit 1 (1 test, 1 assertion, 1 failure). The test used callTool on base and received CallToolResult after the automatic retry.
Result: vendor/bin/phpunit --colors=never --testdox tests/Integration/StatelessClientTest.php --filter testDefersAndResumesToolInput: passed on patched source with exit 0 (1 test, 16 assertions). The test used callToolRound and checked the real outgoing HTTP bodies.
Result: make conformance-tests: passed with exit 0 after preparing writable fixture directories (80/80 server assertions, 252/252 client assertions, all three client extension scenarios passed).
Result: make conformance-draft: passed with exit 0 (152/152 server assertions, 417/417 client assertions, including sep-2322-client-request-state).
Result: make ci: passed with exit 0 on the final source, before the HTTP capture assertions were added (PHPUnit 4435 tests, 13698 assertions, 8 existing interop skips). PHP CS Fixer, PHPStan and Deptrac also passed.
Result: make cs: passed on the final diff with 0 files fixed.
Result: make phpstan: passed on the final diff with no errors.
Result: vendor/bin/phpunit --colors=never tests/Unit/Client tests/Integration/StatelessClientTest.php tests/Integration/StatelessLifecycleTest.php tests/Integration/IntArgumentTest.php: passed with exit 0 on the final diff (1491 tests, 5154 assertions).
Result: git diff --check: passed.
Regression coverage: tests/Integration/StatelessClientTest.php.

Signed-off-by: wangshun <96341168+xixi-box@users.noreply.github.com>

@soyuka soyuka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tthe approach is fine imo. CI is red because of the phpdoc tag below, and we need a CHANGELOG entry under 0.11.0 ### Client for callToolRound(). Can you also rename the PR to [Client] Allow deferring and resuming tool input?

* a server fault, and answering it would be inventing protocol.
*
* @var array<string, class-string<Request>>
* @var array<string, class-string<ElicitRequest|CreateSamplingMessageRequest|ListRootsRequest>>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpDocumentor doesn't like class-string<A|B|C>, that's what fails make docs-api in qa and Documentation. class-string<ElicitRequest>|class-string<CreateSamplingMessageRequest>|class-string<ListRootsRequest> works, or just keep class-string<Request>.


$state = $result['requestState'] ?? null;

return new InputRequiredResult($requests, \is_string($state) ? $state : null);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the server answers input_required with no requests and no state, this throws a Schema InvalidArgumentException, while the auto path throws a client RuntimeException on server faults. Maybe check it here and throw the same one?

Comment thread src/Client/Protocol.php Outdated
*
* @return Response<array<string, mixed>>|Error
*/
public function requestRound(Request $request, int $timeout, array $round, bool $withProgress = false, ?CancellationTokenInterface $cancellation = null, ?float $callTimeout = null): Response|Error

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a huge fan of request() -> requestWithInput() + requestRound(), an optional ?array $round = null on request() would do the same with less methods, no?


$client = $this->connect(elicitation: true, httpClient: $httpClient);
// @phpstan-ignore function.alreadyNarrowedType (Run the same assertion against SDK versions without callToolRound.)
$callRound = method_exists($client, 'callToolRound') ? $client->callToolRound(...) : $client->callTool(...);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method_exists fallback and its phpstan-ignore can go, it was useful to show red on base but now just call callToolRound directly.

$this->assertNotSame($calls[0]->id, $calls[1]->id);
$this->assertSame($pending->requestState, $calls[1]->params->requestState);
$this->assertSame('Grace', $calls[1]->params->inputResponses->who->content->name);
$this->assertSame('Hello, Ada!', self::text($client->callTool('greet')));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already covered by testMultiRoundTripIsTransparentToTheCaller, can be removed.

Fix the `RESOLVABLE` phpdoc (phpDocumentor rejects `class-string<A|B|C>`),
throw the client `RuntimeException` when an `input_required` result carries
neither input requests nor state, fold `requestRound()` into an optional
`$round` argument of `request()`, add the 0.11.0 Client changelog entry, and
drop the `method_exists` fallback and the redundant auto-path assertion.

Signed-off-by: wangshun <96341168+xixi-box@users.noreply.github.com>
@xixi-box xixi-box changed the title Allow callers to defer and resume MCP tool input [Client] Allow deferring and resuming tool input Nov 26, 2026
@xixi-box

Copy link
Copy Markdown
Contributor Author

Pushed 6e9c8a1 with the review applied:

  • the docblock is now class-string<ElicitRequest>|class-string<CreateSamplingMessageRequest>|class-string<ListRootsRequest>, matching what parseRequest() returns
  • an input_required result with neither input requests nor state throws the client RuntimeException from parseResult(), like the auto path
  • request() takes the optional ?array $round = null and requestWithInput()/requestRound() are gone
  • the callToolRound() entry is added under 0.11.0 ### Client
  • the method_exists fallback and the redundant callTool() assertion are removed

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Client] callTool() cannot defer input_required or send a later retry (MRTR)

2 participants