refactor: transport agnostic examples (#131)

This commit is contained in:
Christopher Hertel 2025-11-02 18:07:43 +01:00 • committed by GitHub
commit ff26b4ff61
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 157 additions and 289 deletions

View file

@ -0,0 +1,51 @@
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Example\EnvVariables;
use Mcp\Capability\Attribute\McpTool;
final class EnvToolHandler
{
/**
* Performs an action that can be modified by an environment variable.
* The MCP client should set 'APP_MODE' in its 'env' config for this server.
*
* @param string $input some input data
*
* @return array<string, string|int> the result, varying by APP_MODE
*/
#[McpTool(name: 'process_data_by_mode')]
public function processData(string $input): array
{
$appMode = getenv('APP_MODE'); // Read from environment
if ('debug' === $appMode) {
return [
'mode' => 'debug',
'processed_input' => strtoupper($input),
'message' => 'Processed in DEBUG mode.',
];
} elseif ('production' === $appMode) {
return [
'mode' => 'production',
'processed_input_length' => \strlen($input),
'message' => 'Processed in PRODUCTION mode (summary only).',
];
} else {
return [
'mode' => $appMode ?: 'default',
'original_input' => $input,
'message' => 'Processed in default mode (APP_MODE not recognized or not set).',
];
}
}
}

View file

@ -0,0 +1,61 @@
#!/usr/bin/env php
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once dirname(__DIR__).'/bootstrap.php';
chdir(__DIR__);
use Mcp\Server;
/*
|--------------------------------------------------------------------------
| MCP Environment Variable Example Server
|--------------------------------------------------------------------------
|
| This server demonstrates how to use environment variables to modify tool
| behavior. The MCP client can set the APP_MODE environment variable to
| control the server's behavior.
|
| Configure your MCP Client (e.g. Cursor) for this server like this:
|
| {
| "mcpServers": {
| "my-php-env-server": {
| "command": "php",
| "args": ["/full/path/to/examples/05-stdio-env-variables/server.php"],
| "env": {
| "APP_MODE": "debug" // or "production", or leave it out
| }
| }
| }
| }
|
| The server will read the APP_MODE environment variable and use it to
| modify the behavior of the tools.
|
| If the APP_MODE environment variable is not set, the server will use the
| default behavior.
|
*/
logger()->info('Starting MCP Environment Variable Example Server...');
$server = Server::builder()
->setServerInfo('Env Var Server', '1.0.0')
->setLogger(logger())
->setDiscovery(__DIR__)
->build();
$result = $server->run(transport());
logger()->info('Server listener stopped gracefully.', ['result' => $result]);
shutdown($result);