| 1 | package Net::Async::MCP::Transport::InProcess; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: In-process MCP transport via direct MCP::Server calls | ||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Future; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 26 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 26 | my ( $class, %args ) = @_; | |||||||||||||||||||||||||||
| 13 | 26 | ▶ | croak "server is required" unless $args{server}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | return bless { | ||||||||||||||||||||||||||||
| 15 | server => $args{server}, | ||||||||||||||||||||||||||||
| 16 | 26 | next_id => 0, | |||||||||||||||||||||||||||
| 17 | }, $class; | ||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 55 | ▶ | sub send_request { | ||||||||||||||||||||||||||
|
Subroutine
send_request: called CC=6 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | # %options: binding hints from the client, none of which apply in process | ||||||||||||||||||||||||||||
| 23 | 55 | my ( $self, $method, $params, %options ) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 55 | my $id = ++$self->{next_id}; | |||||||||||||||||||||||||||
| 26 | 55 | ▶ | my $request = { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | jsonrpc => '2.0', | ||||||||||||||||||||||||||||
| 28 | id => $id, | ||||||||||||||||||||||||||||
| 29 | method => $method, | ||||||||||||||||||||||||||||
| 30 | defined $params ? ( params => $params ) : (), | ||||||||||||||||||||||||||||
| 31 | }; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 55 | my $response = $self->{server}->handle($request, $self->_context); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Handle Mojo::Promise from async MCP tools | ||||||||||||||||||||||||||||
| 36 | 55 | ▶ | if (blessed($response) && $response->isa('Mojo::Promise')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($response) and $response->isa("Mojo::Promise")
MC/DC0%0/2
builtin::blessed($response) and $response->isa("Mojo::Promise")
builtin::blessed($response)
$response->isa("Mojo::Promise")
Truth table66%2/3
A and B
A= builtin::blessed($response)B= $response->isa("Mojo::Promise")
| |||||||||||||||||||||||||||||
| 37 | 0 | my ( $resolved, $error ); | |||||||||||||||||||||||||||
| 38 | $response->then( | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub { $resolved = $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub { $error = $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 41 | 0 | )->wait; | |||||||||||||||||||||||||||
| 42 | 0 | ▶ | return Future->fail("MCP async tool error: $error") if $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | $response = $resolved; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # A JSON-RPC response is plain data, so anything still blessed here is a | ||||||||||||||||||||||||||||
| 47 | # return shape only a transport can serve. MCP::Server hands back an | ||||||||||||||||||||||||||||
| 48 | # MCP::Server::Subscription for subscriptions/listen once the server has a | ||||||||||||||||||||||||||||
| 49 | # notification capable transport of its own, expecting it to be turned into a | ||||||||||||||||||||||||||||
| 50 | # notification stream. In process there is nothing to stream over, and that | ||||||||||||||||||||||||||||
| 51 | # is our limitation to report, not a malformed response from the server. | ||||||||||||||||||||||||||||
| 52 | 55 | ▶ | if (blessed $response) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 1 | return Future->fail( | |||||||||||||||||||||||||||
| 54 | "MCP server returned a " . ref($response) . " for '$method' instead of a " | ||||||||||||||||||||||||||||
| 55 | . "JSON-RPC response: the in-process transport cannot carry " | ||||||||||||||||||||||||||||
| 56 | . "server-initiated notifications, so subscriptions/listen is not usable " | ||||||||||||||||||||||||||||
| 57 | . "here"); | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 54 | return $self->_process_response($response); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | sub send_notification { | ||||||||||||||||||||||||||
|
Subroutine
send_notification: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 1 | my ( $self, $method, $params ) = @_; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | my $request = { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | jsonrpc => '2.0', | ||||||||||||||||||||||||||||
| 69 | method => $method, | ||||||||||||||||||||||||||||
| 70 | defined $params ? ( params => $params ) : (), | ||||||||||||||||||||||||||||
| 71 | }; | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 1 | $self->{server}->handle($request, $self->_context); | |||||||||||||||||||||||||||
| 74 | 1 | return Future->done; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub close { Future->done } | ||||||||||||||||||||||||||
|
Subroutine
close: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | ▶ | sub is_alive { 1 } | ||||||||||||||||||||||||||
|
Subroutine
is_alive: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 16 | ▶ | sub mirrors_header_params { 0 } | ||||||||||||||||||||||||||
|
Subroutine
mirrors_header_params: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 56 | ▶ | sub _context { | ||||||||||||||||||||||||||
|
Subroutine
_context: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 88 | 56 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Loaded here rather than at compile time because MCP is a recommendation of | ||||||||||||||||||||||||||||
| 91 | # this distribution and not a requirement, and a use would have made every | ||||||||||||||||||||||||||||
| 92 | # consumer of this transport depend on it. Returning nothing where it is | ||||||||||||||||||||||||||||
| 93 | # missing hands handle() the request alone, which is the whole call an object | ||||||||||||||||||||||||||||
| 94 | # that is not an MCP::Server was ever going to get - see the DESCRIPTION. | ||||||||||||||||||||||||||||
| 95 | 25 | $self->{has_context} = eval { require MCP::Server::Context; 1 } || 0 | |||||||||||||||||||||||||||
| 96 | 56 | ▶ | unless defined $self->{has_context}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
eval {
do {
require MCP::Server::Context;
1
}
} || 0
MC/DC0%0/1
eval {
do {
require MCP::Server::Context;
1
}
} || 0
eval {
do {
require MCP::Server::Context;
1
}
}
Truth table50%1/2
A
A= eval {
do {
require MCP::Server::Context;
1
}
}
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 56 | ▶ | return unless $self->{has_context}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 56 | return MCP::Server::Context->new; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 54 | ▶ | sub _process_response { | ||||||||||||||||||||||||||
|
Subroutine
_process_response: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 103 | 54 | my ( $self, $response ) = @_; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 54 | ▶ | return Future->fail("No response from MCP server") unless $response; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 53 | ▶ | return Future->fail("Invalid response from MCP server") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | unless ref $response eq 'HASH'; | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # The message alone cannot carry a code to switch on or an error->{data} to | ||||||||||||||||||||||||||||
| 110 | # read, so the raw JSON-RPC error object travels with it as the details of a | ||||||||||||||||||||||||||||
| 111 | # failure in category "mcp". The message stays the first element, so a caller | ||||||||||||||||||||||||||||
| 112 | # reading the failure in scalar context sees exactly what it saw before. | ||||||||||||||||||||||||||||
| 113 | 53 | ▶ | if (my $err = $response->{error}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 8 | return Future->fail("MCP error $err->{code}: $err->{message}", mcp => $err); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 45 | return Future->done($response->{result}); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 5 | 1; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||