Scroll to navigation

MCP::Tool(3) User Contributed Perl Documentation MCP::Tool(3)

NAME

MCP::Tool - Tool container

SYNOPSIS

  use MCP::Tool;
  my $tool = MCP::Tool->new;

DESCRIPTION

MCP::Tool is a container for tools to be called.

ATTRIBUTES

MCP::Tool implements the following attributes.

code

  my $code = $tool->code;
  $tool    = $tool->code(sub { ... });

Tool code.

description

  my $description = $tool->description;
  $tool           = $tool->description('A brief description of the tool');

Description of the tool.

input_schema

  my $schema = $tool->input_schema;
  $tool      = $tool->input_schema({type => 'object', properties => {foo => {type => 'string'}}});

JSON schema for validating input arguments.

name

  my $name = $tool->name;
  $tool    = $tool->name('my_tool');

Name of the tool.

output_schema

  my $schema = $tool->output_schema;
  $tool      = $tool->output_schema({type => 'object', properties => {foo => {type => 'string'}}});

JSON schema for validating output results.

METHODS

MCP::Tool inherits all methods from Mojo::Base and implements the following new ones.

call

  my $result = $tool->call($args, $context);

Calls the tool with the given arguments and context, returning a result. The result can be a promise or a direct value.

context

  my $context = $tool->context;

Returns the context in which the tool is executed.

  # Get controller for requests using the HTTP transport
  my $c = $tool->context->{controller};

image_result

  my $result = $tool->image_result($bytes, $options, $is_error);

Returns an image result in the expected format, optionally marking it as an error.

hese options are currently available:

  annotations => {audience => ['user']}
    

Annotations for the image.

  mime_type => 'image/png'
    

Specifies the MIME type of the image, defaults to 'image/png'.

structured_result

  my $result = $tool->structured_result({foo => 'bar'}, $is_error);

Returns a structured result in the format of "output_schema", optionally marking it as an error.

text_result

  my $result = $tool->text_result('Some text', $is_error);

Returns a text result in the expected format, optionally marking it as an error.

validate_input

  my $bool = $tool->validate_input($args);

Validates the input arguments against the tool's input schema. Returns true if validation failed.

SEE ALSO

MCP, <https://mojolicious.org>, <https://modelcontextprotocol.io>.

2025-08-05 perl v5.42.0