Compare commits
45 changed files with 65 additions and 1082 deletions
10
.github/workflows/conformance-weekly.yaml
vendored
10
.github/workflows/conformance-weekly.yaml
vendored
|
|
@ -125,11 +125,10 @@ jobs:
|
|||
name: Open issue on failure
|
||||
runs-on: ubuntu-latest
|
||||
needs: [server, client]
|
||||
if: failure()
|
||||
if: failure() && github.event_name == 'schedule'
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
GH_REPO: ${{ github.repository }}
|
||||
EVENT_NAME: ${{ github.event_name }}
|
||||
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
||||
steps:
|
||||
- name: File or comment tracking issue
|
||||
|
|
@ -141,13 +140,12 @@ jobs:
|
|||
gh issue create \
|
||||
--title '[conformance] Weekly conformance run failed' \
|
||||
--label conformance-weekly \
|
||||
--body "Conformance against the newest published \`@modelcontextprotocol/conformance\` failed.
|
||||
--body "Weekly conformance against \`@modelcontextprotocol/conformance@latest\` failed.
|
||||
|
||||
- Run: $RUN_URL
|
||||
- Trigger: $EVENT_NAME
|
||||
- Date: $(date -u +%FT%TZ)
|
||||
- Triggered: $(date -u +%FT%TZ)
|
||||
|
||||
Upstream likely published a release whose scenarios the SDK does not satisfy. Either fix the SDK, update the conformance fixtures, or add the new failure to the matching \`tests/Conformance/conformance-baseline-<spec-version>.yml\`."
|
||||
Upstream likely published a release whose scenarios the SDK does not satisfy. Either fix the SDK, update the conformance fixtures, or add the new failure to \`tests/Conformance/conformance-baseline-2025-11-25.yml\`."
|
||||
fi
|
||||
|
||||
publish:
|
||||
|
|
|
|||
2
.github/workflows/docs.yml
vendored
2
.github/workflows/docs.yml
vendored
|
|
@ -35,7 +35,7 @@ jobs:
|
|||
uses: "ramsey/composer-install@v4"
|
||||
|
||||
- name: Install uv
|
||||
uses: astral-sh/setup-uv@v10.1.0
|
||||
uses: astral-sh/setup-uv@v9.0.0
|
||||
with:
|
||||
enable-cache: true
|
||||
|
||||
|
|
|
|||
|
|
@ -2,13 +2,6 @@
|
|||
|
||||
All notable changes to `mcp/sdk` will be documented in this file.
|
||||
|
||||
0.9.0
|
||||
-----
|
||||
|
||||
* [BC Break] Remove the `providerClass` argument of `#[CompletionProvider]`. Use `provider:`, which takes the same class-string and is now the first positional argument.
|
||||
* Add `HttpTransport::getSessionId()` to read the server-minted `Mcp-Session-Id`: a request-scoped caller can persist it and pass it back through the constructor's `$headers` on a later transport. Always `null` on `2026-07-28`, which removed protocol-level sessions.
|
||||
* Fix OIDC discovery rejecting issuers with a trailing slash (e.g. Authentik, Auth0).
|
||||
|
||||
0.8.0
|
||||
-----
|
||||
|
||||
|
|
|
|||
20
README.md
20
README.md
|
|
@ -1,10 +1,19 @@
|
|||
# MCP PHP SDK
|
||||
|
||||
<div align="center">
|
||||
|
||||
[](https://packagist.org/packages/mcp/sdk)
|
||||
[](https://github.com/modelcontextprotocol/php-sdk/actions/workflows/pipeline.yaml)
|
||||
[](https://packagist.org/packages/mcp/sdk)
|
||||
[](LICENSE)
|
||||
|
||||
[](https://github.com/modelcontextprotocol/php-sdk/actions/workflows/conformance-weekly.yaml)
|
||||
[](https://github.com/modelcontextprotocol/php-sdk/actions/workflows/conformance-weekly.yaml)
|
||||
[](https://github.com/modelcontextprotocol/php-sdk/actions/workflows/conformance-weekly.yaml)
|
||||
[](https://github.com/modelcontextprotocol/php-sdk/actions/workflows/conformance-weekly.yaml)
|
||||
|
||||
</div>
|
||||
|
||||
The official PHP SDK for the Model Context Protocol (MCP). It provides a framework-agnostic API for implementing MCP
|
||||
servers and clients in PHP — tools, resources, prompts, STDIO and HTTP transports, sessions, authorization, and both
|
||||
protocol eras (the `initialize` handshake and the stateless `2026-07-28` revision).
|
||||
|
|
@ -16,17 +25,6 @@ development practices and standards from the Symfony project, including [Coding
|
|||
Until the first major release, this SDK is considered [experimental](https://symfony.com/doc/current/contributing/code/experimental.html), please see the [roadmap](./ROADMAP.md) for
|
||||
planned next steps and features.
|
||||
|
||||
## Protocol Conformance
|
||||
|
||||
Measured weekly against the [MCP Conformance Test Framework](https://github.com/modelcontextprotocol/conformance),
|
||||
as passing checks out of total. The `2026-07-28` scores run against the framework's `alpha` releases, so they move as
|
||||
upstream publishes new scenarios.
|
||||
|
||||
| Revision | Server | Client |
|
||||
|:--------:|:------:|:------:|
|
||||
| `2026-07-28` | [](https://github.com/modelcontextprotocol/php-sdk/actions/workflows/conformance-weekly.yaml) | [](https://github.com/modelcontextprotocol/php-sdk/actions/workflows/conformance-weekly.yaml) |
|
||||
| `2025-11-25` | [](https://github.com/modelcontextprotocol/php-sdk/actions/workflows/conformance-weekly.yaml) | [](https://github.com/modelcontextprotocol/php-sdk/actions/workflows/conformance-weekly.yaml) |
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
|
|
|
|||
|
|
@ -35,7 +35,6 @@ The SDK dispatches 4 broad event types at the protocol level, allowing you to ob
|
|||
**Dispatched**: When any request is received from the client, before it's processed by handlers.
|
||||
|
||||
**Properties**:
|
||||
|
||||
- `getRequest(): Request` - The incoming request
|
||||
- `setRequest(Request $request): void` - Modify the request before processing
|
||||
- `getSession(): SessionInterface` - The current session
|
||||
|
|
@ -46,7 +45,6 @@ The SDK dispatches 4 broad event types at the protocol level, allowing you to ob
|
|||
**Dispatched**: When a successful response is ready to be sent to the client, after handler execution.
|
||||
|
||||
**Properties**:
|
||||
|
||||
- `getResponse(): Response` - The response being sent
|
||||
- `setResponse(Response $response): void` - Modify the response before sending
|
||||
- `getRequest(): Request` - The original request
|
||||
|
|
@ -58,7 +56,6 @@ The SDK dispatches 4 broad event types at the protocol level, allowing you to ob
|
|||
**Dispatched**: When an error occurs during request processing.
|
||||
|
||||
**Properties**:
|
||||
|
||||
- `getError(): Error` - The error being sent
|
||||
- `setError(Error $error): void` - Modify the error before sending
|
||||
- `getRequest(): Request` - The original request. Messages that fail to parse are rejected before this event, so a listener never sees them.
|
||||
|
|
@ -70,7 +67,6 @@ The SDK dispatches 4 broad event types at the protocol level, allowing you to ob
|
|||
**Dispatched**: When a notification is received from the client, before it's processed by handlers.
|
||||
|
||||
**Properties**:
|
||||
|
||||
- `getNotification(): Notification` - The incoming notification
|
||||
- `setNotification(Notification $notification): void` - Modify the notification before processing
|
||||
- `getSession(): SessionInterface` - The current session
|
||||
|
|
|
|||
|
|
@ -169,7 +169,6 @@ $client->connect($transport);
|
|||
```
|
||||
|
||||
The `connect()` method performs the MCP initialization handshake:
|
||||
|
||||
1. Opens the transport connection
|
||||
2. Sends InitializeRequest with client capabilities
|
||||
3. Waits for InitializeResult from server
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ $transport = new StdioTransport(
|
|||
```
|
||||
|
||||
**Parameters:**
|
||||
|
||||
- `command` (string): The command to execute
|
||||
- `args` (array): Command arguments
|
||||
- `cwd` (string|null): Working directory for the process
|
||||
|
|
@ -40,7 +39,6 @@ $transport = new HttpTransport(
|
|||
```
|
||||
|
||||
**Parameters:**
|
||||
|
||||
- `endpoint` (string): The MCP server URL
|
||||
- `headers` (array): Additional HTTP headers
|
||||
- `httpClient` (ClientInterface|null): PSR-18 HTTP client (auto-discovered)
|
||||
|
|
@ -52,7 +50,6 @@ $transport = new HttpTransport(
|
|||
**PSR-18 Auto-Discovery:**
|
||||
|
||||
The transport automatically discovers PSR-18 HTTP clients from:
|
||||
|
||||
- `php-http/guzzle7-adapter`
|
||||
- `php-http/curl-client`
|
||||
- `symfony/http-client`
|
||||
|
|
|
|||
|
|
@ -54,7 +54,6 @@ $server = Server::builder()
|
|||
```
|
||||
|
||||
**Parameters:**
|
||||
|
||||
- `$name` (string): The server name
|
||||
- `$version` (string): Version string (semantic versioning recommended)
|
||||
- `$description` (string|null): Optional description
|
||||
|
|
@ -153,7 +152,6 @@ $server = Server::builder()
|
|||
```
|
||||
|
||||
**Parameters:**
|
||||
|
||||
- `$basePath` (string): Base directory for discovery (typically `__DIR__`)
|
||||
- `$scanDirs` (array): Directories to recursively scan for `#[McpTool]`, `#[McpResource]`, etc. All subdirectories are included. (default: `['.', 'src']`)
|
||||
- `$excludeDirs` (array): Directory names to exclude **within** the scanned directories during recursive scanning
|
||||
|
|
@ -191,7 +189,6 @@ $server = Server::builder()
|
|||
```
|
||||
|
||||
**How `excludeDirs` works:**
|
||||
|
||||
- If scanning `src/` and there's `src/vendor/`, it will be excluded
|
||||
- If scanning `lib/` and there's `lib/tests/`, it will be excluded
|
||||
- But if `vendor/` and `tests/` are at the same level as `src/`, they're not scanned anyway (not in `scanDirs`)
|
||||
|
|
@ -220,7 +217,6 @@ $server = Server::builder()
|
|||
```
|
||||
|
||||
**Basic Container Features:**
|
||||
|
||||
- Supports constructor auto-wiring for classes with parameterless constructors
|
||||
- Resolves dependencies where all parameters are type-hinted classes/interfaces known to the container
|
||||
- Supports parameters with default values
|
||||
|
|
|
|||
|
|
@ -74,7 +74,6 @@ $server = Server::builder()
|
|||
```
|
||||
|
||||
**Parameters:**
|
||||
|
||||
- `$gcProbability` (int): The numerator of the GC probability fraction (default: `1`). Set to `0` to disable GC.
|
||||
- `$gcDivisor` (int): The denominator of the GC probability fraction (default: `100`). Must be >= 1.
|
||||
|
||||
|
|
@ -82,7 +81,6 @@ $server = Server::builder()
|
|||
> the `gcProbability` and `gcDivisor` settings are ignored — you control GC behavior in your own implementation.
|
||||
|
||||
**Available Session Stores:**
|
||||
|
||||
- `InMemorySessionStore`: Fast in-memory storage (default)
|
||||
- `FileSessionStore`: Persistent file-based storage
|
||||
- `Psr16SessionStore`: PSR-16 compliant cache-based storage
|
||||
|
|
|
|||
|
|
@ -87,7 +87,6 @@ public function getUserProfile(
|
|||
```
|
||||
|
||||
**Provider Resolution:**
|
||||
|
||||
- **Class strings** (`Provider::class`) → Resolved from PSR-11 container
|
||||
- **Instances** (`new Provider()`) → Used directly
|
||||
- **Values** (`['a', 'b']`) → Wrapped in `ListCompletionProvider`
|
||||
|
|
|
|||
|
|
@ -100,7 +100,6 @@ makes the prompt handler throw.
|
|||
### Error Handling
|
||||
|
||||
Prompt handlers can throw any exception, but the type determines how it's handled:
|
||||
|
||||
- **`PromptGetException`**: Converted to JSON-RPC error response with the actual exception message
|
||||
- **Any other exception**: Converted to JSON-RPC error response, but with a generic error message
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ registry somehow. There are three ways to get it there, and they mix freely.
|
|||
## Attribute-Based Discovery
|
||||
|
||||
**Advantages:**
|
||||
|
||||
- Declarative and readable
|
||||
- Automatic parameter inference
|
||||
- DocBlock integration
|
||||
|
|
@ -26,7 +25,6 @@ Register MCP elements programmatically without using attributes. The handler is
|
|||
any PHP callable.
|
||||
|
||||
**Advantages:**
|
||||
|
||||
- Fine-grained control
|
||||
- Runtime configuration
|
||||
- Conditional registration
|
||||
|
|
|
|||
|
|
@ -24,9 +24,9 @@ class CompletionProvider
|
|||
* @param class-string<ProviderInterface>|ProviderInterface|null $provider if a class-string, it will be resolved
|
||||
* from the container at the point of use
|
||||
* @param ?array<int, int|float|string> $values a list of values to use for completion
|
||||
* @param class-string|null $enum an enum whose cases are the completions
|
||||
*/
|
||||
public function __construct(
|
||||
public ?string $providerClass = null,
|
||||
public string|ProviderInterface|null $provider = null,
|
||||
public ?array $values = null,
|
||||
public ?string $enum = null,
|
||||
|
|
|
|||
|
|
@ -325,6 +325,8 @@ final class Discoverer implements DiscovererInterface
|
|||
|
||||
if ($attributeInstance->provider) {
|
||||
$completionProviders[$param->getName()] = $attributeInstance->provider;
|
||||
} elseif ($attributeInstance->providerClass) {
|
||||
$completionProviders[$param->getName()] = $attributeInstance->provider;
|
||||
} elseif ($attributeInstance->values) {
|
||||
$completionProviders[$param->getName()] = new ListCompletionProvider($attributeInstance->values);
|
||||
} elseif ($attributeInstance->enum) {
|
||||
|
|
|
|||
|
|
@ -88,27 +88,10 @@ final class Registry implements RegistryInterface
|
|||
return;
|
||||
}
|
||||
|
||||
$this->loadFrom($this->loader);
|
||||
|
||||
$this->loaded = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs $loader with the change events its registrations would dispatch suppressed, since they
|
||||
* describe the registry filling up rather than changing.
|
||||
*
|
||||
* Re-entrant-safe, and failure propagates. Does not mark the registry loaded: $loader is the
|
||||
* caller's, and the one the constructor took is still owed its run.
|
||||
*/
|
||||
public function loadFrom(LoaderInterface $loader): void
|
||||
{
|
||||
if ($this->loading) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->loading = true;
|
||||
try {
|
||||
$loader->load($this);
|
||||
$this->loader->load($this);
|
||||
$this->loaded = true;
|
||||
} finally {
|
||||
$this->loading = false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -287,7 +287,7 @@ final class ReflectedElementLoader implements LoaderInterface
|
|||
}
|
||||
|
||||
/**
|
||||
* @return array<string, class-string<ProviderInterface>|ProviderInterface>
|
||||
* @return array<string, ProviderInterface>
|
||||
*/
|
||||
private function getCompletionProviders(\ReflectionMethod|\ReflectionFunction $reflection): array
|
||||
{
|
||||
|
|
@ -307,6 +307,8 @@ final class ReflectedElementLoader implements LoaderInterface
|
|||
|
||||
if ($attributeInstance->provider) {
|
||||
$completionProviders[$param->getName()] = $attributeInstance->provider;
|
||||
} elseif ($attributeInstance->providerClass) {
|
||||
$completionProviders[$param->getName()] = $attributeInstance->providerClass;
|
||||
} elseif ($attributeInstance->values) {
|
||||
$completionProviders[$param->getName()] = new ListCompletionProvider($attributeInstance->values);
|
||||
} elseif ($attributeInstance->enum) {
|
||||
|
|
|
|||
|
|
@ -97,32 +97,10 @@ final class ReferenceHandler implements ReferenceHandlerInterface
|
|||
$finalArgs = [];
|
||||
|
||||
foreach ($reflection->getParameters() as $parameter) {
|
||||
// TODO: Handle variadic parameters.
|
||||
$paramName = $parameter->getName();
|
||||
$paramPosition = $parameter->getPosition();
|
||||
|
||||
if ($parameter->isVariadic()) {
|
||||
// Each element is cast individually below; a non-array value is
|
||||
// wrapped as a single element, since prompt arguments arrive as
|
||||
// plain strings (Record<string,string> per the protocol) even
|
||||
// though tool arguments follow the advertised "array" schema.
|
||||
$values = $arguments[$paramName] ?? [];
|
||||
if (!\is_array($values)) {
|
||||
$values = [$values];
|
||||
} elseif (!array_is_list($values)) {
|
||||
throw RegistryException::invalidParams(\sprintf('Parameter `%s` must be a list of values, not an object.', $paramName));
|
||||
}
|
||||
foreach ($values as $index => $value) {
|
||||
try {
|
||||
$finalArgs[] = $this->castArgumentType($value, $parameter);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
throw RegistryException::invalidParams(\sprintf('Parameter `%s[%d]`: %s', $paramName, $index, $e->getMessage()), $e);
|
||||
} catch (\Throwable $e) {
|
||||
throw RegistryException::internalError(\sprintf('Error processing parameter `%s[%d]`: %s', $paramName, $index, $e->getMessage()), $e);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if parameter is a special injectable type
|
||||
$type = $parameter->getType();
|
||||
if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) {
|
||||
|
|
|
|||
|
|
@ -121,22 +121,6 @@ class HttpTransport extends BaseTransport implements HeaderAwareTransportInterfa
|
|||
$this->headerCallback = $callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* The session ID minted by the server for this connection, if any.
|
||||
*
|
||||
* Captured from the Mcp-Session-Id response header of a handshake-era
|
||||
* server; always null on 2026-07-28, which removed protocol-level sessions
|
||||
* (SEP-2567). A caller that cannot keep the transport alive between
|
||||
* interactions can persist it and pass it back through the constructor's
|
||||
* $headers on a later transport, where it wins over the tracked value.
|
||||
*
|
||||
* @return string|null null until the server mints a session, and again after close()
|
||||
*/
|
||||
public function getSessionId(): ?string
|
||||
{
|
||||
return $this->sessionId;
|
||||
}
|
||||
|
||||
public function send(string $data): void
|
||||
{
|
||||
$request = $this->requestFactory->createRequest('POST', $this->endpoint)
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ abstract class Notification implements HasMethodInterface, MessageInterface
|
|||
$array['params'] = $params;
|
||||
}
|
||||
|
||||
if (null !== $this->meta && !isset($params['_meta'])) {
|
||||
if (null !== $this->meta && !isset($params['meta'])) {
|
||||
$array['params']['_meta'] = $this->meta;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -122,7 +122,7 @@ abstract class Request implements HasMethodInterface, MessageInterface
|
|||
$array['params'] = $params;
|
||||
}
|
||||
|
||||
if (null !== $this->meta && !isset($params['_meta'])) {
|
||||
if (null !== $this->meta && !isset($params['meta'])) {
|
||||
$array['params']['_meta'] = $this->meta;
|
||||
}
|
||||
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue