Skip to content

[MCP SDK] Add server capabilities toSymfony\AI\McpSdk\Server\RequestHandler\InitializeHandler #305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 11 additions & 2 deletions src/mcp-sdk/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,17 @@ Capabilities

Any client would like to discover the capabilities of the server. Exactly what
the server supports is defined in the ``Symfony\AI\McpSdk\Server\RequestHandler\InitializeHandler``.
When the client connects, it sees the capabilities and will ask the server to list
the tools/resource/prompts etc. When you want to add a new capability, example a
When the client connects, it sends an ``initialize`` request. The server responds
with its capabilities, which are defined by two main classes:

* ``Symfony\AI\McpSdk\Capability\Server\Implementation``: Describes the server software itself (e.g., its name and version).
* ``Symfony\AI\McpSdk\Capability\Server\ServerCapabilities``: Details the features the server supports, such as tools, prompts, or resources.

After that client see available capabilities and will ask the server to list the tools/resource/prompts etc. depending on ``ServerCapabilities`` you provided.
Also ``initialize`` request provides information about protocol version, instructions and ``_meta`` field.
You can find more information about ``Initialize`` response `InitializeResult <https://modelcontextprotocol.io/specification/2025-06-18/schema#initializeresult>`_

When you want to add a new tool for example:
**Tool** that can tell the current time, you need to provide some metadata to the
``Symfony\AI\McpSdk\Server\RequestHandler\ToolListHandler``::

Expand Down
23 changes: 22 additions & 1 deletion src/mcp-sdk/examples/cli/src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@

namespace App;

use Symfony\AI\McpSdk\Capability\Prompt\PromptCapability;
use Symfony\AI\McpSdk\Capability\PromptChain;
use Symfony\AI\McpSdk\Capability\Resource\ResourceCapability;
use Symfony\AI\McpSdk\Capability\ResourceChain;
use Symfony\AI\McpSdk\Capability\Server\Implementation;
use Symfony\AI\McpSdk\Capability\Server\ProtocolVersion;
use Symfony\AI\McpSdk\Capability\Server\ServerCapabilities;
use Symfony\AI\McpSdk\Capability\Tool\ToolCapability;
use Symfony\AI\McpSdk\Capability\ToolChain;
use Symfony\AI\McpSdk\Server\NotificationHandler\InitializedHandler;
use Symfony\AI\McpSdk\Server\NotificationHandlerInterface;
Expand Down Expand Up @@ -45,8 +51,23 @@ public static function buildRequestHandlers(): array
new ExampleTool(),
]);

$implementation = new Implementation(
name: 'MCP-SDK-CLI-Example',
version: '0.1.0'
);
$serverCapabilities = new ServerCapabilities(
prompts: new PromptCapability(listChanged: false),
resources: new ResourceCapability(subscribe: false, listChanged: false),
tools: new ToolCapability(listChanged: false),
);

return [
new InitializeHandler(),
new InitializeHandler(
implementation: $implementation,
serverCapabilities: $serverCapabilities,
protocolVersion: ProtocolVersion::V2025_03_26,
instructions: 'Optional LLM instructions/hints',
),
new PingHandler(),
new PromptListHandler($promptManager),
new PromptGetHandler($promptManager),
Expand Down
24 changes: 24 additions & 0 deletions src/mcp-sdk/src/Capability/Completion/CompletionCapability.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\McpSdk\Capability\Completion;

/**
* Present if the server supports argument autocompletion suggestions.
*
* @see https://modelcontextprotocol.io/specification/2025-06-18/schema#servercapabilities-completions
*/
final class CompletionCapability
{
public function __construct(
) {
}
}
24 changes: 24 additions & 0 deletions src/mcp-sdk/src/Capability/Logging/LoggingCapability.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\McpSdk\Capability\Logging;

/**
* Present if the server supports sending log messages to the client.
*
* @see https://modelcontextprotocol.io/specification/2025-06-18/schema#servercapabilities-logging
*/
final class LoggingCapability
{
public function __construct(
) {
}
}
26 changes: 26 additions & 0 deletions src/mcp-sdk/src/Capability/Prompt/PromptCapability.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\McpSdk\Capability\Prompt;

/**
* Present if the server offers any prompt templates.
*
* @see https://modelcontextprotocol.io/specification/2025-06-18/schema#servercapabilities-logging
*/
final readonly class PromptCapability
{
public function __construct(
/** Whether this server supports notifications for changes to the prompt list. */
public ?bool $listChanged = null,
) {
}
}
28 changes: 28 additions & 0 deletions src/mcp-sdk/src/Capability/Resource/ResourceCapability.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\McpSdk\Capability\Resource;

/**
* Present if the server offers any resources to read.
*
* @see https://modelcontextprotocol.io/specification/2025-06-18/schema#servercapabilities-resources
*/
final readonly class ResourceCapability
{
public function __construct(
/** Whether this server supports notifications for changes to the resource list. */
public ?bool $subscribe = null,
/** Whether this server supports subscribing to resource updates. */
public ?bool $listChanged = null,
) {
}
}
26 changes: 26 additions & 0 deletions src/mcp-sdk/src/Capability/Server/Implementation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\McpSdk\Capability\Server;

/**
* Describes the name and version of an MCP implementation, with an optional title for UI representation.
*
* @see https://modelcontextprotocol.io/specification/2025-06-18/schema#implementation
*/
final readonly class Implementation
{
public function __construct(
public string $name = 'app',
public string $version = 'dev',
) {
}
}
19 changes: 19 additions & 0 deletions src/mcp-sdk/src/Capability/Server/ProtocolVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\McpSdk\Capability\Server;

enum ProtocolVersion: string
{
case V2024_11_05 = '2024-11-05';
case V2025_03_26 = '2025-03-26';
case V2025_06_18 = '2025-06-18';
}
83 changes: 83 additions & 0 deletions src/mcp-sdk/src/Capability/Server/ServerCapabilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\McpSdk\Capability\Server;

use Symfony\AI\McpSdk\Capability\Completion\CompletionCapability;
use Symfony\AI\McpSdk\Capability\Logging\LoggingCapability;
use Symfony\AI\McpSdk\Capability\Prompt\PromptCapability;
use Symfony\AI\McpSdk\Capability\Resource\ResourceCapability;
use Symfony\AI\McpSdk\Capability\Tool\ToolCapability;

/**
* Capabilities that a server may support. Known capabilities are defined here, in this schema,
* but this is not a closed set: any server can define its own, additional capabilities.
*
* @see https://modelcontextprotocol.io/specification/2025-06-18/schema#servercapabilities
*/
final readonly class ServerCapabilities implements \JsonSerializable
{
/**
* @param array<string, array<string, mixed>>|null $experimental
*/
public function __construct(
/**
* Present if the server supports sending log messages to the client.
*
* @see https://modelcontextprotocol.io/specification/2025-06-18/schema#servercapabilities-logging
*/
public ?LoggingCapability $logging = null,
/**
* Present if the server offers any prompt templates.
*
* @see https://modelcontextprotocol.io/specification/2025-06-18/schema#servercapabilities-prompts
*/
public ?PromptCapability $prompts = null,
/**
* Present if the server offers any resources to read.
*
* @see https://modelcontextprotocol.io/specification/2025-06-18/schema#servercapabilities-resources
*/
public ?ResourceCapability $resources = null,
/**
* Present if the server offers any tools to call.
*
* @see https://modelcontextprotocol.io/specification/2025-06-18/schema#servercapabilities-tools
*/
public ?ToolCapability $tools = null,
/**
* Present if the server supports argument autocompletion suggestions.
*
* @see https://modelcontextprotocol.io/specification/2025-06-18/schema#servercapabilities-completions
*/
public ?CompletionCapability $completions = null,
/**
* @var array<string, array<string, mixed>>|null
*/
public ?array $experimental = null,
) {
}

/**
* @return array{
* logging?: LoggingCapability,
* prompts?: PromptCapability,
* resources?: ResourceCapability,
* tools?: ToolCapability,
* completions?: CompletionCapability,
* experimental?: array<string, array<string, mixed>>
* }
*/
public function jsonSerialize(): array
{
return array_filter((array) $this, fn ($value) => null !== $value);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look nice, ideally it would be better to use centralized solution in \Symfony\AI\McpSdk\Message\Response::jsonSerialize.

Serializer with AbstractObjectNormalizer::SKIP_NULL_VALUES would fit perfectly, but it's not in dependencies list.

}
}
26 changes: 26 additions & 0 deletions src/mcp-sdk/src/Capability/Tool/ToolCapability.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\McpSdk\Capability\Tool;

/**
* Present if the server offers any tools to call.
*
* @see https://modelcontextprotocol.io/specification/2025-06-18/schema#servercapabilities-tools
*/
final readonly class ToolCapability
{
public function __construct(
/** Whether this server supports notifications for changes to the tool list. */
public ?bool $listChanged = null,
) {
}
}
38 changes: 38 additions & 0 deletions src/mcp-sdk/src/Field/MetaField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\McpSdk\Field;

/**
* The _meta property/parameter is reserved by MCP to allow clients and servers to attach additional metadata to their interactions. Certain key names are reserved by MCP for protocol-level metadata, as specified below; implementations MUST NOT make assumptions about values at these keys. Additionally, definitions in the schema may reserve particular names for purpose-specific metadata, as declared in those definitions. Key name format: valid _meta key names have two segments: an optional prefix, and a name. Prefix:
*
* If specified, MUST be a series of labels separated by dots (.), followed by a slash (/).
* Labels MUST start with a letter and end with a letter or digit; interior characters can be letters, digits, or hyphens (-).
* Any prefix beginning with zero or more valid labels, followed by modelcontextprotocol or mcp, followed by any valid label, is reserved for MCP use.
* For example: modelcontextprotocol.io/, mcp.dev/, api.modelcontextprotocol.org/, and tools.mcp.com/ are all reserved.
*
* Name:
*
* Unless empty, MUST begin and end with an alphanumeric character ([a-z0-9A-Z]).
* MAY contain hyphens (-), underscores (_), dots (.), and alphanumerics in between.
*
* @see https://modelcontextprotocol.io/specification/2025-06-18/basic/index#general-fields
*/
final readonly class MetaField
{
/**
* @param array<string, mixed> $meta
*/
public function __construct(
public array $meta,
) {
}
}
10 changes: 5 additions & 5 deletions src/mcp-sdk/src/Message/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public function __construct(
) {
}

public function __toString(): string
{
return \sprintf('%s', $this->method);
}

/**
* @param array{method: string, params?: array<string, mixed>} $data
*/
Expand All @@ -44,9 +49,4 @@ public function jsonSerialize(): array
'params' => $this->params,
];
}

public function __toString(): string
{
return \sprintf('%s', $this->method);
}
}
Loading