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