blib/lib/Net/Async/MCP/Transport/InProcess.pm

filter: Statementstmt 88.1% 52 / 59 Branchbran 73.0% 19 / 26 Conditioncond 75.0% 6 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 82.3% 14 / 17 Podpod 100% 6 / 6 totaltotal 81.8% 99 / 121 CC 17 SCAR 14.3
CC
17
Cov
83%
send_request
8.3
_process_response
7.0
_context
4.1
CRAP 4.2
« blib/lib/Net/Async/MCP/Transport/Stdio.pm ↑ Summary blib/lib/Net/Async/MCP/JSONRPC.pm »
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 5 ▶use Net::Async::MCP::JSONRPC;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 27 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
14 27 my ( $class, %args ) = @_;
15 27 ▶ croak "server is required" unless $args{server};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"server"} 0 27 27
16 return bless {
17 server => $args{server},
18 27 next_id => 0,
19 }, $class;
20 }
21  
22  
23 62 ▶sub send_request {
Subroutine send_request: called CC=6 SCAR=21.2
Pod documented
24 # %options: binding hints from the client, none of which apply in process
25 62 my ( $self, $method, $params, %options ) = @_;
26  
27 62 my $id = ++$self->{next_id};
28 62 ▶ my $request = {
Branch: 1 branch
BranchTrueFalseTotal
defined $params ? : 52 10 62
29 jsonrpc => '2.0',
30 id => $id,
31 method => $method,
32 defined $params ? ( params => $params ) : (),
33 };
34  
35 62 my $response = $self->{server}->handle($request, $self->_context);
36  
37 # Handle Mojo::Promise from async MCP tools
38 62 ▶ if (blessed($response) && $response->isa('Mojo::Promise')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($response) and $response->isa("Mojo::Promise")) 0 62 62
Condition66%2/3
builtin::blessed($response) and $response->isa("Mojo::Promise")
!ll&&!rl&&r
6110
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")
ABresult
0X0
100
111
39 0 my ( $resolved, $error );
40 $response->then(
41 0 ▶ sub { $resolved = $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 0 ▶ sub { $error = $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
43 0 )->wait;
44 0 ▶ return Future->fail("MCP async tool error: $error") if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
45 0 $response = $resolved;
46 }
47  
48 # A JSON-RPC response is plain data, so anything still blessed here is a
49 # return shape only a transport can serve. MCP::Server hands back an
50 # MCP::Server::Subscription for subscriptions/listen once the server has a
51 # notification capable transport of its own, expecting it to be turned into a
52 # notification stream. In process there is nothing to stream over, and that
53 # is our limitation to report, not a malformed response from the server.
54 62 ▶ if (blessed $response) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($response)) 1 61 62
55 1 return Future->fail(
56 "MCP server returned a " . ref($response) . " for '$method' instead of a "
57 . "JSON-RPC response: the in-process transport cannot carry "
58 . "server-initiated notifications, so subscriptions/listen is not usable "
59 . "here");
60 }
61  
62 61 return $self->_process_response($response);
63 }
64  
65  
66 1 ▶sub send_notification {
Subroutine send_notification: called CC=2 SCAR=7.0
Pod documented
67 1 my ( $self, $method, $params ) = @_;
68  
69 1 ▶ my $request = {
Branch: 1 branch
BranchTrueFalseTotal
defined $params ? : 0 1 1
70 jsonrpc => '2.0',
71 method => $method,
72 defined $params ? ( params => $params ) : (),
73 };
74  
75 1 $self->{server}->handle($request, $self->_context);
76 1 return Future->done;
77 }
78  
79  
80 0 ▶sub close { Future->done }
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
81  
82  
83 1 ▶sub is_alive { 1 }
Subroutine is_alive: called CC=1 SCAR=0.0
Pod documented
84  
85  
86 16 ▶sub mirrors_header_params { 0 }
Subroutine mirrors_header_params: called CC=1 SCAR=0.0
Pod documented
87  
88  
89 63 ▶sub _context {
Subroutine _context: called CC=4 SCAR=14.0
90 63 my ( $self ) = @_;
91  
92 # Loaded here rather than at compile time because MCP is a recommendation of
93 # this distribution and not a requirement, and a use would have made every
94 # consumer of this transport depend on it. Returning nothing where it is
95 # missing hands handle() the request alone, which is the whole call an object
96 # that is not an MCP::Server was ever going to get - see the DESCRIPTION.
97 26 $self->{has_context} = eval { require MCP::Server::Context; 1 } || 0
98 63 ▶ unless defined $self->{has_context};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"has_context"} 26 37 63
Condition50%1/2
eval { do { require MCP::Server::Context; 1 } } || 0
l!l
260
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 } }
Aresult
11
00
99  
100 63 ▶ return unless $self->{has_context};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"has_context"} 0 63 63
101 63 return MCP::Server::Context->new;
102 }
103  
104 61 ▶sub _process_response {
Subroutine _process_response: called CC=7 SCAR=19.5
105 61 my ( $self, $response ) = @_;
106  
107 61 ▶ return Future->fail("No response from MCP server") unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 1 60 61
108 60 ▶ return Future->fail("Invalid response from MCP server")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $response eq "HASH" 0 60 60
109 unless ref $response eq 'HASH';
110  
111 # The raw JSON-RPC error object travels with the message as the details of a
112 # failure in category "mcp" - see Net::Async::MCP::JSONRPC, which the wire
113 # transports read their answers with too. MCP::Server always renders one,
114 # but the server object may be anything with a handle(), so an error in any
115 # other shape still says the request failed, with the text it carries and no
116 # category.
117 60 ▶ if (defined(my $err = $response->{error})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $err = $response->{"error"})) 13 47 60
118 13 my @failure = $self->_jsonrpc_class->error_failure($response);
119 13 ▶ return Future->fail(@failure) if @failure;
Branch: 1 branch
BranchTrueFalseTotal
if @failure 8 5 13
120 5 return Future->fail('MCP in-process error response: '.$self->_jsonrpc_class->error_text($err));
121 }
122  
123 # "error": null sits next to every result in JSON-RPC 1.0 and says nothing
124 # there. Without a result it answers nothing, and an undef handed out as the
125 # result would be a success the server never reported.
126 return Future->fail('MCP in-process invalid response: a null error and no result')
127 47 ▶ if exists $response->{error} && !exists $response->{result};
Branch: 1 branch
BranchTrueFalseTotal
if exists $response->{"error"} and not exists $response->{"result"} 1 46 47
Condition100%3/3
exists $response->{"error"} and not exists $response->{"result"}
!ll&&!rl&&r
4511
MC/DC100%2/2
exists $response->{"error"} and not exists $response->{"result"}
exists $response->{"error"} not exists $response->{"result"}
Truth table100%3/3
A and not(B)
A= exists $response->{"error"}B= not exists $response->{"result"}
ABresult
0X0
100
111
128  
129 46 return Future->done($response->{result});
130 }
131  
132 # Private: the class that reads the error of a response, named once.
133 18 ▶sub _jsonrpc_class { 'Net::Async::MCP::JSONRPC' }
Subroutine _jsonrpc_class: called CC=1 SCAR=0.0
134  
135  
136 5 1;
137  
138 __END__
« blib/lib/Net/Async/MCP/Transport/Stdio.pm ↑ Summary blib/lib/Net/Async/MCP/JSONRPC.pm »