Skip to content

Commit ecceaf4

Browse files
committed
Remove ClientAwareInterface
1 parent 6866365 commit ecceaf4

File tree

6 files changed

+17
-137
lines changed

6 files changed

+17
-137
lines changed

docs/client-communication.md

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,21 @@ MCP supports various ways a server can communicate back to a server on top of th
1313
## ClientGateway
1414

1515
Every communication back to client is handled using the `Mcp\Server\ClientGateway` and its dedicated methods per
16-
operation. To use the `ClientGateway` in your code, there are two ways to do so:
16+
operation. To use the `ClientGateway` in your code, you need to use method argument injection for `RequestContext`.
1717

18-
### 1. Method Argument Injection
19-
20-
Every refernce of a MCP element, that translates to an actual method call, can just add an type-hinted argument for the
21-
`ClientGateway` and the SDK will take care to include the gateway in the arguments of the method call:
18+
Every reference of a MCP element, that translates to an actual method call, can just add an type-hinted argument for the
19+
`RequestContext` and the SDK will take care to include the gateway in the arguments of the method call:
2220

2321
```php
2422
use Mcp\Capability\Attribute\McpTool;
25-
use Mcp\Server\ClientGateway;
23+
use Mcp\Server\RequestContext;
2624

2725
class MyService
2826
{
2927
#[McpTool('my_tool', 'My Tool Description')]
30-
public function myTool(ClientGateway $client): string
31-
{
32-
$client->log(...);
33-
```
34-
35-
### 2. Implementing `ClientAwareInterface`
36-
37-
Whenever a service class of an MCP element implements the interface `Mcp\Server\ClientAwareInterface` the `setClient`
38-
method of that class will get called while handling the reference, and in combination with `Mcp\Server\ClientAwareTrait`
39-
this ends up with code like this:
40-
41-
```php
42-
use Mcp\Capability\Attribute\McpTool;
43-
use Mcp\Server\ClientAwareInterface;
44-
use Mcp\Server\ClientAwareTrait;
45-
46-
class MyService implements ClientAwareInterface
47-
{
48-
use ClientAwareTrait;
49-
50-
#[McpTool('my_tool', 'My Tool Description')]
51-
public function myTool(): string
28+
public function myTool(RequestContext $context): string
5229
{
53-
$this->log(...);
30+
$context->getClientGateway()->log(...);
5431
```
5532

5633
## Sampling

docs/examples.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,10 @@ $server = Server::builder()
166166

167167
**File**: `examples/client-communication/`
168168

169-
**What it demostrates:**
170-
- Server initiated communcation back to the client
169+
**What it demonstrates:**
170+
- Server initiated communication back to the client
171171
- Logging, sampling, progress and notifications
172-
- Using `ClientGateway` in service class via `ClientAwareInterface` and corresponding trait
173-
- Using `ClientGateway` in tool method via method argument injection
172+
- Using `ClientGateway` in tool method via method argument injection of `RequestContext`
174173

175174
### Discovery User Profile
176175

examples/client-communication/ClientAwareService.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@
1414
use Mcp\Capability\Attribute\McpTool;
1515
use Mcp\Schema\Content\TextContent;
1616
use Mcp\Schema\Enum\LoggingLevel;
17-
use Mcp\Server\ClientAwareInterface;
18-
use Mcp\Server\ClientAwareTrait;
17+
use Mcp\Server\RequestContext;
1918
use Psr\Log\LoggerInterface;
2019

21-
final class ClientAwareService implements ClientAwareInterface
20+
final class ClientAwareService
2221
{
23-
use ClientAwareTrait;
24-
2522
public function __construct(
2623
private readonly LoggerInterface $logger,
2724
) {
@@ -32,9 +29,10 @@ public function __construct(
3229
* @return array{incident: string, recommended_actions: string, model: string}
3330
*/
3431
#[McpTool('coordinate_incident_response', 'Coordinate an incident response with logging, progress, and sampling.')]
35-
public function coordinateIncident(string $incidentTitle): array
32+
public function coordinateIncident(RequestContext $context, string $incidentTitle): array
3633
{
37-
$this->log(LoggingLevel::Warning, \sprintf('Incident triage started: %s', $incidentTitle));
34+
$clientGateway = $context->getClientGateway();
35+
$clientGateway->log(LoggingLevel::Warning, \sprintf('Incident triage started: %s', $incidentTitle));
3836

3937
$steps = [
4038
'Collecting telemetry',
@@ -45,7 +43,7 @@ public function coordinateIncident(string $incidentTitle): array
4543
foreach ($steps as $index => $step) {
4644
$progress = ($index + 1) / \count($steps);
4745

48-
$this->progress($progress, 1, $step);
46+
$clientGateway->progress($progress, 1, $step);
4947

5048
usleep(180_000); // Simulate work being done
5149
}
@@ -56,11 +54,11 @@ public function coordinateIncident(string $incidentTitle): array
5654
implode(', ', $steps)
5755
);
5856

59-
$result = $this->sample($prompt, 350, 90, ['temperature' => 0.5]);
57+
$result = $clientGateway->sample($prompt, 350, 90, ['temperature' => 0.5]);
6058

6159
$recommendation = $result->content instanceof TextContent ? trim((string) $result->content->text) : '';
6260

63-
$this->log(LoggingLevel::Info, \sprintf('Incident triage completed for %s', $incidentTitle));
61+
$clientGateway->log(LoggingLevel::Info, \sprintf('Incident triage completed for %s', $incidentTitle));
6462

6563
return [
6664
'incident' => $incidentTitle,

src/Capability/Registry/ReferenceHandler.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Mcp\Exception\InvalidArgumentException;
1515
use Mcp\Exception\RegistryException;
16-
use Mcp\Server\ClientAwareInterface;
1716
use Mcp\Server\ClientGateway;
1817
use Mcp\Server\RequestContext;
1918
use Mcp\Server\Session\SessionInterface;
@@ -42,10 +41,6 @@ public function handle(ElementReference $reference, array $arguments): mixed
4241
$instance = $this->getClassInstance($reference->handler);
4342
$arguments = $this->prepareArguments($reflection, $arguments);
4443

45-
if ($instance instanceof ClientAwareInterface) {
46-
$instance->setClient(new ClientGateway($session));
47-
}
48-
4944
return \call_user_func($instance, ...$arguments);
5045
}
5146

@@ -68,11 +63,6 @@ public function handle(ElementReference $reference, array $arguments): mixed
6863
[$className, $methodName] = $reference->handler;
6964
$reflection = new \ReflectionMethod($className, $methodName);
7065
$instance = $this->getClassInstance($className);
71-
72-
if ($instance instanceof ClientAwareInterface) {
73-
$instance->setClient(new ClientGateway($session));
74-
}
75-
7666
$arguments = $this->prepareArguments($reflection, $arguments);
7767

7868
return \call_user_func([$instance, $methodName], ...$arguments);
@@ -109,12 +99,6 @@ private function prepareArguments(\ReflectionFunctionAbstract $reflection, array
10999
if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) {
110100
$typeName = $type->getName();
111101

112-
if (ClientGateway::class === $typeName && isset($arguments['_session'])) {
113-
// Deprecated, use RequestContext instead
114-
$finalArgs[$paramPosition] = new ClientGateway($arguments['_session']);
115-
continue;
116-
}
117-
118102
if (RequestContext::class === $typeName && isset($arguments['_session'], $arguments['_request'])) {
119103
$finalArgs[$paramPosition] = new RequestContext($arguments['_session'], $arguments['_request']);
120104
continue;
@@ -163,10 +147,6 @@ private function getReflectionForCallable(callable $handler, SessionInterface $s
163147
if (\is_array($handler) && 2 === \count($handler)) {
164148
[$class, $method] = $handler;
165149

166-
if ($class instanceof ClientAwareInterface) {
167-
$class->setClient(new ClientGateway($session));
168-
}
169-
170150
return new \ReflectionMethod($class, $method);
171151
}
172152

src/Server/ClientAwareInterface.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Server/ClientAwareTrait.php

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)