MCP/Client.pm

filter: Statementstmt 94.2% 82 / 87 Branchbran 75.0% 21 / 28 Conditioncond 44.4% 4 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 91.3% 21 / 23 Podpod 100% 12 / 12 totaltotal 85.9% 141 / 164 CC 22 SCAR 12.8
CC
22
Cov
86%
send_request
7.2
__ANON__
7.1
delete_session
5.0
CRAP 3.6
« MCP/Server/Context.pm ↑ Summary MCP/Prompt.pm »
1 package MCP::Client;
2 4 ▶use Mojo::Base -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use MCP::Constants qw(PROTOCOL_VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Mojo::JSON qw(from_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 15 ▶has headers => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
11 4 has name => 'PerlClient';
12 4 has 'session_id';
13 0 ▶has ua => sub { Mojo::UserAgent->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
14 0 ▶has url => sub {'http://localhost:3000/mcp'};
Subroutine __ANON__: not called CC=1 SCAR=1.2
15 4 has version => '1.0.0';
16  
17 88 ▶sub build_request ($self, $method, $params = {}) {
Subroutine build_request: called CC=3 SCAR=11.0
Pod documented
18 88 my $request = $self->build_notification($method, $params);
19 88 ▶ $request->{id} = $self->{id} = $self->{id} ? $self->{id} + 1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'id'} ? : 70 18 88
20 88 return $request;
21 }
22  
23 105 ▶sub build_notification ($self, $method, $params = {}) {
Subroutine build_notification: called CC=2 SCAR=6.9
Pod documented
24 105 return {jsonrpc => '2.0', method => $method, params => $params};
25 }
26  
27 25 ▶sub call_tool ($self, $name, $args = {}) {
Subroutine call_tool: called CC=2 SCAR=6.9
Pod documented
28 25 my $request = $self->build_request('tools/call', {name => $name, arguments => $args});
29 25 return _result($self->send_request($request));
30 }
31  
32 ▶sub delete_session ($self) {
Subroutine delete_session: called CC=4 SCAR=16.1
Pod documented
33 10 ▶ return undef unless my $session_id = $self->session_id;
Branch: 1 branch
BranchTrueFalseTotal
unless my $session_id = $self->session_id 0 10 10
34 10 my $tx = $self->ua->build_tx(DELETE => $self->url => {%{$self->headers}, 'Mcp-Session-Id' => $session_id});
35 10 $tx = $self->ua->start($tx);
36 10 ▶ if (my $err = $tx->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $tx->error) 0 10 10
37 0 ▶ croak "$err->{code} response: $err->{message}" if $err->{code};
Branch: 1 branch
BranchTrueFalseTotal
if $err->{"code"} 0 0 0
38 0 croak "Connection error: $err->{message}";
39 }
40 10 $self->session_id(undef);
41 10 return 1;
42 }
43  
44 13 ▶sub get_prompt ($self, $name, $args = {}) {
Subroutine get_prompt: called CC=2 SCAR=6.9
Pod documented
45 13 my $request = $self->build_request('prompts/get', {name => $name, arguments => $args});
46 13 return _result($self->send_request($request));
47 }
48  
49 ▶sub initialize_session ($self) {
Subroutine initialize_session: called CC=1 SCAR=0.0
Pod documented
50 17 my $request = $self->build_request(
51 initialize => {
52 protocolVersion => PROTOCOL_VERSION,
53 capabilities => {},
54 clientInfo => {name => $self->name, version => $self->version,},
55 }
56 );
57 17 my $result = _result($self->send_request($request));
58 17 $self->send_request($self->build_notification('notifications/initialized'));
59 17 return $result;
60 }
61  
62 5 ▶sub list_prompts ($self) { _result($self->send_request($self->build_request('prompts/list'))) }
Subroutine list_prompts: called CC=1 SCAR=0.0
Pod documented
63 5 ▶sub list_resources ($self) { _result($self->send_request($self->build_request('resources/list'))) }
Subroutine list_resources: called CC=1 SCAR=0.0
Pod documented
64 6 ▶sub list_tools ($self) { _result($self->send_request($self->build_request('tools/list'))) }
Subroutine list_tools: called CC=1 SCAR=0.0
Pod documented
65 3 ▶sub ping ($self) { _result($self->send_request($self->build_request('ping'))) }
Subroutine ping: called CC=1 SCAR=0.0
Pod documented
66  
67 ▶sub read_resource ($self, $uri) {
Subroutine read_resource: called CC=1 SCAR=0.0
Pod documented
68 11 my $request = $self->build_request('resources/read', {uri => $uri});
69 11 return _result($self->send_request($request));
70 }
71  
72 ▶sub send_request ($self, $request) {
Subroutine send_request: called CC=7 SCAR=19.8
Pod documented
73 my $headers
74 105 = {%{$self->headers}, Accept => 'application/json, text/event-stream', 'Content-Type' => 'application/json'};
75 105 ▶ if (my $session_id = $self->session_id) { $headers->{'Mcp-Session-Id'} = $session_id }
Branch: 1 branch
BranchTrueFalseTotal
if (my $session_id = $self->session_id) 88 17 105
76 105 my $ua = $self->ua;
77 105 my $tx = $ua->build_tx(POST => $self->url => $headers => json => $request);
78  
79 # SSE handling
80 105 my $id = $request->{id};
81 105 my $response;
82 $tx->res->content->on(
83 sse => sub {
84 12 ▶ my ($content, $event) = @_;
Subroutine __ANON__: called CC=6 SCAR=19.6
85 12 ▶ return unless $event->{text} && (my $res = eval { from_json($event->{text}) });
Branch: 1 branch
BranchTrueFalseTotal
unless $event->{"text"} and my $res = eval { do { from_json($event->{"text"}) } } 6 6 12
Condition66%2/3
$event->{"text"} and my $res = eval { do { from_json($event->{"text"}) } }
!ll&&!rl&&r
606
MC/DC50%1/2
$event->{"text"} and my $res = eval { do { from_json($event->{"text"}) } }
$event->{"text"} my $res = eval { do { from_json($event->{"text"}) } }
Truth table66%2/3
A and B
A= $event->{"text"}B= my $res = eval { do { from_json($event->{"text"}) } }
ABresult
0X0
100
111
86 6 ▶ return unless defined($res->{id}) && defined($id) && $res->{id} eq $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res->{"id"} and defined $id and $res->{"id"} eq $id 0 6 6
Condition33%2/6
defined $res->{"id"} and defined $id and $res->{"id"} eq $id
!ll&&!rl&&r
006
defined $res->{"id"} and defined $id
!ll&&!rl&&r
006
MC/DC0%0/3
defined $res->{"id"} and defined $id and $res->{"id"} eq $id
defined $res->{"id"} defined $id $res->{"id"} eq $id
Truth table25%1/4
A and B and C
A= defined $res->{"id"}B= defined $idC= $res->{"id"} eq $id
ABCresult
0XX0
10X0
1100
1111
87 6 $response = $res;
88 6 $tx->res->error({message => 'Interrupted'});
89 }
90 105 );
91  
92 105 $tx = $ua->start($tx);
93  
94 105 ▶ if (my $session_id = $tx->res->headers->header('Mcp-Session-Id')) { $self->session_id($session_id) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $session_id = $tx->res->headers->header("Mcp-Session-Id")) 103 2 105
95  
96 # Request or notification accepted without a response
97 105 ▶ return undef if $tx->res->code eq '202';
Branch: 1 branch
BranchTrueFalseTotal
if $tx->res->code eq 202 17 88 105
98  
99 88 ▶ if (my $err = $tx->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $tx->error) 11 77 88
100 11 ▶ return $response if $err->{message} eq 'Interrupted';
Branch: 1 branch
BranchTrueFalseTotal
if $err->{"message"} eq "Interrupted" 6 5 11
101 5 ▶ croak "$err->{code} response: $err->{message}" if $err->{code};
Branch: 1 branch
BranchTrueFalseTotal
if $err->{"code"} 5 0 5
102 0 croak "Connection error: $err->{message}";
103 }
104  
105 77 return $tx->res->json;
106 }
107  
108 ▶sub _result ($res) {
Subroutine _result: called CC=3 SCAR=11.0
109 80 ▶ croak 'No response' unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 80 80
110 80 ▶ if (my $err = $res->{error}) { croak "Error $err->{code}: $err->{message}" }
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $res->{"error"}) 9 71 80
111 71 return $res->{result};
112 }
113  
114 4 1;
115  
116 =encoding utf8
117  
118 =head1 NAME
119  
120 MCP::Server::Transport::HTTP - HTTP transport for MCP servers
121  
122 =head1 SYNOPSIS
123  
124 use MCP::Client;
125  
126 my $client = MCP::Client->new(url => 'http://localhost:3000/mcp');
127 $client->initialize_session;
128 my $tools = $client->list_tools;
129  
130 =head1 DESCRIPTION
131  
132 L<MCP::Client> is a client for MCP (Model Context Protocol) that communicates with MCP servers over HTTP.
133  
134 =head1 ATTRIBUTES
135  
136 L<MCP::Client> inherits all attributes from L<Mojo::Base> and implements the following new ones.
137  
138 =head2 headers
139  
140 my $headers = $client->headers;
141 $client = $client->headers({Authorization => 'Bearer abc123'});
142  
143 Extra HTTP headers to send with every request, as a hash reference. Useful for passing an C<Authorization> header to
144 an MCP server that requires OAuth bearer authentication. Defaults to an empty hash reference.
145  
146 =head2 name
147  
148 my $name = $client->name;
149 $client = $client->name('PerlClient');
150  
151 The name of the client, defaults to C<PerlClient>.
152  
153 =head2 session_id
154  
155 my $session_id = $client->session_id;
156 $client = $client->session_id('12345');
157  
158 The session ID for the client, used to maintain state across requests.
159  
160 =head2 ua
161  
162 my $ua = $client->ua;
163 $client = $client->ua(Mojo::UserAgent->new);
164  
165 The user agent used for making HTTP requests, defaults to a new instance of L<Mojo::UserAgent>.
166  
167 =head2 url
168  
169 my $url = $client->url;
170 $client = $client->url('http://localhost:3000/mcp');
171  
172 The URL of the MCP server, defaults to C<http://localhost:3000/mcp>.
173  
174 =head2 version
175  
176 my $version = $client->version;
177 $client = $client->version('1.0.0');
178  
179 The version of the client, defaults to C<1.0.0>.
180  
181 =head1 METHODS
182  
183 L<MCP::Client> inherits all methods from L<Mojo::Base> and implements the following new ones.
184  
185 =head2 build_request
186  
187 my $request = $client->build_request('method_name', {param1 => 'value1'});
188  
189 Builds a JSON-RPC request with the given method name and parameters.
190  
191 =head2 build_notification
192  
193 my $notification = $client->build_notification('method_name', {param1 => 'value1'});
194  
195 Builds a JSON-RPC notification with the given method name and parameters.
196  
197 =head2 call_tool
198  
199 my $result = $client->call_tool('tool_name');
200 my $result = $client->call_tool('tool_name', {arg1 => 'value1'});
201  
202 Calls a tool on the MCP server with the specified name and arguments, returning the result.
203  
204 =head2 delete_session
205  
206 my $bool = $client->delete_session;
207  
208 Send a C<DELETE> request to terminate the current session on the MCP server, and clear the local
209 L</"session_id">. Returns true on success, or C<undef> if no session is active. The server only honors this when it
210 was configured with C<< streaming => 1 >>.
211  
212 =head2 get_prompt
213  
214 my $result = $client->get_prompt('prompt_name');
215 my $result = $client->get_prompt('prompt_name', {arg1 => 'value1'});
216  
217 Get a prompt from the MCP server with the specified name and arguments, returning the result.
218  
219 =head2 initialize_session
220  
221 my $result = $client->initialize_session;
222  
223 Initializes a session with the MCP server, setting up the protocol version and client information.
224  
225 =head2 list_prompts
226  
227 my $prompts = $client->list_prompts;
228  
229 Lists all available prompts on the MCP server.
230  
231 =head2 list_resources
232  
233 my $resources = $client->list_resources;
234  
235 Lists all available resources on the MCP server.
236  
237 =head2 list_tools
238  
239 my $tools = $client->list_tools;
240  
241 Lists all available tools on the MCP server.
242  
243 =head2 ping
244  
245 my $result = $client->ping;
246  
247 Sends a ping request to the MCP server to check connectivity.
248  
249 =head2 read_resource
250  
251 my $result = $client->read_resource('file:///path/to/resource.txt');
252  
253 Reads a resource from the MCP server with the specified URI, returning the result.
254  
255 =head2 send_request
256  
257 my $response = $client->send_request($request);
258  
259 Sends a JSON-RPC request to the MCP server and returns the response.
260  
261 =head1 SEE ALSO
262  
263 L<MCP>, L<https://mojolicious.org>, L<https://modelcontextprotocol.io>.
264  
265 =cut
« MCP/Server/Context.pm ↑ Summary MCP/Prompt.pm »