class McpServerTool implements Approvable, Tool

Traits

Constants

protected MCP_SERVER_TOOL

The MCP server tool class name.

protected MCP_REQUEST

The MCP request class name.

protected MCP_RESPONSE

The MCP response class name.

protected MCP_RESPONSE_FACTORY

The MCP response factory class name.

Properties

protected Approval|bool|null $approvalRequirement from  InteractsWithApprovals

Methods

requireApproval(string|null $reason = null)

Indicate that the tool requires approval before execution.

withoutApproval()

Indicate that the tool may execute without approval.

Approval|null
shouldRequestApproval(Request $request)

Determine whether the tool should request approval for the given request.

Approval|bool
needsApproval(Request $request)

Determine whether the tool needs approval for the given request.

string
errorMessage(string $text)

Format an MCP error payload into tool output.

string
json(array $content)

Encode structured MCP content as JSON.

__construct(object $tool)

Create a new MCP server tool wrapper instance.

static bool
supports(mixed $tool)

Determine whether the given value is an MCP server tool.

string
name()

Get the name of the tool.

Stringable|string
description()

Get the description of the tool's purpose.

array
annotations()

Get the MCP annotations describing the tool's behavior.

Stringable|string
handle(Request $request)

Execute the tool.

array
schema(JsonSchema $schema)

Get the tool's schema definition.

string
convertResponse(mixed $response)

Convert an MCP server response into tool output.

array
normalize(array $items)

Flatten Response, ResponseFactory, string, and nested array items into a flat list of Response instances.

string
finalResponse(array $responses)

Reduce a list of responses to the last non-notification response's text.

Details

InteractsWithApprovals requireApproval(string|null $reason = null)

Indicate that the tool requires approval before execution.

Parameters

string|null $reason

Return Value

InteractsWithApprovals

InteractsWithApprovals withoutApproval()

Indicate that the tool may execute without approval.

Return Value

InteractsWithApprovals

Approval|null shouldRequestApproval(Request $request)

Determine whether the tool should request approval for the given request.

Parameters

Request $request

Return Value

Approval|null

at line 181
protected Approval|bool needsApproval(Request $request)

Determine whether the tool needs approval for the given request.

Parameters

Request $request

Return Value

Approval|bool

protected string errorMessage(string $text)

Format an MCP error payload into tool output.

Parameters

string $text

Return Value

string

protected string json(array $content)

Encode structured MCP content as JSON.

Parameters

array $content

Return Value

string

at line 50
__construct(object $tool)

Create a new MCP server tool wrapper instance.

Parameters

object $tool

at line 57
static bool supports(mixed $tool)

Determine whether the given value is an MCP server tool.

Parameters

mixed $tool

Return Value

bool

at line 65
string name()

Get the name of the tool.

Return Value

string

at line 73
Stringable|string description()

Get the description of the tool's purpose.

Return Value

Stringable|string

at line 83
array annotations()

Get the MCP annotations describing the tool's behavior.

Return Value

array

at line 91
Stringable|string handle(Request $request)

Execute the tool.

Parameters

Request $request

Return Value

Stringable|string

at line 114
array schema(JsonSchema $schema)

Get the tool's schema definition.

Parameters

JsonSchema $schema

Return Value

array

at line 122
protected string convertResponse(mixed $response)

Convert an MCP server response into tool output.

Parameters

mixed $response

Return Value

string

at line 147
protected array normalize(array $items)

Flatten Response, ResponseFactory, string, and nested array items into a flat list of Response instances.

Parameters

array $items

Return Value

array

at line 163
protected string finalResponse(array $responses)

Reduce a list of responses to the last non-notification response's text.

Parameters

array $responses

Return Value

string