| 1 | package Langertha::Role::Tools; | ||||||||||
| 2 | # ABSTRACT: Role for MCP tool calling support | ||||||||||
| 3 | 36 | our $VERSION = '0.502'; | |||||||||
| 4 | 36 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 36 | ▶ | use Future::AsyncAwait; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 36 | ▶ | use Carp qw( croak ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 36 | ▶ | use JSON::MaybeXS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 36 | ▶ | use Log::Any qw( $log ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 36 | with 'Langertha::Role::ParallelToolUse'; | |||||||||
| 11 | |||||||||||
| 12 | |||||||||||
| 13 | has mcp_servers => ( | ||||||||||
| 14 | is => 'ro', | ||||||||||
| 15 | isa => 'ArrayRef', | ||||||||||
| 16 | 229 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 36 | ); | |||||||||
| 18 | |||||||||||
| 19 | |||||||||||
| 20 | 36 | has tool_max_iterations => ( | |||||||||
| 21 | is => 'ro', | ||||||||||
| 22 | isa => 'Int', | ||||||||||
| 23 | default => 10, | ||||||||||
| 24 | ); | ||||||||||
| 25 | |||||||||||
| 26 | |||||||||||
| 27 | sub build_tool_chat_request { | ||||||||||
| 28 | 2 | ▶ | my ( $self, $conversation, $formatted_tools, %extra ) = @_; | ||||||||
|
Subroutine
build_tool_chat_request: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 29 | 2 | return $self->chat_request($conversation, tools => $formatted_tools, %extra); | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | 2 | ▶ | async sub chat_with_tools_f { | ||||||||
|
Subroutine
chat_with_tools_f: called CC=10 SCAR=23.5
Pod
documented
| |||||||||||
| 34 | 2 | my ( $self, @messages ) = @_; | |||||||||
| 35 | |||||||||||
| 36 | 2 | ▶ | croak "No MCP servers configured" unless @{$self->mcp_servers}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | |||||||||||
| 38 | # Gather tools from all MCP servers | ||||||||||
| 39 | 2 | my ( @all_tools, %tool_server_map ); | |||||||||
| 40 | 2 | for my $mcp (@{$self->mcp_servers}) { | |||||||||
| 41 | 2 | my $tools = await $mcp->list_tools; | |||||||||
| 42 | 2 | for my $tool (@$tools) { | |||||||||
| 43 | 2 | $tool_server_map{$tool->{name}} = $mcp; | |||||||||
| 44 | 2 | push @all_tools, $tool; | |||||||||
| 45 | } | ||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 2 | my $formatted_tools = $self->format_tools(\@all_tools); | |||||||||
| 49 | 2 | my $conversation = $self->chat_messages(@messages); | |||||||||
| 50 | |||||||||||
| 51 | $log->debugf("[%s] chat_with_tools_f: %d tools from %d MCP servers, max_iterations=%d", | ||||||||||
| 52 | 2 | ref $self, scalar @all_tools, scalar @{$self->mcp_servers}, $self->tool_max_iterations); | |||||||||
| 53 | |||||||||||
| 54 | 2 | for my $iteration (1..$self->tool_max_iterations) { | |||||||||
| 55 | 4 | $log->debugf("[%s] Tool loop iteration %d/%d", | |||||||||
| 56 | ref $self, $iteration, $self->tool_max_iterations); | ||||||||||
| 57 | |||||||||||
| 58 | 4 | my $request = $self->build_tool_chat_request($conversation, $formatted_tools); | |||||||||
| 59 | 4 | my $response = await $self->_async_http->do_request(request => $request); | |||||||||
| 60 | |||||||||||
| 61 | 4 | ▶ | unless ($response->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 0 | die "".(ref $self)." tool chat request failed: ".$response->status_line; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 4 | my $data = $self->parse_response($response); | |||||||||
| 66 | 4 | my $tool_calls = $self->response_tool_calls($data); | |||||||||
| 67 | |||||||||||
| 68 | # No tool calls means the LLM is done — return final text | ||||||||||
| 69 | 4 | ▶ | unless (@$tool_calls) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 2 | my $text = $self->response_text_content($data); | |||||||||
| 71 | 2 | ▶ | if ($self->think_tag_filter) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 2 | ($text) = $self->filter_think_content($text); | |||||||||
| 73 | } | ||||||||||
| 74 | 2 | return $text; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | # Execute each tool call via the appropriate MCP server | ||||||||||
| 78 | 2 | my @results; | |||||||||
| 79 | 2 | for my $tc (@$tool_calls) { | |||||||||
| 80 | 2 | my ( $name, $input ) = $self->extract_tool_call($tc); | |||||||||
| 81 | |||||||||||
| 82 | 2 | $log->debugf("[%s] Calling tool: %s", ref $self, $name); | |||||||||
| 83 | |||||||||||
| 84 | 2 | ▶ | my $mcp = $tool_server_map{$name} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | or die "Tool '$name' not found on any MCP server"; | ||||||||||
| 86 | |||||||||||
| 87 | my $result = await $mcp->call_tool($name, $input)->else(sub { | ||||||||||
| 88 | 0 | ▶ | my ( $error ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 89 | 0 | Future->done({ | |||||||||
| 90 | content => [{ type => 'text', text => "Error calling tool '$name': $error" }], | ||||||||||
| 91 | isError => JSON::MaybeXS->true, | ||||||||||
| 92 | }); | ||||||||||
| 93 | 2 | }); | |||||||||
| 94 | |||||||||||
| 95 | 2 | push @results, { tool_call => $tc, result => $result }; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | # Append assistant message and tool results to conversation | ||||||||||
| 99 | 2 | push @$conversation, $self->format_tool_results($data, \@results); | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 0 | die "Tool calling loop exceeded ".$self->tool_max_iterations." iterations"; | |||||||||
| 103 | 36 | } | |||||||||
| 104 | |||||||||||
| 105 | |||||||||||
| 106 | |||||||||||
| 107 | 36 | 1; | |||||||||
| 108 | |||||||||||
| 109 | __END__ | ||||||||||
| 110 | |||||||||||
| 111 | =pod | ||||||||||
| 112 | |||||||||||
| 113 | =encoding UTF-8 | ||||||||||
| 114 | |||||||||||
| 115 | =head1 NAME | ||||||||||
| 116 | |||||||||||
| 117 | Langertha::Role::Tools - Role for MCP tool calling support | ||||||||||
| 118 | |||||||||||
| 119 | =head1 VERSION | ||||||||||
| 120 | |||||||||||
| 121 | version 0.502 | ||||||||||
| 122 | |||||||||||
| 123 | =head1 SYNOPSIS | ||||||||||
| 124 | |||||||||||
| 125 | use IO::Async::Loop; | ||||||||||
| 126 | use Net::Async::MCP; | ||||||||||
| 127 | use Future::AsyncAwait; | ||||||||||
| 128 | |||||||||||
| 129 | my $loop = IO::Async::Loop->new; | ||||||||||
| 130 | |||||||||||
| 131 | # Set up an MCP server with tools | ||||||||||
| 132 | my $mcp = Net::Async::MCP->new(server => $my_mcp_server); | ||||||||||
| 133 | $loop->add($mcp); | ||||||||||
| 134 | await $mcp->initialize; | ||||||||||
| 135 | |||||||||||
| 136 | # Create engine with MCP servers (native tool calling) | ||||||||||
| 137 | my $engine = Langertha::Engine::Anthropic->new( | ||||||||||
| 138 | api_key => $ENV{ANTHROPIC_API_KEY}, | ||||||||||
| 139 | model => 'claude-sonnet-4-6', | ||||||||||
| 140 | mcp_servers => [$mcp], | ||||||||||
| 141 | ); | ||||||||||
| 142 | |||||||||||
| 143 | my $response = await $engine->chat_with_tools_f( | ||||||||||
| 144 | 'Use the available tools to answer my question' | ||||||||||
| 145 | ); | ||||||||||
| 146 | |||||||||||
| 147 | # Hermes tool calling (for APIs without native tool support) | ||||||||||
| 148 | my $engine = Langertha::Engine::AKI->new( | ||||||||||
| 149 | api_key => $ENV{AKI_API_KEY}, | ||||||||||
| 150 | mcp_servers => [$mcp], | ||||||||||
| 151 | ); | ||||||||||
| 152 | |||||||||||
| 153 | =head1 DESCRIPTION | ||||||||||
| 154 | |||||||||||
| 155 | This role adds MCP (Model Context Protocol) tool calling support to Langertha | ||||||||||
| 156 | engines. It provides the L</chat_with_tools_f> method which implements the full | ||||||||||
| 157 | async tool-calling loop: | ||||||||||
| 158 | |||||||||||
| 159 | =over 4 | ||||||||||
| 160 | |||||||||||
| 161 | =item 1. Gather available tools from all configured MCP servers | ||||||||||
| 162 | |||||||||||
| 163 | =item 2. Send a chat request with tool definitions to the LLM | ||||||||||
| 164 | |||||||||||
| 165 | =item 3. If the LLM returns tool calls, execute them via MCP | ||||||||||
| 166 | |||||||||||
| 167 | =item 4. Feed tool results back to the LLM and repeat | ||||||||||
| 168 | |||||||||||
| 169 | =item 5. When the LLM returns final text, return it | ||||||||||
| 170 | |||||||||||
| 171 | =back | ||||||||||
| 172 | |||||||||||
| 173 | Engines must provide implementations for five methods that handle | ||||||||||
| 174 | engine-specific tool format conversion: C<format_tools>, | ||||||||||
| 175 | C<response_tool_calls>, C<extract_tool_call>, C<format_tool_results>, and | ||||||||||
| 176 | C<response_text_content>. Native API engines (OpenAI, Anthropic, Gemini, etc.) | ||||||||||
| 177 | implement these directly. Engines without native tool support compose | ||||||||||
| 178 | L<Langertha::Role::HermesTools> which provides implementations using XML tags. | ||||||||||
| 179 | |||||||||||
| 180 | =head2 mcp_servers | ||||||||||
| 181 | |||||||||||
| 182 | mcp_servers => [$mcp1, $mcp2] | ||||||||||
| 183 | |||||||||||
| 184 | ArrayRef of L<Net::Async::MCP> instances to use as tool providers. Defaults to | ||||||||||
| 185 | an empty ArrayRef. At least one server must be configured before calling | ||||||||||
| 186 | L</chat_with_tools_f>. | ||||||||||
| 187 | |||||||||||
| 188 | =head2 tool_max_iterations | ||||||||||
| 189 | |||||||||||
| 190 | tool_max_iterations => 20 | ||||||||||
| 191 | |||||||||||
| 192 | Maximum number of tool-calling round trips before aborting with an error. | ||||||||||
| 193 | Defaults to C<10>. Increase for complex multi-step tool workflows. | ||||||||||
| 194 | |||||||||||
| 195 | =head2 build_tool_chat_request | ||||||||||
| 196 | |||||||||||
| 197 | my $request = $self->build_tool_chat_request($conversation, $formatted_tools); | ||||||||||
| 198 | |||||||||||
| 199 | Builds an HTTP request for a tool-calling chat turn. The default implementation | ||||||||||
| 200 | passes tools as an API parameter via C<chat_request>. Overridden by | ||||||||||
| 201 | L<Langertha::Role::HermesTools> to inject tools into the system prompt instead. | ||||||||||
| 202 | |||||||||||
| 203 | =head2 chat_with_tools_f | ||||||||||
| 204 | |||||||||||
| 205 | my $response = await $engine->chat_with_tools_f(@messages); | ||||||||||
| 206 | |||||||||||
| 207 | Async tool-calling chat loop. Accepts the same message arguments as | ||||||||||
| 208 | L<Langertha::Role::Chat/simple_chat>. Gathers tools from all L</mcp_servers>, | ||||||||||
| 209 | sends the request, executes any tool calls returned by the LLM, and repeats | ||||||||||
| 210 | until the LLM returns a final text response or L</tool_max_iterations> is | ||||||||||
| 211 | exceeded. Returns a L<Future> that resolves to the final text response. | ||||||||||
| 212 | |||||||||||
| 213 | =head1 SEE ALSO | ||||||||||
| 214 | |||||||||||
| 215 | =over | ||||||||||
| 216 | |||||||||||
| 217 | =item * L<Langertha::Role::HermesTools> - Hermes-style tool calling via XML tags | ||||||||||
| 218 | |||||||||||
| 219 | =item * L<Langertha::Role::Chat> - Chat role this is built on top of | ||||||||||
| 220 | |||||||||||
| 221 | =item * L<Langertha::Raider> - Autonomous agent with persistent history using tools | ||||||||||
| 222 | |||||||||||
| 223 | =item * L<Net::Async::MCP> - MCP client used as tool provider | ||||||||||
| 224 | |||||||||||
| 225 | =item * L<Langertha::Engine::Anthropic> - Engine with native tool support | ||||||||||
| 226 | |||||||||||
| 227 | =back | ||||||||||
| 228 | |||||||||||
| 229 | =head1 SUPPORT | ||||||||||
| 230 | |||||||||||
| 231 | =head2 Issues | ||||||||||
| 232 | |||||||||||
| 233 | Please report bugs and feature requests on GitHub at | ||||||||||
| 234 | L<https://github.com/Getty/langertha/issues>. | ||||||||||
| 235 | |||||||||||
| 236 | =head2 IRC | ||||||||||
| 237 | |||||||||||
| 238 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||
| 239 | |||||||||||
| 240 | =head1 CONTRIBUTING | ||||||||||
| 241 | |||||||||||
| 242 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||
| 243 | |||||||||||
| 244 | =head1 AUTHOR | ||||||||||
| 245 | |||||||||||
| 246 | Torsten Raudssus <getty@cpan.org> | ||||||||||
| 247 | |||||||||||
| 248 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 249 | |||||||||||
| 250 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||
| 251 | |||||||||||
| 252 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 253 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 254 | |||||||||||
| 255 | =cut | ||||||||||