MCP PHP SDK

Builder

FinalYes
Tags
phpstan-import-type

Handler from ElementReference

author

Kyrian Obikwelu koshnawaza@gmail.com

Table of Contents

Properties

$container  : ContainerInterface|null
$discoverer  : DiscovererInterface|null
$discoveryBasePath  : string|null
$discoveryCache  : CacheInterface|null
$discoveryExcludeDirs  : array<string|int, mixed>|array<string|int, string>
$discoveryNamePatterns  : array<string|int, string>|null
$discoveryScanDirs  : array<string|int, string>
$eventDispatcher  : EventDispatcherInterface|null
$explicitPrompts  : array<int, ProviderInterface>}>
$explicitResources  : array<int, ResourceHandlerInterface}>
$explicitResourceTemplates  : array<int, ProviderInterface>}>
$explicitTools  : array<int, ToolHandlerInterface}>
$extensions  : array<string, array<string, mixed>>
$gcDivisor  : int
$gcProbability  : int
$instructions  : string|null
$loaders  : array<string|int, LoaderInterface>
$logger  : LoggerInterface|null
$notificationHandlers  : array<int, NotificationHandlerInterface>
$paginationLimit  : int
$prompts  : array<string|int, Icon[], meta: ?array}>
$protocolVersion  : ProtocolVersion|null
$referenceHandler  : ReferenceHandlerInterface|null
$registry  : RegistryInterface
$requestHandlers  : array<int, RequestHandlerInterface<string|int, mixed>>
$resources  : array<string|int, Icon[], meta: ?array}>
$resourceTemplates  : array<string|int, Annotations, meta: ?array}>
$schemaGenerator  : SchemaGeneratorInterface|null
$serverCapabilities  : ServerCapabilities|null
$serverInfo  : Implementation|null
$sessionManager  : SessionManagerInterface|null
$sessionStore  : SessionStoreInterface|null
$subscriptionManager  : SubscriptionManagerInterface|null
$tools  : array<string|int, Icon[], meta: ?array, outputSchema: ?array}>

Methods

add()  : self
Registers an element using an explicit schema value object paired with a handler interface.
addLoader()  : self
Register a single custom loader.
addLoaders()  : self
addNotificationHandler()  : self
Register a single custom notification handler.
addNotificationHandlers()  : self
Register multiple custom notification handlers.
addPrompt()  : self
Manually registers a prompt handler.
addRequestHandler()  : self
Register a single custom method handler.
addRequestHandlers()  : self
Register multiple custom method handlers.
addResource()  : self
Manually registers a resource handler.
addResourceTemplate()  : self
Manually registers a resource template handler.
addTool()  : self
Manually registers a tool handler.
build()  : Server
Builds the fully configured Server instance.
enableExtension()  : self
Enable one or more MCP protocol extensions, announced to clients under `capabilities.extensions` during the initialize handshake.
setCapabilities()  : self
Explicitly set server capabilities. If set, this overrides automatic detection.
setContainer()  : self
Provides a PSR-11 DI container, primarily for resolving user-defined handler classes.
setDiscoverer()  : self
setDiscovery()  : self
setEventDispatcher()  : self
setInstructions()  : self
Configures the instructions describing how to use the server and its features.
setLogger()  : self
Provides a PSR-3 logger instance. Defaults to NullLogger.
setPaginationLimit()  : self
Configures the server's pagination limit.
setProtocolVersion()  : self
setReferenceHandler()  : self
setRegistry()  : self
setResourceSubscriptionManager()  : self
setSchemaGenerator()  : self
setServerInfo()  : self
Sets the server's identity. Required.
setSession()  : self
Configures the session layer.
createDiscoverer()  : DiscovererInterface

Properties

$container

private ContainerInterface|null $container = null

$discoverer

private DiscovererInterface|null $discoverer = null

$discoveryBasePath

private string|null $discoveryBasePath = null

$discoveryCache

private CacheInterface|null $discoveryCache = null

$discoveryExcludeDirs

private array<string|int, mixed>|array<string|int, string> $discoveryExcludeDirs = []

$discoveryNamePatterns

private array<string|int, string>|null $discoveryNamePatterns = null

$discoveryScanDirs

private array<string|int, string> $discoveryScanDirs = []

$eventDispatcher

private EventDispatcherInterface|null $eventDispatcher = null

$explicitPrompts

private array<int, ProviderInterface>}> $explicitPrompts = []

$explicitResources

private array<int, ResourceHandlerInterface}> $explicitResources = []

$explicitResourceTemplates

private array<int, ProviderInterface>}> $explicitResourceTemplates = []

$explicitTools

private array<int, ToolHandlerInterface}> $explicitTools = []

$extensions

private array<string, array<string, mixed>> $extensions = []

$gcDivisor

private int $gcDivisor = 100

$gcProbability

private int $gcProbability = 1

$instructions

private string|null $instructions = null

$logger

private LoggerInterface|null $logger = null

$paginationLimit

private int $paginationLimit = 50

$prompts

private array<string|int, Icon[], meta: ?array}> $prompts = []

$resources

private array<string|int, Icon[], meta: ?array}> $resources = []

$resourceTemplates

private array<string|int, Annotations, meta: ?array}> $resourceTemplates = []

$tools

private array<string|int, Icon[], meta: ?array, outputSchema: ?array}> $tools = []

Methods

add()

Registers an element using an explicit schema value object paired with a handler interface.

public add(Tool|ResourceDefinition|ResourceTemplate|Prompt $definition, ElementHandlerInterface $handler[, array<string, ProviderInterface$completionProviders = [] ]) : self

Use this entry point when an element's name, schema, or description is only known at runtime (e.g. config-driven integrations). For statically-known elements, prefer addTool/addResource/addResourceTemplate/addPrompt, which can derive metadata from reflection of the handler.

Mismatched pairings (e.g. a Tool with a PromptHandlerInterface) raise Mcp\Exception\InvalidArgumentException. Completion providers are only supported on Prompt and ResourceTemplate definitions; supplying them with Tool or ResourceDefinition raises the same exception.

Parameters
$definition : Tool|ResourceDefinition|ResourceTemplate|Prompt
$handler : ElementHandlerInterface
$completionProviders : array<string, ProviderInterface> = []

Keyed by argument/variable name

Return values
self

addPrompt()

Manually registers a prompt handler.

public addPrompt(Handler $handler[, string|null $name = null ][, string|null $title = null ][, string|null $description = null ][, array<string|int, Icon>|null $icons = null ][, array<string, mixed>|null $meta = null ]) : self
Parameters
$handler : Handler
$name : string|null = null
$title : string|null = null
$description : string|null = null
$icons : array<string|int, Icon>|null = null
$meta : array<string, mixed>|null = null
Return values
self

addRequestHandlers()

Register multiple custom method handlers.

public addRequestHandlers(iterable<string|int, RequestHandlerInterface<string|int, mixed>> $handlers) : self
Parameters
$handlers : iterable<string|int, RequestHandlerInterface<string|int, mixed>>
Return values
self

addResource()

Manually registers a resource handler.

public addResource(Handler $handler, string $uri[, string|null $name = null ][, string|null $title = null ][, string|null $description = null ][, string|null $mimeType = null ][, int|null $size = null ][, Annotations|null $annotations = null ][, array<string|int, Icon>|null $icons = null ][, array<string, mixed>|null $meta = null ]) : self
Parameters
$handler : Handler
$uri : string
$name : string|null = null
$title : string|null = null

Optional human-readable title for display in UI

$description : string|null = null
$mimeType : string|null = null
$size : int|null = null
$annotations : Annotations|null = null
$icons : array<string|int, Icon>|null = null
$meta : array<string, mixed>|null = null
Return values
self

addResourceTemplate()

Manually registers a resource template handler.

public addResourceTemplate(Handler $handler, string $uriTemplate[, string|null $name = null ][, string|null $title = null ][, string|null $description = null ][, string|null $mimeType = null ][, Annotations|null $annotations = null ][, array<string, mixed>|null $meta = null ]) : self
Parameters
$handler : Handler
$uriTemplate : string
$name : string|null = null
$title : string|null = null

Optional human-readable title for display in UI

$description : string|null = null
$mimeType : string|null = null
$annotations : Annotations|null = null
$meta : array<string, mixed>|null = null
Return values
self

addTool()

Manually registers a tool handler.

public addTool(Handler $handler[, string|null $name = null ][, string|null $title = null ][, string|null $description = null ][, ToolAnnotations|null $annotations = null ][, array<string, mixed>|null $inputSchema = null ][, array<string|int, Icon>|null $icons = null ][, array<string, mixed>|null $meta = null ][, array<string, mixed>|null $outputSchema = null ]) : self
Parameters
$handler : Handler
$name : string|null = null
$title : string|null = null

Optional human-readable title for display in UI

$description : string|null = null
$annotations : ToolAnnotations|null = null
$inputSchema : array<string, mixed>|null = null
$icons : array<string|int, Icon>|null = null
$meta : array<string, mixed>|null = null
$outputSchema : array<string, mixed>|null = null
Return values
self

build()

Builds the fully configured Server instance.

public build() : Server
Return values
Server

enableExtension()

Enable one or more MCP protocol extensions, announced to clients under `capabilities.extensions` during the initialize handshake.

public enableExtension(ServerExtensionInterface ...$extensions) : self
Parameters
$extensions : ServerExtensionInterface
Tags
throws
LogicException

if the same extension is enabled more than once

Return values
self

setCapabilities()

Explicitly set server capabilities. If set, this overrides automatic detection.

public setCapabilities(ServerCapabilities $serverCapabilities) : self
Parameters
$serverCapabilities : ServerCapabilities
Return values
self

setContainer()

Provides a PSR-11 DI container, primarily for resolving user-defined handler classes.

public setContainer(ContainerInterface $container) : self

Defaults to a basic internal container.

Parameters
$container : ContainerInterface
Return values
self

setDiscoverer()

public setDiscoverer(DiscovererInterface $discoverer) : self
Parameters
$discoverer : DiscovererInterface
Return values
self

setDiscovery()

public setDiscovery(string $basePath[, array<string|int, string> $scanDirs = ['.', 'src'] ][, array<string|int, string> $excludeDirs = [] ][, CacheInterface|null $cache = null ][, array<string|int, string> $namePatterns = DiscovererInterface::DEFAULT_NAME_PATERNS ]) : self
Parameters
$basePath : string
$scanDirs : array<string|int, string> = ['.', 'src']
$excludeDirs : array<string|int, string> = []
$cache : CacheInterface|null = null
$namePatterns : array<string|int, string> = DiscovererInterface::DEFAULT_NAME_PATERNS
Return values
self

setEventDispatcher()

public setEventDispatcher(EventDispatcherInterface $eventDispatcher) : self
Parameters
$eventDispatcher : EventDispatcherInterface
Return values
self

setInstructions()

Configures the instructions describing how to use the server and its features.

public setInstructions(string|null $instructions) : self

This can be used by clients to improve the LLM's understanding of available tools, resources, etc. It can be thought of like a "hint" to the model. For example, this information MAY be added to the system prompt.

Parameters
$instructions : string|null
Return values
self

setLogger()

Provides a PSR-3 logger instance. Defaults to NullLogger.

public setLogger(LoggerInterface $logger) : self
Parameters
$logger : LoggerInterface
Return values
self

setPaginationLimit()

Configures the server's pagination limit.

public setPaginationLimit(int $paginationLimit) : self
Parameters
$paginationLimit : int
Return values
self

setServerInfo()

Sets the server's identity. Required.

public setServerInfo(string $name, string $version[, string|null $description = null ][, array<string|int, Icon>|null $icons = null ][, string|null $websiteUrl = null ]) : self
Parameters
$name : string
$version : string
$description : string|null = null
$icons : array<string|int, Icon>|null = null
$websiteUrl : string|null = null
Return values
self

setSession()

Configures the session layer.

public setSession([SessionStoreInterface|null $sessionStore = null ][, SessionManagerInterface|null $sessionManager = null ][, int $gcProbability = 1 ][, int $gcDivisor = 100 ]) : self
Parameters
$sessionStore : SessionStoreInterface|null = null
$sessionManager : SessionManagerInterface|null = null
$gcProbability : int = 1

The numerator of the GC probability fraction (like PHP's session.gc_probability). Set to 0 to disable GC.

$gcDivisor : int = 100

The denominator of the GC probability fraction (like PHP's session.gc_divisor). Probability = gcProbability/gcDivisor.

Return values
self

createDiscoverer()

private createDiscoverer(LoggerInterface $logger) : DiscovererInterface
Parameters
$logger : LoggerInterface
Return values
DiscovererInterface
On this page

Search results