| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
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")
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
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
}
}
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 63 | ▶ | return unless $self->{has_context}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 108 | 60 | ▶ | return Future->fail("Invalid response from MCP server") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 118 | 13 | my @failure = $self->_jsonrpc_class->error_failure($response); | |||||||||||||||||||||||||||
| 119 | 13 | ▶ | return Future->fail(@failure) if @failure; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
exists $response->{"error"} and not exists $response->{"result"}
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"}
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||