Agent/MCP/SDKBridge.pm

filter: Statementstmt 95.0% 77 / 81 Branchbran 92.8% 39 / 42 Conditioncond 62.5% 15 / 24 MC/DCmcdc 40.0% 6 / 15 Subroutinesub 93.7% 15 / 16 Podpod 100% 2 / 2 totaltotal 85.5% 154 / 180 CC 33 SCAR 20.5
CC
33
Cov
89%
__ANON__
13.7
handle
11.0
_call
6.4
CRAP 7.8
« Agent/Hook/Registry.pm ↑ Summary Agent/Message/Assistant.pm »
1 package Claude::Agent::MCP::SDKBridge;
2  
3 10 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use Object::Proto ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Claude::Agent::Logger '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 Object::Proto::define('Claude::Agent::MCP::SDKBridge',
13 'server:required',
14 'loop:predicate',
15 'tool_timeout:Num:predicate',
16 );
17 }
18  
19 10 my $PROTOCOL_VERSION = '2024-11-05';
20  
21 23 ▶sub handle {
Subroutine handle: called CC=11 SCAR=24.0
Pod documented
22 23 my ($self, $message) = @_;
23  
24 23 ▶ return Future->done($self->_error(undef, -32600, 'Invalid request'))
Branch: 1 branch
BranchTrueFalseTotal
unless ref $message eq "HASH" 1 22 23
25 unless ref $message eq 'HASH';
26  
27 22 ▶ my $method = $message->{method} // '';
Condition100%2/2
$message->{"method"} // ""
l!l
211
MC/DC100%1/1
$message->{"method"} // ""
$message->{"method"}
Truth table100%2/2
A
A= $message->{"method"}
Aresult
11
00
28 22 my $id = $message->{id};
29 22 ▶ my $params = ref $message->{params} eq 'HASH' ? $message->{params} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $message->{'params'} eq 'HASH' ? : 14 8 22
30  
31 22 ▶ return Future->done(undef) if $method =~ m{^notifications/};
Branch: 1 branch
BranchTrueFalseTotal
if $method =~ m[^notifications/]u 2 20 22
32 20 ▶ return Future->done(undef) if !length $method;
Branch: 1 branch
BranchTrueFalseTotal
unless length $method 1 19 20
33  
34 19 $log->debug(sprintf("SDKBridge: %s %s", $self->server->name, $method));
35  
36 19 ▶ if ($method eq 'initialize') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "initialize") 4 15 19
37 return Future->done($self->_result($id, {
38 4 ▶ protocolVersion => $params->{protocolVersion} // $PROTOCOL_VERSION,
Condition66%2/3
$params->{"protocolVersion"} // $PROTOCOL_VERSION
l!l&&r!l&&!r
220
MC/DC0%0/2
$params->{"protocolVersion"} // $PROTOCOL_VERSION
$params->{"protocolVersion"} $PROTOCOL_VERSION
Truth table66%2/3
A // B
A= $params->{"protocolVersion"}B= $PROTOCOL_VERSION
ABresult
1X1
011
000
39 capabilities => { tools => {} },
40 serverInfo => {
41 name => $self->server->name,
42 version => $self->server->version,
43 },
44 }));
45 }
46 15 ▶ if ($method eq 'ping') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "ping") 1 14 15
47 1 return Future->done($self->_result($id, {}));
48 }
49 14 ▶ if ($method eq 'tools/list') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "tools/list") 2 12 14
50 return Future->done($self->_result($id, {
51 2 tools => [ map { $_->to_hash } @{$self->server->tools} ],
52 }));
53 }
54 12 ▶ if ($method eq 'tools/call') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "tools/call") 10 2 12
55 10 return $self->_call($id, $params);
56 }
57 2 return Future->done($self->_error($id, -32601, "Method not found: $method"));
58 }
59  
60 10 ▶sub _call {
Subroutine _call: called CC=6 SCAR=18.5
61 10 my ($self, $id, $params) = @_;
62  
63 10 my $name = $params->{name};
64 10 ▶ my $tool = $self->server->get_tool($name)
Branch: 1 branch
BranchTrueFalseTotal
my $tool = $self->server->get_tool($name) or return "Future"->done($self->_error($id, -32601, 'Unknown tool: ' . _safe($name))) 9 1 10
65 or return Future->done($self->_error($id, -32601, 'Unknown tool: ' . _safe($name)));
66  
67 9 ▶ my $args = ref $params->{arguments} eq 'HASH' ? $params->{arguments} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $params->{'arguments'} eq 'HASH' ? : 8 1 9
68 9 ▶ my $loop = $self->has_loop ? $self->loop : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_loop ? : 8 1 9
69  
70 9 my $future = $tool->execute($args, $loop);
71  
72 9 ▶ if ($loop && !$future->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loop and not $future->is_ready) 2 7 9
Condition100%3/3
$loop and not $future->is_ready
!ll&&!rl&&r
162
MC/DC100%2/2
$loop and not $future->is_ready
$loop not $future->is_ready
Truth table100%3/3
A and not(B)
A= $loopB= not $future->is_ready
ABresult
0X0
100
111
73 2 my $timeout = $self->timeout_seconds;
74 $future = Future->wait_any(
75 $future,
76 1 ▶ $loop->timeout_future(after => $timeout)->else(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
77 1 $log->debug(sprintf("SDKBridge: tool %s timed out after %ss", $name, $timeout));
78 1 return Future->done({
79 content => [{ type => 'text', text => "Tool $name timed out after ${timeout}s" }],
80 is_error => 1,
81 });
82 2 }),
83 );
84 }
85  
86 9 ▶ return $future->then(sub {
Subroutine __ANON__: called CC=11 SCAR=26.2
87 9 my ($result) = @_;
88 9 ▶ my $valid = ref $result eq 'HASH' && ref $result->{content} eq 'ARRAY';
Condition66%2/3
ref $result eq 'HASH' && ref $result->{'content'} eq 'ARRAY'
!ll&&!rl&&r
018
MC/DC50%1/2
ref $result eq 'HASH' && ref $result->{'content'} eq 'ARRAY'
ref $result eq 'HASH' ref $result->{'content'} eq 'ARRAY'
Truth table66%2/3
A && B
A= ref $result eq 'HASH'B= ref $result->{'content'} eq 'ARRAY'
ABresult
0X0
100
111
89 9 ▶ if ($valid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid) 8 1 9
90 8 for my $block (@{$result->{content}}) {
91 8 ▶ next if ref $block eq 'HASH' && defined $block->{type};
Branch: 1 branch
BranchTrueFalseTotal
if ref $block eq "HASH" and defined $block->{"type"} 8 0 8
Condition33%1/3
ref $block eq "HASH" and defined $block->{"type"}
!ll&&!rl&&r
008
MC/DC0%0/2
ref $block eq "HASH" and defined $block->{"type"}
ref $block eq "HASH" defined $block->{"type"}
Truth table33%1/3
A and B
A= ref $block eq "HASH"B= defined $block->{"type"}
ABresult
0X0
100
111
92 0 $valid = 0;
93 0 last;
94 }
95 }
96 $result = {
97 9 ▶ content => [{ type => 'text', text => 'Invalid handler result format' }],
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 1 8 9
98 is_error => 1,
99 } unless $valid;
100 9 for my $block (@{$result->{content}}) {
101 9 ▶ $block->{text} = "$block->{text}" if defined $block->{text} && !ref $block->{text};
Branch: 1 branch
BranchTrueFalseTotal
if defined $block->{"text"} and not ref $block->{"text"} 9 0 9
Condition33%1/3
defined $block->{"text"} and not ref $block->{"text"}
!ll&&!rl&&r
009
MC/DC0%0/2
defined $block->{"text"} and not ref $block->{"text"}
defined $block->{"text"} not ref $block->{"text"}
Truth table33%1/3
A and not(B)
A= defined $block->{"text"}B= not ref $block->{"text"}
ABresult
0X0
100
111
102 }
103 return Future->done($self->_result($id, {
104 content => $result->{content},
105 9 ▶ isError => $result->{is_error} ? \1 : \0,
Branch: 1 branch
BranchTrueFalseTotal
$result->{'is_error'} ? : 4 5 9
106 }));
107 0 ▶ })->else(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
108 0 my ($error) = @_;
109 0 ▶ return Future->done($self->_error($id, -32603, _safe($error // 'Internal error')));
Condition0%0/2
$error // "Internal error"
l!l
00
MC/DC0%0/1
$error // "Internal error"
$error
Truth table0%0/2
A
A= $error
Aresult
11
00
110 9 });
111 }
112  
113 7 ▶sub timeout_seconds {
Subroutine timeout_seconds: called CC=6 SCAR=17.9
Pod documented
114 7 my ($self) = @_;
115 7 ▶ return $self->tool_timeout if $self->has_tool_timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_tool_timeout 3 4 7
116 4 ▶ my $timeout = $ENV{CLAUDE_AGENT_TOOL_TIMEOUT} // 60;
Condition100%2/2
$ENV{"CLAUDE_AGENT_TOOL_TIMEOUT"} // 60
l!l
31
MC/DC100%1/1
$ENV{"CLAUDE_AGENT_TOOL_TIMEOUT"} // 60
$ENV{"CLAUDE_AGENT_TOOL_TIMEOUT"}
Truth table100%2/2
A
A= $ENV{"CLAUDE_AGENT_TOOL_TIMEOUT"}
Aresult
11
00
117 4 ▶ return 60 if $timeout !~ /^\d+$/ || $timeout < 1;
Branch: 1 branch
BranchTrueFalseTotal
if not $timeout =~ /^\d+$/u or $timeout < 1 1 3 4
Condition66%2/3
not $timeout =~ /^\d+$/u or $timeout < 1
l!l&&r!l&&!r
103
MC/DC50%1/2
not $timeout =~ /^\d+$/u or $timeout < 1
not $timeout =~ /^\d+$/u $timeout < 1
Truth table66%2/3
not(A) or B
A= not $timeout =~ /^\d+$/uB= $timeout < 1
ABresult
1X1
011
000
118 3 ▶ return 300 if $timeout > 300;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout > 300 1 2 3
119 2 return $timeout;
120 }
121  
122 16 ▶sub _result {
Subroutine _result: called CC=1 SCAR=0.0
123 16 my ($self, $id, $result) = @_;
124 16 return { jsonrpc => '2.0', id => $id, result => $result };
125 }
126  
127 4 ▶sub _error {
Subroutine _error: called CC=1 SCAR=0.0
128 4 my ($self, $id, $code, $message) = @_;
129 4 return { jsonrpc => '2.0', id => $id, error => { code => $code, message => $message } };
130 }
131  
132 1 ▶sub _safe {
Subroutine _safe: called CC=2 SCAR=7.0
133 1 my ($text) = @_;
134 1 ▶ $text = defined $text ? "$text" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 1 0 1
135 1 $text = substr($text, 0, 200);
136 1 $text =~ s/[[:cntrl:]]//g;
137 1 return $text;
138 }
139  
140 10 1;
141  
142 __END__
« Agent/Hook/Registry.pm ↑ Summary Agent/Message/Assistant.pm »