| 1 | package Net::Async::MCP::Server; | ||||||||||||||||||||||
| 2 | # ABSTRACT: Async MCP server base class | ||||||||||||||||||||||
| 3 | |||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | 2 | ▶ | use parent 'IO::Async::Notifier'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | 2 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | 2 | our $VERSION = '0.001'; | |||||||||||||||||||||
| 12 | |||||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | 15 | ▶ | sub _init { | ||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 15 | 15 | my ( $self, $params ) = @_; | |||||||||||||||||||||
| 16 | 15 | $self->SUPER::_init($params); | |||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | 15 | ▶ | sub configure { | ||||||||||||||||||||
|
Subroutine
configure: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 20 | 15 | my ( $self, %params ) = @_; | |||||||||||||||||||||
| 21 | 15 | ▶ | if (exists $params{name}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 22 | 2 | $self->{name} = delete $params{name}; | |||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||
| 25 | |||||||||||||||||||||||
| 26 | 0 | ▶ | sub _add_to_loop { | ||||||||||||||||||||
|
Subroutine
_add_to_loop: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 27 | 0 | my ( $self, $loop ) = @_; | |||||||||||||||||||||
| 28 | 0 | $self->SUPER::_add_to_loop($loop); | |||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||
| 30 | |||||||||||||||||||||||
| 31 | 5 | ▶ | sub name { shift->{name} // 'NetAsyncMCPServer' } | ||||||||||||||||||||
|
Subroutine
name: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
+(shift())->{"name"} // "NetAsyncMCPServer"
MC/DC100%1/1
+(shift())->{"name"} // "NetAsyncMCPServer"
+(shift())->{"name"}
Truth table100%2/2
A
A= +(shift())->{"name"}
| |||||||||||||||||||||||
| 32 | |||||||||||||||||||||||
| 33 | |||||||||||||||||||||||
| 34 | 3 | ▶ | sub server_info { | ||||||||||||||||||||
|
Subroutine
server_info: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 35 | 3 | my ( $self ) = @_; | |||||||||||||||||||||
| 36 | return { | ||||||||||||||||||||||
| 37 | 3 | name => $self->name, | |||||||||||||||||||||
| 38 | version => $VERSION, | ||||||||||||||||||||||
| 39 | }; | ||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||
| 41 | |||||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | 3 | ▶ | sub server_capabilities { | ||||||||||||||||||||
|
Subroutine
server_capabilities: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 44 | 3 | my ( $self ) = @_; | |||||||||||||||||||||
| 45 | 3 | ▶ | return $self->{server_capabilities} // {}; | ||||||||||||||||||||
|
Condition100%2/2
$self->{"server_capabilities"} // {}
MC/DC100%1/1
$self->{"server_capabilities"} // {}
$self->{"server_capabilities"}
Truth table100%2/2
A
A= $self->{"server_capabilities"}
| |||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||
| 47 | |||||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | 2 | ▶ | async sub initialize { | ||||||||||||||||||||
|
Subroutine
initialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 50 | 2 | my ( $self ) = @_; | |||||||||||||||||||||
| 51 | |||||||||||||||||||||||
| 52 | 2 | $self->{server_capabilities} = $self->_build_capabilities; | |||||||||||||||||||||
| 53 | 2 | $self->{_initialized} = 1; | |||||||||||||||||||||
| 54 | |||||||||||||||||||||||
| 55 | return { | ||||||||||||||||||||||
| 56 | 2 | protocolVersion => '2025-11-25', | |||||||||||||||||||||
| 57 | capabilities => $self->server_capabilities, | ||||||||||||||||||||||
| 58 | serverInfo => $self->server_info, | ||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||
| 60 | 2 | } | |||||||||||||||||||||
| 61 | |||||||||||||||||||||||
| 62 | |||||||||||||||||||||||
| 63 | 2 | ▶ | sub _build_capabilities { | ||||||||||||||||||||
|
Subroutine
_build_capabilities: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 64 | 2 | my ( $self ) = @_; | |||||||||||||||||||||
| 65 | 2 | return { tools => {} }; | |||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||
| 67 | |||||||||||||||||||||||
| 68 | 3 | ▶ | sub tools { | ||||||||||||||||||||
|
Subroutine
tools: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||
| 69 | 3 | my ( $self ) = @_; | |||||||||||||||||||||
| 70 | 3 | ▶ | return $self->{tools} // []; | ||||||||||||||||||||
|
Condition50%1/2
$self->{"tools"} // []
MC/DC0%0/1
$self->{"tools"} // []
$self->{"tools"}
Truth table50%1/2
A
A= $self->{"tools"}
| |||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||
| 72 | |||||||||||||||||||||||
| 73 | |||||||||||||||||||||||
| 74 | 6 | ▶ | sub register_tool { | ||||||||||||||||||||
|
Subroutine
register_tool: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 75 | 6 | my ( $self, %tool ) = @_; | |||||||||||||||||||||
| 76 | 6 | ▶ | $self->{tools} //= []; | ||||||||||||||||||||
|
Condition100%2/2
$self->{"tools"} //= []
MC/DC100%1/1
$self->{"tools"} //= []
$self->{"tools"}
Truth table100%2/2
A
A= $self->{"tools"}
| |||||||||||||||||||||||
| 77 | 6 | push @{ $self->{tools} }, \%tool; | |||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||
| 79 | |||||||||||||||||||||||
| 80 | |||||||||||||||||||||||
| 81 | 2 | ▶ | async sub list_tools { | ||||||||||||||||||||
|
Subroutine
list_tools: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 82 | 2 | my ( $self ) = @_; | |||||||||||||||||||||
| 83 | 2 | return $self->tools; | |||||||||||||||||||||
| 84 | 2 | } | |||||||||||||||||||||
| 85 | |||||||||||||||||||||||
| 86 | |||||||||||||||||||||||
| 87 | 1 | ▶ | async sub call_tool { | ||||||||||||||||||||
|
Subroutine
call_tool: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||||||||||||||
| 88 | 1 | my ( $self, $name, $arguments ) = @_; | |||||||||||||||||||||
| 89 | |||||||||||||||||||||||
| 90 | 1 | my $tool = $self->_find_tool($name); | |||||||||||||||||||||
| 91 | 1 | ▶ | die "No tool registered: $name" unless $tool; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 92 | |||||||||||||||||||||||
| 93 | 1 | ▶ | if (my $code = $tool->{code}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 94 | 1 | return $code->($arguments); | |||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||
| 96 | |||||||||||||||||||||||
| 97 | 0 | die "Tool '$name' has no implementation"; | |||||||||||||||||||||
| 98 | 2 | } | |||||||||||||||||||||
| 99 | |||||||||||||||||||||||
| 100 | 1 | ▶ | sub _find_tool { | ||||||||||||||||||||
|
Subroutine
_find_tool: called CC=4 SCAR=15.2
| |||||||||||||||||||||||
| 101 | 1 | my ( $self, $name ) = @_; | |||||||||||||||||||||
| 102 | 1 | ▶ | for my $tool (@{ $self->{tools} // [] }) { | ||||||||||||||||||||
|
Condition50%1/2
$self->{"tools"} // []
MC/DC0%0/1
$self->{"tools"} // []
$self->{"tools"}
Truth table50%1/2
A
A= $self->{"tools"}
| |||||||||||||||||||||||
| 103 | 1 | ▶ | return $tool if $tool->{name} eq $name; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||
| 105 | 0 | return undef; | |||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||
| 107 | |||||||||||||||||||||||
| 108 | |||||||||||||||||||||||
| 109 | 8 | ▶ | sub handle { | ||||||||||||||||||||
|
Subroutine
handle: called CC=12 SCAR=25.9
Pod
undocumented
| |||||||||||||||||||||||
| 110 | 8 | my ( $self, $request, $context ) = @_; | |||||||||||||||||||||
| 111 | |||||||||||||||||||||||
| 112 | 8 | ▶ | return _jsonrpc_error( -32700, 'Invalid JSON-RPC request' ) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 113 | unless ref $request eq 'HASH'; | ||||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | 8 | my $method = $request->{method}; | |||||||||||||||||||||
| 116 | 8 | my $id = $request->{id}; | |||||||||||||||||||||
| 117 | |||||||||||||||||||||||
| 118 | 8 | ▶ | return _jsonrpc_error( -32600, 'Missing JSON-RPC method', $id ) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 119 | unless defined $method; | ||||||||||||||||||||||
| 120 | |||||||||||||||||||||||
| 121 | 6 | ▶ | if ( defined $id ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 122 | 5 | ▶ | if ( $method eq 'initialize' ) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 123 | 1 | ▶ | return $self->_handle_initialize( $request->{params} // {}, $id ); | ||||||||||||||||||||
|
Condition50%1/2
$request->{"params"} // {}
MC/DC0%0/1
$request->{"params"} // {}
$request->{"params"}
Truth table50%1/2
A
A= $request->{"params"}
| |||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||
| 125 | elsif ( $method eq 'tools/list' ) { | ||||||||||||||||||||||
| 126 | 1 | return $self->_handle_tools_list( $id ); | |||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||
| 128 | elsif ( $method eq 'tools/call' ) { | ||||||||||||||||||||||
| 129 | 1 | ▶ | return $self->_handle_tools_call( $request->{params} // {}, $id ); | ||||||||||||||||||||
|
Condition50%1/2
$request->{"params"} // {}
MC/DC0%0/1
$request->{"params"} // {}
$request->{"params"}
Truth table50%1/2
A
A= $request->{"params"}
| |||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||
| 131 | elsif ( $method eq 'ping' ) { | ||||||||||||||||||||||
| 132 | 1 | return _jsonrpc_response( {}, $id ); | |||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||
| 134 | |||||||||||||||||||||||
| 135 | 1 | return _jsonrpc_error( -32601, "Method '$method' not found", $id ); | |||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||
| 137 | |||||||||||||||||||||||
| 138 | 1 | ▶ | if ( $method eq 'notifications/initialized' ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 139 | 1 | return undef; | |||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||
| 141 | elsif ( $method eq 'shutdown' ) { | ||||||||||||||||||||||
| 142 | 0 | return undef; | |||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||
| 144 | |||||||||||||||||||||||
| 145 | 0 | return undef; | |||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||
| 147 | |||||||||||||||||||||||
| 148 | 1 | ▶ | sub _handle_initialize { | ||||||||||||||||||||
|
Subroutine
_handle_initialize: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 149 | 1 | my ( $self, $params, $id ) = @_; | |||||||||||||||||||||
| 150 | |||||||||||||||||||||||
| 151 | 1 | my $result = $self->initialize->get; | |||||||||||||||||||||
| 152 | |||||||||||||||||||||||
| 153 | 1 | return _jsonrpc_response( $result, $id ); | |||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||
| 155 | |||||||||||||||||||||||
| 156 | 1 | ▶ | sub _handle_tools_list { | ||||||||||||||||||||
|
Subroutine
_handle_tools_list: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 157 | 1 | my ( $self, $id ) = @_; | |||||||||||||||||||||
| 158 | |||||||||||||||||||||||
| 159 | 1 | my $tools = $self->list_tools->get; | |||||||||||||||||||||
| 160 | 1 | return _jsonrpc_response( { tools => $tools }, $id ); | |||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||
| 162 | |||||||||||||||||||||||
| 163 | 1 | ▶ | sub _handle_tools_call { | ||||||||||||||||||||
|
Subroutine
_handle_tools_call: called CC=3 SCAR=11.3
| |||||||||||||||||||||||
| 164 | 1 | my ( $self, $params, $id ) = @_; | |||||||||||||||||||||
| 165 | |||||||||||||||||||||||
| 166 | 1 | ▶ | my $name = $params->{name} // ''; | ||||||||||||||||||||
|
Condition50%1/2
$params->{"name"} // ""
MC/DC0%0/1
$params->{"name"} // ""
$params->{"name"}
Truth table50%1/2
A
A= $params->{"name"}
| |||||||||||||||||||||||
| 167 | 1 | ▶ | my $args = $params->{arguments} // {}; | ||||||||||||||||||||
|
Condition50%1/2
$params->{"arguments"} // {}
MC/DC0%0/1
$params->{"arguments"} // {}
$params->{"arguments"}
Truth table50%1/2
A
A= $params->{"arguments"}
| |||||||||||||||||||||||
| 168 | |||||||||||||||||||||||
| 169 | 1 | my $result = $self->call_tool( $name, $args )->get; | |||||||||||||||||||||
| 170 | 1 | return _jsonrpc_response( $result, $id ); | |||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||
| 172 | |||||||||||||||||||||||
| 173 | 3 | ▶ | sub _jsonrpc_error { | ||||||||||||||||||||
|
Subroutine
_jsonrpc_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 174 | 3 | my ( $code, $message, $id ) = @_; | |||||||||||||||||||||
| 175 | return { | ||||||||||||||||||||||
| 176 | 3 | jsonrpc => '2.0', | |||||||||||||||||||||
| 177 | id => $id, | ||||||||||||||||||||||
| 178 | error => { code => $code, message => $message }, | ||||||||||||||||||||||
| 179 | }; | ||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||
| 181 | |||||||||||||||||||||||
| 182 | 4 | ▶ | sub _jsonrpc_response { | ||||||||||||||||||||
|
Subroutine
_jsonrpc_response: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 183 | 4 | my ( $result, $id ) = @_; | |||||||||||||||||||||
| 184 | 4 | return { jsonrpc => '2.0', id => $id, result => $result }; | |||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||
| 186 | |||||||||||||||||||||||
| 187 | 2 | 1; | |||||||||||||||||||||
| 188 | |||||||||||||||||||||||
| 189 | __END__ | ||||||||||||||||||||||