Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"phpunit/phpunit": "^10.5",
"symfony/console": "^6.4 || ^7.0",
"psr/cache": "^3.0",
"php-cs-fixer/shim": "^3.84"
"php-cs-fixer/shim": "^3.84",
"nyholm/nsa": "^1.3"
},
"suggest": {
"symfony/console": "To use SymfonyConsoleTransport for STDIO",
Expand Down
7 changes: 3 additions & 4 deletions examples/cli/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
$logger = new SymfonyConsole\Logger\ConsoleLogger($output);

// Configure the JsonRpcHandler and build the functionality
$jsonRpcHandler = new Mcp\Server\JsonRpcHandler(
new Mcp\Message\Factory(),
App\Builder::buildRequestHandlers(),
App\Builder::buildNotificationHandlers(),
$jsonRpcHandler = new Mcp\JsonRpc\Handler(
Mcp\JsonRpc\MessageFactory::make(),
App\Builder::buildMethodHandlers(),
$logger
);

Expand Down
45 changes: 14 additions & 31 deletions examples/cli/src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,19 @@
use Mcp\Capability\PromptChain;
use Mcp\Capability\ResourceChain;
use Mcp\Capability\ToolChain;
use Mcp\Server\NotificationHandler\InitializedHandler;
use Mcp\Server\NotificationHandlerInterface;
use Mcp\Server\RequestHandler\InitializeHandler;
use Mcp\Server\RequestHandler\PingHandler;
use Mcp\Server\RequestHandler\PromptGetHandler;
use Mcp\Server\RequestHandler\PromptListHandler;
use Mcp\Server\RequestHandler\ResourceListHandler;
use Mcp\Server\RequestHandler\ResourceReadHandler;
use Mcp\Server\RequestHandler\ToolCallHandler;
use Mcp\Server\RequestHandler\ToolListHandler;
use Mcp\Server\RequestHandlerInterface;
use Mcp\Server\MethodHandlerInterface;
use Mcp\Server\NotificationHandler;
use Mcp\Server\RequestHandler;

/**
* @author Tobias Nyholm <[email protected]>
*/
class Builder
{
/**
* @return list<RequestHandlerInterface>
* @return list<MethodHandlerInterface>
*/
public static function buildRequestHandlers(): array
public static function buildMethodHandlers(): array
{
$promptManager = new PromptChain([
new ExamplePrompt(),
Expand All @@ -49,24 +41,15 @@ public static function buildRequestHandlers(): array
]);

return [
new InitializeHandler(),
new PingHandler(),
new PromptListHandler($promptManager),
new PromptGetHandler($promptManager),
new ResourceListHandler($resourceManager),
new ResourceReadHandler($resourceManager),
new ToolCallHandler($toolManager),
new ToolListHandler($toolManager),
];
}

/**
* @return list<NotificationHandlerInterface>
*/
public static function buildNotificationHandlers(): array
{
return [
new InitializedHandler(),
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),
];
}
}
22 changes: 12 additions & 10 deletions examples/cli/src/ExamplePrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,28 @@
namespace App;

use Mcp\Capability\Prompt\MetadataInterface;
use Mcp\Capability\Prompt\PromptGet;
use Mcp\Capability\Prompt\PromptGetResult;
use Mcp\Capability\Prompt\PromptGetResultMessages;
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 <[email protected]>
*/
class ExamplePrompt implements MetadataInterface, PromptGetterInterface
{
public function get(PromptGet $input): PromptGetResult
public function get(GetPromptRequest $request): GetPromptResult
{
$firstName = $input->arguments['first name'] ?? null;
$firstName = $request->arguments['firstName'] ?? null;

return new PromptGetResult(
return new GetPromptResult(
[new PromptMessage(
Role::User,
new TextContent(\sprintf('Hello %s', $firstName ?? 'World')),
)],
$this->getDescription(),
[new PromptGetResultMessages(
'user',
\sprintf('Hello %s', $firstName ?? 'World')
)]
);
}

Expand Down
16 changes: 8 additions & 8 deletions examples/cli/src/ExampleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
namespace App;

use Mcp\Capability\Resource\MetadataInterface;
use Mcp\Capability\Resource\ResourceRead;
use Mcp\Capability\Resource\ResourceReaderInterface;
use Mcp\Capability\Resource\ResourceReadResult;
use Mcp\Schema\Content\TextResourceContents;
use Mcp\Schema\Request\ReadResourceRequest;
use Mcp\Schema\Result\ReadResourceResult;

/**
* @author Tobias Nyholm <[email protected]>
*/
class ExampleResource implements MetadataInterface, ResourceReaderInterface
{
public function read(ResourceRead $input): ResourceReadResult
public function read(ReadResourceRequest $request): ReadResourceResult
{
return new ResourceReadResult(
'Content of '.$this->getName(),
$this->getUri(),
);
return new ReadResourceResult([
new TextResourceContents($this->getUri(), null, 'Content of My Resource'),
]);
}

public function getUri(): string
Expand All @@ -36,7 +36,7 @@ public function getUri(): string

public function getName(): string
{
return 'My resource';
return 'my-resource';
}

public function getDescription(): ?string
Expand Down
17 changes: 10 additions & 7 deletions examples/cli/src/ExampleTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,25 @@
namespace App;

use Mcp\Capability\Tool\MetadataInterface;
use Mcp\Capability\Tool\ToolCall;
use Mcp\Capability\Tool\ToolCallResult;
use Mcp\Capability\Tool\ToolExecutorInterface;
use Mcp\Schema\Content\TextContent;
use Mcp\Schema\Request\CallToolRequest;
use Mcp\Schema\Result\CallToolResult;

/**
* @author Tobias Nyholm <[email protected]>
*/
class ExampleTool implements MetadataInterface, ToolExecutorInterface
{
public function call(ToolCall $input): ToolCallResult
public function call(CallToolRequest $request): CallToolResult
{
$format = $input->arguments['format'] ?? 'Y-m-d H:i:s';
$format = $request->arguments['format'] ?? 'Y-m-d H:i:s';

return new ToolCallResult(
(new \DateTime('now', new \DateTimeZone('UTC')))->format($format)
);
return new CallToolResult([
new TextContent(
(new \DateTime('now', new \DateTimeZone('UTC')))->format($format),
),
]);
}

public function getName(): string
Expand Down
3 changes: 3 additions & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ parameters:
excludePaths:
- examples/cli/vendor/* (?)
treatPhpDocTypesAsCertain: false
ignoreErrors:
-
message: "#^Method .*::test.*\\(\\) has no return type specified\\.$#"
28 changes: 0 additions & 28 deletions src/Capability/Prompt/PromptGet.php

This file was deleted.

27 changes: 0 additions & 27 deletions src/Capability/Prompt/PromptGetResult.php

This file was deleted.

30 changes: 0 additions & 30 deletions src/Capability/Prompt/PromptGetResultMessages.php

This file was deleted.

4 changes: 3 additions & 1 deletion src/Capability/Prompt/PromptGetterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Mcp\Exception\PromptGetException;
use Mcp\Exception\PromptNotFoundException;
use Mcp\Schema\Request\GetPromptRequest;
use Mcp\Schema\Result\GetPromptResult;

/**
* @author Tobias Nyholm <[email protected]>
Expand All @@ -23,5 +25,5 @@ interface PromptGetterInterface
* @throws PromptGetException if the prompt execution fails
* @throws PromptNotFoundException if the prompt is not found
*/
public function get(PromptGet $input): PromptGetResult;
public function get(GetPromptRequest $request): GetPromptResult;
}
14 changes: 7 additions & 7 deletions src/Capability/PromptChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
use Mcp\Capability\Prompt\CollectionInterface;
use Mcp\Capability\Prompt\IdentifierInterface;
use Mcp\Capability\Prompt\MetadataInterface;
use Mcp\Capability\Prompt\PromptGet;
use Mcp\Capability\Prompt\PromptGetResult;
use Mcp\Capability\Prompt\PromptGetterInterface;
use Mcp\Exception\InvalidCursorException;
use Mcp\Exception\PromptGetException;
use Mcp\Exception\PromptNotFoundException;
use Mcp\Schema\Request\GetPromptRequest;
use Mcp\Schema\Result\GetPromptResult;

/**
* A collection of prompts. All prompts need to implement IdentifierInterface.
Expand Down Expand Up @@ -60,18 +60,18 @@ public function getMetadata(int $count, ?string $lastIdentifier = null): iterabl
}
}

public function get(PromptGet $input): PromptGetResult
public function get(GetPromptRequest $request): GetPromptResult
{
foreach ($this->items as $item) {
if ($item instanceof PromptGetterInterface && $input->name === $item->getName()) {
if ($item instanceof PromptGetterInterface && $request->name === $item->getName()) {
try {
return $item->get($input);
return $item->get($request);
} catch (\Throwable $e) {
throw new PromptGetException($input, $e);
throw new PromptGetException($request, $e);
}
}
}

throw new PromptNotFoundException($input);
throw new PromptNotFoundException($request);
}
}
4 changes: 3 additions & 1 deletion src/Capability/Resource/ResourceReaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Mcp\Exception\ResourceNotFoundException;
use Mcp\Exception\ResourceReadException;
use Mcp\Schema\Request\ReadResourceRequest;
use Mcp\Schema\Result\ReadResourceResult;

/**
* @author Tobias Nyholm <[email protected]>
Expand All @@ -23,5 +25,5 @@ interface ResourceReaderInterface
* @throws ResourceReadException if the resource execution fails
* @throws ResourceNotFoundException if the resource is not found
*/
public function read(ResourceRead $input): ResourceReadResult;
public function read(ReadResourceRequest $request): ReadResourceResult;
}
Loading