| 1 | package MCP; | ||
| 2 | use Mojo::Base -base, -signatures; | ||
| 3 | |||
| 4 | our $VERSION = '0.12'; | ||
| 5 | |||
| 6 | 1; | ||
| 7 | |||
| 8 | =encoding utf8 | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | MCP - Model Context Protocol Perl SDK | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | use Mojolicious::Lite -signatures; | ||
| 17 | |||
| 18 | use MCP::Server; | ||
| 19 | |||
| 20 | my $server = MCP::Server->new; | ||
| 21 | $server->tool( | ||
| 22 | name => 'time', | ||
| 23 | description => 'Get the current local time', | ||
| 24 | code => sub ($tool, $args) { | ||
| 25 | return localtime(time); | ||
| 26 | } | ||
| 27 | ); | ||
| 28 | |||
| 29 | any '/mcp' => $server->to_action; | ||
| 30 | |||
| 31 | app->start; | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | Connect Perl with AI using the Model Context Protocol (MCP). Currently this module is focused on tool calling and | ||
| 36 | prompts, but it will be extended to support other MCP features in the future. At its core, MCP is all about text | ||
| 37 | processing, making it a great fit for Perl. | ||
| 38 | |||
| 39 | =head3 Streamable HTTP Transport | ||
| 40 | |||
| 41 | Use the L<MCP::Server/"to_action"> method to add an MCP endpoint to any L<Mojolicious> application. The tool name and | ||
| 42 | description are used for discovery, and the L<JSON schema|https://json-schema.org> is used to validate the input. | ||
| 43 | |||
| 44 | use Mojolicious::Lite -signatures; | ||
| 45 | |||
| 46 | use MCP::Server; | ||
| 47 | |||
| 48 | my $server = MCP::Server->new; | ||
| 49 | $server->tool( | ||
| 50 | name => 'echo', | ||
| 51 | description => 'Echo the input text', | ||
| 52 | input_schema => {type => 'object', properties => {msg => {type => 'string'}}, required => ['msg']}, | ||
| 53 | code => sub ($tool, $args) { | ||
| 54 | return "Echo: $args->{msg}"; | ||
| 55 | } | ||
| 56 | ); | ||
| 57 | |||
| 58 | any '/mcp' => $server->to_action; | ||
| 59 | |||
| 60 | app->start; | ||
| 61 | |||
| 62 | Authentication can be added by the web application, just like for any other route. OAuth scopes can be enforced per | ||
| 63 | tool, prompt and resource. To allow for MCP applications to scale with prefork web servers, server to client | ||
| 64 | streaming is currentlly avoided when possible. | ||
| 65 | |||
| 66 | =head3 Stdio Transport | ||
| 67 | |||
| 68 | Build local command line applications and use the stdio transport for testing with the L<MCP::Server/"to_stdio"> | ||
| 69 | method. | ||
| 70 | |||
| 71 | use Mojo::Base -strict, -signatures; | ||
| 72 | |||
| 73 | use MCP::Server; | ||
| 74 | |||
| 75 | my $server = MCP::Server->new; | ||
| 76 | $server->tool( | ||
| 77 | name => 'echo', | ||
| 78 | description => 'Echo the input text', | ||
| 79 | input_schema => {type => 'object', properties => {msg => {type => 'string'}}, required => ['msg']}, | ||
| 80 | code => sub ($tool, $args) { | ||
| 81 | return "Echo: $args->{msg}"; | ||
| 82 | } | ||
| 83 | ); | ||
| 84 | |||
| 85 | $server->to_stdio; | ||
| 86 | |||
| 87 | Just run the script and type requests on the command line. | ||
| 88 | |||
| 89 | $ perl examples/echo_stdio.pl | ||
| 90 | {"jsonrpc":"2.0","id":"1","method":"tools/list"} | ||
| 91 | {"jsonrpc":"2.0","id":"2","method":"tools/call","params":{"name":"echo","arguments":{"msg":"hello perl"}}} | ||
| 92 | |||
| 93 | =head1 COPYRIGHT AND LICENSE | ||
| 94 | |||
| 95 | Copyright (C) 2025-2026, Sebastian Riedel. | ||
| 96 | |||
| 97 | This program is free software, you can redistribute it and/or modify it under the terms of the MIT license. | ||
| 98 | |||
| 99 | =head1 SEE ALSO | ||
| 100 | |||
| 101 | L<Mojolicious>, L<https://mojolicious.org>, L<https://modelcontextprotocol.io>. | ||
| 102 | |||
| 103 | =cut |