Scroll to navigation

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

NAME

MCP::Server - MCP server implementation

SYNOPSIS

  use MCP::Server;
  my $server = MCP::Server->new(name => 'MyServer');
  $server->tool(
    name         => 'echo',
    description  => 'Echo the input text',
    input_schema => {type => 'object', properties => {msg => {type => 'string'}}, required => ['msg']},
    code         => sub ($tool, $args) {
      return "Echo: $args->{msg}";
    }
  );
  $server->prompt(
    name        => 'echo',
    description => 'A prompt to demonstrate the echo tool',
    code        => sub ($prompt, $args) {
      return 'Use the echo tool with the message "Hello, World!"';
    }
  );
  $server->resource(
    uri         => 'file:///example.txt',
    name        => 'example',
    description => 'A simple text resource',
    mime_type   => 'text/plain',
    code        => sub ($resource) {
      return 'This is an example resource content.';
    }
  );
  $server->to_stdio;

DESCRIPTION

MCP::Server is an MCP (Model Context Protocol) server.

EVENTS

MCP::Server inherits all events from Mojo::EventEmitter and emits the following new ones.

prompts

  $server->on(prompts => sub ($server, $prompts, $context) { ... });

Emitted whenever the list of prompts is accessed.

resources

  $server->on(resources => sub ($server, $resources, $context) { ... });

Emitted whenever the list of resources is accessed.

tools

  $server->on(tools => sub ($server, $tools, $context) { ... });

Emitted whenever the list of tools is accessed.

ATTRIBUTES

MCP::Server implements the following attributes.

name

  my $name = $server->name;
  $server  = $server->name('MyServer');

The name of the server, used for identification.

prompts

  my $prompts = $server->prompts;
  $server    = $server->prompts([MCP::Prompt->new]);

An array reference containing registered prompts.

resources

  my $resources = $server->resources;
  $server      = $server->resources([MCP::Resource->new]);

An array reference containing registered resources.

tools

  my $tools = $server->tools;
  $server   = $server->tools([MCP::Tool->new]);

An array reference containing registered tools.

transport

  my $transport = $server->transport;
  $server       = $server->transport(MCP::Server::Transport::HTTP->new);

The transport layer used by the server, such as MCP::Server::Transport::HTTP or MCP::Server::Transport::Stdio.

version

  my $version = $server->version;
  $server     = $server->version('1.0.0');

The version of the server.

METHODS

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

handle

  my $response = $server->handle($request, $context);

Handle a JSON-RPC request and return a response.

prompt

  my $prompt = $server->prompt(
    name        => 'my_prompt',
    description => 'A sample prompt',
    arguments   => [{name => 'foo', description => 'Whatever', required => 1}],
    code        => sub ($prompt, $args) { ... }
  );

Register a new prompt with the server.

resource

  my $resource = $server->resource(
    uri         => 'file://my_resource',
    name        => 'sample_resource',
    description => 'A sample resource',
    mime_type   => 'text/plain',
    code        => sub ($resource) { ... }
  );

Register a new resource with the server.

to_action

  my $action = $server->to_action;

Convert the server to a Mojolicious action.

to_stdio

  $server->to_stdio;

Handles JSON-RPC requests over standard input/output.

tool

  my $tool = $server->tool(
    name         => 'my_tool',
    description  => 'A sample tool',
    input_schema => {type => 'object', properties => {foo => {type => 'string'}}},
    code         => sub ($tool, $args) { ... }
  );

Register a new tool with the server.

SEE ALSO

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

2026-01-17 perl v5.42.0