@@ -34,6 +34,7 @@ use event::ConfirmToolResponse;
34
34
use futures:: StreamExt ;
35
35
use itertools:: Itertools ;
36
36
use mcp_core:: types:: ProtocolVersion ;
37
+ use mcp_core:: types:: ToolResponseContent ;
37
38
use rig:: agent:: AgentBuilder ;
38
39
use rig:: completion:: CompletionError ;
39
40
use rig:: completion:: CompletionModel ;
@@ -201,19 +202,20 @@ impl Agent {
201
202
agent_builder
202
203
}
203
204
204
- async fn add_mcp_tools < M > (
205
+ async fn add_mcp_tools < ' a , M > (
205
206
mut agent_builder : AgentBuilder < M > ,
206
207
mcp : Option < & McpConfig > ,
207
- ) -> Result < AgentBuilder < M > >
208
+ ) -> Result < ( AgentBuilder < M > , String ) >
208
209
where
209
210
M : CompletionModel ,
210
211
{
211
212
let Some ( mcp_config) = mcp else {
212
- return Ok ( agent_builder) ;
213
+ return Ok ( ( agent_builder, String :: default ( ) ) ) ;
213
214
} ;
214
215
216
+ let mut system_prompt_addons = Vec :: default ( ) ;
215
217
for server_config in mcp_config. servers . values ( ) {
216
- match server_config {
218
+ match & server_config. transport {
217
219
McpClientTransport :: Stdio ( config) => {
218
220
let transport = mcp_core:: transport:: ClientStdioTransport :: new (
219
221
& config. command ,
@@ -261,16 +263,43 @@ impl Agent {
261
263
} ) ?;
262
264
let tools_list_res = mcp_client. list_tools ( None , None ) . await ?;
263
265
266
+ if let Some ( system_prompt_template) = & server_config. system_prompt {
267
+ if let Some ( context_tool) = & server_config. context_tool {
268
+ let result = mcp_client. call_tool ( & context_tool, None ) . await ?;
269
+ if result. is_error . is_none_or ( |is_error| !is_error) {
270
+ let txt = result
271
+ . content
272
+ . iter ( )
273
+ . filter_map ( |content| {
274
+ if let ToolResponseContent :: Text ( txt) = content {
275
+ Some ( txt. text . clone ( ) )
276
+ } else {
277
+ None
278
+ }
279
+ } )
280
+ . join ( "\\ n" ) ;
281
+ let system_prompt =
282
+ system_prompt_template. replace ( "{CONTEXT_TOOL}" , & txt) ;
283
+ system_prompt_addons. push ( system_prompt) ;
284
+ }
285
+ }
286
+ }
264
287
agent_builder = tools_list_res
265
288
. tools
266
289
. into_iter ( )
290
+ . filter ( |tool| {
291
+ server_config
292
+ . context_tool
293
+ . as_ref ( )
294
+ . is_none_or ( |ctx_tool| ctx_tool != & tool. name )
295
+ } )
267
296
. fold ( agent_builder, |builder, tool| {
268
297
builder. mcp_tool ( tool, mcp_client. clone ( ) )
269
298
} )
270
299
}
271
300
}
272
301
}
273
- Ok ( agent_builder)
302
+ Ok ( ( agent_builder, system_prompt_addons . join ( " \\ n" ) ) )
274
303
}
275
304
276
305
async fn configure_agent < M > (
@@ -281,13 +310,14 @@ impl Agent {
281
310
where
282
311
M : CompletionModel ,
283
312
{
284
- agent_builder = agent_builder
285
- . preamble ( & context. system_prompt )
286
- . temperature ( 0.0 ) ;
313
+ agent_builder = agent_builder. temperature ( 0.0 ) ;
314
+ let mut system_prompt = context. system_prompt . clone ( ) ;
287
315
let mcp_config = context. config . mcp . as_ref ( ) ;
288
316
agent_builder = Self :: add_static_tools ( agent_builder, context) ;
289
- agent_builder = Self :: add_mcp_tools ( agent_builder, mcp_config) . await ?;
290
- let agent = agent_builder. build ( ) ;
317
+ let ( agent_builder, system_prompt_addons) =
318
+ Self :: add_mcp_tools ( agent_builder, mcp_config) . await ?;
319
+ system_prompt. push_str ( & system_prompt_addons) ;
320
+ let agent = agent_builder. preamble ( & system_prompt) . build ( ) ;
291
321
* tools_tokens = count_tokens (
292
322
& agent
293
323
. tools
0 commit comments