Назад к каталогу
mcp-sdk-php

mcp-sdk-php

Сообщество

от logiscape

0.0
0 отзывов

Model Context Protocol SDK for PHP

Описание

# Model Context Protocol SDK for PHP English | [中文](README.zh-CN.md) This package provides a PHP implementation of the [Model Context Protocol](https://modelcontextprotocol.io), allowing applications to provide context for LLMs in a standardized way. It separates the concerns of providing context from the actual LLM interaction. ## Overview This PHP SDK implements the full MCP specification, making it easy to: * Build MCP clients that can connect to any MCP server * Create MCP servers that expose resources, prompts and tools * Use standard transports like stdio and HTTP * Handle all MCP protocol messages and lifecycle events This SDK began as a PHP port of the official [Python SDK](https://github.com/modelcontextprotocol/python-sdk) for the Model Context Protocol. It has since been expanded to fully support MCP using native PHP functions, helping to maximize compatibility with most standard web hosting environments. This SDK is primarily targeted at developers working on frontier AI integration solutions. Some functionality may be incomplete and implementations should undergo thorough testing and security review by experienced developers prior to production use. ## Installation You can install the package via composer: ```bash composer require logiscape/mcp-sdk-php ``` ### Requirements * PHP 8.1 or higher * ext-curl * ext-json * ext-pcntl (optional, recommended for CLI environments) * monolog/monolog (optional, used by example clients/servers for logging) ## Basic Usage ### Creating an MCP Server Here's a complete example of creating an MCP server that provides prompts: ```php <?php // A basic example server with a list of prompts for testing require 'vendor/autoload.php'; use Mcp\Server\Server; use Mcp\Server\ServerRunner; use Mcp\Types\Prompt; use Mcp\Types\PromptArgument; use Mcp\Types\PromptMessage; use Mcp\Types\ListPromptsResult; use Mcp\Types\TextContent; use Mcp\Types\Role; use Mcp\Types\GetPromptResult; use Mcp\Types\GetPromptRequestParams; // Create a server instance $server = new Server('example-server'); // Register prompt handlers $server->registerHandler('prompts/list', function($params) { $prompt = new Prompt( name: 'example-prompt', description: 'An example prompt template', arguments: [ new PromptArgument( name: 'arg1', description: 'Example argument', required: true ) ] ); return new ListPromptsResult([$prompt]); }); $server->registerHandler('prompts/get', function(GetPromptRequestParams $params) { $name = $params->name; $arguments = $params->arguments; if ($name !== 'example-prompt') { throw new \InvalidArgumentException("Unknown prompt: {$name}"); } // Get argument value safely $argValue = $arguments ? $arguments->arg1 : 'none'; $prompt = new Prompt( name: 'example-prompt', description: 'An example prompt template', arguments: [ new PromptArgument( name: 'arg1', description: 'Example argument', required: true ) ] ); return new GetPromptResult( messages: [ new PromptMessage( role: Role::USER, content: new TextContent( text: "Example prompt text with argument: $argValue" ) ) ], description: 'Example prompt' ); }); // Create initialization options and run server $initOptions = $server->createInitializationOptions(); $runner = new ServerRunner($server, $initOptions); $runner->run(); ``` Save this as `example_server.php` ### Creating an MCP Client Here's how to create a client that connects to the example server: ```php <?php // A basic example client that connects to example_server.php and outputs the prompts require 'vendor/autoload.php'; use Mcp\Client\Client; use Mcp\Client\Transport\StdioServerParameters; use Mcp\Types\TextContent; // Create server parameters for stdio connection $serverParams = new StdioServerParameters( command: 'php', // Executable args: ['example_server.php'], // File path to the server env: null // Optional environment variables ); // Create client instance $client = new Client(); try { echo("Starting to connect\n"); // Connect to the server using stdio transport $session = $client->connect( commandOrUrl: $serverParams->getCommand(), args: $serverParams->getArgs(), env: $serverParams->getEnv() ); echo("Starting to get available prompts\n"); // List available prompts $promptsResult = $session->listPrompts(); // Output the list of prompts if (!empty($promptsResult->prompts)) { echo "Available prompts:\n"; foreach ($promptsResult->prompts as $prompt) { echo " - Name: " . $prompt->name . "\n"; echo " Description: " . $prompt->description .

Отзывы (0)

Пока нет отзывов. Будьте первым!