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->to_stdio;

DESCRIPTION

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

ATTRIBUTES

MCP::Server implements the following attributes.

name

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

The name of the server, used for identification.

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::Base 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.

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.