php-mcp-sdk / rules
Cursor rule27 starsChanged 15 months agoMITView on GitHub
Guidelines for implementing MCP servers in PHP
Where files are headingNew Cursor rule files have more rules about planning and tracking tasks, and fewer about code style.
Of the Cursor rule files we read, the 1,533 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository7 other files it gives its agents.
- .cursor/rules/changelog-maintenance.mdcCursor rule
- .cursor/rules/laravel-inertia-integration.mdcCursor rule
- .cursor/rules/php-auth-implementation.mdcCursor rule
- .cursor/rules/php-client-implementation.mdcCursor rule
- .cursor/rules/php-mcp-sdk-main.mdcCursor rule
- .cursor/rules/php-transport-implementation.mdcCursor rule
- .cursor/rules/php-types-validation.mdcCursor rule
The file
We split this php-server-implementation.mdc into the rules it gives an agent
3 rules. About 0 are common, 3 are distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 283
Validation: Always validate input against schemas
- Distinctive: line 285
Async Operations: Use ReactPHP promises for async operations
- Distinctive: line 288
Logging: Use the server's logging capabilities for debugging
≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
---
description: Guidelines for implementing MCP servers in PHP
---
# PHP MCP Server Implementation Guide
## High-Level McpServer Class
The `McpServer` class provides a simplified API for creating MCP servers. It wraps the lower-level `Server` class.
### Basic Server Structure
```php
namespace MCP\Server;
use MCP\Types\Implementation;
use MCP\Shared\Transport;
use React\Promise\Promise;
class McpServer {
private Server $server;
private array $registeredTools = [];
private array $registeredResources = [];
private array $registeredResourceTemplates = [];
private array $registeredPrompts = [];
public function __construct(Implementation $serverInfo, ?ServerOptions $options = null) {
$this->server = new Server($serverInfo, $options);
}
public function connect(Transport $transport): Promise {
return $this->server->connect($transport);
}
}
```
### Registering Tools
```php
/**
* Register a tool with validation schema and handler
*/
public function registerTool(
string $name,
array $config,
callable $handler
): RegisteredTool {
// Tool configuration includes:
// - title: Display name
// - description: Tool description
// - inputSchema: Input validation schema
// - outputSchema: Optional output schema
// - annotations: Tool hints
$tool = new RegisteredTool($name, $config, $handler);
$this->registeredTools[$name] = $tool;
// Emit list changed notification if connected
if ($this->server->isConnected()) {
$this->server->sendNotification([
'method' => 'notifications/tools/list_changed'
]);
}
return $tool;
}
```
### Registering Resources
```php
/**
* Register a static resource
*/
public function registerResource(
string $name,
string $uri,
array $metadata,
callable $handler
): RegisteredResource {
$resource = new RegisteredResource($name, $uri, $metadata, $handler);
$this->registeredResources[$uri] = $resource;
return $resource;
}
/**
* Register a resource template for dynamic resources
*/
public function registerResourceTemplate(
string $name,
UriTemplate $template,
array $metadata,
callable $handler
): RegisteredResourceTemplate {
$resourceTemplate = new RegisteredResourceTemplate($name, $template, $metadata, $handler);
$this->registeredResourceTemplates[$name] = $resourceTemplate;
return $resourceTemplate;
}
```
### Registering Prompts
```php
/**
* Register a prompt template
*/
public function registerPrompt(
string $name,
array $config,
callable $handler
): RegisteredPrompt {
// Config includes:
// - title: Display name
// - description: Prompt description
// - argsSchema: Arguments validation schema
$prompt = new RegisteredPrompt($name, $config, $handler);
$this->registeredPrompts[$name] = $prompt;
return $prompt;
}
```
## Tool Implementation Pattern
```php
// Example: Calculator tool
$server->registerTool(
'calculate',
[
'title' => 'Calculator',
'description' => 'Perform mathematical calculations',
'inputSchema' => [
'type' => 'object',
'properties' => [
'expression' => ['type' => 'string'],
'precision' => ['type' => 'integer', 'default' => 2]
],
'required' => ['expression']
]
],
function (array $params) use ($calculator): array {
try {
$result = $calculator->evaluate($params['expression']);
return [
'content' => [[
'type' => 'text',
'text' => (string) round($result, $params['precision'] ?? 2)
]]
];
} catch (\Exception $e) {
return [
'content' => [[
'type' => 'text',
'text' => "Error: " . $e->getMessage()
]],
'isError' => true
];
}
}
);
```
## Resource Implementation Pattern
```php
// Static resource
$server->registerResource(
'config',
'config://app',
[
'title' => 'Application Config',
'description' => 'Current application configuration',
'mimeType' => 'application/json'
],
function (string $uri): array {
return [
'contents' => [[
'uri' => $uri,
'text' => json_encode($this->getConfig()),
'mimeType' => 'application/json'
]]
];
}
);
// Dynamic resource with template
$server->registerResourceTemplate(
'user-profile',
new UriTemplate('users://{userId}/profile'),
[
'title' => 'User Profile',
'description' => 'User profile information'
],
function (string $uri, array $params): array {
$user = $this->userRepository->find($params['userId']);
return [
'contents' => [[
'uri' => $uri,
'text' => json_encode($user),
'mimeType' => 'application/json'
]]
];
}
);
```
## Prompt Implementation Pattern
```php
use MCP\Utils\Completable;
$server->registerPrompt(
'code-review',
[
'title' => 'Code Review',
'description' => 'Review code for best practices',
'argsSchema' => [
'language' => Completable::string()
->withCompletions(['php', 'javascript', 'python']),
'code' => Completable::string()
]
],
function (array $args): array {
return [
'messages' => [[
'role' => 'user',
'content' => [
'type' => 'text',
'text' => sprintf(
"Please review this %s code:\n\n%s",
$args['language'],
$args['code']
)
]
]]
];
}
);
```
## Server Lifecycle Management
```php
// Dynamic tool management
$tool = $server->registerTool('example', [...], $handler);
// Disable tool temporarily
$tool->disable();
// Enable tool
$tool->enable();
// Update tool configuration
$tool->update(['description' => 'Updated description']);
// Remove tool completely
$tool->remove();
```
## Error Handling
```php
// In handlers, return errors properly
function toolHandler(array $params): array {
try {
// Tool logic
return ['content' => [['type' => 'text', 'text' => 'Success']]];
} catch (\Exception $e) {
// Return error in result, not as exception
return [
'content' => [[
'type' => 'text',
'text' => 'Error: ' . $e->getMessage()
]],
'isError' => true
];
}
}
```
## Best Practices
1. **Validation**: Always validate input against schemas
2. **Error Handling**: Return errors in results, not as protocol errors
3. **Async Operations**: Use ReactPHP promises for async operations
4. **Resource Links**: Tools can return resource links without content
5. **Notifications**: Send list_changed notifications when modifying registrations
6. **Logging**: Use the server's logging capabilities for debugging
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

