php-sdk/examples/server/mcp-apps/server.php
Christopher Hertel 0751a83658
[Examples][Tests] Make the MCP Apps example work over HTTP, and pin it (#439)
* [Examples][Tests] Make the MCP Apps example work over HTTP, and pin it

It was the only example without a session store, so every request under
`php -S` or php-fpm - each its own process - was answered "Session not found
or has expired". It now uses a FileSessionStore like the others.

Inspector snapshots pin what the extension actually puts on the wire: the
ui:// resource's `text/html;profile=mcp-app` MIME type and `ui` marker, and
the `_meta.ui` on the tool that links the two.

* Apply suggestion from @chr-hertel

* Apply suggestion from @chr-hertel
2026-08-18 14:51:34 +02:00

53 lines
1.5 KiB
PHP

#!/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\Example\Server\McpApps\WeatherApp;
use Mcp\Schema\Extension\Apps\McpApps;
use Mcp\Schema\Extension\Apps\ToolVisibility;
use Mcp\Schema\Extension\Apps\UiToolMeta;
use Mcp\Server;
use Mcp\Server\Session\FileSessionStore;
logger()->info('Starting MCP Apps Example Server...');
$server = Server::builder()
->setServerInfo('MCP Apps Weather Example', '1.0.0')
->setLogger(logger())
->setSession(new FileSessionStore(__DIR__.'/sessions'))
->enableExtension(new McpApps())
->addResource(
[WeatherApp::class, 'getWeatherApp'],
'ui://weather-app',
'weather-app',
description: 'Interactive weather dashboard',
mimeType: McpApps::MIME_TYPE,
meta: ['ui' => McpApps::resourceMarker()],
)
->addTool(
[WeatherApp::class, 'getWeather'],
'get_weather',
description: 'Get current weather for a city',
meta: ['ui' => new UiToolMeta(
resourceUri: 'ui://weather-app',
visibility: [ToolVisibility::Model, ToolVisibility::App],
)],
)
->build();
$result = $server->run(transport());
logger()->info('Server stopped gracefully.', ['result' => $result]);
shutdown($result);