| 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
| |||||||||||||||||||||||||||||
| 25 | unless ref $message eq 'HASH'; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 22 | ▶ | my $method = $message->{method} // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$message->{"method"} // ""
MC/DC100%1/1
$message->{"method"} // ""
$message->{"method"}
Truth table100%2/2
A
A= $message->{"method"}
| |||||||||||||||||||||||||||||
| 28 | 22 | my $id = $message->{id}; | |||||||||||||||||||||||||||
| 29 | 22 | ▶ | my $params = ref $message->{params} eq 'HASH' ? $message->{params} : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 22 | ▶ | return Future->done(undef) if $method =~ m{^notifications/}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 20 | ▶ | return Future->done(undef) if !length $method; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 19 | $log->debug(sprintf("SDKBridge: %s %s", $self->server->name, $method)); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 19 | ▶ | if ($method eq 'initialize') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | return Future->done($self->_result($id, { | ||||||||||||||||||||||||||||
| 38 | 4 | ▶ | protocolVersion => $params->{protocolVersion} // $PROTOCOL_VERSION, | ||||||||||||||||||||||||||
|
Condition66%2/3
$params->{"protocolVersion"} // $PROTOCOL_VERSION
MC/DC0%0/2
$params->{"protocolVersion"} // $PROTOCOL_VERSION
$params->{"protocolVersion"}
$PROTOCOL_VERSION
Truth table66%2/3
A // B
A= $params->{"protocolVersion"}B= $PROTOCOL_VERSION
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 47 | 1 | return Future->done($self->_result($id, {})); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 14 | ▶ | if ($method eq 'tools/list') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 68 | 9 | ▶ | my $loop = $self->has_loop ? $self->loop : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 9 | my $future = $tool->execute($args, $loop); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 9 | ▶ | if ($loop && !$future->is_ready) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$loop and not $future->is_ready
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
| |||||||||||||||||||||||||||||
| 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'
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'
| |||||||||||||||||||||||||||||
| 89 | 9 | ▶ | if ($valid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 8 | for my $block (@{$result->{content}}) { | |||||||||||||||||||||||||||
| 91 | 8 | ▶ | next if ref $block eq 'HASH' && defined $block->{type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $block eq "HASH" and defined $block->{"type"}
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"}
| |||||||||||||||||||||||||||||
| 92 | 0 | $valid = 0; | |||||||||||||||||||||||||||
| 93 | 0 | last; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | $result = { | ||||||||||||||||||||||||||||
| 97 | 9 | ▶ | content => [{ type => 'text', text => 'Invalid handler result format' }], | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $block->{"text"} and not ref $block->{"text"}
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"}
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | return Future->done($self->_result($id, { | ||||||||||||||||||||||||||||
| 104 | content => $result->{content}, | ||||||||||||||||||||||||||||
| 105 | 9 | ▶ | isError => $result->{is_error} ? \1 : \0, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$error // "Internal error"
$error
Truth table0%0/2
A
A= $error
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 116 | 4 | ▶ | my $timeout = $ENV{CLAUDE_AGENT_TOOL_TIMEOUT} // 60; | ||||||||||||||||||||||||||
|
Condition100%2/2
$ENV{"CLAUDE_AGENT_TOOL_TIMEOUT"} // 60
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"}
| |||||||||||||||||||||||||||||
| 117 | 4 | ▶ | return 60 if $timeout !~ /^\d+$/ || $timeout < 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $timeout =~ /^\d+$/u or $timeout < 1
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
| |||||||||||||||||||||||||||||
| 118 | 3 | ▶ | return 300 if $timeout > 300; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||