Adopt server examples
This commit is contained in:
parent
da0d5a87a0
commit
2ef051a355
4 changed files with 3639 additions and 756 deletions
55
examples/09-standalone-cli/src/Builder.php
Normal file
55
examples/09-standalone-cli/src/Builder.php
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
<?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 App;
|
||||
|
||||
use Mcp\Capability\PromptChain;
|
||||
use Mcp\Capability\ResourceChain;
|
||||
use Mcp\Capability\ToolChain;
|
||||
use Mcp\Server\MethodHandlerInterface;
|
||||
use Mcp\Server\NotificationHandler;
|
||||
use Mcp\Server\RequestHandler;
|
||||
|
||||
/**
|
||||
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
||||
*/
|
||||
class Builder
|
||||
{
|
||||
/**
|
||||
* @return list<MethodHandlerInterface>
|
||||
*/
|
||||
public static function buildMethodHandlers(): array
|
||||
{
|
||||
$promptManager = new PromptChain([
|
||||
new ExamplePrompt(),
|
||||
]);
|
||||
|
||||
$resourceManager = new ResourceChain([
|
||||
new ExampleResource(),
|
||||
]);
|
||||
|
||||
$toolManager = new ToolChain([
|
||||
new ExampleTool(),
|
||||
]);
|
||||
|
||||
return [
|
||||
new NotificationHandler\InitializedHandler(),
|
||||
new RequestHandler\InitializeHandler(),
|
||||
new RequestHandler\PingHandler(),
|
||||
new RequestHandler\ListPromptsHandler($promptManager),
|
||||
new RequestHandler\GetPromptHandler($promptManager),
|
||||
new RequestHandler\ListResourcesHandler($resourceManager),
|
||||
new RequestHandler\ReadResourceHandler($resourceManager),
|
||||
new RequestHandler\CallToolHandler($toolManager),
|
||||
new RequestHandler\ListToolsHandler($toolManager),
|
||||
];
|
||||
}
|
||||
}
|
||||
60
examples/09-standalone-cli/src/ExamplePrompt.php
Normal file
60
examples/09-standalone-cli/src/ExamplePrompt.php
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
<?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 App;
|
||||
|
||||
use Mcp\Capability\Prompt\MetadataInterface;
|
||||
use Mcp\Capability\Prompt\PromptGetterInterface;
|
||||
use Mcp\Schema\Content\PromptMessage;
|
||||
use Mcp\Schema\Content\TextContent;
|
||||
use Mcp\Schema\Enum\Role;
|
||||
use Mcp\Schema\Request\GetPromptRequest;
|
||||
use Mcp\Schema\Result\GetPromptResult;
|
||||
|
||||
/**
|
||||
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
||||
*/
|
||||
class ExamplePrompt implements MetadataInterface, PromptGetterInterface
|
||||
{
|
||||
public function get(GetPromptRequest $request): GetPromptResult
|
||||
{
|
||||
$firstName = $request->arguments['firstName'] ?? null;
|
||||
|
||||
return new GetPromptResult(
|
||||
[new PromptMessage(
|
||||
Role::User,
|
||||
new TextContent(\sprintf('Hello %s', $firstName ?? 'World')),
|
||||
)],
|
||||
$this->getDescription(),
|
||||
);
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'Greet';
|
||||
}
|
||||
|
||||
public function getDescription(): ?string
|
||||
{
|
||||
return 'Greet a person with a nice message';
|
||||
}
|
||||
|
||||
public function getArguments(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'name' => 'first name',
|
||||
'description' => 'The name of the person to greet',
|
||||
'required' => false,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
56
examples/09-standalone-cli/src/ExampleResource.php
Normal file
56
examples/09-standalone-cli/src/ExampleResource.php
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
<?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 App;
|
||||
|
||||
use Mcp\Capability\Resource\MetadataInterface;
|
||||
use Mcp\Capability\Resource\ResourceReaderInterface;
|
||||
use Mcp\Schema\Content\TextResourceContents;
|
||||
use Mcp\Schema\Request\ReadResourceRequest;
|
||||
use Mcp\Schema\Result\ReadResourceResult;
|
||||
|
||||
/**
|
||||
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
||||
*/
|
||||
class ExampleResource implements MetadataInterface, ResourceReaderInterface
|
||||
{
|
||||
public function read(ReadResourceRequest $request): ReadResourceResult
|
||||
{
|
||||
return new ReadResourceResult([
|
||||
new TextResourceContents($this->getUri(), null, 'Content of My Resource'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function getUri(): string
|
||||
{
|
||||
return 'file:///project/src/main.rs';
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'my-resource';
|
||||
}
|
||||
|
||||
public function getDescription(): ?string
|
||||
{
|
||||
return 'This is just an example';
|
||||
}
|
||||
|
||||
public function getMimeType(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getSize(): ?int
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
60
examples/09-standalone-cli/src/ExampleTool.php
Normal file
60
examples/09-standalone-cli/src/ExampleTool.php
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
<?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 App;
|
||||
|
||||
use Mcp\Capability\Tool\MetadataInterface;
|
||||
use Mcp\Capability\Tool\ToolExecutorInterface;
|
||||
use Mcp\Schema\Content\TextContent;
|
||||
use Mcp\Schema\Request\CallToolRequest;
|
||||
use Mcp\Schema\Result\CallToolResult;
|
||||
|
||||
/**
|
||||
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
||||
*/
|
||||
class ExampleTool implements MetadataInterface, ToolExecutorInterface
|
||||
{
|
||||
public function call(CallToolRequest $request): CallToolResult
|
||||
{
|
||||
$format = $request->arguments['format'] ?? 'Y-m-d H:i:s';
|
||||
|
||||
return new CallToolResult([
|
||||
new TextContent(
|
||||
(new \DateTime('now', new \DateTimeZone('UTC')))->format($format),
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'Current time';
|
||||
}
|
||||
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Returns the current time in UTC';
|
||||
}
|
||||
|
||||
public function getInputSchema(): array
|
||||
{
|
||||
return [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'format' => [
|
||||
'type' => 'string',
|
||||
'description' => 'The format of the time, e.g. "Y-m-d H:i:s"',
|
||||
'default' => 'Y-m-d H:i:s',
|
||||
],
|
||||
],
|
||||
'required' => [],
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue