[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
1 changed files with 1973 additions and 445 deletions
|
|
@ -13,19 +13,11 @@
|
|||
require_once dirname(__DIR__).'/bootstrap.php';
|
||||
chdir(__DIR__);
|
||||
|
||||
use Mcp\Schema\Content\TextContent;
|
||||
use Mcp\Schema\JsonRpc\Error;
|
||||
use Mcp\Schema\JsonRpc\Request;
|
||||
use Mcp\Schema\JsonRpc\Response;
|
||||
use Mcp\Schema\Request\CallToolRequest;
|
||||
use Mcp\Schema\Request\ListToolsRequest;
|
||||
use Mcp\Schema\Result\CallToolResult;
|
||||
use Mcp\Schema\Result\ListToolsResult;
|
||||
use Mcp\Example\CustomMethodHandlers\CallToolRequestHandler;
|
||||
use Mcp\Example\CustomMethodHandlers\ListToolsRequestHandler;
|
||||
use Mcp\Schema\ServerCapabilities;
|
||||
use Mcp\Schema\Tool;
|
||||
use Mcp\Server;
|
||||
use Mcp\Server\Handler\Request\RequestHandlerInterface;
|
||||
use Mcp\Server\Session\SessionInterface;
|
||||
use Mcp\Server\Transport\StdioTransport;
|
||||
|
||||
logger()->info('Starting MCP Custom Method Handlers (Stdio) Server...');
|
||||
|
|
@ -58,73 +50,8 @@ $toolDefinitions = [
|
|||
),
|
||||
];
|
||||
|
||||
$listToolsHandler = new class($toolDefinitions) implements RequestHandlerInterface {
|
||||
/**
|
||||
* @param array<string, Tool> $toolDefinitions
|
||||
*/
|
||||
public function __construct(private array $toolDefinitions)
|
||||
{
|
||||
}
|
||||
|
||||
public function supports(Request $request): bool
|
||||
{
|
||||
return $request instanceof ListToolsRequest;
|
||||
}
|
||||
|
||||
public function handle(Request $request, SessionInterface $session): Response
|
||||
{
|
||||
assert($request instanceof ListToolsRequest);
|
||||
|
||||
return new Response($request->getId(), new ListToolsResult(array_values($this->toolDefinitions), null));
|
||||
}
|
||||
};
|
||||
|
||||
$callToolHandler = new class($toolDefinitions) implements RequestHandlerInterface {
|
||||
/**
|
||||
* @param array<string, Tool> $toolDefinitions
|
||||
*/
|
||||
public function __construct(private array $toolDefinitions)
|
||||
{
|
||||
}
|
||||
|
||||
public function supports(Request $request): bool
|
||||
{
|
||||
return $request instanceof CallToolRequest;
|
||||
}
|
||||
|
||||
public function handle(Request $request, SessionInterface $session): Response|Error
|
||||
{
|
||||
assert($request instanceof CallToolRequest);
|
||||
|
||||
$name = $request->name;
|
||||
$args = $request->arguments ?? [];
|
||||
|
||||
if (!isset($this->toolDefinitions[$name])) {
|
||||
return new Error($request->getId(), Error::METHOD_NOT_FOUND, sprintf('Tool not found: %s', $name));
|
||||
}
|
||||
|
||||
try {
|
||||
switch ($name) {
|
||||
case 'say_hello':
|
||||
$greetName = (string) ($args['name'] ?? 'world');
|
||||
$result = [new TextContent(sprintf('Hello, %s!', $greetName))];
|
||||
break;
|
||||
case 'sum':
|
||||
$a = (float) ($args['a'] ?? 0);
|
||||
$b = (float) ($args['b'] ?? 0);
|
||||
$result = [new TextContent((string) ($a + $b))];
|
||||
break;
|
||||
default:
|
||||
$result = [new TextContent('Unknown tool')];
|
||||
}
|
||||
|
||||
return new Response($request->getId(), new CallToolResult($result));
|
||||
} catch (Throwable $e) {
|
||||
return new Response($request->getId(), new CallToolResult([new TextContent('Tool execution failed')], true));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
$listToolsHandler = new ListToolsRequestHandler($toolDefinitions);
|
||||
$callToolHandler = new CallToolRequestHandler($toolDefinitions);
|
||||
$capabilities = new ServerCapabilities(tools: true, resources: false, prompts: false);
|
||||
|
||||
$server = Server::builder()
|
||||
|
|
|
|||
Loading…
Reference in a new issue