|
| 1 | +Model Context Protocol SDK |
| 2 | +========================== |
| 3 | + |
| 4 | +The PHP MCP SDK is the low level library that enables communication between a PHP application and an LLM model. |
| 5 | + |
| 6 | +Installation |
| 7 | +------------ |
| 8 | + |
| 9 | +Install the SDK using Composer: |
| 10 | + |
| 11 | +.. code-block:: terminal |
| 12 | +
|
| 13 | + $ composer require mcp/sdk |
| 14 | +
|
| 15 | +Usage |
| 16 | +----- |
| 17 | + |
| 18 | +The `Model Context Protocol`_ is built on top of JSON-RPC. There two types of |
| 19 | +messages. A Notification and Request. The Notification is just a status update |
| 20 | +that something has happened. There is never a response to a Notification. A Request |
| 21 | +is a message that expects a response. There are 3 concepts/capabilities that you |
| 22 | +may use. These are:: |
| 23 | + |
| 24 | +1. **Resources**: File-like data that can be read by clients (like API responses or file contents) |
| 25 | +1. **Tools**: Functions that can be called by the LLM (with user approval) |
| 26 | +1. **Prompts**: Pre-written templates that help users accomplish specific tasks |
| 27 | + |
| 28 | +The SDK comes with NotificationHandlers and RequestHandlers which are expected |
| 29 | +to be wired up in your application. |
| 30 | + |
| 31 | +JsonRpcHandler |
| 32 | +.............. |
| 33 | + |
| 34 | +The ``Mcp\Server\JsonRpcHandler`` is the heart of the SDK. It is here |
| 35 | +you inject the NotificationHandlers and RequestHandlers. It is recommended to use |
| 36 | +the built-in handlers in ``Mcp\Server\NotificationHandlers\*`` and |
| 37 | +``Mcp\Server\RequestHandlers\*``. |
| 38 | + |
| 39 | +The ``Mcp\Server\JsonRpcHandler`` is started and kept running by |
| 40 | +the ``Mcp\Server`` |
| 41 | + |
| 42 | +Transports |
| 43 | +.......... |
| 44 | + |
| 45 | +The SDK supports multiple transports for sending and receiving messages. The |
| 46 | +``Mcp\Server`` is using the transport to fetch a message, then |
| 47 | +give it to the ``Mcp\Server\JsonRpcHandler`` and finally send the |
| 48 | +response/error back to the transport. The SDK comes with a few transports:: |
| 49 | + |
| 50 | +1. **Symfony Console Transport**: Good for testing and for CLI applications |
| 51 | +1. **Stream Transport**: It uses Server Side Events (SSE) and HTTP streaming |
| 52 | + |
| 53 | +Capabilities |
| 54 | +............ |
| 55 | + |
| 56 | +Any client would like to discover the capabilities of the server. Exactly what |
| 57 | +the server supports is defined in the ``Mcp\Server\RequestHandler\InitializeHandler``. |
| 58 | +When the client connects, it sees the capabilities and will ask the server to list |
| 59 | +the tools/resource/prompts etc. When you want to add a new capability, example a |
| 60 | +**Tool** that can tell the current time, you need to provide some metadata to the |
| 61 | +``Mcp\Server\RequestHandler\ToolListHandler``:: |
| 62 | + |
| 63 | + namespace App; |
| 64 | + |
| 65 | + use Mcp\Capability\Tool\MetadataInterface; |
| 66 | + |
| 67 | + class CurrentTimeToolMetadata implements MetadataInterface |
| 68 | + { |
| 69 | + public function getName(): string |
| 70 | + { |
| 71 | + return 'Current time'; |
| 72 | + } |
| 73 | + |
| 74 | + public function getDescription(): string |
| 75 | + { |
| 76 | + return 'Returns the current time in UTC'; |
| 77 | + } |
| 78 | + |
| 79 | + public function getInputSchema(): array |
| 80 | + { |
| 81 | + return [ |
| 82 | + 'type' => 'object', |
| 83 | + 'properties' => [ |
| 84 | + 'format' => [ |
| 85 | + 'type' => 'string', |
| 86 | + 'description' => 'The format of the time, e.g. "Y-m-d H:i:s"', |
| 87 | + 'default' => 'Y-m-d H:i:s', |
| 88 | + ], |
| 89 | + ], |
| 90 | + 'required' => [], |
| 91 | + ]; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | +We would also need a class to actually execute the tool:: |
| 96 | + |
| 97 | + namespace App; |
| 98 | + |
| 99 | + use Mcp\Capability\Tool\IdentifierInterface; |
| 100 | + use Mcp\Capability\Tool\ToolCall; |
| 101 | + use Mcp\Capability\Tool\ToolCallResult; |
| 102 | + use Mcp\Capability\Tool\ToolExecutorInterface; |
| 103 | + |
| 104 | + class CurrentTimeToolExecutor implements ToolExecutorInterface, IdentifierInterface |
| 105 | + { |
| 106 | + public function getName(): string |
| 107 | + { |
| 108 | + return 'Current time'; |
| 109 | + } |
| 110 | + |
| 111 | + public function call(ToolCall $input): ToolCallResult |
| 112 | + { |
| 113 | + $format = $input->arguments['format'] ?? 'Y-m-d H:i:s'; |
| 114 | + |
| 115 | + return new ToolCallResult( |
| 116 | + (new \DateTime('now', new \DateTimeZone('UTC')))->format($format) |
| 117 | + ); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | +If you have multiple tools, you can put them in a ToolChain:: |
| 122 | + |
| 123 | + $tools = new ToolChain([ |
| 124 | + new CurrentTimeToolMetadata(), |
| 125 | + new CurrentTimeToolExecutor(), |
| 126 | + ]); |
| 127 | + |
| 128 | + $jsonRpcHandler = new Mcp\Server\JsonRpcHandler( |
| 129 | + new Mcp\Message\Factory(), |
| 130 | + [ |
| 131 | + new ToolCallHandler($tools), |
| 132 | + new ToolListHandler($tools), |
| 133 | + // Other RequestHandlers ... |
| 134 | + ], |
| 135 | + [ |
| 136 | + // Other NotificationHandlers ... |
| 137 | + ], |
| 138 | + new NullLogger() |
| 139 | + ); |
| 140 | + |
| 141 | +With this metadata and executor, the client can now call the tool. |
| 142 | + |
| 143 | +Extending the SDK |
| 144 | +----------------- |
| 145 | + |
| 146 | +If you want to extend the SDK, you can create your own RequestHandlers and NotificationHandlers. |
| 147 | +The provided one are very good defaults for most applications but they are not |
| 148 | +a requirement. |
| 149 | + |
| 150 | +If you do decide to use them, you get the benefit of having a well-defined interfaces |
| 151 | +and value objects to work with. They will assure that you follow the `Model Context Protocol`_. |
| 152 | +specification. |
| 153 | + |
| 154 | +You also have the Transport abstraction that allows you to create your own transport |
| 155 | +if non of the standard ones fit your needs. |
| 156 | + |
| 157 | +.. _`Model Context Protocol`: https://modelcontextprotocol.io/ |
0 commit comments