Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion gateway/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2359,8 +2359,34 @@ async def _handle_message_with_agent(self, event, source, _quick_key: str):
except Exception:
pass

available_tool_names = None
try:
user_config = _load_gateway_config()
platform_key = _platform_config_key(source.platform)

from hermes_cli.tools_config import _get_platform_tools
from tools.registry import registry
from toolsets import resolve_toolset, validate_toolset

enabled_toolsets = sorted(_get_platform_tools(user_config, platform_key))
tools_to_include: set[str] = set()
for toolset_name in enabled_toolsets:
if validate_toolset(toolset_name):
tools_to_include.update(resolve_toolset(toolset_name))

available_tool_names = {
tool_def["function"]["name"]
for tool_def in registry.get_definitions(tools_to_include, quiet=True)
}
except Exception:
available_tool_names = None

# Build the context prompt to inject
context_prompt = build_session_context_prompt(context, redact_pii=_redact_pii)
context_prompt = build_session_context_prompt(
context,
redact_pii=_redact_pii,
available_tool_names=available_tool_names,
)

# If the previous session expired and was auto-reset, prepend a notice
# so the agent knows this is a fresh conversation (not an intentional /reset).
Expand Down
40 changes: 35 additions & 5 deletions gateway/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,42 @@ def to_dict(self) -> Dict[str, Any]:
and the LLM needs the real ID to tag users."""


def _has_scoped_slack_tools(available_tool_names: Optional[set[str]]) -> bool:
"""Return True when this session exposes dedicated Slack-scoped tools."""
if not available_tool_names:
return False

return any(
name.startswith(("mcp_slack_", "slack_"))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This predicate uses the pre-June MCP name form. Current MCP registration is mcp__<server>__<tool> (tools/mcp_tool.py:4466-4484, commit e01f58ff1fde), so mcp__slack__conversations_history will not match and the conservative note remains. Please use the canonical naming contract and update the test fixture accordingly.

for name in available_tool_names
)


def _build_slack_platform_note(has_scoped_tools: bool) -> str:
"""Build the Slack platform note for the current tool surface."""
if not has_scoped_tools:
return (
"**Platform notes:** You are running inside Slack. "
"You do NOT have access to Slack-specific APIs — you cannot search "
"channel history, pin/unpin messages, manage channels, or list users. "
"Do not promise to perform these actions. If the user asks, explain "
"that you can only read messages sent directly to you and respond."
)

return (
"**Platform notes:** You are running inside Slack. "
"Dedicated Slack tools may be available in this session for Slack-specific "
"requests such as permalink, thread, and history lookups. Use those "
"scoped tools when they are available, and do not promise Slack actions "
"beyond the capabilities exposed by those tools."
)


def build_session_context_prompt(
context: SessionContext,
*,
redact_pii: bool = False,
available_tool_names: Optional[set[str]] = None,
) -> str:
"""
Build the dynamic system prompt section that tells the agent about its context.
Expand Down Expand Up @@ -282,11 +314,9 @@ def build_session_context_prompt(
if context.source.platform == Platform.SLACK:
lines.append("")
lines.append(
"**Platform notes:** You are running inside Slack. "
"You do NOT have access to Slack-specific APIs — you cannot search "
"channel history, pin/unpin messages, manage channels, or list users. "
"Do not promise to perform these actions. If the user asks, explain "
"that you can only read messages sent directly to you and respond."
_build_slack_platform_note(
_has_scoped_slack_tools(available_tool_names)
)
)
elif context.source.platform == Platform.DISCORD:
lines.append("")
Expand Down
35 changes: 33 additions & 2 deletions tests/gateway/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def test_discord_prompt(self):
assert "Discord" in prompt
assert "cannot search" in prompt.lower() or "do not have access" in prompt.lower()

def test_slack_prompt_includes_platform_notes(self):
def test_slack_prompt_preserves_conservative_note_without_scoped_tools(self):
config = GatewayConfig(
platforms={
Platform.SLACK: PlatformConfig(enabled=True, token="fake"),
Expand All @@ -213,11 +213,42 @@ def test_slack_prompt_includes_platform_notes(self):
user_name="bob",
)
ctx = build_session_context(source, config)
prompt = build_session_context_prompt(ctx)
prompt = build_session_context_prompt(ctx, available_tool_names=set())

assert "Slack" in prompt
assert "cannot search" in prompt.lower()
assert "pin" in prompt.lower()
assert "only read messages sent directly to you" in prompt

def test_slack_prompt_mentions_scoped_tools_when_available(self):
config = GatewayConfig(
platforms={
Platform.SLACK: PlatformConfig(enabled=True, token="fake"),
},
)
source = SessionSource(
platform=Platform.SLACK,
chat_id="C123",
chat_name="general",
chat_type="group",
user_name="bob",
)
ctx = build_session_context(source, config)
prompt = build_session_context_prompt(
ctx,
available_tool_names={
"mcp_slack_conversations_history",
"mcp_slack_conversations_replies",
},
)

assert "Slack" in prompt
assert "dedicated slack tools may be available" in prompt.lower()
assert "permalink, thread, and history lookups" in prompt.lower()
assert "only read messages sent directly to you" not in prompt
assert "cannot search channel history" not in prompt.lower()
assert "token" not in prompt.lower()
assert "curl" not in prompt.lower()

def test_discord_prompt_with_channel_topic(self):
"""Channel topic should appear in the session context prompt."""
Expand Down