-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path01-simple-stdio-sync.php
More file actions
151 lines (124 loc) · 5.11 KB
/
Copy path01-simple-stdio-sync.php
File metadata and controls
151 lines (124 loc) · 5.11 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
declare(strict_types=1);
// This example demonstrates basic synchronous-style usage
// with a stdio server
require __DIR__.'/../vendor/autoload.php';
use PhpMcp\Client\Client;
use PhpMcp\Client\Enum\TransportType;
use PhpMcp\Client\Exception\McpClientException;
use PhpMcp\Client\Exception\RequestException;
use PhpMcp\Client\Model\Capabilities as ClientCapabilities;
use PhpMcp\Client\Model\Content\EmbeddedResource;
use PhpMcp\Client\Model\Content\TextContent;
use PhpMcp\Client\ServerConfig;
use PhpMcp\Client\StreamLogger;
// --- Configuration ---
$clientName = 'MySimplePHPClient';
$clientVersion = '0.1.0';
$clientCapabilities = ClientCapabilities::forClient(supportsSampling: false);
$logger = new StreamLogger(__DIR__.'/client_stdio.log');
// --- Server Configuration ---
$stdioServerConfig = new ServerConfig(
name: 'local_stdio_server',
transport: TransportType::Stdio,
timeout: 10,
command: 'php',
args: ['server.php'],
workingDir: __DIR__.'/../../server/samples/php_stdio',
);
$stdioClient = Client::make()
->withClientInfo($clientName, $clientVersion)
->withCapabilities($clientCapabilities)
->withLogger($logger)
->withServerConfig($stdioServerConfig)
->build();
try {
echo "Attempting to initialize connection to {$stdioServerConfig->name}...\n";
$stdioClient->initialize();
echo "Connection to {$stdioServerConfig->name} initialized successfully!\n";
echo "Server: {$stdioClient->getServerName()} v{$stdioClient->getServerVersion()}, Protocol: {$stdioClient->getNegotiatedProtocolVersion()}\n";
// 1. List Tools
echo "\n[1] Listing Tools...\n";
$tools = $stdioClient->listTools();
if (empty($tools)) {
echo " No tools found on the server.\n";
} else {
echo " Available Tools:\n";
foreach ($tools as $tool) {
echo " - {$tool->name}".($tool->description ? " ({$tool->description})" : '')."\n";
}
}
// 2. Call a Tool
echo "\n[2] Calling 'greeter' tool...\n";
$toolName = 'greeter';
$arguments = ['name' => 'Kyrian', 'count' => 2];
try {
$callResult = $stdioClient->callTool($toolName, $arguments);
if ($callResult->isSuccess()) {
$textContent = $callResult->content[0] ?? null;
echo " Tool '{$toolName}' Result: ".$textContent->text."\n";
} else {
$errorContent = $callResult->content[0] ?? null;
$errorMessage = ($errorContent instanceof TextContent) ? $errorContent->text : 'Unknown tool error format';
echo " Tool '{$toolName}' reported an error: {$errorMessage}\n";
}
} catch (RequestException $e) {
if ($e->getRpcError()) {
echo " Error calling tool '{$toolName}': Server Error Code {$e->getRpcError()->code} - {$e->getRpcError()->message}\n";
} else {
echo " Error calling tool '{$toolName}': {$e->getMessage()}\n";
}
} catch (McpClientException $e) {
echo " Error calling tool '{$toolName}': {$e->getMessage()}\n";
}
// 3. List Resources
echo "\n[3] Listing Resources...\n";
$resources = $stdioClient->listResources();
if (empty($resources)) {
echo " No resources found on the server.\n";
} else {
echo " Available Resources:\n";
foreach ($resources as $resource) {
echo " - {$resource->uri}".($resource->name ? " (Name: {$resource->name})" : '')."\n";
}
}
// 4. Read a Resource
$resourceUri = 'user://data';
echo "\n[4] Reading resource '{$resourceUri}'...\n";
try {
$readResult = $stdioClient->readResource($resourceUri);
$resourceContent = $readResult->contents[0] ?? null;
if ($resourceContent instanceof EmbeddedResource) {
echo " Resource MIME Type: {$resourceContent->mimeType}\n";
echo ' Resource Content: '.($resourceContent->text ?? '[Binary Data]')."\n";
} else {
echo " Resource '{$resourceUri}' not found, empty, or invalid format.\n";
}
} catch (RequestException $e) {
if ($e->getRpcError()) {
echo " Error reading resource '{$resourceUri}': Server Error Code {$e->getRpcError()->code} - {$e->getRpcError()->message}\n";
} else {
echo " Error reading resource '{$resourceUri}': {$e->getMessage()}\n";
}
} catch (McpClientException $e) {
echo " Error reading resource '{$resourceUri}': {$e->getMessage()}\n";
}
// 5. Ping the server
echo "\n[5] Pinging server...\n";
try {
$stdioClient->ping();
echo " Ping successful!\n";
} catch (McpClientException $e) {
echo " Ping failed: {$e->getMessage()}\n";
}
} catch (McpClientException $e) {
echo "\n MCP Client Error: ".get_class($e).' - '.$e->getMessage()."\n";
} catch (\Throwable $e) {
echo "\n Unexpected Error: ".get_class($e).' - '.$e->getMessage()."\n";
echo $e->getTraceAsString()."\n";
} finally {
// IMPORTANT: Always disconnect when finished
echo "\nDisconnecting from server '{$stdioServerConfig->name}'...\n";
$stdioClient->disconnect();
echo "Disconnected.\n";
}