Skip to content

Commit ee4e58f

Browse files
committed
Fixed missing string representations of client and transports
1 parent 636915c commit ee4e58f

5 files changed

Lines changed: 81 additions & 4 deletions

File tree

src/main/php/io/modelcontextprotocol/McpClient.class.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace io\modelcontextprotocol;
22

3+
use lang\Value;
34
use util\log\Traceable;
45

56
/**
@@ -8,7 +9,7 @@
89
* @see https://deadprogrammersociety.com/2025/03/calling-mcp-servers-the-hard-way.html
910
* @test io.modelcontextprotocol.unittest.McpClientTest
1011
*/
11-
class McpClient implements Traceable {
12+
class McpClient implements Traceable, Value {
1213
private $transport, $version, $capabilities;
1314
private $server= null;
1415

@@ -92,4 +93,24 @@ public function close() {
9293
$this->transport->close();
9394
$this->server= null;
9495
}
96+
97+
/** @return string */
98+
public function hashCode() {
99+
return 'M'.$this->version.':'.$this->transport->hashCode();
100+
}
101+
102+
/** @return string */
103+
public function toString() {
104+
return nameof($this).'(->'.$this->transport->toString().', version= '.$this->version.')';
105+
}
106+
107+
/**
108+
* Comparison
109+
*
110+
* @param var $value
111+
* @return int
112+
*/
113+
public function compareTo($value) {
114+
return $value instanceof self ? $this->hashCode() <=> $value->hashCode() : 1;
115+
}
95116
}

src/main/php/io/modelcontextprotocol/StdIo.class.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,24 @@ public function close() {
9494
$this->process->close();
9595
}
9696
}
97+
98+
/** @return string */
99+
public function hashCode() {
100+
return 'I'.crc32($this->process->getCommandLine());
101+
}
102+
103+
/** @return string */
104+
public function toString() {
105+
return nameof($this).'<'.$this->process->getCommandLine().'>';
106+
}
107+
108+
/**
109+
* Comparison
110+
*
111+
* @param var $value
112+
* @return int
113+
*/
114+
public function compareTo($value) {
115+
return $value instanceof self ? $this->process <=> $value->process : 1;
116+
}
97117
}

src/main/php/io/modelcontextprotocol/StreamableHttp.class.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php namespace io\modelcontextprotocol;
22

33
use lang\FormatException;
4-
use webservices\rest\Endpoint;
54
use util\URI;
5+
use webservices\rest\Endpoint;
66

77
/**
88
* Streamable HTTP MCP transport
@@ -125,4 +125,24 @@ public function close() {
125125
$this->endpoint->resource($this->path)->delete();
126126
$this->endpoint->with(self::SESSION, null);
127127
}
128+
129+
/** @return string */
130+
public function hashCode() {
131+
return 'H'.$this->endpoint->base()->hashCode();
132+
}
133+
134+
/** @return string */
135+
public function toString() {
136+
return nameof($this).'<'.$this->endpoint->base().'>';
137+
}
138+
139+
/**
140+
* Comparison
141+
*
142+
* @param var $value
143+
* @return int
144+
*/
145+
public function compareTo($value) {
146+
return $value instanceof self ? $this->endpoint->compareTo($value->endpoint) : 1;
147+
}
128148
}

src/main/php/io/modelcontextprotocol/Transport.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php namespace io\modelcontextprotocol;
22

3-
use lang\{Closeable, CommandLine, IllegalArgumentException};
3+
use lang\{Closeable, CommandLine, IllegalArgumentException, Value};
44
use util\URI;
55
use util\log\Traceable;
66

77
/** @test io.modelcontextprotocol.unittest.TransportTest */
8-
abstract class Transport implements Closeable, Traceable {
8+
abstract class Transport implements Closeable, Traceable, Value {
99

1010
/** @param string $version */
1111
public abstract function version($version);

src/test/php/io/modelcontextprotocol/unittest/McpClientTest.class.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ public function call($method, $params= null) {
6464
}
6565

6666
public function close() { }
67+
68+
public function hashCode() { return spl_object_id($this); }
69+
70+
public function toString() { return nameof($this); }
71+
72+
public function compareTo($value) {
73+
return $value instanceof self ? $this <=> $value : 1;
74+
}
6775
};
6876
$fixture= new McpClient($transport);
6977

@@ -87,4 +95,12 @@ public function close() { }
8795
$transport->sent
8896
);
8997
}
98+
99+
#[Test]
100+
public function string_representation() {
101+
Assert::equals(
102+
'io.modelcontextprotocol.McpClient(->io.modelcontextprotocol.StreamableHttp<http://localhost/>, version= 2025-06-18)',
103+
(new McpClient(self::ENDPOINT))->toString()
104+
);
105+
}
90106
}

0 commit comments

Comments
 (0)