-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmcp_cog.py
More file actions
1394 lines (1203 loc) · 73.6 KB
/
Copy pathmcp_cog.py
File metadata and controls
1394 lines (1203 loc) · 73.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-FileCopyrightText: 2025-present Oori Data <info@oori.dev>
# SPDX-License-Identifier: Apache-2.0
# mcp_cog.py
'''
Discord cog for MCP integration using the official MCP Python SDK with Streamable HTTP transport.
Connects to MCP servers and provides commands to interact with MCP tools via an LLM.
'''
import os
import json
import asyncio
from typing import Any, List
import uuid
import time # For current time
from dataclasses import dataclass, field
from enum import Enum
# import time
# import traceback
import discord
from discord import app_commands, abc as discord_abc
from discord.ext import commands
import structlog # type: ignore
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from ogbujipt.embedding.pgvector import MessageDB
# Official MCP SDK imports
from fastmcp import Client
from fastmcp.tools import Tool
from fastmcp.exceptions import McpError
# Tenacity for retry logic
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
from b4a_config import B4ALoader, MCPConfig, resolve_value
from discord_aiagent.source_handlers import FEEDPARSER_AVAILABLE
from discord_aiagent.rssutil import RSSAgent, create_rss_agent
from discord_aiagent.discordutil import send_long_message, handle_attachments, get_formatted_sysmsg
from discord_aiagent.openaiutil import MissingLLMResponseError, LLMResponseError, ToolCallExecutionError, replace_system_prompt
from discord_aiagent.llm_providers import LLMProviderFactory
from discord_aiagent.history import HistoryProvider, MemoryHistoryProvider, PGVectorHistoryProvider
from discord.ext import tasks # Ensure tasks is imported
logger = structlog.get_logger(__name__)
# Define a constant namespace UUID for generating channel-specific history keys
# Generated one-time by Uche using uuid.uuid4()
DISCORD_CHANNEL_HISTORY_NAMESPACE = uuid.UUID('faa9e9c4-1f01-4e27-933d-6460b8924924')
ALLOWED_FOR_LLM_INIT = ['base_url', 'api_key'] # Adjusted for openai > 1.0
ALLOWED_FOR_LLM_CHAT = ['model', 'temperature', 'max_tokens', 'top_p'] # Added common chat params
# FIXME: Use Word Loom
DEFAULT_SYSTEM_MESSAGE = 'You are a helpful AI assistant. When you need to use a tool, you MUST use the provided function-calling mechanism. Do NOT simply describe the tool call in your text response.'
# API Key Handling - ensure OPENAI_API_KEY is checked/set if needed by the AsyncOpenAI client
# The logic in __init__ already handles this using the config/env var.
# Define schedule types and their durations in seconds
class ScheduleType(str, Enum): # Inherit from str for app_commands.Choice compatibility
EVERY_5_MINUTES = "Every 5 minutes"
HOURLY = "Hourly"
DAILY = "Daily"
WEEKLY = "Weekly"
SCHEDULE_DURATIONS = {
ScheduleType.EVERY_5_MINUTES: 5 * 60,
ScheduleType.HOURLY: 60 * 60,
ScheduleType.DAILY: 24 * 60 * 60,
ScheduleType.WEEKLY: 7 * 24 * 60 * 60,
}
@dataclass
class StandingPrompt:
schedule_type: ScheduleType
prompt_text: str
channel_id: int
user_id: int # User who initiated this standing prompt
id: uuid.UUID = field(default_factory=uuid.uuid4)
created_at: float = field(default_factory=time.time)
last_run_time: float = 0.0 # Timestamp of last execution
last_execution_timestamp: float = 0.0 # Timestamp to use for RSS filtering in next execution
@property
def interval_seconds(self) -> int:
return SCHEDULE_DURATIONS.get(self.schedule_type, 0)
os.environ['TOKENIZERS_PARALLELISM'] = 'false' # Disable parallelism for tokenizers to avoid warnings
class MCPCog(commands.Cog):
def __init__(self, bot: commands.Bot, config: dict[str, Any], b4a_data: B4ALoader, pgvector_config: dict[str, Any]):
self.bot = bot
self.config = config # Agent config (LLM, etc.)
self.b4a_data = b4a_data
self.mcp_urls: dict[str, str] = {} # Keyed by MCPConfig.name, store URLs
self.mcp_tools: dict[str, list[Tool]] = {} # Keyed by MCPConfig.name, cache tool definitions
self.mcp_clients: dict[str, Client] = {} # Keyed by MCPConfig.name, persistent client instances
self._connection_tasks: dict[str, asyncio.Task] = {}
self._shutdown_event = asyncio.Event()
self.pgvector_config = pgvector_config
self.pgvector_db: 'MessageDB | None' = None # Lazy import - only imported when PG history enabled
# Handle case where pgvector_config is None (disabled)
self.pgvector_enabled = self.pgvector_config.get('enabled', False) if self.pgvector_config else False
# Initialize history provider (will be set up fully in cog_load after PGVector connection)
max_history_length = self.config.get('max_history_length', 20)
self.history_provider: HistoryProvider = MemoryHistoryProvider(max_history_length)
self._rss_feed_cache: dict[str, tuple[float, Any]] = {}
self._rss_cache_ttl: int = self.config.get('rss_cache_ttl', 300) # Default 5 mins, configurable in agent TOML
# Create RSS agents for each configured RSS source
self._rss_agents: dict[str, RSSAgent] = {}
for rss_conf in self.b4a_data.rss_sources:
agent = create_rss_agent(rss_conf.agent_type, b4a_data, self._rss_feed_cache, self._rss_cache_ttl)
self._rss_agents[rss_conf.name] = agent
self.standing_prompts: List[StandingPrompt] = []
self._current_standing_prompt: StandingPrompt | None = None # Track current standing prompt for context injection
# LLM Client and Parameter Initialization
llm_endpoint_config = self.config.get('llm_endpoint', {})
model_params_config = self.config.get('model_params', {})
# Get API type and initialize appropriate provider
self.api_type = llm_endpoint_config.get('api_type', 'generic')
logger.info(f'Initializing LLM provider: {self.api_type}')
# Initialize LLM Client
llm_init_params = {k: resolve_value(v) for k, v in llm_endpoint_config.items() if k in ALLOWED_FOR_LLM_INIT}
if self.api_type == 'generic':
if not llm_init_params.get('base_url'):
raise ValueError('LLM `base_url` config is required for generic providers')
if not os.environ.get('OPENAI_API_KEY'): # Default to UNSPECIFIED if not set
os.environ['OPENAI_API_KEY'] = 'UNSPECIFIED'
# Configure LLM Chat Parameters (Combine defaults, model_params, llm_endpoint)
llm_chat_params = {
'model': llm_endpoint_config.get('model', 'local-model'), # Default model name
'temperature': 0.3, 'max_tokens': 1000, # Base defaults
}
# Apply overrides from [model_params] first
model_param_overrides = {k: resolve_value(v) for k, v in model_params_config.items() if k in ALLOWED_FOR_LLM_CHAT}
llm_chat_params.update(model_param_overrides)
# Apply any *other* allowed chat params found in [llm_endpoint]
endpoint_chat_overrides = {k: resolve_value(v) for k, v in llm_endpoint_config.items() if k in ALLOWED_FOR_LLM_CHAT and k not in model_param_overrides}
llm_chat_params.update(endpoint_chat_overrides)
logger.info('LLM chat parameters configured', **llm_chat_params)
# Configure System Message Template and Postscript
self.sysmsg_template = resolve_value(llm_endpoint_config.get('sysmsg', DEFAULT_SYSTEM_MESSAGE))
print('sysmsg_template', self.sysmsg_template, flush=True)
self.sys_postscript = resolve_value(llm_endpoint_config.get('sys_postscript', ''))
logger.info('System message template configured.', template_len=len(self.sysmsg_template), postscript_len=len(self.sys_postscript))
logger.debug(f'System message template: {self.sysmsg_template}')
try:
self.llm_client = LLMProviderFactory.create_provider(self.api_type, llm_init_params, llm_chat_params, self)
logger.info(f'{self.api_type} LLM provider initialized.', **llm_init_params)
except Exception as e:
logger.exception(f'Failed to initialize {self.api_type} LLM provider', config=llm_init_params)
raise e
# Log B4A Loading Summary
if self.b4a_data.load_errors:
logger.warning('Errors occurred during B4A source loading.', count=len(self.b4a_data.load_errors), errors=self.b4a_data.load_errors)
logger.info('MCP Cog initialized',
mcp_sources_found=len(self.b4a_data.mcp_sources),
rss_sources_found=len(self.b4a_data.rss_sources),
pgvector_enabled=self.pgvector_enabled,
rss_handler_available=FEEDPARSER_AVAILABLE)
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=4, max=10),
retry=retry_if_exception_type((ConnectionError, McpError))
)
async def _manage_mcp_connection(self, mcp_config: MCPConfig):
'''Persistent task to manage MCP server discovery and maintain persistent client connection.'''
url = mcp_config.url
name = mcp_config.name # Use the name from B4A config
reconnect_delay = 15 # Initial delay seconds
max_reconnect_delay = 300 # Max delay seconds
while not self._shutdown_event.is_set():
logger.info(f'Attempting to discover MCP source: \'{name}\'', url=url)
try:
# Create a persistent client for this MCP server
client = Client(url)
logger.info(f'Discovering tools for MCP source \'{name}\'...')
# Connect and discover tools
async with client:
# List tools to verify connection and get tool definitions
tools_list = await client.list_tools()
# Store the client, URL and tools for later use
self.mcp_clients[name] = client
self.mcp_urls[name] = url
self.mcp_tools[name] = tools_list
logger.info(f'Discovered MCP source \'{name}\' with {len(tools_list)} tools.', server_name=name)
reconnect_delay = 15 # Reset delay on success
# Wait for shutdown signal while maintaining the connection
await self._shutdown_event.wait()
logger.info(f'Shutdown signal received for {name}. Ending discovery task.')
break
except McpError as mcp_err:
logger.warning(f'MCP error for server {name}: {type(mcp_err).__name__}. Retrying…', server_name=name, error=str(mcp_err))
except ConnectionError as conn_err:
logger.warning(f'Connection failed for MCP server {name}: {type(conn_err).__name__}. Retrying…', server_name=name, error=str(conn_err))
except asyncio.TimeoutError:
logger.error(f'Timeout connecting or listing tools for MCP server {name}. Retrying…', server_name=name)
except asyncio.CancelledError:
logger.info(f'Discovery task for {name} cancelled.')
break # Exit loop immediately
except Exception as e:
# Catch other errors from MCP SDK
logger.exception(f'Unexpected error discovering {name}. Retrying…', server_name=name, url=url)
# Cleanup before Reconnect/Shutdown
logger.debug(f'Cleaning up resources for {name} before retry/shutdown.')
if name in self.mcp_clients:
del self.mcp_clients[name]
if name in self.mcp_urls:
del self.mcp_urls[name]
if name in self.mcp_tools:
del self.mcp_tools[name]
# Wait before Retrying (if not shutting down)
if not self._shutdown_event.is_set():
logger.info(f'Waiting {reconnect_delay}s before rediscovering {name}.')
try:
await asyncio.wait_for(self._shutdown_event.wait(), timeout=reconnect_delay)
logger.info(f'Shutdown signaled during reconnect delay for {name}.')
break # Exit loop if shutdown signaled
except asyncio.TimeoutError:
reconnect_delay = min(reconnect_delay * 2, max_reconnect_delay) # Exponential backoff
except asyncio.CancelledError:
logger.info(f'Reconnect wait for {name} cancelled.')
break # Exit loop
# Final Task Cleanup (on shutdown or cancellation)
logger.info(f'Discovery task for {name} finished cleanly.')
if name in self.mcp_clients:
del self.mcp_clients[name]
if name in self.mcp_urls:
del self.mcp_urls[name]
if name in self.mcp_tools:
del self.mcp_tools[name]
async def cog_load(self):
'''
Set up MCP connections when the cog is loaded by starting persistent tasks.
'''
logger.info('Loading MCPCog and starting connection managers…')
self._shutdown_event.clear() # Ensure shutdown is not set initially
if self.pgvector_enabled and self.pgvector_db is None:
logger.info('Initializing PGVector MessageDB connection...')
# Lazy import - only import MessageDB when PG history is actually enabled
from ogbujipt.embedding.pgvector import MessageDB
try:
if self.pgvector_config['conn_str']:
# Use connection string if provided
self.pgvector_db = await MessageDB.from_conn_string(
self.pgvector_config['conn_str'],
self.pgvector_config['embedding_model'],
self.pgvector_config['table_name'],
# stringify_json=False
)
else:
self.pgvector_db = await MessageDB.from_conn_params(
embedding_model=self.pgvector_config['embedding_model'],
table_name=self.pgvector_config['table_name'],
db_name=self.pgvector_config['db_name'],
host=self.pgvector_config['host'],
port=self.pgvector_config['port'],
user=self.pgvector_config['user'],
password=self.pgvector_config['password']
)
try:
# Might not be running as superuser, so don't assume table creation is possible
# await self.pgvector_db.create_table()
logger.info(f'PGVector table `{self.pgvector_config['table_name']}` ensured.')
except Exception as table_err:
# Log error, but maybe don't disable PGVector entirely if table exists
logger.exception(f'Error ensuring PGVector table exists (it might already exist): {table_err}')
logger.info('PGVector MessageDB connection successful.')
# Switch to PGVector history provider
self.history_provider = PGVectorHistoryProvider(
self.pgvector_db,
DISCORD_CHANNEL_HISTORY_NAMESPACE
)
logger.info('Using PGVector history provider')
except Exception as e:
logger.exception('Failed to initialize PGVector MessageDB connection during cog_load. Disabling PGVector.')
self.pgvector_enabled = False
self.pgvector_db = None
# Keep using memory history provider (already initialized in __init__)
logger.info('Using in-memory history provider (fallback)')
mcp_source_configs = self.b4a_data.mcp_sources # Get sources from B4A data
if not mcp_source_configs:
logger.warning('No valid @mcp B4A sources loaded. No MCP connections will be established.')
return
active_task_count = 0
for mcp_conf in mcp_source_configs: # Iterate B4A sources
name = mcp_conf.name
if name not in self._connection_tasks or self._connection_tasks[name].done():
logger.info(f'Creating connection task for B4A MCP source: \'{name}\'', url=mcp_conf.url)
# Pass the MCPConfig object
task = asyncio.create_task(self._manage_mcp_connection(mcp_conf), name=f'mcp_conn_{name}')
self._connection_tasks[name] = task
active_task_count += 1
else:
logger.info(f'Connection task for B4A MCP source \'{name}\' already running.')
active_task_count += 1
logger.info(f'MCPCog load processed. {active_task_count} discovery tasks are active or starting.')
self.standing_prompt_loop.start()
logger.info('Standing prompt loop started.')
async def cog_unload(self):
'''
Clean up MCP connections when the cog is unloaded by stopping tasks.
'''
# XXX Shouldn't be eny PGVector Cleanup needed, but maybe set self.pgvector_db to None?
logger.info('Unloading MCPCog and stopping connection tasks…')
self._shutdown_event.set() # Signal all tasks to stop their loops
tasks_to_wait_for = list(self._connection_tasks.values())
if tasks_to_wait_for:
logger.info(f'Waiting for {len(tasks_to_wait_for)} MCP connection tasks to complete shutdown…')
# Wait for tasks to finish processing the shutdown signal
# Give them a bit more time as they might be in a wait/retry loop
done, pending = await asyncio.wait(tasks_to_wait_for, timeout=15.0)
if pending:
logger.warning(f'{len(pending)} connection tasks did not shut down cleanly within timeout. Attempting cancellation.')
for task in pending:
task_name = task.get_name() if hasattr(task, 'get_name') else 'unknown task'
logger.debug(f'Cancelling task: {task_name}')
task.cancel()
try:
# Allow cancellation to propagate briefly
await asyncio.wait_for(task, timeout=1.0)
except asyncio.CancelledError:
logger.debug(f'Task {task_name} confirmed cancelled.')
except asyncio.TimeoutError:
logger.warning(f'Task {task_name} did not respond to cancellation quickly.')
except Exception as e:
logger.error(f'Error during task cancellation for {task_name}', exc_info=e)
else:
logger.info('All connection tasks shut down gracefully.')
else:
logger.info('No active connection tasks to stop.')
# Clean up discovery tasks
for server_name in list(self.mcp_urls.keys()):
try:
del self.mcp_urls[server_name]
logger.debug(f'Removed MCP server from discovered list: {server_name}')
except Exception as e:
logger.warning(f'Error removing MCP server {server_name}: {e}')
# Clear stored data regardless of task shutdown status
self.mcp_urls.clear()
self.mcp_tools.clear()
self._connection_tasks.clear() # Clear task references
self._rss_feed_cache.clear()
self.standing_prompt_loop.cancel()
logger.info('Standing prompt loop stopped.')
logger.info('MCPCog unloaded and connection resources cleared.')
async def _get_channel_history(self, channel_id: int) -> list[dict[str, Any]]:
'''Get message history for a channel using the configured history provider.'''
limit = self.config.get('max_history_length', 20)
return await self.history_provider.get_messages(channel_id, limit)
async def execute_tool(self, tool_name: str, tool_input: dict[str, Any]) -> dict[str, Any] | dict[str, str]:
'''
Execute a tool on the appropriate MCP server.
Returns a dict with either 'content' or 'error' key.
'''
logger.debug('Executing tool', tool_name=tool_name, tool_input=tool_input)
if tool_name == 'query_rss_feed':
if not FEEDPARSER_AVAILABLE:
return {'error': 'RSS querying is disabled because the `feedparser` library is not installed.'}
# Get the feed name from the tool input
feed_name = tool_input.get('feed_name')
if not feed_name:
return {'error': 'Missing required parameter: feed_name'}
# Find the appropriate RSS agent for this feed
if feed_name in self._rss_agents:
return await self._rss_agents[feed_name].execute_rss_query(tool_input)
else:
return {'error': f'Configured RSS feed named `{feed_name}` not found.'}
server_name_found: str | None = None # B4A source name
# Find the server (keyed by B4A source name) that has the tool
for source_name, url in self.mcp_urls.items():
# Check if the tool name exists in the list of tools for this server
if source_name in self.mcp_tools and any(tool.name == tool_name for tool in self.mcp_tools[source_name]):
server_name_found = source_name
logger.debug(f'Found tool `{tool_name}` on MCP source \'{server_name_found}\'.')
break
if not server_name_found:
logger.warning(f'MCP Tool `{tool_name}` not found on any *discovered* MCP source.')
# Provide more specific error based on B4A data
if not self.b4a_data.mcp_sources:
return {'error': f'Tool `{tool_name}` cannot be executed: No @mcp B4A sources configured.'}
if not self.mcp_urls:
return {'error': f'Tool `{tool_name}` cannot be executed: No MCP sources currently discovered.'}
# Check if tool exists on a configured but undiscovered source
origin_source = next((s.name for s in self.b4a_data.mcp_sources if s.name in self.mcp_tools and any(t.name == tool_name for t in self.mcp_tools[s.name])), None)
if origin_source and origin_source not in self.mcp_urls:
return {'error': f'Tool `{tool_name}` exists (on MCP source \'{origin_source}\'), but it\'s currently undiscovered.'}
return {'error': f'Tool `{tool_name}` not found on any configured and discovered MCP source.'}
logger.info(f'Calling MCP tool `{tool_name}` on MCP server `{server_name_found}`')
try:
# Use the persistent client for this tool call
client = self.mcp_clients.get(server_name_found)
if not client:
logger.error(f'No persistent client found for MCP source `{server_name_found}`')
return {'error': f'MCP source `{server_name_found}` is not currently connected.'}
# Execute the tool using the persistent client (no context manager needed - already connected)
result = await asyncio.wait_for(
client.call_tool(tool_name, tool_input),
timeout=120.0 # FIXME: Make configurable
)
logger.debug(f'Tool `{tool_name}` executed via MCP SDK.', result_content=result)
# Check for errors in CallToolResult
if hasattr(result, 'is_error') and result.is_error:
error_msg = 'Unknown error'
if hasattr(result, 'content') and result.content:
# Try to extract error message from content
error_parts = []
for content in result.content:
if hasattr(content, 'text'):
error_parts.append(content.text)
error_msg = '\n'.join(error_parts) if error_parts else str(result)
return {'error': f'Tool execution returned error: {error_msg}'}
# Extract content from CallToolResult object
# CallToolResult has: content (list), structured_content (dict), data (any), is_error (bool)
text_content = []
# First, try to extract from result.content (list of content objects)
if hasattr(result, 'content') and isinstance(result.content, list) and len(result.content) > 0:
for content in result.content:
if hasattr(content, 'text'):
text_content.append(str(content.text))
elif hasattr(content, 'type') and content.type == 'text' and hasattr(content, 'text'):
text_content.append(str(content.text))
else:
text_content.append(str(content))
# If no content extracted, try result.data
if not text_content and hasattr(result, 'data') and result.data is not None:
text_content.append(str(result.data))
# If still no content, try result.structured_content
if not text_content and hasattr(result, 'structured_content') and result.structured_content:
# Format structured content as JSON
text_content.append(json.dumps(result.structured_content, indent=2))
# Fallback: convert the whole result to string
if not text_content:
text_content.append(str(result))
return {'content': '\n'.join(text_content)}
except asyncio.TimeoutError:
logger.error(f'Timeout calling MCP tool `{tool_name}` on server `{server_name_found}`', tool_input=tool_input)
return {'error': f'Tool call `{tool_name}` timed out.'}
except McpError as mcp_err:
# Catch MCP-specific errors
logger.error(f'MCP error during tool call `{tool_name}` on server `{server_name_found}`: {type(mcp_err).__name__}', tool_input=tool_input, error=str(mcp_err))
# Remove the server from discovered list; the discovery task will handle rediscovery
if server_name_found in self.mcp_clients:
del self.mcp_clients[server_name_found]
if server_name_found in self.mcp_urls:
del self.mcp_urls[server_name_found]
if server_name_found in self.mcp_tools:
del self.mcp_tools[server_name_found]
return {'error': f'MCP error executing tool `{tool_name}`: {str(mcp_err)}'}
except ConnectionError as conn_err:
# Catch connection-related errors during the tool call
logger.error(f'Connection error during MCP tool call `{tool_name}` on server `{server_name_found}`: {type(conn_err).__name__}', tool_input=tool_input, error=str(conn_err))
# Remove the server from discovered list; the discovery task will handle rediscovery
if server_name_found in self.mcp_clients:
del self.mcp_clients[server_name_found]
if server_name_found in self.mcp_urls:
del self.mcp_urls[server_name_found]
if server_name_found in self.mcp_tools:
del self.mcp_tools[server_name_found]
return {'error': f'Connection error executing MCP tool `{tool_name}`. The server may be temporarily unavailable. Please try again.'}
except Exception as e:
logger.exception(f'Unexpected error calling tool `{tool_name}` on server `{server_name_found}`', tool_input=tool_input)
return {'error': f'Error calling tool `{tool_name}`: {str(e)}'}
def _map_mcp_type_to_json_type(self, mcp_type: str) -> str:
'''Maps MCP parameter types to JSON Schema types.'''
# Based on the official MCP SDK's Tool.inputSchema structure
type_map = {
'string': 'string',
'integer': 'integer',
'number': 'number',
'boolean': 'boolean',
'array': 'array',
'object': 'object',
}
json_type = type_map.get(mcp_type.lower(), 'string') # Default to string if unknown
if json_type != mcp_type.lower():
logger.debug(f'Mapped MCP type `{mcp_type}` to JSON type `{json_type}`.')
return json_type
async def format_tools_for_openai(self) -> list[dict[str, Any]]:
'''
Format active MCP tools and the RSS query tool for OpenAI API.
'''
openai_tools = []
active_server_names = list(self.mcp_urls.keys())
for server_name in active_server_names:
if server_name not in self.mcp_tools:
logger.warning(f'Server `{server_name}` is connected but has no tools listed. Skipping for OpenAI format.', server_name=server_name)
continue
tool_defs: list[Tool] = self.mcp_tools[server_name]
logger.debug(f'Formatting {len(tool_defs)} MCP tools from active server `{server_name}` for OpenAI.')
# Debug: Log tool structure for troubleshooting
for i, tool in enumerate(tool_defs):
logger.debug(f'Tool {i} from {server_name}: name={getattr(tool, "name", "NO_NAME")}, title={getattr(tool, "title", "NO_TITLE")}, description={getattr(tool, "description", "NO_DESC")[:50]}...')
for tool in tool_defs:
# Validate Tool structure (basic check)
# Handle both fastmcp.tools.Tool and mcp.types.Tool
if not (isinstance(tool, Tool) or hasattr(tool, 'name')):
logger.warning(f'Skipping non-Tool object from server `{server_name}`', tool_data=tool)
continue
# Check if tool has a name (required for OpenAI function calling)
if not hasattr(tool, 'name') or not tool.name:
logger.warning(f'Skipping nameless Tool from server `{server_name}`', tool_data=tool)
continue
# Log successful tool processing for debugging
logger.debug(f'Successfully processing tool `{tool.name}` from server `{server_name}`')
# Build JSON schema for parameters from tool.inputSchema
properties = {}
required_params = []
if tool.inputSchema and hasattr(tool.inputSchema, 'properties'):
schema_props = tool.inputSchema.properties
if hasattr(schema_props, 'items'):
# Handle array-like properties
for prop_name, prop_schema in schema_props.items():
param_schema = {
'type': self._map_mcp_type_to_json_type(prop_schema.type if hasattr(prop_schema, 'type') else 'string'),
'description': getattr(prop_schema, 'description', f'Parameter {prop_name}')
}
# Add enum if present
if hasattr(prop_schema, 'enum') and prop_schema.enum:
param_schema['enum'] = prop_schema.enum
# Add default if present
if hasattr(prop_schema, 'default') and prop_schema.default is not None:
param_schema['default'] = prop_schema.default
properties[prop_name] = param_schema
# Check for required flag
if hasattr(prop_schema, 'required') and prop_schema.required:
required_params.append(prop_name)
else:
# Fallback: create a simple schema if inputSchema is not available
logger.debug(f'Tool `{tool.name}` has no inputSchema, using fallback schema.')
properties = {}
required_params = []
# Final JSON Schema for the tool
parameters_schema = {
'type': 'object',
'properties': properties,
}
if required_params:
parameters_schema['required'] = required_params
# OpenAI Tool structure
openai_tool = {
'type': 'function',
'function': {
'name': tool.name,
'description': tool.description or f'Executes the {tool.name} tool.',
'parameters': parameters_schema,
},
}
openai_tools.append(openai_tool)
# Log summary of tools processed for this server
logger.info(f'Successfully processed {len([t for t in tool_defs if hasattr(t, "name") and t.name])} tools from server `{server_name}`')
# Format RSS Query Tool
if FEEDPARSER_AVAILABLE and self.b4a_data.rss_sources:
rss_feed_names = [conf.name for conf in self.b4a_data.rss_sources]
if rss_feed_names:
logger.debug(f'Formatting RSS query tool for {len(rss_feed_names)} feeds.')
rss_tool_def = {
'type': 'function',
'function': {
'name': 'query_rss_feed',
'description': 'Retrieves the latest entries from a configured RSS feed. Can optionally filter entries by a query string in the title or summary, and by timestamp to get only entries published since a given time.',
'parameters': {
'type': 'object',
'properties': {
'feed_name': {
'type': 'string',
'description': 'The name of the configured RSS feed to query.',
'enum': rss_feed_names # Use loaded feed names
},
'query': {
'type': 'string',
'description': 'Optional: A keyword or phrase to search for in entry titles or summaries.'
},
'limit': {
'type': 'integer',
'description': 'Optional: The maximum number of entries to return (default is 5).'
},
'since_timestamp': {
'type': 'number',
'description': 'Optional: Unix timestamp. Only return entries published/updated since this time. Useful for avoiding duplicate content in repeated queries.'
}
},
'required': ['feed_name'] # Only feed_name is strictly required
}
}
}
openai_tools.append(rss_tool_def)
else:
logger.debug('No RSS feed names found, skipping RSS tool formatting.')
elif not FEEDPARSER_AVAILABLE and self.b4a_data.rss_sources:
logger.warning('RSS sources configured but `feedparser` not installed. RSS tool disabled.')
return openai_tools
async def _handle_chat_logic(
self,
sendable: commands.Context | discord.Interaction | discord_abc.Messageable,
message: str,
channel_id: int,
user_id: int,
stream: bool,
attachments: list[discord.Attachment] | None = None
):
'''
Core logic for handling chat interactions, LLM calls, and tool execution.
Uses PGVector/in-memory history and handles MCP/RSS tools.
'''
# Determine if we need to use followup/ephemeral (only for Interactions)
is_interaction = isinstance(sendable, discord.Interaction)
send_followup = is_interaction and sendable.response.is_done()
# Ephemeral only makes sense for interactions
can_be_ephemeral = is_interaction
processed_message = message # Start with the original text message
if attachments:
logger.info(f'Processing {len(attachments)} attachments for channel {channel_id}', user_id=user_id)
processed_message = await handle_attachments(attachments, message)
try:
# Get and update history
channel_history = await self._get_channel_history(channel_id)
user_message_dict = {'role': 'user', 'content': processed_message}
channel_history.append(user_message_dict)
logger.debug(f'User message added to history for channel {channel_id}')
# Store user message to history provider
try:
metadata = {'user_id': str(user_id), 'discord_role': 'user'}
await self.history_provider.add_message(
channel_id=channel_id,
role='user',
content=processed_message,
metadata=metadata
)
except Exception as e:
logger.exception(f'Failed to store user message to history provider for channel {channel_id}')
openai_tools = await self.format_tools_for_openai() # Gets both MCP and RSS tools
if openai_tools:
extra_chat_params = {
'tools': openai_tools,
}
# Only include tool_choice for providers that support it (OpenAI/generic, not Claude)
if self.api_type in ['openai', 'generic']:
extra_chat_params['tool_choice'] = 'auto'
logger.debug(f'Including {len(openai_tools)} tools in LLM call for channel {channel_id}')
else:
extra_chat_params = {}
logger.debug(f'No active tools available for LLM call for channel {channel_id}')
# Format the system message dynamically
# For standing prompts, we need to pass the last execution timestamp
last_execution_time = 0.0
if hasattr(self, '_current_standing_prompt') and self._current_standing_prompt:
last_execution_time = self._current_standing_prompt.last_execution_timestamp
current_system_message = get_formatted_sysmsg(self.sysmsg_template, str(user_id), last_execution_time)
if self.sys_postscript:
current_system_message += f'\n\n{self.sys_postscript}'
replace_system_prompt(channel_history, current_system_message)
print(channel_history, flush=True)
try:
# Pass the dynamically formatted system message to the LLM client
# The channel_history should NOT contain a system message itself.
initial_response_content, tool_calls_aggregated = await self.llm_client(
channel_history,
str(user_id), # For OpenAI 'user' field
extra_chat_params,
stream=stream
)
except (MissingLLMResponseError, LLMResponseError) as llm_err:
await send_long_message(sendable, llm_err.message, followup=send_followup, ephemeral=can_be_ephemeral)
return
except Exception as e: # Catch other unexpected errors (e.g., network)
logger.exception('Unexpected error during LLM communication', channel_id=channel_id, user_id=user_id)
await send_long_message(sendable, f'⚠️ Unexpected error communicating with AI: {str(e)}', followup=send_followup, ephemeral=can_be_ephemeral)
return
# Send Initial Response & Update History
assistant_message_dict: dict[str, Any] = {'role': 'assistant', 'content': None}
sent_initial_message = False
if initial_response_content.strip():
logger.debug(f'Sending initial LLM response to channel {channel_id}', length=len(initial_response_content), stream=stream)
await send_long_message(sendable, initial_response_content, followup=send_followup)
assistant_message_dict['content'] = initial_response_content # Ephemeral handled by send_long_message
sent_initial_message = True
send_followup = True # Any subsequent messages MUST be followups
if tool_calls_aggregated:
assistant_message_dict['tool_calls'] = tool_calls_aggregated
logger.info(f'LLM requested {len(tool_calls_aggregated)} tool call(s) for channel {channel_id}', tool_names=[tc['function']['name'] for tc in tool_calls_aggregated], stream=stream)
if assistant_message_dict.get('content') or assistant_message_dict.get('tool_calls'):
channel_history.append(assistant_message_dict)
sent_initial_message = True # Flag if we added *something* from the assistant
# Store assistant message to history provider
try:
# Ensure content is not None before insertion
db_content = assistant_message_dict.get('content')
content_to_insert = db_content if db_content is not None else ''
metadata = {'user_id': str(self.bot.user.id), 'discord_role': 'assistant'}
if assistant_message_dict.get('tool_calls'):
metadata['tool_calls'] = assistant_message_dict['tool_calls'] # Store tool calls in metadata
await self.history_provider.add_message(
channel_id=channel_id,
role='assistant',
content=content_to_insert,
metadata=metadata
)
except Exception as e:
logger.exception(f'Failed to store assistant message to history provider for channel {channel_id}')
elif not sent_initial_message:
logger.info(f'LLM call finished for channel {channel_id} with no text/tools.', stream=stream)
await send_long_message(sendable, 'I received your message but didn\'t have anything specific to add or do.', followup=send_followup, ephemeral=can_be_ephemeral)
return
# Process Tool Calls
async def tool_call_result_hook(tool_name, tool_call_id, tool_result_content_formatted, exc=None):
'''
Hook to handle tool call results and errors. Called for each tool call in the aggregated list.
'''
nonlocal send_followup
send_followup = True
if exc:
await send_long_message(sendable, f'⚠️ Error during tool call `{tool_name}` execution: {str(exc)}', followup=send_followup, ephemeral=can_be_ephemeral)
else:
await send_long_message(sendable, f'```Tool Call: {tool_name}\nResult:\n{tool_result_content_formatted}```', followup=True)
# Store tool result to history provider
try:
metadata = {'user_id': 'tool_executor', 'discord_role': 'tool', 'tool_name': tool_name, 'tool_call_id': tool_call_id}
await self.history_provider.add_message(
channel_id=channel_id,
role='tool',
content=tool_result_content_formatted,
metadata=metadata
)
except Exception as e:
logger.exception(f'Failed to store tool result ({tool_name}) to history provider for channel {channel_id}')
if tool_calls_aggregated:
try:
follow_up_text = await self.llm_client.process_tool_calls(
tool_calls_aggregated, channel_history, channel_id, str(user_id), tool_call_result_hook, stream)
# Send Follow-up & Update History
if follow_up_text.strip():
logger.debug(f'Sending follow-up LLM response to channel {channel_id}', length=len(follow_up_text), stream=stream)
await send_long_message(sendable, follow_up_text, followup=True) # Must be followup
follow_up_dict = {'role': 'assistant', 'content': follow_up_text}
channel_history.append(follow_up_dict)
# Store follow-up message to history provider
try:
metadata = {'user_id': str(self.bot.user.id), 'discord_role': 'assistant'}
await self.history_provider.add_message(
channel_id=channel_id,
role='assistant',
content=follow_up_text,
metadata=metadata
)
except Exception as e:
logger.exception(f'Failed to store follow-up assistant message to history provider for channel {channel_id}')
else:
logger.info(f'LLM follow-up call finished for channel {channel_id} with no text content.', stream=stream)
except MissingLLMResponseError as llm_err:
await send_long_message(sendable, llm_err.message, followup=True, ephemeral=can_be_ephemeral)
return
except Exception as e:
await send_long_message(sendable, f'⚠️ Unexpected error getting follow-up AI response: {str(e)}', followup=True, ephemeral=can_be_ephemeral)
return
except Exception as e:
logger.exception(f'Unhandled error during chat logic execution for channel {channel_id}', user_id=user_id, stream=stream)
try:
error_message = 'An unexpected error occurred while processing your request. Please check the logs.'
# Send ephemeral only if it's an interaction
await send_long_message(sendable, error_message, followup=send_followup, ephemeral=is_interaction)
except Exception as send_err:
logger.error('Failed to send error message back to user after main chat logic failure', send_error=send_err)
# Event Listeners
@commands.Cog.listener()
async def on_ready(self):
''' Cog ready listener. Connection tasks are started in cog_load. '''
logger.info(f'Cog {self.__class__.__name__} is ready.')
# Log PGVector status after cog is ready and init attempted
if self.pgvector_config.get('enabled', False):
if self.pgvector_enabled: logger.info('PGVector history persistence is active.')
else: logger.warning('PGVector history persistence was configured but failed to initialize. History disabled.')
else: logger.info('Using in-memory chat history.')
# Log RSS status
if self.b4a_data.rss_sources:
if FEEDPARSER_AVAILABLE: logger.info('RSS query tool is available.')
else: logger.warning('RSS sources configured but `feedparser` not installed. RSS tool disabled.')
@commands.Cog.listener()
async def on_message(self, message: discord.Message):
''' Listener to handle direct messages (DMs) to the bot. '''
# Ignore messages from bots (including self)
if message.author.bot:
return
# Only process messages in DMs (where message.guild is None)
if message.guild is not None:
return
# Ignore messages that start with typical command prefixes
# to avoid potential conflicts if user accidentally uses one in DM
# You might adjust this list based on other bots or expected usage
common_prefixes = ['!', '/', '$', '%', '?', '.', ',']
if any(message.content.startswith(p) for p in common_prefixes):
# Optionally, you could inform the user prefixes aren't needed in DMs
# await message.channel.send('You don't need a prefix when talking to me in DMs!')
logger.debug(f'Ignoring DM from {message.author} starting with a common prefix: {message.content[:10]}...')
return
# Check for empty message content AND no attachments
if not message.content.strip() and not message.attachments:
logger.debug(f'Ignoring empty DM from {message.author}')
# Avoid sending 'provide a message' error for empty DMs, just ignore.
return
logger.info(f'Received DM from {message.author} ({message.author.id}) in channel {message.channel.id}')
# Use typing context manager for user feedback in the DM channel
async with message.channel.typing():
await self._handle_chat_logic(
sendable=message.channel, # Pass the DMChannel directly
message=message.content,
channel_id=message.channel.id,
user_id=message.author.id,
stream=False, # Defaulting DMs to non-streaming for simplicity
attachments=message.attachments
)
# Discord Commands (mcp_list, chat_command, chat_slash)
@commands.hybrid_command(name='context_info', description='list configured B4A (model context) sources and their status')
async def context_info(self, ctx: commands.Context):
''' Command to list configured B4A sources and MCP connection status. '''
logger.info(f'Command context_info invoked by {ctx.author}')
# Defer for both slash and prefix commands
if isinstance(ctx, commands.Context) and ctx.interaction:
await ctx.interaction.response.defer(thinking=True, ephemeral=False)
elif isinstance(ctx, commands.Context):
async with ctx.typing():
pass # Show typing for prefix commands
message = '**B4A Sources Status:**\n\n'
# MCP Sources
message += f'**MCP Sources (@mcp): {len(self.b4a_data.mcp_sources)} configured**\n'
if not self.b4a_data.mcp_sources: message += ' *None configured or loaded.*\n'
for mcp_conf in self.b4a_data.mcp_sources:
name = mcp_conf.name
status_icon = '❓'
status_text = 'Unknown'
if name in self.mcp_urls and name in self.mcp_tools:
status_icon = '🟢'; status_text = f'Connected ({len(self.mcp_tools[name])} tools)'
elif name in self.mcp_urls:
status_icon = '🟡'; status_text = 'Connected (Tool list issue?)'
elif name in self._connection_tasks and not self._connection_tasks[name].done():
status_icon = '🟠'; status_text = 'Connecting…'
else:
status_icon = '🔴'; status_text = 'Disconnected / Failed' # Assumes task finished if not connecting
message += f'- **{name}**: {status_icon} {status_text} ({mcp_conf.url})\n'
message += '\n'
# RSS Sources
# Update status based on FEEDPARSER_AVAILABLE
message += f'**RSS Sources (@rss): {len(self.b4a_data.rss_sources)} configured**\n'
if self.b4a_data.rss_sources:
for rss_conf in self.b4a_data.rss_sources:
if FEEDPARSER_AVAILABLE: status_icon = '🟢'; status_text = 'Active (Tool Available)'
else: status_icon = '⚠️'; status_text = 'Inactive (`feedparser` missing)'
message += f'- **{rss_conf.name}**: {status_icon} {status_text} ({rss_conf.url})\n'
else:
message += ' *None configured or loaded.*\n'
message += '\n'
# Unhandled Sources
if self.b4a_data.unhandled_sources:
message += f'**Unhandled Sources: {len(self.b4a_data.unhandled_sources)} found**\n'
for src in self.b4a_data.unhandled_sources:
name = src.get('_sourcename', 'Unknown Name')
type = src.get('type', 'Unknown Type')
reason = src.get('_reason', 'No handler')
message += f'- **{name}**: ⚠️ Type \'{type}\' - {reason}\n'
message += '\n'
# Loading Errors
if self.b4a_data.load_errors:
message += f'**Loading Errors: {len(self.b4a_data.load_errors)} encountered**\n'
for path, err in self.b4a_data.load_errors[:5]: # Show first 5 errors
message += f'- `{os.path.basename(path)}`: {err[:150]}…\n'
if len(self.b4a_data.load_errors) > 5: message += '- … (see logs for more details)\n'
message += '\n'
# PGVector Status
message += '**Chat History Storage:**\n'
if self.pgvector_config.get('enabled', False):
if self.pgvector_enabled: message += '- 🟢 PGVector: **Enabled and Active**\n'
else: message += '- 🔴 PGVector: **Configured but FAILED to initialize** (Check logs)\n'
else: message += '- ⚪ In-Memory: **Enabled** (PGVector not configured or disabled)\n'
# Send message (works for both slash and prefix)
await ctx.send(message.strip() or 'No B4A sources found or configured.')