-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathStreamableHttpTestServer.php
More file actions
executable file
·63 lines (52 loc) · 2.5 KB
/
StreamableHttpTestServer.php
File metadata and controls
executable file
·63 lines (52 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env php
<?php
declare(strict_types=1);
require_once __DIR__ . '/../../../vendor/autoload.php';
use PhpMcp\Server\Server;
use PhpMcp\Server\Transports\StreamableHttpServerTransport;
use PhpMcp\Server\Tests\Fixtures\General\ToolHandlerFixture;
use PhpMcp\Server\Tests\Fixtures\General\ResourceHandlerFixture;
use PhpMcp\Server\Tests\Fixtures\General\PromptHandlerFixture;
use PhpMcp\Server\Defaults\InMemoryEventStore;
use Psr\Log\AbstractLogger;
use Psr\Log\NullLogger;
class StdErrLogger extends AbstractLogger
{
public function log($level, \Stringable|string $message, array $context = []): void
{
fwrite(STDERR, sprintf("[%s] SERVER_LOG: %s %s\n", strtoupper((string)$level), $message, empty($context) ? '' : json_encode($context)));
}
}
$host = $argv[1] ?? '127.0.0.1';
$port = (int)($argv[2] ?? 8992);
$mcpPath = $argv[3] ?? 'mcp_streamable_test';
$enableJsonResponse = filter_var($argv[4] ?? 'true', FILTER_VALIDATE_BOOLEAN);
$useEventStore = filter_var($argv[5] ?? 'false', FILTER_VALIDATE_BOOLEAN);
$stateless = filter_var($argv[6] ?? 'false', FILTER_VALIDATE_BOOLEAN);
try {
$logger = new NullLogger();
$logger->info("Starting StreamableHttpTestServer on {$host}:{$port}/{$mcpPath}, JSON Mode: " . ($enableJsonResponse ? 'ON' : 'OFF') . ", Stateless: " . ($stateless ? 'ON' : 'OFF'));
$eventStore = $useEventStore ? new InMemoryEventStore() : null;
$server = Server::make()
->withServerInfo('StreamableHttpIntegrationServer', '0.1.0')
->withLogger($logger)
->withTool([ToolHandlerFixture::class, 'greet'], 'greet_streamable_tool')
->withTool([ToolHandlerFixture::class, 'sum'], 'sum_streamable_tool') // For batch testing
->withResource([ResourceHandlerFixture::class, 'getStaticText'], "test://streamable/static", 'static_streamable_resource')
->withPrompt([PromptHandlerFixture::class, 'generateSimpleGreeting'], 'simple_streamable_prompt')
->build();
$transport = new StreamableHttpServerTransport(
host: $host,
port: $port,
mcpPath: $mcpPath,
enableJsonResponse: $enableJsonResponse,
stateless: $stateless,
eventStore: $eventStore
);
$server->listen($transport);
$logger->info("StreamableHttpTestServer listener stopped on {$host}:{$port}.");
exit(0);
} catch (\Throwable $e) {
fwrite(STDERR, "[STREAMABLE_HTTP_SERVER_CRITICAL_ERROR]\nHost:{$host} Port:{$port} Prefix:{$mcpPath}\n" . $e->getMessage() . "\n" . $e->getTraceAsString() . "\n");
exit(1);
}