| 1 | package Praxo::Adapters::MCP; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Praxo::Errors qw(praxo_error); | ||
| 6 | |||
| 7 | sub new { | ||
| 8 | my ($class, %args) = @_; | ||
| 9 | return bless { | ||
| 10 | transport => $args{transport}, | ||
| 11 | client_version => $args{client_version} // '0.5.0', | ||
| 12 | protocol_version => $args{protocol_version} // '2024-11-05', | ||
| 13 | connected => 0, | ||
| 14 | closed => 0, | ||
| 15 | negotiated_version => '', | ||
| 16 | server_info => {}, | ||
| 17 | }, $class; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub protocol_version { return $_[0]{negotiated_version} } | ||
| 21 | sub server_info { return $_[0]{server_info} } | ||
| 22 | |||
| 23 | sub start { | ||
| 24 | my ($self) = @_; | ||
| 25 | return $self if $self->{connected}; | ||
| 26 | my $result = $self->_request('initialize', { | ||
| 27 | protocolVersion => $self->{protocol_version}, | ||
| 28 | capabilities => {}, | ||
| 29 | clientInfo => { name => 'praxo-core', version => $self->{client_version} }, | ||
| 30 | }); | ||
| 31 | my $version = $result->{protocolVersion} // ''; | ||
| 32 | praxo_error('MCP_HANDSHAKE', 'mcp: initialize returned no protocol version') | ||
| 33 | if $version eq ''; | ||
| 34 | $self->{negotiated_version} = $version; | ||
| 35 | $self->{server_info} = $result->{serverInfo} // {}; | ||
| 36 | $self->{connected} = 1; | ||
| 37 | return $self; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub list_tools { | ||
| 41 | my ($self) = @_; | ||
| 42 | $self->start; | ||
| 43 | my @tools; | ||
| 44 | my %names; | ||
| 45 | my %seen; | ||
| 46 | my $cursor; | ||
| 47 | while (1) { | ||
| 48 | my $result = $self->_request('tools/list', | ||
| 49 | defined $cursor ? { cursor => $cursor } : {}); | ||
| 50 | for my $tool (@{ $result->{tools} // [] }) { | ||
| 51 | my $name = $tool->{name} // ''; | ||
| 52 | praxo_error('MCP_DUPLICATE_TOOL', "mcp: duplicate tool $name") | ||
| 53 | if $names{$name}++; | ||
| 54 | push @tools, $tool; | ||
| 55 | } | ||
| 56 | $cursor = $result->{nextCursor}; | ||
| 57 | last if !defined $cursor || $cursor eq ''; | ||
| 58 | praxo_error('MCP_PAGINATION', 'mcp: repeated cursor') if $seen{$cursor}++; | ||
| 59 | } | ||
| 60 | return \@tools; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub call_tool { | ||
| 64 | my ($self, $name, $arguments) = @_; | ||
| 65 | $self->start; | ||
| 66 | my $result = $self->_request('tools/call', { | ||
| 67 | name => $name, | ||
| 68 | arguments => $arguments // {}, | ||
| 69 | }); | ||
| 70 | praxo_error('MCP_TOOL_ERROR', "mcp: tool $name failed") | ||
| 71 | if $result->{isError}; | ||
| 72 | return $result->{structuredContent} if exists $result->{structuredContent}; | ||
| 73 | my @texts = map { $_->{text} // '' } | ||
| 74 | grep { ($_->{type} // '') eq 'text' } @{ $result->{content} // [] }; | ||
| 75 | return undef if @texts == 0; | ||
| 76 | return @texts == 1 ? $texts[0] : \@texts; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub close { | ||
| 80 | my ($self) = @_; | ||
| 81 | $self->{closed} = 1; | ||
| 82 | $self->{connected} = 0; | ||
| 83 | my $transport = $self->{transport}; | ||
| 84 | if (ref($transport) && eval { $transport->can('close') }) { | ||
| 85 | eval { $transport->close }; | ||
| 86 | } | ||
| 87 | return; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub _request { | ||
| 91 | my ($self, $method, $params) = @_; | ||
| 92 | praxo_error('MCP_CLOSED', 'mcp: client is closed') if $self->{closed}; | ||
| 93 | my $transport = $self->{transport}; | ||
| 94 | my $result; | ||
| 95 | if (ref($transport) && eval { $transport->can('request') }) { | ||
| 96 | $result = $transport->request($method, $params); | ||
| 97 | } elsif (ref($transport) eq 'CODE') { | ||
| 98 | $result = $transport->($method, $params); | ||
| 99 | } else { | ||
| 100 | praxo_error('MCP_TRANSPORT', 'mcp: transport is required'); | ||
| 101 | } | ||
| 102 | return ref($result) eq 'HASH' ? $result : {}; | ||
| 103 | } | ||
| 104 | |||
| 105 | 1; |