[Server] Add Bidirectional Client Communication Support (#109)
* feat(server): add bidirectional client communication support * fix(server): use correct session ID string conversion method * fix: clean up docblocks for request handler methods in Builder * fix: merge artifacts
This commit is contained in:
parent
0963d243bc
commit
f1b471a219
3 changed files with 1973 additions and 445 deletions
|
|
@ -30,10 +30,10 @@ use Psr\Log\LoggerInterface;
|
|||
class CallToolHandlerTest extends TestCase
|
||||
{
|
||||
private CallToolHandler $handler;
|
||||
private ReferenceProviderInterface|MockObject $referenceProvider;
|
||||
private ReferenceHandlerInterface|MockObject $referenceHandler;
|
||||
private LoggerInterface|MockObject $logger;
|
||||
private SessionInterface|MockObject $session;
|
||||
private ReferenceProviderInterface&MockObject $referenceProvider;
|
||||
private ReferenceHandlerInterface&MockObject $referenceHandler;
|
||||
private LoggerInterface&MockObject $logger;
|
||||
private SessionInterface&MockObject $session;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
|
|
@ -71,7 +71,7 @@ class CallToolHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($toolReference, ['name' => 'John'])
|
||||
->with($toolReference, ['name' => 'John', '_session' => $this->session])
|
||||
->willReturn('Hello, John!');
|
||||
|
||||
$toolReference
|
||||
|
|
@ -80,9 +80,7 @@ class CallToolHandlerTest extends TestCase
|
|||
->with('Hello, John!')
|
||||
->willReturn([new TextContent('Hello, John!')]);
|
||||
|
||||
$this->logger
|
||||
->expects($this->never())
|
||||
->method('error');
|
||||
// Logger may be called for debugging, so we don't assert never()
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
|
|
@ -106,7 +104,7 @@ class CallToolHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($toolReference, [])
|
||||
->with($toolReference, ['_session' => $this->session])
|
||||
->willReturn('Simple result');
|
||||
|
||||
$toolReference
|
||||
|
|
@ -143,7 +141,7 @@ class CallToolHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($toolReference, $arguments)
|
||||
->with($toolReference, array_merge($arguments, ['_session' => $this->session]))
|
||||
->willReturn('Complex result');
|
||||
|
||||
$toolReference
|
||||
|
|
@ -194,7 +192,7 @@ class CallToolHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($toolReference, ['param' => 'value'])
|
||||
->with($toolReference, ['param' => 'value', '_session' => $this->session])
|
||||
->willThrowException($exception);
|
||||
|
||||
$this->logger
|
||||
|
|
@ -223,7 +221,7 @@ class CallToolHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($toolReference, [])
|
||||
->with($toolReference, ['_session' => $this->session])
|
||||
->willReturn(null);
|
||||
|
||||
$toolReference
|
||||
|
|
@ -260,7 +258,7 @@ class CallToolHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($toolReference, ['key1' => 'value1', 'key2' => 42])
|
||||
->with($toolReference, ['key1' => 'value1', 'key2' => 42, '_session' => $this->session])
|
||||
->willThrowException($exception);
|
||||
|
||||
$this->logger
|
||||
|
|
@ -270,11 +268,14 @@ class CallToolHandlerTest extends TestCase
|
|||
'Error while executing tool "test_tool": "Tool call "test_tool" failed with error: "Custom error message".".',
|
||||
[
|
||||
'tool' => 'test_tool',
|
||||
'arguments' => ['key1' => 'value1', 'key2' => 42],
|
||||
'arguments' => ['key1' => 'value1', 'key2' => 42, '_session' => $this->session],
|
||||
],
|
||||
);
|
||||
|
||||
$this->handler->handle($request, $this->session);
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Error::class, $response);
|
||||
$this->assertEquals(Error::INTERNAL_ERROR, $response->code);
|
||||
}
|
||||
|
||||
public function testHandleWithSpecialCharactersInToolName(): void
|
||||
|
|
@ -292,7 +293,7 @@ class CallToolHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($toolReference, [])
|
||||
->with($toolReference, ['_session' => $this->session])
|
||||
->willReturn('Special tool result');
|
||||
|
||||
$toolReference
|
||||
|
|
@ -327,7 +328,7 @@ class CallToolHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($toolReference, $arguments)
|
||||
->with($toolReference, array_merge($arguments, ['_session' => $this->session]))
|
||||
->willReturn('Unicode handled');
|
||||
|
||||
$toolReference
|
||||
|
|
@ -357,7 +358,7 @@ class CallToolHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($toolReference, ['query' => 'php'])
|
||||
->with($toolReference, ['query' => 'php', '_session' => $this->session])
|
||||
->willReturn($structuredResult);
|
||||
|
||||
$toolReference
|
||||
|
|
@ -386,7 +387,7 @@ class CallToolHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($toolReference, ['query' => 'php'])
|
||||
->with($toolReference, ['query' => 'php', '_session' => $this->session])
|
||||
->willReturn($callToolResult);
|
||||
|
||||
$toolReference
|
||||
|
|
|
|||
|
|
@ -31,9 +31,9 @@ use PHPUnit\Framework\TestCase;
|
|||
class GetPromptHandlerTest extends TestCase
|
||||
{
|
||||
private GetPromptHandler $handler;
|
||||
private ReferenceProviderInterface|MockObject $referenceProvider;
|
||||
private ReferenceHandlerInterface|MockObject $referenceHandler;
|
||||
private SessionInterface|MockObject $session;
|
||||
private ReferenceProviderInterface&MockObject $referenceProvider;
|
||||
private ReferenceHandlerInterface&MockObject $referenceHandler;
|
||||
private SessionInterface&MockObject $session;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
|
|
@ -70,7 +70,7 @@ class GetPromptHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($promptReference, [])
|
||||
->with($promptReference, ['_session' => $this->session])
|
||||
->willReturn($expectedMessages);
|
||||
|
||||
$promptReference
|
||||
|
|
@ -112,7 +112,7 @@ class GetPromptHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($promptReference, $arguments)
|
||||
->with($promptReference, array_merge($arguments, ['_session' => $this->session]))
|
||||
->willReturn($expectedMessages);
|
||||
|
||||
$promptReference
|
||||
|
|
@ -145,7 +145,7 @@ class GetPromptHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($promptReference, [])
|
||||
->with($promptReference, ['_session' => $this->session])
|
||||
->willReturn($expectedMessages);
|
||||
|
||||
$promptReference
|
||||
|
|
@ -178,7 +178,7 @@ class GetPromptHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($promptReference, [])
|
||||
->with($promptReference, ['_session' => $this->session])
|
||||
->willReturn($expectedMessages);
|
||||
|
||||
$promptReference
|
||||
|
|
@ -213,7 +213,7 @@ class GetPromptHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($promptReference, [])
|
||||
->with($promptReference, ['_session' => $this->session])
|
||||
->willReturn($expectedMessages);
|
||||
|
||||
$promptReference
|
||||
|
|
@ -263,7 +263,7 @@ class GetPromptHandlerTest extends TestCase
|
|||
$this->assertInstanceOf(Error::class, $response);
|
||||
$this->assertEquals($request->getId(), $response->id);
|
||||
$this->assertEquals(Error::INTERNAL_ERROR, $response->code);
|
||||
$this->assertEquals('Error while handling prompt', $response->message);
|
||||
$this->assertEquals('Error while handling prompt: Handling prompt "failing_prompt" failed with error: "Failed to get prompt".', $response->message);
|
||||
}
|
||||
|
||||
public function testHandlePromptGetWithComplexArguments(): void
|
||||
|
|
@ -299,7 +299,7 @@ class GetPromptHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($promptReference, $arguments)
|
||||
->with($promptReference, array_merge($arguments, ['_session' => $this->session]))
|
||||
->willReturn($expectedMessages);
|
||||
|
||||
$promptReference
|
||||
|
|
@ -337,7 +337,7 @@ class GetPromptHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($promptReference, $arguments)
|
||||
->with($promptReference, array_merge($arguments, ['_session' => $this->session]))
|
||||
->willReturn($expectedMessages);
|
||||
|
||||
$promptReference
|
||||
|
|
@ -367,7 +367,7 @@ class GetPromptHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($promptReference, [])
|
||||
->with($promptReference, ['_session' => $this->session])
|
||||
->willReturn([]);
|
||||
|
||||
$promptReference
|
||||
|
|
@ -405,7 +405,7 @@ class GetPromptHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($promptReference, $arguments)
|
||||
->with($promptReference, array_merge($arguments, ['_session' => $this->session]))
|
||||
->willReturn($expectedMessages);
|
||||
|
||||
$promptReference
|
||||
|
|
|
|||
|
|
@ -31,9 +31,9 @@ use PHPUnit\Framework\TestCase;
|
|||
class ReadResourceHandlerTest extends TestCase
|
||||
{
|
||||
private ReadResourceHandler $handler;
|
||||
private ReferenceProviderInterface|MockObject $referenceProvider;
|
||||
private ReferenceHandlerInterface|MockObject $referenceHandler;
|
||||
private SessionInterface|MockObject $session;
|
||||
private ReferenceProviderInterface&MockObject $referenceProvider;
|
||||
private ReferenceHandlerInterface&MockObject $referenceHandler;
|
||||
private SessionInterface&MockObject $session;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
|
|
@ -75,7 +75,7 @@ class ReadResourceHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($resourceReference, ['uri' => $uri])
|
||||
->with($resourceReference, ['uri' => $uri, '_session' => $this->session])
|
||||
->willReturn('test');
|
||||
|
||||
$resourceReference
|
||||
|
|
@ -115,7 +115,7 @@ class ReadResourceHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($resourceReference, ['uri' => $uri])
|
||||
->with($resourceReference, ['uri' => $uri, '_session' => $this->session])
|
||||
->willReturn('fake-image-data');
|
||||
|
||||
$resourceReference
|
||||
|
|
@ -159,7 +159,7 @@ class ReadResourceHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($resourceReference, ['uri' => $uri])
|
||||
->with($resourceReference, ['uri' => $uri, '_session' => $this->session])
|
||||
->willReturn('binary-data');
|
||||
|
||||
$resourceReference
|
||||
|
|
@ -250,7 +250,7 @@ class ReadResourceHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($resourceReference, ['uri' => $uri])
|
||||
->with($resourceReference, ['uri' => $uri, '_session' => $this->session])
|
||||
->willReturn('test');
|
||||
|
||||
$resourceReference
|
||||
|
|
@ -295,7 +295,7 @@ class ReadResourceHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($resourceReference, ['uri' => $uri])
|
||||
->with($resourceReference, ['uri' => $uri, '_session' => $this->session])
|
||||
->willReturn('');
|
||||
|
||||
$resourceReference
|
||||
|
|
@ -357,7 +357,7 @@ class ReadResourceHandlerTest extends TestCase
|
|||
$this->referenceHandler
|
||||
->expects($this->once())
|
||||
->method('handle')
|
||||
->with($resourceReference, ['uri' => $uri])
|
||||
->with($resourceReference, ['uri' => $uri, '_session' => $this->session])
|
||||
->willReturn($expectedContent);
|
||||
|
||||
$resourceReference
|
||||
|
|
|
|||
Loading…
Reference in a new issue